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