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 unsigned raid10_md_layout_to_copies(int layout
)
99 static int raid10_format_to_md_layout(char *format
, unsigned copies
)
101 /* 1 "far" copy, and 'copies' "near" copies */
102 return (1 << 8) | (copies
& 0xFF);
105 static struct raid_type
*get_raid_type(char *name
)
109 for (i
= 0; i
< ARRAY_SIZE(raid_types
); i
++)
110 if (!strcmp(raid_types
[i
].name
, name
))
111 return &raid_types
[i
];
116 static struct raid_set
*context_alloc(struct dm_target
*ti
, struct raid_type
*raid_type
, unsigned raid_devs
)
121 if (raid_devs
<= raid_type
->parity_devs
) {
122 ti
->error
= "Insufficient number of devices";
123 return ERR_PTR(-EINVAL
);
126 rs
= kzalloc(sizeof(*rs
) + raid_devs
* sizeof(rs
->dev
[0]), GFP_KERNEL
);
128 ti
->error
= "Cannot allocate raid context";
129 return ERR_PTR(-ENOMEM
);
135 rs
->raid_type
= raid_type
;
136 rs
->md
.raid_disks
= raid_devs
;
137 rs
->md
.level
= raid_type
->level
;
138 rs
->md
.new_level
= rs
->md
.level
;
139 rs
->md
.layout
= raid_type
->algorithm
;
140 rs
->md
.new_layout
= rs
->md
.layout
;
141 rs
->md
.delta_disks
= 0;
142 rs
->md
.recovery_cp
= 0;
144 for (i
= 0; i
< raid_devs
; i
++)
145 md_rdev_init(&rs
->dev
[i
].rdev
);
148 * Remaining items to be initialized by further RAID params:
151 * rs->md.chunk_sectors
152 * rs->md.new_chunk_sectors
159 static void context_free(struct raid_set
*rs
)
163 for (i
= 0; i
< rs
->md
.raid_disks
; i
++) {
164 if (rs
->dev
[i
].meta_dev
)
165 dm_put_device(rs
->ti
, rs
->dev
[i
].meta_dev
);
166 md_rdev_clear(&rs
->dev
[i
].rdev
);
167 if (rs
->dev
[i
].data_dev
)
168 dm_put_device(rs
->ti
, rs
->dev
[i
].data_dev
);
175 * For every device we have two words
176 * <meta_dev>: meta device name or '-' if missing
177 * <data_dev>: data device name or '-' if missing
179 * The following are permitted:
182 * <meta_dev> <data_dev>
184 * The following is not allowed:
187 * This code parses those words. If there is a failure,
188 * the caller must use context_free to unwind the operations.
190 static int dev_parms(struct raid_set
*rs
, char **argv
)
194 int metadata_available
= 0;
197 for (i
= 0; i
< rs
->md
.raid_disks
; i
++, argv
+= 2) {
198 rs
->dev
[i
].rdev
.raid_disk
= i
;
200 rs
->dev
[i
].meta_dev
= NULL
;
201 rs
->dev
[i
].data_dev
= NULL
;
204 * There are no offsets, since there is a separate device
205 * for data and metadata.
207 rs
->dev
[i
].rdev
.data_offset
= 0;
208 rs
->dev
[i
].rdev
.mddev
= &rs
->md
;
210 if (strcmp(argv
[0], "-")) {
211 ret
= dm_get_device(rs
->ti
, argv
[0],
212 dm_table_get_mode(rs
->ti
->table
),
213 &rs
->dev
[i
].meta_dev
);
214 rs
->ti
->error
= "RAID metadata device lookup failure";
218 rs
->dev
[i
].rdev
.sb_page
= alloc_page(GFP_KERNEL
);
219 if (!rs
->dev
[i
].rdev
.sb_page
)
223 if (!strcmp(argv
[1], "-")) {
224 if (!test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
) &&
225 (!rs
->dev
[i
].rdev
.recovery_offset
)) {
226 rs
->ti
->error
= "Drive designated for rebuild not specified";
230 rs
->ti
->error
= "No data device supplied with metadata device";
231 if (rs
->dev
[i
].meta_dev
)
237 ret
= dm_get_device(rs
->ti
, argv
[1],
238 dm_table_get_mode(rs
->ti
->table
),
239 &rs
->dev
[i
].data_dev
);
241 rs
->ti
->error
= "RAID device lookup failure";
245 if (rs
->dev
[i
].meta_dev
) {
246 metadata_available
= 1;
247 rs
->dev
[i
].rdev
.meta_bdev
= rs
->dev
[i
].meta_dev
->bdev
;
249 rs
->dev
[i
].rdev
.bdev
= rs
->dev
[i
].data_dev
->bdev
;
250 list_add(&rs
->dev
[i
].rdev
.same_set
, &rs
->md
.disks
);
251 if (!test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
255 if (metadata_available
) {
257 rs
->md
.persistent
= 1;
258 rs
->md
.major_version
= 2;
259 } else if (rebuild
&& !rs
->md
.recovery_cp
) {
261 * Without metadata, we will not be able to tell if the array
262 * is in-sync or not - we must assume it is not. Therefore,
263 * it is impossible to rebuild a drive.
265 * Even if there is metadata, the on-disk information may
266 * indicate that the array is not in-sync and it will then
269 * User could specify 'nosync' option if desperate.
271 DMERR("Unable to rebuild drive while array is not in-sync");
272 rs
->ti
->error
= "RAID device lookup failure";
280 * validate_region_size
282 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
284 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
285 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
287 * Returns: 0 on success, -EINVAL on failure.
289 static int validate_region_size(struct raid_set
*rs
, unsigned long region_size
)
291 unsigned long min_region_size
= rs
->ti
->len
/ (1 << 21);
295 * Choose a reasonable default. All figures in sectors.
297 if (min_region_size
> (1 << 13)) {
298 DMINFO("Choosing default region size of %lu sectors",
300 region_size
= min_region_size
;
302 DMINFO("Choosing default region size of 4MiB");
303 region_size
= 1 << 13; /* sectors */
307 * Validate user-supplied value.
309 if (region_size
> rs
->ti
->len
) {
310 rs
->ti
->error
= "Supplied region size is too large";
314 if (region_size
< min_region_size
) {
315 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
316 region_size
, min_region_size
);
317 rs
->ti
->error
= "Supplied region size is too small";
321 if (!is_power_of_2(region_size
)) {
322 rs
->ti
->error
= "Region size is not a power of 2";
326 if (region_size
< rs
->md
.chunk_sectors
) {
327 rs
->ti
->error
= "Region size is smaller than the chunk size";
333 * Convert sectors to bytes.
335 rs
->md
.bitmap_info
.chunksize
= (region_size
<< 9);
341 * validate_rebuild_devices
344 * Determine if the devices specified for rebuild can result in a valid
345 * usable array that is capable of rebuilding the given devices.
347 * Returns: 0 on success, -EINVAL on failure.
349 static int validate_rebuild_devices(struct raid_set
*rs
)
351 unsigned i
, rebuild_cnt
= 0;
352 unsigned rebuilds_per_group
, copies
, d
;
354 if (!(rs
->print_flags
& DMPF_REBUILD
))
357 for (i
= 0; i
< rs
->md
.raid_disks
; i
++)
358 if (!test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
361 switch (rs
->raid_type
->level
) {
363 if (rebuild_cnt
>= rs
->md
.raid_disks
)
369 if (rebuild_cnt
> rs
->raid_type
->parity_devs
)
373 copies
= raid10_md_layout_to_copies(rs
->md
.layout
);
374 if (rebuild_cnt
< copies
)
378 * It is possible to have a higher rebuild count for RAID10,
379 * as long as the failed devices occur in different mirror
380 * groups (i.e. different stripes).
382 * Right now, we only allow for "near" copies. When other
383 * formats are added, we will have to check those too.
385 * When checking "near" format, make sure no adjacent devices
386 * have failed beyond what can be handled. In addition to the
387 * simple case where the number of devices is a multiple of the
388 * number of copies, we must also handle cases where the number
389 * of devices is not a multiple of the number of copies.
390 * E.g. dev1 dev2 dev3 dev4 dev5
394 rebuilds_per_group
= 0;
395 for (i
= 0; i
< rs
->md
.raid_disks
* copies
; i
++) {
396 d
= i
% rs
->md
.raid_disks
;
397 if (!test_bit(In_sync
, &rs
->dev
[d
].rdev
.flags
) &&
398 (++rebuilds_per_group
>= copies
))
400 if (!((i
+ 1) % copies
))
401 rebuilds_per_group
= 0;
405 DMERR("The rebuild parameter is not supported for %s",
406 rs
->raid_type
->name
);
407 rs
->ti
->error
= "Rebuild not supported for this RAID type";
414 rs
->ti
->error
= "Too many rebuild devices specified";
419 * Possible arguments are...
420 * <chunk_size> [optional_args]
422 * Argument definitions
423 * <chunk_size> The number of sectors per disk that
424 * will form the "stripe"
425 * [[no]sync] Force or prevent recovery of the
427 * [rebuild <idx>] Rebuild the drive indicated by the index
428 * [daemon_sleep <ms>] Time between bitmap daemon work to
430 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
431 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
432 * [write_mostly <idx>] Indicate a write mostly drive via index
433 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
434 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
435 * [region_size <sectors>] Defines granularity of bitmap
437 * RAID10-only options:
438 * [raid10_copies <# copies>] Number of copies. (Default: 2)
439 * [raid10_format <near>] Layout algorithm. (Default: near)
441 static int parse_raid_params(struct raid_set
*rs
, char **argv
,
442 unsigned num_raid_params
)
444 char *raid10_format
= "near";
445 unsigned raid10_copies
= 2;
447 unsigned long value
, region_size
= 0;
448 sector_t sectors_per_dev
= rs
->ti
->len
;
453 * First, parse the in-order required arguments
454 * "chunk_size" is the only argument of this type.
456 if ((strict_strtoul(argv
[0], 10, &value
) < 0)) {
457 rs
->ti
->error
= "Bad chunk size";
459 } else if (rs
->raid_type
->level
== 1) {
461 DMERR("Ignoring chunk size parameter for RAID 1");
463 } else if (!is_power_of_2(value
)) {
464 rs
->ti
->error
= "Chunk size must be a power of 2";
466 } else if (value
< 8) {
467 rs
->ti
->error
= "Chunk size value is too small";
471 rs
->md
.new_chunk_sectors
= rs
->md
.chunk_sectors
= value
;
476 * We set each individual device as In_sync with a completed
477 * 'recovery_offset'. If there has been a device failure or
478 * replacement then one of the following cases applies:
480 * 1) User specifies 'rebuild'.
481 * - Device is reset when param is read.
482 * 2) A new device is supplied.
483 * - No matching superblock found, resets device.
484 * 3) Device failure was transient and returns on reload.
485 * - Failure noticed, resets device for bitmap replay.
486 * 4) Device hadn't completed recovery after previous failure.
487 * - Superblock is read and overrides recovery_offset.
489 * What is found in the superblocks of the devices is always
490 * authoritative, unless 'rebuild' or '[no]sync' was specified.
492 for (i
= 0; i
< rs
->md
.raid_disks
; i
++) {
493 set_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
);
494 rs
->dev
[i
].rdev
.recovery_offset
= MaxSector
;
498 * Second, parse the unordered optional arguments
500 for (i
= 0; i
< num_raid_params
; i
++) {
501 if (!strcasecmp(argv
[i
], "nosync")) {
502 rs
->md
.recovery_cp
= MaxSector
;
503 rs
->print_flags
|= DMPF_NOSYNC
;
506 if (!strcasecmp(argv
[i
], "sync")) {
507 rs
->md
.recovery_cp
= 0;
508 rs
->print_flags
|= DMPF_SYNC
;
512 /* The rest of the optional arguments come in key/value pairs */
513 if ((i
+ 1) >= num_raid_params
) {
514 rs
->ti
->error
= "Wrong number of raid parameters given";
520 /* Parameters that take a string value are checked here. */
521 if (!strcasecmp(key
, "raid10_format")) {
522 if (rs
->raid_type
->level
!= 10) {
523 rs
->ti
->error
= "'raid10_format' is an invalid parameter for this RAID type";
526 if (strcmp("near", argv
[i
])) {
527 rs
->ti
->error
= "Invalid 'raid10_format' value given";
530 raid10_format
= argv
[i
];
531 rs
->print_flags
|= DMPF_RAID10_FORMAT
;
535 if (strict_strtoul(argv
[i
], 10, &value
) < 0) {
536 rs
->ti
->error
= "Bad numerical argument given in raid params";
540 /* Parameters that take a numeric value are checked here */
541 if (!strcasecmp(key
, "rebuild")) {
542 if (value
>= rs
->md
.raid_disks
) {
543 rs
->ti
->error
= "Invalid rebuild index given";
546 clear_bit(In_sync
, &rs
->dev
[value
].rdev
.flags
);
547 rs
->dev
[value
].rdev
.recovery_offset
= 0;
548 rs
->print_flags
|= DMPF_REBUILD
;
549 } else if (!strcasecmp(key
, "write_mostly")) {
550 if (rs
->raid_type
->level
!= 1) {
551 rs
->ti
->error
= "write_mostly option is only valid for RAID1";
554 if (value
>= rs
->md
.raid_disks
) {
555 rs
->ti
->error
= "Invalid write_mostly drive index given";
558 set_bit(WriteMostly
, &rs
->dev
[value
].rdev
.flags
);
559 } else if (!strcasecmp(key
, "max_write_behind")) {
560 if (rs
->raid_type
->level
!= 1) {
561 rs
->ti
->error
= "max_write_behind option is only valid for RAID1";
564 rs
->print_flags
|= DMPF_MAX_WRITE_BEHIND
;
567 * In device-mapper, we specify things in sectors, but
568 * MD records this value in kB
571 if (value
> COUNTER_MAX
) {
572 rs
->ti
->error
= "Max write-behind limit out of range";
575 rs
->md
.bitmap_info
.max_write_behind
= value
;
576 } else if (!strcasecmp(key
, "daemon_sleep")) {
577 rs
->print_flags
|= DMPF_DAEMON_SLEEP
;
578 if (!value
|| (value
> MAX_SCHEDULE_TIMEOUT
)) {
579 rs
->ti
->error
= "daemon sleep period out of range";
582 rs
->md
.bitmap_info
.daemon_sleep
= value
;
583 } else if (!strcasecmp(key
, "stripe_cache")) {
584 rs
->print_flags
|= DMPF_STRIPE_CACHE
;
587 * In device-mapper, we specify things in sectors, but
588 * MD records this value in kB
592 if ((rs
->raid_type
->level
!= 5) &&
593 (rs
->raid_type
->level
!= 6)) {
594 rs
->ti
->error
= "Inappropriate argument: stripe_cache";
597 if (raid5_set_cache_size(&rs
->md
, (int)value
)) {
598 rs
->ti
->error
= "Bad stripe_cache size";
601 } else if (!strcasecmp(key
, "min_recovery_rate")) {
602 rs
->print_flags
|= DMPF_MIN_RECOVERY_RATE
;
603 if (value
> INT_MAX
) {
604 rs
->ti
->error
= "min_recovery_rate out of range";
607 rs
->md
.sync_speed_min
= (int)value
;
608 } else if (!strcasecmp(key
, "max_recovery_rate")) {
609 rs
->print_flags
|= DMPF_MAX_RECOVERY_RATE
;
610 if (value
> INT_MAX
) {
611 rs
->ti
->error
= "max_recovery_rate out of range";
614 rs
->md
.sync_speed_max
= (int)value
;
615 } else if (!strcasecmp(key
, "region_size")) {
616 rs
->print_flags
|= DMPF_REGION_SIZE
;
618 } else if (!strcasecmp(key
, "raid10_copies") &&
619 (rs
->raid_type
->level
== 10)) {
620 if ((value
< 2) || (value
> 0xFF)) {
621 rs
->ti
->error
= "Bad value for 'raid10_copies'";
624 rs
->print_flags
|= DMPF_RAID10_COPIES
;
625 raid10_copies
= value
;
627 DMERR("Unable to parse RAID parameter: %s", key
);
628 rs
->ti
->error
= "Unable to parse RAID parameters";
633 if (validate_region_size(rs
, region_size
))
636 if (rs
->md
.chunk_sectors
)
637 max_io_len
= rs
->md
.chunk_sectors
;
639 max_io_len
= region_size
;
641 if (dm_set_target_max_io_len(rs
->ti
, max_io_len
))
644 if (rs
->raid_type
->level
== 10) {
645 if (raid10_copies
> rs
->md
.raid_disks
) {
646 rs
->ti
->error
= "Not enough devices to satisfy specification";
650 /* (Len * #mirrors) / #devices */
651 sectors_per_dev
= rs
->ti
->len
* raid10_copies
;
652 sector_div(sectors_per_dev
, rs
->md
.raid_disks
);
654 rs
->md
.layout
= raid10_format_to_md_layout(raid10_format
,
656 rs
->md
.new_layout
= rs
->md
.layout
;
657 } else if ((rs
->raid_type
->level
> 1) &&
658 sector_div(sectors_per_dev
,
659 (rs
->md
.raid_disks
- rs
->raid_type
->parity_devs
))) {
660 rs
->ti
->error
= "Target length not divisible by number of data devices";
663 rs
->md
.dev_sectors
= sectors_per_dev
;
665 if (validate_rebuild_devices(rs
))
668 /* Assume there are no metadata devices until the drives are parsed */
669 rs
->md
.persistent
= 0;
675 static void do_table_event(struct work_struct
*ws
)
677 struct raid_set
*rs
= container_of(ws
, struct raid_set
, md
.event_work
);
679 dm_table_event(rs
->ti
->table
);
682 static int raid_is_congested(struct dm_target_callbacks
*cb
, int bits
)
684 struct raid_set
*rs
= container_of(cb
, struct raid_set
, callbacks
);
686 if (rs
->raid_type
->level
== 1)
687 return md_raid1_congested(&rs
->md
, bits
);
689 if (rs
->raid_type
->level
== 10)
690 return md_raid10_congested(&rs
->md
, bits
);
692 return md_raid5_congested(&rs
->md
, bits
);
696 * This structure is never routinely used by userspace, unlike md superblocks.
697 * Devices with this superblock should only ever be accessed via device-mapper.
699 #define DM_RAID_MAGIC 0x64526D44
700 struct dm_raid_superblock
{
701 __le32 magic
; /* "DmRd" */
702 __le32 features
; /* Used to indicate possible future changes */
704 __le32 num_devices
; /* Number of devices in this array. (Max 64) */
705 __le32 array_position
; /* The position of this drive in the array */
707 __le64 events
; /* Incremented by md when superblock updated */
708 __le64 failed_devices
; /* Bit field of devices to indicate failures */
711 * This offset tracks the progress of the repair or replacement of
712 * an individual drive.
714 __le64 disk_recovery_offset
;
717 * This offset tracks the progress of the initial array
718 * synchronisation/parity calculation.
720 __le64 array_resync_offset
;
723 * RAID characteristics
727 __le32 stripe_sectors
;
729 __u8 pad
[452]; /* Round struct to 512 bytes. */
730 /* Always set to 0 when writing. */
733 static int read_disk_sb(struct md_rdev
*rdev
, int size
)
735 BUG_ON(!rdev
->sb_page
);
740 if (!sync_page_io(rdev
, 0, size
, rdev
->sb_page
, READ
, 1)) {
741 DMERR("Failed to read superblock of device at position %d",
743 md_error(rdev
->mddev
, rdev
);
752 static void super_sync(struct mddev
*mddev
, struct md_rdev
*rdev
)
755 uint64_t failed_devices
;
756 struct dm_raid_superblock
*sb
;
757 struct raid_set
*rs
= container_of(mddev
, struct raid_set
, md
);
759 sb
= page_address(rdev
->sb_page
);
760 failed_devices
= le64_to_cpu(sb
->failed_devices
);
762 for (i
= 0; i
< mddev
->raid_disks
; i
++)
763 if (!rs
->dev
[i
].data_dev
||
764 test_bit(Faulty
, &(rs
->dev
[i
].rdev
.flags
)))
765 failed_devices
|= (1ULL << i
);
767 memset(sb
, 0, sizeof(*sb
));
769 sb
->magic
= cpu_to_le32(DM_RAID_MAGIC
);
770 sb
->features
= cpu_to_le32(0); /* No features yet */
772 sb
->num_devices
= cpu_to_le32(mddev
->raid_disks
);
773 sb
->array_position
= cpu_to_le32(rdev
->raid_disk
);
775 sb
->events
= cpu_to_le64(mddev
->events
);
776 sb
->failed_devices
= cpu_to_le64(failed_devices
);
778 sb
->disk_recovery_offset
= cpu_to_le64(rdev
->recovery_offset
);
779 sb
->array_resync_offset
= cpu_to_le64(mddev
->recovery_cp
);
781 sb
->level
= cpu_to_le32(mddev
->level
);
782 sb
->layout
= cpu_to_le32(mddev
->layout
);
783 sb
->stripe_sectors
= cpu_to_le32(mddev
->chunk_sectors
);
789 * This function creates a superblock if one is not found on the device
790 * and will decide which superblock to use if there's a choice.
792 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
794 static int super_load(struct md_rdev
*rdev
, struct md_rdev
*refdev
)
797 struct dm_raid_superblock
*sb
;
798 struct dm_raid_superblock
*refsb
;
799 uint64_t events_sb
, events_refsb
;
802 rdev
->sb_size
= sizeof(*sb
);
804 ret
= read_disk_sb(rdev
, rdev
->sb_size
);
808 sb
= page_address(rdev
->sb_page
);
811 * Two cases that we want to write new superblocks and rebuild:
812 * 1) New device (no matching magic number)
813 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
815 if ((sb
->magic
!= cpu_to_le32(DM_RAID_MAGIC
)) ||
816 (!test_bit(In_sync
, &rdev
->flags
) && !rdev
->recovery_offset
)) {
817 super_sync(rdev
->mddev
, rdev
);
819 set_bit(FirstUse
, &rdev
->flags
);
821 /* Force writing of superblocks to disk */
822 set_bit(MD_CHANGE_DEVS
, &rdev
->mddev
->flags
);
824 /* Any superblock is better than none, choose that if given */
825 return refdev
? 0 : 1;
831 events_sb
= le64_to_cpu(sb
->events
);
833 refsb
= page_address(refdev
->sb_page
);
834 events_refsb
= le64_to_cpu(refsb
->events
);
836 return (events_sb
> events_refsb
) ? 1 : 0;
839 static int super_init_validation(struct mddev
*mddev
, struct md_rdev
*rdev
)
842 struct raid_set
*rs
= container_of(mddev
, struct raid_set
, md
);
844 uint64_t failed_devices
;
845 struct dm_raid_superblock
*sb
;
846 uint32_t new_devs
= 0;
847 uint32_t rebuilds
= 0;
849 struct dm_raid_superblock
*sb2
;
851 sb
= page_address(rdev
->sb_page
);
852 events_sb
= le64_to_cpu(sb
->events
);
853 failed_devices
= le64_to_cpu(sb
->failed_devices
);
856 * Initialise to 1 if this is a new superblock.
858 mddev
->events
= events_sb
? : 1;
861 * Reshaping is not currently allowed
863 if ((le32_to_cpu(sb
->level
) != mddev
->level
) ||
864 (le32_to_cpu(sb
->layout
) != mddev
->layout
) ||
865 (le32_to_cpu(sb
->stripe_sectors
) != mddev
->chunk_sectors
)) {
866 DMERR("Reshaping arrays not yet supported.");
870 /* We can only change the number of devices in RAID1 right now */
871 if ((rs
->raid_type
->level
!= 1) &&
872 (le32_to_cpu(sb
->num_devices
) != mddev
->raid_disks
)) {
873 DMERR("Reshaping arrays not yet supported.");
877 if (!(rs
->print_flags
& (DMPF_SYNC
| DMPF_NOSYNC
)))
878 mddev
->recovery_cp
= le64_to_cpu(sb
->array_resync_offset
);
881 * During load, we set FirstUse if a new superblock was written.
882 * There are two reasons we might not have a superblock:
883 * 1) The array is brand new - in which case, all of the
884 * devices must have their In_sync bit set. Also,
885 * recovery_cp must be 0, unless forced.
886 * 2) This is a new device being added to an old array
887 * and the new device needs to be rebuilt - in which
888 * case the In_sync bit will /not/ be set and
889 * recovery_cp must be MaxSector.
891 rdev_for_each(r
, mddev
) {
892 if (!test_bit(In_sync
, &r
->flags
)) {
893 DMINFO("Device %d specified for rebuild: "
894 "Clearing superblock", r
->raid_disk
);
896 } else if (test_bit(FirstUse
, &r
->flags
))
901 if (new_devs
== mddev
->raid_disks
) {
902 DMINFO("Superblocks created for new array");
903 set_bit(MD_ARRAY_FIRST_USE
, &mddev
->flags
);
904 } else if (new_devs
) {
905 DMERR("New device injected "
906 "into existing array without 'rebuild' "
907 "parameter specified");
910 } else if (new_devs
) {
911 DMERR("'rebuild' devices cannot be "
912 "injected into an array with other first-time devices");
914 } else if (mddev
->recovery_cp
!= MaxSector
) {
915 DMERR("'rebuild' specified while array is not in-sync");
920 * Now we set the Faulty bit for those devices that are
921 * recorded in the superblock as failed.
923 rdev_for_each(r
, mddev
) {
926 sb2
= page_address(r
->sb_page
);
927 sb2
->failed_devices
= 0;
930 * Check for any device re-ordering.
932 if (!test_bit(FirstUse
, &r
->flags
) && (r
->raid_disk
>= 0)) {
933 role
= le32_to_cpu(sb2
->array_position
);
934 if (role
!= r
->raid_disk
) {
935 if (rs
->raid_type
->level
!= 1) {
936 rs
->ti
->error
= "Cannot change device "
937 "positions in RAID array";
940 DMINFO("RAID1 device #%d now at position #%d",
945 * Partial recovery is performed on
946 * returning failed devices.
948 if (failed_devices
& (1 << role
))
949 set_bit(Faulty
, &r
->flags
);
956 static int super_validate(struct mddev
*mddev
, struct md_rdev
*rdev
)
958 struct dm_raid_superblock
*sb
= page_address(rdev
->sb_page
);
961 * If mddev->events is not set, we know we have not yet initialized
964 if (!mddev
->events
&& super_init_validation(mddev
, rdev
))
967 mddev
->bitmap_info
.offset
= 4096 >> 9; /* Enable bitmap creation */
968 rdev
->mddev
->bitmap_info
.default_offset
= 4096 >> 9;
969 if (!test_bit(FirstUse
, &rdev
->flags
)) {
970 rdev
->recovery_offset
= le64_to_cpu(sb
->disk_recovery_offset
);
971 if (rdev
->recovery_offset
!= MaxSector
)
972 clear_bit(In_sync
, &rdev
->flags
);
976 * If a device comes back, set it as not In_sync and no longer faulty.
978 if (test_bit(Faulty
, &rdev
->flags
)) {
979 clear_bit(Faulty
, &rdev
->flags
);
980 clear_bit(In_sync
, &rdev
->flags
);
981 rdev
->saved_raid_disk
= rdev
->raid_disk
;
982 rdev
->recovery_offset
= 0;
985 clear_bit(FirstUse
, &rdev
->flags
);
991 * Analyse superblocks and select the freshest.
993 static int analyse_superblocks(struct dm_target
*ti
, struct raid_set
*rs
)
996 unsigned redundancy
= 0;
997 struct raid_dev
*dev
;
998 struct md_rdev
*rdev
, *tmp
, *freshest
;
999 struct mddev
*mddev
= &rs
->md
;
1001 switch (rs
->raid_type
->level
) {
1003 redundancy
= rs
->md
.raid_disks
- 1;
1008 redundancy
= rs
->raid_type
->parity_devs
;
1011 redundancy
= raid10_md_layout_to_copies(mddev
->layout
) - 1;
1014 ti
->error
= "Unknown RAID type";
1019 rdev_for_each_safe(rdev
, tmp
, mddev
) {
1021 * Skipping super_load due to DMPF_SYNC will cause
1022 * the array to undergo initialization again as
1023 * though it were new. This is the intended effect
1024 * of the "sync" directive.
1026 * When reshaping capability is added, we must ensure
1027 * that the "sync" directive is disallowed during the
1030 if (rs
->print_flags
& DMPF_SYNC
)
1033 if (!rdev
->meta_bdev
)
1036 ret
= super_load(rdev
, freshest
);
1045 dev
= container_of(rdev
, struct raid_dev
, rdev
);
1048 dm_put_device(ti
, dev
->meta_dev
);
1050 dev
->meta_dev
= NULL
;
1051 rdev
->meta_bdev
= NULL
;
1054 put_page(rdev
->sb_page
);
1056 rdev
->sb_page
= NULL
;
1058 rdev
->sb_loaded
= 0;
1061 * We might be able to salvage the data device
1062 * even though the meta device has failed. For
1063 * now, we behave as though '- -' had been
1064 * set for this device in the table.
1067 dm_put_device(ti
, dev
->data_dev
);
1069 dev
->data_dev
= NULL
;
1072 list_del(&rdev
->same_set
);
1076 ti
->error
= "Failed to load superblock";
1085 * Validation of the freshest device provides the source of
1086 * validation for the remaining devices.
1088 ti
->error
= "Unable to assemble array: Invalid superblocks";
1089 if (super_validate(mddev
, freshest
))
1092 rdev_for_each(rdev
, mddev
)
1093 if ((rdev
!= freshest
) && super_validate(mddev
, rdev
))
1100 * Construct a RAID4/5/6 mapping:
1102 * <raid_type> <#raid_params> <raid_params> \
1103 * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
1105 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
1106 * details on possible <raid_params>.
1108 static int raid_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
1111 struct raid_type
*rt
;
1112 unsigned long num_raid_params
, num_raid_devs
;
1113 struct raid_set
*rs
= NULL
;
1115 /* Must have at least <raid_type> <#raid_params> */
1117 ti
->error
= "Too few arguments";
1122 rt
= get_raid_type(argv
[0]);
1124 ti
->error
= "Unrecognised raid_type";
1130 /* number of RAID parameters */
1131 if (strict_strtoul(argv
[0], 10, &num_raid_params
) < 0) {
1132 ti
->error
= "Cannot understand number of RAID parameters";
1138 /* Skip over RAID params for now and find out # of devices */
1139 if (num_raid_params
+ 1 > argc
) {
1140 ti
->error
= "Arguments do not agree with counts given";
1144 if ((strict_strtoul(argv
[num_raid_params
], 10, &num_raid_devs
) < 0) ||
1145 (num_raid_devs
>= INT_MAX
)) {
1146 ti
->error
= "Cannot understand number of raid devices";
1150 rs
= context_alloc(ti
, rt
, (unsigned)num_raid_devs
);
1154 ret
= parse_raid_params(rs
, argv
, (unsigned)num_raid_params
);
1160 argc
-= num_raid_params
+ 1; /* +1: we already have num_raid_devs */
1161 argv
+= num_raid_params
+ 1;
1163 if (argc
!= (num_raid_devs
* 2)) {
1164 ti
->error
= "Supplied RAID devices does not match the count given";
1168 ret
= dev_parms(rs
, argv
);
1172 rs
->md
.sync_super
= super_sync
;
1173 ret
= analyse_superblocks(ti
, rs
);
1177 INIT_WORK(&rs
->md
.event_work
, do_table_event
);
1179 ti
->num_flush_requests
= 1;
1181 mutex_lock(&rs
->md
.reconfig_mutex
);
1182 ret
= md_run(&rs
->md
);
1183 rs
->md
.in_sync
= 0; /* Assume already marked dirty */
1184 mutex_unlock(&rs
->md
.reconfig_mutex
);
1187 ti
->error
= "Fail to run raid array";
1191 if (ti
->len
!= rs
->md
.array_sectors
) {
1192 ti
->error
= "Array size does not match requested target length";
1196 rs
->callbacks
.congested_fn
= raid_is_congested
;
1197 dm_table_add_target_callbacks(ti
->table
, &rs
->callbacks
);
1199 mddev_suspend(&rs
->md
);
1210 static void raid_dtr(struct dm_target
*ti
)
1212 struct raid_set
*rs
= ti
->private;
1214 list_del_init(&rs
->callbacks
.list
);
1219 static int raid_map(struct dm_target
*ti
, struct bio
*bio
, union map_info
*map_context
)
1221 struct raid_set
*rs
= ti
->private;
1222 struct mddev
*mddev
= &rs
->md
;
1224 mddev
->pers
->make_request(mddev
, bio
);
1226 return DM_MAPIO_SUBMITTED
;
1229 static int raid_status(struct dm_target
*ti
, status_type_t type
,
1230 unsigned status_flags
, char *result
, unsigned maxlen
)
1232 struct raid_set
*rs
= ti
->private;
1233 unsigned raid_param_cnt
= 1; /* at least 1 for chunksize */
1235 int i
, array_in_sync
= 0;
1239 case STATUSTYPE_INFO
:
1240 DMEMIT("%s %d ", rs
->raid_type
->name
, rs
->md
.raid_disks
);
1242 if (test_bit(MD_RECOVERY_RUNNING
, &rs
->md
.recovery
))
1243 sync
= rs
->md
.curr_resync_completed
;
1245 sync
= rs
->md
.recovery_cp
;
1247 if (sync
>= rs
->md
.resync_max_sectors
) {
1249 sync
= rs
->md
.resync_max_sectors
;
1252 * The array may be doing an initial sync, or it may
1253 * be rebuilding individual components. If all the
1254 * devices are In_sync, then it is the array that is
1255 * being initialized.
1257 for (i
= 0; i
< rs
->md
.raid_disks
; i
++)
1258 if (!test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
1262 * Status characters:
1263 * 'D' = Dead/Failed device
1264 * 'a' = Alive but not in-sync
1265 * 'A' = Alive and in-sync
1267 for (i
= 0; i
< rs
->md
.raid_disks
; i
++) {
1268 if (test_bit(Faulty
, &rs
->dev
[i
].rdev
.flags
))
1270 else if (!array_in_sync
||
1271 !test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
1279 * The in-sync ratio shows the progress of:
1280 * - Initializing the array
1281 * - Rebuilding a subset of devices of the array
1282 * The user can distinguish between the two by referring
1283 * to the status characters.
1285 DMEMIT(" %llu/%llu",
1286 (unsigned long long) sync
,
1287 (unsigned long long) rs
->md
.resync_max_sectors
);
1290 case STATUSTYPE_TABLE
:
1291 /* The string you would use to construct this array */
1292 for (i
= 0; i
< rs
->md
.raid_disks
; i
++) {
1293 if ((rs
->print_flags
& DMPF_REBUILD
) &&
1294 rs
->dev
[i
].data_dev
&&
1295 !test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
1296 raid_param_cnt
+= 2; /* for rebuilds */
1297 if (rs
->dev
[i
].data_dev
&&
1298 test_bit(WriteMostly
, &rs
->dev
[i
].rdev
.flags
))
1299 raid_param_cnt
+= 2;
1302 raid_param_cnt
+= (hweight32(rs
->print_flags
& ~DMPF_REBUILD
) * 2);
1303 if (rs
->print_flags
& (DMPF_SYNC
| DMPF_NOSYNC
))
1306 DMEMIT("%s %u %u", rs
->raid_type
->name
,
1307 raid_param_cnt
, rs
->md
.chunk_sectors
);
1309 if ((rs
->print_flags
& DMPF_SYNC
) &&
1310 (rs
->md
.recovery_cp
== MaxSector
))
1312 if (rs
->print_flags
& DMPF_NOSYNC
)
1315 for (i
= 0; i
< rs
->md
.raid_disks
; i
++)
1316 if ((rs
->print_flags
& DMPF_REBUILD
) &&
1317 rs
->dev
[i
].data_dev
&&
1318 !test_bit(In_sync
, &rs
->dev
[i
].rdev
.flags
))
1319 DMEMIT(" rebuild %u", i
);
1321 if (rs
->print_flags
& DMPF_DAEMON_SLEEP
)
1322 DMEMIT(" daemon_sleep %lu",
1323 rs
->md
.bitmap_info
.daemon_sleep
);
1325 if (rs
->print_flags
& DMPF_MIN_RECOVERY_RATE
)
1326 DMEMIT(" min_recovery_rate %d", rs
->md
.sync_speed_min
);
1328 if (rs
->print_flags
& DMPF_MAX_RECOVERY_RATE
)
1329 DMEMIT(" max_recovery_rate %d", rs
->md
.sync_speed_max
);
1331 for (i
= 0; i
< rs
->md
.raid_disks
; i
++)
1332 if (rs
->dev
[i
].data_dev
&&
1333 test_bit(WriteMostly
, &rs
->dev
[i
].rdev
.flags
))
1334 DMEMIT(" write_mostly %u", i
);
1336 if (rs
->print_flags
& DMPF_MAX_WRITE_BEHIND
)
1337 DMEMIT(" max_write_behind %lu",
1338 rs
->md
.bitmap_info
.max_write_behind
);
1340 if (rs
->print_flags
& DMPF_STRIPE_CACHE
) {
1341 struct r5conf
*conf
= rs
->md
.private;
1343 /* convert from kiB to sectors */
1344 DMEMIT(" stripe_cache %d",
1345 conf
? conf
->max_nr_stripes
* 2 : 0);
1348 if (rs
->print_flags
& DMPF_REGION_SIZE
)
1349 DMEMIT(" region_size %lu",
1350 rs
->md
.bitmap_info
.chunksize
>> 9);
1352 if (rs
->print_flags
& DMPF_RAID10_COPIES
)
1353 DMEMIT(" raid10_copies %u",
1354 raid10_md_layout_to_copies(rs
->md
.layout
));
1356 if (rs
->print_flags
& DMPF_RAID10_FORMAT
)
1357 DMEMIT(" raid10_format near");
1359 DMEMIT(" %d", rs
->md
.raid_disks
);
1360 for (i
= 0; i
< rs
->md
.raid_disks
; i
++) {
1361 if (rs
->dev
[i
].meta_dev
)
1362 DMEMIT(" %s", rs
->dev
[i
].meta_dev
->name
);
1366 if (rs
->dev
[i
].data_dev
)
1367 DMEMIT(" %s", rs
->dev
[i
].data_dev
->name
);
1376 static int raid_iterate_devices(struct dm_target
*ti
, iterate_devices_callout_fn fn
, void *data
)
1378 struct raid_set
*rs
= ti
->private;
1382 for (i
= 0; !ret
&& i
< rs
->md
.raid_disks
; i
++)
1383 if (rs
->dev
[i
].data_dev
)
1385 rs
->dev
[i
].data_dev
,
1386 0, /* No offset on data devs */
1393 static void raid_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
1395 struct raid_set
*rs
= ti
->private;
1396 unsigned chunk_size
= rs
->md
.chunk_sectors
<< 9;
1397 struct r5conf
*conf
= rs
->md
.private;
1399 blk_limits_io_min(limits
, chunk_size
);
1400 blk_limits_io_opt(limits
, chunk_size
* (conf
->raid_disks
- conf
->max_degraded
));
1403 static void raid_presuspend(struct dm_target
*ti
)
1405 struct raid_set
*rs
= ti
->private;
1407 md_stop_writes(&rs
->md
);
1410 static void raid_postsuspend(struct dm_target
*ti
)
1412 struct raid_set
*rs
= ti
->private;
1414 mddev_suspend(&rs
->md
);
1417 static void raid_resume(struct dm_target
*ti
)
1419 struct raid_set
*rs
= ti
->private;
1421 set_bit(MD_CHANGE_DEVS
, &rs
->md
.flags
);
1422 if (!rs
->bitmap_loaded
) {
1423 bitmap_load(&rs
->md
);
1424 rs
->bitmap_loaded
= 1;
1427 clear_bit(MD_RECOVERY_FROZEN
, &rs
->md
.recovery
);
1428 mddev_resume(&rs
->md
);
1431 static struct target_type raid_target
= {
1433 .version
= {1, 3, 1},
1434 .module
= THIS_MODULE
,
1438 .status
= raid_status
,
1439 .iterate_devices
= raid_iterate_devices
,
1440 .io_hints
= raid_io_hints
,
1441 .presuspend
= raid_presuspend
,
1442 .postsuspend
= raid_postsuspend
,
1443 .resume
= raid_resume
,
1446 static int __init
dm_raid_init(void)
1448 return dm_register_target(&raid_target
);
1451 static void __exit
dm_raid_exit(void)
1453 dm_unregister_target(&raid_target
);
1456 module_init(dm_raid_init
);
1457 module_exit(dm_raid_exit
);
1459 MODULE_DESCRIPTION(DM_NAME
" raid4/5/6 target");
1460 MODULE_ALIAS("dm-raid1");
1461 MODULE_ALIAS("dm-raid10");
1462 MODULE_ALIAS("dm-raid4");
1463 MODULE_ALIAS("dm-raid5");
1464 MODULE_ALIAS("dm-raid6");
1465 MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
1466 MODULE_LICENSE("GPL");