1 /* 2 * Copyright (C) 2010-2011 Neil Brown 3 * Copyright (C) 2010-2016 Red Hat, Inc. All rights reserved. 4 * 5 * This file is released under the GPL. 6 */ 7 8 #include <linux/slab.h> 9 #include <linux/module.h> 10 11 #include "md.h" 12 #include "raid1.h" 13 #include "raid5.h" 14 #include "raid10.h" 15 #include "bitmap.h" 16 17 #include <linux/device-mapper.h> 18 19 #define DM_MSG_PREFIX "raid" 20 #define MAX_RAID_DEVICES 253 /* md-raid kernel limit */ 21 22 /* 23 * Minimum sectors of free reshape space per raid device 24 */ 25 #define MIN_FREE_RESHAPE_SPACE to_sector(4*4096) 26 27 /* 28 * Minimum journal space 4 MiB in sectors. 29 */ 30 #define MIN_RAID456_JOURNAL_SPACE (4*2048) 31 32 static bool devices_handle_discard_safely = false; 33 34 /* 35 * The following flags are used by dm-raid.c to set up the array state. 36 * They must be cleared before md_run is called. 37 */ 38 #define FirstUse 10 /* rdev flag */ 39 40 struct raid_dev { 41 /* 42 * Two DM devices, one to hold metadata and one to hold the 43 * actual data/parity. The reason for this is to not confuse 44 * ti->len and give more flexibility in altering size and 45 * characteristics. 46 * 47 * While it is possible for this device to be associated 48 * with a different physical device than the data_dev, it 49 * is intended for it to be the same. 50 * |--------- Physical Device ---------| 51 * |- meta_dev -|------ data_dev ------| 52 */ 53 struct dm_dev *meta_dev; 54 struct dm_dev *data_dev; 55 struct md_rdev rdev; 56 }; 57 58 /* 59 * Bits for establishing rs->ctr_flags 60 * 61 * 1 = no flag value 62 * 2 = flag with value 63 */ 64 #define __CTR_FLAG_SYNC 0 /* 1 */ /* Not with raid0! */ 65 #define __CTR_FLAG_NOSYNC 1 /* 1 */ /* Not with raid0! */ 66 #define __CTR_FLAG_REBUILD 2 /* 2 */ /* Not with raid0! */ 67 #define __CTR_FLAG_DAEMON_SLEEP 3 /* 2 */ /* Not with raid0! */ 68 #define __CTR_FLAG_MIN_RECOVERY_RATE 4 /* 2 */ /* Not with raid0! */ 69 #define __CTR_FLAG_MAX_RECOVERY_RATE 5 /* 2 */ /* Not with raid0! */ 70 #define __CTR_FLAG_MAX_WRITE_BEHIND 6 /* 2 */ /* Only with raid1! */ 71 #define __CTR_FLAG_WRITE_MOSTLY 7 /* 2 */ /* Only with raid1! */ 72 #define __CTR_FLAG_STRIPE_CACHE 8 /* 2 */ /* Only with raid4/5/6! */ 73 #define __CTR_FLAG_REGION_SIZE 9 /* 2 */ /* Not with raid0! */ 74 #define __CTR_FLAG_RAID10_COPIES 10 /* 2 */ /* Only with raid10 */ 75 #define __CTR_FLAG_RAID10_FORMAT 11 /* 2 */ /* Only with raid10 */ 76 /* New for v1.9.0 */ 77 #define __CTR_FLAG_DELTA_DISKS 12 /* 2 */ /* Only with reshapable raid1/4/5/6/10! */ 78 #define __CTR_FLAG_DATA_OFFSET 13 /* 2 */ /* Only with reshapable raid4/5/6/10! */ 79 #define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */ 80 81 /* New for v1.10.0 */ 82 #define __CTR_FLAG_JOURNAL_DEV 15 /* 2 */ /* Only with raid4/5/6! */ 83 84 /* 85 * Flags for rs->ctr_flags field. 86 */ 87 #define CTR_FLAG_SYNC (1 << __CTR_FLAG_SYNC) 88 #define CTR_FLAG_NOSYNC (1 << __CTR_FLAG_NOSYNC) 89 #define CTR_FLAG_REBUILD (1 << __CTR_FLAG_REBUILD) 90 #define CTR_FLAG_DAEMON_SLEEP (1 << __CTR_FLAG_DAEMON_SLEEP) 91 #define CTR_FLAG_MIN_RECOVERY_RATE (1 << __CTR_FLAG_MIN_RECOVERY_RATE) 92 #define CTR_FLAG_MAX_RECOVERY_RATE (1 << __CTR_FLAG_MAX_RECOVERY_RATE) 93 #define CTR_FLAG_MAX_WRITE_BEHIND (1 << __CTR_FLAG_MAX_WRITE_BEHIND) 94 #define CTR_FLAG_WRITE_MOSTLY (1 << __CTR_FLAG_WRITE_MOSTLY) 95 #define CTR_FLAG_STRIPE_CACHE (1 << __CTR_FLAG_STRIPE_CACHE) 96 #define CTR_FLAG_REGION_SIZE (1 << __CTR_FLAG_REGION_SIZE) 97 #define CTR_FLAG_RAID10_COPIES (1 << __CTR_FLAG_RAID10_COPIES) 98 #define CTR_FLAG_RAID10_FORMAT (1 << __CTR_FLAG_RAID10_FORMAT) 99 #define CTR_FLAG_DELTA_DISKS (1 << __CTR_FLAG_DELTA_DISKS) 100 #define CTR_FLAG_DATA_OFFSET (1 << __CTR_FLAG_DATA_OFFSET) 101 #define CTR_FLAG_RAID10_USE_NEAR_SETS (1 << __CTR_FLAG_RAID10_USE_NEAR_SETS) 102 #define CTR_FLAG_JOURNAL_DEV (1 << __CTR_FLAG_JOURNAL_DEV) 103 104 /* 105 * Definitions of various constructor flags to 106 * be used in checks of valid / invalid flags 107 * per raid level. 108 */ 109 /* Define all any sync flags */ 110 #define CTR_FLAGS_ANY_SYNC (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC) 111 112 /* Define flags for options without argument (e.g. 'nosync') */ 113 #define CTR_FLAG_OPTIONS_NO_ARGS (CTR_FLAGS_ANY_SYNC | \ 114 CTR_FLAG_RAID10_USE_NEAR_SETS) 115 116 /* Define flags for options with one argument (e.g. 'delta_disks +2') */ 117 #define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \ 118 CTR_FLAG_WRITE_MOSTLY | \ 119 CTR_FLAG_DAEMON_SLEEP | \ 120 CTR_FLAG_MIN_RECOVERY_RATE | \ 121 CTR_FLAG_MAX_RECOVERY_RATE | \ 122 CTR_FLAG_MAX_WRITE_BEHIND | \ 123 CTR_FLAG_STRIPE_CACHE | \ 124 CTR_FLAG_REGION_SIZE | \ 125 CTR_FLAG_RAID10_COPIES | \ 126 CTR_FLAG_RAID10_FORMAT | \ 127 CTR_FLAG_DELTA_DISKS | \ 128 CTR_FLAG_DATA_OFFSET) 129 130 /* Valid options definitions per raid level... */ 131 132 /* "raid0" does only accept data offset */ 133 #define RAID0_VALID_FLAGS (CTR_FLAG_DATA_OFFSET) 134 135 /* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */ 136 #define RAID1_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \ 137 CTR_FLAG_REBUILD | \ 138 CTR_FLAG_WRITE_MOSTLY | \ 139 CTR_FLAG_DAEMON_SLEEP | \ 140 CTR_FLAG_MIN_RECOVERY_RATE | \ 141 CTR_FLAG_MAX_RECOVERY_RATE | \ 142 CTR_FLAG_MAX_WRITE_BEHIND | \ 143 CTR_FLAG_REGION_SIZE | \ 144 CTR_FLAG_DELTA_DISKS | \ 145 CTR_FLAG_DATA_OFFSET) 146 147 /* "raid10" does not accept any raid1 or stripe cache options */ 148 #define RAID10_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \ 149 CTR_FLAG_REBUILD | \ 150 CTR_FLAG_DAEMON_SLEEP | \ 151 CTR_FLAG_MIN_RECOVERY_RATE | \ 152 CTR_FLAG_MAX_RECOVERY_RATE | \ 153 CTR_FLAG_REGION_SIZE | \ 154 CTR_FLAG_RAID10_COPIES | \ 155 CTR_FLAG_RAID10_FORMAT | \ 156 CTR_FLAG_DELTA_DISKS | \ 157 CTR_FLAG_DATA_OFFSET | \ 158 CTR_FLAG_RAID10_USE_NEAR_SETS) 159 160 /* 161 * "raid4/5/6" do not accept any raid1 or raid10 specific options 162 * 163 * "raid6" does not accept "nosync", because it is not guaranteed 164 * that both parity and q-syndrome are being written properly with 165 * any writes 166 */ 167 #define RAID45_VALID_FLAGS (CTR_FLAGS_ANY_SYNC | \ 168 CTR_FLAG_REBUILD | \ 169 CTR_FLAG_DAEMON_SLEEP | \ 170 CTR_FLAG_MIN_RECOVERY_RATE | \ 171 CTR_FLAG_MAX_RECOVERY_RATE | \ 172 CTR_FLAG_STRIPE_CACHE | \ 173 CTR_FLAG_REGION_SIZE | \ 174 CTR_FLAG_DELTA_DISKS | \ 175 CTR_FLAG_DATA_OFFSET | \ 176 CTR_FLAG_JOURNAL_DEV) 177 178 #define RAID6_VALID_FLAGS (CTR_FLAG_SYNC | \ 179 CTR_FLAG_REBUILD | \ 180 CTR_FLAG_DAEMON_SLEEP | \ 181 CTR_FLAG_MIN_RECOVERY_RATE | \ 182 CTR_FLAG_MAX_RECOVERY_RATE | \ 183 CTR_FLAG_STRIPE_CACHE | \ 184 CTR_FLAG_REGION_SIZE | \ 185 CTR_FLAG_DELTA_DISKS | \ 186 CTR_FLAG_DATA_OFFSET | \ 187 CTR_FLAG_JOURNAL_DEV) 188 /* ...valid options definitions per raid level */ 189 190 /* 191 * Flags for rs->runtime_flags field 192 * (RT_FLAG prefix meaning "runtime flag") 193 * 194 * These are all internal and used to define runtime state, 195 * e.g. to prevent another resume from preresume processing 196 * the raid set all over again. 197 */ 198 #define RT_FLAG_RS_PRERESUMED 0 199 #define RT_FLAG_RS_RESUMED 1 200 #define RT_FLAG_RS_BITMAP_LOADED 2 201 #define RT_FLAG_UPDATE_SBS 3 202 #define RT_FLAG_RESHAPE_RS 4 203 204 /* Array elements of 64 bit needed for rebuild/failed disk bits */ 205 #define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8) 206 207 /* 208 * raid set level, layout and chunk sectors backup/restore 209 */ 210 struct rs_layout { 211 int new_level; 212 int new_layout; 213 int new_chunk_sectors; 214 }; 215 216 struct raid_set { 217 struct dm_target *ti; 218 219 uint32_t bitmap_loaded; 220 uint32_t stripe_cache_entries; 221 unsigned long ctr_flags; 222 unsigned long runtime_flags; 223 224 uint64_t rebuild_disks[DISKS_ARRAY_ELEMS]; 225 226 int raid_disks; 227 int delta_disks; 228 int data_offset; 229 int raid10_copies; 230 int requested_bitmap_chunk_sectors; 231 232 struct mddev md; 233 struct raid_type *raid_type; 234 struct dm_target_callbacks callbacks; 235 236 /* Optional raid4/5/6 journal device */ 237 struct journal_dev { 238 struct dm_dev *dev; 239 struct md_rdev rdev; 240 } journal_dev; 241 242 struct raid_dev dev[0]; 243 }; 244 245 static void rs_config_backup(struct raid_set *rs, struct rs_layout *l) 246 { 247 struct mddev *mddev = &rs->md; 248 249 l->new_level = mddev->new_level; 250 l->new_layout = mddev->new_layout; 251 l->new_chunk_sectors = mddev->new_chunk_sectors; 252 } 253 254 static void rs_config_restore(struct raid_set *rs, struct rs_layout *l) 255 { 256 struct mddev *mddev = &rs->md; 257 258 mddev->new_level = l->new_level; 259 mddev->new_layout = l->new_layout; 260 mddev->new_chunk_sectors = l->new_chunk_sectors; 261 } 262 263 /* raid10 algorithms (i.e. formats) */ 264 #define ALGORITHM_RAID10_DEFAULT 0 265 #define ALGORITHM_RAID10_NEAR 1 266 #define ALGORITHM_RAID10_OFFSET 2 267 #define ALGORITHM_RAID10_FAR 3 268 269 /* Supported raid types and properties. */ 270 static struct raid_type { 271 const char *name; /* RAID algorithm. */ 272 const char *descr; /* Descriptor text for logging. */ 273 const unsigned int parity_devs; /* # of parity devices. */ 274 const unsigned int minimal_devs;/* minimal # of devices in set. */ 275 const unsigned int level; /* RAID level. */ 276 const unsigned int algorithm; /* RAID algorithm. */ 277 } raid_types[] = { 278 {"raid0", "raid0 (striping)", 0, 2, 0, 0 /* NONE */}, 279 {"raid1", "raid1 (mirroring)", 0, 2, 1, 0 /* NONE */}, 280 {"raid10_far", "raid10 far (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_FAR}, 281 {"raid10_offset", "raid10 offset (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_OFFSET}, 282 {"raid10_near", "raid10 near (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_NEAR}, 283 {"raid10", "raid10 (striped mirrors)", 0, 2, 10, ALGORITHM_RAID10_DEFAULT}, 284 {"raid4", "raid4 (dedicated first parity disk)", 1, 2, 5, ALGORITHM_PARITY_0}, /* raid4 layout = raid5_0 */ 285 {"raid5_n", "raid5 (dedicated last parity disk)", 1, 2, 5, ALGORITHM_PARITY_N}, 286 {"raid5_ls", "raid5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC}, 287 {"raid5_rs", "raid5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC}, 288 {"raid5_la", "raid5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC}, 289 {"raid5_ra", "raid5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC}, 290 {"raid6_zr", "raid6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART}, 291 {"raid6_nr", "raid6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART}, 292 {"raid6_nc", "raid6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}, 293 {"raid6_n_6", "raid6 (dedicated parity/Q n/6)", 2, 4, 6, ALGORITHM_PARITY_N_6}, 294 {"raid6_ls_6", "raid6 (left symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_SYMMETRIC_6}, 295 {"raid6_rs_6", "raid6 (right symmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_SYMMETRIC_6}, 296 {"raid6_la_6", "raid6 (left asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_LEFT_ASYMMETRIC_6}, 297 {"raid6_ra_6", "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6, ALGORITHM_RIGHT_ASYMMETRIC_6} 298 }; 299 300 /* True, if @v is in inclusive range [@min, @max] */ 301 static bool __within_range(long v, long min, long max) 302 { 303 return v >= min && v <= max; 304 } 305 306 /* All table line arguments are defined here */ 307 static struct arg_name_flag { 308 const unsigned long flag; 309 const char *name; 310 } __arg_name_flags[] = { 311 { CTR_FLAG_SYNC, "sync"}, 312 { CTR_FLAG_NOSYNC, "nosync"}, 313 { CTR_FLAG_REBUILD, "rebuild"}, 314 { CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"}, 315 { CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"}, 316 { CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"}, 317 { CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"}, 318 { CTR_FLAG_WRITE_MOSTLY, "write_mostly"}, 319 { CTR_FLAG_STRIPE_CACHE, "stripe_cache"}, 320 { CTR_FLAG_REGION_SIZE, "region_size"}, 321 { CTR_FLAG_RAID10_COPIES, "raid10_copies"}, 322 { CTR_FLAG_RAID10_FORMAT, "raid10_format"}, 323 { CTR_FLAG_DATA_OFFSET, "data_offset"}, 324 { CTR_FLAG_DELTA_DISKS, "delta_disks"}, 325 { CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"}, 326 { CTR_FLAG_JOURNAL_DEV, "journal_dev" }, 327 }; 328 329 /* Return argument name string for given @flag */ 330 static const char *dm_raid_arg_name_by_flag(const uint32_t flag) 331 { 332 if (hweight32(flag) == 1) { 333 struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags); 334 335 while (anf-- > __arg_name_flags) 336 if (flag & anf->flag) 337 return anf->name; 338 339 } else 340 DMERR("%s called with more than one flag!", __func__); 341 342 return NULL; 343 } 344 345 /* 346 * Bool helpers to test for various raid levels of a raid set. 347 * It's level as reported by the superblock rather than 348 * the requested raid_type passed to the constructor. 349 */ 350 /* Return true, if raid set in @rs is raid0 */ 351 static bool rs_is_raid0(struct raid_set *rs) 352 { 353 return !rs->md.level; 354 } 355 356 /* Return true, if raid set in @rs is raid1 */ 357 static bool rs_is_raid1(struct raid_set *rs) 358 { 359 return rs->md.level == 1; 360 } 361 362 /* Return true, if raid set in @rs is raid10 */ 363 static bool rs_is_raid10(struct raid_set *rs) 364 { 365 return rs->md.level == 10; 366 } 367 368 /* Return true, if raid set in @rs is level 6 */ 369 static bool rs_is_raid6(struct raid_set *rs) 370 { 371 return rs->md.level == 6; 372 } 373 374 /* Return true, if raid set in @rs is level 4, 5 or 6 */ 375 static bool rs_is_raid456(struct raid_set *rs) 376 { 377 return __within_range(rs->md.level, 4, 6); 378 } 379 380 /* Return true, if raid set in @rs is reshapable */ 381 static bool __is_raid10_far(int layout); 382 static bool rs_is_reshapable(struct raid_set *rs) 383 { 384 return rs_is_raid456(rs) || 385 (rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout)); 386 } 387 388 /* Return true, if raid set in @rs is recovering */ 389 static bool rs_is_recovering(struct raid_set *rs) 390 { 391 return rs->md.recovery_cp < rs->md.dev_sectors; 392 } 393 394 /* Return true, if raid set in @rs is reshaping */ 395 static bool rs_is_reshaping(struct raid_set *rs) 396 { 397 return rs->md.reshape_position != MaxSector; 398 } 399 400 /* 401 * bool helpers to test for various raid levels of a raid type @rt 402 */ 403 404 /* Return true, if raid type in @rt is raid0 */ 405 static bool rt_is_raid0(struct raid_type *rt) 406 { 407 return !rt->level; 408 } 409 410 /* Return true, if raid type in @rt is raid1 */ 411 static bool rt_is_raid1(struct raid_type *rt) 412 { 413 return rt->level == 1; 414 } 415 416 /* Return true, if raid type in @rt is raid10 */ 417 static bool rt_is_raid10(struct raid_type *rt) 418 { 419 return rt->level == 10; 420 } 421 422 /* Return true, if raid type in @rt is raid4/5 */ 423 static bool rt_is_raid45(struct raid_type *rt) 424 { 425 return __within_range(rt->level, 4, 5); 426 } 427 428 /* Return true, if raid type in @rt is raid6 */ 429 static bool rt_is_raid6(struct raid_type *rt) 430 { 431 return rt->level == 6; 432 } 433 434 /* Return true, if raid type in @rt is raid4/5/6 */ 435 static bool rt_is_raid456(struct raid_type *rt) 436 { 437 return __within_range(rt->level, 4, 6); 438 } 439 /* END: raid level bools */ 440 441 /* Return valid ctr flags for the raid level of @rs */ 442 static unsigned long __valid_flags(struct raid_set *rs) 443 { 444 if (rt_is_raid0(rs->raid_type)) 445 return RAID0_VALID_FLAGS; 446 else if (rt_is_raid1(rs->raid_type)) 447 return RAID1_VALID_FLAGS; 448 else if (rt_is_raid10(rs->raid_type)) 449 return RAID10_VALID_FLAGS; 450 else if (rt_is_raid45(rs->raid_type)) 451 return RAID45_VALID_FLAGS; 452 else if (rt_is_raid6(rs->raid_type)) 453 return RAID6_VALID_FLAGS; 454 455 return 0; 456 } 457 458 /* 459 * Check for valid flags set on @rs 460 * 461 * Has to be called after parsing of the ctr flags! 462 */ 463 static int rs_check_for_valid_flags(struct raid_set *rs) 464 { 465 if (rs->ctr_flags & ~__valid_flags(rs)) { 466 rs->ti->error = "Invalid flags combination"; 467 return -EINVAL; 468 } 469 470 return 0; 471 } 472 473 /* MD raid10 bit definitions and helpers */ 474 #define RAID10_OFFSET (1 << 16) /* stripes with data copies area adjacent on devices */ 475 #define RAID10_BROCKEN_USE_FAR_SETS (1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */ 476 #define RAID10_USE_FAR_SETS (1 << 18) /* Use sets instead of whole stripe rotation */ 477 #define RAID10_FAR_COPIES_SHIFT 8 /* raid10 # far copies shift (2nd byte of layout) */ 478 479 /* Return md raid10 near copies for @layout */ 480 static unsigned int __raid10_near_copies(int layout) 481 { 482 return layout & 0xFF; 483 } 484 485 /* Return md raid10 far copies for @layout */ 486 static unsigned int __raid10_far_copies(int layout) 487 { 488 return __raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT); 489 } 490 491 /* Return true if md raid10 offset for @layout */ 492 static bool __is_raid10_offset(int layout) 493 { 494 return !!(layout & RAID10_OFFSET); 495 } 496 497 /* Return true if md raid10 near for @layout */ 498 static bool __is_raid10_near(int layout) 499 { 500 return !__is_raid10_offset(layout) && __raid10_near_copies(layout) > 1; 501 } 502 503 /* Return true if md raid10 far for @layout */ 504 static bool __is_raid10_far(int layout) 505 { 506 return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1; 507 } 508 509 /* Return md raid10 layout string for @layout */ 510 static const char *raid10_md_layout_to_format(int layout) 511 { 512 /* 513 * Bit 16 stands for "offset" 514 * (i.e. adjacent stripes hold copies) 515 * 516 * Refer to MD's raid10.c for details 517 */ 518 if (__is_raid10_offset(layout)) 519 return "offset"; 520 521 if (__raid10_near_copies(layout) > 1) 522 return "near"; 523 524 WARN_ON(__raid10_far_copies(layout) < 2); 525 526 return "far"; 527 } 528 529 /* Return md raid10 algorithm for @name */ 530 static int raid10_name_to_format(const char *name) 531 { 532 if (!strcasecmp(name, "near")) 533 return ALGORITHM_RAID10_NEAR; 534 else if (!strcasecmp(name, "offset")) 535 return ALGORITHM_RAID10_OFFSET; 536 else if (!strcasecmp(name, "far")) 537 return ALGORITHM_RAID10_FAR; 538 539 return -EINVAL; 540 } 541 542 /* Return md raid10 copies for @layout */ 543 static unsigned int raid10_md_layout_to_copies(int layout) 544 { 545 return max(__raid10_near_copies(layout), __raid10_far_copies(layout)); 546 } 547 548 /* Return md raid10 format id for @format string */ 549 static int raid10_format_to_md_layout(struct raid_set *rs, 550 unsigned int algorithm, 551 unsigned int copies) 552 { 553 unsigned int n = 1, f = 1, r = 0; 554 555 /* 556 * MD resilienece flaw: 557 * 558 * enabling use_far_sets for far/offset formats causes copies 559 * to be colocated on the same devs together with their origins! 560 * 561 * -> disable it for now in the definition above 562 */ 563 if (algorithm == ALGORITHM_RAID10_DEFAULT || 564 algorithm == ALGORITHM_RAID10_NEAR) 565 n = copies; 566 567 else if (algorithm == ALGORITHM_RAID10_OFFSET) { 568 f = copies; 569 r = RAID10_OFFSET; 570 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) 571 r |= RAID10_USE_FAR_SETS; 572 573 } else if (algorithm == ALGORITHM_RAID10_FAR) { 574 f = copies; 575 r = !RAID10_OFFSET; 576 if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) 577 r |= RAID10_USE_FAR_SETS; 578 579 } else 580 return -EINVAL; 581 582 return r | (f << RAID10_FAR_COPIES_SHIFT) | n; 583 } 584 /* END: MD raid10 bit definitions and helpers */ 585 586 /* Check for any of the raid10 algorithms */ 587 static bool __got_raid10(struct raid_type *rtp, const int layout) 588 { 589 if (rtp->level == 10) { 590 switch (rtp->algorithm) { 591 case ALGORITHM_RAID10_DEFAULT: 592 case ALGORITHM_RAID10_NEAR: 593 return __is_raid10_near(layout); 594 case ALGORITHM_RAID10_OFFSET: 595 return __is_raid10_offset(layout); 596 case ALGORITHM_RAID10_FAR: 597 return __is_raid10_far(layout); 598 default: 599 break; 600 } 601 } 602 603 return false; 604 } 605 606 /* Return raid_type for @name */ 607 static struct raid_type *get_raid_type(const char *name) 608 { 609 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types); 610 611 while (rtp-- > raid_types) 612 if (!strcasecmp(rtp->name, name)) 613 return rtp; 614 615 return NULL; 616 } 617 618 /* Return raid_type for @name based derived from @level and @layout */ 619 static struct raid_type *get_raid_type_by_ll(const int level, const int layout) 620 { 621 struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types); 622 623 while (rtp-- > raid_types) { 624 /* RAID10 special checks based on @layout flags/properties */ 625 if (rtp->level == level && 626 (__got_raid10(rtp, layout) || rtp->algorithm == layout)) 627 return rtp; 628 } 629 630 return NULL; 631 } 632 633 /* 634 * Conditionally change bdev capacity of @rs 635 * in case of a disk add/remove reshape 636 */ 637 static void rs_set_capacity(struct raid_set *rs) 638 { 639 struct mddev *mddev = &rs->md; 640 struct md_rdev *rdev; 641 struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table)); 642 643 /* 644 * raid10 sets rdev->sector to the device size, which 645 * is unintended in case of out-of-place reshaping 646 */ 647 rdev_for_each(rdev, mddev) 648 if (!test_bit(Journal, &rdev->flags)) 649 rdev->sectors = mddev->dev_sectors; 650 651 set_capacity(gendisk, mddev->array_sectors); 652 revalidate_disk(gendisk); 653 } 654 655 /* 656 * Set the mddev properties in @rs to the current 657 * ones retrieved from the freshest superblock 658 */ 659 static void rs_set_cur(struct raid_set *rs) 660 { 661 struct mddev *mddev = &rs->md; 662 663 mddev->new_level = mddev->level; 664 mddev->new_layout = mddev->layout; 665 mddev->new_chunk_sectors = mddev->chunk_sectors; 666 } 667 668 /* 669 * Set the mddev properties in @rs to the new 670 * ones requested by the ctr 671 */ 672 static void rs_set_new(struct raid_set *rs) 673 { 674 struct mddev *mddev = &rs->md; 675 676 mddev->level = mddev->new_level; 677 mddev->layout = mddev->new_layout; 678 mddev->chunk_sectors = mddev->new_chunk_sectors; 679 mddev->raid_disks = rs->raid_disks; 680 mddev->delta_disks = 0; 681 } 682 683 static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type, 684 unsigned int raid_devs) 685 { 686 unsigned int i; 687 struct raid_set *rs; 688 689 if (raid_devs <= raid_type->parity_devs) { 690 ti->error = "Insufficient number of devices"; 691 return ERR_PTR(-EINVAL); 692 } 693 694 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL); 695 if (!rs) { 696 ti->error = "Cannot allocate raid context"; 697 return ERR_PTR(-ENOMEM); 698 } 699 700 mddev_init(&rs->md); 701 702 rs->raid_disks = raid_devs; 703 rs->delta_disks = 0; 704 705 rs->ti = ti; 706 rs->raid_type = raid_type; 707 rs->stripe_cache_entries = 256; 708 rs->md.raid_disks = raid_devs; 709 rs->md.level = raid_type->level; 710 rs->md.new_level = rs->md.level; 711 rs->md.layout = raid_type->algorithm; 712 rs->md.new_layout = rs->md.layout; 713 rs->md.delta_disks = 0; 714 rs->md.recovery_cp = MaxSector; 715 716 for (i = 0; i < raid_devs; i++) 717 md_rdev_init(&rs->dev[i].rdev); 718 719 /* 720 * Remaining items to be initialized by further RAID params: 721 * rs->md.persistent 722 * rs->md.external 723 * rs->md.chunk_sectors 724 * rs->md.new_chunk_sectors 725 * rs->md.dev_sectors 726 */ 727 728 return rs; 729 } 730 731 static void raid_set_free(struct raid_set *rs) 732 { 733 int i; 734 735 if (rs->journal_dev.dev) { 736 md_rdev_clear(&rs->journal_dev.rdev); 737 dm_put_device(rs->ti, rs->journal_dev.dev); 738 } 739 740 for (i = 0; i < rs->raid_disks; i++) { 741 if (rs->dev[i].meta_dev) 742 dm_put_device(rs->ti, rs->dev[i].meta_dev); 743 md_rdev_clear(&rs->dev[i].rdev); 744 if (rs->dev[i].data_dev) 745 dm_put_device(rs->ti, rs->dev[i].data_dev); 746 } 747 748 kfree(rs); 749 } 750 751 /* 752 * For every device we have two words 753 * <meta_dev>: meta device name or '-' if missing 754 * <data_dev>: data device name or '-' if missing 755 * 756 * The following are permitted: 757 * - - 758 * - <data_dev> 759 * <meta_dev> <data_dev> 760 * 761 * The following is not allowed: 762 * <meta_dev> - 763 * 764 * This code parses those words. If there is a failure, 765 * the caller must use raid_set_free() to unwind the operations. 766 */ 767 static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as) 768 { 769 int i; 770 int rebuild = 0; 771 int metadata_available = 0; 772 int r = 0; 773 const char *arg; 774 775 /* Put off the number of raid devices argument to get to dev pairs */ 776 arg = dm_shift_arg(as); 777 if (!arg) 778 return -EINVAL; 779 780 for (i = 0; i < rs->raid_disks; i++) { 781 rs->dev[i].rdev.raid_disk = i; 782 783 rs->dev[i].meta_dev = NULL; 784 rs->dev[i].data_dev = NULL; 785 786 /* 787 * There are no offsets initially. 788 * Out of place reshape will set them accordingly. 789 */ 790 rs->dev[i].rdev.data_offset = 0; 791 rs->dev[i].rdev.new_data_offset = 0; 792 rs->dev[i].rdev.mddev = &rs->md; 793 794 arg = dm_shift_arg(as); 795 if (!arg) 796 return -EINVAL; 797 798 if (strcmp(arg, "-")) { 799 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table), 800 &rs->dev[i].meta_dev); 801 if (r) { 802 rs->ti->error = "RAID metadata device lookup failure"; 803 return r; 804 } 805 806 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL); 807 if (!rs->dev[i].rdev.sb_page) { 808 rs->ti->error = "Failed to allocate superblock page"; 809 return -ENOMEM; 810 } 811 } 812 813 arg = dm_shift_arg(as); 814 if (!arg) 815 return -EINVAL; 816 817 if (!strcmp(arg, "-")) { 818 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) && 819 (!rs->dev[i].rdev.recovery_offset)) { 820 rs->ti->error = "Drive designated for rebuild not specified"; 821 return -EINVAL; 822 } 823 824 if (rs->dev[i].meta_dev) { 825 rs->ti->error = "No data device supplied with metadata device"; 826 return -EINVAL; 827 } 828 829 continue; 830 } 831 832 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table), 833 &rs->dev[i].data_dev); 834 if (r) { 835 rs->ti->error = "RAID device lookup failure"; 836 return r; 837 } 838 839 if (rs->dev[i].meta_dev) { 840 metadata_available = 1; 841 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev; 842 } 843 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev; 844 list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks); 845 if (!test_bit(In_sync, &rs->dev[i].rdev.flags)) 846 rebuild++; 847 } 848 849 if (rs->journal_dev.dev) 850 list_add_tail(&rs->journal_dev.rdev.same_set, &rs->md.disks); 851 852 if (metadata_available) { 853 rs->md.external = 0; 854 rs->md.persistent = 1; 855 rs->md.major_version = 2; 856 } else if (rebuild && !rs->md.recovery_cp) { 857 /* 858 * Without metadata, we will not be able to tell if the array 859 * is in-sync or not - we must assume it is not. Therefore, 860 * it is impossible to rebuild a drive. 861 * 862 * Even if there is metadata, the on-disk information may 863 * indicate that the array is not in-sync and it will then 864 * fail at that time. 865 * 866 * User could specify 'nosync' option if desperate. 867 */ 868 rs->ti->error = "Unable to rebuild drive while array is not in-sync"; 869 return -EINVAL; 870 } 871 872 return 0; 873 } 874 875 /* 876 * validate_region_size 877 * @rs 878 * @region_size: region size in sectors. If 0, pick a size (4MiB default). 879 * 880 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size'). 881 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap. 882 * 883 * Returns: 0 on success, -EINVAL on failure. 884 */ 885 static int validate_region_size(struct raid_set *rs, unsigned long region_size) 886 { 887 unsigned long min_region_size = rs->ti->len / (1 << 21); 888 889 if (rs_is_raid0(rs)) 890 return 0; 891 892 if (!region_size) { 893 /* 894 * Choose a reasonable default. All figures in sectors. 895 */ 896 if (min_region_size > (1 << 13)) { 897 /* If not a power of 2, make it the next power of 2 */ 898 region_size = roundup_pow_of_two(min_region_size); 899 DMINFO("Choosing default region size of %lu sectors", 900 region_size); 901 } else { 902 DMINFO("Choosing default region size of 4MiB"); 903 region_size = 1 << 13; /* sectors */ 904 } 905 } else { 906 /* 907 * Validate user-supplied value. 908 */ 909 if (region_size > rs->ti->len) { 910 rs->ti->error = "Supplied region size is too large"; 911 return -EINVAL; 912 } 913 914 if (region_size < min_region_size) { 915 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)", 916 region_size, min_region_size); 917 rs->ti->error = "Supplied region size is too small"; 918 return -EINVAL; 919 } 920 921 if (!is_power_of_2(region_size)) { 922 rs->ti->error = "Region size is not a power of 2"; 923 return -EINVAL; 924 } 925 926 if (region_size < rs->md.chunk_sectors) { 927 rs->ti->error = "Region size is smaller than the chunk size"; 928 return -EINVAL; 929 } 930 } 931 932 /* 933 * Convert sectors to bytes. 934 */ 935 rs->md.bitmap_info.chunksize = to_bytes(region_size); 936 937 return 0; 938 } 939 940 /* 941 * validate_raid_redundancy 942 * @rs 943 * 944 * Determine if there are enough devices in the array that haven't 945 * failed (or are being rebuilt) to form a usable array. 946 * 947 * Returns: 0 on success, -EINVAL on failure. 948 */ 949 static int validate_raid_redundancy(struct raid_set *rs) 950 { 951 unsigned int i, rebuild_cnt = 0; 952 unsigned int rebuilds_per_group = 0, copies; 953 unsigned int group_size, last_group_start; 954 955 for (i = 0; i < rs->md.raid_disks; i++) 956 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) || 957 !rs->dev[i].rdev.sb_page) 958 rebuild_cnt++; 959 960 switch (rs->raid_type->level) { 961 case 0: 962 break; 963 case 1: 964 if (rebuild_cnt >= rs->md.raid_disks) 965 goto too_many; 966 break; 967 case 4: 968 case 5: 969 case 6: 970 if (rebuild_cnt > rs->raid_type->parity_devs) 971 goto too_many; 972 break; 973 case 10: 974 copies = raid10_md_layout_to_copies(rs->md.new_layout); 975 if (rebuild_cnt < copies) 976 break; 977 978 /* 979 * It is possible to have a higher rebuild count for RAID10, 980 * as long as the failed devices occur in different mirror 981 * groups (i.e. different stripes). 982 * 983 * When checking "near" format, make sure no adjacent devices 984 * have failed beyond what can be handled. In addition to the 985 * simple case where the number of devices is a multiple of the 986 * number of copies, we must also handle cases where the number 987 * of devices is not a multiple of the number of copies. 988 * E.g. dev1 dev2 dev3 dev4 dev5 989 * A A B B C 990 * C D D E E 991 */ 992 if (__is_raid10_near(rs->md.new_layout)) { 993 for (i = 0; i < rs->md.raid_disks; i++) { 994 if (!(i % copies)) 995 rebuilds_per_group = 0; 996 if ((!rs->dev[i].rdev.sb_page || 997 !test_bit(In_sync, &rs->dev[i].rdev.flags)) && 998 (++rebuilds_per_group >= copies)) 999 goto too_many; 1000 } 1001 break; 1002 } 1003 1004 /* 1005 * When checking "far" and "offset" formats, we need to ensure 1006 * that the device that holds its copy is not also dead or 1007 * being rebuilt. (Note that "far" and "offset" formats only 1008 * support two copies right now. These formats also only ever 1009 * use the 'use_far_sets' variant.) 1010 * 1011 * This check is somewhat complicated by the need to account 1012 * for arrays that are not a multiple of (far) copies. This 1013 * results in the need to treat the last (potentially larger) 1014 * set differently. 1015 */ 1016 group_size = (rs->md.raid_disks / copies); 1017 last_group_start = (rs->md.raid_disks / group_size) - 1; 1018 last_group_start *= group_size; 1019 for (i = 0; i < rs->md.raid_disks; i++) { 1020 if (!(i % copies) && !(i > last_group_start)) 1021 rebuilds_per_group = 0; 1022 if ((!rs->dev[i].rdev.sb_page || 1023 !test_bit(In_sync, &rs->dev[i].rdev.flags)) && 1024 (++rebuilds_per_group >= copies)) 1025 goto too_many; 1026 } 1027 break; 1028 default: 1029 if (rebuild_cnt) 1030 return -EINVAL; 1031 } 1032 1033 return 0; 1034 1035 too_many: 1036 return -EINVAL; 1037 } 1038 1039 /* 1040 * Possible arguments are... 1041 * <chunk_size> [optional_args] 1042 * 1043 * Argument definitions 1044 * <chunk_size> The number of sectors per disk that 1045 * will form the "stripe" 1046 * [[no]sync] Force or prevent recovery of the 1047 * entire array 1048 * [rebuild <idx>] Rebuild the drive indicated by the index 1049 * [daemon_sleep <ms>] Time between bitmap daemon work to 1050 * clear bits 1051 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization 1052 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization 1053 * [write_mostly <idx>] Indicate a write mostly drive via index 1054 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm) 1055 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs 1056 * [region_size <sectors>] Defines granularity of bitmap 1057 * [journal_dev <dev>] raid4/5/6 journaling deviice 1058 * (i.e. write hole closing log) 1059 * 1060 * RAID10-only options: 1061 * [raid10_copies <# copies>] Number of copies. (Default: 2) 1062 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near) 1063 */ 1064 static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as, 1065 unsigned int num_raid_params) 1066 { 1067 int value, raid10_format = ALGORITHM_RAID10_DEFAULT; 1068 unsigned int raid10_copies = 2; 1069 unsigned int i, write_mostly = 0; 1070 unsigned int region_size = 0; 1071 sector_t max_io_len; 1072 const char *arg, *key; 1073 struct raid_dev *rd; 1074 struct raid_type *rt = rs->raid_type; 1075 1076 arg = dm_shift_arg(as); 1077 num_raid_params--; /* Account for chunk_size argument */ 1078 1079 if (kstrtoint(arg, 10, &value) < 0) { 1080 rs->ti->error = "Bad numerical argument given for chunk_size"; 1081 return -EINVAL; 1082 } 1083 1084 /* 1085 * First, parse the in-order required arguments 1086 * "chunk_size" is the only argument of this type. 1087 */ 1088 if (rt_is_raid1(rt)) { 1089 if (value) 1090 DMERR("Ignoring chunk size parameter for RAID 1"); 1091 value = 0; 1092 } else if (!is_power_of_2(value)) { 1093 rs->ti->error = "Chunk size must be a power of 2"; 1094 return -EINVAL; 1095 } else if (value < 8) { 1096 rs->ti->error = "Chunk size value is too small"; 1097 return -EINVAL; 1098 } 1099 1100 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value; 1101 1102 /* 1103 * We set each individual device as In_sync with a completed 1104 * 'recovery_offset'. If there has been a device failure or 1105 * replacement then one of the following cases applies: 1106 * 1107 * 1) User specifies 'rebuild'. 1108 * - Device is reset when param is read. 1109 * 2) A new device is supplied. 1110 * - No matching superblock found, resets device. 1111 * 3) Device failure was transient and returns on reload. 1112 * - Failure noticed, resets device for bitmap replay. 1113 * 4) Device hadn't completed recovery after previous failure. 1114 * - Superblock is read and overrides recovery_offset. 1115 * 1116 * What is found in the superblocks of the devices is always 1117 * authoritative, unless 'rebuild' or '[no]sync' was specified. 1118 */ 1119 for (i = 0; i < rs->raid_disks; i++) { 1120 set_bit(In_sync, &rs->dev[i].rdev.flags); 1121 rs->dev[i].rdev.recovery_offset = MaxSector; 1122 } 1123 1124 /* 1125 * Second, parse the unordered optional arguments 1126 */ 1127 for (i = 0; i < num_raid_params; i++) { 1128 key = dm_shift_arg(as); 1129 if (!key) { 1130 rs->ti->error = "Not enough raid parameters given"; 1131 return -EINVAL; 1132 } 1133 1134 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC))) { 1135 if (test_and_set_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) { 1136 rs->ti->error = "Only one 'nosync' argument allowed"; 1137 return -EINVAL; 1138 } 1139 continue; 1140 } 1141 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_SYNC))) { 1142 if (test_and_set_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) { 1143 rs->ti->error = "Only one 'sync' argument allowed"; 1144 return -EINVAL; 1145 } 1146 continue; 1147 } 1148 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_USE_NEAR_SETS))) { 1149 if (test_and_set_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) { 1150 rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed"; 1151 return -EINVAL; 1152 } 1153 continue; 1154 } 1155 1156 arg = dm_shift_arg(as); 1157 i++; /* Account for the argument pairs */ 1158 if (!arg) { 1159 rs->ti->error = "Wrong number of raid parameters given"; 1160 return -EINVAL; 1161 } 1162 1163 /* 1164 * Parameters that take a string value are checked here. 1165 */ 1166 /* "raid10_format {near|offset|far} */ 1167 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT))) { 1168 if (test_and_set_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) { 1169 rs->ti->error = "Only one 'raid10_format' argument pair allowed"; 1170 return -EINVAL; 1171 } 1172 if (!rt_is_raid10(rt)) { 1173 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type"; 1174 return -EINVAL; 1175 } 1176 raid10_format = raid10_name_to_format(arg); 1177 if (raid10_format < 0) { 1178 rs->ti->error = "Invalid 'raid10_format' value given"; 1179 return raid10_format; 1180 } 1181 continue; 1182 } 1183 1184 /* "journal_dev dev" */ 1185 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV))) { 1186 int r; 1187 struct md_rdev *jdev; 1188 1189 if (test_and_set_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) { 1190 rs->ti->error = "Only one raid4/5/6 set journaling device allowed"; 1191 return -EINVAL; 1192 } 1193 if (!rt_is_raid456(rt)) { 1194 rs->ti->error = "'journal_dev' is an invalid parameter for this RAID type"; 1195 return -EINVAL; 1196 } 1197 r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table), 1198 &rs->journal_dev.dev); 1199 if (r) { 1200 rs->ti->error = "raid4/5/6 journal device lookup failure"; 1201 return r; 1202 } 1203 jdev = &rs->journal_dev.rdev; 1204 md_rdev_init(jdev); 1205 jdev->mddev = &rs->md; 1206 jdev->bdev = rs->journal_dev.dev->bdev; 1207 jdev->sectors = to_sector(i_size_read(jdev->bdev->bd_inode)); 1208 if (jdev->sectors < MIN_RAID456_JOURNAL_SPACE) { 1209 rs->ti->error = "No space for raid4/5/6 journal"; 1210 return -ENOSPC; 1211 } 1212 set_bit(Journal, &jdev->flags); 1213 continue; 1214 } 1215 1216 /* 1217 * Parameters with number values from here on. 1218 */ 1219 if (kstrtoint(arg, 10, &value) < 0) { 1220 rs->ti->error = "Bad numerical argument given in raid params"; 1221 return -EINVAL; 1222 } 1223 1224 if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) { 1225 /* 1226 * "rebuild" is being passed in by userspace to provide 1227 * indexes of replaced devices and to set up additional 1228 * devices on raid level takeover. 1229 */ 1230 if (!__within_range(value, 0, rs->raid_disks - 1)) { 1231 rs->ti->error = "Invalid rebuild index given"; 1232 return -EINVAL; 1233 } 1234 1235 if (test_and_set_bit(value, (void *) rs->rebuild_disks)) { 1236 rs->ti->error = "rebuild for this index already given"; 1237 return -EINVAL; 1238 } 1239 1240 rd = rs->dev + value; 1241 clear_bit(In_sync, &rd->rdev.flags); 1242 clear_bit(Faulty, &rd->rdev.flags); 1243 rd->rdev.recovery_offset = 0; 1244 set_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags); 1245 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY))) { 1246 if (!rt_is_raid1(rt)) { 1247 rs->ti->error = "write_mostly option is only valid for RAID1"; 1248 return -EINVAL; 1249 } 1250 1251 if (!__within_range(value, 0, rs->md.raid_disks - 1)) { 1252 rs->ti->error = "Invalid write_mostly index given"; 1253 return -EINVAL; 1254 } 1255 1256 write_mostly++; 1257 set_bit(WriteMostly, &rs->dev[value].rdev.flags); 1258 set_bit(__CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags); 1259 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) { 1260 if (!rt_is_raid1(rt)) { 1261 rs->ti->error = "max_write_behind option is only valid for RAID1"; 1262 return -EINVAL; 1263 } 1264 1265 if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) { 1266 rs->ti->error = "Only one max_write_behind argument pair allowed"; 1267 return -EINVAL; 1268 } 1269 1270 /* 1271 * In device-mapper, we specify things in sectors, but 1272 * MD records this value in kB 1273 */ 1274 value /= 2; 1275 if (value > COUNTER_MAX) { 1276 rs->ti->error = "Max write-behind limit out of range"; 1277 return -EINVAL; 1278 } 1279 1280 rs->md.bitmap_info.max_write_behind = value; 1281 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP))) { 1282 if (test_and_set_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) { 1283 rs->ti->error = "Only one daemon_sleep argument pair allowed"; 1284 return -EINVAL; 1285 } 1286 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) { 1287 rs->ti->error = "daemon sleep period out of range"; 1288 return -EINVAL; 1289 } 1290 rs->md.bitmap_info.daemon_sleep = value; 1291 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET))) { 1292 /* Userspace passes new data_offset after having extended the the data image LV */ 1293 if (test_and_set_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) { 1294 rs->ti->error = "Only one data_offset argument pair allowed"; 1295 return -EINVAL; 1296 } 1297 /* Ensure sensible data offset */ 1298 if (value < 0 || 1299 (value && (value < MIN_FREE_RESHAPE_SPACE || value % to_sector(PAGE_SIZE)))) { 1300 rs->ti->error = "Bogus data_offset value"; 1301 return -EINVAL; 1302 } 1303 rs->data_offset = value; 1304 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS))) { 1305 /* Define the +/-# of disks to add to/remove from the given raid set */ 1306 if (test_and_set_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) { 1307 rs->ti->error = "Only one delta_disks argument pair allowed"; 1308 return -EINVAL; 1309 } 1310 /* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */ 1311 if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) { 1312 rs->ti->error = "Too many delta_disk requested"; 1313 return -EINVAL; 1314 } 1315 1316 rs->delta_disks = value; 1317 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE))) { 1318 if (test_and_set_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) { 1319 rs->ti->error = "Only one stripe_cache argument pair allowed"; 1320 return -EINVAL; 1321 } 1322 1323 if (!rt_is_raid456(rt)) { 1324 rs->ti->error = "Inappropriate argument: stripe_cache"; 1325 return -EINVAL; 1326 } 1327 1328 rs->stripe_cache_entries = value; 1329 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) { 1330 if (test_and_set_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) { 1331 rs->ti->error = "Only one min_recovery_rate argument pair allowed"; 1332 return -EINVAL; 1333 } 1334 if (value > INT_MAX) { 1335 rs->ti->error = "min_recovery_rate out of range"; 1336 return -EINVAL; 1337 } 1338 rs->md.sync_speed_min = (int)value; 1339 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) { 1340 if (test_and_set_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags)) { 1341 rs->ti->error = "Only one max_recovery_rate argument pair allowed"; 1342 return -EINVAL; 1343 } 1344 if (value > INT_MAX) { 1345 rs->ti->error = "max_recovery_rate out of range"; 1346 return -EINVAL; 1347 } 1348 rs->md.sync_speed_max = (int)value; 1349 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE))) { 1350 if (test_and_set_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) { 1351 rs->ti->error = "Only one region_size argument pair allowed"; 1352 return -EINVAL; 1353 } 1354 1355 region_size = value; 1356 rs->requested_bitmap_chunk_sectors = value; 1357 } else if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES))) { 1358 if (test_and_set_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) { 1359 rs->ti->error = "Only one raid10_copies argument pair allowed"; 1360 return -EINVAL; 1361 } 1362 1363 if (!__within_range(value, 2, rs->md.raid_disks)) { 1364 rs->ti->error = "Bad value for 'raid10_copies'"; 1365 return -EINVAL; 1366 } 1367 1368 raid10_copies = value; 1369 } else { 1370 DMERR("Unable to parse RAID parameter: %s", key); 1371 rs->ti->error = "Unable to parse RAID parameter"; 1372 return -EINVAL; 1373 } 1374 } 1375 1376 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) && 1377 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) { 1378 rs->ti->error = "sync and nosync are mutually exclusive"; 1379 return -EINVAL; 1380 } 1381 1382 if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) && 1383 (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) || 1384 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))) { 1385 rs->ti->error = "sync/nosync and rebuild are mutually exclusive"; 1386 return -EINVAL; 1387 } 1388 1389 if (write_mostly >= rs->md.raid_disks) { 1390 rs->ti->error = "Can't set all raid1 devices to write_mostly"; 1391 return -EINVAL; 1392 } 1393 1394 if (validate_region_size(rs, region_size)) 1395 return -EINVAL; 1396 1397 if (rs->md.chunk_sectors) 1398 max_io_len = rs->md.chunk_sectors; 1399 else 1400 max_io_len = region_size; 1401 1402 if (dm_set_target_max_io_len(rs->ti, max_io_len)) 1403 return -EINVAL; 1404 1405 if (rt_is_raid10(rt)) { 1406 if (raid10_copies > rs->md.raid_disks) { 1407 rs->ti->error = "Not enough devices to satisfy specification"; 1408 return -EINVAL; 1409 } 1410 1411 rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies); 1412 if (rs->md.new_layout < 0) { 1413 rs->ti->error = "Error getting raid10 format"; 1414 return rs->md.new_layout; 1415 } 1416 1417 rt = get_raid_type_by_ll(10, rs->md.new_layout); 1418 if (!rt) { 1419 rs->ti->error = "Failed to recognize new raid10 layout"; 1420 return -EINVAL; 1421 } 1422 1423 if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT || 1424 rt->algorithm == ALGORITHM_RAID10_NEAR) && 1425 test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) { 1426 rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible"; 1427 return -EINVAL; 1428 } 1429 } 1430 1431 rs->raid10_copies = raid10_copies; 1432 1433 /* Assume there are no metadata devices until the drives are parsed */ 1434 rs->md.persistent = 0; 1435 rs->md.external = 1; 1436 1437 /* Check, if any invalid ctr arguments have been passed in for the raid level */ 1438 return rs_check_for_valid_flags(rs); 1439 } 1440 1441 /* Set raid4/5/6 cache size */ 1442 static int rs_set_raid456_stripe_cache(struct raid_set *rs) 1443 { 1444 int r; 1445 struct r5conf *conf; 1446 struct mddev *mddev = &rs->md; 1447 uint32_t min_stripes = max(mddev->chunk_sectors, mddev->new_chunk_sectors) / 2; 1448 uint32_t nr_stripes = rs->stripe_cache_entries; 1449 1450 if (!rt_is_raid456(rs->raid_type)) { 1451 rs->ti->error = "Inappropriate raid level; cannot change stripe_cache size"; 1452 return -EINVAL; 1453 } 1454 1455 if (nr_stripes < min_stripes) { 1456 DMINFO("Adjusting requested %u stripe cache entries to %u to suit stripe size", 1457 nr_stripes, min_stripes); 1458 nr_stripes = min_stripes; 1459 } 1460 1461 conf = mddev->private; 1462 if (!conf) { 1463 rs->ti->error = "Cannot change stripe_cache size on inactive RAID set"; 1464 return -EINVAL; 1465 } 1466 1467 /* Try setting number of stripes in raid456 stripe cache */ 1468 if (conf->min_nr_stripes != nr_stripes) { 1469 r = raid5_set_cache_size(mddev, nr_stripes); 1470 if (r) { 1471 rs->ti->error = "Failed to set raid4/5/6 stripe cache size"; 1472 return r; 1473 } 1474 1475 DMINFO("%u stripe cache entries", nr_stripes); 1476 } 1477 1478 return 0; 1479 } 1480 1481 /* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */ 1482 static unsigned int mddev_data_stripes(struct raid_set *rs) 1483 { 1484 return rs->md.raid_disks - rs->raid_type->parity_devs; 1485 } 1486 1487 /* Return # of data stripes of @rs (i.e. as of ctr) */ 1488 static unsigned int rs_data_stripes(struct raid_set *rs) 1489 { 1490 return rs->raid_disks - rs->raid_type->parity_devs; 1491 } 1492 1493 /* 1494 * Retrieve rdev->sectors from any valid raid device of @rs 1495 * to allow userpace to pass in arbitray "- -" device tupples. 1496 */ 1497 static sector_t __rdev_sectors(struct raid_set *rs) 1498 { 1499 int i; 1500 1501 for (i = 0; i < rs->md.raid_disks; i++) { 1502 struct md_rdev *rdev = &rs->dev[i].rdev; 1503 1504 if (!test_bit(Journal, &rdev->flags) && 1505 rdev->bdev && rdev->sectors) 1506 return rdev->sectors; 1507 } 1508 1509 BUG(); /* Constructor ensures we got some. */ 1510 } 1511 1512 /* Calculate the sectors per device and per array used for @rs */ 1513 static int rs_set_dev_and_array_sectors(struct raid_set *rs, bool use_mddev) 1514 { 1515 int delta_disks; 1516 unsigned int data_stripes; 1517 struct mddev *mddev = &rs->md; 1518 struct md_rdev *rdev; 1519 sector_t array_sectors = rs->ti->len, dev_sectors = rs->ti->len; 1520 1521 if (use_mddev) { 1522 delta_disks = mddev->delta_disks; 1523 data_stripes = mddev_data_stripes(rs); 1524 } else { 1525 delta_disks = rs->delta_disks; 1526 data_stripes = rs_data_stripes(rs); 1527 } 1528 1529 /* Special raid1 case w/o delta_disks support (yet) */ 1530 if (rt_is_raid1(rs->raid_type)) 1531 ; 1532 else if (rt_is_raid10(rs->raid_type)) { 1533 if (rs->raid10_copies < 2 || 1534 delta_disks < 0) { 1535 rs->ti->error = "Bogus raid10 data copies or delta disks"; 1536 return -EINVAL; 1537 } 1538 1539 dev_sectors *= rs->raid10_copies; 1540 if (sector_div(dev_sectors, data_stripes)) 1541 goto bad; 1542 1543 array_sectors = (data_stripes + delta_disks) * dev_sectors; 1544 if (sector_div(array_sectors, rs->raid10_copies)) 1545 goto bad; 1546 1547 } else if (sector_div(dev_sectors, data_stripes)) 1548 goto bad; 1549 1550 else 1551 /* Striped layouts */ 1552 array_sectors = (data_stripes + delta_disks) * dev_sectors; 1553 1554 rdev_for_each(rdev, mddev) 1555 if (!test_bit(Journal, &rdev->flags)) 1556 rdev->sectors = dev_sectors; 1557 1558 mddev->array_sectors = array_sectors; 1559 mddev->dev_sectors = dev_sectors; 1560 1561 return 0; 1562 bad: 1563 rs->ti->error = "Target length not divisible by number of data devices"; 1564 return -EINVAL; 1565 } 1566 1567 /* Setup recovery on @rs */ 1568 static void __rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors) 1569 { 1570 /* raid0 does not recover */ 1571 if (rs_is_raid0(rs)) 1572 rs->md.recovery_cp = MaxSector; 1573 /* 1574 * A raid6 set has to be recovered either 1575 * completely or for the grown part to 1576 * ensure proper parity and Q-Syndrome 1577 */ 1578 else if (rs_is_raid6(rs)) 1579 rs->md.recovery_cp = dev_sectors; 1580 /* 1581 * Other raid set types may skip recovery 1582 * depending on the 'nosync' flag. 1583 */ 1584 else 1585 rs->md.recovery_cp = test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags) 1586 ? MaxSector : dev_sectors; 1587 } 1588 1589 /* Setup recovery on @rs based on raid type, device size and 'nosync' flag */ 1590 static void rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors) 1591 { 1592 if (!dev_sectors) 1593 /* New raid set or 'sync' flag provided */ 1594 __rs_setup_recovery(rs, 0); 1595 else if (dev_sectors == MaxSector) 1596 /* Prevent recovery */ 1597 __rs_setup_recovery(rs, MaxSector); 1598 else if (__rdev_sectors(rs) < dev_sectors) 1599 /* Grown raid set */ 1600 __rs_setup_recovery(rs, __rdev_sectors(rs)); 1601 else 1602 __rs_setup_recovery(rs, MaxSector); 1603 } 1604 1605 static void do_table_event(struct work_struct *ws) 1606 { 1607 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work); 1608 1609 smp_rmb(); /* Make sure we access most actual mddev properties */ 1610 if (!rs_is_reshaping(rs)) 1611 rs_set_capacity(rs); 1612 dm_table_event(rs->ti->table); 1613 } 1614 1615 static int raid_is_congested(struct dm_target_callbacks *cb, int bits) 1616 { 1617 struct raid_set *rs = container_of(cb, struct raid_set, callbacks); 1618 1619 return mddev_congested(&rs->md, bits); 1620 } 1621 1622 /* 1623 * Make sure a valid takover (level switch) is being requested on @rs 1624 * 1625 * Conversions of raid sets from one MD personality to another 1626 * have to conform to restrictions which are enforced here. 1627 */ 1628 static int rs_check_takeover(struct raid_set *rs) 1629 { 1630 struct mddev *mddev = &rs->md; 1631 unsigned int near_copies; 1632 1633 if (rs->md.degraded) { 1634 rs->ti->error = "Can't takeover degraded raid set"; 1635 return -EPERM; 1636 } 1637 1638 if (rs_is_reshaping(rs)) { 1639 rs->ti->error = "Can't takeover reshaping raid set"; 1640 return -EPERM; 1641 } 1642 1643 switch (mddev->level) { 1644 case 0: 1645 /* raid0 -> raid1/5 with one disk */ 1646 if ((mddev->new_level == 1 || mddev->new_level == 5) && 1647 mddev->raid_disks == 1) 1648 return 0; 1649 1650 /* raid0 -> raid10 */ 1651 if (mddev->new_level == 10 && 1652 !(rs->raid_disks % mddev->raid_disks)) 1653 return 0; 1654 1655 /* raid0 with multiple disks -> raid4/5/6 */ 1656 if (__within_range(mddev->new_level, 4, 6) && 1657 mddev->new_layout == ALGORITHM_PARITY_N && 1658 mddev->raid_disks > 1) 1659 return 0; 1660 1661 break; 1662 1663 case 10: 1664 /* Can't takeover raid10_offset! */ 1665 if (__is_raid10_offset(mddev->layout)) 1666 break; 1667 1668 near_copies = __raid10_near_copies(mddev->layout); 1669 1670 /* raid10* -> raid0 */ 1671 if (mddev->new_level == 0) { 1672 /* Can takeover raid10_near with raid disks divisable by data copies! */ 1673 if (near_copies > 1 && 1674 !(mddev->raid_disks % near_copies)) { 1675 mddev->raid_disks /= near_copies; 1676 mddev->delta_disks = mddev->raid_disks; 1677 return 0; 1678 } 1679 1680 /* Can takeover raid10_far */ 1681 if (near_copies == 1 && 1682 __raid10_far_copies(mddev->layout) > 1) 1683 return 0; 1684 1685 break; 1686 } 1687 1688 /* raid10_{near,far} -> raid1 */ 1689 if (mddev->new_level == 1 && 1690 max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks) 1691 return 0; 1692 1693 /* raid10_{near,far} with 2 disks -> raid4/5 */ 1694 if (__within_range(mddev->new_level, 4, 5) && 1695 mddev->raid_disks == 2) 1696 return 0; 1697 break; 1698 1699 case 1: 1700 /* raid1 with 2 disks -> raid4/5 */ 1701 if (__within_range(mddev->new_level, 4, 5) && 1702 mddev->raid_disks == 2) { 1703 mddev->degraded = 1; 1704 return 0; 1705 } 1706 1707 /* raid1 -> raid0 */ 1708 if (mddev->new_level == 0 && 1709 mddev->raid_disks == 1) 1710 return 0; 1711 1712 /* raid1 -> raid10 */ 1713 if (mddev->new_level == 10) 1714 return 0; 1715 break; 1716 1717 case 4: 1718 /* raid4 -> raid0 */ 1719 if (mddev->new_level == 0) 1720 return 0; 1721 1722 /* raid4 -> raid1/5 with 2 disks */ 1723 if ((mddev->new_level == 1 || mddev->new_level == 5) && 1724 mddev->raid_disks == 2) 1725 return 0; 1726 1727 /* raid4 -> raid5/6 with parity N */ 1728 if (__within_range(mddev->new_level, 5, 6) && 1729 mddev->layout == ALGORITHM_PARITY_N) 1730 return 0; 1731 break; 1732 1733 case 5: 1734 /* raid5 with parity N -> raid0 */ 1735 if (mddev->new_level == 0 && 1736 mddev->layout == ALGORITHM_PARITY_N) 1737 return 0; 1738 1739 /* raid5 with parity N -> raid4 */ 1740 if (mddev->new_level == 4 && 1741 mddev->layout == ALGORITHM_PARITY_N) 1742 return 0; 1743 1744 /* raid5 with 2 disks -> raid1/4/10 */ 1745 if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) && 1746 mddev->raid_disks == 2) 1747 return 0; 1748 1749 /* raid5_* -> raid6_*_6 with Q-Syndrome N (e.g. raid5_ra -> raid6_ra_6 */ 1750 if (mddev->new_level == 6 && 1751 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) || 1752 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC_6, ALGORITHM_RIGHT_SYMMETRIC_6))) 1753 return 0; 1754 break; 1755 1756 case 6: 1757 /* raid6 with parity N -> raid0 */ 1758 if (mddev->new_level == 0 && 1759 mddev->layout == ALGORITHM_PARITY_N) 1760 return 0; 1761 1762 /* raid6 with parity N -> raid4 */ 1763 if (mddev->new_level == 4 && 1764 mddev->layout == ALGORITHM_PARITY_N) 1765 return 0; 1766 1767 /* raid6_*_n with Q-Syndrome N -> raid5_* */ 1768 if (mddev->new_level == 5 && 1769 ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) || 1770 __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC))) 1771 return 0; 1772 1773 default: 1774 break; 1775 } 1776 1777 rs->ti->error = "takeover not possible"; 1778 return -EINVAL; 1779 } 1780 1781 /* True if @rs requested to be taken over */ 1782 static bool rs_takeover_requested(struct raid_set *rs) 1783 { 1784 return rs->md.new_level != rs->md.level; 1785 } 1786 1787 /* True if @rs is requested to reshape by ctr */ 1788 static bool rs_reshape_requested(struct raid_set *rs) 1789 { 1790 bool change; 1791 struct mddev *mddev = &rs->md; 1792 1793 if (rs_takeover_requested(rs)) 1794 return false; 1795 1796 if (!mddev->level) 1797 return false; 1798 1799 change = mddev->new_layout != mddev->layout || 1800 mddev->new_chunk_sectors != mddev->chunk_sectors || 1801 rs->delta_disks; 1802 1803 /* Historical case to support raid1 reshape without delta disks */ 1804 if (mddev->level == 1) { 1805 if (rs->delta_disks) 1806 return !!rs->delta_disks; 1807 1808 return !change && 1809 mddev->raid_disks != rs->raid_disks; 1810 } 1811 1812 if (mddev->level == 10) 1813 return change && 1814 !__is_raid10_far(mddev->new_layout) && 1815 rs->delta_disks >= 0; 1816 1817 return change; 1818 } 1819 1820 /* Features */ 1821 #define FEATURE_FLAG_SUPPORTS_V190 0x1 /* Supports extended superblock */ 1822 1823 /* State flags for sb->flags */ 1824 #define SB_FLAG_RESHAPE_ACTIVE 0x1 1825 #define SB_FLAG_RESHAPE_BACKWARDS 0x2 1826 1827 /* 1828 * This structure is never routinely used by userspace, unlike md superblocks. 1829 * Devices with this superblock should only ever be accessed via device-mapper. 1830 */ 1831 #define DM_RAID_MAGIC 0x64526D44 1832 struct dm_raid_superblock { 1833 __le32 magic; /* "DmRd" */ 1834 __le32 compat_features; /* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */ 1835 1836 __le32 num_devices; /* Number of devices in this raid set. (Max 64) */ 1837 __le32 array_position; /* The position of this drive in the raid set */ 1838 1839 __le64 events; /* Incremented by md when superblock updated */ 1840 __le64 failed_devices; /* Pre 1.9.0 part of bit field of devices to */ 1841 /* indicate failures (see extension below) */ 1842 1843 /* 1844 * This offset tracks the progress of the repair or replacement of 1845 * an individual drive. 1846 */ 1847 __le64 disk_recovery_offset; 1848 1849 /* 1850 * This offset tracks the progress of the initial raid set 1851 * synchronisation/parity calculation. 1852 */ 1853 __le64 array_resync_offset; 1854 1855 /* 1856 * raid characteristics 1857 */ 1858 __le32 level; 1859 __le32 layout; 1860 __le32 stripe_sectors; 1861 1862 /******************************************************************** 1863 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!! 1864 * 1865 * FEATURE_FLAG_SUPPORTS_V190 in the features member indicates that those exist 1866 */ 1867 1868 __le32 flags; /* Flags defining array states for reshaping */ 1869 1870 /* 1871 * This offset tracks the progress of a raid 1872 * set reshape in order to be able to restart it 1873 */ 1874 __le64 reshape_position; 1875 1876 /* 1877 * These define the properties of the array in case of an interrupted reshape 1878 */ 1879 __le32 new_level; 1880 __le32 new_layout; 1881 __le32 new_stripe_sectors; 1882 __le32 delta_disks; 1883 1884 __le64 array_sectors; /* Array size in sectors */ 1885 1886 /* 1887 * Sector offsets to data on devices (reshaping). 1888 * Needed to support out of place reshaping, thus 1889 * not writing over any stripes whilst converting 1890 * them from old to new layout 1891 */ 1892 __le64 data_offset; 1893 __le64 new_data_offset; 1894 1895 __le64 sectors; /* Used device size in sectors */ 1896 1897 /* 1898 * Additonal Bit field of devices indicating failures to support 1899 * up to 256 devices with the 1.9.0 on-disk metadata format 1900 */ 1901 __le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1]; 1902 1903 __le32 incompat_features; /* Used to indicate any incompatible features */ 1904 1905 /* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */ 1906 } __packed; 1907 1908 /* 1909 * Check for reshape constraints on raid set @rs: 1910 * 1911 * - reshape function non-existent 1912 * - degraded set 1913 * - ongoing recovery 1914 * - ongoing reshape 1915 * 1916 * Returns 0 if none or -EPERM if given constraint 1917 * and error message reference in @errmsg 1918 */ 1919 static int rs_check_reshape(struct raid_set *rs) 1920 { 1921 struct mddev *mddev = &rs->md; 1922 1923 if (!mddev->pers || !mddev->pers->check_reshape) 1924 rs->ti->error = "Reshape not supported"; 1925 else if (mddev->degraded) 1926 rs->ti->error = "Can't reshape degraded raid set"; 1927 else if (rs_is_recovering(rs)) 1928 rs->ti->error = "Convert request on recovering raid set prohibited"; 1929 else if (rs_is_reshaping(rs)) 1930 rs->ti->error = "raid set already reshaping!"; 1931 else if (!(rs_is_raid1(rs) || rs_is_raid10(rs) || rs_is_raid456(rs))) 1932 rs->ti->error = "Reshaping only supported for raid1/4/5/6/10"; 1933 else 1934 return 0; 1935 1936 return -EPERM; 1937 } 1938 1939 static int read_disk_sb(struct md_rdev *rdev, int size, bool force_reload) 1940 { 1941 BUG_ON(!rdev->sb_page); 1942 1943 if (rdev->sb_loaded && !force_reload) 1944 return 0; 1945 1946 rdev->sb_loaded = 0; 1947 1948 if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) { 1949 DMERR("Failed to read superblock of device at position %d", 1950 rdev->raid_disk); 1951 md_error(rdev->mddev, rdev); 1952 set_bit(Faulty, &rdev->flags); 1953 return -EIO; 1954 } 1955 1956 rdev->sb_loaded = 1; 1957 1958 return 0; 1959 } 1960 1961 static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices) 1962 { 1963 failed_devices[0] = le64_to_cpu(sb->failed_devices); 1964 memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices)); 1965 1966 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) { 1967 int i = ARRAY_SIZE(sb->extended_failed_devices); 1968 1969 while (i--) 1970 failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]); 1971 } 1972 } 1973 1974 static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices) 1975 { 1976 int i = ARRAY_SIZE(sb->extended_failed_devices); 1977 1978 sb->failed_devices = cpu_to_le64(failed_devices[0]); 1979 while (i--) 1980 sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]); 1981 } 1982 1983 /* 1984 * Synchronize the superblock members with the raid set properties 1985 * 1986 * All superblock data is little endian. 1987 */ 1988 static void super_sync(struct mddev *mddev, struct md_rdev *rdev) 1989 { 1990 bool update_failed_devices = false; 1991 unsigned int i; 1992 uint64_t failed_devices[DISKS_ARRAY_ELEMS]; 1993 struct dm_raid_superblock *sb; 1994 struct raid_set *rs = container_of(mddev, struct raid_set, md); 1995 1996 /* No metadata device, no superblock */ 1997 if (!rdev->meta_bdev) 1998 return; 1999 2000 BUG_ON(!rdev->sb_page); 2001 2002 sb = page_address(rdev->sb_page); 2003 2004 sb_retrieve_failed_devices(sb, failed_devices); 2005 2006 for (i = 0; i < rs->raid_disks; i++) 2007 if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) { 2008 update_failed_devices = true; 2009 set_bit(i, (void *) failed_devices); 2010 } 2011 2012 if (update_failed_devices) 2013 sb_update_failed_devices(sb, failed_devices); 2014 2015 sb->magic = cpu_to_le32(DM_RAID_MAGIC); 2016 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190); 2017 2018 sb->num_devices = cpu_to_le32(mddev->raid_disks); 2019 sb->array_position = cpu_to_le32(rdev->raid_disk); 2020 2021 sb->events = cpu_to_le64(mddev->events); 2022 2023 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset); 2024 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp); 2025 2026 sb->level = cpu_to_le32(mddev->level); 2027 sb->layout = cpu_to_le32(mddev->layout); 2028 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors); 2029 2030 sb->new_level = cpu_to_le32(mddev->new_level); 2031 sb->new_layout = cpu_to_le32(mddev->new_layout); 2032 sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors); 2033 2034 sb->delta_disks = cpu_to_le32(mddev->delta_disks); 2035 2036 smp_rmb(); /* Make sure we access most recent reshape position */ 2037 sb->reshape_position = cpu_to_le64(mddev->reshape_position); 2038 if (le64_to_cpu(sb->reshape_position) != MaxSector) { 2039 /* Flag ongoing reshape */ 2040 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE); 2041 2042 if (mddev->delta_disks < 0 || mddev->reshape_backwards) 2043 sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS); 2044 } else { 2045 /* Clear reshape flags */ 2046 sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS)); 2047 } 2048 2049 sb->array_sectors = cpu_to_le64(mddev->array_sectors); 2050 sb->data_offset = cpu_to_le64(rdev->data_offset); 2051 sb->new_data_offset = cpu_to_le64(rdev->new_data_offset); 2052 sb->sectors = cpu_to_le64(rdev->sectors); 2053 sb->incompat_features = cpu_to_le32(0); 2054 2055 /* Zero out the rest of the payload after the size of the superblock */ 2056 memset(sb + 1, 0, rdev->sb_size - sizeof(*sb)); 2057 } 2058 2059 /* 2060 * super_load 2061 * 2062 * This function creates a superblock if one is not found on the device 2063 * and will decide which superblock to use if there's a choice. 2064 * 2065 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise 2066 */ 2067 static int super_load(struct md_rdev *rdev, struct md_rdev *refdev) 2068 { 2069 int r; 2070 struct dm_raid_superblock *sb; 2071 struct dm_raid_superblock *refsb; 2072 uint64_t events_sb, events_refsb; 2073 2074 rdev->sb_start = 0; 2075 rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev); 2076 if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) { 2077 DMERR("superblock size of a logical block is no longer valid"); 2078 return -EINVAL; 2079 } 2080 2081 r = read_disk_sb(rdev, rdev->sb_size, false); 2082 if (r) 2083 return r; 2084 2085 sb = page_address(rdev->sb_page); 2086 2087 /* 2088 * Two cases that we want to write new superblocks and rebuild: 2089 * 1) New device (no matching magic number) 2090 * 2) Device specified for rebuild (!In_sync w/ offset == 0) 2091 */ 2092 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) || 2093 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) { 2094 super_sync(rdev->mddev, rdev); 2095 2096 set_bit(FirstUse, &rdev->flags); 2097 sb->compat_features = cpu_to_le32(FEATURE_FLAG_SUPPORTS_V190); 2098 2099 /* Force writing of superblocks to disk */ 2100 set_bit(MD_SB_CHANGE_DEVS, &rdev->mddev->sb_flags); 2101 2102 /* Any superblock is better than none, choose that if given */ 2103 return refdev ? 0 : 1; 2104 } 2105 2106 if (!refdev) 2107 return 1; 2108 2109 events_sb = le64_to_cpu(sb->events); 2110 2111 refsb = page_address(refdev->sb_page); 2112 events_refsb = le64_to_cpu(refsb->events); 2113 2114 return (events_sb > events_refsb) ? 1 : 0; 2115 } 2116 2117 static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev) 2118 { 2119 int role; 2120 unsigned int d; 2121 struct mddev *mddev = &rs->md; 2122 uint64_t events_sb; 2123 uint64_t failed_devices[DISKS_ARRAY_ELEMS]; 2124 struct dm_raid_superblock *sb; 2125 uint32_t new_devs = 0, rebuild_and_new = 0, rebuilds = 0; 2126 struct md_rdev *r; 2127 struct dm_raid_superblock *sb2; 2128 2129 sb = page_address(rdev->sb_page); 2130 events_sb = le64_to_cpu(sb->events); 2131 2132 /* 2133 * Initialise to 1 if this is a new superblock. 2134 */ 2135 mddev->events = events_sb ? : 1; 2136 2137 mddev->reshape_position = MaxSector; 2138 2139 mddev->raid_disks = le32_to_cpu(sb->num_devices); 2140 mddev->level = le32_to_cpu(sb->level); 2141 mddev->layout = le32_to_cpu(sb->layout); 2142 mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors); 2143 2144 /* 2145 * Reshaping is supported, e.g. reshape_position is valid 2146 * in superblock and superblock content is authoritative. 2147 */ 2148 if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) { 2149 /* Superblock is authoritative wrt given raid set layout! */ 2150 mddev->new_level = le32_to_cpu(sb->new_level); 2151 mddev->new_layout = le32_to_cpu(sb->new_layout); 2152 mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors); 2153 mddev->delta_disks = le32_to_cpu(sb->delta_disks); 2154 mddev->array_sectors = le64_to_cpu(sb->array_sectors); 2155 2156 /* raid was reshaping and got interrupted */ 2157 if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) { 2158 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) { 2159 DMERR("Reshape requested but raid set is still reshaping"); 2160 return -EINVAL; 2161 } 2162 2163 if (mddev->delta_disks < 0 || 2164 (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS))) 2165 mddev->reshape_backwards = 1; 2166 else 2167 mddev->reshape_backwards = 0; 2168 2169 mddev->reshape_position = le64_to_cpu(sb->reshape_position); 2170 rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout); 2171 } 2172 2173 } else { 2174 /* 2175 * No takeover/reshaping, because we don't have the extended v1.9.0 metadata 2176 */ 2177 struct raid_type *rt_cur = get_raid_type_by_ll(mddev->level, mddev->layout); 2178 struct raid_type *rt_new = get_raid_type_by_ll(mddev->new_level, mddev->new_layout); 2179 2180 if (rs_takeover_requested(rs)) { 2181 if (rt_cur && rt_new) 2182 DMERR("Takeover raid sets from %s to %s not yet supported by metadata. (raid level change)", 2183 rt_cur->name, rt_new->name); 2184 else 2185 DMERR("Takeover raid sets not yet supported by metadata. (raid level change)"); 2186 return -EINVAL; 2187 } else if (rs_reshape_requested(rs)) { 2188 DMERR("Reshaping raid sets not yet supported by metadata. (raid layout change keeping level)"); 2189 if (mddev->layout != mddev->new_layout) { 2190 if (rt_cur && rt_new) 2191 DMERR(" current layout %s vs new layout %s", 2192 rt_cur->name, rt_new->name); 2193 else 2194 DMERR(" current layout 0x%X vs new layout 0x%X", 2195 le32_to_cpu(sb->layout), mddev->new_layout); 2196 } 2197 if (mddev->chunk_sectors != mddev->new_chunk_sectors) 2198 DMERR(" current stripe sectors %u vs new stripe sectors %u", 2199 mddev->chunk_sectors, mddev->new_chunk_sectors); 2200 if (rs->delta_disks) 2201 DMERR(" current %u disks vs new %u disks", 2202 mddev->raid_disks, mddev->raid_disks + rs->delta_disks); 2203 if (rs_is_raid10(rs)) { 2204 DMERR(" Old layout: %s w/ %u copies", 2205 raid10_md_layout_to_format(mddev->layout), 2206 raid10_md_layout_to_copies(mddev->layout)); 2207 DMERR(" New layout: %s w/ %u copies", 2208 raid10_md_layout_to_format(mddev->new_layout), 2209 raid10_md_layout_to_copies(mddev->new_layout)); 2210 } 2211 return -EINVAL; 2212 } 2213 2214 DMINFO("Discovered old metadata format; upgrading to extended metadata format"); 2215 } 2216 2217 if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) 2218 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset); 2219 2220 /* 2221 * During load, we set FirstUse if a new superblock was written. 2222 * There are two reasons we might not have a superblock: 2223 * 1) The raid set is brand new - in which case, all of the 2224 * devices must have their In_sync bit set. Also, 2225 * recovery_cp must be 0, unless forced. 2226 * 2) This is a new device being added to an old raid set 2227 * and the new device needs to be rebuilt - in which 2228 * case the In_sync bit will /not/ be set and 2229 * recovery_cp must be MaxSector. 2230 * 3) This is/are a new device(s) being added to an old 2231 * raid set during takeover to a higher raid level 2232 * to provide capacity for redundancy or during reshape 2233 * to add capacity to grow the raid set. 2234 */ 2235 d = 0; 2236 rdev_for_each(r, mddev) { 2237 if (test_bit(Journal, &rdev->flags)) 2238 continue; 2239 2240 if (test_bit(FirstUse, &r->flags)) 2241 new_devs++; 2242 2243 if (!test_bit(In_sync, &r->flags)) { 2244 DMINFO("Device %d specified for rebuild; clearing superblock", 2245 r->raid_disk); 2246 rebuilds++; 2247 2248 if (test_bit(FirstUse, &r->flags)) 2249 rebuild_and_new++; 2250 } 2251 2252 d++; 2253 } 2254 2255 if (new_devs == rs->raid_disks || !rebuilds) { 2256 /* Replace a broken device */ 2257 if (new_devs == 1 && !rs->delta_disks) 2258 ; 2259 if (new_devs == rs->raid_disks) { 2260 DMINFO("Superblocks created for new raid set"); 2261 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags); 2262 } else if (new_devs != rebuilds && 2263 new_devs != rs->delta_disks) { 2264 DMERR("New device injected into existing raid set without " 2265 "'delta_disks' or 'rebuild' parameter specified"); 2266 return -EINVAL; 2267 } 2268 } else if (new_devs && new_devs != rebuilds) { 2269 DMERR("%u 'rebuild' devices cannot be injected into" 2270 " a raid set with %u other first-time devices", 2271 rebuilds, new_devs); 2272 return -EINVAL; 2273 } else if (rebuilds) { 2274 if (rebuild_and_new && rebuilds != rebuild_and_new) { 2275 DMERR("new device%s provided without 'rebuild'", 2276 new_devs > 1 ? "s" : ""); 2277 return -EINVAL; 2278 } else if (rs_is_recovering(rs)) { 2279 DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)", 2280 (unsigned long long) mddev->recovery_cp); 2281 return -EINVAL; 2282 } else if (rs_is_reshaping(rs)) { 2283 DMERR("'rebuild' specified while raid set is being reshaped (reshape_position=%llu)", 2284 (unsigned long long) mddev->reshape_position); 2285 return -EINVAL; 2286 } 2287 } 2288 2289 /* 2290 * Now we set the Faulty bit for those devices that are 2291 * recorded in the superblock as failed. 2292 */ 2293 sb_retrieve_failed_devices(sb, failed_devices); 2294 rdev_for_each(r, mddev) { 2295 if (test_bit(Journal, &rdev->flags) || 2296 !r->sb_page) 2297 continue; 2298 sb2 = page_address(r->sb_page); 2299 sb2->failed_devices = 0; 2300 memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices)); 2301 2302 /* 2303 * Check for any device re-ordering. 2304 */ 2305 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) { 2306 role = le32_to_cpu(sb2->array_position); 2307 if (role < 0) 2308 continue; 2309 2310 if (role != r->raid_disk) { 2311 if (rs_is_raid10(rs) && __is_raid10_near(mddev->layout)) { 2312 if (mddev->raid_disks % __raid10_near_copies(mddev->layout) || 2313 rs->raid_disks % rs->raid10_copies) { 2314 rs->ti->error = 2315 "Cannot change raid10 near set to odd # of devices!"; 2316 return -EINVAL; 2317 } 2318 2319 sb2->array_position = cpu_to_le32(r->raid_disk); 2320 2321 } else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) && 2322 !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) && 2323 !rt_is_raid1(rs->raid_type)) { 2324 rs->ti->error = "Cannot change device positions in raid set"; 2325 return -EINVAL; 2326 } 2327 2328 DMINFO("raid device #%d now at position #%d", role, r->raid_disk); 2329 } 2330 2331 /* 2332 * Partial recovery is performed on 2333 * returning failed devices. 2334 */ 2335 if (test_bit(role, (void *) failed_devices)) 2336 set_bit(Faulty, &r->flags); 2337 } 2338 } 2339 2340 return 0; 2341 } 2342 2343 static int super_validate(struct raid_set *rs, struct md_rdev *rdev) 2344 { 2345 struct mddev *mddev = &rs->md; 2346 struct dm_raid_superblock *sb; 2347 2348 if (rs_is_raid0(rs) || !rdev->sb_page || rdev->raid_disk < 0) 2349 return 0; 2350 2351 sb = page_address(rdev->sb_page); 2352 2353 /* 2354 * If mddev->events is not set, we know we have not yet initialized 2355 * the array. 2356 */ 2357 if (!mddev->events && super_init_validation(rs, rdev)) 2358 return -EINVAL; 2359 2360 if (le32_to_cpu(sb->compat_features) && 2361 le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) { 2362 rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags"; 2363 return -EINVAL; 2364 } 2365 2366 if (sb->incompat_features) { 2367 rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet"; 2368 return -EINVAL; 2369 } 2370 2371 /* Enable bitmap creation for RAID levels != 0 */ 2372 mddev->bitmap_info.offset = rt_is_raid0(rs->raid_type) ? 0 : to_sector(4096); 2373 mddev->bitmap_info.default_offset = mddev->bitmap_info.offset; 2374 2375 if (!test_and_clear_bit(FirstUse, &rdev->flags)) { 2376 /* Retrieve device size stored in superblock to be prepared for shrink */ 2377 rdev->sectors = le64_to_cpu(sb->sectors); 2378 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset); 2379 if (rdev->recovery_offset == MaxSector) 2380 set_bit(In_sync, &rdev->flags); 2381 /* 2382 * If no reshape in progress -> we're recovering single 2383 * disk(s) and have to set the device(s) to out-of-sync 2384 */ 2385 else if (!rs_is_reshaping(rs)) 2386 clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */ 2387 } 2388 2389 /* 2390 * If a device comes back, set it as not In_sync and no longer faulty. 2391 */ 2392 if (test_and_clear_bit(Faulty, &rdev->flags)) { 2393 rdev->recovery_offset = 0; 2394 clear_bit(In_sync, &rdev->flags); 2395 rdev->saved_raid_disk = rdev->raid_disk; 2396 } 2397 2398 /* Reshape support -> restore repective data offsets */ 2399 rdev->data_offset = le64_to_cpu(sb->data_offset); 2400 rdev->new_data_offset = le64_to_cpu(sb->new_data_offset); 2401 2402 return 0; 2403 } 2404 2405 /* 2406 * Analyse superblocks and select the freshest. 2407 */ 2408 static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs) 2409 { 2410 int r; 2411 struct md_rdev *rdev, *freshest; 2412 struct mddev *mddev = &rs->md; 2413 2414 freshest = NULL; 2415 rdev_for_each(rdev, mddev) { 2416 if (test_bit(Journal, &rdev->flags)) 2417 continue; 2418 2419 /* 2420 * Skipping super_load due to CTR_FLAG_SYNC will cause 2421 * the array to undergo initialization again as 2422 * though it were new. This is the intended effect 2423 * of the "sync" directive. 2424 * 2425 * With reshaping capability added, we must ensure that 2426 * that the "sync" directive is disallowed during the reshape. 2427 */ 2428 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) 2429 continue; 2430 2431 if (!rdev->meta_bdev) 2432 continue; 2433 2434 r = super_load(rdev, freshest); 2435 2436 switch (r) { 2437 case 1: 2438 freshest = rdev; 2439 break; 2440 case 0: 2441 break; 2442 default: 2443 /* This is a failure to read the superblock from the metadata device. */ 2444 /* 2445 * We have to keep any raid0 data/metadata device pairs or 2446 * the MD raid0 personality will fail to start the array. 2447 */ 2448 if (rs_is_raid0(rs)) 2449 continue; 2450 2451 /* 2452 * We keep the dm_devs to be able to emit the device tuple 2453 * properly on the table line in raid_status() (rather than 2454 * mistakenly acting as if '- -' got passed into the constructor). 2455 * 2456 * The rdev has to stay on the same_set list to allow for 2457 * the attempt to restore faulty devices on second resume. 2458 */ 2459 rdev->raid_disk = rdev->saved_raid_disk = -1; 2460 break; 2461 } 2462 } 2463 2464 if (!freshest) 2465 return 0; 2466 2467 if (validate_raid_redundancy(rs)) { 2468 rs->ti->error = "Insufficient redundancy to activate array"; 2469 return -EINVAL; 2470 } 2471 2472 /* 2473 * Validation of the freshest device provides the source of 2474 * validation for the remaining devices. 2475 */ 2476 rs->ti->error = "Unable to assemble array: Invalid superblocks"; 2477 if (super_validate(rs, freshest)) 2478 return -EINVAL; 2479 2480 rdev_for_each(rdev, mddev) 2481 if (!test_bit(Journal, &rdev->flags) && 2482 rdev != freshest && 2483 super_validate(rs, rdev)) 2484 return -EINVAL; 2485 return 0; 2486 } 2487 2488 /* 2489 * Adjust data_offset and new_data_offset on all disk members of @rs 2490 * for out of place reshaping if requested by contructor 2491 * 2492 * We need free space at the beginning of each raid disk for forward 2493 * and at the end for backward reshapes which userspace has to provide 2494 * via remapping/reordering of space. 2495 */ 2496 static int rs_adjust_data_offsets(struct raid_set *rs) 2497 { 2498 sector_t data_offset = 0, new_data_offset = 0; 2499 struct md_rdev *rdev; 2500 2501 /* Constructor did not request data offset change */ 2502 if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) { 2503 if (!rs_is_reshapable(rs)) 2504 goto out; 2505 2506 return 0; 2507 } 2508 2509 /* HM FIXME: get InSync raid_dev? */ 2510 rdev = &rs->dev[0].rdev; 2511 2512 if (rs->delta_disks < 0) { 2513 /* 2514 * Removing disks (reshaping backwards): 2515 * 2516 * - before reshape: data is at offset 0 and free space 2517 * is at end of each component LV 2518 * 2519 * - after reshape: data is at offset rs->data_offset != 0 on each component LV 2520 */ 2521 data_offset = 0; 2522 new_data_offset = rs->data_offset; 2523 2524 } else if (rs->delta_disks > 0) { 2525 /* 2526 * Adding disks (reshaping forwards): 2527 * 2528 * - before reshape: data is at offset rs->data_offset != 0 and 2529 * free space is at begin of each component LV 2530 * 2531 * - after reshape: data is at offset 0 on each component LV 2532 */ 2533 data_offset = rs->data_offset; 2534 new_data_offset = 0; 2535 2536 } else { 2537 /* 2538 * User space passes in 0 for data offset after having removed reshape space 2539 * 2540 * - or - (data offset != 0) 2541 * 2542 * Changing RAID layout or chunk size -> toggle offsets 2543 * 2544 * - before reshape: data is at offset rs->data_offset 0 and 2545 * free space is at end of each component LV 2546 * -or- 2547 * data is at offset rs->data_offset != 0 and 2548 * free space is at begin of each component LV 2549 * 2550 * - after reshape: data is at offset 0 if it was at offset != 0 2551 * or at offset != 0 if it was at offset 0 2552 * on each component LV 2553 * 2554 */ 2555 data_offset = rs->data_offset ? rdev->data_offset : 0; 2556 new_data_offset = data_offset ? 0 : rs->data_offset; 2557 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags); 2558 } 2559 2560 /* 2561 * Make sure we got a minimum amount of free sectors per device 2562 */ 2563 if (rs->data_offset && 2564 to_sector(i_size_read(rdev->bdev->bd_inode)) - rdev->sectors < MIN_FREE_RESHAPE_SPACE) { 2565 rs->ti->error = data_offset ? "No space for forward reshape" : 2566 "No space for backward reshape"; 2567 return -ENOSPC; 2568 } 2569 out: 2570 /* Adjust data offsets on all rdevs but on any raid4/5/6 journal device */ 2571 rdev_for_each(rdev, &rs->md) { 2572 if (!test_bit(Journal, &rdev->flags)) { 2573 rdev->data_offset = data_offset; 2574 rdev->new_data_offset = new_data_offset; 2575 } 2576 } 2577 2578 return 0; 2579 } 2580 2581 /* Userpace reordered disks -> adjust raid_disk indexes in @rs */ 2582 static void __reorder_raid_disk_indexes(struct raid_set *rs) 2583 { 2584 int i = 0; 2585 struct md_rdev *rdev; 2586 2587 rdev_for_each(rdev, &rs->md) { 2588 if (!test_bit(Journal, &rdev->flags)) { 2589 rdev->raid_disk = i++; 2590 rdev->saved_raid_disk = rdev->new_raid_disk = -1; 2591 } 2592 } 2593 } 2594 2595 /* 2596 * Setup @rs for takeover by a different raid level 2597 */ 2598 static int rs_setup_takeover(struct raid_set *rs) 2599 { 2600 struct mddev *mddev = &rs->md; 2601 struct md_rdev *rdev; 2602 unsigned int d = mddev->raid_disks = rs->raid_disks; 2603 sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset; 2604 2605 if (rt_is_raid10(rs->raid_type)) { 2606 if (mddev->level == 0) { 2607 /* Userpace reordered disks -> adjust raid_disk indexes */ 2608 __reorder_raid_disk_indexes(rs); 2609 2610 /* raid0 -> raid10_far layout */ 2611 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR, 2612 rs->raid10_copies); 2613 } else if (mddev->level == 1) 2614 /* raid1 -> raid10_near layout */ 2615 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR, 2616 rs->raid_disks); 2617 else 2618 return -EINVAL; 2619 2620 } 2621 2622 clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags); 2623 mddev->recovery_cp = MaxSector; 2624 2625 while (d--) { 2626 rdev = &rs->dev[d].rdev; 2627 2628 if (test_bit(d, (void *) rs->rebuild_disks)) { 2629 clear_bit(In_sync, &rdev->flags); 2630 clear_bit(Faulty, &rdev->flags); 2631 mddev->recovery_cp = rdev->recovery_offset = 0; 2632 /* Bitmap has to be created when we do an "up" takeover */ 2633 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags); 2634 } 2635 2636 rdev->new_data_offset = new_data_offset; 2637 } 2638 2639 return 0; 2640 } 2641 2642 /* Prepare @rs for reshape */ 2643 static int rs_prepare_reshape(struct raid_set *rs) 2644 { 2645 bool reshape; 2646 struct mddev *mddev = &rs->md; 2647 2648 if (rs_is_raid10(rs)) { 2649 if (rs->raid_disks != mddev->raid_disks && 2650 __is_raid10_near(mddev->layout) && 2651 rs->raid10_copies && 2652 rs->raid10_copies != __raid10_near_copies(mddev->layout)) { 2653 /* 2654 * raid disk have to be multiple of data copies to allow this conversion, 2655 * 2656 * This is actually not a reshape it is a 2657 * rebuild of any additional mirrors per group 2658 */ 2659 if (rs->raid_disks % rs->raid10_copies) { 2660 rs->ti->error = "Can't reshape raid10 mirror groups"; 2661 return -EINVAL; 2662 } 2663 2664 /* Userpace reordered disks to add/remove mirrors -> adjust raid_disk indexes */ 2665 __reorder_raid_disk_indexes(rs); 2666 mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR, 2667 rs->raid10_copies); 2668 mddev->new_layout = mddev->layout; 2669 reshape = false; 2670 } else 2671 reshape = true; 2672 2673 } else if (rs_is_raid456(rs)) 2674 reshape = true; 2675 2676 else if (rs_is_raid1(rs)) { 2677 if (rs->delta_disks) { 2678 /* Process raid1 via delta_disks */ 2679 mddev->degraded = rs->delta_disks < 0 ? -rs->delta_disks : rs->delta_disks; 2680 reshape = true; 2681 } else { 2682 /* Process raid1 without delta_disks */ 2683 mddev->raid_disks = rs->raid_disks; 2684 reshape = false; 2685 } 2686 } else { 2687 rs->ti->error = "Called with bogus raid type"; 2688 return -EINVAL; 2689 } 2690 2691 if (reshape) { 2692 set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags); 2693 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags); 2694 } else if (mddev->raid_disks < rs->raid_disks) 2695 /* Create new superblocks and bitmaps, if any new disks */ 2696 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags); 2697 2698 return 0; 2699 } 2700 2701 /* 2702 * 2703 * - change raid layout 2704 * - change chunk size 2705 * - add disks 2706 * - remove disks 2707 */ 2708 static int rs_setup_reshape(struct raid_set *rs) 2709 { 2710 int r = 0; 2711 unsigned int cur_raid_devs, d; 2712 struct mddev *mddev = &rs->md; 2713 struct md_rdev *rdev; 2714 2715 mddev->delta_disks = rs->delta_disks; 2716 cur_raid_devs = mddev->raid_disks; 2717 2718 /* Ignore impossible layout change whilst adding/removing disks */ 2719 if (mddev->delta_disks && 2720 mddev->layout != mddev->new_layout) { 2721 DMINFO("Ignoring invalid layout change with delta_disks=%d", rs->delta_disks); 2722 mddev->new_layout = mddev->layout; 2723 } 2724 2725 /* 2726 * Adjust array size: 2727 * 2728 * - in case of adding disks, array size has 2729 * to grow after the disk adding reshape, 2730 * which'll hapen in the event handler; 2731 * reshape will happen forward, so space has to 2732 * be available at the beginning of each disk 2733 * 2734 * - in case of removing disks, array size 2735 * has to shrink before starting the reshape, 2736 * which'll happen here; 2737 * reshape will happen backward, so space has to 2738 * be available at the end of each disk 2739 * 2740 * - data_offset and new_data_offset are 2741 * adjusted for aforementioned out of place 2742 * reshaping based on userspace passing in 2743 * the "data_offset <sectors>" key/value 2744 * pair via the constructor 2745 */ 2746 2747 /* Add disk(s) */ 2748 if (rs->delta_disks > 0) { 2749 /* Prepare disks for check in raid4/5/6/10 {check|start}_reshape */ 2750 for (d = cur_raid_devs; d < rs->raid_disks; d++) { 2751 rdev = &rs->dev[d].rdev; 2752 clear_bit(In_sync, &rdev->flags); 2753 2754 /* 2755 * save_raid_disk needs to be -1, or recovery_offset will be set to 0 2756 * by md, which'll store that erroneously in the superblock on reshape 2757 */ 2758 rdev->saved_raid_disk = -1; 2759 rdev->raid_disk = d; 2760 2761 rdev->sectors = mddev->dev_sectors; 2762 rdev->recovery_offset = rs_is_raid1(rs) ? 0 : MaxSector; 2763 } 2764 2765 mddev->reshape_backwards = 0; /* adding disks -> forward reshape */ 2766 2767 /* Remove disk(s) */ 2768 } else if (rs->delta_disks < 0) { 2769 r = rs_set_dev_and_array_sectors(rs, true); 2770 mddev->reshape_backwards = 1; /* removing disk(s) -> backward reshape */ 2771 2772 /* Change layout and/or chunk size */ 2773 } else { 2774 /* 2775 * Reshape layout (e.g. raid5_ls -> raid5_n) and/or chunk size: 2776 * 2777 * keeping number of disks and do layout change -> 2778 * 2779 * toggle reshape_backward depending on data_offset: 2780 * 2781 * - free space upfront -> reshape forward 2782 * 2783 * - free space at the end -> reshape backward 2784 * 2785 * 2786 * This utilizes free reshape space avoiding the need 2787 * for userspace to move (parts of) LV segments in 2788 * case of layout/chunksize change (for disk 2789 * adding/removing reshape space has to be at 2790 * the proper address (see above with delta_disks): 2791 * 2792 * add disk(s) -> begin 2793 * remove disk(s)-> end 2794 */ 2795 mddev->reshape_backwards = rs->dev[0].rdev.data_offset ? 0 : 1; 2796 } 2797 2798 return r; 2799 } 2800 2801 /* 2802 * Enable/disable discard support on RAID set depending on 2803 * RAID level and discard properties of underlying RAID members. 2804 */ 2805 static void configure_discard_support(struct raid_set *rs) 2806 { 2807 int i; 2808 bool raid456; 2809 struct dm_target *ti = rs->ti; 2810 2811 /* Assume discards not supported until after checks below. */ 2812 ti->discards_supported = false; 2813 2814 /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */ 2815 raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6); 2816 2817 for (i = 0; i < rs->raid_disks; i++) { 2818 struct request_queue *q; 2819 2820 if (!rs->dev[i].rdev.bdev) 2821 continue; 2822 2823 q = bdev_get_queue(rs->dev[i].rdev.bdev); 2824 if (!q || !blk_queue_discard(q)) 2825 return; 2826 2827 if (raid456) { 2828 if (!q->limits.discard_zeroes_data) 2829 return; 2830 if (!devices_handle_discard_safely) { 2831 DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty."); 2832 DMERR("Set dm-raid.devices_handle_discard_safely=Y to override."); 2833 return; 2834 } 2835 } 2836 } 2837 2838 /* All RAID members properly support discards */ 2839 ti->discards_supported = true; 2840 2841 /* 2842 * RAID1 and RAID10 personalities require bio splitting, 2843 * RAID0/4/5/6 don't and process large discard bios properly. 2844 */ 2845 ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10); 2846 ti->num_discard_bios = 1; 2847 } 2848 2849 /* 2850 * Construct a RAID0/1/10/4/5/6 mapping: 2851 * Args: 2852 * <raid_type> <#raid_params> <raid_params>{0,} \ 2853 * <#raid_devs> [<meta_dev1> <dev1>]{1,} 2854 * 2855 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for 2856 * details on possible <raid_params>. 2857 * 2858 * Userspace is free to initialize the metadata devices, hence the superblocks to 2859 * enforce recreation based on the passed in table parameters. 2860 * 2861 */ 2862 static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv) 2863 { 2864 int r; 2865 bool resize; 2866 struct raid_type *rt; 2867 unsigned int num_raid_params, num_raid_devs; 2868 sector_t calculated_dev_sectors; 2869 struct raid_set *rs = NULL; 2870 const char *arg; 2871 struct rs_layout rs_layout; 2872 struct dm_arg_set as = { argc, argv }, as_nrd; 2873 struct dm_arg _args[] = { 2874 { 0, as.argc, "Cannot understand number of raid parameters" }, 2875 { 1, 254, "Cannot understand number of raid devices parameters" } 2876 }; 2877 2878 /* Must have <raid_type> */ 2879 arg = dm_shift_arg(&as); 2880 if (!arg) { 2881 ti->error = "No arguments"; 2882 return -EINVAL; 2883 } 2884 2885 rt = get_raid_type(arg); 2886 if (!rt) { 2887 ti->error = "Unrecognised raid_type"; 2888 return -EINVAL; 2889 } 2890 2891 /* Must have <#raid_params> */ 2892 if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error)) 2893 return -EINVAL; 2894 2895 /* number of raid device tupples <meta_dev data_dev> */ 2896 as_nrd = as; 2897 dm_consume_args(&as_nrd, num_raid_params); 2898 _args[1].max = (as_nrd.argc - 1) / 2; 2899 if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error)) 2900 return -EINVAL; 2901 2902 if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) { 2903 ti->error = "Invalid number of supplied raid devices"; 2904 return -EINVAL; 2905 } 2906 2907 rs = raid_set_alloc(ti, rt, num_raid_devs); 2908 if (IS_ERR(rs)) 2909 return PTR_ERR(rs); 2910 2911 r = parse_raid_params(rs, &as, num_raid_params); 2912 if (r) 2913 goto bad; 2914 2915 r = parse_dev_params(rs, &as); 2916 if (r) 2917 goto bad; 2918 2919 rs->md.sync_super = super_sync; 2920 2921 /* 2922 * Calculate ctr requested array and device sizes to allow 2923 * for superblock analysis needing device sizes defined. 2924 * 2925 * Any existing superblock will overwrite the array and device sizes 2926 */ 2927 r = rs_set_dev_and_array_sectors(rs, false); 2928 if (r) 2929 goto bad; 2930 2931 calculated_dev_sectors = rs->md.dev_sectors; 2932 2933 /* 2934 * Backup any new raid set level, layout, ... 2935 * requested to be able to compare to superblock 2936 * members for conversion decisions. 2937 */ 2938 rs_config_backup(rs, &rs_layout); 2939 2940 r = analyse_superblocks(ti, rs); 2941 if (r) 2942 goto bad; 2943 2944 resize = calculated_dev_sectors != __rdev_sectors(rs); 2945 2946 INIT_WORK(&rs->md.event_work, do_table_event); 2947 ti->private = rs; 2948 ti->num_flush_bios = 1; 2949 2950 /* Restore any requested new layout for conversion decision */ 2951 rs_config_restore(rs, &rs_layout); 2952 2953 /* 2954 * Now that we have any superblock metadata available, 2955 * check for new, recovering, reshaping, to be taken over, 2956 * to be reshaped or an existing, unchanged raid set to 2957 * run in sequence. 2958 */ 2959 if (test_bit(MD_ARRAY_FIRST_USE, &rs->md.flags)) { 2960 /* A new raid6 set has to be recovered to ensure proper parity and Q-Syndrome */ 2961 if (rs_is_raid6(rs) && 2962 test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) { 2963 ti->error = "'nosync' not allowed for new raid6 set"; 2964 r = -EINVAL; 2965 goto bad; 2966 } 2967 rs_setup_recovery(rs, 0); 2968 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags); 2969 rs_set_new(rs); 2970 } else if (rs_is_recovering(rs)) { 2971 /* A recovering raid set may be resized */ 2972 ; /* skip setup rs */ 2973 } else if (rs_is_reshaping(rs)) { 2974 /* Have to reject size change request during reshape */ 2975 if (resize) { 2976 ti->error = "Can't resize a reshaping raid set"; 2977 r = -EPERM; 2978 goto bad; 2979 } 2980 /* skip setup rs */ 2981 } else if (rs_takeover_requested(rs)) { 2982 if (rs_is_reshaping(rs)) { 2983 ti->error = "Can't takeover a reshaping raid set"; 2984 r = -EPERM; 2985 goto bad; 2986 } 2987 2988 /* We can't takeover a journaled raid4/5/6 */ 2989 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) { 2990 ti->error = "Can't takeover a journaled raid4/5/6 set"; 2991 r = -EPERM; 2992 goto bad; 2993 } 2994 2995 /* 2996 * If a takeover is needed, userspace sets any additional 2997 * devices to rebuild and we can check for a valid request here. 2998 * 2999 * If acceptible, set the level to the new requested 3000 * one, prohibit requesting recovery, allow the raid 3001 * set to run and store superblocks during resume. 3002 */ 3003 r = rs_check_takeover(rs); 3004 if (r) 3005 goto bad; 3006 3007 r = rs_setup_takeover(rs); 3008 if (r) 3009 goto bad; 3010 3011 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags); 3012 /* Takeover ain't recovery, so disable recovery */ 3013 rs_setup_recovery(rs, MaxSector); 3014 rs_set_new(rs); 3015 } else if (rs_reshape_requested(rs)) { 3016 /* 3017 * No need to check for 'ongoing' takeover here, because takeover 3018 * is an instant operation as oposed to an ongoing reshape. 3019 */ 3020 3021 /* We can't reshape a journaled raid4/5/6 */ 3022 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) { 3023 ti->error = "Can't reshape a journaled raid4/5/6 set"; 3024 r = -EPERM; 3025 goto bad; 3026 } 3027 3028 /* 3029 * We can only prepare for a reshape here, because the 3030 * raid set needs to run to provide the repective reshape 3031 * check functions via its MD personality instance. 3032 * 3033 * So do the reshape check after md_run() succeeded. 3034 */ 3035 r = rs_prepare_reshape(rs); 3036 if (r) 3037 return r; 3038 3039 /* Reshaping ain't recovery, so disable recovery */ 3040 rs_setup_recovery(rs, MaxSector); 3041 rs_set_cur(rs); 3042 } else { 3043 /* May not set recovery when a device rebuild is requested */ 3044 if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) { 3045 rs_setup_recovery(rs, MaxSector); 3046 set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags); 3047 } else 3048 rs_setup_recovery(rs, test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ? 3049 0 : (resize ? calculated_dev_sectors : MaxSector)); 3050 rs_set_cur(rs); 3051 } 3052 3053 /* If constructor requested it, change data and new_data offsets */ 3054 r = rs_adjust_data_offsets(rs); 3055 if (r) 3056 goto bad; 3057 3058 /* Start raid set read-only and assumed clean to change in raid_resume() */ 3059 rs->md.ro = 1; 3060 rs->md.in_sync = 1; 3061 set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery); 3062 3063 /* Has to be held on running the array */ 3064 mddev_lock_nointr(&rs->md); 3065 r = md_run(&rs->md); 3066 rs->md.in_sync = 0; /* Assume already marked dirty */ 3067 3068 if (r) { 3069 ti->error = "Failed to run raid array"; 3070 mddev_unlock(&rs->md); 3071 goto bad; 3072 } 3073 3074 rs->callbacks.congested_fn = raid_is_congested; 3075 dm_table_add_target_callbacks(ti->table, &rs->callbacks); 3076 3077 mddev_suspend(&rs->md); 3078 3079 /* Try to adjust the raid4/5/6 stripe cache size to the stripe size */ 3080 if (rs_is_raid456(rs)) { 3081 r = rs_set_raid456_stripe_cache(rs); 3082 if (r) 3083 goto bad_stripe_cache; 3084 } 3085 3086 /* Now do an early reshape check */ 3087 if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) { 3088 r = rs_check_reshape(rs); 3089 if (r) 3090 goto bad_check_reshape; 3091 3092 /* Restore new, ctr requested layout to perform check */ 3093 rs_config_restore(rs, &rs_layout); 3094 3095 if (rs->md.pers->start_reshape) { 3096 r = rs->md.pers->check_reshape(&rs->md); 3097 if (r) { 3098 ti->error = "Reshape check failed"; 3099 goto bad_check_reshape; 3100 } 3101 } 3102 } 3103 3104 /* Disable/enable discard support on raid set. */ 3105 configure_discard_support(rs); 3106 3107 mddev_unlock(&rs->md); 3108 return 0; 3109 3110 bad_stripe_cache: 3111 bad_check_reshape: 3112 md_stop(&rs->md); 3113 bad: 3114 raid_set_free(rs); 3115 3116 return r; 3117 } 3118 3119 static void raid_dtr(struct dm_target *ti) 3120 { 3121 struct raid_set *rs = ti->private; 3122 3123 list_del_init(&rs->callbacks.list); 3124 md_stop(&rs->md); 3125 raid_set_free(rs); 3126 } 3127 3128 static int raid_map(struct dm_target *ti, struct bio *bio) 3129 { 3130 struct raid_set *rs = ti->private; 3131 struct mddev *mddev = &rs->md; 3132 3133 /* 3134 * If we're reshaping to add disk(s)), ti->len and 3135 * mddev->array_sectors will differ during the process 3136 * (ti->len > mddev->array_sectors), so we have to requeue 3137 * bios with addresses > mddev->array_sectors here or 3138 * there will occur accesses past EOD of the component 3139 * data images thus erroring the raid set. 3140 */ 3141 if (unlikely(bio_end_sector(bio) > mddev->array_sectors)) 3142 return DM_MAPIO_REQUEUE; 3143 3144 mddev->pers->make_request(mddev, bio); 3145 3146 return DM_MAPIO_SUBMITTED; 3147 } 3148 3149 /* Return string describing the current sync action of @mddev */ 3150 static const char *decipher_sync_action(struct mddev *mddev) 3151 { 3152 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) 3153 return "frozen"; 3154 3155 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || 3156 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { 3157 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) 3158 return "reshape"; 3159 3160 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { 3161 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) 3162 return "resync"; 3163 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) 3164 return "check"; 3165 return "repair"; 3166 } 3167 3168 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) 3169 return "recover"; 3170 } 3171 3172 return "idle"; 3173 } 3174 3175 /* 3176 * Return status string for @rdev 3177 * 3178 * Status characters: 3179 * 3180 * 'D' = Dead/Failed raid set component or raid4/5/6 journal device 3181 * 'a' = Alive but not in-sync 3182 * 'A' = Alive and in-sync raid set component or alive raid4/5/6 journal device 3183 * '-' = Non-existing device (i.e. uspace passed '- -' into the ctr) 3184 */ 3185 static const char *__raid_dev_status(struct md_rdev *rdev, bool array_in_sync) 3186 { 3187 if (!rdev->bdev) 3188 return "-"; 3189 else if (test_bit(Faulty, &rdev->flags)) 3190 return "D"; 3191 else if (test_bit(Journal, &rdev->flags)) 3192 return "A"; 3193 else if (!array_in_sync || !test_bit(In_sync, &rdev->flags)) 3194 return "a"; 3195 else 3196 return "A"; 3197 } 3198 3199 /* Helper to return resync/reshape progress for @rs and @array_in_sync */ 3200 static sector_t rs_get_progress(struct raid_set *rs, 3201 sector_t resync_max_sectors, bool *array_in_sync) 3202 { 3203 sector_t r, recovery_cp, curr_resync_completed; 3204 struct mddev *mddev = &rs->md; 3205 3206 curr_resync_completed = mddev->curr_resync_completed ?: mddev->recovery_cp; 3207 recovery_cp = mddev->recovery_cp; 3208 *array_in_sync = false; 3209 3210 if (rs_is_raid0(rs)) { 3211 r = resync_max_sectors; 3212 *array_in_sync = true; 3213 3214 } else { 3215 r = mddev->reshape_position; 3216 3217 /* Reshape is relative to the array size */ 3218 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || 3219 r != MaxSector) { 3220 if (r == MaxSector) { 3221 *array_in_sync = true; 3222 r = resync_max_sectors; 3223 } else { 3224 /* Got to reverse on backward reshape */ 3225 if (mddev->reshape_backwards) 3226 r = mddev->array_sectors - r; 3227 3228 /* Devide by # of data stripes */ 3229 sector_div(r, mddev_data_stripes(rs)); 3230 } 3231 3232 /* Sync is relative to the component device size */ 3233 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) 3234 r = curr_resync_completed; 3235 else 3236 r = recovery_cp; 3237 3238 if (r == MaxSector) { 3239 /* 3240 * Sync complete. 3241 */ 3242 *array_in_sync = true; 3243 r = resync_max_sectors; 3244 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { 3245 /* 3246 * If "check" or "repair" is occurring, the raid set has 3247 * undergone an initial sync and the health characters 3248 * should not be 'a' anymore. 3249 */ 3250 *array_in_sync = true; 3251 } else { 3252 struct md_rdev *rdev; 3253 3254 /* 3255 * The raid set may be doing an initial sync, or it may 3256 * be rebuilding individual components. If all the 3257 * devices are In_sync, then it is the raid set that is 3258 * being initialized. 3259 */ 3260 rdev_for_each(rdev, mddev) 3261 if (!test_bit(Journal, &rdev->flags) && 3262 !test_bit(In_sync, &rdev->flags)) 3263 *array_in_sync = true; 3264 #if 0 3265 r = 0; /* HM FIXME: TESTME: https://bugzilla.redhat.com/show_bug.cgi?id=1210637 ? */ 3266 #endif 3267 } 3268 } 3269 3270 return r; 3271 } 3272 3273 /* Helper to return @dev name or "-" if !@dev */ 3274 static const char *__get_dev_name(struct dm_dev *dev) 3275 { 3276 return dev ? dev->name : "-"; 3277 } 3278 3279 static void raid_status(struct dm_target *ti, status_type_t type, 3280 unsigned int status_flags, char *result, unsigned int maxlen) 3281 { 3282 struct raid_set *rs = ti->private; 3283 struct mddev *mddev = &rs->md; 3284 struct r5conf *conf = mddev->private; 3285 int i, max_nr_stripes = conf ? conf->max_nr_stripes : 0; 3286 bool array_in_sync; 3287 unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */ 3288 unsigned int sz = 0; 3289 unsigned int rebuild_disks; 3290 unsigned int write_mostly_params = 0; 3291 sector_t progress, resync_max_sectors, resync_mismatches; 3292 const char *sync_action; 3293 struct raid_type *rt; 3294 3295 switch (type) { 3296 case STATUSTYPE_INFO: 3297 /* *Should* always succeed */ 3298 rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout); 3299 if (!rt) 3300 return; 3301 3302 DMEMIT("%s %d ", rt->name, mddev->raid_disks); 3303 3304 /* Access most recent mddev properties for status output */ 3305 smp_rmb(); 3306 /* Get sensible max sectors even if raid set not yet started */ 3307 resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ? 3308 mddev->resync_max_sectors : mddev->dev_sectors; 3309 progress = rs_get_progress(rs, resync_max_sectors, &array_in_sync); 3310 resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ? 3311 atomic64_read(&mddev->resync_mismatches) : 0; 3312 sync_action = decipher_sync_action(&rs->md); 3313 3314 /* HM FIXME: do we want another state char for raid0? It shows 'D'/'A'/'-' now */ 3315 for (i = 0; i < rs->raid_disks; i++) 3316 DMEMIT(__raid_dev_status(&rs->dev[i].rdev, array_in_sync)); 3317 3318 /* 3319 * In-sync/Reshape ratio: 3320 * The in-sync ratio shows the progress of: 3321 * - Initializing the raid set 3322 * - Rebuilding a subset of devices of the raid set 3323 * The user can distinguish between the two by referring 3324 * to the status characters. 3325 * 3326 * The reshape ratio shows the progress of 3327 * changing the raid layout or the number of 3328 * disks of a raid set 3329 */ 3330 DMEMIT(" %llu/%llu", (unsigned long long) progress, 3331 (unsigned long long) resync_max_sectors); 3332 3333 /* 3334 * v1.5.0+: 3335 * 3336 * Sync action: 3337 * See Documentation/device-mapper/dm-raid.txt for 3338 * information on each of these states. 3339 */ 3340 DMEMIT(" %s", sync_action); 3341 3342 /* 3343 * v1.5.0+: 3344 * 3345 * resync_mismatches/mismatch_cnt 3346 * This field shows the number of discrepancies found when 3347 * performing a "check" of the raid set. 3348 */ 3349 DMEMIT(" %llu", (unsigned long long) resync_mismatches); 3350 3351 /* 3352 * v1.9.0+: 3353 * 3354 * data_offset (needed for out of space reshaping) 3355 * This field shows the data offset into the data 3356 * image LV where the first stripes data starts. 3357 * 3358 * We keep data_offset equal on all raid disks of the set, 3359 * so retrieving it from the first raid disk is sufficient. 3360 */ 3361 DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset); 3362 3363 /* 3364 * v1.10.0+: 3365 */ 3366 DMEMIT(" %s", test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ? 3367 __raid_dev_status(&rs->journal_dev.rdev, 0) : "-"); 3368 break; 3369 3370 case STATUSTYPE_TABLE: 3371 /* Report the table line string you would use to construct this raid set */ 3372 3373 /* Calculate raid parameter count */ 3374 for (i = 0; i < rs->raid_disks; i++) 3375 if (test_bit(WriteMostly, &rs->dev[i].rdev.flags)) 3376 write_mostly_params += 2; 3377 rebuild_disks = memweight(rs->rebuild_disks, DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks)); 3378 raid_param_cnt += rebuild_disks * 2 + 3379 write_mostly_params + 3380 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) + 3381 hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2 + 3382 (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ? 2 : 0); 3383 /* Emit table line */ 3384 DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors); 3385 if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags)) 3386 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT), 3387 raid10_md_layout_to_format(mddev->layout)); 3388 if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags)) 3389 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES), 3390 raid10_md_layout_to_copies(mddev->layout)); 3391 if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) 3392 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC)); 3393 if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags)) 3394 DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC)); 3395 if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags)) 3396 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE), 3397 (unsigned long long) to_sector(mddev->bitmap_info.chunksize)); 3398 if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) 3399 DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET), 3400 (unsigned long long) rs->data_offset); 3401 if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags)) 3402 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP), 3403 mddev->bitmap_info.daemon_sleep); 3404 if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) 3405 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS), 3406 max(rs->delta_disks, mddev->delta_disks)); 3407 if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags)) 3408 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE), 3409 max_nr_stripes); 3410 if (rebuild_disks) 3411 for (i = 0; i < rs->raid_disks; i++) 3412 if (test_bit(rs->dev[i].rdev.raid_disk, (void *) rs->rebuild_disks)) 3413 DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD), 3414 rs->dev[i].rdev.raid_disk); 3415 if (write_mostly_params) 3416 for (i = 0; i < rs->raid_disks; i++) 3417 if (test_bit(WriteMostly, &rs->dev[i].rdev.flags)) 3418 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY), 3419 rs->dev[i].rdev.raid_disk); 3420 if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) 3421 DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND), 3422 mddev->bitmap_info.max_write_behind); 3423 if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags)) 3424 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE), 3425 mddev->sync_speed_max); 3426 if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags)) 3427 DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE), 3428 mddev->sync_speed_min); 3429 if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) 3430 DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV), 3431 __get_dev_name(rs->journal_dev.dev)); 3432 DMEMIT(" %d", rs->raid_disks); 3433 for (i = 0; i < rs->raid_disks; i++) 3434 DMEMIT(" %s %s", __get_dev_name(rs->dev[i].meta_dev), 3435 __get_dev_name(rs->dev[i].data_dev)); 3436 } 3437 } 3438 3439 static int raid_message(struct dm_target *ti, unsigned int argc, char **argv) 3440 { 3441 struct raid_set *rs = ti->private; 3442 struct mddev *mddev = &rs->md; 3443 3444 if (!mddev->pers || !mddev->pers->sync_request) 3445 return -EINVAL; 3446 3447 if (!strcasecmp(argv[0], "frozen")) 3448 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 3449 else 3450 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 3451 3452 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) { 3453 if (mddev->sync_thread) { 3454 set_bit(MD_RECOVERY_INTR, &mddev->recovery); 3455 md_reap_sync_thread(mddev); 3456 } 3457 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || 3458 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) 3459 return -EBUSY; 3460 else if (!strcasecmp(argv[0], "resync")) 3461 ; /* MD_RECOVERY_NEEDED set below */ 3462 else if (!strcasecmp(argv[0], "recover")) 3463 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); 3464 else { 3465 if (!strcasecmp(argv[0], "check")) 3466 set_bit(MD_RECOVERY_CHECK, &mddev->recovery); 3467 else if (!strcasecmp(argv[0], "repair")) { 3468 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); 3469 set_bit(MD_RECOVERY_SYNC, &mddev->recovery); 3470 } else 3471 return -EINVAL; 3472 } 3473 if (mddev->ro == 2) { 3474 /* A write to sync_action is enough to justify 3475 * canceling read-auto mode 3476 */ 3477 mddev->ro = 0; 3478 if (!mddev->suspended && mddev->sync_thread) 3479 md_wakeup_thread(mddev->sync_thread); 3480 } 3481 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); 3482 if (!mddev->suspended && mddev->thread) 3483 md_wakeup_thread(mddev->thread); 3484 3485 return 0; 3486 } 3487 3488 static int raid_iterate_devices(struct dm_target *ti, 3489 iterate_devices_callout_fn fn, void *data) 3490 { 3491 struct raid_set *rs = ti->private; 3492 unsigned int i; 3493 int r = 0; 3494 3495 for (i = 0; !r && i < rs->md.raid_disks; i++) 3496 if (rs->dev[i].data_dev) 3497 r = fn(ti, 3498 rs->dev[i].data_dev, 3499 0, /* No offset on data devs */ 3500 rs->md.dev_sectors, 3501 data); 3502 3503 return r; 3504 } 3505 3506 static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits) 3507 { 3508 struct raid_set *rs = ti->private; 3509 unsigned int chunk_size = to_bytes(rs->md.chunk_sectors); 3510 3511 blk_limits_io_min(limits, chunk_size); 3512 blk_limits_io_opt(limits, chunk_size * mddev_data_stripes(rs)); 3513 } 3514 3515 static void raid_presuspend(struct dm_target *ti) 3516 { 3517 struct raid_set *rs = ti->private; 3518 3519 md_stop_writes(&rs->md); 3520 } 3521 3522 static void raid_postsuspend(struct dm_target *ti) 3523 { 3524 struct raid_set *rs = ti->private; 3525 3526 if (!rs->md.suspended) 3527 mddev_suspend(&rs->md); 3528 3529 rs->md.ro = 1; 3530 } 3531 3532 static void attempt_restore_of_faulty_devices(struct raid_set *rs) 3533 { 3534 int i; 3535 uint64_t cleared_failed_devices[DISKS_ARRAY_ELEMS]; 3536 unsigned long flags; 3537 bool cleared = false; 3538 struct dm_raid_superblock *sb; 3539 struct mddev *mddev = &rs->md; 3540 struct md_rdev *r; 3541 3542 /* RAID personalities have to provide hot add/remove methods or we need to bail out. */ 3543 if (!mddev->pers || !mddev->pers->hot_add_disk || !mddev->pers->hot_remove_disk) 3544 return; 3545 3546 memset(cleared_failed_devices, 0, sizeof(cleared_failed_devices)); 3547 3548 for (i = 0; i < mddev->raid_disks; i++) { 3549 r = &rs->dev[i].rdev; 3550 /* HM FIXME: enhance journal device recovery processing */ 3551 if (test_bit(Journal, &r->flags)) 3552 continue; 3553 3554 if (test_bit(Faulty, &r->flags) && 3555 r->meta_bdev && !read_disk_sb(r, r->sb_size, true)) { 3556 DMINFO("Faulty %s device #%d has readable super block." 3557 " Attempting to revive it.", 3558 rs->raid_type->name, i); 3559 3560 /* 3561 * Faulty bit may be set, but sometimes the array can 3562 * be suspended before the personalities can respond 3563 * by removing the device from the array (i.e. calling 3564 * 'hot_remove_disk'). If they haven't yet removed 3565 * the failed device, its 'raid_disk' number will be 3566 * '>= 0' - meaning we must call this function 3567 * ourselves. 3568 */ 3569 flags = r->flags; 3570 clear_bit(In_sync, &r->flags); /* Mandatory for hot remove. */ 3571 if (r->raid_disk >= 0) { 3572 if (mddev->pers->hot_remove_disk(mddev, r)) { 3573 /* Failed to revive this device, try next */ 3574 r->flags = flags; 3575 continue; 3576 } 3577 } else 3578 r->raid_disk = r->saved_raid_disk = i; 3579 3580 clear_bit(Faulty, &r->flags); 3581 clear_bit(WriteErrorSeen, &r->flags); 3582 3583 if (mddev->pers->hot_add_disk(mddev, r)) { 3584 /* Failed to revive this device, try next */ 3585 r->raid_disk = r->saved_raid_disk = -1; 3586 r->flags = flags; 3587 } else { 3588 clear_bit(In_sync, &r->flags); 3589 r->recovery_offset = 0; 3590 set_bit(i, (void *) cleared_failed_devices); 3591 cleared = true; 3592 } 3593 } 3594 } 3595 3596 /* If any failed devices could be cleared, update all sbs failed_devices bits */ 3597 if (cleared) { 3598 uint64_t failed_devices[DISKS_ARRAY_ELEMS]; 3599 3600 rdev_for_each(r, &rs->md) { 3601 if (test_bit(Journal, &r->flags)) 3602 continue; 3603 3604 sb = page_address(r->sb_page); 3605 sb_retrieve_failed_devices(sb, failed_devices); 3606 3607 for (i = 0; i < DISKS_ARRAY_ELEMS; i++) 3608 failed_devices[i] &= ~cleared_failed_devices[i]; 3609 3610 sb_update_failed_devices(sb, failed_devices); 3611 } 3612 } 3613 } 3614 3615 static int __load_dirty_region_bitmap(struct raid_set *rs) 3616 { 3617 int r = 0; 3618 3619 /* Try loading the bitmap unless "raid0", which does not have one */ 3620 if (!rs_is_raid0(rs) && 3621 !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) { 3622 r = bitmap_load(&rs->md); 3623 if (r) 3624 DMERR("Failed to load bitmap"); 3625 } 3626 3627 return r; 3628 } 3629 3630 /* Enforce updating all superblocks */ 3631 static void rs_update_sbs(struct raid_set *rs) 3632 { 3633 struct mddev *mddev = &rs->md; 3634 int ro = mddev->ro; 3635 3636 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 3637 mddev->ro = 0; 3638 md_update_sb(mddev, 1); 3639 mddev->ro = ro; 3640 } 3641 3642 /* 3643 * Reshape changes raid algorithm of @rs to new one within personality 3644 * (e.g. raid6_zr -> raid6_nc), changes stripe size, adds/removes 3645 * disks from a raid set thus growing/shrinking it or resizes the set 3646 * 3647 * Call mddev_lock_nointr() before! 3648 */ 3649 static int rs_start_reshape(struct raid_set *rs) 3650 { 3651 int r; 3652 struct mddev *mddev = &rs->md; 3653 struct md_personality *pers = mddev->pers; 3654 3655 r = rs_setup_reshape(rs); 3656 if (r) 3657 return r; 3658 3659 /* Need to be resumed to be able to start reshape, recovery is frozen until raid_resume() though */ 3660 if (mddev->suspended) 3661 mddev_resume(mddev); 3662 3663 /* 3664 * Check any reshape constraints enforced by the personalility 3665 * 3666 * May as well already kick the reshape off so that * pers->start_reshape() becomes optional. 3667 */ 3668 r = pers->check_reshape(mddev); 3669 if (r) { 3670 rs->ti->error = "pers->check_reshape() failed"; 3671 return r; 3672 } 3673 3674 /* 3675 * Personality may not provide start reshape method in which 3676 * case check_reshape above has already covered everything 3677 */ 3678 if (pers->start_reshape) { 3679 r = pers->start_reshape(mddev); 3680 if (r) { 3681 rs->ti->error = "pers->start_reshape() failed"; 3682 return r; 3683 } 3684 } 3685 3686 /* Suspend because a resume will happen in raid_resume() */ 3687 if (!mddev->suspended) 3688 mddev_suspend(mddev); 3689 3690 /* 3691 * Now reshape got set up, update superblocks to 3692 * reflect the fact so that a table reload will 3693 * access proper superblock content in the ctr. 3694 */ 3695 rs_update_sbs(rs); 3696 3697 return 0; 3698 } 3699 3700 static int raid_preresume(struct dm_target *ti) 3701 { 3702 int r; 3703 struct raid_set *rs = ti->private; 3704 struct mddev *mddev = &rs->md; 3705 3706 /* This is a resume after a suspend of the set -> it's already started */ 3707 if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags)) 3708 return 0; 3709 3710 /* 3711 * The superblocks need to be updated on disk if the 3712 * array is new or new devices got added (thus zeroed 3713 * out by userspace) or __load_dirty_region_bitmap 3714 * will overwrite them in core with old data or fail. 3715 */ 3716 if (test_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags)) 3717 rs_update_sbs(rs); 3718 3719 /* Load the bitmap from disk unless raid0 */ 3720 r = __load_dirty_region_bitmap(rs); 3721 if (r) 3722 return r; 3723 3724 /* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) */ 3725 if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) && 3726 mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)) { 3727 r = bitmap_resize(mddev->bitmap, mddev->dev_sectors, 3728 to_bytes(rs->requested_bitmap_chunk_sectors), 0); 3729 if (r) 3730 DMERR("Failed to resize bitmap"); 3731 } 3732 3733 /* Check for any resize/reshape on @rs and adjust/initiate */ 3734 /* Be prepared for mddev_resume() in raid_resume() */ 3735 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 3736 if (mddev->recovery_cp && mddev->recovery_cp < MaxSector) { 3737 set_bit(MD_RECOVERY_SYNC, &mddev->recovery); 3738 mddev->resync_min = mddev->recovery_cp; 3739 } 3740 3741 rs_set_capacity(rs); 3742 3743 /* Check for any reshape request unless new raid set */ 3744 if (test_and_clear_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) { 3745 /* Initiate a reshape. */ 3746 mddev_lock_nointr(mddev); 3747 r = rs_start_reshape(rs); 3748 mddev_unlock(mddev); 3749 if (r) 3750 DMWARN("Failed to check/start reshape, continuing without change"); 3751 r = 0; 3752 } 3753 3754 return r; 3755 } 3756 3757 static void raid_resume(struct dm_target *ti) 3758 { 3759 struct raid_set *rs = ti->private; 3760 struct mddev *mddev = &rs->md; 3761 3762 if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) { 3763 /* 3764 * A secondary resume while the device is active. 3765 * Take this opportunity to check whether any failed 3766 * devices are reachable again. 3767 */ 3768 attempt_restore_of_faulty_devices(rs); 3769 } 3770 3771 mddev->ro = 0; 3772 mddev->in_sync = 0; 3773 3774 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); 3775 3776 if (mddev->suspended) 3777 mddev_resume(mddev); 3778 } 3779 3780 static struct target_type raid_target = { 3781 .name = "raid", 3782 .version = {1, 10, 0}, 3783 .module = THIS_MODULE, 3784 .ctr = raid_ctr, 3785 .dtr = raid_dtr, 3786 .map = raid_map, 3787 .status = raid_status, 3788 .message = raid_message, 3789 .iterate_devices = raid_iterate_devices, 3790 .io_hints = raid_io_hints, 3791 .presuspend = raid_presuspend, 3792 .postsuspend = raid_postsuspend, 3793 .preresume = raid_preresume, 3794 .resume = raid_resume, 3795 }; 3796 3797 static int __init dm_raid_init(void) 3798 { 3799 DMINFO("Loading target version %u.%u.%u", 3800 raid_target.version[0], 3801 raid_target.version[1], 3802 raid_target.version[2]); 3803 return dm_register_target(&raid_target); 3804 } 3805 3806 static void __exit dm_raid_exit(void) 3807 { 3808 dm_unregister_target(&raid_target); 3809 } 3810 3811 module_init(dm_raid_init); 3812 module_exit(dm_raid_exit); 3813 3814 module_param(devices_handle_discard_safely, bool, 0644); 3815 MODULE_PARM_DESC(devices_handle_discard_safely, 3816 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions"); 3817 3818 MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target"); 3819 MODULE_ALIAS("dm-raid0"); 3820 MODULE_ALIAS("dm-raid1"); 3821 MODULE_ALIAS("dm-raid10"); 3822 MODULE_ALIAS("dm-raid4"); 3823 MODULE_ALIAS("dm-raid5"); 3824 MODULE_ALIAS("dm-raid6"); 3825 MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>"); 3826 MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>"); 3827 MODULE_LICENSE("GPL"); 3828