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