]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/md/dm-raid.c
dm raid: rename variable 'ret' to 'r' to conform to other dm code
[mirror_ubuntu-artful-kernel.git] / drivers / md / dm-raid.c
CommitLineData
9d09e663
N
1/*
2 * Copyright (C) 2010-2011 Neil Brown
0cf45031 3 * Copyright (C) 2010-2015 Red Hat, Inc. All rights reserved.
9d09e663
N
4 *
5 * This file is released under the GPL.
6 */
7
8#include <linux/slab.h>
056075c7 9#include <linux/module.h>
9d09e663
N
10
11#include "md.h"
32737279 12#include "raid1.h"
9d09e663 13#include "raid5.h"
63f33b8d 14#include "raid10.h"
9d09e663
N
15#include "bitmap.h"
16
3e8dbb7f
AK
17#include <linux/device-mapper.h>
18
9d09e663 19#define DM_MSG_PREFIX "raid"
c76d53f4 20#define MAX_RAID_DEVICES 253 /* raid4/5/6 limit */
9d09e663 21
48cf06bc
HM
22static bool devices_handle_discard_safely = false;
23
9d09e663 24/*
b12d437b
JB
25 * 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.
9d09e663 27 */
b12d437b 28#define FirstUse 10 /* rdev flag */
9d09e663
N
29
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;
3cb03002 45 struct md_rdev rdev;
9d09e663
N
46};
47
48/*
c76d53f4 49 * Flags for rs->ctr_flags field.
9d09e663 50 */
c76d53f4
HM
51#define CTR_FLAG_SYNC 0x1
52#define CTR_FLAG_NOSYNC 0x2
53#define CTR_FLAG_REBUILD 0x4
54#define CTR_FLAG_DAEMON_SLEEP 0x8
55#define CTR_FLAG_MIN_RECOVERY_RATE 0x10
56#define CTR_FLAG_MAX_RECOVERY_RATE 0x20
57#define CTR_FLAG_MAX_WRITE_BEHIND 0x40
58#define CTR_FLAG_STRIPE_CACHE 0x80
59#define CTR_FLAG_REGION_SIZE 0x100
60#define CTR_FLAG_RAID10_COPIES 0x200
61#define CTR_FLAG_RAID10_FORMAT 0x400
63f33b8d 62
9d09e663
N
63struct raid_set {
64 struct dm_target *ti;
65
34f8ac6d 66 uint32_t bitmap_loaded;
c76d53f4 67 uint32_t ctr_flags;
9d09e663 68
fd01b88c 69 struct mddev md;
9d09e663
N
70 struct raid_type *raid_type;
71 struct dm_target_callbacks callbacks;
72
73 struct raid_dev dev[0];
74};
75
76/* Supported raid types and properties. */
77static struct raid_type {
78 const char *name; /* RAID algorithm. */
79 const char *descr; /* Descriptor text for logging. */
80 const unsigned parity_devs; /* # of parity devices. */
81 const unsigned minimal_devs; /* minimal # of devices in set. */
82 const unsigned level; /* RAID level. */
83 const unsigned algorithm; /* RAID algorithm. */
84} raid_types[] = {
0cf45031 85 {"raid0", "RAID0 (striping)", 0, 2, 0, 0 /* NONE */},
32737279 86 {"raid1", "RAID1 (mirroring)", 0, 2, 1, 0 /* NONE */},
63f33b8d 87 {"raid10", "RAID10 (striped mirrors)", 0, 2, 10, UINT_MAX /* Varies */},
9d09e663
N
88 {"raid4", "RAID4 (dedicated parity disk)", 1, 2, 5, ALGORITHM_PARITY_0},
89 {"raid5_la", "RAID5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
90 {"raid5_ra", "RAID5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
91 {"raid5_ls", "RAID5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
92 {"raid5_rs", "RAID5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
93 {"raid6_zr", "RAID6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
94 {"raid6_nr", "RAID6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
95 {"raid6_nc", "RAID6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
96};
97
fe5d2f4a
JB
98static char *raid10_md_layout_to_format(int layout)
99{
100 /*
101 * Bit 16 and 17 stand for "offset" and "use_far_sets"
102 * Refer to MD's raid10.c for details
103 */
104 if ((layout & 0x10000) && (layout & 0x20000))
105 return "offset";
106
107 if ((layout & 0xFF) > 1)
108 return "near";
109
110 return "far";
111}
112
63f33b8d
JB
113static unsigned raid10_md_layout_to_copies(int layout)
114{
fe5d2f4a
JB
115 if ((layout & 0xFF) > 1)
116 return layout & 0xFF;
117 return (layout >> 8) & 0xFF;
63f33b8d
JB
118}
119
120static int raid10_format_to_md_layout(char *format, unsigned copies)
121{
fe5d2f4a
JB
122 unsigned n = 1, f = 1;
123
c76d53f4 124 if (!strcasecmp("near", format))
fe5d2f4a
JB
125 n = copies;
126 else
127 f = copies;
128
c76d53f4 129 if (!strcasecmp("offset", format))
fe5d2f4a
JB
130 return 0x30000 | (f << 8) | n;
131
c76d53f4 132 if (!strcasecmp("far", format))
fe5d2f4a
JB
133 return 0x20000 | (f << 8) | n;
134
135 return (f << 8) | n;
63f33b8d
JB
136}
137
9d09e663
N
138static struct raid_type *get_raid_type(char *name)
139{
140 int i;
141
142 for (i = 0; i < ARRAY_SIZE(raid_types); i++)
143 if (!strcmp(raid_types[i].name, name))
144 return &raid_types[i];
145
146 return NULL;
147}
148
149static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
150{
151 unsigned i;
152 struct raid_set *rs;
9d09e663
N
153
154 if (raid_devs <= raid_type->parity_devs) {
155 ti->error = "Insufficient number of devices";
156 return ERR_PTR(-EINVAL);
157 }
158
9d09e663
N
159 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
160 if (!rs) {
161 ti->error = "Cannot allocate raid context";
162 return ERR_PTR(-ENOMEM);
163 }
164
165 mddev_init(&rs->md);
166
167 rs->ti = ti;
168 rs->raid_type = raid_type;
169 rs->md.raid_disks = raid_devs;
170 rs->md.level = raid_type->level;
171 rs->md.new_level = rs->md.level;
9d09e663
N
172 rs->md.layout = raid_type->algorithm;
173 rs->md.new_layout = rs->md.layout;
174 rs->md.delta_disks = 0;
175 rs->md.recovery_cp = 0;
176
177 for (i = 0; i < raid_devs; i++)
178 md_rdev_init(&rs->dev[i].rdev);
179
180 /*
181 * Remaining items to be initialized by further RAID params:
182 * rs->md.persistent
183 * rs->md.external
184 * rs->md.chunk_sectors
185 * rs->md.new_chunk_sectors
c039c332 186 * rs->md.dev_sectors
9d09e663
N
187 */
188
189 return rs;
190}
191
192static void context_free(struct raid_set *rs)
193{
194 int i;
195
b12d437b
JB
196 for (i = 0; i < rs->md.raid_disks; i++) {
197 if (rs->dev[i].meta_dev)
198 dm_put_device(rs->ti, rs->dev[i].meta_dev);
545c8795 199 md_rdev_clear(&rs->dev[i].rdev);
9d09e663
N
200 if (rs->dev[i].data_dev)
201 dm_put_device(rs->ti, rs->dev[i].data_dev);
b12d437b 202 }
9d09e663
N
203
204 kfree(rs);
205}
206
207/*
208 * For every device we have two words
209 * <meta_dev>: meta device name or '-' if missing
210 * <data_dev>: data device name or '-' if missing
211 *
b12d437b
JB
212 * The following are permitted:
213 * - -
214 * - <data_dev>
215 * <meta_dev> <data_dev>
216 *
217 * The following is not allowed:
218 * <meta_dev> -
219 *
220 * This code parses those words. If there is a failure,
221 * the caller must use context_free to unwind the operations.
9d09e663
N
222 */
223static int dev_parms(struct raid_set *rs, char **argv)
224{
225 int i;
226 int rebuild = 0;
227 int metadata_available = 0;
73c6f239 228 int r = 0;
9d09e663
N
229
230 for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {
231 rs->dev[i].rdev.raid_disk = i;
232
233 rs->dev[i].meta_dev = NULL;
234 rs->dev[i].data_dev = NULL;
235
236 /*
237 * There are no offsets, since there is a separate device
238 * for data and metadata.
239 */
240 rs->dev[i].rdev.data_offset = 0;
241 rs->dev[i].rdev.mddev = &rs->md;
242
243 if (strcmp(argv[0], "-")) {
73c6f239 244 r = dm_get_device(rs->ti, argv[0],
b12d437b
JB
245 dm_table_get_mode(rs->ti->table),
246 &rs->dev[i].meta_dev);
247 rs->ti->error = "RAID metadata device lookup failure";
73c6f239
HM
248 if (r)
249 return r;
b12d437b
JB
250
251 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
252 if (!rs->dev[i].rdev.sb_page)
253 return -ENOMEM;
9d09e663
N
254 }
255
256 if (!strcmp(argv[1], "-")) {
257 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
258 (!rs->dev[i].rdev.recovery_offset)) {
259 rs->ti->error = "Drive designated for rebuild not specified";
260 return -EINVAL;
261 }
262
b12d437b
JB
263 rs->ti->error = "No data device supplied with metadata device";
264 if (rs->dev[i].meta_dev)
265 return -EINVAL;
266
9d09e663
N
267 continue;
268 }
269
73c6f239 270 r = dm_get_device(rs->ti, argv[1],
9d09e663
N
271 dm_table_get_mode(rs->ti->table),
272 &rs->dev[i].data_dev);
73c6f239 273 if (r) {
9d09e663 274 rs->ti->error = "RAID device lookup failure";
73c6f239 275 return r;
9d09e663
N
276 }
277
b12d437b
JB
278 if (rs->dev[i].meta_dev) {
279 metadata_available = 1;
280 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
281 }
9d09e663
N
282 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
283 list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
284 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
285 rebuild++;
286 }
287
288 if (metadata_available) {
289 rs->md.external = 0;
290 rs->md.persistent = 1;
291 rs->md.major_version = 2;
292 } else if (rebuild && !rs->md.recovery_cp) {
293 /*
294 * Without metadata, we will not be able to tell if the array
295 * is in-sync or not - we must assume it is not. Therefore,
296 * it is impossible to rebuild a drive.
297 *
298 * Even if there is metadata, the on-disk information may
299 * indicate that the array is not in-sync and it will then
300 * fail at that time.
301 *
302 * User could specify 'nosync' option if desperate.
303 */
304 DMERR("Unable to rebuild drive while array is not in-sync");
305 rs->ti->error = "RAID device lookup failure";
306 return -EINVAL;
307 }
308
309 return 0;
310}
311
c1084561
JB
312/*
313 * validate_region_size
314 * @rs
315 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
316 *
317 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
318 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
319 *
320 * Returns: 0 on success, -EINVAL on failure.
321 */
322static int validate_region_size(struct raid_set *rs, unsigned long region_size)
323{
324 unsigned long min_region_size = rs->ti->len / (1 << 21);
325
326 if (!region_size) {
327 /*
328 * Choose a reasonable default. All figures in sectors.
329 */
330 if (min_region_size > (1 << 13)) {
3a0f9aae 331 /* If not a power of 2, make it the next power of 2 */
042745ee 332 region_size = roundup_pow_of_two(min_region_size);
c1084561
JB
333 DMINFO("Choosing default region size of %lu sectors",
334 region_size);
c1084561
JB
335 } else {
336 DMINFO("Choosing default region size of 4MiB");
337 region_size = 1 << 13; /* sectors */
338 }
339 } else {
340 /*
341 * Validate user-supplied value.
342 */
343 if (region_size > rs->ti->len) {
344 rs->ti->error = "Supplied region size is too large";
345 return -EINVAL;
346 }
347
348 if (region_size < min_region_size) {
349 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
350 region_size, min_region_size);
351 rs->ti->error = "Supplied region size is too small";
352 return -EINVAL;
353 }
354
355 if (!is_power_of_2(region_size)) {
356 rs->ti->error = "Region size is not a power of 2";
357 return -EINVAL;
358 }
359
360 if (region_size < rs->md.chunk_sectors) {
361 rs->ti->error = "Region size is smaller than the chunk size";
362 return -EINVAL;
363 }
364 }
365
366 /*
367 * Convert sectors to bytes.
368 */
369 rs->md.bitmap_info.chunksize = (region_size << 9);
370
371 return 0;
372}
373
eb649123 374/*
55ebbb59 375 * validate_raid_redundancy
eb649123
JB
376 * @rs
377 *
55ebbb59
JB
378 * Determine if there are enough devices in the array that haven't
379 * failed (or are being rebuilt) to form a usable array.
eb649123
JB
380 *
381 * Returns: 0 on success, -EINVAL on failure.
382 */
55ebbb59 383static int validate_raid_redundancy(struct raid_set *rs)
eb649123
JB
384{
385 unsigned i, rebuild_cnt = 0;
3f6bbd3f 386 unsigned rebuilds_per_group = 0, copies, d;
fe5d2f4a 387 unsigned group_size, last_group_start;
eb649123 388
eb649123 389 for (i = 0; i < rs->md.raid_disks; i++)
55ebbb59
JB
390 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
391 !rs->dev[i].rdev.sb_page)
eb649123
JB
392 rebuild_cnt++;
393
394 switch (rs->raid_type->level) {
395 case 1:
396 if (rebuild_cnt >= rs->md.raid_disks)
397 goto too_many;
398 break;
399 case 4:
400 case 5:
401 case 6:
402 if (rebuild_cnt > rs->raid_type->parity_devs)
403 goto too_many;
404 break;
405 case 10:
4ec1e369
JB
406 copies = raid10_md_layout_to_copies(rs->md.layout);
407 if (rebuild_cnt < copies)
408 break;
409
410 /*
411 * It is possible to have a higher rebuild count for RAID10,
412 * as long as the failed devices occur in different mirror
413 * groups (i.e. different stripes).
414 *
4ec1e369
JB
415 * When checking "near" format, make sure no adjacent devices
416 * have failed beyond what can be handled. In addition to the
417 * simple case where the number of devices is a multiple of the
418 * number of copies, we must also handle cases where the number
419 * of devices is not a multiple of the number of copies.
420 * E.g. dev1 dev2 dev3 dev4 dev5
421 * A A B B C
422 * C D D E E
423 */
fe5d2f4a
JB
424 if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
425 for (i = 0; i < rs->md.raid_disks * copies; i++) {
426 if (!(i % copies))
427 rebuilds_per_group = 0;
428 d = i % rs->md.raid_disks;
429 if ((!rs->dev[d].rdev.sb_page ||
430 !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
431 (++rebuilds_per_group >= copies))
432 goto too_many;
433 }
434 break;
435 }
436
437 /*
438 * When checking "far" and "offset" formats, we need to ensure
439 * that the device that holds its copy is not also dead or
440 * being rebuilt. (Note that "far" and "offset" formats only
441 * support two copies right now. These formats also only ever
442 * use the 'use_far_sets' variant.)
443 *
444 * This check is somewhat complicated by the need to account
445 * for arrays that are not a multiple of (far) copies. This
446 * results in the need to treat the last (potentially larger)
447 * set differently.
448 */
449 group_size = (rs->md.raid_disks / copies);
450 last_group_start = (rs->md.raid_disks / group_size) - 1;
451 last_group_start *= group_size;
452 for (i = 0; i < rs->md.raid_disks; i++) {
453 if (!(i % copies) && !(i > last_group_start))
55ebbb59 454 rebuilds_per_group = 0;
fe5d2f4a
JB
455 if ((!rs->dev[i].rdev.sb_page ||
456 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
4ec1e369 457 (++rebuilds_per_group >= copies))
fe5d2f4a 458 goto too_many;
4ec1e369
JB
459 }
460 break;
eb649123 461 default:
55ebbb59
JB
462 if (rebuild_cnt)
463 return -EINVAL;
eb649123
JB
464 }
465
466 return 0;
467
468too_many:
eb649123
JB
469 return -EINVAL;
470}
471
9d09e663
N
472/*
473 * Possible arguments are...
9d09e663
N
474 * <chunk_size> [optional_args]
475 *
32737279
JB
476 * Argument definitions
477 * <chunk_size> The number of sectors per disk that
478 * will form the "stripe"
479 * [[no]sync] Force or prevent recovery of the
480 * entire array
9d09e663 481 * [rebuild <idx>] Rebuild the drive indicated by the index
32737279
JB
482 * [daemon_sleep <ms>] Time between bitmap daemon work to
483 * clear bits
9d09e663
N
484 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
485 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
46bed2b5 486 * [write_mostly <idx>] Indicate a write mostly drive via index
9d09e663
N
487 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
488 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
c1084561 489 * [region_size <sectors>] Defines granularity of bitmap
63f33b8d
JB
490 *
491 * RAID10-only options:
492 * [raid10_copies <# copies>] Number of copies. (Default: 2)
fe5d2f4a 493 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
9d09e663
N
494 */
495static int parse_raid_params(struct raid_set *rs, char **argv,
496 unsigned num_raid_params)
497{
63f33b8d
JB
498 char *raid10_format = "near";
499 unsigned raid10_copies = 2;
eb649123 500 unsigned i;
c1084561 501 unsigned long value, region_size = 0;
c039c332 502 sector_t sectors_per_dev = rs->ti->len;
542f9038 503 sector_t max_io_len;
9d09e663
N
504 char *key;
505
506 /*
507 * First, parse the in-order required arguments
32737279 508 * "chunk_size" is the only argument of this type.
9d09e663 509 */
b29bebd6 510 if ((kstrtoul(argv[0], 10, &value) < 0)) {
9d09e663
N
511 rs->ti->error = "Bad chunk size";
512 return -EINVAL;
32737279
JB
513 } else if (rs->raid_type->level == 1) {
514 if (value)
515 DMERR("Ignoring chunk size parameter for RAID 1");
516 value = 0;
517 } else if (!is_power_of_2(value)) {
518 rs->ti->error = "Chunk size must be a power of 2";
519 return -EINVAL;
520 } else if (value < 8) {
521 rs->ti->error = "Chunk size value is too small";
522 return -EINVAL;
9d09e663
N
523 }
524
525 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
526 argv++;
527 num_raid_params--;
528
529 /*
b12d437b
JB
530 * We set each individual device as In_sync with a completed
531 * 'recovery_offset'. If there has been a device failure or
532 * replacement then one of the following cases applies:
533 *
534 * 1) User specifies 'rebuild'.
535 * - Device is reset when param is read.
536 * 2) A new device is supplied.
537 * - No matching superblock found, resets device.
538 * 3) Device failure was transient and returns on reload.
539 * - Failure noticed, resets device for bitmap replay.
540 * 4) Device hadn't completed recovery after previous failure.
541 * - Superblock is read and overrides recovery_offset.
542 *
543 * What is found in the superblocks of the devices is always
544 * authoritative, unless 'rebuild' or '[no]sync' was specified.
9d09e663 545 */
b12d437b 546 for (i = 0; i < rs->md.raid_disks; i++) {
9d09e663 547 set_bit(In_sync, &rs->dev[i].rdev.flags);
b12d437b
JB
548 rs->dev[i].rdev.recovery_offset = MaxSector;
549 }
9d09e663 550
b12d437b
JB
551 /*
552 * Second, parse the unordered optional arguments
553 */
9d09e663 554 for (i = 0; i < num_raid_params; i++) {
13c87583 555 if (!strcasecmp(argv[i], "nosync")) {
9d09e663 556 rs->md.recovery_cp = MaxSector;
c76d53f4 557 rs->ctr_flags |= CTR_FLAG_NOSYNC;
9d09e663
N
558 continue;
559 }
13c87583 560 if (!strcasecmp(argv[i], "sync")) {
9d09e663 561 rs->md.recovery_cp = 0;
c76d53f4 562 rs->ctr_flags |= CTR_FLAG_SYNC;
9d09e663
N
563 continue;
564 }
565
566 /* The rest of the optional arguments come in key/value pairs */
567 if ((i + 1) >= num_raid_params) {
568 rs->ti->error = "Wrong number of raid parameters given";
569 return -EINVAL;
570 }
571
572 key = argv[i++];
63f33b8d
JB
573
574 /* Parameters that take a string value are checked here. */
575 if (!strcasecmp(key, "raid10_format")) {
576 if (rs->raid_type->level != 10) {
577 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
578 return -EINVAL;
579 }
fe5d2f4a
JB
580 if (strcmp("near", argv[i]) &&
581 strcmp("far", argv[i]) &&
582 strcmp("offset", argv[i])) {
63f33b8d
JB
583 rs->ti->error = "Invalid 'raid10_format' value given";
584 return -EINVAL;
585 }
586 raid10_format = argv[i];
c76d53f4 587 rs->ctr_flags |= CTR_FLAG_RAID10_FORMAT;
63f33b8d
JB
588 continue;
589 }
590
b29bebd6 591 if (kstrtoul(argv[i], 10, &value) < 0) {
9d09e663
N
592 rs->ti->error = "Bad numerical argument given in raid params";
593 return -EINVAL;
594 }
595
63f33b8d 596 /* Parameters that take a numeric value are checked here */
13c87583 597 if (!strcasecmp(key, "rebuild")) {
7386199c 598 if (value >= rs->md.raid_disks) {
9d09e663
N
599 rs->ti->error = "Invalid rebuild index given";
600 return -EINVAL;
601 }
602 clear_bit(In_sync, &rs->dev[value].rdev.flags);
603 rs->dev[value].rdev.recovery_offset = 0;
c76d53f4 604 rs->ctr_flags |= CTR_FLAG_REBUILD;
46bed2b5
JB
605 } else if (!strcasecmp(key, "write_mostly")) {
606 if (rs->raid_type->level != 1) {
607 rs->ti->error = "write_mostly option is only valid for RAID1";
608 return -EINVAL;
609 }
82324809 610 if (value >= rs->md.raid_disks) {
46bed2b5
JB
611 rs->ti->error = "Invalid write_mostly drive index given";
612 return -EINVAL;
613 }
614 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
13c87583 615 } else if (!strcasecmp(key, "max_write_behind")) {
46bed2b5
JB
616 if (rs->raid_type->level != 1) {
617 rs->ti->error = "max_write_behind option is only valid for RAID1";
618 return -EINVAL;
619 }
c76d53f4 620 rs->ctr_flags |= CTR_FLAG_MAX_WRITE_BEHIND;
9d09e663
N
621
622 /*
623 * In device-mapper, we specify things in sectors, but
624 * MD records this value in kB
625 */
626 value /= 2;
627 if (value > COUNTER_MAX) {
628 rs->ti->error = "Max write-behind limit out of range";
629 return -EINVAL;
630 }
631 rs->md.bitmap_info.max_write_behind = value;
13c87583 632 } else if (!strcasecmp(key, "daemon_sleep")) {
c76d53f4 633 rs->ctr_flags |= CTR_FLAG_DAEMON_SLEEP;
9d09e663
N
634 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
635 rs->ti->error = "daemon sleep period out of range";
636 return -EINVAL;
637 }
638 rs->md.bitmap_info.daemon_sleep = value;
13c87583 639 } else if (!strcasecmp(key, "stripe_cache")) {
c76d53f4 640 rs->ctr_flags |= CTR_FLAG_STRIPE_CACHE;
9d09e663
N
641
642 /*
643 * In device-mapper, we specify things in sectors, but
644 * MD records this value in kB
645 */
646 value /= 2;
647
63f33b8d
JB
648 if ((rs->raid_type->level != 5) &&
649 (rs->raid_type->level != 6)) {
9d09e663
N
650 rs->ti->error = "Inappropriate argument: stripe_cache";
651 return -EINVAL;
652 }
653 if (raid5_set_cache_size(&rs->md, (int)value)) {
654 rs->ti->error = "Bad stripe_cache size";
655 return -EINVAL;
656 }
13c87583 657 } else if (!strcasecmp(key, "min_recovery_rate")) {
c76d53f4 658 rs->ctr_flags |= CTR_FLAG_MIN_RECOVERY_RATE;
9d09e663
N
659 if (value > INT_MAX) {
660 rs->ti->error = "min_recovery_rate out of range";
661 return -EINVAL;
662 }
663 rs->md.sync_speed_min = (int)value;
13c87583 664 } else if (!strcasecmp(key, "max_recovery_rate")) {
c76d53f4 665 rs->ctr_flags |= CTR_FLAG_MAX_RECOVERY_RATE;
9d09e663
N
666 if (value > INT_MAX) {
667 rs->ti->error = "max_recovery_rate out of range";
668 return -EINVAL;
669 }
670 rs->md.sync_speed_max = (int)value;
c1084561 671 } else if (!strcasecmp(key, "region_size")) {
c76d53f4 672 rs->ctr_flags |= CTR_FLAG_REGION_SIZE;
c1084561 673 region_size = value;
63f33b8d
JB
674 } else if (!strcasecmp(key, "raid10_copies") &&
675 (rs->raid_type->level == 10)) {
676 if ((value < 2) || (value > 0xFF)) {
677 rs->ti->error = "Bad value for 'raid10_copies'";
678 return -EINVAL;
679 }
c76d53f4 680 rs->ctr_flags |= CTR_FLAG_RAID10_COPIES;
63f33b8d 681 raid10_copies = value;
9d09e663
N
682 } else {
683 DMERR("Unable to parse RAID parameter: %s", key);
684 rs->ti->error = "Unable to parse RAID parameters";
685 return -EINVAL;
686 }
687 }
688
c1084561
JB
689 if (validate_region_size(rs, region_size))
690 return -EINVAL;
691
692 if (rs->md.chunk_sectors)
542f9038 693 max_io_len = rs->md.chunk_sectors;
c1084561 694 else
542f9038 695 max_io_len = region_size;
c1084561 696
542f9038
MS
697 if (dm_set_target_max_io_len(rs->ti, max_io_len))
698 return -EINVAL;
32737279 699
63f33b8d
JB
700 if (rs->raid_type->level == 10) {
701 if (raid10_copies > rs->md.raid_disks) {
702 rs->ti->error = "Not enough devices to satisfy specification";
703 return -EINVAL;
704 }
705
fe5d2f4a
JB
706 /*
707 * If the format is not "near", we only support
708 * two copies at the moment.
709 */
710 if (strcmp("near", raid10_format) && (raid10_copies > 2)) {
711 rs->ti->error = "Too many copies for given RAID10 format.";
712 return -EINVAL;
713 }
714
63f33b8d
JB
715 /* (Len * #mirrors) / #devices */
716 sectors_per_dev = rs->ti->len * raid10_copies;
717 sector_div(sectors_per_dev, rs->md.raid_disks);
718
719 rs->md.layout = raid10_format_to_md_layout(raid10_format,
720 raid10_copies);
721 rs->md.new_layout = rs->md.layout;
0cf45031 722 } else if ((!rs->raid_type->level || rs->raid_type->level > 1) &&
63f33b8d
JB
723 sector_div(sectors_per_dev,
724 (rs->md.raid_disks - rs->raid_type->parity_devs))) {
c039c332
JB
725 rs->ti->error = "Target length not divisible by number of data devices";
726 return -EINVAL;
727 }
728 rs->md.dev_sectors = sectors_per_dev;
729
9d09e663
N
730 /* Assume there are no metadata devices until the drives are parsed */
731 rs->md.persistent = 0;
732 rs->md.external = 1;
733
734 return 0;
735}
736
737static void do_table_event(struct work_struct *ws)
738{
739 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
740
741 dm_table_event(rs->ti->table);
742}
743
744static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
745{
746 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
747
5c675f83 748 return mddev_congested(&rs->md, bits);
9d09e663
N
749}
750
b12d437b
JB
751/*
752 * This structure is never routinely used by userspace, unlike md superblocks.
753 * Devices with this superblock should only ever be accessed via device-mapper.
754 */
755#define DM_RAID_MAGIC 0x64526D44
756struct dm_raid_superblock {
757 __le32 magic; /* "DmRd" */
758 __le32 features; /* Used to indicate possible future changes */
759
760 __le32 num_devices; /* Number of devices in this array. (Max 64) */
761 __le32 array_position; /* The position of this drive in the array */
762
763 __le64 events; /* Incremented by md when superblock updated */
764 __le64 failed_devices; /* Bit field of devices to indicate failures */
765
766 /*
767 * This offset tracks the progress of the repair or replacement of
768 * an individual drive.
769 */
770 __le64 disk_recovery_offset;
771
772 /*
773 * This offset tracks the progress of the initial array
774 * synchronisation/parity calculation.
775 */
776 __le64 array_resync_offset;
777
778 /*
779 * RAID characteristics
780 */
781 __le32 level;
782 __le32 layout;
783 __le32 stripe_sectors;
784
40d43c4b 785 /* Remainder of a logical block is zero-filled when writing (see super_sync()). */
b12d437b
JB
786} __packed;
787
3cb03002 788static int read_disk_sb(struct md_rdev *rdev, int size)
b12d437b
JB
789{
790 BUG_ON(!rdev->sb_page);
791
792 if (rdev->sb_loaded)
793 return 0;
794
796a5cf0 795 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, 1)) {
0447568f
JB
796 DMERR("Failed to read superblock of device at position %d",
797 rdev->raid_disk);
c32fb9e7 798 md_error(rdev->mddev, rdev);
b12d437b
JB
799 return -EINVAL;
800 }
801
802 rdev->sb_loaded = 1;
803
804 return 0;
805}
806
fd01b88c 807static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
b12d437b 808{
81f382f9 809 int i;
b12d437b
JB
810 uint64_t failed_devices;
811 struct dm_raid_superblock *sb;
81f382f9 812 struct raid_set *rs = container_of(mddev, struct raid_set, md);
b12d437b
JB
813
814 sb = page_address(rdev->sb_page);
815 failed_devices = le64_to_cpu(sb->failed_devices);
816
81f382f9
JB
817 for (i = 0; i < mddev->raid_disks; i++)
818 if (!rs->dev[i].data_dev ||
819 test_bit(Faulty, &(rs->dev[i].rdev.flags)))
820 failed_devices |= (1ULL << i);
b12d437b 821
40d43c4b 822 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
b12d437b
JB
823
824 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
825 sb->features = cpu_to_le32(0); /* No features yet */
826
827 sb->num_devices = cpu_to_le32(mddev->raid_disks);
828 sb->array_position = cpu_to_le32(rdev->raid_disk);
829
830 sb->events = cpu_to_le64(mddev->events);
831 sb->failed_devices = cpu_to_le64(failed_devices);
832
833 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
834 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
835
836 sb->level = cpu_to_le32(mddev->level);
837 sb->layout = cpu_to_le32(mddev->layout);
838 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
839}
840
841/*
842 * super_load
843 *
844 * This function creates a superblock if one is not found on the device
845 * and will decide which superblock to use if there's a choice.
846 *
847 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
848 */
3cb03002 849static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
b12d437b 850{
73c6f239 851 int r;
b12d437b
JB
852 struct dm_raid_superblock *sb;
853 struct dm_raid_superblock *refsb;
854 uint64_t events_sb, events_refsb;
855
856 rdev->sb_start = 0;
40d43c4b
HM
857 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
858 if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
859 DMERR("superblock size of a logical block is no longer valid");
860 return -EINVAL;
861 }
b12d437b 862
73c6f239
HM
863 r = read_disk_sb(rdev, rdev->sb_size);
864 if (r)
865 return r;
b12d437b
JB
866
867 sb = page_address(rdev->sb_page);
3aa3b2b2
JB
868
869 /*
870 * Two cases that we want to write new superblocks and rebuild:
871 * 1) New device (no matching magic number)
872 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
873 */
874 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
875 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
b12d437b
JB
876 super_sync(rdev->mddev, rdev);
877
878 set_bit(FirstUse, &rdev->flags);
879
880 /* Force writing of superblocks to disk */
881 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
882
883 /* Any superblock is better than none, choose that if given */
884 return refdev ? 0 : 1;
885 }
886
887 if (!refdev)
888 return 1;
889
890 events_sb = le64_to_cpu(sb->events);
891
892 refsb = page_address(refdev->sb_page);
893 events_refsb = le64_to_cpu(refsb->events);
894
895 return (events_sb > events_refsb) ? 1 : 0;
896}
897
fd01b88c 898static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
b12d437b
JB
899{
900 int role;
901 struct raid_set *rs = container_of(mddev, struct raid_set, md);
902 uint64_t events_sb;
903 uint64_t failed_devices;
904 struct dm_raid_superblock *sb;
905 uint32_t new_devs = 0;
906 uint32_t rebuilds = 0;
dafb20fa 907 struct md_rdev *r;
b12d437b
JB
908 struct dm_raid_superblock *sb2;
909
910 sb = page_address(rdev->sb_page);
911 events_sb = le64_to_cpu(sb->events);
912 failed_devices = le64_to_cpu(sb->failed_devices);
913
914 /*
915 * Initialise to 1 if this is a new superblock.
916 */
917 mddev->events = events_sb ? : 1;
918
919 /*
920 * Reshaping is not currently allowed
921 */
fe5d2f4a
JB
922 if (le32_to_cpu(sb->level) != mddev->level) {
923 DMERR("Reshaping arrays not yet supported. (RAID level change)");
924 return -EINVAL;
925 }
926 if (le32_to_cpu(sb->layout) != mddev->layout) {
927 DMERR("Reshaping arrays not yet supported. (RAID layout change)");
928 DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
929 DMERR(" Old layout: %s w/ %d copies",
930 raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
931 raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
932 DMERR(" New layout: %s w/ %d copies",
933 raid10_md_layout_to_format(mddev->layout),
934 raid10_md_layout_to_copies(mddev->layout));
935 return -EINVAL;
936 }
937 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
938 DMERR("Reshaping arrays not yet supported. (stripe sectors change)");
b12d437b
JB
939 return -EINVAL;
940 }
941
942 /* We can only change the number of devices in RAID1 right now */
943 if ((rs->raid_type->level != 1) &&
944 (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
fe5d2f4a 945 DMERR("Reshaping arrays not yet supported. (device count change)");
b12d437b
JB
946 return -EINVAL;
947 }
948
c76d53f4 949 if (!(rs->ctr_flags & (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)))
b12d437b
JB
950 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
951
952 /*
953 * During load, we set FirstUse if a new superblock was written.
954 * There are two reasons we might not have a superblock:
955 * 1) The array is brand new - in which case, all of the
956 * devices must have their In_sync bit set. Also,
957 * recovery_cp must be 0, unless forced.
958 * 2) This is a new device being added to an old array
959 * and the new device needs to be rebuilt - in which
960 * case the In_sync bit will /not/ be set and
961 * recovery_cp must be MaxSector.
962 */
dafb20fa 963 rdev_for_each(r, mddev) {
b12d437b 964 if (!test_bit(In_sync, &r->flags)) {
3aa3b2b2
JB
965 DMINFO("Device %d specified for rebuild: "
966 "Clearing superblock", r->raid_disk);
b12d437b
JB
967 rebuilds++;
968 } else if (test_bit(FirstUse, &r->flags))
969 new_devs++;
970 }
971
972 if (!rebuilds) {
973 if (new_devs == mddev->raid_disks) {
974 DMINFO("Superblocks created for new array");
975 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
976 } else if (new_devs) {
977 DMERR("New device injected "
978 "into existing array without 'rebuild' "
979 "parameter specified");
980 return -EINVAL;
981 }
982 } else if (new_devs) {
983 DMERR("'rebuild' devices cannot be "
984 "injected into an array with other first-time devices");
985 return -EINVAL;
986 } else if (mddev->recovery_cp != MaxSector) {
987 DMERR("'rebuild' specified while array is not in-sync");
988 return -EINVAL;
989 }
990
991 /*
992 * Now we set the Faulty bit for those devices that are
993 * recorded in the superblock as failed.
994 */
dafb20fa 995 rdev_for_each(r, mddev) {
b12d437b
JB
996 if (!r->sb_page)
997 continue;
998 sb2 = page_address(r->sb_page);
999 sb2->failed_devices = 0;
1000
1001 /*
1002 * Check for any device re-ordering.
1003 */
1004 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
1005 role = le32_to_cpu(sb2->array_position);
1006 if (role != r->raid_disk) {
1007 if (rs->raid_type->level != 1) {
1008 rs->ti->error = "Cannot change device "
1009 "positions in RAID array";
1010 return -EINVAL;
1011 }
1012 DMINFO("RAID1 device #%d now at position #%d",
1013 role, r->raid_disk);
1014 }
1015
1016 /*
1017 * Partial recovery is performed on
1018 * returning failed devices.
1019 */
1020 if (failed_devices & (1 << role))
1021 set_bit(Faulty, &r->flags);
1022 }
1023 }
1024
1025 return 0;
1026}
1027
0cf45031 1028static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
b12d437b 1029{
0cf45031 1030 struct mddev *mddev = &rs->md;
b12d437b
JB
1031 struct dm_raid_superblock *sb = page_address(rdev->sb_page);
1032
1033 /*
1034 * If mddev->events is not set, we know we have not yet initialized
1035 * the array.
1036 */
1037 if (!mddev->events && super_init_validation(mddev, rdev))
1038 return -EINVAL;
1039
4c9971ca
HM
1040 if (le32_to_cpu(sb->features)) {
1041 rs->ti->error = "Unable to assemble array: No feature flags supported yet";
1042 return -EINVAL;
1043 }
1044
0cf45031
HM
1045 /* Enable bitmap creation for RAID levels != 0 */
1046 mddev->bitmap_info.offset = (rs->raid_type->level) ? to_sector(4096) : 0;
1047 rdev->mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
1048
b12d437b
JB
1049 if (!test_bit(FirstUse, &rdev->flags)) {
1050 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
1051 if (rdev->recovery_offset != MaxSector)
1052 clear_bit(In_sync, &rdev->flags);
1053 }
1054
1055 /*
1056 * If a device comes back, set it as not In_sync and no longer faulty.
1057 */
1058 if (test_bit(Faulty, &rdev->flags)) {
1059 clear_bit(Faulty, &rdev->flags);
1060 clear_bit(In_sync, &rdev->flags);
1061 rdev->saved_raid_disk = rdev->raid_disk;
1062 rdev->recovery_offset = 0;
1063 }
1064
1065 clear_bit(FirstUse, &rdev->flags);
1066
1067 return 0;
1068}
1069
1070/*
1071 * Analyse superblocks and select the freshest.
1072 */
1073static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
1074{
73c6f239 1075 int r;
0447568f 1076 struct raid_dev *dev;
a9ad8526 1077 struct md_rdev *rdev, *tmp, *freshest;
fd01b88c 1078 struct mddev *mddev = &rs->md;
b12d437b
JB
1079
1080 freshest = NULL;
a9ad8526 1081 rdev_for_each_safe(rdev, tmp, mddev) {
761becff 1082 /*
c76d53f4 1083 * Skipping super_load due to CTR_FLAG_SYNC will cause
761becff
JB
1084 * the array to undergo initialization again as
1085 * though it were new. This is the intended effect
1086 * of the "sync" directive.
1087 *
1088 * When reshaping capability is added, we must ensure
1089 * that the "sync" directive is disallowed during the
1090 * reshape.
1091 */
0cf45031
HM
1092 rdev->sectors = to_sector(i_size_read(rdev->bdev->bd_inode));
1093
c76d53f4 1094 if (rs->ctr_flags & CTR_FLAG_SYNC)
761becff
JB
1095 continue;
1096
b12d437b
JB
1097 if (!rdev->meta_bdev)
1098 continue;
1099
73c6f239 1100 r = super_load(rdev, freshest);
b12d437b 1101
73c6f239 1102 switch (r) {
b12d437b
JB
1103 case 1:
1104 freshest = rdev;
1105 break;
1106 case 0:
1107 break;
1108 default:
0447568f 1109 dev = container_of(rdev, struct raid_dev, rdev);
55ebbb59
JB
1110 if (dev->meta_dev)
1111 dm_put_device(ti, dev->meta_dev);
0447568f 1112
55ebbb59
JB
1113 dev->meta_dev = NULL;
1114 rdev->meta_bdev = NULL;
0447568f 1115
55ebbb59
JB
1116 if (rdev->sb_page)
1117 put_page(rdev->sb_page);
0447568f 1118
55ebbb59 1119 rdev->sb_page = NULL;
0447568f 1120
55ebbb59 1121 rdev->sb_loaded = 0;
0447568f 1122
55ebbb59
JB
1123 /*
1124 * We might be able to salvage the data device
1125 * even though the meta device has failed. For
1126 * now, we behave as though '- -' had been
1127 * set for this device in the table.
1128 */
1129 if (dev->data_dev)
1130 dm_put_device(ti, dev->data_dev);
0447568f 1131
55ebbb59
JB
1132 dev->data_dev = NULL;
1133 rdev->bdev = NULL;
0447568f 1134
55ebbb59 1135 list_del(&rdev->same_set);
b12d437b
JB
1136 }
1137 }
1138
1139 if (!freshest)
1140 return 0;
1141
55ebbb59
JB
1142 if (validate_raid_redundancy(rs)) {
1143 rs->ti->error = "Insufficient redundancy to activate array";
1144 return -EINVAL;
1145 }
1146
b12d437b
JB
1147 /*
1148 * Validation of the freshest device provides the source of
1149 * validation for the remaining devices.
1150 */
1151 ti->error = "Unable to assemble array: Invalid superblocks";
0cf45031 1152 if (super_validate(rs, freshest))
b12d437b
JB
1153 return -EINVAL;
1154
dafb20fa 1155 rdev_for_each(rdev, mddev)
0cf45031 1156 if ((rdev != freshest) && super_validate(rs, rdev))
b12d437b
JB
1157 return -EINVAL;
1158
1159 return 0;
1160}
1161
75b8e04b 1162/*
48cf06bc
HM
1163 * Enable/disable discard support on RAID set depending on
1164 * RAID level and discard properties of underlying RAID members.
75b8e04b
HM
1165 */
1166static void configure_discard_support(struct dm_target *ti, struct raid_set *rs)
1167{
48cf06bc
HM
1168 int i;
1169 bool raid456;
1170
75b8e04b
HM
1171 /* Assume discards not supported until after checks below. */
1172 ti->discards_supported = false;
1173
1174 /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
48cf06bc 1175 raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
75b8e04b 1176
48cf06bc 1177 for (i = 0; i < rs->md.raid_disks; i++) {
d20c4b08 1178 struct request_queue *q;
48cf06bc 1179
d20c4b08
HM
1180 if (!rs->dev[i].rdev.bdev)
1181 continue;
1182
1183 q = bdev_get_queue(rs->dev[i].rdev.bdev);
48cf06bc
HM
1184 if (!q || !blk_queue_discard(q))
1185 return;
1186
1187 if (raid456) {
1188 if (!q->limits.discard_zeroes_data)
1189 return;
1190 if (!devices_handle_discard_safely) {
1191 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
1192 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
1193 return;
1194 }
1195 }
1196 }
1197
1198 /* All RAID members properly support discards */
75b8e04b
HM
1199 ti->discards_supported = true;
1200
1201 /*
1202 * RAID1 and RAID10 personalities require bio splitting,
48cf06bc 1203 * RAID0/4/5/6 don't and process large discard bios properly.
75b8e04b 1204 */
48cf06bc 1205 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
75b8e04b
HM
1206 ti->num_discard_bios = 1;
1207}
1208
9d09e663 1209/*
73c6f239 1210 * Construct a RAID0/1/10/4/5/6 mapping:
9d09e663 1211 * Args:
73c6f239
HM
1212 * <raid_type> <#raid_params> <raid_params>{0,} \
1213 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
9d09e663 1214 *
9d09e663
N
1215 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
1216 * details on possible <raid_params>.
73c6f239
HM
1217 *
1218 * Userspace is free to initialize the metadata devices, hence the superblocks to
1219 * enforce recreation based on the passed in table parameters.
1220 *
9d09e663
N
1221 */
1222static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
1223{
73c6f239 1224 int r;
9d09e663
N
1225 struct raid_type *rt;
1226 unsigned long num_raid_params, num_raid_devs;
1227 struct raid_set *rs = NULL;
1228
1229 /* Must have at least <raid_type> <#raid_params> */
1230 if (argc < 2) {
1231 ti->error = "Too few arguments";
1232 return -EINVAL;
1233 }
1234
1235 /* raid type */
1236 rt = get_raid_type(argv[0]);
1237 if (!rt) {
1238 ti->error = "Unrecognised raid_type";
1239 return -EINVAL;
1240 }
1241 argc--;
1242 argv++;
1243
1244 /* number of RAID parameters */
b29bebd6 1245 if (kstrtoul(argv[0], 10, &num_raid_params) < 0) {
9d09e663
N
1246 ti->error = "Cannot understand number of RAID parameters";
1247 return -EINVAL;
1248 }
1249 argc--;
1250 argv++;
1251
1252 /* Skip over RAID params for now and find out # of devices */
3ca5a21a 1253 if (num_raid_params >= argc) {
9d09e663
N
1254 ti->error = "Arguments do not agree with counts given";
1255 return -EINVAL;
1256 }
1257
b29bebd6 1258 if ((kstrtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) ||
c76d53f4 1259 (num_raid_devs > MAX_RAID_DEVICES)) {
9d09e663
N
1260 ti->error = "Cannot understand number of raid devices";
1261 return -EINVAL;
1262 }
1263
3ca5a21a
DC
1264 argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
1265 if (argc != (num_raid_devs * 2)) {
1266 ti->error = "Supplied RAID devices does not match the count given";
1267 return -EINVAL;
1268 }
1269
9d09e663
N
1270 rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
1271 if (IS_ERR(rs))
1272 return PTR_ERR(rs);
1273
73c6f239
HM
1274 r = parse_raid_params(rs, argv, (unsigned)num_raid_params);
1275 if (r)
9d09e663
N
1276 goto bad;
1277
9d09e663
N
1278 argv += num_raid_params + 1;
1279
73c6f239
HM
1280 r = dev_parms(rs, argv);
1281 if (r)
9d09e663
N
1282 goto bad;
1283
b12d437b 1284 rs->md.sync_super = super_sync;
73c6f239
HM
1285 r = analyse_superblocks(ti, rs);
1286 if (r)
b12d437b
JB
1287 goto bad;
1288
9d09e663 1289 INIT_WORK(&rs->md.event_work, do_table_event);
9d09e663 1290 ti->private = rs;
55a62eef 1291 ti->num_flush_bios = 1;
9d09e663 1292
75b8e04b
HM
1293 /*
1294 * Disable/enable discard support on RAID set.
1295 */
1296 configure_discard_support(ti, rs);
1297
0cf45031
HM
1298 /* Has to be held on running the array */
1299 mddev_lock_nointr(&rs->md);
73c6f239 1300 r = md_run(&rs->md);
9d09e663 1301 rs->md.in_sync = 0; /* Assume already marked dirty */
0cf45031 1302 mddev_unlock(&rs->md);
9d09e663 1303
73c6f239 1304 if (r) {
9d09e663
N
1305 ti->error = "Fail to run raid array";
1306 goto bad;
1307 }
1308
63f33b8d
JB
1309 if (ti->len != rs->md.array_sectors) {
1310 ti->error = "Array size does not match requested target length";
73c6f239 1311 r = -EINVAL;
63f33b8d
JB
1312 goto size_mismatch;
1313 }
9d09e663 1314 rs->callbacks.congested_fn = raid_is_congested;
9d09e663
N
1315 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
1316
32737279 1317 mddev_suspend(&rs->md);
9d09e663
N
1318 return 0;
1319
63f33b8d
JB
1320size_mismatch:
1321 md_stop(&rs->md);
9d09e663
N
1322bad:
1323 context_free(rs);
1324
73c6f239 1325 return r;
9d09e663
N
1326}
1327
1328static void raid_dtr(struct dm_target *ti)
1329{
1330 struct raid_set *rs = ti->private;
1331
1332 list_del_init(&rs->callbacks.list);
1333 md_stop(&rs->md);
1334 context_free(rs);
1335}
1336
7de3ee57 1337static int raid_map(struct dm_target *ti, struct bio *bio)
9d09e663
N
1338{
1339 struct raid_set *rs = ti->private;
fd01b88c 1340 struct mddev *mddev = &rs->md;
9d09e663
N
1341
1342 mddev->pers->make_request(mddev, bio);
1343
1344 return DM_MAPIO_SUBMITTED;
1345}
1346
be83651f
JB
1347static const char *decipher_sync_action(struct mddev *mddev)
1348{
1349 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
1350 return "frozen";
1351
1352 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1353 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
1354 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1355 return "reshape";
1356
1357 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1358 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1359 return "resync";
1360 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1361 return "check";
1362 return "repair";
1363 }
1364
1365 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
1366 return "recover";
1367 }
1368
1369 return "idle";
1370}
1371
fd7c092e
MP
1372static void raid_status(struct dm_target *ti, status_type_t type,
1373 unsigned status_flags, char *result, unsigned maxlen)
9d09e663
N
1374{
1375 struct raid_set *rs = ti->private;
1376 unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
1377 unsigned sz = 0;
2e727c3c 1378 int i, array_in_sync = 0;
9d09e663
N
1379 sector_t sync;
1380
1381 switch (type) {
1382 case STATUSTYPE_INFO:
1383 DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
1384
0cf45031
HM
1385 if (rs->raid_type->level) {
1386 if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
1387 sync = rs->md.curr_resync_completed;
1388 else
1389 sync = rs->md.recovery_cp;
1390
1391 if (sync >= rs->md.resync_max_sectors) {
1392 /*
1393 * Sync complete.
1394 */
1395 array_in_sync = 1;
1396 sync = rs->md.resync_max_sectors;
1397 } else if (test_bit(MD_RECOVERY_REQUESTED, &rs->md.recovery)) {
1398 /*
1399 * If "check" or "repair" is occurring, the array has
1400 * undergone and initial sync and the health characters
1401 * should not be 'a' anymore.
1402 */
1403 array_in_sync = 1;
1404 } else {
1405 /*
1406 * The array may be doing an initial sync, or it may
1407 * be rebuilding individual components. If all the
1408 * devices are In_sync, then it is the array that is
1409 * being initialized.
1410 */
1411 for (i = 0; i < rs->md.raid_disks; i++)
1412 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
1413 array_in_sync = 1;
1414 }
1415 } else {
1416 /* RAID0 */
2e727c3c 1417 array_in_sync = 1;
9d09e663 1418 sync = rs->md.resync_max_sectors;
2e727c3c 1419 }
be83651f 1420
2e727c3c
JB
1421 /*
1422 * Status characters:
1423 * 'D' = Dead/Failed device
1424 * 'a' = Alive but not in-sync
1425 * 'A' = Alive and in-sync
1426 */
1427 for (i = 0; i < rs->md.raid_disks; i++) {
1428 if (test_bit(Faulty, &rs->dev[i].rdev.flags))
1429 DMEMIT("D");
1430 else if (!array_in_sync ||
1431 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1432 DMEMIT("a");
1433 else
1434 DMEMIT("A");
1435 }
9d09e663 1436
2e727c3c
JB
1437 /*
1438 * In-sync ratio:
1439 * The in-sync ratio shows the progress of:
1440 * - Initializing the array
1441 * - Rebuilding a subset of devices of the array
1442 * The user can distinguish between the two by referring
1443 * to the status characters.
1444 */
9d09e663
N
1445 DMEMIT(" %llu/%llu",
1446 (unsigned long long) sync,
1447 (unsigned long long) rs->md.resync_max_sectors);
1448
be83651f
JB
1449 /*
1450 * Sync action:
1451 * See Documentation/device-mapper/dm-raid.c for
1452 * information on each of these states.
1453 */
1454 DMEMIT(" %s", decipher_sync_action(&rs->md));
1455
1456 /*
1457 * resync_mismatches/mismatch_cnt
1458 * This field shows the number of discrepancies found when
1459 * performing a "check" of the array.
1460 */
1461 DMEMIT(" %llu",
c4a39551 1462 (strcmp(rs->md.last_sync_action, "check")) ? 0 :
be83651f
JB
1463 (unsigned long long)
1464 atomic64_read(&rs->md.resync_mismatches));
9d09e663
N
1465 break;
1466 case STATUSTYPE_TABLE:
1467 /* The string you would use to construct this array */
46bed2b5 1468 for (i = 0; i < rs->md.raid_disks; i++) {
c76d53f4 1469 if ((rs->ctr_flags & CTR_FLAG_REBUILD) &&
13c87583 1470 rs->dev[i].data_dev &&
9d09e663 1471 !test_bit(In_sync, &rs->dev[i].rdev.flags))
13c87583 1472 raid_param_cnt += 2; /* for rebuilds */
46bed2b5
JB
1473 if (rs->dev[i].data_dev &&
1474 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1475 raid_param_cnt += 2;
1476 }
9d09e663 1477
c76d53f4
HM
1478 raid_param_cnt += (hweight32(rs->ctr_flags & ~CTR_FLAG_REBUILD) * 2);
1479 if (rs->ctr_flags & (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC))
9d09e663
N
1480 raid_param_cnt--;
1481
1482 DMEMIT("%s %u %u", rs->raid_type->name,
1483 raid_param_cnt, rs->md.chunk_sectors);
1484
c76d53f4 1485 if ((rs->ctr_flags & CTR_FLAG_SYNC) &&
9d09e663
N
1486 (rs->md.recovery_cp == MaxSector))
1487 DMEMIT(" sync");
c76d53f4 1488 if (rs->ctr_flags & CTR_FLAG_NOSYNC)
9d09e663
N
1489 DMEMIT(" nosync");
1490
1491 for (i = 0; i < rs->md.raid_disks; i++)
c76d53f4 1492 if ((rs->ctr_flags & CTR_FLAG_REBUILD) &&
13c87583 1493 rs->dev[i].data_dev &&
9d09e663
N
1494 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1495 DMEMIT(" rebuild %u", i);
1496
c76d53f4 1497 if (rs->ctr_flags & CTR_FLAG_DAEMON_SLEEP)
9d09e663
N
1498 DMEMIT(" daemon_sleep %lu",
1499 rs->md.bitmap_info.daemon_sleep);
1500
c76d53f4 1501 if (rs->ctr_flags & CTR_FLAG_MIN_RECOVERY_RATE)
9d09e663
N
1502 DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
1503
c76d53f4 1504 if (rs->ctr_flags & CTR_FLAG_MAX_RECOVERY_RATE)
9d09e663
N
1505 DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
1506
46bed2b5
JB
1507 for (i = 0; i < rs->md.raid_disks; i++)
1508 if (rs->dev[i].data_dev &&
1509 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1510 DMEMIT(" write_mostly %u", i);
1511
c76d53f4 1512 if (rs->ctr_flags & CTR_FLAG_MAX_WRITE_BEHIND)
9d09e663
N
1513 DMEMIT(" max_write_behind %lu",
1514 rs->md.bitmap_info.max_write_behind);
1515
c76d53f4 1516 if (rs->ctr_flags & CTR_FLAG_STRIPE_CACHE) {
d1688a6d 1517 struct r5conf *conf = rs->md.private;
9d09e663
N
1518
1519 /* convert from kiB to sectors */
1520 DMEMIT(" stripe_cache %d",
1521 conf ? conf->max_nr_stripes * 2 : 0);
1522 }
1523
c76d53f4 1524 if (rs->ctr_flags & CTR_FLAG_REGION_SIZE)
c1084561
JB
1525 DMEMIT(" region_size %lu",
1526 rs->md.bitmap_info.chunksize >> 9);
1527
c76d53f4 1528 if (rs->ctr_flags & CTR_FLAG_RAID10_COPIES)
63f33b8d
JB
1529 DMEMIT(" raid10_copies %u",
1530 raid10_md_layout_to_copies(rs->md.layout));
1531
c76d53f4 1532 if (rs->ctr_flags & CTR_FLAG_RAID10_FORMAT)
fe5d2f4a
JB
1533 DMEMIT(" raid10_format %s",
1534 raid10_md_layout_to_format(rs->md.layout));
63f33b8d 1535
9d09e663
N
1536 DMEMIT(" %d", rs->md.raid_disks);
1537 for (i = 0; i < rs->md.raid_disks; i++) {
b12d437b
JB
1538 if (rs->dev[i].meta_dev)
1539 DMEMIT(" %s", rs->dev[i].meta_dev->name);
1540 else
1541 DMEMIT(" -");
9d09e663
N
1542
1543 if (rs->dev[i].data_dev)
1544 DMEMIT(" %s", rs->dev[i].data_dev->name);
1545 else
1546 DMEMIT(" -");
1547 }
1548 }
9d09e663
N
1549}
1550
be83651f
JB
1551static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
1552{
1553 struct raid_set *rs = ti->private;
1554 struct mddev *mddev = &rs->md;
1555
1556 if (!strcasecmp(argv[0], "reshape")) {
1557 DMERR("Reshape not supported.");
1558 return -EINVAL;
1559 }
1560
1561 if (!mddev->pers || !mddev->pers->sync_request)
1562 return -EINVAL;
1563
1564 if (!strcasecmp(argv[0], "frozen"))
1565 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
1566 else
1567 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
1568
1569 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
1570 if (mddev->sync_thread) {
1571 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1572 md_reap_sync_thread(mddev);
1573 }
1574 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1575 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
1576 return -EBUSY;
1577 else if (!strcasecmp(argv[0], "resync"))
1578 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1579 else if (!strcasecmp(argv[0], "recover")) {
1580 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
1581 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1582 } else {
1583 if (!strcasecmp(argv[0], "check"))
1584 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1585 else if (!!strcasecmp(argv[0], "repair"))
1586 return -EINVAL;
1587 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1588 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1589 }
1590 if (mddev->ro == 2) {
1591 /* A write to sync_action is enough to justify
1592 * canceling read-auto mode
1593 */
1594 mddev->ro = 0;
1595 if (!mddev->suspended)
1596 md_wakeup_thread(mddev->sync_thread);
1597 }
1598 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1599 if (!mddev->suspended)
1600 md_wakeup_thread(mddev->thread);
1601
1602 return 0;
1603}
1604
1605static int raid_iterate_devices(struct dm_target *ti,
1606 iterate_devices_callout_fn fn, void *data)
9d09e663
N
1607{
1608 struct raid_set *rs = ti->private;
1609 unsigned i;
73c6f239 1610 int r = 0;
9d09e663 1611
73c6f239 1612 for (i = 0; !r && i < rs->md.raid_disks; i++)
9d09e663 1613 if (rs->dev[i].data_dev)
73c6f239 1614 r = fn(ti,
9d09e663
N
1615 rs->dev[i].data_dev,
1616 0, /* No offset on data devs */
1617 rs->md.dev_sectors,
1618 data);
1619
73c6f239 1620 return r;
9d09e663
N
1621}
1622
1623static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
1624{
1625 struct raid_set *rs = ti->private;
1626 unsigned chunk_size = rs->md.chunk_sectors << 9;
d1688a6d 1627 struct r5conf *conf = rs->md.private;
9d09e663
N
1628
1629 blk_limits_io_min(limits, chunk_size);
1630 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
1631}
1632
1633static void raid_presuspend(struct dm_target *ti)
1634{
1635 struct raid_set *rs = ti->private;
1636
1637 md_stop_writes(&rs->md);
1638}
1639
1640static void raid_postsuspend(struct dm_target *ti)
1641{
1642 struct raid_set *rs = ti->private;
1643
1644 mddev_suspend(&rs->md);
1645}
1646
f381e71b 1647static void attempt_restore_of_faulty_devices(struct raid_set *rs)
9d09e663 1648{
9092c02d
JB
1649 int i;
1650 uint64_t failed_devices, cleared_failed_devices = 0;
1651 unsigned long flags;
1652 struct dm_raid_superblock *sb;
9092c02d 1653 struct md_rdev *r;
9d09e663 1654
f381e71b
JB
1655 for (i = 0; i < rs->md.raid_disks; i++) {
1656 r = &rs->dev[i].rdev;
1657 if (test_bit(Faulty, &r->flags) && r->sb_page &&
796a5cf0
MC
1658 sync_page_io(r, 0, r->sb_size, r->sb_page, REQ_OP_READ, 0,
1659 1)) {
f381e71b
JB
1660 DMINFO("Faulty %s device #%d has readable super block."
1661 " Attempting to revive it.",
1662 rs->raid_type->name, i);
a4dc163a
JB
1663
1664 /*
1665 * Faulty bit may be set, but sometimes the array can
1666 * be suspended before the personalities can respond
1667 * by removing the device from the array (i.e. calling
1668 * 'hot_remove_disk'). If they haven't yet removed
1669 * the failed device, its 'raid_disk' number will be
1670 * '>= 0' - meaning we must call this function
1671 * ourselves.
1672 */
1673 if ((r->raid_disk >= 0) &&
1674 (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0))
1675 /* Failed to revive this device, try next */
1676 continue;
1677
f381e71b
JB
1678 r->raid_disk = i;
1679 r->saved_raid_disk = i;
1680 flags = r->flags;
1681 clear_bit(Faulty, &r->flags);
1682 clear_bit(WriteErrorSeen, &r->flags);
1683 clear_bit(In_sync, &r->flags);
1684 if (r->mddev->pers->hot_add_disk(r->mddev, r)) {
1685 r->raid_disk = -1;
1686 r->saved_raid_disk = -1;
1687 r->flags = flags;
1688 } else {
1689 r->recovery_offset = 0;
1690 cleared_failed_devices |= 1 << i;
1691 }
1692 }
1693 }
1694 if (cleared_failed_devices) {
1695 rdev_for_each(r, &rs->md) {
1696 sb = page_address(r->sb_page);
1697 failed_devices = le64_to_cpu(sb->failed_devices);
1698 failed_devices &= ~cleared_failed_devices;
1699 sb->failed_devices = cpu_to_le64(failed_devices);
1700 }
1701 }
1702}
1703
1704static void raid_resume(struct dm_target *ti)
1705{
1706 struct raid_set *rs = ti->private;
1707
0cf45031
HM
1708 if (rs->raid_type->level) {
1709 set_bit(MD_CHANGE_DEVS, &rs->md.flags);
1710
1711 if (!rs->bitmap_loaded) {
1712 bitmap_load(&rs->md);
1713 rs->bitmap_loaded = 1;
1714 } else {
1715 /*
1716 * A secondary resume while the device is active.
1717 * Take this opportunity to check whether any failed
1718 * devices are reachable again.
1719 */
1720 attempt_restore_of_faulty_devices(rs);
1721 }
1722
1723 clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
47525e59 1724 }
34f8ac6d 1725
9d09e663
N
1726 mddev_resume(&rs->md);
1727}
1728
1729static struct target_type raid_target = {
1730 .name = "raid",
4c9971ca 1731 .version = {1, 8, 0},
9d09e663
N
1732 .module = THIS_MODULE,
1733 .ctr = raid_ctr,
1734 .dtr = raid_dtr,
1735 .map = raid_map,
1736 .status = raid_status,
be83651f 1737 .message = raid_message,
9d09e663
N
1738 .iterate_devices = raid_iterate_devices,
1739 .io_hints = raid_io_hints,
1740 .presuspend = raid_presuspend,
1741 .postsuspend = raid_postsuspend,
1742 .resume = raid_resume,
1743};
1744
1745static int __init dm_raid_init(void)
1746{
fe5d2f4a
JB
1747 DMINFO("Loading target version %u.%u.%u",
1748 raid_target.version[0],
1749 raid_target.version[1],
1750 raid_target.version[2]);
9d09e663
N
1751 return dm_register_target(&raid_target);
1752}
1753
1754static void __exit dm_raid_exit(void)
1755{
1756 dm_unregister_target(&raid_target);
1757}
1758
1759module_init(dm_raid_init);
1760module_exit(dm_raid_exit);
1761
48cf06bc
HM
1762module_param(devices_handle_discard_safely, bool, 0644);
1763MODULE_PARM_DESC(devices_handle_discard_safely,
1764 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
1765
9d09e663 1766MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
63f33b8d
JB
1767MODULE_ALIAS("dm-raid1");
1768MODULE_ALIAS("dm-raid10");
9d09e663
N
1769MODULE_ALIAS("dm-raid4");
1770MODULE_ALIAS("dm-raid5");
1771MODULE_ALIAS("dm-raid6");
1772MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
1773MODULE_LICENSE("GPL");