team: Replace rcu_read_lock with a mutex in team_vlan_rx_kill_vid
[linux/fpc-iii.git] / drivers / md / dm-raid.c
blob4805c15185c2e6ec5853536ce78b26d7b30d2e63
1 /*
2 * Copyright (C) 2010-2011 Neil Brown
3 * Copyright (C) 2010-2011 Red Hat, Inc. All rights reserved.
5 * This file is released under the GPL.
6 */
8 #include <linux/slab.h>
9 #include <linux/module.h>
11 #include "md.h"
12 #include "raid1.h"
13 #include "raid5.h"
14 #include "raid10.h"
15 #include "bitmap.h"
17 #include <linux/device-mapper.h>
19 #define DM_MSG_PREFIX "raid"
22 * The following flags are used by dm-raid.c to set up the array state.
23 * They must be cleared before md_run is called.
25 #define FirstUse 10 /* rdev flag */
27 struct raid_dev {
29 * Two DM devices, one to hold metadata and one to hold the
30 * actual data/parity. The reason for this is to not confuse
31 * ti->len and give more flexibility in altering size and
32 * characteristics.
34 * While it is possible for this device to be associated
35 * with a different physical device than the data_dev, it
36 * is intended for it to be the same.
37 * |--------- Physical Device ---------|
38 * |- meta_dev -|------ data_dev ------|
40 struct dm_dev *meta_dev;
41 struct dm_dev *data_dev;
42 struct md_rdev rdev;
46 * Flags for rs->print_flags field.
48 #define DMPF_SYNC 0x1
49 #define DMPF_NOSYNC 0x2
50 #define DMPF_REBUILD 0x4
51 #define DMPF_DAEMON_SLEEP 0x8
52 #define DMPF_MIN_RECOVERY_RATE 0x10
53 #define DMPF_MAX_RECOVERY_RATE 0x20
54 #define DMPF_MAX_WRITE_BEHIND 0x40
55 #define DMPF_STRIPE_CACHE 0x80
56 #define DMPF_REGION_SIZE 0x100
57 #define DMPF_RAID10_COPIES 0x200
58 #define DMPF_RAID10_FORMAT 0x400
60 struct raid_set {
61 struct dm_target *ti;
63 uint32_t bitmap_loaded;
64 uint32_t print_flags;
66 struct mddev md;
67 struct raid_type *raid_type;
68 struct dm_target_callbacks callbacks;
70 struct raid_dev dev[0];
73 /* Supported raid types and properties. */
74 static struct raid_type {
75 const char *name; /* RAID algorithm. */
76 const char *descr; /* Descriptor text for logging. */
77 const unsigned parity_devs; /* # of parity devices. */
78 const unsigned minimal_devs; /* minimal # of devices in set. */
79 const unsigned level; /* RAID level. */
80 const unsigned algorithm; /* RAID algorithm. */
81 } raid_types[] = {
82 {"raid1", "RAID1 (mirroring)", 0, 2, 1, 0 /* NONE */},
83 {"raid10", "RAID10 (striped mirrors)", 0, 2, 10, UINT_MAX /* Varies */},
84 {"raid4", "RAID4 (dedicated parity disk)", 1, 2, 5, ALGORITHM_PARITY_0},
85 {"raid5_la", "RAID5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
86 {"raid5_ra", "RAID5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
87 {"raid5_ls", "RAID5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
88 {"raid5_rs", "RAID5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
89 {"raid6_zr", "RAID6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
90 {"raid6_nr", "RAID6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
91 {"raid6_nc", "RAID6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
94 static char *raid10_md_layout_to_format(int layout)
97 * Bit 16 and 17 stand for "offset" and "use_far_sets"
98 * Refer to MD's raid10.c for details
100 if ((layout & 0x10000) && (layout & 0x20000))
101 return "offset";
103 if ((layout & 0xFF) > 1)
104 return "near";
106 return "far";
109 static unsigned raid10_md_layout_to_copies(int layout)
111 if ((layout & 0xFF) > 1)
112 return layout & 0xFF;
113 return (layout >> 8) & 0xFF;
116 static int raid10_format_to_md_layout(char *format, unsigned copies)
118 unsigned n = 1, f = 1;
120 if (!strcmp("near", format))
121 n = copies;
122 else
123 f = copies;
125 if (!strcmp("offset", format))
126 return 0x30000 | (f << 8) | n;
128 if (!strcmp("far", format))
129 return 0x20000 | (f << 8) | n;
131 return (f << 8) | n;
134 static struct raid_type *get_raid_type(char *name)
136 int i;
138 for (i = 0; i < ARRAY_SIZE(raid_types); i++)
139 if (!strcmp(raid_types[i].name, name))
140 return &raid_types[i];
142 return NULL;
145 static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
147 unsigned i;
148 struct raid_set *rs;
150 if (raid_devs <= raid_type->parity_devs) {
151 ti->error = "Insufficient number of devices";
152 return ERR_PTR(-EINVAL);
155 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
156 if (!rs) {
157 ti->error = "Cannot allocate raid context";
158 return ERR_PTR(-ENOMEM);
161 mddev_init(&rs->md);
163 rs->ti = ti;
164 rs->raid_type = raid_type;
165 rs->md.raid_disks = raid_devs;
166 rs->md.level = raid_type->level;
167 rs->md.new_level = rs->md.level;
168 rs->md.layout = raid_type->algorithm;
169 rs->md.new_layout = rs->md.layout;
170 rs->md.delta_disks = 0;
171 rs->md.recovery_cp = 0;
173 for (i = 0; i < raid_devs; i++)
174 md_rdev_init(&rs->dev[i].rdev);
177 * Remaining items to be initialized by further RAID params:
178 * rs->md.persistent
179 * rs->md.external
180 * rs->md.chunk_sectors
181 * rs->md.new_chunk_sectors
182 * rs->md.dev_sectors
185 return rs;
188 static void context_free(struct raid_set *rs)
190 int i;
192 for (i = 0; i < rs->md.raid_disks; i++) {
193 if (rs->dev[i].meta_dev)
194 dm_put_device(rs->ti, rs->dev[i].meta_dev);
195 md_rdev_clear(&rs->dev[i].rdev);
196 if (rs->dev[i].data_dev)
197 dm_put_device(rs->ti, rs->dev[i].data_dev);
200 kfree(rs);
204 * For every device we have two words
205 * <meta_dev>: meta device name or '-' if missing
206 * <data_dev>: data device name or '-' if missing
208 * The following are permitted:
209 * - -
210 * - <data_dev>
211 * <meta_dev> <data_dev>
213 * The following is not allowed:
214 * <meta_dev> -
216 * This code parses those words. If there is a failure,
217 * the caller must use context_free to unwind the operations.
219 static int dev_parms(struct raid_set *rs, char **argv)
221 int i;
222 int rebuild = 0;
223 int metadata_available = 0;
224 int ret = 0;
226 for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {
227 rs->dev[i].rdev.raid_disk = i;
229 rs->dev[i].meta_dev = NULL;
230 rs->dev[i].data_dev = NULL;
233 * There are no offsets, since there is a separate device
234 * for data and metadata.
236 rs->dev[i].rdev.data_offset = 0;
237 rs->dev[i].rdev.mddev = &rs->md;
239 if (strcmp(argv[0], "-")) {
240 ret = dm_get_device(rs->ti, argv[0],
241 dm_table_get_mode(rs->ti->table),
242 &rs->dev[i].meta_dev);
243 rs->ti->error = "RAID metadata device lookup failure";
244 if (ret)
245 return ret;
247 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
248 if (!rs->dev[i].rdev.sb_page)
249 return -ENOMEM;
252 if (!strcmp(argv[1], "-")) {
253 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
254 (!rs->dev[i].rdev.recovery_offset)) {
255 rs->ti->error = "Drive designated for rebuild not specified";
256 return -EINVAL;
259 rs->ti->error = "No data device supplied with metadata device";
260 if (rs->dev[i].meta_dev)
261 return -EINVAL;
263 continue;
266 ret = dm_get_device(rs->ti, argv[1],
267 dm_table_get_mode(rs->ti->table),
268 &rs->dev[i].data_dev);
269 if (ret) {
270 rs->ti->error = "RAID device lookup failure";
271 return ret;
274 if (rs->dev[i].meta_dev) {
275 metadata_available = 1;
276 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
278 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
279 list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
280 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
281 rebuild++;
284 if (metadata_available) {
285 rs->md.external = 0;
286 rs->md.persistent = 1;
287 rs->md.major_version = 2;
288 } else if (rebuild && !rs->md.recovery_cp) {
290 * Without metadata, we will not be able to tell if the array
291 * is in-sync or not - we must assume it is not. Therefore,
292 * it is impossible to rebuild a drive.
294 * Even if there is metadata, the on-disk information may
295 * indicate that the array is not in-sync and it will then
296 * fail at that time.
298 * User could specify 'nosync' option if desperate.
300 DMERR("Unable to rebuild drive while array is not in-sync");
301 rs->ti->error = "RAID device lookup failure";
302 return -EINVAL;
305 return 0;
309 * validate_region_size
310 * @rs
311 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
313 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
314 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
316 * Returns: 0 on success, -EINVAL on failure.
318 static int validate_region_size(struct raid_set *rs, unsigned long region_size)
320 unsigned long min_region_size = rs->ti->len / (1 << 21);
322 if (!region_size) {
324 * Choose a reasonable default. All figures in sectors.
326 if (min_region_size > (1 << 13)) {
327 /* If not a power of 2, make it the next power of 2 */
328 region_size = roundup_pow_of_two(min_region_size);
329 DMINFO("Choosing default region size of %lu sectors",
330 region_size);
331 } else {
332 DMINFO("Choosing default region size of 4MiB");
333 region_size = 1 << 13; /* sectors */
335 } else {
337 * Validate user-supplied value.
339 if (region_size > rs->ti->len) {
340 rs->ti->error = "Supplied region size is too large";
341 return -EINVAL;
344 if (region_size < min_region_size) {
345 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
346 region_size, min_region_size);
347 rs->ti->error = "Supplied region size is too small";
348 return -EINVAL;
351 if (!is_power_of_2(region_size)) {
352 rs->ti->error = "Region size is not a power of 2";
353 return -EINVAL;
356 if (region_size < rs->md.chunk_sectors) {
357 rs->ti->error = "Region size is smaller than the chunk size";
358 return -EINVAL;
363 * Convert sectors to bytes.
365 rs->md.bitmap_info.chunksize = (region_size << 9);
367 return 0;
371 * validate_raid_redundancy
372 * @rs
374 * Determine if there are enough devices in the array that haven't
375 * failed (or are being rebuilt) to form a usable array.
377 * Returns: 0 on success, -EINVAL on failure.
379 static int validate_raid_redundancy(struct raid_set *rs)
381 unsigned i, rebuild_cnt = 0;
382 unsigned rebuilds_per_group = 0, copies, d;
383 unsigned group_size, last_group_start;
385 for (i = 0; i < rs->md.raid_disks; i++)
386 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
387 !rs->dev[i].rdev.sb_page)
388 rebuild_cnt++;
390 switch (rs->raid_type->level) {
391 case 1:
392 if (rebuild_cnt >= rs->md.raid_disks)
393 goto too_many;
394 break;
395 case 4:
396 case 5:
397 case 6:
398 if (rebuild_cnt > rs->raid_type->parity_devs)
399 goto too_many;
400 break;
401 case 10:
402 copies = raid10_md_layout_to_copies(rs->md.layout);
403 if (rebuild_cnt < copies)
404 break;
407 * It is possible to have a higher rebuild count for RAID10,
408 * as long as the failed devices occur in different mirror
409 * groups (i.e. different stripes).
411 * When checking "near" format, make sure no adjacent devices
412 * have failed beyond what can be handled. In addition to the
413 * simple case where the number of devices is a multiple of the
414 * number of copies, we must also handle cases where the number
415 * of devices is not a multiple of the number of copies.
416 * E.g. dev1 dev2 dev3 dev4 dev5
417 * A A B B C
418 * C D D E E
420 if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
421 for (i = 0; i < rs->md.raid_disks * copies; i++) {
422 if (!(i % copies))
423 rebuilds_per_group = 0;
424 d = i % rs->md.raid_disks;
425 if ((!rs->dev[d].rdev.sb_page ||
426 !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
427 (++rebuilds_per_group >= copies))
428 goto too_many;
430 break;
434 * When checking "far" and "offset" formats, we need to ensure
435 * that the device that holds its copy is not also dead or
436 * being rebuilt. (Note that "far" and "offset" formats only
437 * support two copies right now. These formats also only ever
438 * use the 'use_far_sets' variant.)
440 * This check is somewhat complicated by the need to account
441 * for arrays that are not a multiple of (far) copies. This
442 * results in the need to treat the last (potentially larger)
443 * set differently.
445 group_size = (rs->md.raid_disks / copies);
446 last_group_start = (rs->md.raid_disks / group_size) - 1;
447 last_group_start *= group_size;
448 for (i = 0; i < rs->md.raid_disks; i++) {
449 if (!(i % copies) && !(i > last_group_start))
450 rebuilds_per_group = 0;
451 if ((!rs->dev[i].rdev.sb_page ||
452 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
453 (++rebuilds_per_group >= copies))
454 goto too_many;
456 break;
457 default:
458 if (rebuild_cnt)
459 return -EINVAL;
462 return 0;
464 too_many:
465 return -EINVAL;
469 * Possible arguments are...
470 * <chunk_size> [optional_args]
472 * Argument definitions
473 * <chunk_size> The number of sectors per disk that
474 * will form the "stripe"
475 * [[no]sync] Force or prevent recovery of the
476 * entire array
477 * [rebuild <idx>] Rebuild the drive indicated by the index
478 * [daemon_sleep <ms>] Time between bitmap daemon work to
479 * clear bits
480 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
481 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
482 * [write_mostly <idx>] Indicate a write mostly drive via index
483 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
484 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
485 * [region_size <sectors>] Defines granularity of bitmap
487 * RAID10-only options:
488 * [raid10_copies <# copies>] Number of copies. (Default: 2)
489 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
491 static int parse_raid_params(struct raid_set *rs, char **argv,
492 unsigned num_raid_params)
494 char *raid10_format = "near";
495 unsigned raid10_copies = 2;
496 unsigned i;
497 unsigned long value, region_size = 0;
498 sector_t sectors_per_dev = rs->ti->len;
499 sector_t max_io_len;
500 char *key;
503 * First, parse the in-order required arguments
504 * "chunk_size" is the only argument of this type.
506 if ((strict_strtoul(argv[0], 10, &value) < 0)) {
507 rs->ti->error = "Bad chunk size";
508 return -EINVAL;
509 } else if (rs->raid_type->level == 1) {
510 if (value)
511 DMERR("Ignoring chunk size parameter for RAID 1");
512 value = 0;
513 } else if (!is_power_of_2(value)) {
514 rs->ti->error = "Chunk size must be a power of 2";
515 return -EINVAL;
516 } else if (value < 8) {
517 rs->ti->error = "Chunk size value is too small";
518 return -EINVAL;
521 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
522 argv++;
523 num_raid_params--;
526 * We set each individual device as In_sync with a completed
527 * 'recovery_offset'. If there has been a device failure or
528 * replacement then one of the following cases applies:
530 * 1) User specifies 'rebuild'.
531 * - Device is reset when param is read.
532 * 2) A new device is supplied.
533 * - No matching superblock found, resets device.
534 * 3) Device failure was transient and returns on reload.
535 * - Failure noticed, resets device for bitmap replay.
536 * 4) Device hadn't completed recovery after previous failure.
537 * - Superblock is read and overrides recovery_offset.
539 * What is found in the superblocks of the devices is always
540 * authoritative, unless 'rebuild' or '[no]sync' was specified.
542 for (i = 0; i < rs->md.raid_disks; i++) {
543 set_bit(In_sync, &rs->dev[i].rdev.flags);
544 rs->dev[i].rdev.recovery_offset = MaxSector;
548 * Second, parse the unordered optional arguments
550 for (i = 0; i < num_raid_params; i++) {
551 if (!strcasecmp(argv[i], "nosync")) {
552 rs->md.recovery_cp = MaxSector;
553 rs->print_flags |= DMPF_NOSYNC;
554 continue;
556 if (!strcasecmp(argv[i], "sync")) {
557 rs->md.recovery_cp = 0;
558 rs->print_flags |= DMPF_SYNC;
559 continue;
562 /* The rest of the optional arguments come in key/value pairs */
563 if ((i + 1) >= num_raid_params) {
564 rs->ti->error = "Wrong number of raid parameters given";
565 return -EINVAL;
568 key = argv[i++];
570 /* Parameters that take a string value are checked here. */
571 if (!strcasecmp(key, "raid10_format")) {
572 if (rs->raid_type->level != 10) {
573 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
574 return -EINVAL;
576 if (strcmp("near", argv[i]) &&
577 strcmp("far", argv[i]) &&
578 strcmp("offset", argv[i])) {
579 rs->ti->error = "Invalid 'raid10_format' value given";
580 return -EINVAL;
582 raid10_format = argv[i];
583 rs->print_flags |= DMPF_RAID10_FORMAT;
584 continue;
587 if (strict_strtoul(argv[i], 10, &value) < 0) {
588 rs->ti->error = "Bad numerical argument given in raid params";
589 return -EINVAL;
592 /* Parameters that take a numeric value are checked here */
593 if (!strcasecmp(key, "rebuild")) {
594 if (value >= rs->md.raid_disks) {
595 rs->ti->error = "Invalid rebuild index given";
596 return -EINVAL;
598 clear_bit(In_sync, &rs->dev[value].rdev.flags);
599 rs->dev[value].rdev.recovery_offset = 0;
600 rs->print_flags |= DMPF_REBUILD;
601 } else if (!strcasecmp(key, "write_mostly")) {
602 if (rs->raid_type->level != 1) {
603 rs->ti->error = "write_mostly option is only valid for RAID1";
604 return -EINVAL;
606 if (value >= rs->md.raid_disks) {
607 rs->ti->error = "Invalid write_mostly drive index given";
608 return -EINVAL;
610 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
611 } else if (!strcasecmp(key, "max_write_behind")) {
612 if (rs->raid_type->level != 1) {
613 rs->ti->error = "max_write_behind option is only valid for RAID1";
614 return -EINVAL;
616 rs->print_flags |= DMPF_MAX_WRITE_BEHIND;
619 * In device-mapper, we specify things in sectors, but
620 * MD records this value in kB
622 value /= 2;
623 if (value > COUNTER_MAX) {
624 rs->ti->error = "Max write-behind limit out of range";
625 return -EINVAL;
627 rs->md.bitmap_info.max_write_behind = value;
628 } else if (!strcasecmp(key, "daemon_sleep")) {
629 rs->print_flags |= DMPF_DAEMON_SLEEP;
630 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
631 rs->ti->error = "daemon sleep period out of range";
632 return -EINVAL;
634 rs->md.bitmap_info.daemon_sleep = value;
635 } else if (!strcasecmp(key, "stripe_cache")) {
636 rs->print_flags |= DMPF_STRIPE_CACHE;
639 * In device-mapper, we specify things in sectors, but
640 * MD records this value in kB
642 value /= 2;
644 if ((rs->raid_type->level != 5) &&
645 (rs->raid_type->level != 6)) {
646 rs->ti->error = "Inappropriate argument: stripe_cache";
647 return -EINVAL;
649 if (raid5_set_cache_size(&rs->md, (int)value)) {
650 rs->ti->error = "Bad stripe_cache size";
651 return -EINVAL;
653 } else if (!strcasecmp(key, "min_recovery_rate")) {
654 rs->print_flags |= DMPF_MIN_RECOVERY_RATE;
655 if (value > INT_MAX) {
656 rs->ti->error = "min_recovery_rate out of range";
657 return -EINVAL;
659 rs->md.sync_speed_min = (int)value;
660 } else if (!strcasecmp(key, "max_recovery_rate")) {
661 rs->print_flags |= DMPF_MAX_RECOVERY_RATE;
662 if (value > INT_MAX) {
663 rs->ti->error = "max_recovery_rate out of range";
664 return -EINVAL;
666 rs->md.sync_speed_max = (int)value;
667 } else if (!strcasecmp(key, "region_size")) {
668 rs->print_flags |= DMPF_REGION_SIZE;
669 region_size = value;
670 } else if (!strcasecmp(key, "raid10_copies") &&
671 (rs->raid_type->level == 10)) {
672 if ((value < 2) || (value > 0xFF)) {
673 rs->ti->error = "Bad value for 'raid10_copies'";
674 return -EINVAL;
676 rs->print_flags |= DMPF_RAID10_COPIES;
677 raid10_copies = value;
678 } else {
679 DMERR("Unable to parse RAID parameter: %s", key);
680 rs->ti->error = "Unable to parse RAID parameters";
681 return -EINVAL;
685 if (validate_region_size(rs, region_size))
686 return -EINVAL;
688 if (rs->md.chunk_sectors)
689 max_io_len = rs->md.chunk_sectors;
690 else
691 max_io_len = region_size;
693 if (dm_set_target_max_io_len(rs->ti, max_io_len))
694 return -EINVAL;
696 if (rs->raid_type->level == 10) {
697 if (raid10_copies > rs->md.raid_disks) {
698 rs->ti->error = "Not enough devices to satisfy specification";
699 return -EINVAL;
703 * If the format is not "near", we only support
704 * two copies at the moment.
706 if (strcmp("near", raid10_format) && (raid10_copies > 2)) {
707 rs->ti->error = "Too many copies for given RAID10 format.";
708 return -EINVAL;
711 /* (Len * #mirrors) / #devices */
712 sectors_per_dev = rs->ti->len * raid10_copies;
713 sector_div(sectors_per_dev, rs->md.raid_disks);
715 rs->md.layout = raid10_format_to_md_layout(raid10_format,
716 raid10_copies);
717 rs->md.new_layout = rs->md.layout;
718 } else if ((rs->raid_type->level > 1) &&
719 sector_div(sectors_per_dev,
720 (rs->md.raid_disks - rs->raid_type->parity_devs))) {
721 rs->ti->error = "Target length not divisible by number of data devices";
722 return -EINVAL;
724 rs->md.dev_sectors = sectors_per_dev;
726 /* Assume there are no metadata devices until the drives are parsed */
727 rs->md.persistent = 0;
728 rs->md.external = 1;
730 return 0;
733 static void do_table_event(struct work_struct *ws)
735 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
737 dm_table_event(rs->ti->table);
740 static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
742 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
744 if (rs->raid_type->level == 1)
745 return md_raid1_congested(&rs->md, bits);
747 if (rs->raid_type->level == 10)
748 return md_raid10_congested(&rs->md, bits);
750 return md_raid5_congested(&rs->md, bits);
754 * This structure is never routinely used by userspace, unlike md superblocks.
755 * Devices with this superblock should only ever be accessed via device-mapper.
757 #define DM_RAID_MAGIC 0x64526D44
758 struct dm_raid_superblock {
759 __le32 magic; /* "DmRd" */
760 __le32 features; /* Used to indicate possible future changes */
762 __le32 num_devices; /* Number of devices in this array. (Max 64) */
763 __le32 array_position; /* The position of this drive in the array */
765 __le64 events; /* Incremented by md when superblock updated */
766 __le64 failed_devices; /* Bit field of devices to indicate failures */
769 * This offset tracks the progress of the repair or replacement of
770 * an individual drive.
772 __le64 disk_recovery_offset;
775 * This offset tracks the progress of the initial array
776 * synchronisation/parity calculation.
778 __le64 array_resync_offset;
781 * RAID characteristics
783 __le32 level;
784 __le32 layout;
785 __le32 stripe_sectors;
787 /* Remainder of a logical block is zero-filled when writing (see super_sync()). */
788 } __packed;
790 static int read_disk_sb(struct md_rdev *rdev, int size)
792 BUG_ON(!rdev->sb_page);
794 if (rdev->sb_loaded)
795 return 0;
797 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 1)) {
798 DMERR("Failed to read superblock of device at position %d",
799 rdev->raid_disk);
800 md_error(rdev->mddev, rdev);
801 return -EINVAL;
804 rdev->sb_loaded = 1;
806 return 0;
809 static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
811 int i;
812 uint64_t failed_devices;
813 struct dm_raid_superblock *sb;
814 struct raid_set *rs = container_of(mddev, struct raid_set, md);
816 sb = page_address(rdev->sb_page);
817 failed_devices = le64_to_cpu(sb->failed_devices);
819 for (i = 0; i < mddev->raid_disks; i++)
820 if (!rs->dev[i].data_dev ||
821 test_bit(Faulty, &(rs->dev[i].rdev.flags)))
822 failed_devices |= (1ULL << i);
824 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
826 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
827 sb->features = cpu_to_le32(0); /* No features yet */
829 sb->num_devices = cpu_to_le32(mddev->raid_disks);
830 sb->array_position = cpu_to_le32(rdev->raid_disk);
832 sb->events = cpu_to_le64(mddev->events);
833 sb->failed_devices = cpu_to_le64(failed_devices);
835 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
836 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
838 sb->level = cpu_to_le32(mddev->level);
839 sb->layout = cpu_to_le32(mddev->layout);
840 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
844 * super_load
846 * This function creates a superblock if one is not found on the device
847 * and will decide which superblock to use if there's a choice.
849 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
851 static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
853 int ret;
854 struct dm_raid_superblock *sb;
855 struct dm_raid_superblock *refsb;
856 uint64_t events_sb, events_refsb;
858 rdev->sb_start = 0;
859 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
860 if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
861 DMERR("superblock size of a logical block is no longer valid");
862 return -EINVAL;
865 ret = read_disk_sb(rdev, rdev->sb_size);
866 if (ret)
867 return ret;
869 sb = page_address(rdev->sb_page);
872 * Two cases that we want to write new superblocks and rebuild:
873 * 1) New device (no matching magic number)
874 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
876 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
877 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
878 super_sync(rdev->mddev, rdev);
880 set_bit(FirstUse, &rdev->flags);
882 /* Force writing of superblocks to disk */
883 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
885 /* Any superblock is better than none, choose that if given */
886 return refdev ? 0 : 1;
889 if (!refdev)
890 return 1;
892 events_sb = le64_to_cpu(sb->events);
894 refsb = page_address(refdev->sb_page);
895 events_refsb = le64_to_cpu(refsb->events);
897 return (events_sb > events_refsb) ? 1 : 0;
900 static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
902 int role;
903 struct raid_set *rs = container_of(mddev, struct raid_set, md);
904 uint64_t events_sb;
905 uint64_t failed_devices;
906 struct dm_raid_superblock *sb;
907 uint32_t new_devs = 0;
908 uint32_t rebuilds = 0;
909 struct md_rdev *r;
910 struct dm_raid_superblock *sb2;
912 sb = page_address(rdev->sb_page);
913 events_sb = le64_to_cpu(sb->events);
914 failed_devices = le64_to_cpu(sb->failed_devices);
917 * Initialise to 1 if this is a new superblock.
919 mddev->events = events_sb ? : 1;
922 * Reshaping is not currently allowed
924 if (le32_to_cpu(sb->level) != mddev->level) {
925 DMERR("Reshaping arrays not yet supported. (RAID level change)");
926 return -EINVAL;
928 if (le32_to_cpu(sb->layout) != mddev->layout) {
929 DMERR("Reshaping arrays not yet supported. (RAID layout change)");
930 DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
931 DMERR(" Old layout: %s w/ %d copies",
932 raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
933 raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
934 DMERR(" New layout: %s w/ %d copies",
935 raid10_md_layout_to_format(mddev->layout),
936 raid10_md_layout_to_copies(mddev->layout));
937 return -EINVAL;
939 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
940 DMERR("Reshaping arrays not yet supported. (stripe sectors change)");
941 return -EINVAL;
944 /* We can only change the number of devices in RAID1 right now */
945 if ((rs->raid_type->level != 1) &&
946 (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
947 DMERR("Reshaping arrays not yet supported. (device count change)");
948 return -EINVAL;
951 if (!(rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC)))
952 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
955 * During load, we set FirstUse if a new superblock was written.
956 * There are two reasons we might not have a superblock:
957 * 1) The array is brand new - in which case, all of the
958 * devices must have their In_sync bit set. Also,
959 * recovery_cp must be 0, unless forced.
960 * 2) This is a new device being added to an old array
961 * and the new device needs to be rebuilt - in which
962 * case the In_sync bit will /not/ be set and
963 * recovery_cp must be MaxSector.
965 rdev_for_each(r, mddev) {
966 if (!test_bit(In_sync, &r->flags)) {
967 DMINFO("Device %d specified for rebuild: "
968 "Clearing superblock", r->raid_disk);
969 rebuilds++;
970 } else if (test_bit(FirstUse, &r->flags))
971 new_devs++;
974 if (!rebuilds) {
975 if (new_devs == mddev->raid_disks) {
976 DMINFO("Superblocks created for new array");
977 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
978 } else if (new_devs) {
979 DMERR("New device injected "
980 "into existing array without 'rebuild' "
981 "parameter specified");
982 return -EINVAL;
984 } else if (new_devs) {
985 DMERR("'rebuild' devices cannot be "
986 "injected into an array with other first-time devices");
987 return -EINVAL;
988 } else if (mddev->recovery_cp != MaxSector) {
989 DMERR("'rebuild' specified while array is not in-sync");
990 return -EINVAL;
994 * Now we set the Faulty bit for those devices that are
995 * recorded in the superblock as failed.
997 rdev_for_each(r, mddev) {
998 if (!r->sb_page)
999 continue;
1000 sb2 = page_address(r->sb_page);
1001 sb2->failed_devices = 0;
1004 * Check for any device re-ordering.
1006 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
1007 role = le32_to_cpu(sb2->array_position);
1008 if (role != r->raid_disk) {
1009 if (rs->raid_type->level != 1) {
1010 rs->ti->error = "Cannot change device "
1011 "positions in RAID array";
1012 return -EINVAL;
1014 DMINFO("RAID1 device #%d now at position #%d",
1015 role, r->raid_disk);
1019 * Partial recovery is performed on
1020 * returning failed devices.
1022 if (failed_devices & (1 << role))
1023 set_bit(Faulty, &r->flags);
1027 return 0;
1030 static int super_validate(struct mddev *mddev, struct md_rdev *rdev)
1032 struct dm_raid_superblock *sb = page_address(rdev->sb_page);
1035 * If mddev->events is not set, we know we have not yet initialized
1036 * the array.
1038 if (!mddev->events && super_init_validation(mddev, rdev))
1039 return -EINVAL;
1041 mddev->bitmap_info.offset = 4096 >> 9; /* Enable bitmap creation */
1042 rdev->mddev->bitmap_info.default_offset = 4096 >> 9;
1043 if (!test_bit(FirstUse, &rdev->flags)) {
1044 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
1045 if (rdev->recovery_offset != MaxSector)
1046 clear_bit(In_sync, &rdev->flags);
1050 * If a device comes back, set it as not In_sync and no longer faulty.
1052 if (test_bit(Faulty, &rdev->flags)) {
1053 clear_bit(Faulty, &rdev->flags);
1054 clear_bit(In_sync, &rdev->flags);
1055 rdev->saved_raid_disk = rdev->raid_disk;
1056 rdev->recovery_offset = 0;
1059 clear_bit(FirstUse, &rdev->flags);
1061 return 0;
1065 * Analyse superblocks and select the freshest.
1067 static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
1069 int ret;
1070 struct raid_dev *dev;
1071 struct md_rdev *rdev, *tmp, *freshest;
1072 struct mddev *mddev = &rs->md;
1074 freshest = NULL;
1075 rdev_for_each_safe(rdev, tmp, mddev) {
1077 * Skipping super_load due to DMPF_SYNC will cause
1078 * the array to undergo initialization again as
1079 * though it were new. This is the intended effect
1080 * of the "sync" directive.
1082 * When reshaping capability is added, we must ensure
1083 * that the "sync" directive is disallowed during the
1084 * reshape.
1086 if (rs->print_flags & DMPF_SYNC)
1087 continue;
1089 if (!rdev->meta_bdev)
1090 continue;
1092 ret = super_load(rdev, freshest);
1094 switch (ret) {
1095 case 1:
1096 freshest = rdev;
1097 break;
1098 case 0:
1099 break;
1100 default:
1101 dev = container_of(rdev, struct raid_dev, rdev);
1102 if (dev->meta_dev)
1103 dm_put_device(ti, dev->meta_dev);
1105 dev->meta_dev = NULL;
1106 rdev->meta_bdev = NULL;
1108 if (rdev->sb_page)
1109 put_page(rdev->sb_page);
1111 rdev->sb_page = NULL;
1113 rdev->sb_loaded = 0;
1116 * We might be able to salvage the data device
1117 * even though the meta device has failed. For
1118 * now, we behave as though '- -' had been
1119 * set for this device in the table.
1121 if (dev->data_dev)
1122 dm_put_device(ti, dev->data_dev);
1124 dev->data_dev = NULL;
1125 rdev->bdev = NULL;
1127 list_del(&rdev->same_set);
1131 if (!freshest)
1132 return 0;
1134 if (validate_raid_redundancy(rs)) {
1135 rs->ti->error = "Insufficient redundancy to activate array";
1136 return -EINVAL;
1140 * Validation of the freshest device provides the source of
1141 * validation for the remaining devices.
1143 ti->error = "Unable to assemble array: Invalid superblocks";
1144 if (super_validate(mddev, freshest))
1145 return -EINVAL;
1147 rdev_for_each(rdev, mddev)
1148 if ((rdev != freshest) && super_validate(mddev, rdev))
1149 return -EINVAL;
1151 return 0;
1155 * Construct a RAID4/5/6 mapping:
1156 * Args:
1157 * <raid_type> <#raid_params> <raid_params> \
1158 * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
1160 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
1161 * details on possible <raid_params>.
1163 static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
1165 int ret;
1166 struct raid_type *rt;
1167 unsigned long num_raid_params, num_raid_devs;
1168 struct raid_set *rs = NULL;
1170 /* Must have at least <raid_type> <#raid_params> */
1171 if (argc < 2) {
1172 ti->error = "Too few arguments";
1173 return -EINVAL;
1176 /* raid type */
1177 rt = get_raid_type(argv[0]);
1178 if (!rt) {
1179 ti->error = "Unrecognised raid_type";
1180 return -EINVAL;
1182 argc--;
1183 argv++;
1185 /* number of RAID parameters */
1186 if (strict_strtoul(argv[0], 10, &num_raid_params) < 0) {
1187 ti->error = "Cannot understand number of RAID parameters";
1188 return -EINVAL;
1190 argc--;
1191 argv++;
1193 /* Skip over RAID params for now and find out # of devices */
1194 if (num_raid_params + 1 > argc) {
1195 ti->error = "Arguments do not agree with counts given";
1196 return -EINVAL;
1199 if ((strict_strtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) ||
1200 (num_raid_devs >= INT_MAX)) {
1201 ti->error = "Cannot understand number of raid devices";
1202 return -EINVAL;
1205 rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
1206 if (IS_ERR(rs))
1207 return PTR_ERR(rs);
1209 ret = parse_raid_params(rs, argv, (unsigned)num_raid_params);
1210 if (ret)
1211 goto bad;
1213 ret = -EINVAL;
1215 argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
1216 argv += num_raid_params + 1;
1218 if (argc != (num_raid_devs * 2)) {
1219 ti->error = "Supplied RAID devices does not match the count given";
1220 goto bad;
1223 ret = dev_parms(rs, argv);
1224 if (ret)
1225 goto bad;
1227 rs->md.sync_super = super_sync;
1228 ret = analyse_superblocks(ti, rs);
1229 if (ret)
1230 goto bad;
1232 INIT_WORK(&rs->md.event_work, do_table_event);
1233 ti->private = rs;
1234 ti->num_flush_bios = 1;
1236 mutex_lock(&rs->md.reconfig_mutex);
1237 ret = md_run(&rs->md);
1238 rs->md.in_sync = 0; /* Assume already marked dirty */
1239 mutex_unlock(&rs->md.reconfig_mutex);
1241 if (ret) {
1242 ti->error = "Fail to run raid array";
1243 goto bad;
1246 if (ti->len != rs->md.array_sectors) {
1247 ti->error = "Array size does not match requested target length";
1248 ret = -EINVAL;
1249 goto size_mismatch;
1251 rs->callbacks.congested_fn = raid_is_congested;
1252 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
1254 mddev_suspend(&rs->md);
1255 return 0;
1257 size_mismatch:
1258 md_stop(&rs->md);
1259 bad:
1260 context_free(rs);
1262 return ret;
1265 static void raid_dtr(struct dm_target *ti)
1267 struct raid_set *rs = ti->private;
1269 list_del_init(&rs->callbacks.list);
1270 md_stop(&rs->md);
1271 context_free(rs);
1274 static int raid_map(struct dm_target *ti, struct bio *bio)
1276 struct raid_set *rs = ti->private;
1277 struct mddev *mddev = &rs->md;
1279 mddev->pers->make_request(mddev, bio);
1281 return DM_MAPIO_SUBMITTED;
1284 static const char *decipher_sync_action(struct mddev *mddev)
1286 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
1287 return "frozen";
1289 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1290 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
1291 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1292 return "reshape";
1294 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1295 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1296 return "resync";
1297 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1298 return "check";
1299 return "repair";
1302 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
1303 return "recover";
1306 return "idle";
1309 static void raid_status(struct dm_target *ti, status_type_t type,
1310 unsigned status_flags, char *result, unsigned maxlen)
1312 struct raid_set *rs = ti->private;
1313 unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
1314 unsigned sz = 0;
1315 int i, array_in_sync = 0;
1316 sector_t sync;
1318 switch (type) {
1319 case STATUSTYPE_INFO:
1320 DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
1322 if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
1323 sync = rs->md.curr_resync_completed;
1324 else
1325 sync = rs->md.recovery_cp;
1327 if (sync >= rs->md.resync_max_sectors) {
1329 * Sync complete.
1331 array_in_sync = 1;
1332 sync = rs->md.resync_max_sectors;
1333 } else if (test_bit(MD_RECOVERY_REQUESTED, &rs->md.recovery)) {
1335 * If "check" or "repair" is occurring, the array has
1336 * undergone and initial sync and the health characters
1337 * should not be 'a' anymore.
1339 array_in_sync = 1;
1340 } else {
1342 * The array may be doing an initial sync, or it may
1343 * be rebuilding individual components. If all the
1344 * devices are In_sync, then it is the array that is
1345 * being initialized.
1347 for (i = 0; i < rs->md.raid_disks; i++)
1348 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
1349 array_in_sync = 1;
1353 * Status characters:
1354 * 'D' = Dead/Failed device
1355 * 'a' = Alive but not in-sync
1356 * 'A' = Alive and in-sync
1358 for (i = 0; i < rs->md.raid_disks; i++) {
1359 if (test_bit(Faulty, &rs->dev[i].rdev.flags))
1360 DMEMIT("D");
1361 else if (!array_in_sync ||
1362 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1363 DMEMIT("a");
1364 else
1365 DMEMIT("A");
1369 * In-sync ratio:
1370 * The in-sync ratio shows the progress of:
1371 * - Initializing the array
1372 * - Rebuilding a subset of devices of the array
1373 * The user can distinguish between the two by referring
1374 * to the status characters.
1376 DMEMIT(" %llu/%llu",
1377 (unsigned long long) sync,
1378 (unsigned long long) rs->md.resync_max_sectors);
1381 * Sync action:
1382 * See Documentation/device-mapper/dm-raid.c for
1383 * information on each of these states.
1385 DMEMIT(" %s", decipher_sync_action(&rs->md));
1388 * resync_mismatches/mismatch_cnt
1389 * This field shows the number of discrepancies found when
1390 * performing a "check" of the array.
1392 DMEMIT(" %llu",
1393 (unsigned long long)
1394 atomic64_read(&rs->md.resync_mismatches));
1395 break;
1396 case STATUSTYPE_TABLE:
1397 /* The string you would use to construct this array */
1398 for (i = 0; i < rs->md.raid_disks; i++) {
1399 if ((rs->print_flags & DMPF_REBUILD) &&
1400 rs->dev[i].data_dev &&
1401 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1402 raid_param_cnt += 2; /* for rebuilds */
1403 if (rs->dev[i].data_dev &&
1404 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1405 raid_param_cnt += 2;
1408 raid_param_cnt += (hweight32(rs->print_flags & ~DMPF_REBUILD) * 2);
1409 if (rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC))
1410 raid_param_cnt--;
1412 DMEMIT("%s %u %u", rs->raid_type->name,
1413 raid_param_cnt, rs->md.chunk_sectors);
1415 if ((rs->print_flags & DMPF_SYNC) &&
1416 (rs->md.recovery_cp == MaxSector))
1417 DMEMIT(" sync");
1418 if (rs->print_flags & DMPF_NOSYNC)
1419 DMEMIT(" nosync");
1421 for (i = 0; i < rs->md.raid_disks; i++)
1422 if ((rs->print_flags & DMPF_REBUILD) &&
1423 rs->dev[i].data_dev &&
1424 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1425 DMEMIT(" rebuild %u", i);
1427 if (rs->print_flags & DMPF_DAEMON_SLEEP)
1428 DMEMIT(" daemon_sleep %lu",
1429 rs->md.bitmap_info.daemon_sleep);
1431 if (rs->print_flags & DMPF_MIN_RECOVERY_RATE)
1432 DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
1434 if (rs->print_flags & DMPF_MAX_RECOVERY_RATE)
1435 DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
1437 for (i = 0; i < rs->md.raid_disks; i++)
1438 if (rs->dev[i].data_dev &&
1439 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1440 DMEMIT(" write_mostly %u", i);
1442 if (rs->print_flags & DMPF_MAX_WRITE_BEHIND)
1443 DMEMIT(" max_write_behind %lu",
1444 rs->md.bitmap_info.max_write_behind);
1446 if (rs->print_flags & DMPF_STRIPE_CACHE) {
1447 struct r5conf *conf = rs->md.private;
1449 /* convert from kiB to sectors */
1450 DMEMIT(" stripe_cache %d",
1451 conf ? conf->max_nr_stripes * 2 : 0);
1454 if (rs->print_flags & DMPF_REGION_SIZE)
1455 DMEMIT(" region_size %lu",
1456 rs->md.bitmap_info.chunksize >> 9);
1458 if (rs->print_flags & DMPF_RAID10_COPIES)
1459 DMEMIT(" raid10_copies %u",
1460 raid10_md_layout_to_copies(rs->md.layout));
1462 if (rs->print_flags & DMPF_RAID10_FORMAT)
1463 DMEMIT(" raid10_format %s",
1464 raid10_md_layout_to_format(rs->md.layout));
1466 DMEMIT(" %d", rs->md.raid_disks);
1467 for (i = 0; i < rs->md.raid_disks; i++) {
1468 if (rs->dev[i].meta_dev)
1469 DMEMIT(" %s", rs->dev[i].meta_dev->name);
1470 else
1471 DMEMIT(" -");
1473 if (rs->dev[i].data_dev)
1474 DMEMIT(" %s", rs->dev[i].data_dev->name);
1475 else
1476 DMEMIT(" -");
1481 static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
1483 struct raid_set *rs = ti->private;
1484 struct mddev *mddev = &rs->md;
1486 if (!strcasecmp(argv[0], "reshape")) {
1487 DMERR("Reshape not supported.");
1488 return -EINVAL;
1491 if (!mddev->pers || !mddev->pers->sync_request)
1492 return -EINVAL;
1494 if (!strcasecmp(argv[0], "frozen"))
1495 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
1496 else
1497 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
1499 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
1500 if (mddev->sync_thread) {
1501 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1502 md_reap_sync_thread(mddev);
1504 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1505 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
1506 return -EBUSY;
1507 else if (!strcasecmp(argv[0], "resync"))
1508 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1509 else if (!strcasecmp(argv[0], "recover")) {
1510 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
1511 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1512 } else {
1513 if (!strcasecmp(argv[0], "check"))
1514 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1515 else if (!!strcasecmp(argv[0], "repair"))
1516 return -EINVAL;
1517 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1518 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1520 if (mddev->ro == 2) {
1521 /* A write to sync_action is enough to justify
1522 * canceling read-auto mode
1524 mddev->ro = 0;
1525 if (!mddev->suspended)
1526 md_wakeup_thread(mddev->sync_thread);
1528 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1529 if (!mddev->suspended)
1530 md_wakeup_thread(mddev->thread);
1532 return 0;
1535 static int raid_iterate_devices(struct dm_target *ti,
1536 iterate_devices_callout_fn fn, void *data)
1538 struct raid_set *rs = ti->private;
1539 unsigned i;
1540 int ret = 0;
1542 for (i = 0; !ret && i < rs->md.raid_disks; i++)
1543 if (rs->dev[i].data_dev)
1544 ret = fn(ti,
1545 rs->dev[i].data_dev,
1546 0, /* No offset on data devs */
1547 rs->md.dev_sectors,
1548 data);
1550 return ret;
1553 static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
1555 struct raid_set *rs = ti->private;
1556 unsigned chunk_size = rs->md.chunk_sectors << 9;
1557 struct r5conf *conf = rs->md.private;
1559 blk_limits_io_min(limits, chunk_size);
1560 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
1563 static void raid_presuspend(struct dm_target *ti)
1565 struct raid_set *rs = ti->private;
1567 md_stop_writes(&rs->md);
1570 static void raid_postsuspend(struct dm_target *ti)
1572 struct raid_set *rs = ti->private;
1574 mddev_suspend(&rs->md);
1577 static void raid_resume(struct dm_target *ti)
1579 struct raid_set *rs = ti->private;
1581 set_bit(MD_CHANGE_DEVS, &rs->md.flags);
1582 if (!rs->bitmap_loaded) {
1583 bitmap_load(&rs->md);
1584 rs->bitmap_loaded = 1;
1587 clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
1588 mddev_resume(&rs->md);
1591 static struct target_type raid_target = {
1592 .name = "raid",
1593 .version = {1, 5, 0},
1594 .module = THIS_MODULE,
1595 .ctr = raid_ctr,
1596 .dtr = raid_dtr,
1597 .map = raid_map,
1598 .status = raid_status,
1599 .message = raid_message,
1600 .iterate_devices = raid_iterate_devices,
1601 .io_hints = raid_io_hints,
1602 .presuspend = raid_presuspend,
1603 .postsuspend = raid_postsuspend,
1604 .resume = raid_resume,
1607 static int __init dm_raid_init(void)
1609 DMINFO("Loading target version %u.%u.%u",
1610 raid_target.version[0],
1611 raid_target.version[1],
1612 raid_target.version[2]);
1613 return dm_register_target(&raid_target);
1616 static void __exit dm_raid_exit(void)
1618 dm_unregister_target(&raid_target);
1621 module_init(dm_raid_init);
1622 module_exit(dm_raid_exit);
1624 MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
1625 MODULE_ALIAS("dm-raid1");
1626 MODULE_ALIAS("dm-raid10");
1627 MODULE_ALIAS("dm-raid4");
1628 MODULE_ALIAS("dm-raid5");
1629 MODULE_ALIAS("dm-raid6");
1630 MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
1631 MODULE_LICENSE("GPL");