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 /* Return true if single @flag is set in @*flags, else set it and return false */
218 static bool _test_and_set_flag(uint32_t flag, uint32_t *flags)
220 if (_test_flag(flag, *flags))
223 _set_flag(flag, flags);
226 /* ...ctr and runtime flag bit manipulation */
228 /* All table line arguments are defined here */
229 static struct arg_name_flag {
232 } _arg_name_flags[] = {
233 { CTR_FLAG_SYNC, "sync"},
234 { CTR_FLAG_NOSYNC, "nosync"},
235 { CTR_FLAG_REBUILD, "rebuild"},
236 { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
237 { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
238 { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
239 { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
240 { CTR_FLAG_WRITE_MOSTLY, "writemostly"},
241 { CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
242 { CTR_FLAG_REGION_SIZE, "region_size"},
243 { CTR_FLAG_RAID10_COPIES, "raid10_copies"},
244 { CTR_FLAG_RAID10_FORMAT, "raid10_format"},
245 { CTR_FLAG_DATA_OFFSET, "data_offset"},
246 { CTR_FLAG_DELTA_DISKS, "delta_disks"},
247 { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
250 /* Return argument name string for given @flag */
251 static const char *_argname_by_flag(const uint32_t flag)
253 if (hweight32(flag) == 1) {
254 struct arg_name_flag *anf = _arg_name_flags + ARRAY_SIZE(_arg_name_flags);
256 while (anf-- > _arg_name_flags)
257 if (_test_flag(flag, anf->flag))
261 DMERR("%s called with more than one flag!", __func__);
267 * bool helpers to test for various raid levels of a raid set,
268 * is. it's level as reported by the superblock rather than
269 * the requested raid_type passed to the constructor.
271 /* Return true, if raid set in @rs is raid0 */
272 static bool rs_is_raid0(struct raid_set *rs)
274 return !rs->md.level;
277 /* Return true, if raid set in @rs is raid10 */
278 static bool rs_is_raid10(struct raid_set *rs)
280 return rs->md.level == 10;
284 * bool helpers to test for various raid levels of a raid type
287 /* Return true, if raid type in @rt is raid0 */
288 static bool rt_is_raid0(struct raid_type *rt)
293 /* Return true, if raid type in @rt is raid1 */
294 static bool rt_is_raid1(struct raid_type *rt)
296 return rt->level == 1;
299 /* Return true, if raid type in @rt is raid10 */
300 static bool rt_is_raid10(struct raid_type *rt)
302 return rt->level == 10;
305 /* Return true, if raid type in @rt is raid4/5 */
306 static bool rt_is_raid45(struct raid_type *rt)
308 return _in_range(rt->level, 4, 5);
311 /* Return true, if raid type in @rt is raid6 */
312 static bool rt_is_raid6(struct raid_type *rt)
314 return rt->level == 6;
317 /* Return true, if raid type in @rt is raid4/5/6 */
318 static bool rt_is_raid456(struct raid_type *rt)
320 return _in_range(rt->level, 4, 6);
322 /* END: raid level bools */
325 * Convenience functions to set ti->error to @errmsg and
326 * return @r in order to shorten code in a lot of places
328 static int ti_error_ret(struct dm_target *ti, const char *errmsg, int r)
330 ti->error = (char *) errmsg;
334 static int ti_error_einval(struct dm_target *ti, const char *errmsg)
336 return ti_error_ret(ti, errmsg, -EINVAL);
338 /* END: convenience functions to set ti->error to @errmsg... */
340 /* Return invalid ctr flags for the raid level of @rs */
341 static uint32_t _invalid_flags(struct raid_set *rs)
343 if (rt_is_raid0(rs->raid_type))
344 return RAID0_INVALID_FLAGS;
345 else if (rt_is_raid1(rs->raid_type))
346 return RAID1_INVALID_FLAGS;
347 else if (rt_is_raid10(rs->raid_type))
348 return RAID10_INVALID_FLAGS;
349 else if (rt_is_raid45(rs->raid_type))
350 return RAID45_INVALID_FLAGS;
351 else if (rt_is_raid6(rs->raid_type))
352 return RAID6_INVALID_FLAGS;
358 * Check for any invalid flags set on @rs defined by bitset @invalid_flags
360 * Has to be called after parsing of the ctr flags!
362 static int rs_check_for_invalid_flags(struct raid_set *rs)
364 if (_test_flags(rs->ctr_flags, _invalid_flags(rs)))
365 return ti_error_einval(rs->ti, "Invalid flag combined");
371 /* MD raid10 bit definitions and helpers */
372 #define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */
373 #define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
374 #define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */
375 #define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */
377 /* Return md raid10 near copies for @layout */
378 static unsigned int _raid10_near_copies(int layout)
380 return layout & 0xFF;
383 /* Return md raid10 far copies for @layout */
384 static unsigned int _raid10_far_copies(int layout)
386 return _raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
389 /* Return true if md raid10 offset for @layout */
390 static unsigned int _is_raid10_offset(int layout)
392 return layout & RAID10_OFFSET;
395 /* Return true if md raid10 near for @layout */
396 static unsigned int _is_raid10_near(int layout)
398 return !_is_raid10_offset(layout) && _raid10_near_copies(layout) > 1;
401 /* Return true if md raid10 far for @layout */
402 static unsigned int _is_raid10_far(int layout)
404 return !_is_raid10_offset(layout) && _raid10_far_copies(layout) > 1;
407 /* Return md raid10 layout string for @layout */
408 static const char *raid10_md_layout_to_format(int layout)
411 * Bit 16 stands for "offset"
412 * (i.e. adjacent stripes hold copies)
414 * Refer to MD's raid10.c for details
416 if (_is_raid10_offset(layout))
419 if (_raid10_near_copies(layout) > 1)
422 WARN_ON(_raid10_far_copies(layout) < 2);
427 /* Return md raid10 algorithm for @name */
428 static const int raid10_name_to_format(const char *name)
430 if (!strcasecmp(name, "near"))
431 return ALGORITHM_RAID10_NEAR;
432 else if (!strcasecmp(name, "offset"))
433 return ALGORITHM_RAID10_OFFSET;
434 else if (!strcasecmp(name, "far"))
435 return ALGORITHM_RAID10_FAR;
441 /* Return md raid10 copies for @layout */
442 static unsigned int raid10_md_layout_to_copies(int layout)
444 return _raid10_near_copies(layout) > 1 ?
445 _raid10_near_copies(layout) : _raid10_far_copies(layout);
448 /* Return md raid10 format id for @format string */
449 static int raid10_format_to_md_layout(struct raid_set *rs,
450 unsigned int algorithm,
453 unsigned int n = 1, f = 1, r = 0;
456 * MD resilienece flaw:
458 * enabling use_far_sets for far/offset formats causes copies
459 * to be colocated on the same devs together with their origins!
461 * -> disable it for now in the definition above
463 if (algorithm == ALGORITHM_RAID10_DEFAULT ||
464 algorithm == ALGORITHM_RAID10_NEAR)
467 else if (algorithm == ALGORITHM_RAID10_OFFSET) {
470 if (!_test_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, rs->ctr_flags))
471 r |= RAID10_USE_FAR_SETS;
473 } else if (algorithm == ALGORITHM_RAID10_FAR) {
476 if (!_test_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, rs->ctr_flags))
477 r |= RAID10_USE_FAR_SETS;
482 return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
484 /* END: MD raid10 bit definitions and helpers */
486 /* Check for any of the raid10 algorithms */
487 static int _got_raid10(struct raid_type *rtp, const int layout)
489 if (rtp->level == 10) {
490 switch (rtp->algorithm) {
491 case ALGORITHM_RAID10_DEFAULT:
492 case ALGORITHM_RAID10_NEAR:
493 return _is_raid10_near(layout);
494 case ALGORITHM_RAID10_OFFSET:
495 return _is_raid10_offset(layout);
496 case ALGORITHM_RAID10_FAR:
497 return _is_raid10_far(layout);
506 /* Return raid_type for @name */
507 static struct raid_type *get_raid_type(const char *name)
509 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
511 while (rtp-- > raid_types)
512 if (!strcasecmp(rtp->name, name))
518 /* Return raid_type for @name based derived from @level and @layout */
519 static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
521 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
523 while (rtp-- > raid_types) {
524 /* RAID10 special checks based on @layout flags/properties */
525 if (rtp->level == level &&
526 (_got_raid10(rtp, layout) || rtp->algorithm == layout))
534 * Set the mddev properties in @rs to the new
535 * ones requested by the ctr
537 static void rs_set_new(struct raid_set *rs)
539 struct mddev *mddev = &rs->md;
541 mddev->level = mddev->new_level;
542 mddev->layout = mddev->new_layout;
543 mddev->chunk_sectors = mddev->new_chunk_sectors;
544 mddev->delta_disks = 0;
548 static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
553 if (raid_devs <= raid_type->parity_devs)
554 return ERR_PTR(ti_error_einval(ti, "Insufficient number of devices"));
556 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
558 return ERR_PTR(ti_error_ret(ti, "Cannot allocate raid context", -ENOMEM));
562 rs->raid_disks = raid_devs;
566 rs->raid_type = raid_type;
567 rs->md.raid_disks = raid_devs;
568 rs->md.level = raid_type->level;
569 rs->md.new_level = rs->md.level;
570 rs->md.layout = raid_type->algorithm;
571 rs->md.new_layout = rs->md.layout;
572 rs->md.delta_disks = 0;
573 rs->md.recovery_cp = 0;
575 for (i = 0; i < raid_devs; i++)
576 md_rdev_init(&rs->dev[i].rdev);
579 * Remaining items to be initialized by further RAID params:
582 * rs->md.chunk_sectors
583 * rs->md.new_chunk_sectors
590 static void context_free(struct raid_set *rs)
594 for (i = 0; i < rs->md.raid_disks; i++) {
595 if (rs->dev[i].meta_dev)
596 dm_put_device(rs->ti, rs->dev[i].meta_dev);
597 md_rdev_clear(&rs->dev[i].rdev);
598 if (rs->dev[i].data_dev)
599 dm_put_device(rs->ti, rs->dev[i].data_dev);
606 * For every device we have two words
607 * <meta_dev>: meta device name or '-' if missing
608 * <data_dev>: data device name or '-' if missing
610 * The following are permitted:
613 * <meta_dev> <data_dev>
615 * The following is not allowed:
618 * This code parses those words. If there is a failure,
619 * the caller must use context_free to unwind the operations.
621 static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
625 int metadata_available = 0;
629 /* Put off the number of raid devices argument to get to dev pairs */
630 arg = dm_shift_arg(as);
634 for (i = 0; i < rs->md.raid_disks; i++) {
635 rs->dev[i].rdev.raid_disk = i;
637 rs->dev[i].meta_dev = NULL;
638 rs->dev[i].data_dev = NULL;
641 * There are no offsets, since there is a separate device
642 * for data and metadata.
644 rs->dev[i].rdev.data_offset = 0;
645 rs->dev[i].rdev.mddev = &rs->md;
647 arg = dm_shift_arg(as);
651 if (strcmp(arg, "-")) {
652 r = dm_get_device(rs->ti, arg,
653 dm_table_get_mode(rs->ti->table),
654 &rs->dev[i].meta_dev);
656 return ti_error_ret(rs->ti, "RAID metadata device lookup failure", r);
658 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
659 if (!rs->dev[i].rdev.sb_page)
660 return ti_error_ret(rs->ti, "Failed to allocate superblock page", -ENOMEM);
663 arg = dm_shift_arg(as);
667 if (!strcmp(arg, "-")) {
668 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
669 (!rs->dev[i].rdev.recovery_offset))
670 return ti_error_einval(rs->ti, "Drive designated for rebuild not specified");
672 if (rs->dev[i].meta_dev)
673 return ti_error_einval(rs->ti, "No data device supplied with metadata device");
678 r = dm_get_device(rs->ti, arg,
679 dm_table_get_mode(rs->ti->table),
680 &rs->dev[i].data_dev);
682 return ti_error_ret(rs->ti, "RAID device lookup failure", r);
684 if (rs->dev[i].meta_dev) {
685 metadata_available = 1;
686 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
688 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
689 list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
690 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
694 if (metadata_available) {
696 rs->md.persistent = 1;
697 rs->md.major_version = 2;
698 } else if (rebuild && !rs->md.recovery_cp) {
700 * Without metadata, we will not be able to tell if the array
701 * is in-sync or not - we must assume it is not. Therefore,
702 * it is impossible to rebuild a drive.
704 * Even if there is metadata, the on-disk information may
705 * indicate that the array is not in-sync and it will then
708 * User could specify 'nosync' option if desperate.
710 DMERR("Unable to rebuild drive while array is not in-sync");
711 return ti_error_einval(rs->ti, "Unable to rebuild drive while array is not in-sync");
718 * validate_region_size
720 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
722 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
723 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
725 * Returns: 0 on success, -EINVAL on failure.
727 static int validate_region_size(struct raid_set *rs, unsigned long region_size)
729 unsigned long min_region_size = rs->ti->len / (1 << 21);
733 * Choose a reasonable default. All figures in sectors.
735 if (min_region_size > (1 << 13)) {
736 /* If not a power of 2, make it the next power of 2 */
737 region_size = roundup_pow_of_two(min_region_size);
738 DMINFO("Choosing default region size of %lu sectors",
741 DMINFO("Choosing default region size of 4MiB");
742 region_size = 1 << 13; /* sectors */
746 * Validate user-supplied value.
748 if (region_size > rs->ti->len)
749 return ti_error_einval(rs->ti, "Supplied region size is too large");
751 if (region_size < min_region_size) {
752 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
753 region_size, min_region_size);
754 return ti_error_einval(rs->ti, "Supplied region size is too small");
757 if (!is_power_of_2(region_size))
758 return ti_error_einval(rs->ti, "Region size is not a power of 2");
760 if (region_size < rs->md.chunk_sectors)
761 return ti_error_einval(rs->ti, "Region size is smaller than the chunk size");
765 * Convert sectors to bytes.
767 rs->md.bitmap_info.chunksize = (region_size << 9);
773 * validate_raid_redundancy
776 * Determine if there are enough devices in the array that haven't
777 * failed (or are being rebuilt) to form a usable array.
779 * Returns: 0 on success, -EINVAL on failure.
781 static int validate_raid_redundancy(struct raid_set *rs)
783 unsigned i, rebuild_cnt = 0;
784 unsigned rebuilds_per_group = 0, copies, d;
785 unsigned group_size, last_group_start;
787 for (i = 0; i < rs->md.raid_disks; i++)
788 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
789 !rs->dev[i].rdev.sb_page)
792 switch (rs->raid_type->level) {
794 if (rebuild_cnt >= rs->md.raid_disks)
800 if (rebuild_cnt > rs->raid_type->parity_devs)
804 copies = raid10_md_layout_to_copies(rs->md.layout);
805 if (rebuild_cnt < copies)
809 * It is possible to have a higher rebuild count for RAID10,
810 * as long as the failed devices occur in different mirror
811 * groups (i.e. different stripes).
813 * When checking "near" format, make sure no adjacent devices
814 * have failed beyond what can be handled. In addition to the
815 * simple case where the number of devices is a multiple of the
816 * number of copies, we must also handle cases where the number
817 * of devices is not a multiple of the number of copies.
818 * E.g. dev1 dev2 dev3 dev4 dev5
822 if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
823 for (i = 0; i < rs->md.raid_disks * copies; i++) {
825 rebuilds_per_group = 0;
826 d = i % rs->md.raid_disks;
827 if ((!rs->dev[d].rdev.sb_page ||
828 !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
829 (++rebuilds_per_group >= copies))
836 * When checking "far" and "offset" formats, we need to ensure
837 * that the device that holds its copy is not also dead or
838 * being rebuilt. (Note that "far" and "offset" formats only
839 * support two copies right now. These formats also only ever
840 * use the 'use_far_sets' variant.)
842 * This check is somewhat complicated by the need to account
843 * for arrays that are not a multiple of (far) copies. This
844 * results in the need to treat the last (potentially larger)
847 group_size = (rs->md.raid_disks / copies);
848 last_group_start = (rs->md.raid_disks / group_size) - 1;
849 last_group_start *= group_size;
850 for (i = 0; i < rs->md.raid_disks; i++) {
851 if (!(i % copies) && !(i > last_group_start))
852 rebuilds_per_group = 0;
853 if ((!rs->dev[i].rdev.sb_page ||
854 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
855 (++rebuilds_per_group >= copies))
871 * Possible arguments are...
872 * <chunk_size> [optional_args]
874 * Argument definitions
875 * <chunk_size> The number of sectors per disk that
876 * will form the "stripe"
877 * [[no]sync] Force or prevent recovery of the
879 * [rebuild <idx>] Rebuild the drive indicated by the index
880 * [daemon_sleep <ms>] Time between bitmap daemon work to
882 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
883 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
884 * [write_mostly <idx>] Indicate a write mostly drive via index
885 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
886 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
887 * [region_size <sectors>] Defines granularity of bitmap
889 * RAID10-only options:
890 * [raid10_copies <# copies>] Number of copies. (Default: 2)
891 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
893 static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
894 unsigned num_raid_params)
896 int raid10_format = ALGORITHM_RAID10_DEFAULT;
897 unsigned raid10_copies = 2;
899 unsigned value, region_size = 0;
900 sector_t sectors_per_dev = rs->ti->len;
902 const char *arg, *key;
904 struct raid_type *rt = rs->raid_type;
906 arg = dm_shift_arg(as);
907 num_raid_params--; /* Account for chunk_size argument */
909 if (kstrtouint(arg, 10, &value) < 0)
910 return ti_error_einval(rs->ti, "Bad numerical argument given for chunk_size");
913 * First, parse the in-order required arguments
914 * "chunk_size" is the only argument of this type.
916 if (rt_is_raid1(rt)) {
918 DMERR("Ignoring chunk size parameter for RAID 1");
920 } else if (!is_power_of_2(value))
921 return ti_error_einval(rs->ti, "Chunk size must be a power of 2");
923 return ti_error_einval(rs->ti, "Chunk size value is too small");
925 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
928 * We set each individual device as In_sync with a completed
929 * 'recovery_offset'. If there has been a device failure or
930 * replacement then one of the following cases applies:
932 * 1) User specifies 'rebuild'.
933 * - Device is reset when param is read.
934 * 2) A new device is supplied.
935 * - No matching superblock found, resets device.
936 * 3) Device failure was transient and returns on reload.
937 * - Failure noticed, resets device for bitmap replay.
938 * 4) Device hadn't completed recovery after previous failure.
939 * - Superblock is read and overrides recovery_offset.
941 * What is found in the superblocks of the devices is always
942 * authoritative, unless 'rebuild' or '[no]sync' was specified.
944 for (i = 0; i < rs->md.raid_disks; i++) {
945 set_bit(In_sync, &rs->dev[i].rdev.flags);
946 rs->dev[i].rdev.recovery_offset = MaxSector;
950 * Second, parse the unordered optional arguments
952 for (i = 0; i < num_raid_params; i++) {
953 key = dm_shift_arg(as);
955 return ti_error_einval(rs->ti, "Not enough raid parameters given");
957 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_NOSYNC))) {
958 if (_test_and_set_flag(CTR_FLAG_NOSYNC, &rs->ctr_flags))
959 return ti_error_einval(rs->ti, "Only one 'nosync' argument allowed");
960 rs->md.recovery_cp = MaxSector;
963 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_SYNC))) {
964 if (_test_and_set_flag(CTR_FLAG_SYNC, &rs->ctr_flags))
965 return ti_error_einval(rs->ti, "Only one 'sync' argument allowed");
966 rs->md.recovery_cp = 0;
969 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) {
970 if (_test_and_set_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
971 return ti_error_einval(rs->ti, "Only one 'raid10_use_new_sets' argument allowed");
975 arg = dm_shift_arg(as);
976 i++; /* Account for the argument pairs */
978 return ti_error_einval(rs->ti, "Wrong number of raid parameters given");
981 * Parameters that take a string value are checked here.
984 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_RAID10_FORMAT))) {
985 if (_test_and_set_flag(CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
986 return ti_error_einval(rs->ti, "Only one 'raid10_format' argument pair allowed");
987 if (!rt_is_raid10(rt))
988 return ti_error_einval(rs->ti, "'raid10_format' is an invalid parameter for this RAID type");
989 raid10_format = raid10_name_to_format(arg);
990 if (raid10_format < 0)
991 return ti_error_ret(rs->ti, "Invalid 'raid10_format' value given", raid10_format);
995 if (kstrtouint(arg, 10, &value) < 0)
996 return ti_error_einval(rs->ti, "Bad numerical argument given in raid params");
998 if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_REBUILD))) {
1000 * "rebuild" is being passed in by userspace to provide
1001 * indexes of replaced devices and to set up additional
1002 * devices on raid level takeover.
1004 if (!_in_range(value, 0, rs->md.raid_disks - 1))
1005 return ti_error_einval(rs->ti, "Invalid rebuild index given");
1007 rd = rs->dev + value;
1008 clear_bit(In_sync, &rd->rdev.flags);
1009 clear_bit(Faulty, &rd->rdev.flags);
1010 rd->rdev.recovery_offset = 0;
1011 _set_flag(CTR_FLAG_REBUILD, &rs->ctr_flags);
1012 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
1013 if (!rt_is_raid1(rt))
1014 return ti_error_einval(rs->ti, "write_mostly option is only valid for RAID1");
1016 if (!_in_range(value, 0, rs->md.raid_disks - 1))
1017 return ti_error_einval(rs->ti, "Invalid write_mostly index given");
1019 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
1020 _set_flag(CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
1021 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
1022 if (!rt_is_raid1(rt))
1023 return ti_error_einval(rs->ti, "max_write_behind option is only valid for RAID1");
1025 if (_test_and_set_flag(CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
1026 return ti_error_einval(rs->ti, "Only one max_write_behind argument pair allowed");
1029 * In device-mapper, we specify things in sectors, but
1030 * MD records this value in kB
1033 if (value > COUNTER_MAX)
1034 return ti_error_einval(rs->ti, "Max write-behind limit out of range");
1036 rs->md.bitmap_info.max_write_behind = value;
1037 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
1038 if (_test_and_set_flag(CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
1039 return ti_error_einval(rs->ti, "Only one daemon_sleep argument pair allowed");
1040 if (!value || (value > MAX_SCHEDULE_TIMEOUT))
1041 return ti_error_einval(rs->ti, "daemon sleep period out of range");
1042 rs->md.bitmap_info.daemon_sleep = value;
1043 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_DATA_OFFSET))) {
1044 /* Userspace passes new data_offset after having extended the the data image LV */
1045 if (_test_and_set_flag(CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
1046 return ti_error_einval(rs->ti, "Only one data_offset argument pair allowed");
1048 /* Ensure sensible data offset */
1050 return ti_error_einval(rs->ti, "Bogus data_offset value");
1052 rs->data_offset = value;
1053 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_DELTA_DISKS))) {
1054 /* Define the +/-# of disks to add to/remove from the given raid set */
1055 if (_test_and_set_flag(CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
1056 return ti_error_einval(rs->ti, "Only one delta_disks argument pair allowed");
1058 /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
1059 if (!_in_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs))
1060 return ti_error_einval(rs->ti, "Too many delta_disk requested");
1062 rs->delta_disks = value;
1063 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_STRIPE_CACHE))) {
1064 if (_test_and_set_flag(CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
1065 return ti_error_einval(rs->ti, "Only one stripe_cache argument pair allowed");
1067 * In device-mapper, we specify things in sectors, but
1068 * MD records this value in kB
1072 if (!rt_is_raid456(rt))
1073 return ti_error_einval(rs->ti, "Inappropriate argument: stripe_cache");
1074 if (raid5_set_cache_size(&rs->md, (int)value))
1075 return ti_error_einval(rs->ti, "Bad stripe_cache size");
1077 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
1078 if (_test_and_set_flag(CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
1079 return ti_error_einval(rs->ti, "Only one min_recovery_rate argument pair allowed");
1080 if (value > INT_MAX)
1081 return ti_error_einval(rs->ti, "min_recovery_rate out of range");
1082 rs->md.sync_speed_min = (int)value;
1083 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MAX_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 max_recovery_rate argument pair allowed");
1086 if (value > INT_MAX)
1087 return ti_error_einval(rs->ti, "max_recovery_rate out of range");
1088 rs->md.sync_speed_max = (int)value;
1089 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_REGION_SIZE))) {
1090 if (_test_and_set_flag(CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
1091 return ti_error_einval(rs->ti, "Only one region_size argument pair allowed");
1093 region_size = value;
1094 } else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_RAID10_COPIES))) {
1095 if (_test_and_set_flag(CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
1096 return ti_error_einval(rs->ti, "Only one raid10_copies argument pair allowed");
1098 if (!_in_range(value, 2, rs->md.raid_disks))
1099 return ti_error_einval(rs->ti, "Bad value for 'raid10_copies'");
1101 raid10_copies = value;
1103 DMERR("Unable to parse RAID parameter: %s", key);
1104 return ti_error_einval(rs->ti, "Unable to parse RAID parameters");
1108 if (validate_region_size(rs, region_size))
1111 if (rs->md.chunk_sectors)
1112 max_io_len = rs->md.chunk_sectors;
1114 max_io_len = region_size;
1116 if (dm_set_target_max_io_len(rs->ti, max_io_len))
1119 if (rt_is_raid10(rt)) {
1120 if (raid10_copies > rs->md.raid_disks)
1121 return ti_error_einval(rs->ti, "Not enough devices to satisfy specification");
1123 rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
1124 if (rs->md.new_layout < 0)
1125 return ti_error_ret(rs->ti, "Error getting raid10 format", rs->md.new_layout);
1127 rt = get_raid_type_by_ll(10, rs->md.new_layout);
1129 return ti_error_einval(rs->ti, "Failed to recognize new raid10 layout");
1131 if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
1132 rt->algorithm == ALGORITHM_RAID10_NEAR) &&
1133 _test_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, rs->ctr_flags))
1134 return ti_error_einval(rs->ti, "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible");
1136 /* (Len * #mirrors) / #devices */
1137 sectors_per_dev = rs->ti->len * raid10_copies;
1138 sector_div(sectors_per_dev, rs->md.raid_disks);
1140 rs->md.layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
1141 rs->md.new_layout = rs->md.layout;
1142 } else if (!rt_is_raid1(rt) &&
1143 sector_div(sectors_per_dev,
1144 (rs->md.raid_disks - rt->parity_devs)))
1145 return ti_error_einval(rs->ti, "Target length not divisible by number of data devices");
1147 rs->raid10_copies = raid10_copies;
1148 rs->md.dev_sectors = sectors_per_dev;
1150 /* Assume there are no metadata devices until the drives are parsed */
1151 rs->md.persistent = 0;
1152 rs->md.external = 1;
1154 /* Check, if any invalid ctr arguments have been passed in for the raid level */
1155 return rs_check_for_invalid_flags(rs);
1158 static void do_table_event(struct work_struct *ws)
1160 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
1162 dm_table_event(rs->ti->table);
1165 static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
1167 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
1169 return mddev_congested(&rs->md, bits);
1173 #define FEATURE_FLAG_SUPPORTS_RESHAPE 0x1
1175 /* State flags for sb->flags */
1176 #define SB_FLAG_RESHAPE_ACTIVE 0x1
1177 #define SB_FLAG_RESHAPE_BACKWARDS 0x2
1180 * This structure is never routinely used by userspace, unlike md superblocks.
1181 * Devices with this superblock should only ever be accessed via device-mapper.
1183 #define DM_RAID_MAGIC 0x64526D44
1184 struct dm_raid_superblock {
1185 __le32 magic; /* "DmRd" */
1186 __le32 compat_features; /* Used to indicate compatible features (like 1.8.0 ondisk metadata extension) */
1188 __le32 num_devices; /* Number of devices in this raid set. (Max 64) */
1189 __le32 array_position; /* The position of this drive in the raid set */
1191 __le64 events; /* Incremented by md when superblock updated */
1192 __le64 failed_devices; /* Pre 1.8.0 part of bit field of devices to */
1193 /* indicate failures (see extension below) */
1196 * This offset tracks the progress of the repair or replacement of
1197 * an individual drive.
1199 __le64 disk_recovery_offset;
1202 * This offset tracks the progress of the initial raid set
1203 * synchronisation/parity calculation.
1205 __le64 array_resync_offset;
1208 * raid characteristics
1212 __le32 stripe_sectors;
1214 /********************************************************************
1215 * BELOW FOLLOW V1.8.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
1217 * FEATURE_FLAG_SUPPORTS_RESHAPE in the features member indicates that those exist
1220 __le32 flags; /* Flags defining array states for reshaping */
1223 * This offset tracks the progress of a raid
1224 * set reshape in order to be able to restart it
1226 __le64 reshape_position;
1229 * These define the properties of the array in case of an interrupted reshape
1233 __le32 new_stripe_sectors;
1236 __le64 array_sectors; /* Array size in sectors */
1239 * Sector offsets to data on devices (reshaping).
1240 * Needed to support out of place reshaping, thus
1241 * not writing over any stripes whilst converting
1242 * them from old to new layout
1245 __le64 new_data_offset;
1247 __le64 sectors; /* Used device size in sectors */
1250 * Additonal Bit field of devices indicating failures to support
1251 * up to 256 devices with the 1.8.0 on-disk metadata format
1253 __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];
1255 __le32 incompat_features; /* Used to indicate any incompatible features */
1257 /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
1260 static int read_disk_sb(struct md_rdev *rdev, int size)
1262 BUG_ON(!rdev->sb_page);
1264 if (rdev->sb_loaded)
1267 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, 1)) {
1268 DMERR("Failed to read superblock of device at position %d",
1270 md_error(rdev->mddev, rdev);
1274 rdev->sb_loaded = 1;
1279 static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
1281 failed_devices[0] = le64_to_cpu(sb->failed_devices);
1282 memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));
1284 if (_test_flag(FEATURE_FLAG_SUPPORTS_RESHAPE, le32_to_cpu(sb->compat_features))) {
1285 int i = ARRAY_SIZE(sb->extended_failed_devices);
1288 failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
1292 static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
1295 uint64_t failed_devices;
1296 struct dm_raid_superblock *sb;
1297 struct raid_set *rs = container_of(mddev, struct raid_set, md);
1299 sb = page_address(rdev->sb_page);
1300 failed_devices = le64_to_cpu(sb->failed_devices);
1302 for (i = 0; i < mddev->raid_disks; i++)
1303 if (!rs->dev[i].data_dev ||
1304 test_bit(Faulty, &(rs->dev[i].rdev.flags)))
1305 failed_devices |= (1ULL << i);
1307 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
1309 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
1310 sb->compat_features = cpu_to_le32(0); /* No features yet */
1312 sb->num_devices = cpu_to_le32(mddev->raid_disks);
1313 sb->array_position = cpu_to_le32(rdev->raid_disk);
1315 sb->events = cpu_to_le64(mddev->events);
1316 sb->failed_devices = cpu_to_le64(failed_devices);
1318 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
1319 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
1321 sb->level = cpu_to_le32(mddev->level);
1322 sb->layout = cpu_to_le32(mddev->layout);
1323 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
1329 * This function creates a superblock if one is not found on the device
1330 * and will decide which superblock to use if there's a choice.
1332 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
1334 static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
1337 struct dm_raid_superblock *sb;
1338 struct dm_raid_superblock *refsb;
1339 uint64_t events_sb, events_refsb;
1342 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
1343 if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
1344 DMERR("superblock size of a logical block is no longer valid");
1348 r = read_disk_sb(rdev, rdev->sb_size);
1352 sb = page_address(rdev->sb_page);
1355 * Two cases that we want to write new superblocks and rebuild:
1356 * 1) New device (no matching magic number)
1357 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
1359 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
1360 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
1361 super_sync(rdev->mddev, rdev);
1363 set_bit(FirstUse, &rdev->flags);
1365 /* Force writing of superblocks to disk */
1366 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
1368 /* Any superblock is better than none, choose that if given */
1369 return refdev ? 0 : 1;
1375 events_sb = le64_to_cpu(sb->events);
1377 refsb = page_address(refdev->sb_page);
1378 events_refsb = le64_to_cpu(refsb->events);
1380 return (events_sb > events_refsb) ? 1 : 0;
1383 static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
1387 struct mddev *mddev = &rs->md;
1389 uint64_t failed_devices[DISKS_ARRAY_ELEMS];
1390 struct dm_raid_superblock *sb;
1391 uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0;
1393 struct dm_raid_superblock *sb2;
1395 sb = page_address(rdev->sb_page);
1396 events_sb = le64_to_cpu(sb->events);
1399 * Initialise to 1 if this is a new superblock.
1401 mddev->events = events_sb ? : 1;
1403 mddev->reshape_position = MaxSector;
1406 * Reshaping is supported, e.g. reshape_position is valid
1407 * in superblock and superblock content is authoritative.
1409 if (_test_flag(FEATURE_FLAG_SUPPORTS_RESHAPE, le32_to_cpu(sb->compat_features))) {
1410 /* Superblock is authoritative wrt given raid set layout! */
1411 mddev->raid_disks = le32_to_cpu(sb->num_devices);
1412 mddev->level = le32_to_cpu(sb->level);
1413 mddev->layout = le32_to_cpu(sb->layout);
1414 mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);
1415 mddev->new_level = le32_to_cpu(sb->new_level);
1416 mddev->new_layout = le32_to_cpu(sb->new_layout);
1417 mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
1418 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1419 mddev->array_sectors = le64_to_cpu(sb->array_sectors);
1421 /* raid was reshaping and got interrupted */
1422 if (_test_flag(SB_FLAG_RESHAPE_ACTIVE, le32_to_cpu(sb->flags))) {
1423 if (_test_flag(CTR_FLAG_DELTA_DISKS, rs->ctr_flags)) {
1424 DMERR("Reshape requested but raid set is still reshaping");
1428 if (mddev->delta_disks < 0 ||
1429 (!mddev->delta_disks && _test_flag(SB_FLAG_RESHAPE_BACKWARDS, le32_to_cpu(sb->flags))))
1430 mddev->reshape_backwards = 1;
1432 mddev->reshape_backwards = 0;
1434 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1435 rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
1440 * Reshaping is not allowed, because we don't have the appropriate metadata
1442 if (le32_to_cpu(sb->level) != mddev->level) {
1443 DMERR("Reshaping/takeover raid sets not yet supported. (raid level/stripes/size change)");
1446 if (le32_to_cpu(sb->layout) != mddev->layout) {
1447 DMERR("Reshaping raid sets not yet supported. (raid layout change)");
1448 DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
1449 DMERR(" Old layout: %s w/ %d copies",
1450 raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
1451 raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
1452 DMERR(" New layout: %s w/ %d copies",
1453 raid10_md_layout_to_format(mddev->layout),
1454 raid10_md_layout_to_copies(mddev->layout));
1457 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
1458 DMERR("Reshaping raid sets not yet supported. (stripe sectors change)");
1462 /* We can only change the number of devices in raid1 with old (i.e. pre 1.0.7) metadata */
1463 if (!rt_is_raid1(rs->raid_type) &&
1464 (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
1465 DMERR("Reshaping raid sets not yet supported. (device count change from %u to %u)",
1466 sb->num_devices, mddev->raid_disks);
1470 /* Table line is checked vs. authoritative superblock */
1474 if (!_test_flag(CTR_FLAG_NOSYNC, rs->ctr_flags))
1475 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
1478 * During load, we set FirstUse if a new superblock was written.
1479 * There are two reasons we might not have a superblock:
1480 * 1) The raid set is brand new - in which case, all of the
1481 * devices must have their In_sync bit set. Also,
1482 * recovery_cp must be 0, unless forced.
1483 * 2) This is a new device being added to an old raid set
1484 * and the new device needs to be rebuilt - in which
1485 * case the In_sync bit will /not/ be set and
1486 * recovery_cp must be MaxSector.
1489 rdev_for_each(r, mddev) {
1490 if (test_bit(FirstUse, &r->flags))
1493 if (!test_bit(In_sync, &r->flags)) {
1494 DMINFO("Device %d specified for rebuild; clearing superblock",
1498 if (test_bit(FirstUse, &r->flags))
1505 if (new_devs == rs->raid_disks || !rebuilds) {
1506 /* Replace a broken device */
1507 if (new_devs == 1 && !rs->delta_disks)
1509 if (new_devs == rs->raid_disks) {
1510 DMINFO("Superblocks created for new raid set");
1511 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
1512 mddev->recovery_cp = 0;
1513 } else if (new_devs && new_devs != rs->raid_disks && !rebuilds) {
1514 DMERR("New device injected into existing raid set without "
1515 "'delta_disks' or 'rebuild' parameter specified");
1518 } else if (new_devs && new_devs != rebuilds) {
1519 DMERR("%u 'rebuild' devices cannot be injected into"
1520 " a raid set with %u other first-time devices",
1521 rebuilds, new_devs);
1523 } else if (rebuilds) {
1524 if (rebuild_and_new && rebuilds != rebuild_and_new) {
1525 DMERR("new device%s provided without 'rebuild'",
1526 new_devs > 1 ? "s" : "");
1528 } else if (mddev->recovery_cp != MaxSector) {
1529 DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
1530 (unsigned long long) mddev->recovery_cp);
1532 } else if (mddev->reshape_position != MaxSector) {
1533 DMERR("'rebuild' specified while raid set is being reshaped");
1539 * Now we set the Faulty bit for those devices that are
1540 * recorded in the superblock as failed.
1542 sb_retrieve_failed_devices(sb, failed_devices);
1543 rdev_for_each(r, mddev) {
1546 sb2 = page_address(r->sb_page);
1547 sb2->failed_devices = 0;
1548 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
1551 * Check for any device re-ordering.
1553 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
1554 role = le32_to_cpu(sb2->array_position);
1558 if (role != r->raid_disk) {
1559 if (_is_raid10_near(mddev->layout)) {
1560 if (mddev->raid_disks % _raid10_near_copies(mddev->layout) ||
1561 rs->raid_disks % rs->raid10_copies)
1562 return ti_error_einval(rs->ti, "Cannot change raid10 near "
1563 "set to odd # of devices!");
1565 sb2->array_position = cpu_to_le32(r->raid_disk);
1567 } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
1568 !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
1569 !rt_is_raid1(rs->raid_type))
1570 return ti_error_einval(rs->ti, "Cannot change device positions in raid set");
1572 DMINFO("raid device #%d now at position #%d",
1573 role, r->raid_disk);
1577 * Partial recovery is performed on
1578 * returning failed devices.
1580 if (test_bit(role, (void *) failed_devices))
1581 set_bit(Faulty, &r->flags);
1588 static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
1590 struct mddev *mddev = &rs->md;
1591 struct dm_raid_superblock *sb;
1596 sb = page_address(rdev->sb_page);
1599 * If mddev->events is not set, we know we have not yet initialized
1602 if (!mddev->events && super_init_validation(rs, rdev))
1605 if (sb->compat_features || sb->incompat_features) {
1606 rs->ti->error = "Unable to assemble array: No feature flags supported yet";
1610 /* Enable bitmap creation for RAID levels != 0 */
1611 mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096);
1612 rdev->mddev->bitmap_info.default_offset = mddev->bitmap_info.offset;
1614 if (!test_and_clear_bit(FirstUse, &rdev->flags)) {
1615 /* Retrieve device size stored in superblock to be prepared for shrink */
1616 rdev->sectors = le64_to_cpu(sb->sectors);
1617 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
1618 if (rdev->recovery_offset == MaxSector)
1619 set_bit(In_sync, &rdev->flags);
1621 * If no reshape in progress -> we're recovering single
1622 * disk(s) and have to set the device(s) to out-of-sync
1624 else if (rs->md.reshape_position == MaxSector)
1625 clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
1629 * If a device comes back, set it as not In_sync and no longer faulty.
1631 if (test_and_clear_bit(Faulty, &rdev->flags)) {
1632 rdev->recovery_offset = 0;
1633 clear_bit(In_sync, &rdev->flags);
1634 rdev->saved_raid_disk = rdev->raid_disk;
1637 /* Reshape support -> restore repective data offsets */
1638 rdev->data_offset = le64_to_cpu(sb->data_offset);
1639 rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
1645 * Analyse superblocks and select the freshest.
1647 static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
1650 struct raid_dev *dev;
1651 struct md_rdev *rdev, *tmp, *freshest;
1652 struct mddev *mddev = &rs->md;
1655 rdev_for_each_safe(rdev, tmp, mddev) {
1657 * Skipping super_load due to CTR_FLAG_SYNC will cause
1658 * the array to undergo initialization again as
1659 * though it were new. This is the intended effect
1660 * of the "sync" directive.
1662 * When reshaping capability is added, we must ensure
1663 * that the "sync" directive is disallowed during the
1666 rdev->sectors = to_sector(i_size_read(rdev->bdev->bd_inode));
1668 if (_test_flag(CTR_FLAG_SYNC, rs->ctr_flags))
1671 if (!rdev->meta_bdev)
1674 r = super_load(rdev, freshest);
1683 dev = container_of(rdev, struct raid_dev, rdev);
1685 dm_put_device(ti, dev->meta_dev);
1687 dev->meta_dev = NULL;
1688 rdev->meta_bdev = NULL;
1691 put_page(rdev->sb_page);
1693 rdev->sb_page = NULL;
1695 rdev->sb_loaded = 0;
1698 * We might be able to salvage the data device
1699 * even though the meta device has failed. For
1700 * now, we behave as though '- -' had been
1701 * set for this device in the table.
1704 dm_put_device(ti, dev->data_dev);
1706 dev->data_dev = NULL;
1709 list_del(&rdev->same_set);
1716 if (validate_raid_redundancy(rs))
1717 return ti_error_einval(rs->ti, "Insufficient redundancy to activate array");
1720 * Validation of the freshest device provides the source of
1721 * validation for the remaining devices.
1723 if (super_validate(rs, freshest))
1724 return ti_error_einval(rs->ti, "Unable to assemble array: Invalid superblocks");
1726 rdev_for_each(rdev, mddev)
1727 if ((rdev != freshest) && super_validate(rs, rdev))
1734 * Enable/disable discard support on RAID set depending on
1735 * RAID level and discard properties of underlying RAID members.
1737 static void configure_discard_support(struct dm_target *ti, struct raid_set *rs)
1742 /* Assume discards not supported until after checks below. */
1743 ti->discards_supported = false;
1745 /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
1746 raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
1748 for (i = 0; i < rs->md.raid_disks; i++) {
1749 struct request_queue *q;
1751 if (!rs->dev[i].rdev.bdev)
1754 q = bdev_get_queue(rs->dev[i].rdev.bdev);
1755 if (!q || !blk_queue_discard(q))
1759 if (!q->limits.discard_zeroes_data)
1761 if (!devices_handle_discard_safely) {
1762 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
1763 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
1769 /* All RAID members properly support discards */
1770 ti->discards_supported = true;
1773 * RAID1 and RAID10 personalities require bio splitting,
1774 * RAID0/4/5/6 don't and process large discard bios properly.
1776 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
1777 ti->num_discard_bios = 1;
1781 * Construct a RAID0/1/10/4/5/6 mapping:
1783 * <raid_type> <#raid_params> <raid_params>{0,} \
1784 * <#raid_devs> [<meta_dev1> <dev1>]{1,}
1786 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
1787 * details on possible <raid_params>.
1789 * Userspace is free to initialize the metadata devices, hence the superblocks to
1790 * enforce recreation based on the passed in table parameters.
1793 static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
1796 struct raid_type *rt;
1797 unsigned num_raid_params, num_raid_devs;
1798 struct raid_set *rs = NULL;
1800 struct dm_arg_set as = { argc, argv }, as_nrd;
1801 struct dm_arg _args[] = {
1802 { 0, as.argc, "Cannot understand number of raid parameters" },
1803 { 1, 254, "Cannot understand number of raid devices parameters" }
1806 /* Must have <raid_type> */
1807 arg = dm_shift_arg(&as);
1809 return ti_error_einval(rs->ti, "No arguments");
1811 rt = get_raid_type(arg);
1813 return ti_error_einval(rs->ti, "Unrecognised raid_type");
1815 /* Must have <#raid_params> */
1816 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
1819 /* number of raid device tupples <meta_dev data_dev> */
1821 dm_consume_args(&as_nrd, num_raid_params);
1822 _args[1].max = (as_nrd.argc - 1) / 2;
1823 if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
1826 if (!_in_range(num_raid_devs, 1, MAX_RAID_DEVICES))
1827 return ti_error_einval(rs->ti, "Invalid number of supplied raid devices");
1829 rs = context_alloc(ti, rt, num_raid_devs);
1833 r = parse_raid_params(rs, &as, num_raid_params);
1837 r = parse_dev_params(rs, &as);
1841 rs->md.sync_super = super_sync;
1842 r = analyse_superblocks(ti, rs);
1846 INIT_WORK(&rs->md.event_work, do_table_event);
1848 ti->num_flush_bios = 1;
1851 * Disable/enable discard support on RAID set.
1853 configure_discard_support(ti, rs);
1855 /* Has to be held on running the array */
1856 mddev_lock_nointr(&rs->md);
1857 r = md_run(&rs->md);
1858 rs->md.in_sync = 0; /* Assume already marked dirty */
1859 mddev_unlock(&rs->md);
1862 ti->error = "Fail to run raid array";
1866 if (ti->len != rs->md.array_sectors) {
1867 r = ti_error_einval(ti, "Array size does not match requested target length");
1870 rs->callbacks.congested_fn = raid_is_congested;
1871 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
1873 mddev_suspend(&rs->md);
1884 static void raid_dtr(struct dm_target *ti)
1886 struct raid_set *rs = ti->private;
1888 list_del_init(&rs->callbacks.list);
1893 static int raid_map(struct dm_target *ti, struct bio *bio)
1895 struct raid_set *rs = ti->private;
1896 struct mddev *mddev = &rs->md;
1898 mddev->pers->make_request(mddev, bio);
1900 return DM_MAPIO_SUBMITTED;
1903 static const char *decipher_sync_action(struct mddev *mddev)
1905 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
1908 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1909 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
1910 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1913 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1914 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1916 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1921 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
1928 static void raid_status(struct dm_target *ti, status_type_t type,
1929 unsigned status_flags, char *result, unsigned maxlen)
1931 struct raid_set *rs = ti->private;
1932 unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
1934 int i, array_in_sync = 0;
1938 case STATUSTYPE_INFO:
1939 DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
1941 if (!rt_is_raid0(rs->raid_type)) {
1942 if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
1943 sync = rs->md.curr_resync_completed;
1945 sync = rs->md.recovery_cp;
1947 if (sync >= rs->md.resync_max_sectors) {
1952 sync = rs->md.resync_max_sectors;
1953 } else if (test_bit(MD_RECOVERY_REQUESTED, &rs->md.recovery)) {
1955 * If "check" or "repair" is occurring, the array has
1956 * undergone and initial sync and the health characters
1957 * should not be 'a' anymore.
1962 * The array may be doing an initial sync, or it may
1963 * be rebuilding individual components. If all the
1964 * devices are In_sync, then it is the array that is
1965 * being initialized.
1967 for (i = 0; i < rs->md.raid_disks; i++)
1968 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
1974 sync = rs->md.resync_max_sectors;
1978 * Status characters:
1979 * 'D' = Dead/Failed device
1980 * 'a' = Alive but not in-sync
1981 * 'A' = Alive and in-sync
1983 for (i = 0; i < rs->md.raid_disks; i++) {
1984 if (test_bit(Faulty, &rs->dev[i].rdev.flags))
1986 else if (!array_in_sync ||
1987 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1995 * The in-sync ratio shows the progress of:
1996 * - Initializing the array
1997 * - Rebuilding a subset of devices of the array
1998 * The user can distinguish between the two by referring
1999 * to the status characters.
2001 DMEMIT(" %llu/%llu",
2002 (unsigned long long) sync,
2003 (unsigned long long) rs->md.resync_max_sectors);
2007 * See Documentation/device-mapper/dm-raid.c for
2008 * information on each of these states.
2010 DMEMIT(" %s", decipher_sync_action(&rs->md));
2013 * resync_mismatches/mismatch_cnt
2014 * This field shows the number of discrepancies found when
2015 * performing a "check" of the array.
2018 (strcmp(rs->md.last_sync_action, "check")) ? 0 :
2019 (unsigned long long)
2020 atomic64_read(&rs->md.resync_mismatches));
2022 case STATUSTYPE_TABLE:
2023 /* The string you would use to construct this array */
2024 for (i = 0; i < rs->md.raid_disks; i++) {
2025 if (_test_flag(CTR_FLAG_REBUILD, rs->ctr_flags) &&
2026 rs->dev[i].data_dev &&
2027 !test_bit(In_sync, &rs->dev[i].rdev.flags))
2028 raid_param_cnt += 2; /* for rebuilds */
2029 if (rs->dev[i].data_dev &&
2030 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
2031 raid_param_cnt += 2;
2034 raid_param_cnt += (hweight32(rs->ctr_flags & ~CTR_FLAG_REBUILD) * 2);
2035 if (rs->ctr_flags & (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC))
2038 DMEMIT("%s %u %u", rs->raid_type->name,
2039 raid_param_cnt, rs->md.chunk_sectors);
2041 if (_test_flag(CTR_FLAG_SYNC, rs->ctr_flags) &&
2042 rs->md.recovery_cp == MaxSector)
2044 if (_test_flag(CTR_FLAG_NOSYNC, rs->ctr_flags))
2047 for (i = 0; i < rs->md.raid_disks; i++)
2048 if (_test_flag(CTR_FLAG_REBUILD, rs->ctr_flags) &&
2049 rs->dev[i].data_dev &&
2050 !test_bit(In_sync, &rs->dev[i].rdev.flags))
2051 DMEMIT(" rebuild %u", i);
2053 if (_test_flag(CTR_FLAG_DAEMON_SLEEP, rs->ctr_flags))
2054 DMEMIT(" daemon_sleep %lu",
2055 rs->md.bitmap_info.daemon_sleep);
2057 if (_test_flag(CTR_FLAG_MIN_RECOVERY_RATE, rs->ctr_flags))
2058 DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
2060 if (_test_flag(CTR_FLAG_MAX_RECOVERY_RATE, rs->ctr_flags))
2061 DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
2063 for (i = 0; i < rs->md.raid_disks; i++)
2064 if (rs->dev[i].data_dev &&
2065 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
2066 DMEMIT(" write_mostly %u", i);
2068 if (_test_flag(CTR_FLAG_MAX_WRITE_BEHIND, rs->ctr_flags))
2069 DMEMIT(" max_write_behind %lu",
2070 rs->md.bitmap_info.max_write_behind);
2072 if (_test_flag(CTR_FLAG_STRIPE_CACHE, rs->ctr_flags)) {
2073 struct r5conf *conf = rs->md.private;
2075 /* convert from kiB to sectors */
2076 DMEMIT(" stripe_cache %d",
2077 conf ? conf->max_nr_stripes * 2 : 0);
2080 if (_test_flag(CTR_FLAG_REGION_SIZE, rs->ctr_flags))
2081 DMEMIT(" region_size %lu",
2082 rs->md.bitmap_info.chunksize >> 9);
2084 if (_test_flag(CTR_FLAG_RAID10_COPIES, rs->ctr_flags))
2085 DMEMIT(" raid10_copies %u",
2086 raid10_md_layout_to_copies(rs->md.layout));
2088 if (_test_flag(CTR_FLAG_RAID10_FORMAT, rs->ctr_flags))
2089 DMEMIT(" raid10_format %s",
2090 raid10_md_layout_to_format(rs->md.layout));
2092 DMEMIT(" %d", rs->md.raid_disks);
2093 for (i = 0; i < rs->md.raid_disks; i++) {
2094 if (rs->dev[i].meta_dev)
2095 DMEMIT(" %s", rs->dev[i].meta_dev->name);
2099 if (rs->dev[i].data_dev)
2100 DMEMIT(" %s", rs->dev[i].data_dev->name);
2107 static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
2109 struct raid_set *rs = ti->private;
2110 struct mddev *mddev = &rs->md;
2112 if (!strcasecmp(argv[0], "reshape")) {
2113 DMERR("Reshape not supported.");
2117 if (!mddev->pers || !mddev->pers->sync_request)
2120 if (!strcasecmp(argv[0], "frozen"))
2121 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
2123 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
2125 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
2126 if (mddev->sync_thread) {
2127 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2128 md_reap_sync_thread(mddev);
2130 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2131 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
2133 else if (!strcasecmp(argv[0], "resync"))
2134 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2135 else if (!strcasecmp(argv[0], "recover")) {
2136 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2137 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2139 if (!strcasecmp(argv[0], "check"))
2140 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2141 else if (!!strcasecmp(argv[0], "repair"))
2143 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2144 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2146 if (mddev->ro == 2) {
2147 /* A write to sync_action is enough to justify
2148 * canceling read-auto mode
2151 if (!mddev->suspended)
2152 md_wakeup_thread(mddev->sync_thread);
2154 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2155 if (!mddev->suspended)
2156 md_wakeup_thread(mddev->thread);
2161 static int raid_iterate_devices(struct dm_target *ti,
2162 iterate_devices_callout_fn fn, void *data)
2164 struct raid_set *rs = ti->private;
2168 for (i = 0; !r && i < rs->md.raid_disks; i++)
2169 if (rs->dev[i].data_dev)
2171 rs->dev[i].data_dev,
2172 0, /* No offset on data devs */
2179 static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
2181 struct raid_set *rs = ti->private;
2182 unsigned chunk_size = rs->md.chunk_sectors << 9;
2183 struct r5conf *conf = rs->md.private;
2185 blk_limits_io_min(limits, chunk_size);
2186 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
2189 static void raid_presuspend(struct dm_target *ti)
2191 struct raid_set *rs = ti->private;
2193 md_stop_writes(&rs->md);
2196 static void raid_postsuspend(struct dm_target *ti)
2198 struct raid_set *rs = ti->private;
2200 mddev_suspend(&rs->md);
2203 static void attempt_restore_of_faulty_devices(struct raid_set *rs)
2206 uint64_t failed_devices, cleared_failed_devices = 0;
2207 unsigned long flags;
2208 struct dm_raid_superblock *sb;
2211 for (i = 0; i < rs->md.raid_disks; i++) {
2212 r = &rs->dev[i].rdev;
2213 if (test_bit(Faulty, &r->flags) && r->sb_page &&
2214 sync_page_io(r, 0, r->sb_size, r->sb_page, REQ_OP_READ, 0,
2216 DMINFO("Faulty %s device #%d has readable super block."
2217 " Attempting to revive it.",
2218 rs->raid_type->name, i);
2221 * Faulty bit may be set, but sometimes the array can
2222 * be suspended before the personalities can respond
2223 * by removing the device from the array (i.e. calling
2224 * 'hot_remove_disk'). If they haven't yet removed
2225 * the failed device, its 'raid_disk' number will be
2226 * '>= 0' - meaning we must call this function
2229 if ((r->raid_disk >= 0) &&
2230 (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0))
2231 /* Failed to revive this device, try next */
2235 r->saved_raid_disk = i;
2237 clear_bit(Faulty, &r->flags);
2238 clear_bit(WriteErrorSeen, &r->flags);
2239 clear_bit(In_sync, &r->flags);
2240 if (r->mddev->pers->hot_add_disk(r->mddev, r)) {
2242 r->saved_raid_disk = -1;
2245 r->recovery_offset = 0;
2246 cleared_failed_devices |= 1 << i;
2250 if (cleared_failed_devices) {
2251 rdev_for_each(r, &rs->md) {
2252 sb = page_address(r->sb_page);
2253 failed_devices = le64_to_cpu(sb->failed_devices);
2254 failed_devices &= ~cleared_failed_devices;
2255 sb->failed_devices = cpu_to_le64(failed_devices);
2260 static void raid_resume(struct dm_target *ti)
2262 struct raid_set *rs = ti->private;
2264 if (!rt_is_raid0(rs->raid_type)) {
2265 set_bit(MD_CHANGE_DEVS, &rs->md.flags);
2267 if (!rs->bitmap_loaded) {
2268 bitmap_load(&rs->md);
2269 rs->bitmap_loaded = 1;
2272 * A secondary resume while the device is active.
2273 * Take this opportunity to check whether any failed
2274 * devices are reachable again.
2276 attempt_restore_of_faulty_devices(rs);
2279 clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
2282 mddev_resume(&rs->md);
2285 static struct target_type raid_target = {
2287 .version = {1, 8, 1},
2288 .module = THIS_MODULE,
2292 .status = raid_status,
2293 .message = raid_message,
2294 .iterate_devices = raid_iterate_devices,
2295 .io_hints = raid_io_hints,
2296 .presuspend = raid_presuspend,
2297 .postsuspend = raid_postsuspend,
2298 .resume = raid_resume,
2301 static int __init dm_raid_init(void)
2303 DMINFO("Loading target version %u.%u.%u",
2304 raid_target.version[0],
2305 raid_target.version[1],
2306 raid_target.version[2]);
2307 return dm_register_target(&raid_target);
2310 static void __exit dm_raid_exit(void)
2312 dm_unregister_target(&raid_target);
2315 module_init(dm_raid_init);
2316 module_exit(dm_raid_exit);
2318 module_param(devices_handle_discard_safely, bool, 0644);
2319 MODULE_PARM_DESC(devices_handle_discard_safely,
2320 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
2322 MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
2323 MODULE_ALIAS("dm-raid1");
2324 MODULE_ALIAS("dm-raid10");
2325 MODULE_ALIAS("dm-raid4");
2326 MODULE_ALIAS("dm-raid5");
2327 MODULE_ALIAS("dm-raid6");
2328 MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
2329 MODULE_LICENSE("GPL");