2 * Copyright (C) 2010-2011 Neil Brown
3 * Copyright (C) 2010-2016 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
8 #include <linux/slab.h>
9 #include <linux/module.h>
17 #include <linux/device-mapper.h>
19 #define DM_MSG_PREFIX "raid"
20 #define MAX_RAID_DEVICES 253 /* md-raid kernel limit */
22 static bool devices_handle_discard_safely = false;
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.
28 #define FirstUse 10 /* rdev flag */
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
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 ------|
43 struct dm_dev *meta_dev;
44 struct dm_dev *data_dev;
49 * Flags for rs->ctr_flags field.
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 */
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! */
72 * Definitions of various constructor flags to
73 * be used in checks of valid / invalid flags
76 /* Define all any sync flags */
77 #define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)
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)
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 | \
97 /* All ctr optional arguments */
98 #define ALL_CTR_FLAGS (CTR_FLAG_OPTIONS_NO_ARGS | \
99 CTR_FLAG_OPTIONS_ONE_ARG)
101 /* Invalid options definitions per raid level... */
103 /* "raid0" does not accept any options */
104 #define RAID0_INVALID_FLAGS ALL_CTR_FLAGS
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)
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)
118 * "raid4/5/6" do not accept any raid1 or raid10 specific options
120 * "raid6" does not accept "nosync", because it is not guaranteed
121 * that both parity and q-syndrome are being written properly with
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 */
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)
136 struct dm_target *ti;
138 uint32_t bitmap_loaded;
147 struct raid_type *raid_type;
148 struct dm_target_callbacks callbacks;
150 struct raid_dev dev[0];
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
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. */
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}
190 /* True, if @v is in inclusive range [@min, @max] */
191 static bool _in_range(long v, long min, long max)
193 return v >= min && v <= max;
196 /* ctr flag bit manipulation... */
197 /* Set single @flag in @flags */
198 static void _set_flag(uint32_t flag, uint32_t *flags)
200 WARN_ON_ONCE(hweight32(flag) != 1);
204 /* Test single @flag in @flags */
205 static bool _test_flag(uint32_t flag, uint32_t flags)
207 WARN_ON_ONCE(hweight32(flag) != 1);
208 return (flag & flags) ? true : false;
211 /* Test multiple @flags in @all_flags */
212 static bool _test_flags(uint32_t flags, uint32_t all_flags)
214 return (flags & all_flags) ? true : false;
217 /* Clear (multiple) @flags in @all_flags */
218 static void _clear_flags(uint32_t flags, uint32_t *all_flags)
220 *all_flags &= ~flags;
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)
226 if (_test_flag(flag, *flags))
229 _set_flag(flag, flags);
232 /* ...ctr and runtime flag bit manipulation */
234 /* All table line arguments are defined here */
235 static struct arg_name_flag {
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"},
256 /* Return argument name string for given @flag */
257 static const char *_argname_by_flag(const uint32_t flag)
259 if (hweight32(flag) == 1) {
260 struct arg_name_flag *anf = _arg_name_flags + ARRAY_SIZE(_arg_name_flags);
262 while (anf-- > _arg_name_flags)
263 if (_test_flag(flag, anf->flag))
267 DMERR("%s called with more than one flag!", __func__);
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.
277 /* Return true, if raid set in @rs is raid0 */
278 static bool rs_is_raid0(struct raid_set *rs)
280 return !rs->md.level;
283 /* Return true, if raid set in @rs is raid10 */
284 static bool rs_is_raid10(struct raid_set *rs)
286 return rs->md.level == 10;
290 * bool helpers to test for various raid levels of a raid type
293 /* Return true, if raid type in @rt is raid0 */
294 static bool rt_is_raid0(struct raid_type *rt)
299 /* Return true, if raid type in @rt is raid1 */
300 static bool rt_is_raid1(struct raid_type *rt)
302 return rt->level == 1;
305 /* Return true, if raid type in @rt is raid10 */
306 static bool rt_is_raid10(struct raid_type *rt)
308 return rt->level == 10;
311 /* Return true, if raid type in @rt is raid4/5 */
312 static bool rt_is_raid45(struct raid_type *rt)
314 return _in_range(rt->level, 4, 5);
317 /* Return true, if raid type in @rt is raid6 */
318 static bool rt_is_raid6(struct raid_type *rt)
320 return rt->level == 6;
323 /* Return true, if raid type in @rt is raid4/5/6 */
324 static bool rt_is_raid456(struct raid_type *rt)
326 return _in_range(rt->level, 4, 6);
328 /* END: raid level bools */
331 * Convenience functions to set ti->error to @errmsg and
332 * return @r in order to shorten code in a lot of places
334 static int ti_error_ret(struct dm_target *ti, const char *errmsg, int r)
336 ti->error = (char *) errmsg;
340 static int ti_error_einval(struct dm_target *ti, const char *errmsg)
342 return ti_error_ret(ti, errmsg, -EINVAL);
344 /* END: convenience functions to set ti->error to @errmsg... */
346 /* Return invalid ctr flags for the raid level of @rs */
347 static uint32_t _invalid_flags(struct raid_set *rs)
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;
364 * Check for any invalid flags set on @rs defined by bitset @invalid_flags
366 * Has to be called after parsing of the ctr flags!
368 static int rs_check_for_invalid_flags(struct raid_set *rs)
370 if (_test_flags(rs->ctr_flags, _invalid_flags(rs)))
371 return ti_error_einval(rs->ti, "Invalid flag combined");
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) */
383 /* Return md raid10 near copies for @layout */
384 static unsigned int _raid10_near_copies(int layout)
386 return layout & 0xFF;
389 /* Return md raid10 far copies for @layout */
390 static unsigned int _raid10_far_copies(int layout)
392 return _raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
395 /* Return true if md raid10 offset for @layout */
396 static unsigned int _is_raid10_offset(int layout)
398 return layout & RAID10_OFFSET;
401 /* Return true if md raid10 near for @layout */
402 static unsigned int _is_raid10_near(int layout)
404 return !_is_raid10_offset(layout) && _raid10_near_copies(layout) > 1;
407 /* Return true if md raid10 far for @layout */
408 static unsigned int _is_raid10_far(int layout)
410 return !_is_raid10_offset(layout) && _raid10_far_copies(layout) > 1;
413 /* Return md raid10 layout string for @layout */
414 static const char *raid10_md_layout_to_format(int layout)
417 * Bit 16 stands for "offset"
418 * (i.e. adjacent stripes hold copies)
420 * Refer to MD's raid10.c for details
422 if (_is_raid10_offset(layout))
425 if (_raid10_near_copies(layout) > 1)
428 WARN_ON(_raid10_far_copies(layout) < 2);
433 /* Return md raid10 algorithm for @name */
434 static const int raid10_name_to_format(const char *name)
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;
447 /* Return md raid10 copies for @layout */
448 static unsigned int raid10_md_layout_to_copies(int layout)
450 return _raid10_near_copies(layout) > 1 ?
451 _raid10_near_copies(layout) : _raid10_far_copies(layout);
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,
459 unsigned int n = 1, f = 1, r = 0;
462 * MD resilienece flaw:
464 * enabling use_far_sets for far/offset formats causes copies
465 * to be colocated on the same devs together with their origins!
467 * -> disable it for now in the definition above
469 if (algorithm == ALGORITHM_RAID10_DEFAULT ||
470 algorithm == ALGORITHM_RAID10_NEAR)
473 else if (algorithm == ALGORITHM_RAID10_OFFSET) {
476 if (!_test_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, rs->ctr_flags))
477 r |= RAID10_USE_FAR_SETS;
479 } else if (algorithm == ALGORITHM_RAID10_FAR) {
482 if (!_test_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, rs->ctr_flags))
483 r |= RAID10_USE_FAR_SETS;
488 return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
490 /* END: MD raid10 bit definitions and helpers */
492 /* Check for any of the raid10 algorithms */
493 static int _got_raid10(struct raid_type *rtp, const int layout)
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);
512 /* Return raid_type for @name */
513 static struct raid_type *get_raid_type(const char *name)
515 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
517 while (rtp-- > raid_types)
518 if (!strcasecmp(rtp->name, name))
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)
527 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
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))
540 * Set the mddev properties in @rs to the new
541 * ones requested by the ctr
543 static void rs_set_new(struct raid_set *rs)
545 struct mddev *mddev = &rs->md;
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;
554 static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
559 if (raid_devs <= raid_type->parity_devs)
560 return ERR_PTR(ti_error_einval(ti, "Insufficient number of devices"));
562 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
564 return ERR_PTR(ti_error_ret(ti, "Cannot allocate raid context", -ENOMEM));
568 rs->raid_disks = raid_devs;
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;
581 for (i = 0; i < raid_devs; i++)
582 md_rdev_init(&rs->dev[i].rdev);
585 * Remaining items to be initialized by further RAID params:
588 * rs->md.chunk_sectors
589 * rs->md.new_chunk_sectors
596 static void context_free(struct raid_set *rs)
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);
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
616 * The following are permitted:
619 * <meta_dev> <data_dev>
621 * The following is not allowed:
624 * This code parses those words. If there is a failure,
625 * the caller must use context_free to unwind the operations.
627 static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
631 int metadata_available = 0;
635 /* Put off the number of raid devices argument to get to dev pairs */
636 arg = dm_shift_arg(as);
640 for (i = 0; i < rs->md.raid_disks; i++) {
641 rs->dev[i].rdev.raid_disk = i;
643 rs->dev[i].meta_dev = NULL;
644 rs->dev[i].data_dev = NULL;
647 * There are no offsets, since there is a separate device
648 * for data and metadata.
650 rs->dev[i].rdev.data_offset = 0;
651 rs->dev[i].rdev.mddev = &rs->md;
653 arg = dm_shift_arg(as);
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);
662 return ti_error_ret(rs->ti, "RAID metadata device lookup failure", r);
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);
669 arg = dm_shift_arg(as);
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");
678 if (rs->dev[i].meta_dev)
679 return ti_error_einval(rs->ti, "No data device supplied with metadata device");
684 r = dm_get_device(rs->ti, arg,
685 dm_table_get_mode(rs->ti->table),
686 &rs->dev[i].data_dev);
688 return ti_error_ret(rs->ti, "RAID device lookup failure", r);
690 if (rs->dev[i].meta_dev) {
691 metadata_available = 1;
692 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
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))
700 if (metadata_available) {
702 rs->md.persistent = 1;
703 rs->md.major_version = 2;
704 } else if (rebuild && !rs->md.recovery_cp) {
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.
710 * Even if there is metadata, the on-disk information may
711 * indicate that the array is not in-sync and it will then
714 * User could specify 'nosync' option if desperate.
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");
724 * validate_region_size
726 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
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.
731 * Returns: 0 on success, -EINVAL on failure.
733 static int validate_region_size(struct raid_set *rs, unsigned long region_size)
735 unsigned long min_region_size = rs->ti->len / (1 << 21);
739 * Choose a reasonable default. All figures in sectors.
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",
747 DMINFO("Choosing default region size of 4MiB");
748 region_size = 1 << 13; /* sectors */
752 * Validate user-supplied value.
754 if (region_size > rs->ti->len)
755 return ti_error_einval(rs->ti, "Supplied region size is too large");
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");
763 if (!is_power_of_2(region_size))
764 return ti_error_einval(rs->ti, "Region size is not a power of 2");
766 if (region_size < rs->md.chunk_sectors)
767 return ti_error_einval(rs->ti, "Region size is smaller than the chunk size");
771 * Convert sectors to bytes.
773 rs->md.bitmap_info.chunksize = (region_size << 9);
779 * validate_raid_redundancy
782 * Determine if there are enough devices in the array that haven't
783 * failed (or are being rebuilt) to form a usable array.
785 * Returns: 0 on success, -EINVAL on failure.
787 static int validate_raid_redundancy(struct raid_set *rs)
789 unsigned i, rebuild_cnt = 0;
790 unsigned rebuilds_per_group = 0, copies, d;
791 unsigned group_size, last_group_start;
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)
798 switch (rs->raid_type->level) {
800 if (rebuild_cnt >= rs->md.raid_disks)
806 if (rebuild_cnt > rs->raid_type->parity_devs)
810 copies = raid10_md_layout_to_copies(rs->md.layout);
811 if (rebuild_cnt < copies)
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).
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
828 if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
829 for (i = 0; i < rs->md.raid_disks * copies; i++) {
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))
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.)
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)
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))
877 * Possible arguments are...
878 * <chunk_size> [optional_args]
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
885 * [rebuild <idx>] Rebuild the drive indicated by the index
886 * [daemon_sleep <ms>] Time between bitmap daemon work to
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
895 * RAID10-only options:
896 * [raid10_copies <# copies>] Number of copies. (Default: 2)
897 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
899 static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
900 unsigned num_raid_params)
902 int raid10_format = ALGORITHM_RAID10_DEFAULT;
903 unsigned raid10_copies = 2;
905 unsigned value, region_size = 0;
906 sector_t sectors_per_dev = rs->ti->len;
908 const char *arg, *key;
910 struct raid_type *rt = rs->raid_type;
912 arg = dm_shift_arg(as);
913 num_raid_params--; /* Account for chunk_size argument */
915 if (kstrtouint(arg, 10, &value) < 0)
916 return ti_error_einval(rs->ti, "Bad numerical argument given for chunk_size");
919 * First, parse the in-order required arguments
920 * "chunk_size" is the only argument of this type.
922 if (rt_is_raid1(rt)) {
924 DMERR("Ignoring chunk size parameter for RAID 1");
926 } else if (!is_power_of_2(value))
927 return ti_error_einval(rs->ti, "Chunk size must be a power of 2");
929 return ti_error_einval(rs->ti, "Chunk size value is too small");
931 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
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:
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.
947 * What is found in the superblocks of the devices is always
948 * authoritative, unless 'rebuild' or '[no]sync' was specified.
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;
956 * Second, parse the unordered optional arguments
958 for (i = 0; i < num_raid_params; i++) {
959 key = dm_shift_arg(as);
961 return ti_error_einval(rs->ti, "Not enough raid parameters given");
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;
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;
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");
981 arg = dm_shift_arg(as);
982 i++; /* Account for the argument pairs */
984 return ti_error_einval(rs->ti, "Wrong number of raid parameters given");
987 * Parameters that take a string value are checked here.
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);
1001 if (kstrtouint(arg, 10, &value) < 0)
1002 return ti_error_einval(rs->ti, "Bad numerical argument given in raid params");
1004 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_REBUILD))) {
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.
1010 if (!_in_range(value, 0, rs->md.raid_disks - 1))
1011 return ti_error_einval(rs->ti, "Invalid rebuild index given");
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");
1022 if (!_in_range(value, 0, rs->md.raid_disks - 1))
1023 return ti_error_einval(rs->ti, "Invalid write_mostly index given");
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");
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");
1035 * In device-mapper, we specify things in sectors, but
1036 * MD records this value in kB
1039 if (value > COUNTER_MAX)
1040 return ti_error_einval(rs->ti, "Max write-behind limit out of range");
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");
1054 /* Ensure sensible data offset */
1056 return ti_error_einval(rs->ti, "Bogus data_offset value");
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");
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");
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");
1073 * In device-mapper, we specify things in sectors, but
1074 * MD records this value in kB
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");
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");
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");
1104 if (!_in_range(value, 2, rs->md.raid_disks))
1105 return ti_error_einval(rs->ti, "Bad value for 'raid10_copies'");
1107 raid10_copies = value;
1109 DMERR("Unable to parse RAID parameter: %s", key);
1110 return ti_error_einval(rs->ti, "Unable to parse RAID parameters");
1114 if (validate_region_size(rs, region_size))
1117 if (rs->md.chunk_sectors)
1118 max_io_len = rs->md.chunk_sectors;
1120 max_io_len = region_size;
1122 if (dm_set_target_max_io_len(rs->ti, max_io_len))
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");
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);
1133 rt = get_raid_type_by_ll(10, rs->md.new_layout);
1135 return ti_error_einval(rs->ti, "Failed to recognize new raid10 layout");
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");
1142 /* (Len * #mirrors) / #devices */
1143 sectors_per_dev = rs->ti->len * raid10_copies;
1144 sector_div(sectors_per_dev, rs->md.raid_disks);
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");
1153 rs->raid10_copies = raid10_copies;
1154 rs->md.dev_sectors = sectors_per_dev;
1156 /* Assume there are no metadata devices until the drives are parsed */
1157 rs->md.persistent = 0;
1158 rs->md.external = 1;
1160 /* Check, if any invalid ctr arguments have been passed in for the raid level */
1161 return rs_check_for_invalid_flags(rs);
1164 static void do_table_event(struct work_struct *ws)
1166 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
1168 dm_table_event(rs->ti->table);
1171 static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
1173 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
1175 return mddev_congested(&rs->md, bits);
1179 #define FEATURE_FLAG_SUPPORTS_RESHAPE 0x1
1181 /* State flags for sb->flags */
1182 #define SB_FLAG_RESHAPE_ACTIVE 0x1
1183 #define SB_FLAG_RESHAPE_BACKWARDS 0x2
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.
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) */
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 */
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) */
1202 * This offset tracks the progress of the repair or replacement of
1203 * an individual drive.
1205 __le64 disk_recovery_offset;
1208 * This offset tracks the progress of the initial raid set
1209 * synchronisation/parity calculation.
1211 __le64 array_resync_offset;
1214 * raid characteristics
1218 __le32 stripe_sectors;
1220 /********************************************************************
1221 * BELOW FOLLOW V1.8.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
1223 * FEATURE_FLAG_SUPPORTS_RESHAPE in the features member indicates that those exist
1226 __le32 flags; /* Flags defining array states for reshaping */
1229 * This offset tracks the progress of a raid
1230 * set reshape in order to be able to restart it
1232 __le64 reshape_position;
1235 * These define the properties of the array in case of an interrupted reshape
1239 __le32 new_stripe_sectors;
1242 __le64 array_sectors; /* Array size in sectors */
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
1251 __le64 new_data_offset;
1253 __le64 sectors; /* Used device size in sectors */
1256 * Additonal Bit field of devices indicating failures to support
1257 * up to 256 devices with the 1.8.0 on-disk metadata format
1259 __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
1261 __le32 incompat_features; /* Used to indicate any incompatible features */
1263 /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
1266 static int read_disk_sb(struct md_rdev *rdev, int size)
1268 BUG_ON(!rdev->sb_page);
1270 if (rdev->sb_loaded)
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",
1276 md_error(rdev->mddev, rdev);
1280 rdev->sb_loaded = 1;
1285 static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1287 failed_devices[0] = le64_to_cpu(sb->failed_devices);
1288 memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
1290 if (_test_flag(FEATURE_FLAG_SUPPORTS_RESHAPE, le32_to_cpu(sb->compat_features))) {
1291 int i = ARRAY_SIZE(sb->extended_failed_devices);
1294 failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
1298 static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1300 int i = ARRAY_SIZE(sb->extended_failed_devices);
1302 sb->failed_devices = cpu_to_le64(failed_devices[0]);
1304 sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
1308 * Synchronize the superblock members with the raid set properties
1310 * All superblock data is little endian.
1312 static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
1314 bool update_failed_devices = false;
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);
1320 /* No metadata device, no superblock */
1321 if (!rdev->meta_bdev)
1324 BUG_ON(!rdev->sb_page);
1326 sb = page_address(rdev->sb_page);
1328 sb_retrieve_failed_devices(sb, failed_devices);
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);
1336 if (update_failed_devices)
1337 sb_update_failed_devices(sb, failed_devices);
1339 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
1340 sb->compat_features = cpu_to_le32(0); /* Don't set reshape flag yet */
1342 sb->num_devices = cpu_to_le32(mddev->raid_disks);
1343 sb->array_position = cpu_to_le32(rdev->raid_disk);
1345 sb->events = cpu_to_le64(mddev->events);
1347 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
1348 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
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);
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);
1358 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
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);
1366 if (mddev->delta_disks < 0 || mddev->reshape_backwards)
1367 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
1369 /* Flag no reshape */
1370 _clear_flags(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS), &sb->flags);
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);
1377 /* Zero out the rest of the payload after the size of the superblock */
1378 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
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.
1387 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
1389 static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
1392 struct dm_raid_superblock *sb;
1393 struct dm_raid_superblock *refsb;
1394 uint64_t events_sb, events_refsb;
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");
1403 r = read_disk_sb(rdev, rdev->sb_size);
1407 sb = page_address(rdev->sb_page);
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)
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);
1418 set_bit(FirstUse, &rdev->flags);
1420 /* Force writing of superblocks to disk */
1421 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
1423 /* Any superblock is better than none, choose that if given */
1424 return refdev ? 0 : 1;
1430 events_sb = le64_to_cpu(sb->events);
1432 refsb = page_address(refdev->sb_page);
1433 events_refsb = le64_to_cpu(refsb->events);
1435 return (events_sb > events_refsb) ? 1 : 0;
1438 static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
1442 struct mddev *mddev = &rs->md;
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;
1448 struct dm_raid_superblock *sb2;
1450 sb = page_address(rdev->sb_page);
1451 events_sb = le64_to_cpu(sb->events);
1454 * Initialise to 1 if this is a new superblock.
1456 mddev->events = events_sb ? : 1;
1458 mddev->reshape_position = MaxSector;
1461 * Reshaping is supported, e.g. reshape_position is valid
1462 * in superblock and superblock content is authoritative.
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);
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");
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;
1487 mddev->reshape_backwards = 0;
1489 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1490 rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
1495 * Reshaping is not allowed, because we don't have the appropriate metadata
1497 if (le32_to_cpu(sb->level) != mddev->level) {
1498 DMERR("Reshaping/takeover raid sets not yet supported. (raid level/stripes/size change)");
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));
1512 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
1513 DMERR("Reshaping raid sets not yet supported. (stripe sectors change)");
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);
1525 /* Table line is checked vs. authoritative superblock */
1529 if (!_test_flag(CTR_FLAG_NOSYNC, rs->ctr_flags))
1530 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
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.
1544 rdev_for_each(r, mddev) {
1545 if (test_bit(FirstUse, &r->flags))
1548 if (!test_bit(In_sync, &r->flags)) {
1549 DMINFO("Device %d specified for rebuild; clearing superblock",
1553 if (test_bit(FirstUse, &r->flags))
1560 if (new_devs == rs->raid_disks || !rebuilds) {
1561 /* Replace a broken device */
1562 if (new_devs == 1 && !rs->delta_disks)
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");
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);
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" : "");
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);
1587 } else if (mddev->reshape_position != MaxSector) {
1588 DMERR("'rebuild' specified while raid set is being reshaped");
1594 * Now we set the Faulty bit for those devices that are
1595 * recorded in the superblock as failed.
1597 sb_retrieve_failed_devices(sb, failed_devices);
1598 rdev_for_each(r, mddev) {
1601 sb2 = page_address(r->sb_page);
1602 sb2->failed_devices = 0;
1603 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
1606 * Check for any device re-ordering.
1608 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
1609 role = le32_to_cpu(sb2->array_position);
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!");
1620 sb2->array_position = cpu_to_le32(r->raid_disk);
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");
1627 DMINFO("raid device #%d now at position #%d",
1628 role, r->raid_disk);
1632 * Partial recovery is performed on
1633 * returning failed devices.
1635 if (test_bit(role, (void *) failed_devices))
1636 set_bit(Faulty, &r->flags);
1643 static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
1645 struct mddev *mddev = &rs->md;
1646 struct dm_raid_superblock *sb;
1651 sb = page_address(rdev->sb_page);
1654 * If mddev->events is not set, we know we have not yet initialized
1657 if (!mddev->events && super_init_validation(rs, rdev))
1660 if (sb->compat_features || sb->incompat_features) {
1661 rs->ti->error = "Unable to assemble array: No feature flags supported yet";
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;
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);
1676 * If no reshape in progress -> we're recovering single
1677 * disk(s) and have to set the device(s) to out-of-sync
1679 else if (rs->md.reshape_position == MaxSector)
1680 clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
1684 * If a device comes back, set it as not In_sync and no longer faulty.
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;
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);
1700 * Analyse superblocks and select the freshest.
1702 static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
1705 struct raid_dev *dev;
1706 struct md_rdev *rdev, *tmp, *freshest;
1707 struct mddev *mddev = &rs->md;
1710 rdev_for_each_safe(rdev, tmp, mddev) {
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.
1717 * When reshaping capability is added, we must ensure
1718 * that the "sync" directive is disallowed during the
1721 rdev->sectors = to_sector(i_size_read(rdev->bdev->bd_inode));
1723 if (_test_flag(CTR_FLAG_SYNC, rs->ctr_flags))
1726 if (!rdev->meta_bdev)
1729 r = super_load(rdev, freshest);
1738 dev = container_of(rdev, struct raid_dev, rdev);
1740 dm_put_device(ti, dev->meta_dev);
1742 dev->meta_dev = NULL;
1743 rdev->meta_bdev = NULL;
1746 put_page(rdev->sb_page);
1748 rdev->sb_page = NULL;
1750 rdev->sb_loaded = 0;
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.
1759 dm_put_device(ti, dev->data_dev);
1761 dev->data_dev = NULL;
1764 list_del(&rdev->same_set);
1771 if (validate_raid_redundancy(rs))
1772 return ti_error_einval(rs->ti, "Insufficient redundancy to activate array");
1775 * Validation of the freshest device provides the source of
1776 * validation for the remaining devices.
1778 if (super_validate(rs, freshest))
1779 return ti_error_einval(rs->ti, "Unable to assemble array: Invalid superblocks");
1781 rdev_for_each(rdev, mddev)
1782 if ((rdev != freshest) && super_validate(rs, rdev))
1789 * Enable/disable discard support on RAID set depending on
1790 * RAID level and discard properties of underlying RAID members.
1792 static void configure_discard_support(struct dm_target *ti, struct raid_set *rs)
1797 /* Assume discards not supported until after checks below. */
1798 ti->discards_supported = false;
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);
1803 for (i = 0; i < rs->md.raid_disks; i++) {
1804 struct request_queue *q;
1806 if (!rs->dev[i].rdev.bdev)
1809 q = bdev_get_queue(rs->dev[i].rdev.bdev);
1810 if (!q || !blk_queue_discard(q))
1814 if (!q->limits.discard_zeroes_data)
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.");
1824 /* All RAID members properly support discards */
1825 ti->discards_supported = true;
1828 * RAID1 and RAID10 personalities require bio splitting,
1829 * RAID0/4/5/6 don't and process large discard bios properly.
1831 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
1832 ti->num_discard_bios = 1;
1836 * Construct a RAID0/1/10/4/5/6 mapping:
1838 * <raid_type> <#raid_params> <raid_params>{0,} \
1839 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
1841 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
1842 * details on possible <raid_params>.
1844 * Userspace is free to initialize the metadata devices, hence the superblocks to
1845 * enforce recreation based on the passed in table parameters.
1848 static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
1851 struct raid_type *rt;
1852 unsigned num_raid_params, num_raid_devs;
1853 struct raid_set *rs = NULL;
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" }
1861 /* Must have <raid_type> */
1862 arg = dm_shift_arg(&as);
1864 return ti_error_einval(rs->ti, "No arguments");
1866 rt = get_raid_type(arg);
1868 return ti_error_einval(rs->ti, "Unrecognised raid_type");
1870 /* Must have <#raid_params> */
1871 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
1874 /* number of raid device tupples <meta_dev data_dev> */
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))
1881 if (!_in_range(num_raid_devs, 1, MAX_RAID_DEVICES))
1882 return ti_error_einval(rs->ti, "Invalid number of supplied raid devices");
1884 rs = context_alloc(ti, rt, num_raid_devs);
1888 r = parse_raid_params(rs, &as, num_raid_params);
1892 r = parse_dev_params(rs, &as);
1896 rs->md.sync_super = super_sync;
1897 r = analyse_superblocks(ti, rs);
1901 INIT_WORK(&rs->md.event_work, do_table_event);
1903 ti->num_flush_bios = 1;
1906 * Disable/enable discard support on RAID set.
1908 configure_discard_support(ti, rs);
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);
1917 ti->error = "Fail to run raid array";
1921 if (ti->len != rs->md.array_sectors) {
1922 r = ti_error_einval(ti, "Array size does not match requested target length");
1925 rs->callbacks.congested_fn = raid_is_congested;
1926 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
1928 mddev_suspend(&rs->md);
1939 static void raid_dtr(struct dm_target *ti)
1941 struct raid_set *rs = ti->private;
1943 list_del_init(&rs->callbacks.list);
1948 static int raid_map(struct dm_target *ti, struct bio *bio)
1950 struct raid_set *rs = ti->private;
1951 struct mddev *mddev = &rs->md;
1953 mddev->pers->make_request(mddev, bio);
1955 return DM_MAPIO_SUBMITTED;
1958 static const char *decipher_sync_action(struct mddev *mddev)
1960 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
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))
1968 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1969 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1971 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1976 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
1983 static void raid_status(struct dm_target *ti, status_type_t type,
1984 unsigned status_flags, char *result, unsigned maxlen)
1986 struct raid_set *rs = ti->private;
1987 unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
1989 int i, array_in_sync = 0;
1993 case STATUSTYPE_INFO:
1994 DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
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;
2000 sync = rs->md.recovery_cp;
2002 if (sync >= rs->md.resync_max_sectors) {
2007 sync = rs->md.resync_max_sectors;
2008 } else if (test_bit(MD_RECOVERY_REQUESTED, &rs->md.recovery)) {
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.
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.
2022 for (i = 0; i < rs->md.raid_disks; i++)
2023 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
2029 sync = rs->md.resync_max_sectors;
2033 * Status characters:
2034 * 'D' = Dead/Failed device
2035 * 'a' = Alive but not in-sync
2036 * 'A' = Alive and in-sync
2038 for (i = 0; i < rs->md.raid_disks; i++) {
2039 if (test_bit(Faulty, &rs->dev[i].rdev.flags))
2041 else if (!array_in_sync ||
2042 !test_bit(In_sync, &rs->dev[i].rdev.flags))
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.
2056 DMEMIT(" %llu/%llu",
2057 (unsigned long long) sync,
2058 (unsigned long long) rs->md.resync_max_sectors);
2062 * See Documentation/device-mapper/dm-raid.c for
2063 * information on each of these states.
2065 DMEMIT(" %s", decipher_sync_action(&rs->md));
2068 * resync_mismatches/mismatch_cnt
2069 * This field shows the number of discrepancies found when
2070 * performing a "check" of the array.
2073 (strcmp(rs->md.last_sync_action, "check")) ? 0 :
2074 (unsigned long long)
2075 atomic64_read(&rs->md.resync_mismatches));
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;
2089 raid_param_cnt += (hweight32(rs->ctr_flags & ~CTR_FLAG_REBUILD) * 2);
2090 if (rs->ctr_flags & (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC))
2093 DMEMIT("%s %u %u", rs->raid_type->name,
2094 raid_param_cnt, rs->md.chunk_sectors);
2096 if (_test_flag(CTR_FLAG_SYNC, rs->ctr_flags) &&
2097 rs->md.recovery_cp == MaxSector)
2099 if (_test_flag(CTR_FLAG_NOSYNC, rs->ctr_flags))
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);
2108 if (_test_flag(CTR_FLAG_DAEMON_SLEEP, rs->ctr_flags))
2109 DMEMIT(" daemon_sleep %lu",
2110 rs->md.bitmap_info.daemon_sleep);
2112 if (_test_flag(CTR_FLAG_MIN_RECOVERY_RATE, rs->ctr_flags))
2113 DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
2115 if (_test_flag(CTR_FLAG_MAX_RECOVERY_RATE, rs->ctr_flags))
2116 DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
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);
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);
2127 if (_test_flag(CTR_FLAG_STRIPE_CACHE, rs->ctr_flags)) {
2128 struct r5conf *conf = rs->md.private;
2130 /* convert from kiB to sectors */
2131 DMEMIT(" stripe_cache %d",
2132 conf ? conf->max_nr_stripes * 2 : 0);
2135 if (_test_flag(CTR_FLAG_REGION_SIZE, rs->ctr_flags))
2136 DMEMIT(" region_size %lu",
2137 rs->md.bitmap_info.chunksize >> 9);
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));
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));
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);
2154 if (rs->dev[i].data_dev)
2155 DMEMIT(" %s", rs->dev[i].data_dev->name);
2162 static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
2164 struct raid_set *rs = ti->private;
2165 struct mddev *mddev = &rs->md;
2167 if (!strcasecmp(argv[0], "reshape")) {
2168 DMERR("Reshape not supported.");
2172 if (!mddev->pers || !mddev->pers->sync_request)
2175 if (!strcasecmp(argv[0], "frozen"))
2176 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
2178 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
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);
2185 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2186 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
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);
2194 if (!strcasecmp(argv[0], "check"))
2195 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2196 else if (!!strcasecmp(argv[0], "repair"))
2198 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2199 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2201 if (mddev->ro == 2) {
2202 /* A write to sync_action is enough to justify
2203 * canceling read-auto mode
2206 if (!mddev->suspended)
2207 md_wakeup_thread(mddev->sync_thread);
2209 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2210 if (!mddev->suspended)
2211 md_wakeup_thread(mddev->thread);
2216 static int raid_iterate_devices(struct dm_target *ti,
2217 iterate_devices_callout_fn fn, void *data)
2219 struct raid_set *rs = ti->private;
2223 for (i = 0; !r && i < rs->md.raid_disks; i++)
2224 if (rs->dev[i].data_dev)
2226 rs->dev[i].data_dev,
2227 0, /* No offset on data devs */
2234 static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
2236 struct raid_set *rs = ti->private;
2237 unsigned chunk_size = rs->md.chunk_sectors << 9;
2238 struct r5conf *conf = rs->md.private;
2240 blk_limits_io_min(limits, chunk_size);
2241 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
2244 static void raid_presuspend(struct dm_target *ti)
2246 struct raid_set *rs = ti->private;
2248 md_stop_writes(&rs->md);
2251 static void raid_postsuspend(struct dm_target *ti)
2253 struct raid_set *rs = ti->private;
2255 mddev_suspend(&rs->md);
2258 static void attempt_restore_of_faulty_devices(struct raid_set *rs)
2261 uint64_t failed_devices, cleared_failed_devices = 0;
2262 unsigned long flags;
2263 struct dm_raid_superblock *sb;
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,
2271 DMINFO("Faulty %s device #%d has readable super block."
2272 " Attempting to revive it.",
2273 rs->raid_type->name, i);
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
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 */
2290 r->saved_raid_disk = i;
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)) {
2297 r->saved_raid_disk = -1;
2300 r->recovery_offset = 0;
2301 cleared_failed_devices |= 1 << i;
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);
2315 static void raid_resume(struct dm_target *ti)
2317 struct raid_set *rs = ti->private;
2319 if (!rt_is_raid0(rs->raid_type)) {
2320 set_bit(MD_CHANGE_DEVS, &rs->md.flags);
2322 if (!rs->bitmap_loaded) {
2323 bitmap_load(&rs->md);
2324 rs->bitmap_loaded = 1;
2327 * A secondary resume while the device is active.
2328 * Take this opportunity to check whether any failed
2329 * devices are reachable again.
2331 attempt_restore_of_faulty_devices(rs);
2334 clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
2337 mddev_resume(&rs->md);
2340 static struct target_type raid_target = {
2342 .version = {1, 8, 1},
2343 .module = THIS_MODULE,
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,
2356 static int __init dm_raid_init(void)
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);
2365 static void __exit dm_raid_exit(void)
2367 dm_unregister_target(&raid_target);
2370 module_init(dm_raid_init);
2371 module_exit(dm_raid_exit);
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");
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");