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>
17 #include <linux/device-mapper.h>
19 #define DM_MSG_PREFIX "raid"
22 * The following flags are used by dm-raid.c to set up the array state.
23 * They must be cleared before md_run is called.
25 #define FirstUse 10 /* rdev flag */
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
34 * While it is possible for this device to be associated
35 * with a different physical device than the data_dev, it
36 * is intended for it to be the same.
37 * |--------- Physical Device ---------|
38 * |- meta_dev -|------ data_dev ------|
40 struct dm_dev
*meta_dev
;
41 struct dm_dev
*data_dev
;
46 * Flags for rs->print_flags field.
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
63 uint32_t bitmap_loaded
;
67 struct raid_type
*raid_type
;
68 struct dm_target_callbacks callbacks
;
70 struct raid_dev dev
[0];
73 /* Supported raid types and properties. */
74 static struct raid_type
{
75 const char *name
; /* RAID algorithm. */
76 const char *descr
; /* Descriptor text for logging. */
77 const unsigned parity_devs
; /* # of parity devices. */
78 const unsigned minimal_devs
; /* minimal # of devices in set. */
79 const unsigned level
; /* RAID level. */
80 const unsigned algorithm
; /* RAID algorithm. */
82 {"raid1", "RAID1 (mirroring)", 0, 2, 1, 0 /* NONE */},
83 {"raid10", "RAID10 (striped mirrors)", 0, 2, 10, UINT_MAX
/* Varies */},
84 {"raid4", "RAID4 (dedicated parity disk)", 1, 2, 5, ALGORITHM_PARITY_0
},
85 {"raid5_la", "RAID5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC
},
86 {"raid5_ra", "RAID5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC
},
87 {"raid5_ls", "RAID5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC
},
88 {"raid5_rs", "RAID5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC
},
89 {"raid6_zr", "RAID6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART
},
90 {"raid6_nr", "RAID6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART
},
91 {"raid6_nc", "RAID6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE
}
94 static char *raid10_md_layout_to_format(int layout
)
97 * Bit 16 and 17 stand for "offset" and "use_far_sets"
98 * Refer to MD's raid10.c for details
100 if ((layout
& 0x10000) && (layout
& 0x20000))
103 if ((layout
& 0xFF) > 1)
109 static unsigned raid10_md_layout_to_copies(int layout
)
111 if ((layout
& 0xFF) > 1)
112 return layout
& 0xFF;
113 return (layout
>> 8) & 0xFF;
116 static int raid10_format_to_md_layout(char *format
, unsigned copies
)
118 unsigned n
= 1, f
= 1;
120 if (!strcmp("near", format
))
125 if (!strcmp("offset", format
))
126 return 0x30000 | (f
<< 8) | n
;
128 if (!strcmp("far", format
))
129 return 0x20000 | (f
<< 8) | n
;
134 static struct raid_type
*get_raid_type(char *name
)
138 for (i
= 0; i
< ARRAY_SIZE(raid_types
); i
++)
139 if (!strcmp(raid_types
[i
].name
, name
))
140 return &raid_types
[i
];
145 static struct raid_set
*context_alloc(struct dm_target
*ti
, struct raid_type
*raid_type
, unsigned raid_devs
)
150 if (raid_devs
<= raid_type
->parity_devs
) {
151 ti
->error
= "Insufficient number of devices";
152 return ERR_PTR(-EINVAL
);
155 rs
= kzalloc(sizeof(*rs
) + raid_devs
* sizeof(rs
->dev
[0]), GFP_KERNEL
);
157 ti
->error
= "Cannot allocate raid context";
158 return ERR_PTR(-ENOMEM
);
164 rs
->raid_type
= raid_type
;
165 rs
->md
.raid_disks
= raid_devs
;
166 rs
->md
.level
= raid_type
->level
;
167 rs
->md
.new_level
= rs
->md
.level
;
168 rs
->md
.layout
= raid_type
->algorithm
;
169 rs
->md
.new_layout
= rs
->md
.layout
;
170 rs
->md
.delta_disks
= 0;
171 rs
->md
.recovery_cp
= 0;
173 for (i
= 0; i
< raid_devs
; i
++)
174 md_rdev_init(&rs
->dev
[i
].rdev
);
177 * Remaining items to be initialized by further RAID params:
180 * rs->md.chunk_sectors
181 * rs->md.new_chunk_sectors
188 static void context_free(struct raid_set
*rs
)
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
);
204 * For every device we have two words
205 * <meta_dev>: meta device name or '-' if missing
206 * <data_dev>: data device name or '-' if missing
208 * The following are permitted:
211 * <meta_dev> <data_dev>
213 * The following is not allowed:
216 * This code parses those words. If there is a failure,
217 * the caller must use context_free to unwind the operations.
219 static int dev_parms(struct raid_set
*rs
, char **argv
)
223 int metadata_available
= 0;
226 for (i
= 0; i
< rs
->md
.raid_disks
; i
++, argv
+= 2) {
227 rs
->dev
[i
].rdev
.raid_disk
= i
;
229 rs
->dev
[i
].meta_dev
= NULL
;
230 rs
->dev
[i
].data_dev
= NULL
;
233 * There are no offsets, since there is a separate device
234 * for data and metadata.
236 rs
->dev
[i
].rdev
.data_offset
= 0;
237 rs
->dev
[i
].rdev
.mddev
= &rs
->md
;
239 if (strcmp(argv
[0], "-")) {
240 ret
= dm_get_device(rs
->ti
, argv
[0],
241 dm_table_get_mode(rs
->ti
->table
),
242 &rs
->dev
[i
].meta_dev
);
243 rs
->ti
->error
= "RAID metadata device lookup failure";
247 rs
->dev
[i
].rdev
.sb_page
= alloc_page(GFP_KERNEL
);
248 if (!rs
->dev
[i
].rdev
.sb_page
)
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";
259 rs
->ti
->error
= "No data device supplied with metadata device";
260 if (rs
->dev
[i
].meta_dev
)
266 ret
= dm_get_device(rs
->ti
, argv
[1],
267 dm_table_get_mode(rs
->ti
->table
),
268 &rs
->dev
[i
].data_dev
);
270 rs
->ti
->error
= "RAID device lookup failure";
274 if (rs
->dev
[i
].meta_dev
) {
275 metadata_available
= 1;
276 rs
->dev
[i
].rdev
.meta_bdev
= rs
->dev
[i
].meta_dev
->bdev
;
278 rs
->dev
[i
].rdev
.bdev
= rs
->dev
[i
].data_dev
->bdev
;
279 list_add(&rs
->dev
[i
].rdev
.same_set
, &rs
->md
.disks
);
280 if (!test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
284 if (metadata_available
) {
286 rs
->md
.persistent
= 1;
287 rs
->md
.major_version
= 2;
288 } else if (rebuild
&& !rs
->md
.recovery_cp
) {
290 * Without metadata, we will not be able to tell if the array
291 * is in-sync or not - we must assume it is not. Therefore,
292 * it is impossible to rebuild a drive.
294 * Even if there is metadata, the on-disk information may
295 * indicate that the array is not in-sync and it will then
298 * User could specify 'nosync' option if desperate.
300 DMERR("Unable to rebuild drive while array is not in-sync");
301 rs
->ti
->error
= "RAID device lookup failure";
309 * validate_region_size
311 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
313 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
314 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
316 * Returns: 0 on success, -EINVAL on failure.
318 static int validate_region_size(struct raid_set
*rs
, unsigned long region_size
)
320 unsigned long min_region_size
= rs
->ti
->len
/ (1 << 21);
324 * Choose a reasonable default. All figures in sectors.
326 if (min_region_size
> (1 << 13)) {
327 /* If not a power of 2, make it the next power of 2 */
328 if (min_region_size
& (min_region_size
- 1))
329 region_size
= 1 << fls(region_size
);
330 DMINFO("Choosing default region size of %lu sectors",
333 DMINFO("Choosing default region size of 4MiB");
334 region_size
= 1 << 13; /* sectors */
338 * Validate user-supplied value.
340 if (region_size
> rs
->ti
->len
) {
341 rs
->ti
->error
= "Supplied region size is too large";
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";
352 if (!is_power_of_2(region_size
)) {
353 rs
->ti
->error
= "Region size is not a power of 2";
357 if (region_size
< rs
->md
.chunk_sectors
) {
358 rs
->ti
->error
= "Region size is smaller than the chunk size";
364 * Convert sectors to bytes.
366 rs
->md
.bitmap_info
.chunksize
= (region_size
<< 9);
372 * validate_raid_redundancy
375 * Determine if there are enough devices in the array that haven't
376 * failed (or are being rebuilt) to form a usable array.
378 * Returns: 0 on success, -EINVAL on failure.
380 static int validate_raid_redundancy(struct raid_set
*rs
)
382 unsigned i
, rebuild_cnt
= 0;
383 unsigned rebuilds_per_group
= 0, copies
, d
;
384 unsigned group_size
, last_group_start
;
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
)
391 switch (rs
->raid_type
->level
) {
393 if (rebuild_cnt
>= rs
->md
.raid_disks
)
399 if (rebuild_cnt
> rs
->raid_type
->parity_devs
)
403 copies
= raid10_md_layout_to_copies(rs
->md
.layout
);
404 if (rebuild_cnt
< copies
)
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).
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
421 if (!strcmp("near", raid10_md_layout_to_format(rs
->md
.layout
))) {
422 for (i
= 0; i
< rs
->md
.raid_disks
* copies
; i
++) {
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
))
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.)
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)
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
))
470 * Possible arguments are...
471 * <chunk_size> [optional_args]
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
478 * [rebuild <idx>] Rebuild the drive indicated by the index
479 * [daemon_sleep <ms>] Time between bitmap daemon work to
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
488 * RAID10-only options:
489 * [raid10_copies <# copies>] Number of copies. (Default: 2)
490 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
492 static int parse_raid_params(struct raid_set
*rs
, char **argv
,
493 unsigned num_raid_params
)
495 char *raid10_format
= "near";
496 unsigned raid10_copies
= 2;
498 unsigned long value
, region_size
= 0;
499 sector_t sectors_per_dev
= rs
->ti
->len
;
504 * First, parse the in-order required arguments
505 * "chunk_size" is the only argument of this type.
507 if ((kstrtoul(argv
[0], 10, &value
) < 0)) {
508 rs
->ti
->error
= "Bad chunk size";
510 } else if (rs
->raid_type
->level
== 1) {
512 DMERR("Ignoring chunk size parameter for RAID 1");
514 } else if (!is_power_of_2(value
)) {
515 rs
->ti
->error
= "Chunk size must be a power of 2";
517 } else if (value
< 8) {
518 rs
->ti
->error
= "Chunk size value is too small";
522 rs
->md
.new_chunk_sectors
= rs
->md
.chunk_sectors
= value
;
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:
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.
540 * What is found in the superblocks of the devices is always
541 * authoritative, unless 'rebuild' or '[no]sync' was specified.
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
;
549 * Second, parse the unordered optional arguments
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
;
557 if (!strcasecmp(argv
[i
], "sync")) {
558 rs
->md
.recovery_cp
= 0;
559 rs
->print_flags
|= DMPF_SYNC
;
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";
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";
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";
583 raid10_format
= argv
[i
];
584 rs
->print_flags
|= DMPF_RAID10_FORMAT
;
588 if (kstrtoul(argv
[i
], 10, &value
) < 0) {
589 rs
->ti
->error
= "Bad numerical argument given in raid params";
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";
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";
607 if (value
>= rs
->md
.raid_disks
) {
608 rs
->ti
->error
= "Invalid write_mostly drive index given";
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";
617 rs
->print_flags
|= DMPF_MAX_WRITE_BEHIND
;
620 * In device-mapper, we specify things in sectors, but
621 * MD records this value in kB
624 if (value
> COUNTER_MAX
) {
625 rs
->ti
->error
= "Max write-behind limit out of range";
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";
635 rs
->md
.bitmap_info
.daemon_sleep
= value
;
636 } else if (!strcasecmp(key
, "stripe_cache")) {
637 rs
->print_flags
|= DMPF_STRIPE_CACHE
;
640 * In device-mapper, we specify things in sectors, but
641 * MD records this value in kB
645 if ((rs
->raid_type
->level
!= 5) &&
646 (rs
->raid_type
->level
!= 6)) {
647 rs
->ti
->error
= "Inappropriate argument: stripe_cache";
650 if (raid5_set_cache_size(&rs
->md
, (int)value
)) {
651 rs
->ti
->error
= "Bad stripe_cache size";
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";
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";
667 rs
->md
.sync_speed_max
= (int)value
;
668 } else if (!strcasecmp(key
, "region_size")) {
669 rs
->print_flags
|= DMPF_REGION_SIZE
;
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'";
677 rs
->print_flags
|= DMPF_RAID10_COPIES
;
678 raid10_copies
= value
;
680 DMERR("Unable to parse RAID parameter: %s", key
);
681 rs
->ti
->error
= "Unable to parse RAID parameters";
686 if (validate_region_size(rs
, region_size
))
689 if (rs
->md
.chunk_sectors
)
690 max_io_len
= rs
->md
.chunk_sectors
;
692 max_io_len
= region_size
;
694 if (dm_set_target_max_io_len(rs
->ti
, max_io_len
))
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";
704 * If the format is not "near", we only support
705 * two copies at the moment.
707 if (strcmp("near", raid10_format
) && (raid10_copies
> 2)) {
708 rs
->ti
->error
= "Too many copies for given RAID10 format.";
712 /* (Len * #mirrors) / #devices */
713 sectors_per_dev
= rs
->ti
->len
* raid10_copies
;
714 sector_div(sectors_per_dev
, rs
->md
.raid_disks
);
716 rs
->md
.layout
= raid10_format_to_md_layout(raid10_format
,
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";
725 rs
->md
.dev_sectors
= sectors_per_dev
;
727 /* Assume there are no metadata devices until the drives are parsed */
728 rs
->md
.persistent
= 0;
734 static void do_table_event(struct work_struct
*ws
)
736 struct raid_set
*rs
= container_of(ws
, struct raid_set
, md
.event_work
);
738 dm_table_event(rs
->ti
->table
);
741 static int raid_is_congested(struct dm_target_callbacks
*cb
, int bits
)
743 struct raid_set
*rs
= container_of(cb
, struct raid_set
, callbacks
);
745 if (rs
->raid_type
->level
== 1)
746 return md_raid1_congested(&rs
->md
, bits
);
748 if (rs
->raid_type
->level
== 10)
749 return md_raid10_congested(&rs
->md
, bits
);
751 return md_raid5_congested(&rs
->md
, bits
);
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.
758 #define DM_RAID_MAGIC 0x64526D44
759 struct dm_raid_superblock
{
760 __le32 magic
; /* "DmRd" */
761 __le32 features
; /* Used to indicate possible future changes */
763 __le32 num_devices
; /* Number of devices in this array. (Max 64) */
764 __le32 array_position
; /* The position of this drive in the array */
766 __le64 events
; /* Incremented by md when superblock updated */
767 __le64 failed_devices
; /* Bit field of devices to indicate failures */
770 * This offset tracks the progress of the repair or replacement of
771 * an individual drive.
773 __le64 disk_recovery_offset
;
776 * This offset tracks the progress of the initial array
777 * synchronisation/parity calculation.
779 __le64 array_resync_offset
;
782 * RAID characteristics
786 __le32 stripe_sectors
;
788 /* Remainder of a logical block is zero-filled when writing (see super_sync()). */
791 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
793 BUG_ON(!rdev
->sb_page
);
798 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, 1)) {
799 DMERR("Failed to read superblock of device at position %d",
801 md_error(rdev
->mddev
, rdev
);
810 static void super_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
813 uint64_t failed_devices
;
814 struct dm_raid_superblock
*sb
;
815 struct raid_set
*rs
= container_of(mddev
, struct raid_set
, md
);
817 sb
= page_address(rdev
->sb_page
);
818 failed_devices
= le64_to_cpu(sb
->failed_devices
);
820 for (i
= 0; i
< mddev
->raid_disks
; i
++)
821 if (!rs
->dev
[i
].data_dev
||
822 test_bit(Faulty
, &(rs
->dev
[i
].rdev
.flags
)))
823 failed_devices
|= (1ULL << i
);
825 memset(sb
+ 1, 0, rdev
->sb_size
- sizeof(*sb
));
827 sb
->magic
= cpu_to_le32(DM_RAID_MAGIC
);
828 sb
->features
= cpu_to_le32(0); /* No features yet */
830 sb
->num_devices
= cpu_to_le32(mddev
->raid_disks
);
831 sb
->array_position
= cpu_to_le32(rdev
->raid_disk
);
833 sb
->events
= cpu_to_le64(mddev
->events
);
834 sb
->failed_devices
= cpu_to_le64(failed_devices
);
836 sb
->disk_recovery_offset
= cpu_to_le64(rdev
->recovery_offset
);
837 sb
->array_resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
839 sb
->level
= cpu_to_le32(mddev
->level
);
840 sb
->layout
= cpu_to_le32(mddev
->layout
);
841 sb
->stripe_sectors
= cpu_to_le32(mddev
->chunk_sectors
);
847 * This function creates a superblock if one is not found on the device
848 * and will decide which superblock to use if there's a choice.
850 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
852 static int super_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
)
855 struct dm_raid_superblock
*sb
;
856 struct dm_raid_superblock
*refsb
;
857 uint64_t events_sb
, events_refsb
;
860 rdev
->sb_size
= bdev_logical_block_size(rdev
->meta_bdev
);
861 if (rdev
->sb_size
< sizeof(*sb
) || rdev
->sb_size
> PAGE_SIZE
) {
862 DMERR("superblock size of a logical block is no longer valid");
866 ret
= read_disk_sb(rdev
, rdev
->sb_size
);
870 sb
= page_address(rdev
->sb_page
);
873 * Two cases that we want to write new superblocks and rebuild:
874 * 1) New device (no matching magic number)
875 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
877 if ((sb
->magic
!= cpu_to_le32(DM_RAID_MAGIC
)) ||
878 (!test_bit(In_sync
, &rdev
->flags
) && !rdev
->recovery_offset
)) {
879 super_sync(rdev
->mddev
, rdev
);
881 set_bit(FirstUse
, &rdev
->flags
);
883 /* Force writing of superblocks to disk */
884 set_bit(MD_CHANGE_DEVS
, &rdev
->mddev
->flags
);
886 /* Any superblock is better than none, choose that if given */
887 return refdev
? 0 : 1;
893 events_sb
= le64_to_cpu(sb
->events
);
895 refsb
= page_address(refdev
->sb_page
);
896 events_refsb
= le64_to_cpu(refsb
->events
);
898 return (events_sb
> events_refsb
) ? 1 : 0;
901 static int super_init_validation(struct mddev
*mddev
, struct md_rdev
*rdev
)
904 struct raid_set
*rs
= container_of(mddev
, struct raid_set
, md
);
906 uint64_t failed_devices
;
907 struct dm_raid_superblock
*sb
;
908 uint32_t new_devs
= 0;
909 uint32_t rebuilds
= 0;
911 struct dm_raid_superblock
*sb2
;
913 sb
= page_address(rdev
->sb_page
);
914 events_sb
= le64_to_cpu(sb
->events
);
915 failed_devices
= le64_to_cpu(sb
->failed_devices
);
918 * Initialise to 1 if this is a new superblock.
920 mddev
->events
= events_sb
? : 1;
923 * Reshaping is not currently allowed
925 if (le32_to_cpu(sb
->level
) != mddev
->level
) {
926 DMERR("Reshaping arrays not yet supported. (RAID level change)");
929 if (le32_to_cpu(sb
->layout
) != mddev
->layout
) {
930 DMERR("Reshaping arrays not yet supported. (RAID layout change)");
931 DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb
->layout
), mddev
->layout
);
932 DMERR(" Old layout: %s w/ %d copies",
933 raid10_md_layout_to_format(le32_to_cpu(sb
->layout
)),
934 raid10_md_layout_to_copies(le32_to_cpu(sb
->layout
)));
935 DMERR(" New layout: %s w/ %d copies",
936 raid10_md_layout_to_format(mddev
->layout
),
937 raid10_md_layout_to_copies(mddev
->layout
));
940 if (le32_to_cpu(sb
->stripe_sectors
) != mddev
->chunk_sectors
) {
941 DMERR("Reshaping arrays not yet supported. (stripe sectors change)");
945 /* We can only change the number of devices in RAID1 right now */
946 if ((rs
->raid_type
->level
!= 1) &&
947 (le32_to_cpu(sb
->num_devices
) != mddev
->raid_disks
)) {
948 DMERR("Reshaping arrays not yet supported. (device count change)");
952 if (!(rs
->print_flags
& (DMPF_SYNC
| DMPF_NOSYNC
)))
953 mddev
->recovery_cp
= le64_to_cpu(sb
->array_resync_offset
);
956 * During load, we set FirstUse if a new superblock was written.
957 * There are two reasons we might not have a superblock:
958 * 1) The array is brand new - in which case, all of the
959 * devices must have their In_sync bit set. Also,
960 * recovery_cp must be 0, unless forced.
961 * 2) This is a new device being added to an old array
962 * and the new device needs to be rebuilt - in which
963 * case the In_sync bit will /not/ be set and
964 * recovery_cp must be MaxSector.
966 rdev_for_each(r
, mddev
) {
967 if (!test_bit(In_sync
, &r
->flags
)) {
968 DMINFO("Device %d specified for rebuild: "
969 "Clearing superblock", r
->raid_disk
);
971 } else if (test_bit(FirstUse
, &r
->flags
))
976 if (new_devs
== mddev
->raid_disks
) {
977 DMINFO("Superblocks created for new array");
978 set_bit(MD_ARRAY_FIRST_USE
, &mddev
->flags
);
979 } else if (new_devs
) {
980 DMERR("New device injected "
981 "into existing array without 'rebuild' "
982 "parameter specified");
985 } else if (new_devs
) {
986 DMERR("'rebuild' devices cannot be "
987 "injected into an array with other first-time devices");
989 } else if (mddev
->recovery_cp
!= MaxSector
) {
990 DMERR("'rebuild' specified while array is not in-sync");
995 * Now we set the Faulty bit for those devices that are
996 * recorded in the superblock as failed.
998 rdev_for_each(r
, mddev
) {
1001 sb2
= page_address(r
->sb_page
);
1002 sb2
->failed_devices
= 0;
1005 * Check for any device re-ordering.
1007 if (!test_bit(FirstUse
, &r
->flags
) && (r
->raid_disk
>= 0)) {
1008 role
= le32_to_cpu(sb2
->array_position
);
1009 if (role
!= r
->raid_disk
) {
1010 if (rs
->raid_type
->level
!= 1) {
1011 rs
->ti
->error
= "Cannot change device "
1012 "positions in RAID array";
1015 DMINFO("RAID1 device #%d now at position #%d",
1016 role
, r
->raid_disk
);
1020 * Partial recovery is performed on
1021 * returning failed devices.
1023 if (failed_devices
& (1 << role
))
1024 set_bit(Faulty
, &r
->flags
);
1031 static int super_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
1033 struct dm_raid_superblock
*sb
= page_address(rdev
->sb_page
);
1036 * If mddev->events is not set, we know we have not yet initialized
1039 if (!mddev
->events
&& super_init_validation(mddev
, rdev
))
1042 mddev
->bitmap_info
.offset
= 4096 >> 9; /* Enable bitmap creation */
1043 rdev
->mddev
->bitmap_info
.default_offset
= 4096 >> 9;
1044 if (!test_bit(FirstUse
, &rdev
->flags
)) {
1045 rdev
->recovery_offset
= le64_to_cpu(sb
->disk_recovery_offset
);
1046 if (rdev
->recovery_offset
!= MaxSector
)
1047 clear_bit(In_sync
, &rdev
->flags
);
1051 * If a device comes back, set it as not In_sync and no longer faulty.
1053 if (test_bit(Faulty
, &rdev
->flags
)) {
1054 clear_bit(Faulty
, &rdev
->flags
);
1055 clear_bit(In_sync
, &rdev
->flags
);
1056 rdev
->saved_raid_disk
= rdev
->raid_disk
;
1057 rdev
->recovery_offset
= 0;
1060 clear_bit(FirstUse
, &rdev
->flags
);
1066 * Analyse superblocks and select the freshest.
1068 static int analyse_superblocks(struct dm_target
*ti
, struct raid_set
*rs
)
1071 struct raid_dev
*dev
;
1072 struct md_rdev
*rdev
, *tmp
, *freshest
;
1073 struct mddev
*mddev
= &rs
->md
;
1076 rdev_for_each_safe(rdev
, tmp
, mddev
) {
1078 * Skipping super_load due to DMPF_SYNC will cause
1079 * the array to undergo initialization again as
1080 * though it were new. This is the intended effect
1081 * of the "sync" directive.
1083 * When reshaping capability is added, we must ensure
1084 * that the "sync" directive is disallowed during the
1087 if (rs
->print_flags
& DMPF_SYNC
)
1090 if (!rdev
->meta_bdev
)
1093 ret
= super_load(rdev
, freshest
);
1102 dev
= container_of(rdev
, struct raid_dev
, rdev
);
1104 dm_put_device(ti
, dev
->meta_dev
);
1106 dev
->meta_dev
= NULL
;
1107 rdev
->meta_bdev
= NULL
;
1110 put_page(rdev
->sb_page
);
1112 rdev
->sb_page
= NULL
;
1114 rdev
->sb_loaded
= 0;
1117 * We might be able to salvage the data device
1118 * even though the meta device has failed. For
1119 * now, we behave as though '- -' had been
1120 * set for this device in the table.
1123 dm_put_device(ti
, dev
->data_dev
);
1125 dev
->data_dev
= NULL
;
1128 list_del(&rdev
->same_set
);
1135 if (validate_raid_redundancy(rs
)) {
1136 rs
->ti
->error
= "Insufficient redundancy to activate array";
1141 * Validation of the freshest device provides the source of
1142 * validation for the remaining devices.
1144 ti
->error
= "Unable to assemble array: Invalid superblocks";
1145 if (super_validate(mddev
, freshest
))
1148 rdev_for_each(rdev
, mddev
)
1149 if ((rdev
!= freshest
) && super_validate(mddev
, rdev
))
1156 * Construct a RAID4/5/6 mapping:
1158 * <raid_type> <#raid_params> <raid_params> \
1159 * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
1161 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
1162 * details on possible <raid_params>.
1164 static int raid_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
1167 struct raid_type
*rt
;
1168 unsigned long num_raid_params
, num_raid_devs
;
1169 struct raid_set
*rs
= NULL
;
1171 /* Must have at least <raid_type> <#raid_params> */
1173 ti
->error
= "Too few arguments";
1178 rt
= get_raid_type(argv
[0]);
1180 ti
->error
= "Unrecognised raid_type";
1186 /* number of RAID parameters */
1187 if (kstrtoul(argv
[0], 10, &num_raid_params
) < 0) {
1188 ti
->error
= "Cannot understand number of RAID parameters";
1194 /* Skip over RAID params for now and find out # of devices */
1195 if (num_raid_params
+ 1 > argc
) {
1196 ti
->error
= "Arguments do not agree with counts given";
1200 if ((kstrtoul(argv
[num_raid_params
], 10, &num_raid_devs
) < 0) ||
1201 (num_raid_devs
>= INT_MAX
)) {
1202 ti
->error
= "Cannot understand number of raid devices";
1206 rs
= context_alloc(ti
, rt
, (unsigned)num_raid_devs
);
1210 ret
= parse_raid_params(rs
, argv
, (unsigned)num_raid_params
);
1216 argc
-= num_raid_params
+ 1; /* +1: we already have num_raid_devs */
1217 argv
+= num_raid_params
+ 1;
1219 if (argc
!= (num_raid_devs
* 2)) {
1220 ti
->error
= "Supplied RAID devices does not match the count given";
1224 ret
= dev_parms(rs
, argv
);
1228 rs
->md
.sync_super
= super_sync
;
1229 ret
= analyse_superblocks(ti
, rs
);
1233 INIT_WORK(&rs
->md
.event_work
, do_table_event
);
1235 ti
->num_flush_bios
= 1;
1237 mutex_lock(&rs
->md
.reconfig_mutex
);
1238 ret
= md_run(&rs
->md
);
1239 rs
->md
.in_sync
= 0; /* Assume already marked dirty */
1240 mutex_unlock(&rs
->md
.reconfig_mutex
);
1243 ti
->error
= "Fail to run raid array";
1247 if (ti
->len
!= rs
->md
.array_sectors
) {
1248 ti
->error
= "Array size does not match requested target length";
1252 rs
->callbacks
.congested_fn
= raid_is_congested
;
1253 dm_table_add_target_callbacks(ti
->table
, &rs
->callbacks
);
1255 mddev_suspend(&rs
->md
);
1266 static void raid_dtr(struct dm_target
*ti
)
1268 struct raid_set
*rs
= ti
->private;
1270 list_del_init(&rs
->callbacks
.list
);
1275 static int raid_map(struct dm_target
*ti
, struct bio
*bio
)
1277 struct raid_set
*rs
= ti
->private;
1278 struct mddev
*mddev
= &rs
->md
;
1280 mddev
->pers
->make_request(mddev
, bio
);
1282 return DM_MAPIO_SUBMITTED
;
1285 static const char *decipher_sync_action(struct mddev
*mddev
)
1287 if (test_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
))
1290 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
1291 (!mddev
->ro
&& test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))) {
1292 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
1295 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1296 if (!test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
1298 else if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
1303 if (test_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
))
1310 static void raid_status(struct dm_target
*ti
, status_type_t type
,
1311 unsigned status_flags
, char *result
, unsigned maxlen
)
1313 struct raid_set
*rs
= ti
->private;
1314 unsigned raid_param_cnt
= 1; /* at least 1 for chunksize */
1316 int i
, array_in_sync
= 0;
1320 case STATUSTYPE_INFO
:
1321 DMEMIT("%s %d ", rs
->raid_type
->name
, rs
->md
.raid_disks
);
1323 if (test_bit(MD_RECOVERY_RUNNING
, &rs
->md
.recovery
))
1324 sync
= rs
->md
.curr_resync_completed
;
1326 sync
= rs
->md
.recovery_cp
;
1328 if (sync
>= rs
->md
.resync_max_sectors
) {
1333 sync
= rs
->md
.resync_max_sectors
;
1334 } else if (test_bit(MD_RECOVERY_REQUESTED
, &rs
->md
.recovery
)) {
1336 * If "check" or "repair" is occurring, the array has
1337 * undergone and initial sync and the health characters
1338 * should not be 'a' anymore.
1343 * The array may be doing an initial sync, or it may
1344 * be rebuilding individual components. If all the
1345 * devices are In_sync, then it is the array that is
1346 * being initialized.
1348 for (i
= 0; i
< rs
->md
.raid_disks
; i
++)
1349 if (!test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
1354 * Status characters:
1355 * 'D' = Dead/Failed device
1356 * 'a' = Alive but not in-sync
1357 * 'A' = Alive and in-sync
1359 for (i
= 0; i
< rs
->md
.raid_disks
; i
++) {
1360 if (test_bit(Faulty
, &rs
->dev
[i
].rdev
.flags
))
1362 else if (!array_in_sync
||
1363 !test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
1371 * The in-sync ratio shows the progress of:
1372 * - Initializing the array
1373 * - Rebuilding a subset of devices of the array
1374 * The user can distinguish between the two by referring
1375 * to the status characters.
1377 DMEMIT(" %llu/%llu",
1378 (unsigned long long) sync
,
1379 (unsigned long long) rs
->md
.resync_max_sectors
);
1383 * See Documentation/device-mapper/dm-raid.c for
1384 * information on each of these states.
1386 DMEMIT(" %s", decipher_sync_action(&rs
->md
));
1389 * resync_mismatches/mismatch_cnt
1390 * This field shows the number of discrepancies found when
1391 * performing a "check" of the array.
1394 (strcmp(rs
->md
.last_sync_action
, "check")) ? 0 :
1395 (unsigned long long)
1396 atomic64_read(&rs
->md
.resync_mismatches
));
1398 case STATUSTYPE_TABLE
:
1399 /* The string you would use to construct this array */
1400 for (i
= 0; i
< rs
->md
.raid_disks
; i
++) {
1401 if ((rs
->print_flags
& DMPF_REBUILD
) &&
1402 rs
->dev
[i
].data_dev
&&
1403 !test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
1404 raid_param_cnt
+= 2; /* for rebuilds */
1405 if (rs
->dev
[i
].data_dev
&&
1406 test_bit(WriteMostly
, &rs
->dev
[i
].rdev
.flags
))
1407 raid_param_cnt
+= 2;
1410 raid_param_cnt
+= (hweight32(rs
->print_flags
& ~DMPF_REBUILD
) * 2);
1411 if (rs
->print_flags
& (DMPF_SYNC
| DMPF_NOSYNC
))
1414 DMEMIT("%s %u %u", rs
->raid_type
->name
,
1415 raid_param_cnt
, rs
->md
.chunk_sectors
);
1417 if ((rs
->print_flags
& DMPF_SYNC
) &&
1418 (rs
->md
.recovery_cp
== MaxSector
))
1420 if (rs
->print_flags
& DMPF_NOSYNC
)
1423 for (i
= 0; i
< rs
->md
.raid_disks
; i
++)
1424 if ((rs
->print_flags
& DMPF_REBUILD
) &&
1425 rs
->dev
[i
].data_dev
&&
1426 !test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
1427 DMEMIT(" rebuild %u", i
);
1429 if (rs
->print_flags
& DMPF_DAEMON_SLEEP
)
1430 DMEMIT(" daemon_sleep %lu",
1431 rs
->md
.bitmap_info
.daemon_sleep
);
1433 if (rs
->print_flags
& DMPF_MIN_RECOVERY_RATE
)
1434 DMEMIT(" min_recovery_rate %d", rs
->md
.sync_speed_min
);
1436 if (rs
->print_flags
& DMPF_MAX_RECOVERY_RATE
)
1437 DMEMIT(" max_recovery_rate %d", rs
->md
.sync_speed_max
);
1439 for (i
= 0; i
< rs
->md
.raid_disks
; i
++)
1440 if (rs
->dev
[i
].data_dev
&&
1441 test_bit(WriteMostly
, &rs
->dev
[i
].rdev
.flags
))
1442 DMEMIT(" write_mostly %u", i
);
1444 if (rs
->print_flags
& DMPF_MAX_WRITE_BEHIND
)
1445 DMEMIT(" max_write_behind %lu",
1446 rs
->md
.bitmap_info
.max_write_behind
);
1448 if (rs
->print_flags
& DMPF_STRIPE_CACHE
) {
1449 struct r5conf
*conf
= rs
->md
.private;
1451 /* convert from kiB to sectors */
1452 DMEMIT(" stripe_cache %d",
1453 conf
? conf
->max_nr_stripes
* 2 : 0);
1456 if (rs
->print_flags
& DMPF_REGION_SIZE
)
1457 DMEMIT(" region_size %lu",
1458 rs
->md
.bitmap_info
.chunksize
>> 9);
1460 if (rs
->print_flags
& DMPF_RAID10_COPIES
)
1461 DMEMIT(" raid10_copies %u",
1462 raid10_md_layout_to_copies(rs
->md
.layout
));
1464 if (rs
->print_flags
& DMPF_RAID10_FORMAT
)
1465 DMEMIT(" raid10_format %s",
1466 raid10_md_layout_to_format(rs
->md
.layout
));
1468 DMEMIT(" %d", rs
->md
.raid_disks
);
1469 for (i
= 0; i
< rs
->md
.raid_disks
; i
++) {
1470 if (rs
->dev
[i
].meta_dev
)
1471 DMEMIT(" %s", rs
->dev
[i
].meta_dev
->name
);
1475 if (rs
->dev
[i
].data_dev
)
1476 DMEMIT(" %s", rs
->dev
[i
].data_dev
->name
);
1483 static int raid_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
1485 struct raid_set
*rs
= ti
->private;
1486 struct mddev
*mddev
= &rs
->md
;
1488 if (!strcasecmp(argv
[0], "reshape")) {
1489 DMERR("Reshape not supported.");
1493 if (!mddev
->pers
|| !mddev
->pers
->sync_request
)
1496 if (!strcasecmp(argv
[0], "frozen"))
1497 set_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
1499 clear_bit(MD_RECOVERY_FROZEN
, &mddev
->recovery
);
1501 if (!strcasecmp(argv
[0], "idle") || !strcasecmp(argv
[0], "frozen")) {
1502 if (mddev
->sync_thread
) {
1503 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1504 md_reap_sync_thread(mddev
);
1506 } else if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
) ||
1507 test_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
))
1509 else if (!strcasecmp(argv
[0], "resync"))
1510 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
1511 else if (!strcasecmp(argv
[0], "recover")) {
1512 set_bit(MD_RECOVERY_RECOVER
, &mddev
->recovery
);
1513 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
1515 if (!strcasecmp(argv
[0], "check"))
1516 set_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
1517 else if (!!strcasecmp(argv
[0], "repair"))
1519 set_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
);
1520 set_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
1522 if (mddev
->ro
== 2) {
1523 /* A write to sync_action is enough to justify
1524 * canceling read-auto mode
1527 if (!mddev
->suspended
)
1528 md_wakeup_thread(mddev
->sync_thread
);
1530 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
1531 if (!mddev
->suspended
)
1532 md_wakeup_thread(mddev
->thread
);
1537 static int raid_iterate_devices(struct dm_target
*ti
,
1538 iterate_devices_callout_fn fn
, void *data
)
1540 struct raid_set
*rs
= ti
->private;
1544 for (i
= 0; !ret
&& i
< rs
->md
.raid_disks
; i
++)
1545 if (rs
->dev
[i
].data_dev
)
1547 rs
->dev
[i
].data_dev
,
1548 0, /* No offset on data devs */
1555 static void raid_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
1557 struct raid_set
*rs
= ti
->private;
1558 unsigned chunk_size
= rs
->md
.chunk_sectors
<< 9;
1559 struct r5conf
*conf
= rs
->md
.private;
1561 blk_limits_io_min(limits
, chunk_size
);
1562 blk_limits_io_opt(limits
, chunk_size
* (conf
->raid_disks
- conf
->max_degraded
));
1565 static void raid_presuspend(struct dm_target
*ti
)
1567 struct raid_set
*rs
= ti
->private;
1569 md_stop_writes(&rs
->md
);
1572 static void raid_postsuspend(struct dm_target
*ti
)
1574 struct raid_set
*rs
= ti
->private;
1576 mddev_suspend(&rs
->md
);
1579 static void attempt_restore_of_faulty_devices(struct raid_set
*rs
)
1582 uint64_t failed_devices
, cleared_failed_devices
= 0;
1583 unsigned long flags
;
1584 struct dm_raid_superblock
*sb
;
1587 for (i
= 0; i
< rs
->md
.raid_disks
; i
++) {
1588 r
= &rs
->dev
[i
].rdev
;
1589 if (test_bit(Faulty
, &r
->flags
) && r
->sb_page
&&
1590 sync_page_io(r
, 0, r
->sb_size
, r
->sb_page
, READ
, 1)) {
1591 DMINFO("Faulty %s device #%d has readable super block."
1592 " Attempting to revive it.",
1593 rs
->raid_type
->name
, i
);
1596 * Faulty bit may be set, but sometimes the array can
1597 * be suspended before the personalities can respond
1598 * by removing the device from the array (i.e. calling
1599 * 'hot_remove_disk'). If they haven't yet removed
1600 * the failed device, its 'raid_disk' number will be
1601 * '>= 0' - meaning we must call this function
1604 if ((r
->raid_disk
>= 0) &&
1605 (r
->mddev
->pers
->hot_remove_disk(r
->mddev
, r
) != 0))
1606 /* Failed to revive this device, try next */
1610 r
->saved_raid_disk
= i
;
1612 clear_bit(Faulty
, &r
->flags
);
1613 clear_bit(WriteErrorSeen
, &r
->flags
);
1614 clear_bit(In_sync
, &r
->flags
);
1615 if (r
->mddev
->pers
->hot_add_disk(r
->mddev
, r
)) {
1617 r
->saved_raid_disk
= -1;
1620 r
->recovery_offset
= 0;
1621 cleared_failed_devices
|= 1 << i
;
1625 if (cleared_failed_devices
) {
1626 rdev_for_each(r
, &rs
->md
) {
1627 sb
= page_address(r
->sb_page
);
1628 failed_devices
= le64_to_cpu(sb
->failed_devices
);
1629 failed_devices
&= ~cleared_failed_devices
;
1630 sb
->failed_devices
= cpu_to_le64(failed_devices
);
1635 static void raid_resume(struct dm_target
*ti
)
1637 struct raid_set
*rs
= ti
->private;
1639 set_bit(MD_CHANGE_DEVS
, &rs
->md
.flags
);
1640 if (!rs
->bitmap_loaded
) {
1641 bitmap_load(&rs
->md
);
1642 rs
->bitmap_loaded
= 1;
1645 * A secondary resume while the device is active.
1646 * Take this opportunity to check whether any failed
1647 * devices are reachable again.
1649 attempt_restore_of_faulty_devices(rs
);
1652 clear_bit(MD_RECOVERY_FROZEN
, &rs
->md
.recovery
);
1653 mddev_resume(&rs
->md
);
1656 static struct target_type raid_target
= {
1658 .version
= {1, 5, 2},
1659 .module
= THIS_MODULE
,
1663 .status
= raid_status
,
1664 .message
= raid_message
,
1665 .iterate_devices
= raid_iterate_devices
,
1666 .io_hints
= raid_io_hints
,
1667 .presuspend
= raid_presuspend
,
1668 .postsuspend
= raid_postsuspend
,
1669 .resume
= raid_resume
,
1672 static int __init
dm_raid_init(void)
1674 DMINFO("Loading target version %u.%u.%u",
1675 raid_target
.version
[0],
1676 raid_target
.version
[1],
1677 raid_target
.version
[2]);
1678 return dm_register_target(&raid_target
);
1681 static void __exit
dm_raid_exit(void)
1683 dm_unregister_target(&raid_target
);
1686 module_init(dm_raid_init
);
1687 module_exit(dm_raid_exit
);
1689 MODULE_DESCRIPTION(DM_NAME
" raid4/5/6 target");
1690 MODULE_ALIAS("dm-raid1");
1691 MODULE_ALIAS("dm-raid10");
1692 MODULE_ALIAS("dm-raid4");
1693 MODULE_ALIAS("dm-raid5");
1694 MODULE_ALIAS("dm-raid6");
1695 MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
1696 MODULE_LICENSE("GPL");