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