dm raid: enhance super_sync() to support new superblock members
[cascardo/linux.git] / drivers / md / dm-raid.c
1 /*
2  * Copyright (C) 2010-2011 Neil Brown
3  * Copyright (C) 2010-2016 Red Hat, Inc. All rights reserved.
4  *
5  * This file is released under the GPL.
6  */
7
8 #include <linux/slab.h>
9 #include <linux/module.h>
10
11 #include "md.h"
12 #include "raid1.h"
13 #include "raid5.h"
14 #include "raid10.h"
15 #include "bitmap.h"
16
17 #include <linux/device-mapper.h>
18
19 #define DM_MSG_PREFIX "raid"
20 #define MAX_RAID_DEVICES        253 /* md-raid kernel limit */
21
22 static bool devices_handle_discard_safely = false;
23
24 /*
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.
27  */
28 #define FirstUse 10             /* rdev flag */
29
30 struct 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;
45         struct md_rdev rdev;
46 };
47
48 /*
49  * Flags for rs->ctr_flags field.
50  *
51  * 1 = no flag value
52  * 2 = flag with value
53  */
54 #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 */
66 /* New for v1.8.0 */
67 #define CTR_FLAG_DELTA_DISKS          0x1000 /* 2 */ /* Only with reshapable raid4/5/6/10! */
68 #define CTR_FLAG_DATA_OFFSET          0x2000 /* 2 */ /* Only with reshapable raid4/5/6/10! */
69 #define CTR_FLAG_RAID10_USE_NEAR_SETS 0x4000 /* 2 */ /* Only with raid10! */
70
71 /*
72  * Definitions of various constructor flags to
73  * be used in checks of valid / invalid flags
74  * per raid level.
75  */
76 /* Define all any sync flags */
77 #define CTR_FLAGS_ANY_SYNC              (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
78
79 /* Define flags for options without argument (e.g. 'nosync') */
80 #define CTR_FLAG_OPTIONS_NO_ARGS        (CTR_FLAGS_ANY_SYNC | \
81                                          CTR_FLAG_RAID10_USE_NEAR_SETS)
82
83 /* Define flags for options with one argument (e.g. 'delta_disks +2') */
84 #define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
85                                   CTR_FLAG_WRITE_MOSTLY | \
86                                   CTR_FLAG_DAEMON_SLEEP | \
87                                   CTR_FLAG_MIN_RECOVERY_RATE | \
88                                   CTR_FLAG_MAX_RECOVERY_RATE | \
89                                   CTR_FLAG_MAX_WRITE_BEHIND | \
90                                   CTR_FLAG_STRIPE_CACHE | \
91                                   CTR_FLAG_REGION_SIZE | \
92                                   CTR_FLAG_RAID10_COPIES | \
93                                   CTR_FLAG_RAID10_FORMAT | \
94                                   CTR_FLAG_DELTA_DISKS | \
95                                   CTR_FLAG_DATA_OFFSET)
96
97 /* All ctr optional arguments */
98 #define ALL_CTR_FLAGS           (CTR_FLAG_OPTIONS_NO_ARGS | \
99                                  CTR_FLAG_OPTIONS_ONE_ARG)
100
101 /* Invalid options definitions per raid level... */
102
103 /* "raid0" does not accept any options */
104 #define RAID0_INVALID_FLAGS ALL_CTR_FLAGS
105
106 /* "raid1" does not accept stripe cache or any raid10 options */
107 #define RAID1_INVALID_FLAGS     (CTR_FLAG_STRIPE_CACHE | \
108                                  CTR_FLAG_RAID10_COPIES | \
109                                  CTR_FLAG_RAID10_FORMAT | \
110                                  CTR_FLAG_DELTA_DISKS | \
111                                  CTR_FLAG_DATA_OFFSET)
112
113 /* "raid10" does not accept any raid1 or stripe cache options */
114 #define RAID10_INVALID_FLAGS    (CTR_FLAG_WRITE_MOSTLY | \
115                                  CTR_FLAG_MAX_WRITE_BEHIND | \
116                                  CTR_FLAG_STRIPE_CACHE)
117 /*
118  * "raid4/5/6" do not accept any raid1 or raid10 specific options
119  *
120  * "raid6" does not accept "nosync", because it is not guaranteed
121  * that both parity and q-syndrome are being written properly with
122  * any writes
123  */
124 #define RAID45_INVALID_FLAGS    (CTR_FLAG_WRITE_MOSTLY | \
125                                  CTR_FLAG_MAX_WRITE_BEHIND | \
126                                  CTR_FLAG_RAID10_FORMAT | \
127                                  CTR_FLAG_RAID10_COPIES | \
128                                  CTR_FLAG_RAID10_USE_NEAR_SETS)
129 #define RAID6_INVALID_FLAGS     (CTR_FLAG_NOSYNC | RAID45_INVALID_FLAGS)
130 /* ...invalid options definitions per raid level */
131
132 /* Array elements of 64 bit needed for rebuild/write_mostly bits */
133 #define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)
134
135 struct raid_set {
136         struct dm_target *ti;
137
138         uint32_t bitmap_loaded;
139         uint32_t ctr_flags;
140
141         int raid_disks;
142         int delta_disks;
143         int data_offset;
144         int raid10_copies;
145
146         struct mddev md;
147         struct raid_type *raid_type;
148         struct dm_target_callbacks callbacks;
149
150         struct raid_dev dev[0];
151 };
152
153 /* raid10 algorithms (i.e. formats) */
154 #define ALGORITHM_RAID10_DEFAULT        0
155 #define ALGORITHM_RAID10_NEAR           1
156 #define ALGORITHM_RAID10_OFFSET         2
157 #define ALGORITHM_RAID10_FAR            3
158
159 /* Supported raid types and properties. */
160 static struct raid_type {
161         const char *name;               /* RAID algorithm. */
162         const char *descr;              /* Descriptor text for logging. */
163         const unsigned parity_devs;     /* # of parity devices. */
164         const unsigned minimal_devs;    /* minimal # of devices in set. */
165         const unsigned level;           /* RAID level. */
166         const unsigned algorithm;       /* RAID algorithm. */
167 } raid_types[] = {
168         {"raid0",         "raid0 (striping)",                       0, 2, 0,  0 /* NONE */},
169         {"raid1",         "raid1 (mirroring)",                      0, 2, 1,  0 /* NONE */},
170         {"raid10_far",    "raid10 far (striped mirrors)",           0, 2, 10, ALGORITHM_RAID10_FAR},
171         {"raid10_offset", "raid10 offset (striped mirrors)",        0, 2, 10, ALGORITHM_RAID10_OFFSET},
172         {"raid10_near",   "raid10 near (striped mirrors)",          0, 2, 10, ALGORITHM_RAID10_NEAR},
173         {"raid10",        "raid10 (striped mirrors)",               0, 2, 10, ALGORITHM_RAID10_DEFAULT},
174         {"raid4",         "raid4 (dedicated last parity disk)",     1, 2, 4,  ALGORITHM_PARITY_N}, /* raid4 layout = raid5_n */
175         {"raid5_n",       "raid5 (dedicated last parity disk)",     1, 2, 5,  ALGORITHM_PARITY_N},
176         {"raid5_ls",      "raid5 (left symmetric)",                 1, 2, 5,  ALGORITHM_LEFT_SYMMETRIC},
177         {"raid5_rs",      "raid5 (right symmetric)",                1, 2, 5,  ALGORITHM_RIGHT_SYMMETRIC},
178         {"raid5_la",      "raid5 (left asymmetric)",                1, 2, 5,  ALGORITHM_LEFT_ASYMMETRIC},
179         {"raid5_ra",      "raid5 (right asymmetric)",               1, 2, 5,  ALGORITHM_RIGHT_ASYMMETRIC},
180         {"raid6_zr",      "raid6 (zero restart)",                   2, 4, 6,  ALGORITHM_ROTATING_ZERO_RESTART},
181         {"raid6_nr",      "raid6 (N restart)",                      2, 4, 6,  ALGORITHM_ROTATING_N_RESTART},
182         {"raid6_nc",      "raid6 (N continue)",                     2, 4, 6,  ALGORITHM_ROTATING_N_CONTINUE},
183         {"raid6_n_6",     "raid6 (dedicated parity/Q n/6)",         2, 4, 6,  ALGORITHM_PARITY_N_6},
184         {"raid6_ls_6",    "raid6 (left symmetric dedicated Q 6)",   2, 4, 6,  ALGORITHM_LEFT_SYMMETRIC_6},
185         {"raid6_rs_6",    "raid6 (right symmetric dedicated Q 6)",  2, 4, 6,  ALGORITHM_RIGHT_SYMMETRIC_6},
186         {"raid6_la_6",    "raid6 (left asymmetric dedicated Q 6)",  2, 4, 6,  ALGORITHM_LEFT_ASYMMETRIC_6},
187         {"raid6_ra_6",    "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6,  ALGORITHM_RIGHT_ASYMMETRIC_6}
188 };
189
190 /* True, if @v is in inclusive range [@min, @max] */
191 static bool _in_range(long v, long min, long max)
192 {
193         return v >= min && v <= max;
194 }
195
196 /* ctr flag bit manipulation... */
197 /* Set single @flag in @flags */
198 static void _set_flag(uint32_t flag, uint32_t *flags)
199 {
200         WARN_ON_ONCE(hweight32(flag) != 1);
201         *flags |= flag;
202 }
203
204 /* Test single @flag in @flags */
205 static bool _test_flag(uint32_t flag, uint32_t flags)
206 {
207         WARN_ON_ONCE(hweight32(flag) != 1);
208         return (flag & flags) ? true : false;
209 }
210
211 /* Test multiple @flags in @all_flags */
212 static bool _test_flags(uint32_t flags, uint32_t all_flags)
213 {
214         return (flags & all_flags) ? true : false;
215 }
216
217 /* Clear (multiple) @flags in @all_flags */
218 static void _clear_flags(uint32_t flags, uint32_t *all_flags)
219 {
220         *all_flags &= ~flags;
221 }
222
223 /* Return true if single @flag is set in @*flags, else set it and return false */
224 static bool _test_and_set_flag(uint32_t flag, uint32_t *flags)
225 {
226         if (_test_flag(flag, *flags))
227                 return true;
228
229         _set_flag(flag, flags);
230         return false;
231 }
232 /* ...ctr and runtime flag bit manipulation */
233
234 /* All table line arguments are defined here */
235 static struct arg_name_flag {
236         const uint32_t flag;
237         const char *name;
238 } _arg_name_flags[] = {
239         { CTR_FLAG_SYNC, "sync"},
240         { CTR_FLAG_NOSYNC, "nosync"},
241         { CTR_FLAG_REBUILD, "rebuild"},
242         { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
243         { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
244         { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
245         { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
246         { CTR_FLAG_WRITE_MOSTLY, "writemostly"},
247         { CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
248         { CTR_FLAG_REGION_SIZE, "region_size"},
249         { CTR_FLAG_RAID10_COPIES, "raid10_copies"},
250         { CTR_FLAG_RAID10_FORMAT, "raid10_format"},
251         { CTR_FLAG_DATA_OFFSET, "data_offset"},
252         { CTR_FLAG_DELTA_DISKS, "delta_disks"},
253         { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
254 };
255
256 /* Return argument name string for given @flag */
257 static const char *_argname_by_flag(const uint32_t flag)
258 {
259         if (hweight32(flag) == 1) {
260                 struct arg_name_flag *anf = _arg_name_flags + ARRAY_SIZE(_arg_name_flags);
261
262                 while (anf-- > _arg_name_flags)
263                         if (_test_flag(flag, anf->flag))
264                                 return anf->name;
265
266         } else
267                 DMERR("%s called with more than one flag!", __func__);
268
269         return NULL;
270 }
271
272 /*
273  * bool helpers to test for various raid levels of a raid set,
274  * is. it's level as reported by the superblock rather than
275  * the requested raid_type passed to the constructor.
276  */
277 /* Return true, if raid set in @rs is raid0 */
278 static bool rs_is_raid0(struct raid_set *rs)
279 {
280         return !rs->md.level;
281 }
282
283 /* Return true, if raid set in @rs is raid10 */
284 static bool rs_is_raid10(struct raid_set *rs)
285 {
286         return rs->md.level == 10;
287 }
288
289 /*
290  * bool helpers to test for various raid levels of a raid type
291  */
292
293 /* Return true, if raid type in @rt is raid0 */
294 static bool rt_is_raid0(struct raid_type *rt)
295 {
296         return !rt->level;
297 }
298
299 /* Return true, if raid type in @rt is raid1 */
300 static bool rt_is_raid1(struct raid_type *rt)
301 {
302         return rt->level == 1;
303 }
304
305 /* Return true, if raid type in @rt is raid10 */
306 static bool rt_is_raid10(struct raid_type *rt)
307 {
308         return rt->level == 10;
309 }
310
311 /* Return true, if raid type in @rt is raid4/5 */
312 static bool rt_is_raid45(struct raid_type *rt)
313 {
314         return _in_range(rt->level, 4, 5);
315 }
316
317 /* Return true, if raid type in @rt is raid6 */
318 static bool rt_is_raid6(struct raid_type *rt)
319 {
320         return rt->level == 6;
321 }
322
323 /* Return true, if raid type in @rt is raid4/5/6 */
324 static bool rt_is_raid456(struct raid_type *rt)
325 {
326         return _in_range(rt->level, 4, 6);
327 }
328 /* END: raid level bools */
329
330 /*
331  * Convenience functions to set ti->error to @errmsg and
332  * return @r in order to shorten code in a lot of places
333  */
334 static int ti_error_ret(struct dm_target *ti, const char *errmsg, int r)
335 {
336         ti->error = (char *) errmsg;
337         return r;
338 }
339
340 static int ti_error_einval(struct dm_target *ti, const char *errmsg)
341 {
342         return ti_error_ret(ti, errmsg, -EINVAL);
343 }
344 /* END: convenience functions to set ti->error to @errmsg... */
345
346 /* Return invalid ctr flags for the raid level of @rs */
347 static uint32_t _invalid_flags(struct raid_set *rs)
348 {
349         if (rt_is_raid0(rs->raid_type))
350                 return RAID0_INVALID_FLAGS;
351         else if (rt_is_raid1(rs->raid_type))
352                 return RAID1_INVALID_FLAGS;
353         else if (rt_is_raid10(rs->raid_type))
354                 return RAID10_INVALID_FLAGS;
355         else if (rt_is_raid45(rs->raid_type))
356                 return RAID45_INVALID_FLAGS;
357         else if (rt_is_raid6(rs->raid_type))
358                 return RAID6_INVALID_FLAGS;
359
360         return ~0;
361 }
362
363 /*
364  * Check for any invalid flags set on @rs defined by bitset @invalid_flags
365  *
366  * Has to be called after parsing of the ctr flags!
367  */
368 static int rs_check_for_invalid_flags(struct raid_set *rs)
369 {
370         if (_test_flags(rs->ctr_flags, _invalid_flags(rs)))
371                 return ti_error_einval(rs->ti, "Invalid flag combined");
372
373         return 0;
374 }
375
376
377 /* MD raid10 bit definitions and helpers */
378 #define RAID10_OFFSET                   (1 << 16) /* stripes with data copies area adjacent on devices */
379 #define RAID10_BROCKEN_USE_FAR_SETS     (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
380 #define RAID10_USE_FAR_SETS             (1 << 18) /* Use sets instead of whole stripe rotation */
381 #define RAID10_FAR_COPIES_SHIFT         8         /* raid10 # far copies shift (2nd byte of layout) */
382
383 /* Return md raid10 near copies for @layout */
384 static unsigned int _raid10_near_copies(int layout)
385 {
386         return layout & 0xFF;
387 }
388
389 /* Return md raid10 far copies for @layout */
390 static unsigned int _raid10_far_copies(int layout)
391 {
392         return _raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
393 }
394
395 /* Return true if md raid10 offset for @layout */
396 static unsigned int _is_raid10_offset(int layout)
397 {
398         return layout & RAID10_OFFSET;
399 }
400
401 /* Return true if md raid10 near for @layout */
402 static unsigned int _is_raid10_near(int layout)
403 {
404         return !_is_raid10_offset(layout) && _raid10_near_copies(layout) > 1;
405 }
406
407 /* Return true if md raid10 far for @layout */
408 static unsigned int _is_raid10_far(int layout)
409 {
410         return !_is_raid10_offset(layout) && _raid10_far_copies(layout) > 1;
411 }
412
413 /* Return md raid10 layout string for @layout */
414 static const char *raid10_md_layout_to_format(int layout)
415 {
416         /*
417          * Bit 16 stands for "offset"
418          * (i.e. adjacent stripes hold copies)
419          *
420          * Refer to MD's raid10.c for details
421          */
422         if (_is_raid10_offset(layout))
423                 return "offset";
424
425         if (_raid10_near_copies(layout) > 1)
426                 return "near";
427
428         WARN_ON(_raid10_far_copies(layout) < 2);
429
430         return "far";
431 }
432
433 /* Return md raid10 algorithm for @name */
434 static const int raid10_name_to_format(const char *name)
435 {
436         if (!strcasecmp(name, "near"))
437                 return ALGORITHM_RAID10_NEAR;
438         else if (!strcasecmp(name, "offset"))
439                 return ALGORITHM_RAID10_OFFSET;
440         else if (!strcasecmp(name, "far"))
441                 return ALGORITHM_RAID10_FAR;
442
443         return -EINVAL;
444 }
445
446
447 /* Return md raid10 copies for @layout */
448 static unsigned int raid10_md_layout_to_copies(int layout)
449 {
450         return _raid10_near_copies(layout) > 1 ?
451                _raid10_near_copies(layout) : _raid10_far_copies(layout);
452 }
453
454 /* Return md raid10 format id for @format string */
455 static int raid10_format_to_md_layout(struct raid_set *rs,
456                                       unsigned int algorithm,
457                                       unsigned int copies)
458 {
459         unsigned int n = 1, f = 1, r = 0;
460
461         /*
462          * MD resilienece flaw:
463          *
464          * enabling use_far_sets for far/offset formats causes copies
465          * to be colocated on the same devs together with their origins!
466          *
467          * -> disable it for now in the definition above
468          */
469         if (algorithm == ALGORITHM_RAID10_DEFAULT ||
470             algorithm == ALGORITHM_RAID10_NEAR)
471                 n = copies;
472
473         else if (algorithm == ALGORITHM_RAID10_OFFSET) {
474                 f = copies;
475                 r = RAID10_OFFSET;
476                 if (!_test_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, rs->ctr_flags))
477                         r |= RAID10_USE_FAR_SETS;
478
479         } else if (algorithm == ALGORITHM_RAID10_FAR) {
480                 f = copies;
481                 r = !RAID10_OFFSET;
482                 if (!_test_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, rs->ctr_flags))
483                         r |= RAID10_USE_FAR_SETS;
484
485         } else
486                 return -EINVAL;
487
488         return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
489 }
490 /* END: MD raid10 bit definitions and helpers */
491
492 /* Check for any of the raid10 algorithms */
493 static int _got_raid10(struct raid_type *rtp, const int layout)
494 {
495         if (rtp->level == 10) {
496                 switch (rtp->algorithm) {
497                 case ALGORITHM_RAID10_DEFAULT:
498                 case ALGORITHM_RAID10_NEAR:
499                         return _is_raid10_near(layout);
500                 case ALGORITHM_RAID10_OFFSET:
501                         return _is_raid10_offset(layout);
502                 case ALGORITHM_RAID10_FAR:
503                         return _is_raid10_far(layout);
504                 default:
505                         break;
506                 }
507         }
508
509         return 0;
510 }
511
512 /* Return raid_type for @name */
513 static struct raid_type *get_raid_type(const char *name)
514 {
515         struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
516
517         while (rtp-- > raid_types)
518                 if (!strcasecmp(rtp->name, name))
519                         return rtp;
520
521         return NULL;
522 }
523
524 /* Return raid_type for @name based derived from @level and @layout */
525 static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
526 {
527         struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
528
529         while (rtp-- > raid_types) {
530                 /* RAID10 special checks based on @layout flags/properties */
531                 if (rtp->level == level &&
532                     (_got_raid10(rtp, layout) || rtp->algorithm == layout))
533                         return rtp;
534         }
535
536         return NULL;
537 }
538
539 /*
540  * Set the mddev properties in @rs to the new
541  * ones requested by the ctr
542  */
543 static void rs_set_new(struct raid_set *rs)
544 {
545         struct mddev *mddev = &rs->md;
546
547         mddev->level = mddev->new_level;
548         mddev->layout = mddev->new_layout;
549         mddev->chunk_sectors = mddev->new_chunk_sectors;
550         mddev->delta_disks = 0;
551 }
552
553
554 static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
555 {
556         unsigned i;
557         struct raid_set *rs;
558
559         if (raid_devs <= raid_type->parity_devs)
560                 return ERR_PTR(ti_error_einval(ti, "Insufficient number of devices"));
561
562         rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
563         if (!rs)
564                 return ERR_PTR(ti_error_ret(ti, "Cannot allocate raid context", -ENOMEM));
565
566         mddev_init(&rs->md);
567
568         rs->raid_disks = raid_devs;
569         rs->delta_disks = 0;
570
571         rs->ti = ti;
572         rs->raid_type = raid_type;
573         rs->md.raid_disks = raid_devs;
574         rs->md.level = raid_type->level;
575         rs->md.new_level = rs->md.level;
576         rs->md.layout = raid_type->algorithm;
577         rs->md.new_layout = rs->md.layout;
578         rs->md.delta_disks = 0;
579         rs->md.recovery_cp = 0;
580
581         for (i = 0; i < raid_devs; i++)
582                 md_rdev_init(&rs->dev[i].rdev);
583
584         /*
585          * Remaining items to be initialized by further RAID params:
586          *  rs->md.persistent
587          *  rs->md.external
588          *  rs->md.chunk_sectors
589          *  rs->md.new_chunk_sectors
590          *  rs->md.dev_sectors
591          */
592
593         return rs;
594 }
595
596 static void context_free(struct raid_set *rs)
597 {
598         int i;
599
600         for (i = 0; i < rs->md.raid_disks; i++) {
601                 if (rs->dev[i].meta_dev)
602                         dm_put_device(rs->ti, rs->dev[i].meta_dev);
603                 md_rdev_clear(&rs->dev[i].rdev);
604                 if (rs->dev[i].data_dev)
605                         dm_put_device(rs->ti, rs->dev[i].data_dev);
606         }
607
608         kfree(rs);
609 }
610
611 /*
612  * For every device we have two words
613  *  <meta_dev>: meta device name or '-' if missing
614  *  <data_dev>: data device name or '-' if missing
615  *
616  * The following are permitted:
617  *    - -
618  *    - <data_dev>
619  *    <meta_dev> <data_dev>
620  *
621  * The following is not allowed:
622  *    <meta_dev> -
623  *
624  * This code parses those words.  If there is a failure,
625  * the caller must use context_free to unwind the operations.
626  */
627 static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
628 {
629         int i;
630         int rebuild = 0;
631         int metadata_available = 0;
632         int r = 0;
633         const char *arg;
634
635         /* Put off the number of raid devices argument to get to dev pairs */
636         arg = dm_shift_arg(as);
637         if (!arg)
638                 return -EINVAL;
639
640         for (i = 0; i < rs->md.raid_disks; i++) {
641                 rs->dev[i].rdev.raid_disk = i;
642
643                 rs->dev[i].meta_dev = NULL;
644                 rs->dev[i].data_dev = NULL;
645
646                 /*
647                  * There are no offsets, since there is a separate device
648                  * for data and metadata.
649                  */
650                 rs->dev[i].rdev.data_offset = 0;
651                 rs->dev[i].rdev.mddev = &rs->md;
652
653                 arg = dm_shift_arg(as);
654                 if (!arg)
655                         return -EINVAL;
656
657                 if (strcmp(arg, "-")) {
658                         r = dm_get_device(rs->ti, arg,
659                                             dm_table_get_mode(rs->ti->table),
660                                             &rs->dev[i].meta_dev);
661                         if (r)
662                                 return ti_error_ret(rs->ti, "RAID metadata device lookup failure", r);
663
664                         rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
665                         if (!rs->dev[i].rdev.sb_page)
666                                 return ti_error_ret(rs->ti, "Failed to allocate superblock page", -ENOMEM);
667                 }
668
669                 arg = dm_shift_arg(as);
670                 if (!arg)
671                         return -EINVAL;
672
673                 if (!strcmp(arg, "-")) {
674                         if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
675                             (!rs->dev[i].rdev.recovery_offset))
676                                 return ti_error_einval(rs->ti, "Drive designated for rebuild not specified");
677
678                         if (rs->dev[i].meta_dev)
679                                 return ti_error_einval(rs->ti, "No data device supplied with metadata device");
680
681                         continue;
682                 }
683
684                 r = dm_get_device(rs->ti, arg,
685                                     dm_table_get_mode(rs->ti->table),
686                                     &rs->dev[i].data_dev);
687                 if (r)
688                         return ti_error_ret(rs->ti, "RAID device lookup failure", r);
689
690                 if (rs->dev[i].meta_dev) {
691                         metadata_available = 1;
692                         rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
693                 }
694                 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
695                 list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
696                 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
697                         rebuild++;
698         }
699
700         if (metadata_available) {
701                 rs->md.external = 0;
702                 rs->md.persistent = 1;
703                 rs->md.major_version = 2;
704         } else if (rebuild && !rs->md.recovery_cp) {
705                 /*
706                  * Without metadata, we will not be able to tell if the array
707                  * is in-sync or not - we must assume it is not.  Therefore,
708                  * it is impossible to rebuild a drive.
709                  *
710                  * Even if there is metadata, the on-disk information may
711                  * indicate that the array is not in-sync and it will then
712                  * fail at that time.
713                  *
714                  * User could specify 'nosync' option if desperate.
715                  */
716                 DMERR("Unable to rebuild drive while array is not in-sync");
717                 return ti_error_einval(rs->ti, "Unable to rebuild drive while array is not in-sync");
718         }
719
720         return 0;
721 }
722
723 /*
724  * validate_region_size
725  * @rs
726  * @region_size:  region size in sectors.  If 0, pick a size (4MiB default).
727  *
728  * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
729  * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
730  *
731  * Returns: 0 on success, -EINVAL on failure.
732  */
733 static int validate_region_size(struct raid_set *rs, unsigned long region_size)
734 {
735         unsigned long min_region_size = rs->ti->len / (1 << 21);
736
737         if (!region_size) {
738                 /*
739                  * Choose a reasonable default.  All figures in sectors.
740                  */
741                 if (min_region_size > (1 << 13)) {
742                         /* If not a power of 2, make it the next power of 2 */
743                         region_size = roundup_pow_of_two(min_region_size);
744                         DMINFO("Choosing default region size of %lu sectors",
745                                region_size);
746                 } else {
747                         DMINFO("Choosing default region size of 4MiB");
748                         region_size = 1 << 13; /* sectors */
749                 }
750         } else {
751                 /*
752                  * Validate user-supplied value.
753                  */
754                 if (region_size > rs->ti->len)
755                         return ti_error_einval(rs->ti, "Supplied region size is too large");
756
757                 if (region_size < min_region_size) {
758                         DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
759                               region_size, min_region_size);
760                         return ti_error_einval(rs->ti, "Supplied region size is too small");
761                 }
762
763                 if (!is_power_of_2(region_size))
764                         return ti_error_einval(rs->ti, "Region size is not a power of 2");
765
766                 if (region_size < rs->md.chunk_sectors)
767                         return ti_error_einval(rs->ti, "Region size is smaller than the chunk size");
768         }
769
770         /*
771          * Convert sectors to bytes.
772          */
773         rs->md.bitmap_info.chunksize = (region_size << 9);
774
775         return 0;
776 }
777
778 /*
779  * validate_raid_redundancy
780  * @rs
781  *
782  * Determine if there are enough devices in the array that haven't
783  * failed (or are being rebuilt) to form a usable array.
784  *
785  * Returns: 0 on success, -EINVAL on failure.
786  */
787 static int validate_raid_redundancy(struct raid_set *rs)
788 {
789         unsigned i, rebuild_cnt = 0;
790         unsigned rebuilds_per_group = 0, copies, d;
791         unsigned group_size, last_group_start;
792
793         for (i = 0; i < rs->md.raid_disks; i++)
794                 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
795                     !rs->dev[i].rdev.sb_page)
796                         rebuild_cnt++;
797
798         switch (rs->raid_type->level) {
799         case 1:
800                 if (rebuild_cnt >= rs->md.raid_disks)
801                         goto too_many;
802                 break;
803         case 4:
804         case 5:
805         case 6:
806                 if (rebuild_cnt > rs->raid_type->parity_devs)
807                         goto too_many;
808                 break;
809         case 10:
810                 copies = raid10_md_layout_to_copies(rs->md.layout);
811                 if (rebuild_cnt < copies)
812                         break;
813
814                 /*
815                  * It is possible to have a higher rebuild count for RAID10,
816                  * as long as the failed devices occur in different mirror
817                  * groups (i.e. different stripes).
818                  *
819                  * When checking "near" format, make sure no adjacent devices
820                  * have failed beyond what can be handled.  In addition to the
821                  * simple case where the number of devices is a multiple of the
822                  * number of copies, we must also handle cases where the number
823                  * of devices is not a multiple of the number of copies.
824                  * E.g.    dev1 dev2 dev3 dev4 dev5
825                  *          A    A    B    B    C
826                  *          C    D    D    E    E
827                  */
828                 if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
829                         for (i = 0; i < rs->md.raid_disks * copies; i++) {
830                                 if (!(i % copies))
831                                         rebuilds_per_group = 0;
832                                 d = i % rs->md.raid_disks;
833                                 if ((!rs->dev[d].rdev.sb_page ||
834                                      !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
835                                     (++rebuilds_per_group >= copies))
836                                         goto too_many;
837                         }
838                         break;
839                 }
840
841                 /*
842                  * When checking "far" and "offset" formats, we need to ensure
843                  * that the device that holds its copy is not also dead or
844                  * being rebuilt.  (Note that "far" and "offset" formats only
845                  * support two copies right now.  These formats also only ever
846                  * use the 'use_far_sets' variant.)
847                  *
848                  * This check is somewhat complicated by the need to account
849                  * for arrays that are not a multiple of (far) copies.  This
850                  * results in the need to treat the last (potentially larger)
851                  * set differently.
852                  */
853                 group_size = (rs->md.raid_disks / copies);
854                 last_group_start = (rs->md.raid_disks / group_size) - 1;
855                 last_group_start *= group_size;
856                 for (i = 0; i < rs->md.raid_disks; i++) {
857                         if (!(i % copies) && !(i > last_group_start))
858                                 rebuilds_per_group = 0;
859                         if ((!rs->dev[i].rdev.sb_page ||
860                              !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
861                             (++rebuilds_per_group >= copies))
862                                         goto too_many;
863                 }
864                 break;
865         default:
866                 if (rebuild_cnt)
867                         return -EINVAL;
868         }
869
870         return 0;
871
872 too_many:
873         return -EINVAL;
874 }
875
876 /*
877  * Possible arguments are...
878  *      <chunk_size> [optional_args]
879  *
880  * Argument definitions
881  *    <chunk_size>                      The number of sectors per disk that
882  *                                      will form the "stripe"
883  *    [[no]sync]                        Force or prevent recovery of the
884  *                                      entire array
885  *    [rebuild <idx>]                   Rebuild the drive indicated by the index
886  *    [daemon_sleep <ms>]               Time between bitmap daemon work to
887  *                                      clear bits
888  *    [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
889  *    [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
890  *    [write_mostly <idx>]              Indicate a write mostly drive via index
891  *    [max_write_behind <sectors>]      See '-write-behind=' (man mdadm)
892  *    [stripe_cache <sectors>]          Stripe cache size for higher RAIDs
893  *    [region_size <sectors>]           Defines granularity of bitmap
894  *
895  * RAID10-only options:
896  *    [raid10_copies <# copies>]        Number of copies.  (Default: 2)
897  *    [raid10_format <near|far|offset>] Layout algorithm.  (Default: near)
898  */
899 static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
900                              unsigned num_raid_params)
901 {
902         int raid10_format = ALGORITHM_RAID10_DEFAULT;
903         unsigned raid10_copies = 2;
904         unsigned i;
905         unsigned value, region_size = 0;
906         sector_t sectors_per_dev = rs->ti->len;
907         sector_t max_io_len;
908         const char *arg, *key;
909         struct raid_dev *rd;
910         struct raid_type *rt = rs->raid_type;
911
912         arg = dm_shift_arg(as);
913         num_raid_params--; /* Account for chunk_size argument */
914
915         if (kstrtouint(arg, 10, &value) < 0)
916                 return ti_error_einval(rs->ti, "Bad numerical argument given for chunk_size");
917
918         /*
919          * First, parse the in-order required arguments
920          * "chunk_size" is the only argument of this type.
921          */
922         if (rt_is_raid1(rt)) {
923                 if (value)
924                         DMERR("Ignoring chunk size parameter for RAID 1");
925                 value = 0;
926         } else if (!is_power_of_2(value))
927                 return ti_error_einval(rs->ti, "Chunk size must be a power of 2");
928         else if (value < 8)
929                 return ti_error_einval(rs->ti, "Chunk size value is too small");
930
931         rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
932
933         /*
934          * We set each individual device as In_sync with a completed
935          * 'recovery_offset'.  If there has been a device failure or
936          * replacement then one of the following cases applies:
937          *
938          *   1) User specifies 'rebuild'.
939          *      - Device is reset when param is read.
940          *   2) A new device is supplied.
941          *      - No matching superblock found, resets device.
942          *   3) Device failure was transient and returns on reload.
943          *      - Failure noticed, resets device for bitmap replay.
944          *   4) Device hadn't completed recovery after previous failure.
945          *      - Superblock is read and overrides recovery_offset.
946          *
947          * What is found in the superblocks of the devices is always
948          * authoritative, unless 'rebuild' or '[no]sync' was specified.
949          */
950         for (i = 0; i < rs->md.raid_disks; i++) {
951                 set_bit(In_sync, &rs->dev[i].rdev.flags);
952                 rs->dev[i].rdev.recovery_offset = MaxSector;
953         }
954
955         /*
956          * Second, parse the unordered optional arguments
957          */
958         for (i = 0; i < num_raid_params; i++) {
959                 key = dm_shift_arg(as);
960                 if (!key)
961                         return ti_error_einval(rs->ti, "Not enough raid parameters given");
962
963                 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_NOSYNC))) {
964                         if (_test_and_set_flag(CTR_FLAG_NOSYNC, &rs->ctr_flags))
965                                 return ti_error_einval(rs->ti, "Only one 'nosync' argument allowed");
966                         rs->md.recovery_cp = MaxSector;
967                         continue;
968                 }
969                 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_SYNC))) {
970                         if (_test_and_set_flag(CTR_FLAG_SYNC, &rs->ctr_flags))
971                                 return ti_error_einval(rs->ti, "Only one 'sync' argument allowed");
972                         rs->md.recovery_cp = 0;
973                         continue;
974                 }
975                 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
976                         if (_test_and_set_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
977                                 return ti_error_einval(rs->ti, "Only one 'raid10_use_new_sets' argument allowed");
978                         continue;
979                 }
980
981                 arg = dm_shift_arg(as);
982                 i++; /* Account for the argument pairs */
983                 if (!arg)
984                         return ti_error_einval(rs->ti, "Wrong number of raid parameters given");
985
986                 /*
987                  * Parameters that take a string value are checked here.
988                  */
989
990                 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_RAID10_FORMAT))) {
991                         if (_test_and_set_flag(CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
992                                 return ti_error_einval(rs->ti, "Only one 'raid10_format' argument pair allowed");
993                         if (!rt_is_raid10(rt))
994                                 return ti_error_einval(rs->ti, "'raid10_format' is an invalid parameter for this RAID type");
995                         raid10_format = raid10_name_to_format(arg);
996                         if (raid10_format < 0)
997                                 return ti_error_ret(rs->ti, "Invalid 'raid10_format' value given", raid10_format);
998                         continue;
999                 }
1000
1001                 if (kstrtouint(arg, 10, &value) < 0)
1002                         return ti_error_einval(rs->ti, "Bad numerical argument given in raid params");
1003
1004                 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_REBUILD))) {
1005                         /*
1006                          * "rebuild" is being passed in by userspace to provide
1007                          * indexes of replaced devices and to set up additional
1008                          * devices on raid level takeover.
1009                          */
1010                         if (!_in_range(value, 0, rs->md.raid_disks - 1))
1011                                 return ti_error_einval(rs->ti, "Invalid rebuild index given");
1012
1013                         rd = rs->dev + value;
1014                         clear_bit(In_sync, &rd->rdev.flags);
1015                         clear_bit(Faulty, &rd->rdev.flags);
1016                         rd->rdev.recovery_offset = 0;
1017                         _set_flag(CTR_FLAG_REBUILD, &rs->ctr_flags);
1018                 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
1019                         if (!rt_is_raid1(rt))
1020                                 return ti_error_einval(rs->ti, "write_mostly option is only valid for RAID1");
1021
1022                         if (!_in_range(value, 0, rs->md.raid_disks - 1))
1023                                 return ti_error_einval(rs->ti, "Invalid write_mostly index given");
1024
1025                         set_bit(WriteMostly, &rs->dev[value].rdev.flags);
1026                         _set_flag(CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
1027                 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
1028                         if (!rt_is_raid1(rt))
1029                                 return ti_error_einval(rs->ti, "max_write_behind option is only valid for RAID1");
1030
1031                         if (_test_and_set_flag(CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
1032                                 return ti_error_einval(rs->ti, "Only one max_write_behind argument pair allowed");
1033
1034                         /*
1035                          * In device-mapper, we specify things in sectors, but
1036                          * MD records this value in kB
1037                          */
1038                         value /= 2;
1039                         if (value > COUNTER_MAX)
1040                                 return ti_error_einval(rs->ti, "Max write-behind limit out of range");
1041
1042                         rs->md.bitmap_info.max_write_behind = value;
1043                 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
1044                         if (_test_and_set_flag(CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
1045                                 return ti_error_einval(rs->ti, "Only one daemon_sleep argument pair allowed");
1046                         if (!value || (value > MAX_SCHEDULE_TIMEOUT))
1047                                 return ti_error_einval(rs->ti, "daemon sleep period out of range");
1048                         rs->md.bitmap_info.daemon_sleep = value;
1049                 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_DATA_OFFSET))) {
1050                         /* Userspace passes new data_offset after having extended the the data image LV */
1051                         if (_test_and_set_flag(CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
1052                                 return ti_error_einval(rs->ti, "Only one data_offset argument pair allowed");
1053
1054                         /* Ensure sensible data offset */
1055                         if (value < 0)
1056                                 return ti_error_einval(rs->ti, "Bogus data_offset value");
1057
1058                         rs->data_offset = value;
1059                 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_DELTA_DISKS))) {
1060                         /* Define the +/-# of disks to add to/remove from the given raid set */
1061                         if (_test_and_set_flag(CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
1062                                 return ti_error_einval(rs->ti, "Only one delta_disks argument pair allowed");
1063
1064                         /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
1065                         if (!_in_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs))
1066                                 return ti_error_einval(rs->ti, "Too many delta_disk requested");
1067
1068                         rs->delta_disks = value;
1069                 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_STRIPE_CACHE))) {
1070                         if (_test_and_set_flag(CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
1071                                 return ti_error_einval(rs->ti, "Only one stripe_cache argument pair allowed");
1072                         /*
1073                          * In device-mapper, we specify things in sectors, but
1074                          * MD records this value in kB
1075                          */
1076                         value /= 2;
1077
1078                         if (!rt_is_raid456(rt))
1079                                 return ti_error_einval(rs->ti, "Inappropriate argument: stripe_cache");
1080                         if (raid5_set_cache_size(&rs->md, (int)value))
1081                                 return ti_error_einval(rs->ti, "Bad stripe_cache size");
1082
1083                 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
1084                         if (_test_and_set_flag(CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
1085                                 return ti_error_einval(rs->ti, "Only one min_recovery_rate argument pair allowed");
1086                         if (value > INT_MAX)
1087                                 return ti_error_einval(rs->ti, "min_recovery_rate out of range");
1088                         rs->md.sync_speed_min = (int)value;
1089                 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
1090                         if (_test_and_set_flag(CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
1091                                 return ti_error_einval(rs->ti, "Only one max_recovery_rate argument pair allowed");
1092                         if (value > INT_MAX)
1093                                 return ti_error_einval(rs->ti, "max_recovery_rate out of range");
1094                         rs->md.sync_speed_max = (int)value;
1095                 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_REGION_SIZE))) {
1096                         if (_test_and_set_flag(CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
1097                                 return ti_error_einval(rs->ti, "Only one region_size argument pair allowed");
1098
1099                         region_size = value;
1100                 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_RAID10_COPIES))) {
1101                         if (_test_and_set_flag(CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
1102                                 return ti_error_einval(rs->ti, "Only one raid10_copies argument pair allowed");
1103
1104                         if (!_in_range(value, 2, rs->md.raid_disks))
1105                                 return ti_error_einval(rs->ti, "Bad value for 'raid10_copies'");
1106
1107                         raid10_copies = value;
1108                 } else {
1109                         DMERR("Unable to parse RAID parameter: %s", key);
1110                         return ti_error_einval(rs->ti, "Unable to parse RAID parameters");
1111                 }
1112         }
1113
1114         if (validate_region_size(rs, region_size))
1115                 return -EINVAL;
1116
1117         if (rs->md.chunk_sectors)
1118                 max_io_len = rs->md.chunk_sectors;
1119         else
1120                 max_io_len = region_size;
1121
1122         if (dm_set_target_max_io_len(rs->ti, max_io_len))
1123                 return -EINVAL;
1124
1125         if (rt_is_raid10(rt)) {
1126                 if (raid10_copies > rs->md.raid_disks)
1127                         return ti_error_einval(rs->ti, "Not enough devices to satisfy specification");
1128
1129                 rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
1130                 if (rs->md.new_layout < 0)
1131                         return ti_error_ret(rs->ti, "Error getting raid10 format", rs->md.new_layout);
1132
1133                 rt = get_raid_type_by_ll(10, rs->md.new_layout);
1134                 if (!rt)
1135                         return ti_error_einval(rs->ti, "Failed to recognize new raid10 layout");
1136
1137                 if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
1138                      rt->algorithm == ALGORITHM_RAID10_NEAR) &&
1139                     _test_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, rs->ctr_flags))
1140                         return ti_error_einval(rs->ti, "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible");
1141
1142                 /* (Len * #mirrors) / #devices */
1143                 sectors_per_dev = rs->ti->len * raid10_copies;
1144                 sector_div(sectors_per_dev, rs->md.raid_disks);
1145
1146                 rs->md.layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
1147                 rs->md.new_layout = rs->md.layout;
1148         } else if (!rt_is_raid1(rt) &&
1149                    sector_div(sectors_per_dev,
1150                               (rs->md.raid_disks - rt->parity_devs)))
1151                 return ti_error_einval(rs->ti, "Target length not divisible by number of data devices");
1152
1153         rs->raid10_copies = raid10_copies;
1154         rs->md.dev_sectors = sectors_per_dev;
1155
1156         /* Assume there are no metadata devices until the drives are parsed */
1157         rs->md.persistent = 0;
1158         rs->md.external = 1;
1159
1160         /* Check, if any invalid ctr arguments have been passed in for the raid level */
1161         return rs_check_for_invalid_flags(rs);
1162 }
1163
1164 static void do_table_event(struct work_struct *ws)
1165 {
1166         struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
1167
1168         dm_table_event(rs->ti->table);
1169 }
1170
1171 static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
1172 {
1173         struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
1174
1175         return mddev_congested(&rs->md, bits);
1176 }
1177
1178 /*  Features */
1179 #define FEATURE_FLAG_SUPPORTS_RESHAPE   0x1
1180
1181 /* State flags for sb->flags */
1182 #define SB_FLAG_RESHAPE_ACTIVE          0x1
1183 #define SB_FLAG_RESHAPE_BACKWARDS       0x2
1184
1185 /*
1186  * This structure is never routinely used by userspace, unlike md superblocks.
1187  * Devices with this superblock should only ever be accessed via device-mapper.
1188  */
1189 #define DM_RAID_MAGIC 0x64526D44
1190 struct dm_raid_superblock {
1191         __le32 magic;           /* "DmRd" */
1192         __le32 compat_features; /* Used to indicate compatible features (like 1.8.0 ondisk metadata extension) */
1193
1194         __le32 num_devices;     /* Number of devices in this raid set. (Max 64) */
1195         __le32 array_position;  /* The position of this drive in the raid set */
1196
1197         __le64 events;          /* Incremented by md when superblock updated */
1198         __le64 failed_devices;  /* Pre 1.8.0 part of bit field of devices to */
1199                                 /* indicate failures (see extension below) */
1200
1201         /*
1202          * This offset tracks the progress of the repair or replacement of
1203          * an individual drive.
1204          */
1205         __le64 disk_recovery_offset;
1206
1207         /*
1208          * This offset tracks the progress of the initial raid set
1209          * synchronisation/parity calculation.
1210          */
1211         __le64 array_resync_offset;
1212
1213         /*
1214          * raid characteristics
1215          */
1216         __le32 level;
1217         __le32 layout;
1218         __le32 stripe_sectors;
1219
1220         /********************************************************************
1221          * BELOW FOLLOW V1.8.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
1222          *
1223          * FEATURE_FLAG_SUPPORTS_RESHAPE in the features member indicates that those exist
1224          */
1225
1226         __le32 flags; /* Flags defining array states for reshaping */
1227
1228         /*
1229          * This offset tracks the progress of a raid
1230          * set reshape in order to be able to restart it
1231          */
1232         __le64 reshape_position;
1233
1234         /*
1235          * These define the properties of the array in case of an interrupted reshape
1236          */
1237         __le32 new_level;
1238         __le32 new_layout;
1239         __le32 new_stripe_sectors;
1240         __le32 delta_disks;
1241
1242         __le64 array_sectors; /* Array size in sectors */
1243
1244         /*
1245          * Sector offsets to data on devices (reshaping).
1246          * Needed to support out of place reshaping, thus
1247          * not writing over any stripes whilst converting
1248          * them from old to new layout
1249          */
1250         __le64 data_offset;
1251         __le64 new_data_offset;
1252
1253         __le64 sectors; /* Used device size in sectors */
1254
1255         /*
1256          * Additonal Bit field of devices indicating failures to support
1257          * up to 256 devices with the 1.8.0 on-disk metadata format
1258          */
1259         __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
1260
1261         __le32 incompat_features;       /* Used to indicate any incompatible features */
1262
1263         /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
1264 } __packed;
1265
1266 static int read_disk_sb(struct md_rdev *rdev, int size)
1267 {
1268         BUG_ON(!rdev->sb_page);
1269
1270         if (rdev->sb_loaded)
1271                 return 0;
1272
1273         if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, 1)) {
1274                 DMERR("Failed to read superblock of device at position %d",
1275                       rdev->raid_disk);
1276                 md_error(rdev->mddev, rdev);
1277                 return -EINVAL;
1278         }
1279
1280         rdev->sb_loaded = 1;
1281
1282         return 0;
1283 }
1284
1285 static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1286 {
1287         failed_devices[0] = le64_to_cpu(sb->failed_devices);
1288         memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
1289
1290         if (_test_flag(FEATURE_FLAG_SUPPORTS_RESHAPE, le32_to_cpu(sb->compat_features))) {
1291                 int i = ARRAY_SIZE(sb->extended_failed_devices);
1292
1293                 while (i--)
1294                         failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
1295         }
1296 }
1297
1298 static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1299 {
1300         int i = ARRAY_SIZE(sb->extended_failed_devices);
1301
1302         sb->failed_devices = cpu_to_le64(failed_devices[0]);
1303         while (i--)
1304                 sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
1305 }
1306
1307 /*
1308  * Synchronize the superblock members with the raid set properties
1309  *
1310  * All superblock data is little endian.
1311  */
1312 static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
1313 {
1314         bool update_failed_devices = false;
1315         unsigned int i;
1316         uint64_t failed_devices[DISKS_ARRAY_ELEMS];
1317         struct dm_raid_superblock *sb;
1318         struct raid_set *rs = container_of(mddev, struct raid_set, md);
1319
1320         /* No metadata device, no superblock */
1321         if (!rdev->meta_bdev)
1322                 return;
1323
1324         BUG_ON(!rdev->sb_page);
1325
1326         sb = page_address(rdev->sb_page);
1327
1328         sb_retrieve_failed_devices(sb, failed_devices);
1329
1330         for (i = 0; i < rs->raid_disks; i++)
1331                 if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
1332                         update_failed_devices = true;
1333                         set_bit(i, (void *) failed_devices);
1334                 }
1335
1336         if (update_failed_devices)
1337                 sb_update_failed_devices(sb, failed_devices);
1338
1339         sb->magic = cpu_to_le32(DM_RAID_MAGIC);
1340         sb->compat_features = cpu_to_le32(0); /* Don't set reshape flag yet */
1341
1342         sb->num_devices = cpu_to_le32(mddev->raid_disks);
1343         sb->array_position = cpu_to_le32(rdev->raid_disk);
1344
1345         sb->events = cpu_to_le64(mddev->events);
1346
1347         sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
1348         sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
1349
1350         sb->level = cpu_to_le32(mddev->level);
1351         sb->layout = cpu_to_le32(mddev->layout);
1352         sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
1353
1354         sb->new_level = cpu_to_le32(mddev->new_level);
1355         sb->new_layout = cpu_to_le32(mddev->new_layout);
1356         sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);
1357
1358         sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1359
1360         smp_rmb(); /* Make sure we access most recent reshape position */
1361         sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1362         if (le64_to_cpu(sb->reshape_position) != MaxSector) {
1363                 /* Flag ongoing reshape */
1364                 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);
1365
1366                 if (mddev->delta_disks < 0 || mddev->reshape_backwards)
1367                         sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
1368         } else
1369                 /* Flag no reshape */
1370                 _clear_flags(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS), &sb->flags);
1371
1372         sb->array_sectors = cpu_to_le64(mddev->array_sectors);
1373         sb->data_offset = cpu_to_le64(rdev->data_offset);
1374         sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
1375         sb->sectors = cpu_to_le64(rdev->sectors);
1376
1377         /* Zero out the rest of the payload after the size of the superblock */
1378         memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
1379 }
1380
1381 /*
1382  * super_load
1383  *
1384  * This function creates a superblock if one is not found on the device
1385  * and will decide which superblock to use if there's a choice.
1386  *
1387  * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
1388  */
1389 static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
1390 {
1391         int r;
1392         struct dm_raid_superblock *sb;
1393         struct dm_raid_superblock *refsb;
1394         uint64_t events_sb, events_refsb;
1395
1396         rdev->sb_start = 0;
1397         rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
1398         if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
1399                 DMERR("superblock size of a logical block is no longer valid");
1400                 return -EINVAL;
1401         }
1402
1403         r = read_disk_sb(rdev, rdev->sb_size);
1404         if (r)
1405                 return r;
1406
1407         sb = page_address(rdev->sb_page);
1408
1409         /*
1410          * Two cases that we want to write new superblocks and rebuild:
1411          * 1) New device (no matching magic number)
1412          * 2) Device specified for rebuild (!In_sync w/ offset == 0)
1413          */
1414         if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
1415             (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
1416                 super_sync(rdev->mddev, rdev);
1417
1418                 set_bit(FirstUse, &rdev->flags);
1419
1420                 /* Force writing of superblocks to disk */
1421                 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
1422
1423                 /* Any superblock is better than none, choose that if given */
1424                 return refdev ? 0 : 1;
1425         }
1426
1427         if (!refdev)
1428                 return 1;
1429
1430         events_sb = le64_to_cpu(sb->events);
1431
1432         refsb = page_address(refdev->sb_page);
1433         events_refsb = le64_to_cpu(refsb->events);
1434
1435         return (events_sb > events_refsb) ? 1 : 0;
1436 }
1437
1438 static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
1439 {
1440         int role;
1441         unsigned int d;
1442         struct mddev *mddev = &rs->md;
1443         uint64_t events_sb;
1444         uint64_t failed_devices[DISKS_ARRAY_ELEMS];
1445         struct dm_raid_superblock *sb;
1446         uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
1447         struct md_rdev *r;
1448         struct dm_raid_superblock *sb2;
1449
1450         sb = page_address(rdev->sb_page);
1451         events_sb = le64_to_cpu(sb->events);
1452
1453         /*
1454          * Initialise to 1 if this is a new superblock.
1455          */
1456         mddev->events = events_sb ? : 1;
1457
1458         mddev->reshape_position = MaxSector;
1459
1460         /*
1461          * Reshaping is supported, e.g. reshape_position is valid
1462          * in superblock and superblock content is authoritative.
1463          */
1464         if (_test_flag(FEATURE_FLAG_SUPPORTS_RESHAPE, le32_to_cpu(sb->compat_features))) {
1465                 /* Superblock is authoritative wrt given raid set layout! */
1466                 mddev->raid_disks = le32_to_cpu(sb->num_devices);
1467                 mddev->level = le32_to_cpu(sb->level);
1468                 mddev->layout = le32_to_cpu(sb->layout);
1469                 mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
1470                 mddev->new_level = le32_to_cpu(sb->new_level);
1471                 mddev->new_layout = le32_to_cpu(sb->new_layout);
1472                 mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
1473                 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1474                 mddev->array_sectors = le64_to_cpu(sb->array_sectors);
1475
1476                 /* raid was reshaping and got interrupted */
1477                 if (_test_flag(SB_FLAG_RESHAPE_ACTIVE, le32_to_cpu(sb->flags))) {
1478                         if (_test_flag(CTR_FLAG_DELTA_DISKS, rs->ctr_flags)) {
1479                                 DMERR("Reshape requested but raid set is still reshaping");
1480                                 return -EINVAL;
1481                         }
1482
1483                         if (mddev->delta_disks < 0 ||
1484                             (!mddev->delta_disks && _test_flag(SB_FLAG_RESHAPE_BACKWARDS, le32_to_cpu(sb->flags))))
1485                                 mddev->reshape_backwards = 1;
1486                         else
1487                                 mddev->reshape_backwards = 0;
1488
1489                         mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1490                         rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
1491                 }
1492
1493         } else {
1494                 /*
1495                  * Reshaping is not allowed, because we don't have the appropriate metadata
1496                  */
1497                 if (le32_to_cpu(sb->level) != mddev->level) {
1498                         DMERR("Reshaping/takeover raid sets not yet supported. (raid level/stripes/size change)");
1499                         return -EINVAL;
1500                 }
1501                 if (le32_to_cpu(sb->layout) != mddev->layout) {
1502                         DMERR("Reshaping raid sets not yet supported. (raid layout change)");
1503                         DMERR("  0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
1504                         DMERR("  Old layout: %s w/ %d copies",
1505                               raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
1506                               raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
1507                         DMERR("  New layout: %s w/ %d copies",
1508                               raid10_md_layout_to_format(mddev->layout),
1509                               raid10_md_layout_to_copies(mddev->layout));
1510                         return -EINVAL;
1511                 }
1512                 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
1513                         DMERR("Reshaping raid sets not yet supported. (stripe sectors change)");
1514                         return -EINVAL;
1515                 }
1516
1517                 /* We can only change the number of devices in raid1 with old (i.e. pre 1.0.7) metadata */
1518                 if (!rt_is_raid1(rs->raid_type) &&
1519                     (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
1520                         DMERR("Reshaping raid sets not yet supported. (device count change from %u to %u)",
1521                               sb->num_devices, mddev->raid_disks);
1522                         return -EINVAL;
1523                 }
1524
1525                 /* Table line is checked vs. authoritative superblock */
1526                 rs_set_new(rs);
1527         }
1528
1529         if (!_test_flag(CTR_FLAG_NOSYNC, rs->ctr_flags))
1530                 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
1531
1532         /*
1533          * During load, we set FirstUse if a new superblock was written.
1534          * There are two reasons we might not have a superblock:
1535          * 1) The raid set is brand new - in which case, all of the
1536          *    devices must have their In_sync bit set.  Also,
1537          *    recovery_cp must be 0, unless forced.
1538          * 2) This is a new device being added to an old raid set
1539          *    and the new device needs to be rebuilt - in which
1540          *    case the In_sync bit will /not/ be set and
1541          *    recovery_cp must be MaxSector.
1542          */
1543         d = 0;
1544         rdev_for_each(r, mddev) {
1545                 if (test_bit(FirstUse, &r->flags))
1546                         new_devs++;
1547
1548                 if (!test_bit(In_sync, &r->flags)) {
1549                         DMINFO("Device %d specified for rebuild; clearing superblock",
1550                                 r->raid_disk);
1551                         rebuilds++;
1552
1553                         if (test_bit(FirstUse, &r->flags))
1554                                 rebuild_and_new++;
1555                 }
1556
1557                 d++;
1558         }
1559
1560         if (new_devs == rs->raid_disks || !rebuilds) {
1561                 /* Replace a broken device */
1562                 if (new_devs == 1 && !rs->delta_disks)
1563                         ;
1564                 if (new_devs == rs->raid_disks) {
1565                         DMINFO("Superblocks created for new raid set");
1566                         set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
1567                         mddev->recovery_cp = 0;
1568                 } else if (new_devs && new_devs != rs->raid_disks && !rebuilds) {
1569                         DMERR("New device injected into existing raid set without "
1570                               "'delta_disks' or 'rebuild' parameter specified");
1571                         return -EINVAL;
1572                 }
1573         } else if (new_devs && new_devs != rebuilds) {
1574                 DMERR("%u 'rebuild' devices cannot be injected into"
1575                       " a raid set with %u other first-time devices",
1576                       rebuilds, new_devs);
1577                 return -EINVAL;
1578         } else if (rebuilds) {
1579                 if (rebuild_and_new && rebuilds != rebuild_and_new) {
1580                         DMERR("new device%s provided without 'rebuild'",
1581                               new_devs > 1 ? "s" : "");
1582                         return -EINVAL;
1583                 } else if (mddev->recovery_cp != MaxSector) {
1584                         DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
1585                               (unsigned long long) mddev->recovery_cp);
1586                         return -EINVAL;
1587                 } else if (mddev->reshape_position != MaxSector) {
1588                         DMERR("'rebuild' specified while raid set is being reshaped");
1589                         return -EINVAL;
1590                 }
1591         }
1592
1593         /*
1594          * Now we set the Faulty bit for those devices that are
1595          * recorded in the superblock as failed.
1596          */
1597         sb_retrieve_failed_devices(sb, failed_devices);
1598         rdev_for_each(r, mddev) {
1599                 if (!r->sb_page)
1600                         continue;
1601                 sb2 = page_address(r->sb_page);
1602                 sb2->failed_devices = 0;
1603                 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
1604
1605                 /*
1606                  * Check for any device re-ordering.
1607                  */
1608                 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
1609                         role = le32_to_cpu(sb2->array_position);
1610                         if (role < 0)
1611                                 continue;
1612
1613                         if (role != r->raid_disk) {
1614                                 if (_is_raid10_near(mddev->layout)) {
1615                                         if (mddev->raid_disks % _raid10_near_copies(mddev->layout) ||
1616                                             rs->raid_disks % rs->raid10_copies)
1617                                                 return ti_error_einval(rs->ti, "Cannot change raid10 near "
1618                                                                                "set to odd # of devices!");
1619
1620                                         sb2->array_position = cpu_to_le32(r->raid_disk);
1621
1622                                 } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
1623                                     !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
1624                                     !rt_is_raid1(rs->raid_type))
1625                                         return ti_error_einval(rs->ti, "Cannot change device positions in raid set");
1626
1627                                 DMINFO("raid device #%d now at position #%d",
1628                                        role, r->raid_disk);
1629                         }
1630
1631                         /*
1632                          * Partial recovery is performed on
1633                          * returning failed devices.
1634                          */
1635                         if (test_bit(role, (void *) failed_devices))
1636                                 set_bit(Faulty, &r->flags);
1637                 }
1638         }
1639
1640         return 0;
1641 }
1642
1643 static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
1644 {
1645         struct mddev *mddev = &rs->md;
1646         struct dm_raid_superblock *sb;
1647
1648         if (!rdev->sb_page)
1649                 return 0;
1650
1651         sb = page_address(rdev->sb_page);
1652
1653         /*
1654          * If mddev->events is not set, we know we have not yet initialized
1655          * the array.
1656          */
1657         if (!mddev->events && super_init_validation(rs, rdev))
1658                 return -EINVAL;
1659
1660         if (sb->compat_features || sb->incompat_features) {
1661                 rs->ti->error = "Unable to assemble array: No feature flags supported yet";
1662                 return -EINVAL;
1663         }
1664
1665         /* Enable bitmap creation for RAID levels != 0 */
1666         mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096);
1667         rdev->mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
1668
1669         if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
1670                 /* Retrieve device size stored in superblock to be prepared for shrink */
1671                 rdev->sectors = le64_to_cpu(sb->sectors);
1672                 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
1673                 if (rdev->recovery_offset == MaxSector)
1674                         set_bit(In_sync, &rdev->flags);
1675                 /*
1676                  * If no reshape in progress -> we're recovering single
1677                  * disk(s) and have to set the device(s) to out-of-sync
1678                  */
1679                 else if (rs->md.reshape_position == MaxSector)
1680                         clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
1681         }
1682
1683         /*
1684          * If a device comes back, set it as not In_sync and no longer faulty.
1685          */
1686         if (test_and_clear_bit(Faulty, &rdev->flags)) {
1687                 rdev->recovery_offset = 0;
1688                 clear_bit(In_sync, &rdev->flags);
1689                 rdev->saved_raid_disk = rdev->raid_disk;
1690         }
1691
1692         /* Reshape support -> restore repective data offsets */
1693         rdev->data_offset = le64_to_cpu(sb->data_offset);
1694         rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
1695
1696         return 0;
1697 }
1698
1699 /*
1700  * Analyse superblocks and select the freshest.
1701  */
1702 static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
1703 {
1704         int r;
1705         struct raid_dev *dev;
1706         struct md_rdev *rdev, *tmp, *freshest;
1707         struct mddev *mddev = &rs->md;
1708
1709         freshest = NULL;
1710         rdev_for_each_safe(rdev, tmp, mddev) {
1711                 /*
1712                  * Skipping super_load due to CTR_FLAG_SYNC will cause
1713                  * the array to undergo initialization again as
1714                  * though it were new.  This is the intended effect
1715                  * of the "sync" directive.
1716                  *
1717                  * When reshaping capability is added, we must ensure
1718                  * that the "sync" directive is disallowed during the
1719                  * reshape.
1720                  */
1721                 rdev->sectors = to_sector(i_size_read(rdev->bdev->bd_inode));
1722
1723                 if (_test_flag(CTR_FLAG_SYNC, rs->ctr_flags))
1724                         continue;
1725
1726                 if (!rdev->meta_bdev)
1727                         continue;
1728
1729                 r = super_load(rdev, freshest);
1730
1731                 switch (r) {
1732                 case 1:
1733                         freshest = rdev;
1734                         break;
1735                 case 0:
1736                         break;
1737                 default:
1738                         dev = container_of(rdev, struct raid_dev, rdev);
1739                         if (dev->meta_dev)
1740                                 dm_put_device(ti, dev->meta_dev);
1741
1742                         dev->meta_dev = NULL;
1743                         rdev->meta_bdev = NULL;
1744
1745                         if (rdev->sb_page)
1746                                 put_page(rdev->sb_page);
1747
1748                         rdev->sb_page = NULL;
1749
1750                         rdev->sb_loaded = 0;
1751
1752                         /*
1753                          * We might be able to salvage the data device
1754                          * even though the meta device has failed.  For
1755                          * now, we behave as though '- -' had been
1756                          * set for this device in the table.
1757                          */
1758                         if (dev->data_dev)
1759                                 dm_put_device(ti, dev->data_dev);
1760
1761                         dev->data_dev = NULL;
1762                         rdev->bdev = NULL;
1763
1764                         list_del(&rdev->same_set);
1765                 }
1766         }
1767
1768         if (!freshest)
1769                 return 0;
1770
1771         if (validate_raid_redundancy(rs))
1772                 return ti_error_einval(rs->ti, "Insufficient redundancy to activate array");
1773
1774         /*
1775          * Validation of the freshest device provides the source of
1776          * validation for the remaining devices.
1777          */
1778         if (super_validate(rs, freshest))
1779                 return ti_error_einval(rs->ti, "Unable to assemble array: Invalid superblocks");
1780
1781         rdev_for_each(rdev, mddev)
1782                 if ((rdev != freshest) && super_validate(rs, rdev))
1783                         return -EINVAL;
1784
1785         return 0;
1786 }
1787
1788 /*
1789  * Enable/disable discard support on RAID set depending on
1790  * RAID level and discard properties of underlying RAID members.
1791  */
1792 static void configure_discard_support(struct dm_target *ti, struct raid_set *rs)
1793 {
1794         int i;
1795         bool raid456;
1796
1797         /* Assume discards not supported until after checks below. */
1798         ti->discards_supported = false;
1799
1800         /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
1801         raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
1802
1803         for (i = 0; i < rs->md.raid_disks; i++) {
1804                 struct request_queue *q;
1805
1806                 if (!rs->dev[i].rdev.bdev)
1807                         continue;
1808
1809                 q = bdev_get_queue(rs->dev[i].rdev.bdev);
1810                 if (!q || !blk_queue_discard(q))
1811                         return;
1812
1813                 if (raid456) {
1814                         if (!q->limits.discard_zeroes_data)
1815                                 return;
1816                         if (!devices_handle_discard_safely) {
1817                                 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
1818                                 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
1819                                 return;
1820                         }
1821                 }
1822         }
1823
1824         /* All RAID members properly support discards */
1825         ti->discards_supported = true;
1826
1827         /*
1828          * RAID1 and RAID10 personalities require bio splitting,
1829          * RAID0/4/5/6 don't and process large discard bios properly.
1830          */
1831         ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
1832         ti->num_discard_bios = 1;
1833 }
1834
1835 /*
1836  * Construct a RAID0/1/10/4/5/6 mapping:
1837  * Args:
1838  *      <raid_type> <#raid_params> <raid_params>{0,}    \
1839  *      <#raid_devs> [<meta_dev1> <dev1>]{1,}
1840  *
1841  * <raid_params> varies by <raid_type>.  See 'parse_raid_params' for
1842  * details on possible <raid_params>.
1843  *
1844  * Userspace is free to initialize the metadata devices, hence the superblocks to
1845  * enforce recreation based on the passed in table parameters.
1846  *
1847  */
1848 static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
1849 {
1850         int r;
1851         struct raid_type *rt;
1852         unsigned num_raid_params, num_raid_devs;
1853         struct raid_set *rs = NULL;
1854         const char *arg;
1855         struct dm_arg_set as = { argc, argv }, as_nrd;
1856         struct dm_arg _args[] = {
1857                 { 0, as.argc, "Cannot understand number of raid parameters" },
1858                 { 1, 254, "Cannot understand number of raid devices parameters" }
1859         };
1860
1861         /* Must have <raid_type> */
1862         arg = dm_shift_arg(&as);
1863         if (!arg)
1864                 return ti_error_einval(rs->ti, "No arguments");
1865
1866         rt = get_raid_type(arg);
1867         if (!rt)
1868                 return ti_error_einval(rs->ti, "Unrecognised raid_type");
1869
1870         /* Must have <#raid_params> */
1871         if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
1872                 return -EINVAL;
1873
1874         /* number of raid device tupples <meta_dev data_dev> */
1875         as_nrd = as;
1876         dm_consume_args(&as_nrd, num_raid_params);
1877         _args[1].max = (as_nrd.argc - 1) / 2;
1878         if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
1879                 return -EINVAL;
1880
1881         if (!_in_range(num_raid_devs, 1, MAX_RAID_DEVICES))
1882                 return ti_error_einval(rs->ti, "Invalid number of supplied raid devices");
1883
1884         rs = context_alloc(ti, rt, num_raid_devs);
1885         if (IS_ERR(rs))
1886                 return PTR_ERR(rs);
1887
1888         r = parse_raid_params(rs, &as, num_raid_params);
1889         if (r)
1890                 goto bad;
1891
1892         r = parse_dev_params(rs, &as);
1893         if (r)
1894                 goto bad;
1895
1896         rs->md.sync_super = super_sync;
1897         r = analyse_superblocks(ti, rs);
1898         if (r)
1899                 goto bad;
1900
1901         INIT_WORK(&rs->md.event_work, do_table_event);
1902         ti->private = rs;
1903         ti->num_flush_bios = 1;
1904
1905         /*
1906          * Disable/enable discard support on RAID set.
1907          */
1908         configure_discard_support(ti, rs);
1909
1910         /* Has to be held on running the array */
1911         mddev_lock_nointr(&rs->md);
1912         r = md_run(&rs->md);
1913         rs->md.in_sync = 0; /* Assume already marked dirty */
1914         mddev_unlock(&rs->md);
1915
1916         if (r) {
1917                 ti->error = "Fail to run raid array";
1918                 goto bad;
1919         }
1920
1921         if (ti->len != rs->md.array_sectors) {
1922                 r = ti_error_einval(ti, "Array size does not match requested target length");
1923                 goto size_mismatch;
1924         }
1925         rs->callbacks.congested_fn = raid_is_congested;
1926         dm_table_add_target_callbacks(ti->table, &rs->callbacks);
1927
1928         mddev_suspend(&rs->md);
1929         return 0;
1930
1931 size_mismatch:
1932         md_stop(&rs->md);
1933 bad:
1934         context_free(rs);
1935
1936         return r;
1937 }
1938
1939 static void raid_dtr(struct dm_target *ti)
1940 {
1941         struct raid_set *rs = ti->private;
1942
1943         list_del_init(&rs->callbacks.list);
1944         md_stop(&rs->md);
1945         context_free(rs);
1946 }
1947
1948 static int raid_map(struct dm_target *ti, struct bio *bio)
1949 {
1950         struct raid_set *rs = ti->private;
1951         struct mddev *mddev = &rs->md;
1952
1953         mddev->pers->make_request(mddev, bio);
1954
1955         return DM_MAPIO_SUBMITTED;
1956 }
1957
1958 static const char *decipher_sync_action(struct mddev *mddev)
1959 {
1960         if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
1961                 return "frozen";
1962
1963         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1964             (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
1965                 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1966                         return "reshape";
1967
1968                 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1969                         if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1970                                 return "resync";
1971                         else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1972                                 return "check";
1973                         return "repair";
1974                 }
1975
1976                 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
1977                         return "recover";
1978         }
1979
1980         return "idle";
1981 }
1982
1983 static void raid_status(struct dm_target *ti, status_type_t type,
1984                         unsigned status_flags, char *result, unsigned maxlen)
1985 {
1986         struct raid_set *rs = ti->private;
1987         unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
1988         unsigned sz = 0;
1989         int i, array_in_sync = 0;
1990         sector_t sync;
1991
1992         switch (type) {
1993         case STATUSTYPE_INFO:
1994                 DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
1995
1996                 if (!rt_is_raid0(rs->raid_type)) {
1997                         if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
1998                                 sync = rs->md.curr_resync_completed;
1999                         else
2000                                 sync = rs->md.recovery_cp;
2001
2002                         if (sync >= rs->md.resync_max_sectors) {
2003                                 /*
2004                                  * Sync complete.
2005                                  */
2006                                 array_in_sync = 1;
2007                                 sync = rs->md.resync_max_sectors;
2008                         } else if (test_bit(MD_RECOVERY_REQUESTED, &rs->md.recovery)) {
2009                                 /*
2010                                  * If "check" or "repair" is occurring, the array has
2011                                  * undergone and initial sync and the health characters
2012                                  * should not be 'a' anymore.
2013                                  */
2014                                 array_in_sync = 1;
2015                         } else {
2016                                 /*
2017                                  * The array may be doing an initial sync, or it may
2018                                  * be rebuilding individual components.  If all the
2019                                  * devices are In_sync, then it is the array that is
2020                                  * being initialized.
2021                                  */
2022                                 for (i = 0; i < rs->md.raid_disks; i++)
2023                                         if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
2024                                                 array_in_sync = 1;
2025                         }
2026                 } else {
2027                         /* RAID0 */
2028                         array_in_sync = 1;
2029                         sync = rs->md.resync_max_sectors;
2030                 }
2031
2032                 /*
2033                  * Status characters:
2034                  *  'D' = Dead/Failed device
2035                  *  'a' = Alive but not in-sync
2036                  *  'A' = Alive and in-sync
2037                  */
2038                 for (i = 0; i < rs->md.raid_disks; i++) {
2039                         if (test_bit(Faulty, &rs->dev[i].rdev.flags))
2040                                 DMEMIT("D");
2041                         else if (!array_in_sync ||
2042                                  !test_bit(In_sync, &rs->dev[i].rdev.flags))
2043                                 DMEMIT("a");
2044                         else
2045                                 DMEMIT("A");
2046                 }
2047
2048                 /*
2049                  * In-sync ratio:
2050                  *  The in-sync ratio shows the progress of:
2051                  *   - Initializing the array
2052                  *   - Rebuilding a subset of devices of the array
2053                  *  The user can distinguish between the two by referring
2054                  *  to the status characters.
2055                  */
2056                 DMEMIT(" %llu/%llu",
2057                        (unsigned long long) sync,
2058                        (unsigned long long) rs->md.resync_max_sectors);
2059
2060                 /*
2061                  * Sync action:
2062                  *   See Documentation/device-mapper/dm-raid.c for
2063                  *   information on each of these states.
2064                  */
2065                 DMEMIT(" %s", decipher_sync_action(&rs->md));
2066
2067                 /*
2068                  * resync_mismatches/mismatch_cnt
2069                  *   This field shows the number of discrepancies found when
2070                  *   performing a "check" of the array.
2071                  */
2072                 DMEMIT(" %llu",
2073                        (strcmp(rs->md.last_sync_action, "check")) ? 0 :
2074                        (unsigned long long)
2075                        atomic64_read(&rs->md.resync_mismatches));
2076                 break;
2077         case STATUSTYPE_TABLE:
2078                 /* The string you would use to construct this array */
2079                 for (i = 0; i < rs->md.raid_disks; i++) {
2080                         if (_test_flag(CTR_FLAG_REBUILD, rs->ctr_flags) &&
2081                             rs->dev[i].data_dev &&
2082                             !test_bit(In_sync, &rs->dev[i].rdev.flags))
2083                                 raid_param_cnt += 2; /* for rebuilds */
2084                         if (rs->dev[i].data_dev &&
2085                             test_bit(WriteMostly, &rs->dev[i].rdev.flags))
2086                                 raid_param_cnt += 2;
2087                 }
2088
2089                 raid_param_cnt += (hweight32(rs->ctr_flags & ~CTR_FLAG_REBUILD) * 2);
2090                 if (rs->ctr_flags & (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC))
2091                         raid_param_cnt--;
2092
2093                 DMEMIT("%s %u %u", rs->raid_type->name,
2094                        raid_param_cnt, rs->md.chunk_sectors);
2095
2096                 if (_test_flag(CTR_FLAG_SYNC, rs->ctr_flags) &&
2097                     rs->md.recovery_cp == MaxSector)
2098                         DMEMIT(" sync");
2099                 if (_test_flag(CTR_FLAG_NOSYNC, rs->ctr_flags))
2100                         DMEMIT(" nosync");
2101
2102                 for (i = 0; i < rs->md.raid_disks; i++)
2103                         if (_test_flag(CTR_FLAG_REBUILD, rs->ctr_flags) &&
2104                             rs->dev[i].data_dev &&
2105                             !test_bit(In_sync, &rs->dev[i].rdev.flags))
2106                                 DMEMIT(" rebuild %u", i);
2107
2108                 if (_test_flag(CTR_FLAG_DAEMON_SLEEP, rs->ctr_flags))
2109                         DMEMIT(" daemon_sleep %lu",
2110                                rs->md.bitmap_info.daemon_sleep);
2111
2112                 if (_test_flag(CTR_FLAG_MIN_RECOVERY_RATE, rs->ctr_flags))
2113                         DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
2114
2115                 if (_test_flag(CTR_FLAG_MAX_RECOVERY_RATE, rs->ctr_flags))
2116                         DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
2117
2118                 for (i = 0; i < rs->md.raid_disks; i++)
2119                         if (rs->dev[i].data_dev &&
2120                             test_bit(WriteMostly, &rs->dev[i].rdev.flags))
2121                                 DMEMIT(" write_mostly %u", i);
2122
2123                 if (_test_flag(CTR_FLAG_MAX_WRITE_BEHIND, rs->ctr_flags))
2124                         DMEMIT(" max_write_behind %lu",
2125                                rs->md.bitmap_info.max_write_behind);
2126
2127                 if (_test_flag(CTR_FLAG_STRIPE_CACHE, rs->ctr_flags)) {
2128                         struct r5conf *conf = rs->md.private;
2129
2130                         /* convert from kiB to sectors */
2131                         DMEMIT(" stripe_cache %d",
2132                                conf ? conf->max_nr_stripes * 2 : 0);
2133                 }
2134
2135                 if (_test_flag(CTR_FLAG_REGION_SIZE, rs->ctr_flags))
2136                         DMEMIT(" region_size %lu",
2137                                rs->md.bitmap_info.chunksize >> 9);
2138
2139                 if (_test_flag(CTR_FLAG_RAID10_COPIES, rs->ctr_flags))
2140                         DMEMIT(" raid10_copies %u",
2141                                raid10_md_layout_to_copies(rs->md.layout));
2142
2143                 if (_test_flag(CTR_FLAG_RAID10_FORMAT, rs->ctr_flags))
2144                         DMEMIT(" raid10_format %s",
2145                                raid10_md_layout_to_format(rs->md.layout));
2146
2147                 DMEMIT(" %d", rs->md.raid_disks);
2148                 for (i = 0; i < rs->md.raid_disks; i++) {
2149                         if (rs->dev[i].meta_dev)
2150                                 DMEMIT(" %s", rs->dev[i].meta_dev->name);
2151                         else
2152                                 DMEMIT(" -");
2153
2154                         if (rs->dev[i].data_dev)
2155                                 DMEMIT(" %s", rs->dev[i].data_dev->name);
2156                         else
2157                                 DMEMIT(" -");
2158                 }
2159         }
2160 }
2161
2162 static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
2163 {
2164         struct raid_set *rs = ti->private;
2165         struct mddev *mddev = &rs->md;
2166
2167         if (!strcasecmp(argv[0], "reshape")) {
2168                 DMERR("Reshape not supported.");
2169                 return -EINVAL;
2170         }
2171
2172         if (!mddev->pers || !mddev->pers->sync_request)
2173                 return -EINVAL;
2174
2175         if (!strcasecmp(argv[0], "frozen"))
2176                 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
2177         else
2178                 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
2179
2180         if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
2181                 if (mddev->sync_thread) {
2182                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2183                         md_reap_sync_thread(mddev);
2184                 }
2185         } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2186                    test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2187                 return -EBUSY;
2188         else if (!strcasecmp(argv[0], "resync"))
2189                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2190         else if (!strcasecmp(argv[0], "recover")) {
2191                 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2192                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2193         } else {
2194                 if (!strcasecmp(argv[0], "check"))
2195                         set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2196                 else if (!!strcasecmp(argv[0], "repair"))
2197                         return -EINVAL;
2198                 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2199                 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2200         }
2201         if (mddev->ro == 2) {
2202                 /* A write to sync_action is enough to justify
2203                  * canceling read-auto mode
2204                  */
2205                 mddev->ro = 0;
2206                 if (!mddev->suspended)
2207                         md_wakeup_thread(mddev->sync_thread);
2208         }
2209         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2210         if (!mddev->suspended)
2211                 md_wakeup_thread(mddev->thread);
2212
2213         return 0;
2214 }
2215
2216 static int raid_iterate_devices(struct dm_target *ti,
2217                                 iterate_devices_callout_fn fn, void *data)
2218 {
2219         struct raid_set *rs = ti->private;
2220         unsigned i;
2221         int r = 0;
2222
2223         for (i = 0; !r && i < rs->md.raid_disks; i++)
2224                 if (rs->dev[i].data_dev)
2225                         r = fn(ti,
2226                                  rs->dev[i].data_dev,
2227                                  0, /* No offset on data devs */
2228                                  rs->md.dev_sectors,
2229                                  data);
2230
2231         return r;
2232 }
2233
2234 static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
2235 {
2236         struct raid_set *rs = ti->private;
2237         unsigned chunk_size = rs->md.chunk_sectors << 9;
2238         struct r5conf *conf = rs->md.private;
2239
2240         blk_limits_io_min(limits, chunk_size);
2241         blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
2242 }
2243
2244 static void raid_presuspend(struct dm_target *ti)
2245 {
2246         struct raid_set *rs = ti->private;
2247
2248         md_stop_writes(&rs->md);
2249 }
2250
2251 static void raid_postsuspend(struct dm_target *ti)
2252 {
2253         struct raid_set *rs = ti->private;
2254
2255         mddev_suspend(&rs->md);
2256 }
2257
2258 static void attempt_restore_of_faulty_devices(struct raid_set *rs)
2259 {
2260         int i;
2261         uint64_t failed_devices, cleared_failed_devices = 0;
2262         unsigned long flags;
2263         struct dm_raid_superblock *sb;
2264         struct md_rdev *r;
2265
2266         for (i = 0; i < rs->md.raid_disks; i++) {
2267                 r = &rs->dev[i].rdev;
2268                 if (test_bit(Faulty, &r->flags) && r->sb_page &&
2269                     sync_page_io(r, 0, r->sb_size, r->sb_page, REQ_OP_READ, 0,
2270                                  1)) {
2271                         DMINFO("Faulty %s device #%d has readable super block."
2272                                "  Attempting to revive it.",
2273                                rs->raid_type->name, i);
2274
2275                         /*
2276                          * Faulty bit may be set, but sometimes the array can
2277                          * be suspended before the personalities can respond
2278                          * by removing the device from the array (i.e. calling
2279                          * 'hot_remove_disk').  If they haven't yet removed
2280                          * the failed device, its 'raid_disk' number will be
2281                          * '>= 0' - meaning we must call this function
2282                          * ourselves.
2283                          */
2284                         if ((r->raid_disk >= 0) &&
2285                             (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0))
2286                                 /* Failed to revive this device, try next */
2287                                 continue;
2288
2289                         r->raid_disk = i;
2290                         r->saved_raid_disk = i;
2291                         flags = r->flags;
2292                         clear_bit(Faulty, &r->flags);
2293                         clear_bit(WriteErrorSeen, &r->flags);
2294                         clear_bit(In_sync, &r->flags);
2295                         if (r->mddev->pers->hot_add_disk(r->mddev, r)) {
2296                                 r->raid_disk = -1;
2297                                 r->saved_raid_disk = -1;
2298                                 r->flags = flags;
2299                         } else {
2300                                 r->recovery_offset = 0;
2301                                 cleared_failed_devices |= 1 << i;
2302                         }
2303                 }
2304         }
2305         if (cleared_failed_devices) {
2306                 rdev_for_each(r, &rs->md) {
2307                         sb = page_address(r->sb_page);
2308                         failed_devices = le64_to_cpu(sb->failed_devices);
2309                         failed_devices &= ~cleared_failed_devices;
2310                         sb->failed_devices = cpu_to_le64(failed_devices);
2311                 }
2312         }
2313 }
2314
2315 static void raid_resume(struct dm_target *ti)
2316 {
2317         struct raid_set *rs = ti->private;
2318
2319         if (!rt_is_raid0(rs->raid_type)) {
2320                 set_bit(MD_CHANGE_DEVS, &rs->md.flags);
2321
2322                 if (!rs->bitmap_loaded) {
2323                         bitmap_load(&rs->md);
2324                         rs->bitmap_loaded = 1;
2325                 } else {
2326                         /*
2327                          * A secondary resume while the device is active.
2328                          * Take this opportunity to check whether any failed
2329                          * devices are reachable again.
2330                          */
2331                         attempt_restore_of_faulty_devices(rs);
2332                 }
2333
2334                 clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
2335         }
2336
2337         mddev_resume(&rs->md);
2338 }
2339
2340 static struct target_type raid_target = {
2341         .name = "raid",
2342         .version = {1, 8, 1},
2343         .module = THIS_MODULE,
2344         .ctr = raid_ctr,
2345         .dtr = raid_dtr,
2346         .map = raid_map,
2347         .status = raid_status,
2348         .message = raid_message,
2349         .iterate_devices = raid_iterate_devices,
2350         .io_hints = raid_io_hints,
2351         .presuspend = raid_presuspend,
2352         .postsuspend = raid_postsuspend,
2353         .resume = raid_resume,
2354 };
2355
2356 static int __init dm_raid_init(void)
2357 {
2358         DMINFO("Loading target version %u.%u.%u",
2359                raid_target.version[0],
2360                raid_target.version[1],
2361                raid_target.version[2]);
2362         return dm_register_target(&raid_target);
2363 }
2364
2365 static void __exit dm_raid_exit(void)
2366 {
2367         dm_unregister_target(&raid_target);
2368 }
2369
2370 module_init(dm_raid_init);
2371 module_exit(dm_raid_exit);
2372
2373 module_param(devices_handle_discard_safely, bool, 0644);
2374 MODULE_PARM_DESC(devices_handle_discard_safely,
2375                  "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
2376
2377 MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
2378 MODULE_ALIAS("dm-raid1");
2379 MODULE_ALIAS("dm-raid10");
2380 MODULE_ALIAS("dm-raid4");
2381 MODULE_ALIAS("dm-raid5");
2382 MODULE_ALIAS("dm-raid6");
2383 MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
2384 MODULE_LICENSE("GPL");