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