1111#include "md.h"
1212#include "raid1.h"
1313#include "raid5.h"
14+ #include "raid10.h"
1415#include "bitmap.h"
1516
1617#include <linux/device-mapper.h>
@@ -52,7 +53,10 @@ struct raid_dev {
5253#define DMPF_MAX_RECOVERY_RATE 0x20
5354#define DMPF_MAX_WRITE_BEHIND 0x40
5455#define DMPF_STRIPE_CACHE 0x80
55- #define DMPF_REGION_SIZE 0X100
56+ #define DMPF_REGION_SIZE 0x100
57+ #define DMPF_RAID10_COPIES 0x200
58+ #define DMPF_RAID10_FORMAT 0x400
59+
5660struct raid_set {
5761 struct dm_target * ti ;
5862
@@ -76,6 +80,7 @@ static struct raid_type {
7680 const unsigned algorithm ; /* RAID algorithm. */
7781} raid_types [] = {
7882 {"raid1" , "RAID1 (mirroring)" , 0 , 2 , 1 , 0 /* NONE */ },
83+ {"raid10" , "RAID10 (striped mirrors)" , 0 , 2 , 10 , UINT_MAX /* Varies */ },
7984 {"raid4" , "RAID4 (dedicated parity disk)" , 1 , 2 , 5 , ALGORITHM_PARITY_0 },
8085 {"raid5_la" , "RAID5 (left asymmetric)" , 1 , 2 , 5 , ALGORITHM_LEFT_ASYMMETRIC },
8186 {"raid5_ra" , "RAID5 (right asymmetric)" , 1 , 2 , 5 , ALGORITHM_RIGHT_ASYMMETRIC },
@@ -86,6 +91,17 @@ static struct raid_type {
8691 {"raid6_nc" , "RAID6 (N continue)" , 2 , 4 , 6 , ALGORITHM_ROTATING_N_CONTINUE }
8792};
8893
94+ static unsigned raid10_md_layout_to_copies (int layout )
95+ {
96+ return layout & 0xFF ;
97+ }
98+
99+ static int raid10_format_to_md_layout (char * format , unsigned copies )
100+ {
101+ /* 1 "far" copy, and 'copies' "near" copies */
102+ return (1 << 8 ) | (copies & 0xFF );
103+ }
104+
89105static struct raid_type * get_raid_type (char * name )
90106{
91107 int i ;
@@ -339,10 +355,16 @@ static int validate_region_size(struct raid_set *rs, unsigned long region_size)
339355 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
340356 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
341357 * [region_size <sectors>] Defines granularity of bitmap
358+ *
359+ * RAID10-only options:
360+ * [raid10_copies <# copies>] Number of copies. (Default: 2)
361+ * [raid10_format <near>] Layout algorithm. (Default: near)
342362 */
343363static int parse_raid_params (struct raid_set * rs , char * * argv ,
344364 unsigned num_raid_params )
345365{
366+ char * raid10_format = "near" ;
367+ unsigned raid10_copies = 2 ;
346368 unsigned i , rebuild_cnt = 0 ;
347369 unsigned long value , region_size = 0 ;
348370 sector_t sectors_per_dev = rs -> ti -> len ;
@@ -416,11 +438,28 @@ static int parse_raid_params(struct raid_set *rs, char **argv,
416438 }
417439
418440 key = argv [i ++ ];
441+
442+ /* Parameters that take a string value are checked here. */
443+ if (!strcasecmp (key , "raid10_format" )) {
444+ if (rs -> raid_type -> level != 10 ) {
445+ rs -> ti -> error = "'raid10_format' is an invalid parameter for this RAID type" ;
446+ return - EINVAL ;
447+ }
448+ if (strcmp ("near" , argv [i ])) {
449+ rs -> ti -> error = "Invalid 'raid10_format' value given" ;
450+ return - EINVAL ;
451+ }
452+ raid10_format = argv [i ];
453+ rs -> print_flags |= DMPF_RAID10_FORMAT ;
454+ continue ;
455+ }
456+
419457 if (strict_strtoul (argv [i ], 10 , & value ) < 0 ) {
420458 rs -> ti -> error = "Bad numerical argument given in raid params" ;
421459 return - EINVAL ;
422460 }
423461
462+ /* Parameters that take a numeric value are checked here */
424463 if (!strcasecmp (key , "rebuild" )) {
425464 rebuild_cnt ++ ;
426465
@@ -439,6 +478,7 @@ static int parse_raid_params(struct raid_set *rs, char **argv,
439478 return - EINVAL ;
440479 }
441480 break ;
481+ case 10 :
442482 default :
443483 DMERR ("The rebuild parameter is not supported for %s" , rs -> raid_type -> name );
444484 rs -> ti -> error = "Rebuild not supported for this RAID type" ;
@@ -495,7 +535,8 @@ static int parse_raid_params(struct raid_set *rs, char **argv,
495535 */
496536 value /= 2 ;
497537
498- if (rs -> raid_type -> level < 5 ) {
538+ if ((rs -> raid_type -> level != 5 ) &&
539+ (rs -> raid_type -> level != 6 )) {
499540 rs -> ti -> error = "Inappropriate argument: stripe_cache" ;
500541 return - EINVAL ;
501542 }
@@ -520,6 +561,14 @@ static int parse_raid_params(struct raid_set *rs, char **argv,
520561 } else if (!strcasecmp (key , "region_size" )) {
521562 rs -> print_flags |= DMPF_REGION_SIZE ;
522563 region_size = value ;
564+ } else if (!strcasecmp (key , "raid10_copies" ) &&
565+ (rs -> raid_type -> level == 10 )) {
566+ if ((value < 2 ) || (value > 0xFF )) {
567+ rs -> ti -> error = "Bad value for 'raid10_copies'" ;
568+ return - EINVAL ;
569+ }
570+ rs -> print_flags |= DMPF_RAID10_COPIES ;
571+ raid10_copies = value ;
523572 } else {
524573 DMERR ("Unable to parse RAID parameter: %s" , key );
525574 rs -> ti -> error = "Unable to parse RAID parameters" ;
@@ -538,8 +587,22 @@ static int parse_raid_params(struct raid_set *rs, char **argv,
538587 if (dm_set_target_max_io_len (rs -> ti , max_io_len ))
539588 return - EINVAL ;
540589
541- if ((rs -> raid_type -> level > 1 ) &&
542- sector_div (sectors_per_dev , (rs -> md .raid_disks - rs -> raid_type -> parity_devs ))) {
590+ if (rs -> raid_type -> level == 10 ) {
591+ if (raid10_copies > rs -> md .raid_disks ) {
592+ rs -> ti -> error = "Not enough devices to satisfy specification" ;
593+ return - EINVAL ;
594+ }
595+
596+ /* (Len * #mirrors) / #devices */
597+ sectors_per_dev = rs -> ti -> len * raid10_copies ;
598+ sector_div (sectors_per_dev , rs -> md .raid_disks );
599+
600+ rs -> md .layout = raid10_format_to_md_layout (raid10_format ,
601+ raid10_copies );
602+ rs -> md .new_layout = rs -> md .layout ;
603+ } else if ((rs -> raid_type -> level > 1 ) &&
604+ sector_div (sectors_per_dev ,
605+ (rs -> md .raid_disks - rs -> raid_type -> parity_devs ))) {
543606 rs -> ti -> error = "Target length not divisible by number of data devices" ;
544607 return - EINVAL ;
545608 }
@@ -566,6 +629,9 @@ static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
566629 if (rs -> raid_type -> level == 1 )
567630 return md_raid1_congested (& rs -> md , bits );
568631
632+ if (rs -> raid_type -> level == 10 )
633+ return md_raid10_congested (& rs -> md , bits );
634+
569635 return md_raid5_congested (& rs -> md , bits );
570636}
571637
@@ -884,6 +950,9 @@ static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
884950 case 6 :
885951 redundancy = rs -> raid_type -> parity_devs ;
886952 break ;
953+ case 10 :
954+ redundancy = raid10_md_layout_to_copies (mddev -> layout ) - 1 ;
955+ break ;
887956 default :
888957 ti -> error = "Unknown RAID type" ;
889958 return - EINVAL ;
@@ -1049,12 +1118,19 @@ static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
10491118 goto bad ;
10501119 }
10511120
1121+ if (ti -> len != rs -> md .array_sectors ) {
1122+ ti -> error = "Array size does not match requested target length" ;
1123+ ret = - EINVAL ;
1124+ goto size_mismatch ;
1125+ }
10521126 rs -> callbacks .congested_fn = raid_is_congested ;
10531127 dm_table_add_target_callbacks (ti -> table , & rs -> callbacks );
10541128
10551129 mddev_suspend (& rs -> md );
10561130 return 0 ;
10571131
1132+ size_mismatch :
1133+ md_stop (& rs -> md );
10581134bad :
10591135 context_free (rs );
10601136
@@ -1203,6 +1279,13 @@ static int raid_status(struct dm_target *ti, status_type_t type,
12031279 DMEMIT (" region_size %lu" ,
12041280 rs -> md .bitmap_info .chunksize >> 9 );
12051281
1282+ if (rs -> print_flags & DMPF_RAID10_COPIES )
1283+ DMEMIT (" raid10_copies %u" ,
1284+ raid10_md_layout_to_copies (rs -> md .layout ));
1285+
1286+ if (rs -> print_flags & DMPF_RAID10_FORMAT )
1287+ DMEMIT (" raid10_format near" );
1288+
12061289 DMEMIT (" %d" , rs -> md .raid_disks );
12071290 for (i = 0 ; i < rs -> md .raid_disks ; i ++ ) {
12081291 if (rs -> dev [i ].meta_dev )
@@ -1277,7 +1360,7 @@ static void raid_resume(struct dm_target *ti)
12771360
12781361static struct target_type raid_target = {
12791362 .name = "raid" ,
1280- .version = {1 , 2 , 0 },
1363+ .version = {1 , 3 , 0 },
12811364 .module = THIS_MODULE ,
12821365 .ctr = raid_ctr ,
12831366 .dtr = raid_dtr ,
@@ -1304,6 +1387,8 @@ module_init(dm_raid_init);
13041387module_exit (dm_raid_exit );
13051388
13061389MODULE_DESCRIPTION (DM_NAME " raid4/5/6 target" );
1390+ MODULE_ALIAS ("dm-raid1" );
1391+ MODULE_ALIAS ("dm-raid10" );
13071392MODULE_ALIAS ("dm-raid4" );
13081393MODULE_ALIAS ("dm-raid5" );
13091394MODULE_ALIAS ("dm-raid6" );
0 commit comments