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.
8 #include <linux/slab.h>
9 #include <linux/module.h>
16 #include <linux/device-mapper.h>
18 #define DM_MSG_PREFIX "raid"
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.
24 #define FirstUse 10 /* rdev flag */
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
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 ------|
39 struct dm_dev *meta_dev;
40 struct dm_dev *data_dev;
45 * Flags for rs->print_flags field.
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
59 uint32_t bitmap_loaded;
63 struct raid_type *raid_type;
64 struct dm_target_callbacks callbacks;
66 struct raid_dev dev[0];
69 /* Supported raid types and properties. */
70 static 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. */
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}
89 static struct raid_type *get_raid_type(char *name)
93 for (i = 0; i < ARRAY_SIZE(raid_types); i++)
94 if (!strcmp(raid_types[i].name, name))
95 return &raid_types[i];
100 static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
104 sector_t sectors_per_dev;
106 if (raid_devs <= raid_type->parity_devs) {
107 ti->error = "Insufficient number of devices";
108 return ERR_PTR(-EINVAL);
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);
118 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
120 ti->error = "Cannot allocate raid context";
121 return ERR_PTR(-ENOMEM);
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;
137 for (i = 0; i < raid_devs; i++)
138 md_rdev_init(&rs->dev[i].rdev);
141 * Remaining items to be initialized by further RAID params:
144 * rs->md.chunk_sectors
145 * rs->md.new_chunk_sectors
151 static void context_free(struct raid_set *rs)
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);
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
171 * The following are permitted:
174 * <meta_dev> <data_dev>
176 * The following is not allowed:
179 * This code parses those words. If there is a failure,
180 * the caller must use context_free to unwind the operations.
182 static int dev_parms(struct raid_set *rs, char **argv)
186 int metadata_available = 0;
189 for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {
190 rs->dev[i].rdev.raid_disk = i;
192 rs->dev[i].meta_dev = NULL;
193 rs->dev[i].data_dev = NULL;
196 * There are no offsets, since there is a separate device
197 * for data and metadata.
199 rs->dev[i].rdev.data_offset = 0;
200 rs->dev[i].rdev.mddev = &rs->md;
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";
210 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
211 if (!rs->dev[i].rdev.sb_page)
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";
222 rs->ti->error = "No data device supplied with metadata device";
223 if (rs->dev[i].meta_dev)
229 ret = dm_get_device(rs->ti, argv[1],
230 dm_table_get_mode(rs->ti->table),
231 &rs->dev[i].data_dev);
233 rs->ti->error = "RAID device lookup failure";
237 if (rs->dev[i].meta_dev) {
238 metadata_available = 1;
239 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
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))
247 if (metadata_available) {
249 rs->md.persistent = 1;
250 rs->md.major_version = 2;
251 } else if (rebuild && !rs->md.recovery_cp) {
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.
257 * Even if there is metadata, the on-disk information may
258 * indicate that the array is not in-sync and it will then
261 * User could specify 'nosync' option if desperate.
263 DMERR("Unable to rebuild drive while array is not in-sync");
264 rs->ti->error = "RAID device lookup failure";
272 * validate_region_size
274 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
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.
279 * Returns: 0 on success, -EINVAL on failure.
281 static int validate_region_size(struct raid_set *rs, unsigned long region_size)
283 unsigned long min_region_size = rs->ti->len / (1 << 21);
287 * Choose a reasonable default. All figures in sectors.
289 if (min_region_size > (1 << 13)) {
290 DMINFO("Choosing default region size of %lu sectors",
292 region_size = min_region_size;
294 DMINFO("Choosing default region size of 4MiB");
295 region_size = 1 << 13; /* sectors */
299 * Validate user-supplied value.
301 if (region_size > rs->ti->len) {
302 rs->ti->error = "Supplied region size is too large";
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";
313 if (!is_power_of_2(region_size)) {
314 rs->ti->error = "Region size is not a power of 2";
318 if (region_size < rs->md.chunk_sectors) {
319 rs->ti->error = "Region size is smaller than the chunk size";
325 * Convert sectors to bytes.
327 rs->md.bitmap_info.chunksize = (region_size << 9);
333 * Possible arguments are...
334 * <chunk_size> [optional_args]
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
341 * [rebuild <idx>] Rebuild the drive indicated by the index
342 * [daemon_sleep <ms>] Time between bitmap daemon work to
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
351 static int parse_raid_params(struct raid_set *rs, char **argv,
352 unsigned num_raid_params)
354 unsigned i, rebuild_cnt = 0;
355 unsigned long value, region_size = 0;
359 * First, parse the in-order required arguments
360 * "chunk_size" is the only argument of this type.
362 if ((strict_strtoul(argv[0], 10, &value) < 0)) {
363 rs->ti->error = "Bad chunk size";
365 } else if (rs->raid_type->level == 1) {
367 DMERR("Ignoring chunk size parameter for RAID 1");
369 } else if (!is_power_of_2(value)) {
370 rs->ti->error = "Chunk size must be a power of 2";
372 } else if (value < 8) {
373 rs->ti->error = "Chunk size value is too small";
377 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
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:
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.
395 * What is found in the superblocks of the devices is always
396 * authoritative, unless 'rebuild' or '[no]sync' was specified.
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;
404 * Second, parse the unordered optional arguments
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;
412 if (!strcasecmp(argv[i], "sync")) {
413 rs->md.recovery_cp = 0;
414 rs->print_flags |= DMPF_SYNC;
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";
425 if (strict_strtoul(argv[i], 10, &value) < 0) {
426 rs->ti->error = "Bad numerical argument given in raid params";
430 if (!strcasecmp(key, "rebuild")) {
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";
439 if (value > rs->md.raid_disks) {
440 rs->ti->error = "Invalid rebuild index given";
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";
451 if (value >= rs->md.raid_disks) {
452 rs->ti->error = "Invalid write_mostly drive index given";
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";
461 rs->print_flags |= DMPF_MAX_WRITE_BEHIND;
464 * In device-mapper, we specify things in sectors, but
465 * MD records this value in kB
468 if (value > COUNTER_MAX) {
469 rs->ti->error = "Max write-behind limit out of range";
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";
479 rs->md.bitmap_info.daemon_sleep = value;
480 } else if (!strcasecmp(key, "stripe_cache")) {
481 rs->print_flags |= DMPF_STRIPE_CACHE;
484 * In device-mapper, we specify things in sectors, but
485 * MD records this value in kB
489 if (rs->raid_type->level < 5) {
490 rs->ti->error = "Inappropriate argument: stripe_cache";
493 if (raid5_set_cache_size(&rs->md, (int)value)) {
494 rs->ti->error = "Bad stripe_cache size";
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";
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";
510 rs->md.sync_speed_max = (int)value;
511 } else if (!strcasecmp(key, "region_size")) {
512 rs->print_flags |= DMPF_REGION_SIZE;
515 DMERR("Unable to parse RAID parameter: %s", key);
516 rs->ti->error = "Unable to parse RAID parameters";
521 if (validate_region_size(rs, region_size))
524 if (rs->md.chunk_sectors)
525 rs->ti->split_io = rs->md.chunk_sectors;
527 rs->ti->split_io = region_size;
529 if (rs->md.chunk_sectors)
530 rs->ti->split_io = rs->md.chunk_sectors;
532 rs->ti->split_io = region_size;
534 /* Assume there are no metadata devices until the drives are parsed */
535 rs->md.persistent = 0;
541 static void do_table_event(struct work_struct *ws)
543 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
545 dm_table_event(rs->ti->table);
548 static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
550 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
552 if (rs->raid_type->level == 1)
553 return md_raid1_congested(&rs->md, bits);
555 return md_raid5_congested(&rs->md, bits);
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.
562 #define DM_RAID_MAGIC 0x64526D44
563 struct dm_raid_superblock {
564 __le32 magic; /* "DmRd" */
565 __le32 features; /* Used to indicate possible future changes */
567 __le32 num_devices; /* Number of devices in this array. (Max 64) */
568 __le32 array_position; /* The position of this drive in the array */
570 __le64 events; /* Incremented by md when superblock updated */
571 __le64 failed_devices; /* Bit field of devices to indicate failures */
574 * This offset tracks the progress of the repair or replacement of
575 * an individual drive.
577 __le64 disk_recovery_offset;
580 * This offset tracks the progress of the initial array
581 * synchronisation/parity calculation.
583 __le64 array_resync_offset;
586 * RAID characteristics
590 __le32 stripe_sectors;
592 __u8 pad[452]; /* Round struct to 512 bytes. */
593 /* Always set to 0 when writing. */
596 static int read_disk_sb(struct md_rdev *rdev, int size)
598 BUG_ON(!rdev->sb_page);
603 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 1)) {
604 DMERR("Failed to read superblock of device at position %d",
606 md_error(rdev->mddev, rdev);
615 static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
618 uint64_t failed_devices;
619 struct dm_raid_superblock *sb;
620 struct raid_set *rs = container_of(mddev, struct raid_set, md);
622 sb = page_address(rdev->sb_page);
623 failed_devices = le64_to_cpu(sb->failed_devices);
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);
630 memset(sb, 0, sizeof(*sb));
632 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
633 sb->features = cpu_to_le32(0); /* No features yet */
635 sb->num_devices = cpu_to_le32(mddev->raid_disks);
636 sb->array_position = cpu_to_le32(rdev->raid_disk);
638 sb->events = cpu_to_le64(mddev->events);
639 sb->failed_devices = cpu_to_le64(failed_devices);
641 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
642 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
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);
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.
655 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
657 static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
660 struct dm_raid_superblock *sb;
661 struct dm_raid_superblock *refsb;
662 uint64_t events_sb, events_refsb;
665 rdev->sb_size = sizeof(*sb);
667 ret = read_disk_sb(rdev, rdev->sb_size);
671 sb = page_address(rdev->sb_page);
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)
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);
682 set_bit(FirstUse, &rdev->flags);
684 /* Force writing of superblocks to disk */
685 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
687 /* Any superblock is better than none, choose that if given */
688 return refdev ? 0 : 1;
694 events_sb = le64_to_cpu(sb->events);
696 refsb = page_address(refdev->sb_page);
697 events_refsb = le64_to_cpu(refsb->events);
699 return (events_sb > events_refsb) ? 1 : 0;
702 static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
705 struct raid_set *rs = container_of(mddev, struct raid_set, md);
707 uint64_t failed_devices;
708 struct dm_raid_superblock *sb;
709 uint32_t new_devs = 0;
710 uint32_t rebuilds = 0;
712 struct dm_raid_superblock *sb2;
714 sb = page_address(rdev->sb_page);
715 events_sb = le64_to_cpu(sb->events);
716 failed_devices = le64_to_cpu(sb->failed_devices);
719 * Initialise to 1 if this is a new superblock.
721 mddev->events = events_sb ? : 1;
724 * Reshaping is not currently allowed
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.");
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.");
740 if (!(rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC)))
741 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
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.
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);
759 } else if (test_bit(FirstUse, &r->flags))
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");
773 } else if (new_devs) {
774 DMERR("'rebuild' devices cannot be "
775 "injected into an array with other first-time devices");
777 } else if (mddev->recovery_cp != MaxSector) {
778 DMERR("'rebuild' specified while array is not in-sync");
783 * Now we set the Faulty bit for those devices that are
784 * recorded in the superblock as failed.
786 rdev_for_each(r, mddev) {
789 sb2 = page_address(r->sb_page);
790 sb2->failed_devices = 0;
793 * Check for any device re-ordering.
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";
803 DMINFO("RAID1 device #%d now at position #%d",
808 * Partial recovery is performed on
809 * returning failed devices.
811 if (failed_devices & (1 << role))
812 set_bit(Faulty, &r->flags);
819 static int super_validate(struct mddev *mddev, struct md_rdev *rdev)
821 struct dm_raid_superblock *sb = page_address(rdev->sb_page);
824 * If mddev->events is not set, we know we have not yet initialized
827 if (!mddev->events && super_init_validation(mddev, rdev))
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);
839 * If a device comes back, set it as not In_sync and no longer faulty.
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;
848 clear_bit(FirstUse, &rdev->flags);
854 * Analyse superblocks and select the freshest.
856 static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
859 unsigned redundancy = 0;
860 struct raid_dev *dev;
861 struct md_rdev *rdev, *tmp, *freshest;
862 struct mddev *mddev = &rs->md;
864 switch (rs->raid_type->level) {
866 redundancy = rs->md.raid_disks - 1;
871 redundancy = rs->raid_type->parity_devs;
874 ti->error = "Unknown RAID type";
879 rdev_for_each_safe(rdev, tmp, mddev) {
880 if (!rdev->meta_bdev)
883 ret = super_load(rdev, freshest);
892 dev = container_of(rdev, struct raid_dev, rdev);
895 dm_put_device(ti, dev->meta_dev);
897 dev->meta_dev = NULL;
898 rdev->meta_bdev = NULL;
901 put_page(rdev->sb_page);
903 rdev->sb_page = NULL;
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.
914 dm_put_device(ti, dev->data_dev);
916 dev->data_dev = NULL;
919 list_del(&rdev->same_set);
923 ti->error = "Failed to load superblock";
932 * Validation of the freshest device provides the source of
933 * validation for the remaining devices.
935 ti->error = "Unable to assemble array: Invalid superblocks";
936 if (super_validate(mddev, freshest))
939 rdev_for_each(rdev, mddev)
940 if ((rdev != freshest) && super_validate(mddev, rdev))
947 * Construct a RAID4/5/6 mapping:
949 * <raid_type> <#raid_params> <raid_params> \
950 * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
952 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
953 * details on possible <raid_params>.
955 static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
958 struct raid_type *rt;
959 unsigned long num_raid_params, num_raid_devs;
960 struct raid_set *rs = NULL;
962 /* Must have at least <raid_type> <#raid_params> */
964 ti->error = "Too few arguments";
969 rt = get_raid_type(argv[0]);
971 ti->error = "Unrecognised raid_type";
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";
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";
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";
997 rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
1001 ret = parse_raid_params(rs, argv, (unsigned)num_raid_params);
1007 argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
1008 argv += num_raid_params + 1;
1010 if (argc != (num_raid_devs * 2)) {
1011 ti->error = "Supplied RAID devices does not match the count given";
1015 ret = dev_parms(rs, argv);
1019 rs->md.sync_super = super_sync;
1020 ret = analyse_superblocks(ti, rs);
1024 INIT_WORK(&rs->md.event_work, do_table_event);
1026 ti->num_flush_requests = 1;
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);
1034 ti->error = "Fail to run raid array";
1038 rs->callbacks.congested_fn = raid_is_congested;
1039 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
1041 mddev_suspend(&rs->md);
1050 static void raid_dtr(struct dm_target *ti)
1052 struct raid_set *rs = ti->private;
1054 list_del_init(&rs->callbacks.list);
1059 static int raid_map(struct dm_target *ti, struct bio *bio, union map_info *map_context)
1061 struct raid_set *rs = ti->private;
1062 struct mddev *mddev = &rs->md;
1064 mddev->pers->make_request(mddev, bio);
1066 return DM_MAPIO_SUBMITTED;
1069 static int raid_status(struct dm_target *ti, status_type_t type,
1070 char *result, unsigned maxlen)
1072 struct raid_set *rs = ti->private;
1073 unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
1075 int i, array_in_sync = 0;
1079 case STATUSTYPE_INFO:
1080 DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
1082 if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
1083 sync = rs->md.curr_resync_completed;
1085 sync = rs->md.recovery_cp;
1087 if (sync >= rs->md.resync_max_sectors) {
1089 sync = rs->md.resync_max_sectors;
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.
1097 for (i = 0; i < rs->md.raid_disks; i++)
1098 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
1102 * Status characters:
1103 * 'D' = Dead/Failed device
1104 * 'a' = Alive but not in-sync
1105 * 'A' = Alive and in-sync
1107 for (i = 0; i < rs->md.raid_disks; i++) {
1108 if (test_bit(Faulty, &rs->dev[i].rdev.flags))
1110 else if (!array_in_sync ||
1111 !test_bit(In_sync, &rs->dev[i].rdev.flags))
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.
1125 DMEMIT(" %llu/%llu",
1126 (unsigned long long) sync,
1127 (unsigned long long) rs->md.resync_max_sectors);
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;
1142 raid_param_cnt += (hweight32(rs->print_flags & ~DMPF_REBUILD) * 2);
1143 if (rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC))
1146 DMEMIT("%s %u %u", rs->raid_type->name,
1147 raid_param_cnt, rs->md.chunk_sectors);
1149 if ((rs->print_flags & DMPF_SYNC) &&
1150 (rs->md.recovery_cp == MaxSector))
1152 if (rs->print_flags & DMPF_NOSYNC)
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);
1161 if (rs->print_flags & DMPF_DAEMON_SLEEP)
1162 DMEMIT(" daemon_sleep %lu",
1163 rs->md.bitmap_info.daemon_sleep);
1165 if (rs->print_flags & DMPF_MIN_RECOVERY_RATE)
1166 DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
1168 if (rs->print_flags & DMPF_MAX_RECOVERY_RATE)
1169 DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
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);
1176 if (rs->print_flags & DMPF_MAX_WRITE_BEHIND)
1177 DMEMIT(" max_write_behind %lu",
1178 rs->md.bitmap_info.max_write_behind);
1180 if (rs->print_flags & DMPF_STRIPE_CACHE) {
1181 struct r5conf *conf = rs->md.private;
1183 /* convert from kiB to sectors */
1184 DMEMIT(" stripe_cache %d",
1185 conf ? conf->max_nr_stripes * 2 : 0);
1188 if (rs->print_flags & DMPF_REGION_SIZE)
1189 DMEMIT(" region_size %lu",
1190 rs->md.bitmap_info.chunksize >> 9);
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);
1199 if (rs->dev[i].data_dev)
1200 DMEMIT(" %s", rs->dev[i].data_dev->name);
1209 static int raid_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
1211 struct raid_set *rs = ti->private;
1215 for (i = 0; !ret && i < rs->md.raid_disks; i++)
1216 if (rs->dev[i].data_dev)
1218 rs->dev[i].data_dev,
1219 0, /* No offset on data devs */
1226 static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
1228 struct raid_set *rs = ti->private;
1229 unsigned chunk_size = rs->md.chunk_sectors << 9;
1230 struct r5conf *conf = rs->md.private;
1232 blk_limits_io_min(limits, chunk_size);
1233 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
1236 static void raid_presuspend(struct dm_target *ti)
1238 struct raid_set *rs = ti->private;
1240 md_stop_writes(&rs->md);
1243 static void raid_postsuspend(struct dm_target *ti)
1245 struct raid_set *rs = ti->private;
1247 mddev_suspend(&rs->md);
1250 static void raid_resume(struct dm_target *ti)
1252 struct raid_set *rs = ti->private;
1254 set_bit(MD_CHANGE_DEVS, &rs->md.flags);
1255 if (!rs->bitmap_loaded) {
1256 bitmap_load(&rs->md);
1257 rs->bitmap_loaded = 1;
1260 clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
1261 mddev_resume(&rs->md);
1264 static struct target_type raid_target = {
1266 .version = {1, 2, 0},
1267 .module = THIS_MODULE,
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,
1279 static int __init dm_raid_init(void)
1281 return dm_register_target(&raid_target);
1284 static void __exit dm_raid_exit(void)
1286 dm_unregister_target(&raid_target);
1289 module_init(dm_raid_init);
1290 module_exit(dm_raid_exit);
1292 MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
1293 MODULE_ALIAS("dm-raid4");
1294 MODULE_ALIAS("dm-raid5");
1295 MODULE_ALIAS("dm-raid6");
1296 MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
1297 MODULE_LICENSE("GPL");