dm-raid.c 46.8 KB
Newer Older
N
NeilBrown 已提交
1 2
/*
 * Copyright (C) 2010-2011 Neil Brown
3
 * Copyright (C) 2010-2014 Red Hat, Inc. All rights reserved.
N
NeilBrown 已提交
4 5 6 7 8
 *
 * This file is released under the GPL.
 */

#include <linux/slab.h>
9
#include <linux/module.h>
N
NeilBrown 已提交
10 11

#include "md.h"
J
Jonathan Brassow 已提交
12
#include "raid1.h"
N
NeilBrown 已提交
13
#include "raid5.h"
14
#include "raid10.h"
N
NeilBrown 已提交
15 16
#include "bitmap.h"

A
Alasdair G Kergon 已提交
17 18
#include <linux/device-mapper.h>

N
NeilBrown 已提交
19
#define DM_MSG_PREFIX "raid"
H
Heinz Mauelshagen 已提交
20
#define	MAX_RAID_DEVICES	253 /* raid4/5/6 limit */
N
NeilBrown 已提交
21

22 23
static bool devices_handle_discard_safely = false;

N
NeilBrown 已提交
24
/*
25 26
 * The following flags are used by dm-raid.c to set up the array state.
 * They must be cleared before md_run is called.
N
NeilBrown 已提交
27
 */
28
#define FirstUse 10             /* rdev flag */
N
NeilBrown 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

struct raid_dev {
	/*
	 * Two DM devices, one to hold metadata and one to hold the
	 * actual data/parity.  The reason for this is to not confuse
	 * ti->len and give more flexibility in altering size and
	 * characteristics.
	 *
	 * While it is possible for this device to be associated
	 * with a different physical device than the data_dev, it
	 * is intended for it to be the same.
	 *    |--------- Physical Device ---------|
	 *    |- meta_dev -|------ data_dev ------|
	 */
	struct dm_dev *meta_dev;
	struct dm_dev *data_dev;
45
	struct md_rdev rdev;
N
NeilBrown 已提交
46 47 48
};

/*
H
Heinz Mauelshagen 已提交
49
 * Flags for rs->ctr_flags field.
N
NeilBrown 已提交
50
 */
H
Heinz Mauelshagen 已提交
51 52 53 54 55 56 57 58 59 60 61
#define CTR_FLAG_SYNC              0x1
#define CTR_FLAG_NOSYNC            0x2
#define CTR_FLAG_REBUILD           0x4
#define CTR_FLAG_DAEMON_SLEEP      0x8
#define CTR_FLAG_MIN_RECOVERY_RATE 0x10
#define CTR_FLAG_MAX_RECOVERY_RATE 0x20
#define CTR_FLAG_MAX_WRITE_BEHIND  0x40
#define CTR_FLAG_STRIPE_CACHE      0x80
#define CTR_FLAG_REGION_SIZE       0x100
#define CTR_FLAG_RAID10_COPIES     0x200
#define CTR_FLAG_RAID10_FORMAT     0x400
62

N
NeilBrown 已提交
63 64 65
struct raid_set {
	struct dm_target *ti;

66
	uint32_t bitmap_loaded;
H
Heinz Mauelshagen 已提交
67
	uint32_t ctr_flags;
N
NeilBrown 已提交
68

69
	struct mddev md;
N
NeilBrown 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	struct raid_type *raid_type;
	struct dm_target_callbacks callbacks;

	struct raid_dev dev[0];
};

/* Supported raid types and properties. */
static struct raid_type {
	const char *name;		/* RAID algorithm. */
	const char *descr;		/* Descriptor text for logging. */
	const unsigned parity_devs;	/* # of parity devices. */
	const unsigned minimal_devs;	/* minimal # of devices in set. */
	const unsigned level;		/* RAID level. */
	const unsigned algorithm;	/* RAID algorithm. */
} raid_types[] = {
J
Jonathan Brassow 已提交
85
	{"raid1",    "RAID1 (mirroring)",               0, 2, 1, 0 /* NONE */},
86
	{"raid10",   "RAID10 (striped mirrors)",        0, 2, 10, UINT_MAX /* Varies */},
N
NeilBrown 已提交
87 88 89 90 91 92 93 94 95 96
	{"raid4",    "RAID4 (dedicated parity disk)",	1, 2, 5, ALGORITHM_PARITY_0},
	{"raid5_la", "RAID5 (left asymmetric)",		1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
	{"raid5_ra", "RAID5 (right asymmetric)",	1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
	{"raid5_ls", "RAID5 (left symmetric)",		1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
	{"raid5_rs", "RAID5 (right symmetric)",		1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
	{"raid6_zr", "RAID6 (zero restart)",		2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
	{"raid6_nr", "RAID6 (N restart)",		2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
	{"raid6_nc", "RAID6 (N continue)",		2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
};

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
static char *raid10_md_layout_to_format(int layout)
{
	/*
	 * Bit 16 and 17 stand for "offset" and "use_far_sets"
	 * Refer to MD's raid10.c for details
	 */
	if ((layout & 0x10000) && (layout & 0x20000))
		return "offset";

	if ((layout & 0xFF) > 1)
		return "near";

	return "far";
}

112 113
static unsigned raid10_md_layout_to_copies(int layout)
{
114 115 116
	if ((layout & 0xFF) > 1)
		return layout & 0xFF;
	return (layout >> 8) & 0xFF;
117 118 119 120
}

static int raid10_format_to_md_layout(char *format, unsigned copies)
{
121 122
	unsigned n = 1, f = 1;

H
Heinz Mauelshagen 已提交
123
	if (!strcasecmp("near", format))
124 125 126 127
		n = copies;
	else
		f = copies;

H
Heinz Mauelshagen 已提交
128
	if (!strcasecmp("offset", format))
129 130
		return 0x30000 | (f << 8) | n;

H
Heinz Mauelshagen 已提交
131
	if (!strcasecmp("far", format))
132 133 134
		return 0x20000 | (f << 8) | n;

	return (f << 8) | n;
135 136
}

N
NeilBrown 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
static struct raid_type *get_raid_type(char *name)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(raid_types); i++)
		if (!strcmp(raid_types[i].name, name))
			return &raid_types[i];

	return NULL;
}

static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
{
	unsigned i;
	struct raid_set *rs;

	if (raid_devs <= raid_type->parity_devs) {
		ti->error = "Insufficient number of devices";
		return ERR_PTR(-EINVAL);
	}

	rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
	if (!rs) {
		ti->error = "Cannot allocate raid context";
		return ERR_PTR(-ENOMEM);
	}

	mddev_init(&rs->md);

	rs->ti = ti;
	rs->raid_type = raid_type;
	rs->md.raid_disks = raid_devs;
	rs->md.level = raid_type->level;
	rs->md.new_level = rs->md.level;
	rs->md.layout = raid_type->algorithm;
	rs->md.new_layout = rs->md.layout;
	rs->md.delta_disks = 0;
	rs->md.recovery_cp = 0;

	for (i = 0; i < raid_devs; i++)
		md_rdev_init(&rs->dev[i].rdev);

	/*
	 * Remaining items to be initialized by further RAID params:
	 *  rs->md.persistent
	 *  rs->md.external
	 *  rs->md.chunk_sectors
	 *  rs->md.new_chunk_sectors
185
	 *  rs->md.dev_sectors
N
NeilBrown 已提交
186 187 188 189 190 191 192 193 194
	 */

	return rs;
}

static void context_free(struct raid_set *rs)
{
	int i;

195 196 197
	for (i = 0; i < rs->md.raid_disks; i++) {
		if (rs->dev[i].meta_dev)
			dm_put_device(rs->ti, rs->dev[i].meta_dev);
198
		md_rdev_clear(&rs->dev[i].rdev);
N
NeilBrown 已提交
199 200
		if (rs->dev[i].data_dev)
			dm_put_device(rs->ti, rs->dev[i].data_dev);
201
	}
N
NeilBrown 已提交
202 203 204 205 206 207 208 209 210

	kfree(rs);
}

/*
 * For every device we have two words
 *  <meta_dev>: meta device name or '-' if missing
 *  <data_dev>: data device name or '-' if missing
 *
211 212 213 214 215 216 217 218 219 220
 * The following are permitted:
 *    - -
 *    - <data_dev>
 *    <meta_dev> <data_dev>
 *
 * The following is not allowed:
 *    <meta_dev> -
 *
 * This code parses those words.  If there is a failure,
 * the caller must use context_free to unwind the operations.
N
NeilBrown 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
 */
static int dev_parms(struct raid_set *rs, char **argv)
{
	int i;
	int rebuild = 0;
	int metadata_available = 0;
	int ret = 0;

	for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {
		rs->dev[i].rdev.raid_disk = i;

		rs->dev[i].meta_dev = NULL;
		rs->dev[i].data_dev = NULL;

		/*
		 * There are no offsets, since there is a separate device
		 * for data and metadata.
		 */
		rs->dev[i].rdev.data_offset = 0;
		rs->dev[i].rdev.mddev = &rs->md;

		if (strcmp(argv[0], "-")) {
243 244 245 246 247 248 249 250 251 252
			ret = dm_get_device(rs->ti, argv[0],
					    dm_table_get_mode(rs->ti->table),
					    &rs->dev[i].meta_dev);
			rs->ti->error = "RAID metadata device lookup failure";
			if (ret)
				return ret;

			rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
			if (!rs->dev[i].rdev.sb_page)
				return -ENOMEM;
N
NeilBrown 已提交
253 254 255 256 257 258 259 260 261
		}

		if (!strcmp(argv[1], "-")) {
			if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
			    (!rs->dev[i].rdev.recovery_offset)) {
				rs->ti->error = "Drive designated for rebuild not specified";
				return -EINVAL;
			}

262 263 264 265
			rs->ti->error = "No data device supplied with metadata device";
			if (rs->dev[i].meta_dev)
				return -EINVAL;

N
NeilBrown 已提交
266 267 268 269 270 271 272 273 274 275 276
			continue;
		}

		ret = dm_get_device(rs->ti, argv[1],
				    dm_table_get_mode(rs->ti->table),
				    &rs->dev[i].data_dev);
		if (ret) {
			rs->ti->error = "RAID device lookup failure";
			return ret;
		}

277 278 279 280
		if (rs->dev[i].meta_dev) {
			metadata_available = 1;
			rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
		}
N
NeilBrown 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
		rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
		list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
		if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
			rebuild++;
	}

	if (metadata_available) {
		rs->md.external = 0;
		rs->md.persistent = 1;
		rs->md.major_version = 2;
	} else if (rebuild && !rs->md.recovery_cp) {
		/*
		 * Without metadata, we will not be able to tell if the array
		 * is in-sync or not - we must assume it is not.  Therefore,
		 * it is impossible to rebuild a drive.
		 *
		 * Even if there is metadata, the on-disk information may
		 * indicate that the array is not in-sync and it will then
		 * fail at that time.
		 *
		 * User could specify 'nosync' option if desperate.
		 */
		DMERR("Unable to rebuild drive while array is not in-sync");
		rs->ti->error = "RAID device lookup failure";
		return -EINVAL;
	}

	return 0;
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
/*
 * validate_region_size
 * @rs
 * @region_size:  region size in sectors.  If 0, pick a size (4MiB default).
 *
 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
 *
 * Returns: 0 on success, -EINVAL on failure.
 */
static int validate_region_size(struct raid_set *rs, unsigned long region_size)
{
	unsigned long min_region_size = rs->ti->len / (1 << 21);

	if (!region_size) {
		/*
		 * Choose a reasonable default.  All figures in sectors.
		 */
		if (min_region_size > (1 << 13)) {
330 331 332
			/* If not a power of 2, make it the next power of 2 */
			if (min_region_size & (min_region_size - 1))
				region_size = 1 << fls(region_size);
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
			DMINFO("Choosing default region size of %lu sectors",
			       region_size);
		} else {
			DMINFO("Choosing default region size of 4MiB");
			region_size = 1 << 13; /* sectors */
		}
	} else {
		/*
		 * Validate user-supplied value.
		 */
		if (region_size > rs->ti->len) {
			rs->ti->error = "Supplied region size is too large";
			return -EINVAL;
		}

		if (region_size < min_region_size) {
			DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
			      region_size, min_region_size);
			rs->ti->error = "Supplied region size is too small";
			return -EINVAL;
		}

		if (!is_power_of_2(region_size)) {
			rs->ti->error = "Region size is not a power of 2";
			return -EINVAL;
		}

		if (region_size < rs->md.chunk_sectors) {
			rs->ti->error = "Region size is smaller than the chunk size";
			return -EINVAL;
		}
	}

	/*
	 * Convert sectors to bytes.
	 */
	rs->md.bitmap_info.chunksize = (region_size << 9);

	return 0;
}

374
/*
375
 * validate_raid_redundancy
376 377
 * @rs
 *
378 379
 * Determine if there are enough devices in the array that haven't
 * failed (or are being rebuilt) to form a usable array.
380 381 382
 *
 * Returns: 0 on success, -EINVAL on failure.
 */
383
static int validate_raid_redundancy(struct raid_set *rs)
384 385
{
	unsigned i, rebuild_cnt = 0;
386
	unsigned rebuilds_per_group = 0, copies, d;
387
	unsigned group_size, last_group_start;
388 389

	for (i = 0; i < rs->md.raid_disks; i++)
390 391
		if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
		    !rs->dev[i].rdev.sb_page)
392 393 394 395 396 397 398 399 400 401 402 403 404 405
			rebuild_cnt++;

	switch (rs->raid_type->level) {
	case 1:
		if (rebuild_cnt >= rs->md.raid_disks)
			goto too_many;
		break;
	case 4:
	case 5:
	case 6:
		if (rebuild_cnt > rs->raid_type->parity_devs)
			goto too_many;
		break;
	case 10:
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
		copies = raid10_md_layout_to_copies(rs->md.layout);
		if (rebuild_cnt < copies)
			break;

		/*
		 * It is possible to have a higher rebuild count for RAID10,
		 * as long as the failed devices occur in different mirror
		 * groups (i.e. different stripes).
		 *
		 * When checking "near" format, make sure no adjacent devices
		 * have failed beyond what can be handled.  In addition to the
		 * simple case where the number of devices is a multiple of the
		 * number of copies, we must also handle cases where the number
		 * of devices is not a multiple of the number of copies.
		 * E.g.    dev1 dev2 dev3 dev4 dev5
		 *          A    A    B    B    C
		 *          C    D    D    E    E
		 */
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
		if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
			for (i = 0; i < rs->md.raid_disks * copies; i++) {
				if (!(i % copies))
					rebuilds_per_group = 0;
				d = i % rs->md.raid_disks;
				if ((!rs->dev[d].rdev.sb_page ||
				     !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
				    (++rebuilds_per_group >= copies))
					goto too_many;
			}
			break;
		}

		/*
		 * When checking "far" and "offset" formats, we need to ensure
		 * that the device that holds its copy is not also dead or
		 * being rebuilt.  (Note that "far" and "offset" formats only
		 * support two copies right now.  These formats also only ever
		 * use the 'use_far_sets' variant.)
		 *
		 * This check is somewhat complicated by the need to account
		 * for arrays that are not a multiple of (far) copies.  This
		 * results in the need to treat the last (potentially larger)
		 * set differently.
		 */
		group_size = (rs->md.raid_disks / copies);
		last_group_start = (rs->md.raid_disks / group_size) - 1;
		last_group_start *= group_size;
		for (i = 0; i < rs->md.raid_disks; i++) {
			if (!(i % copies) && !(i > last_group_start))
454
				rebuilds_per_group = 0;
455 456
			if ((!rs->dev[i].rdev.sb_page ||
			     !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
457
			    (++rebuilds_per_group >= copies))
458
					goto too_many;
459 460
		}
		break;
461
	default:
462 463
		if (rebuild_cnt)
			return -EINVAL;
464 465 466 467 468 469 470 471
	}

	return 0;

too_many:
	return -EINVAL;
}

N
NeilBrown 已提交
472 473 474 475
/*
 * Possible arguments are...
 *	<chunk_size> [optional_args]
 *
J
Jonathan Brassow 已提交
476 477 478 479 480
 * Argument definitions
 *    <chunk_size>			The number of sectors per disk that
 *                                      will form the "stripe"
 *    [[no]sync]			Force or prevent recovery of the
 *                                      entire array
N
NeilBrown 已提交
481
 *    [rebuild <idx>]			Rebuild the drive indicated by the index
J
Jonathan Brassow 已提交
482 483
 *    [daemon_sleep <ms>]		Time between bitmap daemon work to
 *                                      clear bits
N
NeilBrown 已提交
484 485
 *    [min_recovery_rate <kB/sec/disk>]	Throttle RAID initialization
 *    [max_recovery_rate <kB/sec/disk>]	Throttle RAID initialization
486
 *    [write_mostly <idx>]		Indicate a write mostly drive via index
N
NeilBrown 已提交
487 488
 *    [max_write_behind <sectors>]	See '-write-behind=' (man mdadm)
 *    [stripe_cache <sectors>]		Stripe cache size for higher RAIDs
489
 *    [region_size <sectors>]           Defines granularity of bitmap
490 491 492
 *
 * RAID10-only options:
 *    [raid10_copies <# copies>]        Number of copies.  (Default: 2)
493
 *    [raid10_format <near|far|offset>] Layout algorithm.  (Default: near)
N
NeilBrown 已提交
494 495 496 497
 */
static int parse_raid_params(struct raid_set *rs, char **argv,
			     unsigned num_raid_params)
{
498 499
	char *raid10_format = "near";
	unsigned raid10_copies = 2;
500
	unsigned i;
501
	unsigned long value, region_size = 0;
502
	sector_t sectors_per_dev = rs->ti->len;
503
	sector_t max_io_len;
N
NeilBrown 已提交
504 505 506 507
	char *key;

	/*
	 * First, parse the in-order required arguments
J
Jonathan Brassow 已提交
508
	 * "chunk_size" is the only argument of this type.
N
NeilBrown 已提交
509
	 */
510
	if ((kstrtoul(argv[0], 10, &value) < 0)) {
N
NeilBrown 已提交
511 512
		rs->ti->error = "Bad chunk size";
		return -EINVAL;
J
Jonathan Brassow 已提交
513 514 515 516 517 518 519 520 521 522
	} else if (rs->raid_type->level == 1) {
		if (value)
			DMERR("Ignoring chunk size parameter for RAID 1");
		value = 0;
	} else if (!is_power_of_2(value)) {
		rs->ti->error = "Chunk size must be a power of 2";
		return -EINVAL;
	} else if (value < 8) {
		rs->ti->error = "Chunk size value is too small";
		return -EINVAL;
N
NeilBrown 已提交
523 524 525 526 527 528 529
	}

	rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
	argv++;
	num_raid_params--;

	/*
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	 * We set each individual device as In_sync with a completed
	 * 'recovery_offset'.  If there has been a device failure or
	 * replacement then one of the following cases applies:
	 *
	 *   1) User specifies 'rebuild'.
	 *      - Device is reset when param is read.
	 *   2) A new device is supplied.
	 *      - No matching superblock found, resets device.
	 *   3) Device failure was transient and returns on reload.
	 *      - Failure noticed, resets device for bitmap replay.
	 *   4) Device hadn't completed recovery after previous failure.
	 *      - Superblock is read and overrides recovery_offset.
	 *
	 * What is found in the superblocks of the devices is always
	 * authoritative, unless 'rebuild' or '[no]sync' was specified.
N
NeilBrown 已提交
545
	 */
546
	for (i = 0; i < rs->md.raid_disks; i++) {
N
NeilBrown 已提交
547
		set_bit(In_sync, &rs->dev[i].rdev.flags);
548 549
		rs->dev[i].rdev.recovery_offset = MaxSector;
	}
N
NeilBrown 已提交
550

551 552 553
	/*
	 * Second, parse the unordered optional arguments
	 */
N
NeilBrown 已提交
554
	for (i = 0; i < num_raid_params; i++) {
555
		if (!strcasecmp(argv[i], "nosync")) {
N
NeilBrown 已提交
556
			rs->md.recovery_cp = MaxSector;
H
Heinz Mauelshagen 已提交
557
			rs->ctr_flags |= CTR_FLAG_NOSYNC;
N
NeilBrown 已提交
558 559
			continue;
		}
560
		if (!strcasecmp(argv[i], "sync")) {
N
NeilBrown 已提交
561
			rs->md.recovery_cp = 0;
H
Heinz Mauelshagen 已提交
562
			rs->ctr_flags |= CTR_FLAG_SYNC;
N
NeilBrown 已提交
563 564 565 566 567 568 569 570 571 572
			continue;
		}

		/* The rest of the optional arguments come in key/value pairs */
		if ((i + 1) >= num_raid_params) {
			rs->ti->error = "Wrong number of raid parameters given";
			return -EINVAL;
		}

		key = argv[i++];
573 574 575 576 577 578 579

		/* Parameters that take a string value are checked here. */
		if (!strcasecmp(key, "raid10_format")) {
			if (rs->raid_type->level != 10) {
				rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
				return -EINVAL;
			}
580 581 582
			if (strcmp("near", argv[i]) &&
			    strcmp("far", argv[i]) &&
			    strcmp("offset", argv[i])) {
583 584 585 586
				rs->ti->error = "Invalid 'raid10_format' value given";
				return -EINVAL;
			}
			raid10_format = argv[i];
H
Heinz Mauelshagen 已提交
587
			rs->ctr_flags |= CTR_FLAG_RAID10_FORMAT;
588 589 590
			continue;
		}

591
		if (kstrtoul(argv[i], 10, &value) < 0) {
N
NeilBrown 已提交
592 593 594 595
			rs->ti->error = "Bad numerical argument given in raid params";
			return -EINVAL;
		}

596
		/* Parameters that take a numeric value are checked here */
597
		if (!strcasecmp(key, "rebuild")) {
598
			if (value >= rs->md.raid_disks) {
N
NeilBrown 已提交
599 600 601 602 603
				rs->ti->error = "Invalid rebuild index given";
				return -EINVAL;
			}
			clear_bit(In_sync, &rs->dev[value].rdev.flags);
			rs->dev[value].rdev.recovery_offset = 0;
H
Heinz Mauelshagen 已提交
604
			rs->ctr_flags |= CTR_FLAG_REBUILD;
605 606 607 608 609
		} else if (!strcasecmp(key, "write_mostly")) {
			if (rs->raid_type->level != 1) {
				rs->ti->error = "write_mostly option is only valid for RAID1";
				return -EINVAL;
			}
610
			if (value >= rs->md.raid_disks) {
611 612 613 614
				rs->ti->error = "Invalid write_mostly drive index given";
				return -EINVAL;
			}
			set_bit(WriteMostly, &rs->dev[value].rdev.flags);
615
		} else if (!strcasecmp(key, "max_write_behind")) {
616 617 618 619
			if (rs->raid_type->level != 1) {
				rs->ti->error = "max_write_behind option is only valid for RAID1";
				return -EINVAL;
			}
H
Heinz Mauelshagen 已提交
620
			rs->ctr_flags |= CTR_FLAG_MAX_WRITE_BEHIND;
N
NeilBrown 已提交
621 622 623 624 625 626 627 628 629 630 631

			/*
			 * In device-mapper, we specify things in sectors, but
			 * MD records this value in kB
			 */
			value /= 2;
			if (value > COUNTER_MAX) {
				rs->ti->error = "Max write-behind limit out of range";
				return -EINVAL;
			}
			rs->md.bitmap_info.max_write_behind = value;
632
		} else if (!strcasecmp(key, "daemon_sleep")) {
H
Heinz Mauelshagen 已提交
633
			rs->ctr_flags |= CTR_FLAG_DAEMON_SLEEP;
N
NeilBrown 已提交
634 635 636 637 638
			if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
				rs->ti->error = "daemon sleep period out of range";
				return -EINVAL;
			}
			rs->md.bitmap_info.daemon_sleep = value;
639
		} else if (!strcasecmp(key, "stripe_cache")) {
H
Heinz Mauelshagen 已提交
640
			rs->ctr_flags |= CTR_FLAG_STRIPE_CACHE;
N
NeilBrown 已提交
641 642 643 644 645 646 647

			/*
			 * In device-mapper, we specify things in sectors, but
			 * MD records this value in kB
			 */
			value /= 2;

648 649
			if ((rs->raid_type->level != 5) &&
			    (rs->raid_type->level != 6)) {
N
NeilBrown 已提交
650 651 652 653 654 655 656
				rs->ti->error = "Inappropriate argument: stripe_cache";
				return -EINVAL;
			}
			if (raid5_set_cache_size(&rs->md, (int)value)) {
				rs->ti->error = "Bad stripe_cache size";
				return -EINVAL;
			}
657
		} else if (!strcasecmp(key, "min_recovery_rate")) {
H
Heinz Mauelshagen 已提交
658
			rs->ctr_flags |= CTR_FLAG_MIN_RECOVERY_RATE;
N
NeilBrown 已提交
659 660 661 662 663
			if (value > INT_MAX) {
				rs->ti->error = "min_recovery_rate out of range";
				return -EINVAL;
			}
			rs->md.sync_speed_min = (int)value;
664
		} else if (!strcasecmp(key, "max_recovery_rate")) {
H
Heinz Mauelshagen 已提交
665
			rs->ctr_flags |= CTR_FLAG_MAX_RECOVERY_RATE;
N
NeilBrown 已提交
666 667 668 669 670
			if (value > INT_MAX) {
				rs->ti->error = "max_recovery_rate out of range";
				return -EINVAL;
			}
			rs->md.sync_speed_max = (int)value;
671
		} else if (!strcasecmp(key, "region_size")) {
H
Heinz Mauelshagen 已提交
672
			rs->ctr_flags |= CTR_FLAG_REGION_SIZE;
673
			region_size = value;
674 675 676 677 678 679
		} else if (!strcasecmp(key, "raid10_copies") &&
			   (rs->raid_type->level == 10)) {
			if ((value < 2) || (value > 0xFF)) {
				rs->ti->error = "Bad value for 'raid10_copies'";
				return -EINVAL;
			}
H
Heinz Mauelshagen 已提交
680
			rs->ctr_flags |= CTR_FLAG_RAID10_COPIES;
681
			raid10_copies = value;
N
NeilBrown 已提交
682 683 684 685 686 687 688
		} else {
			DMERR("Unable to parse RAID parameter: %s", key);
			rs->ti->error = "Unable to parse RAID parameters";
			return -EINVAL;
		}
	}

689 690 691 692
	if (validate_region_size(rs, region_size))
		return -EINVAL;

	if (rs->md.chunk_sectors)
693
		max_io_len = rs->md.chunk_sectors;
694
	else
695
		max_io_len = region_size;
696

697 698
	if (dm_set_target_max_io_len(rs->ti, max_io_len))
		return -EINVAL;
J
Jonathan Brassow 已提交
699

700 701 702 703 704 705
	if (rs->raid_type->level == 10) {
		if (raid10_copies > rs->md.raid_disks) {
			rs->ti->error = "Not enough devices to satisfy specification";
			return -EINVAL;
		}

706 707 708 709 710 711 712 713 714
		/*
		 * If the format is not "near", we only support
		 * two copies at the moment.
		 */
		if (strcmp("near", raid10_format) && (raid10_copies > 2)) {
			rs->ti->error = "Too many copies for given RAID10 format.";
			return -EINVAL;
		}

715 716 717 718 719 720 721 722 723 724
		/* (Len * #mirrors) / #devices */
		sectors_per_dev = rs->ti->len * raid10_copies;
		sector_div(sectors_per_dev, rs->md.raid_disks);

		rs->md.layout = raid10_format_to_md_layout(raid10_format,
							   raid10_copies);
		rs->md.new_layout = rs->md.layout;
	} else if ((rs->raid_type->level > 1) &&
		   sector_div(sectors_per_dev,
			      (rs->md.raid_disks - rs->raid_type->parity_devs))) {
725 726 727 728 729
		rs->ti->error = "Target length not divisible by number of data devices";
		return -EINVAL;
	}
	rs->md.dev_sectors = sectors_per_dev;

N
NeilBrown 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
	/* Assume there are no metadata devices until the drives are parsed */
	rs->md.persistent = 0;
	rs->md.external = 1;

	return 0;
}

static void do_table_event(struct work_struct *ws)
{
	struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);

	dm_table_event(rs->ti->table);
}

static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
{
	struct raid_set *rs = container_of(cb, struct raid_set, callbacks);

748
	return mddev_congested(&rs->md, bits);
N
NeilBrown 已提交
749 750
}

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
/*
 * This structure is never routinely used by userspace, unlike md superblocks.
 * Devices with this superblock should only ever be accessed via device-mapper.
 */
#define DM_RAID_MAGIC 0x64526D44
struct dm_raid_superblock {
	__le32 magic;		/* "DmRd" */
	__le32 features;	/* Used to indicate possible future changes */

	__le32 num_devices;	/* Number of devices in this array. (Max 64) */
	__le32 array_position;	/* The position of this drive in the array */

	__le64 events;		/* Incremented by md when superblock updated */
	__le64 failed_devices;	/* Bit field of devices to indicate failures */

	/*
	 * This offset tracks the progress of the repair or replacement of
	 * an individual drive.
	 */
	__le64 disk_recovery_offset;

	/*
	 * This offset tracks the progress of the initial array
	 * synchronisation/parity calculation.
	 */
	__le64 array_resync_offset;

	/*
	 * RAID characteristics
	 */
	__le32 level;
	__le32 layout;
	__le32 stripe_sectors;

785
	/* Remainder of a logical block is zero-filled when writing (see super_sync()). */
786 787
} __packed;

788
static int read_disk_sb(struct md_rdev *rdev, int size)
789 790 791 792 793 794 795
{
	BUG_ON(!rdev->sb_page);

	if (rdev->sb_loaded)
		return 0;

	if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 1)) {
796 797
		DMERR("Failed to read superblock of device at position %d",
		      rdev->raid_disk);
798
		md_error(rdev->mddev, rdev);
799 800 801 802 803 804 805 806
		return -EINVAL;
	}

	rdev->sb_loaded = 1;

	return 0;
}

807
static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
808
{
809
	int i;
810 811
	uint64_t failed_devices;
	struct dm_raid_superblock *sb;
812
	struct raid_set *rs = container_of(mddev, struct raid_set, md);
813 814 815 816

	sb = page_address(rdev->sb_page);
	failed_devices = le64_to_cpu(sb->failed_devices);

817 818 819 820
	for (i = 0; i < mddev->raid_disks; i++)
		if (!rs->dev[i].data_dev ||
		    test_bit(Faulty, &(rs->dev[i].rdev.flags)))
			failed_devices |= (1ULL << i);
821

822
	memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848

	sb->magic = cpu_to_le32(DM_RAID_MAGIC);
	sb->features = cpu_to_le32(0);	/* No features yet */

	sb->num_devices = cpu_to_le32(mddev->raid_disks);
	sb->array_position = cpu_to_le32(rdev->raid_disk);

	sb->events = cpu_to_le64(mddev->events);
	sb->failed_devices = cpu_to_le64(failed_devices);

	sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
	sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);

	sb->level = cpu_to_le32(mddev->level);
	sb->layout = cpu_to_le32(mddev->layout);
	sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
}

/*
 * super_load
 *
 * This function creates a superblock if one is not found on the device
 * and will decide which superblock to use if there's a choice.
 *
 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
 */
849
static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
850 851 852 853 854 855 856
{
	int ret;
	struct dm_raid_superblock *sb;
	struct dm_raid_superblock *refsb;
	uint64_t events_sb, events_refsb;

	rdev->sb_start = 0;
857 858 859 860 861
	rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
	if (rdev->sb_size < sizeof(*sb) || rdev->sb_size > PAGE_SIZE) {
		DMERR("superblock size of a logical block is no longer valid");
		return -EINVAL;
	}
862 863 864 865 866 867

	ret = read_disk_sb(rdev, rdev->sb_size);
	if (ret)
		return ret;

	sb = page_address(rdev->sb_page);
868 869 870 871 872 873 874 875

	/*
	 * Two cases that we want to write new superblocks and rebuild:
	 * 1) New device (no matching magic number)
	 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
	 */
	if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
	    (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
		super_sync(rdev->mddev, rdev);

		set_bit(FirstUse, &rdev->flags);

		/* Force writing of superblocks to disk */
		set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);

		/* Any superblock is better than none, choose that if given */
		return refdev ? 0 : 1;
	}

	if (!refdev)
		return 1;

	events_sb = le64_to_cpu(sb->events);

	refsb = page_address(refdev->sb_page);
	events_refsb = le64_to_cpu(refsb->events);

	return (events_sb > events_refsb) ? 1 : 0;
}

898
static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
899 900 901 902 903 904 905 906
{
	int role;
	struct raid_set *rs = container_of(mddev, struct raid_set, md);
	uint64_t events_sb;
	uint64_t failed_devices;
	struct dm_raid_superblock *sb;
	uint32_t new_devs = 0;
	uint32_t rebuilds = 0;
N
NeilBrown 已提交
907
	struct md_rdev *r;
908 909 910 911 912 913 914 915 916 917 918 919 920 921
	struct dm_raid_superblock *sb2;

	sb = page_address(rdev->sb_page);
	events_sb = le64_to_cpu(sb->events);
	failed_devices = le64_to_cpu(sb->failed_devices);

	/*
	 * Initialise to 1 if this is a new superblock.
	 */
	mddev->events = events_sb ? : 1;

	/*
	 * Reshaping is not currently allowed
	 */
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
	if (le32_to_cpu(sb->level) != mddev->level) {
		DMERR("Reshaping arrays not yet supported. (RAID level change)");
		return -EINVAL;
	}
	if (le32_to_cpu(sb->layout) != mddev->layout) {
		DMERR("Reshaping arrays not yet supported. (RAID layout change)");
		DMERR("  0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
		DMERR("  Old layout: %s w/ %d copies",
		      raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
		      raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
		DMERR("  New layout: %s w/ %d copies",
		      raid10_md_layout_to_format(mddev->layout),
		      raid10_md_layout_to_copies(mddev->layout));
		return -EINVAL;
	}
	if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
		DMERR("Reshaping arrays not yet supported. (stripe sectors change)");
939 940 941 942 943 944
		return -EINVAL;
	}

	/* We can only change the number of devices in RAID1 right now */
	if ((rs->raid_type->level != 1) &&
	    (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
945
		DMERR("Reshaping arrays not yet supported. (device count change)");
946 947 948
		return -EINVAL;
	}

H
Heinz Mauelshagen 已提交
949
	if (!(rs->ctr_flags & (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)))
950 951 952 953 954 955 956 957 958 959 960 961 962
		mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);

	/*
	 * During load, we set FirstUse if a new superblock was written.
	 * There are two reasons we might not have a superblock:
	 * 1) The array is brand new - in which case, all of the
	 *    devices must have their In_sync bit set.  Also,
	 *    recovery_cp must be 0, unless forced.
	 * 2) This is a new device being added to an old array
	 *    and the new device needs to be rebuilt - in which
	 *    case the In_sync bit will /not/ be set and
	 *    recovery_cp must be MaxSector.
	 */
N
NeilBrown 已提交
963
	rdev_for_each(r, mddev) {
964
		if (!test_bit(In_sync, &r->flags)) {
965 966
			DMINFO("Device %d specified for rebuild: "
			       "Clearing superblock", r->raid_disk);
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
			rebuilds++;
		} else if (test_bit(FirstUse, &r->flags))
			new_devs++;
	}

	if (!rebuilds) {
		if (new_devs == mddev->raid_disks) {
			DMINFO("Superblocks created for new array");
			set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
		} else if (new_devs) {
			DMERR("New device injected "
			      "into existing array without 'rebuild' "
			      "parameter specified");
			return -EINVAL;
		}
	} else if (new_devs) {
		DMERR("'rebuild' devices cannot be "
		      "injected into an array with other first-time devices");
		return -EINVAL;
	} else if (mddev->recovery_cp != MaxSector) {
		DMERR("'rebuild' specified while array is not in-sync");
		return -EINVAL;
	}

	/*
	 * Now we set the Faulty bit for those devices that are
	 * recorded in the superblock as failed.
	 */
N
NeilBrown 已提交
995
	rdev_for_each(r, mddev) {
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
		if (!r->sb_page)
			continue;
		sb2 = page_address(r->sb_page);
		sb2->failed_devices = 0;

		/*
		 * Check for any device re-ordering.
		 */
		if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
			role = le32_to_cpu(sb2->array_position);
			if (role != r->raid_disk) {
				if (rs->raid_type->level != 1) {
					rs->ti->error = "Cannot change device "
						"positions in RAID array";
					return -EINVAL;
				}
				DMINFO("RAID1 device #%d now at position #%d",
				       role, r->raid_disk);
			}

			/*
			 * Partial recovery is performed on
			 * returning failed devices.
			 */
			if (failed_devices & (1 << role))
				set_bit(Faulty, &r->flags);
		}
	}

	return 0;
}

1028
static int super_validate(struct mddev *mddev, struct md_rdev *rdev)
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
{
	struct dm_raid_superblock *sb = page_address(rdev->sb_page);

	/*
	 * If mddev->events is not set, we know we have not yet initialized
	 * the array.
	 */
	if (!mddev->events && super_init_validation(mddev, rdev))
		return -EINVAL;

	mddev->bitmap_info.offset = 4096 >> 9; /* Enable bitmap creation */
	rdev->mddev->bitmap_info.default_offset = 4096 >> 9;
	if (!test_bit(FirstUse, &rdev->flags)) {
		rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
		if (rdev->recovery_offset != MaxSector)
			clear_bit(In_sync, &rdev->flags);
	}

	/*
	 * If a device comes back, set it as not In_sync and no longer faulty.
	 */
	if (test_bit(Faulty, &rdev->flags)) {
		clear_bit(Faulty, &rdev->flags);
		clear_bit(In_sync, &rdev->flags);
		rdev->saved_raid_disk = rdev->raid_disk;
		rdev->recovery_offset = 0;
	}

	clear_bit(FirstUse, &rdev->flags);

	return 0;
}

/*
 * Analyse superblocks and select the freshest.
 */
static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
{
	int ret;
1068
	struct raid_dev *dev;
1069
	struct md_rdev *rdev, *tmp, *freshest;
1070
	struct mddev *mddev = &rs->md;
1071 1072

	freshest = NULL;
1073
	rdev_for_each_safe(rdev, tmp, mddev) {
1074
		/*
H
Heinz Mauelshagen 已提交
1075
		 * Skipping super_load due to CTR_FLAG_SYNC will cause
1076 1077 1078 1079 1080 1081 1082 1083
		 * the array to undergo initialization again as
		 * though it were new.  This is the intended effect
		 * of the "sync" directive.
		 *
		 * When reshaping capability is added, we must ensure
		 * that the "sync" directive is disallowed during the
		 * reshape.
		 */
H
Heinz Mauelshagen 已提交
1084
		if (rs->ctr_flags & CTR_FLAG_SYNC)
1085 1086
			continue;

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
		if (!rdev->meta_bdev)
			continue;

		ret = super_load(rdev, freshest);

		switch (ret) {
		case 1:
			freshest = rdev;
			break;
		case 0:
			break;
		default:
1099
			dev = container_of(rdev, struct raid_dev, rdev);
1100 1101
			if (dev->meta_dev)
				dm_put_device(ti, dev->meta_dev);
1102

1103 1104
			dev->meta_dev = NULL;
			rdev->meta_bdev = NULL;
1105

1106 1107
			if (rdev->sb_page)
				put_page(rdev->sb_page);
1108

1109
			rdev->sb_page = NULL;
1110

1111
			rdev->sb_loaded = 0;
1112

1113 1114 1115 1116 1117 1118 1119 1120
			/*
			 * We might be able to salvage the data device
			 * even though the meta device has failed.  For
			 * now, we behave as though '- -' had been
			 * set for this device in the table.
			 */
			if (dev->data_dev)
				dm_put_device(ti, dev->data_dev);
1121

1122 1123
			dev->data_dev = NULL;
			rdev->bdev = NULL;
1124

1125
			list_del(&rdev->same_set);
1126 1127 1128 1129 1130 1131
		}
	}

	if (!freshest)
		return 0;

1132 1133 1134 1135 1136
	if (validate_raid_redundancy(rs)) {
		rs->ti->error = "Insufficient redundancy to activate array";
		return -EINVAL;
	}

1137 1138 1139 1140 1141 1142 1143 1144
	/*
	 * Validation of the freshest device provides the source of
	 * validation for the remaining devices.
	 */
	ti->error = "Unable to assemble array: Invalid superblocks";
	if (super_validate(mddev, freshest))
		return -EINVAL;

N
NeilBrown 已提交
1145
	rdev_for_each(rdev, mddev)
1146 1147 1148 1149 1150 1151
		if ((rdev != freshest) && super_validate(mddev, rdev))
			return -EINVAL;

	return 0;
}

1152
/*
1153 1154
 * Enable/disable discard support on RAID set depending on
 * RAID level and discard properties of underlying RAID members.
1155 1156 1157
 */
static void configure_discard_support(struct dm_target *ti, struct raid_set *rs)
{
1158 1159 1160
	int i;
	bool raid456;

1161 1162 1163 1164
	/* Assume discards not supported until after checks below. */
	ti->discards_supported = false;

	/* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
1165
	raid456 = (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6);
1166

1167
	for (i = 0; i < rs->md.raid_disks; i++) {
1168
		struct request_queue *q;
1169

1170 1171 1172 1173
		if (!rs->dev[i].rdev.bdev)
			continue;

		q = bdev_get_queue(rs->dev[i].rdev.bdev);
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
		if (!q || !blk_queue_discard(q))
			return;

		if (raid456) {
			if (!q->limits.discard_zeroes_data)
				return;
			if (!devices_handle_discard_safely) {
				DMERR("raid456 discard support disabled due to discard_zeroes_data uncertainty.");
				DMERR("Set dm-raid.devices_handle_discard_safely=Y to override.");
				return;
			}
		}
	}

	/* All RAID members properly support discards */
1189 1190 1191 1192
	ti->discards_supported = true;

	/*
	 * RAID1 and RAID10 personalities require bio splitting,
1193
	 * RAID0/4/5/6 don't and process large discard bios properly.
1194
	 */
1195
	ti->split_discard_bios = !!(rs->md.level == 1 || rs->md.level == 10);
1196 1197 1198
	ti->num_discard_bios = 1;
}

N
NeilBrown 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
/*
 * Construct a RAID4/5/6 mapping:
 * Args:
 *	<raid_type> <#raid_params> <raid_params>		\
 *	<#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
 *
 * <raid_params> varies by <raid_type>.  See 'parse_raid_params' for
 * details on possible <raid_params>.
 */
static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
{
	int ret;
	struct raid_type *rt;
	unsigned long num_raid_params, num_raid_devs;
	struct raid_set *rs = NULL;

	/* Must have at least <raid_type> <#raid_params> */
	if (argc < 2) {
		ti->error = "Too few arguments";
		return -EINVAL;
	}

	/* raid type */
	rt = get_raid_type(argv[0]);
	if (!rt) {
		ti->error = "Unrecognised raid_type";
		return -EINVAL;
	}
	argc--;
	argv++;

	/* number of RAID parameters */
1231
	if (kstrtoul(argv[0], 10, &num_raid_params) < 0) {
N
NeilBrown 已提交
1232 1233 1234 1235 1236 1237 1238
		ti->error = "Cannot understand number of RAID parameters";
		return -EINVAL;
	}
	argc--;
	argv++;

	/* Skip over RAID params for now and find out # of devices */
1239
	if (num_raid_params >= argc) {
N
NeilBrown 已提交
1240 1241 1242 1243
		ti->error = "Arguments do not agree with counts given";
		return -EINVAL;
	}

1244
	if ((kstrtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) ||
H
Heinz Mauelshagen 已提交
1245
	    (num_raid_devs > MAX_RAID_DEVICES)) {
N
NeilBrown 已提交
1246 1247 1248 1249
		ti->error = "Cannot understand number of raid devices";
		return -EINVAL;
	}

1250 1251 1252 1253 1254 1255
	argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
	if (argc != (num_raid_devs * 2)) {
		ti->error = "Supplied RAID devices does not match the count given";
		return -EINVAL;
	}

N
NeilBrown 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
	rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
	if (IS_ERR(rs))
		return PTR_ERR(rs);

	ret = parse_raid_params(rs, argv, (unsigned)num_raid_params);
	if (ret)
		goto bad;

	argv += num_raid_params + 1;

	ret = dev_parms(rs, argv);
	if (ret)
		goto bad;

1270 1271 1272 1273 1274
	rs->md.sync_super = super_sync;
	ret = analyse_superblocks(ti, rs);
	if (ret)
		goto bad;

N
NeilBrown 已提交
1275 1276
	INIT_WORK(&rs->md.event_work, do_table_event);
	ti->private = rs;
1277
	ti->num_flush_bios = 1;
N
NeilBrown 已提交
1278

1279 1280 1281 1282 1283
	/*
	 * Disable/enable discard support on RAID set.
	 */
	configure_discard_support(ti, rs);

N
NeilBrown 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
	mutex_lock(&rs->md.reconfig_mutex);
	ret = md_run(&rs->md);
	rs->md.in_sync = 0; /* Assume already marked dirty */
	mutex_unlock(&rs->md.reconfig_mutex);

	if (ret) {
		ti->error = "Fail to run raid array";
		goto bad;
	}

1294 1295 1296 1297 1298
	if (ti->len != rs->md.array_sectors) {
		ti->error = "Array size does not match requested target length";
		ret = -EINVAL;
		goto size_mismatch;
	}
N
NeilBrown 已提交
1299 1300 1301
	rs->callbacks.congested_fn = raid_is_congested;
	dm_table_add_target_callbacks(ti->table, &rs->callbacks);

J
Jonathan Brassow 已提交
1302
	mddev_suspend(&rs->md);
N
NeilBrown 已提交
1303 1304
	return 0;

1305 1306
size_mismatch:
	md_stop(&rs->md);
N
NeilBrown 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
bad:
	context_free(rs);

	return ret;
}

static void raid_dtr(struct dm_target *ti)
{
	struct raid_set *rs = ti->private;

	list_del_init(&rs->callbacks.list);
	md_stop(&rs->md);
	context_free(rs);
}

M
Mikulas Patocka 已提交
1322
static int raid_map(struct dm_target *ti, struct bio *bio)
N
NeilBrown 已提交
1323 1324
{
	struct raid_set *rs = ti->private;
1325
	struct mddev *mddev = &rs->md;
N
NeilBrown 已提交
1326 1327 1328 1329 1330 1331

	mddev->pers->make_request(mddev, bio);

	return DM_MAPIO_SUBMITTED;
}

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
static const char *decipher_sync_action(struct mddev *mddev)
{
	if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
		return "frozen";

	if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
	    (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
		if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
			return "reshape";

		if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
			if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
				return "resync";
			else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
				return "check";
			return "repair";
		}

		if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
			return "recover";
	}

	return "idle";
}

1357 1358
static void raid_status(struct dm_target *ti, status_type_t type,
			unsigned status_flags, char *result, unsigned maxlen)
N
NeilBrown 已提交
1359 1360 1361 1362
{
	struct raid_set *rs = ti->private;
	unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
	unsigned sz = 0;
1363
	int i, array_in_sync = 0;
N
NeilBrown 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
	sector_t sync;

	switch (type) {
	case STATUSTYPE_INFO:
		DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);

		if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
			sync = rs->md.curr_resync_completed;
		else
			sync = rs->md.recovery_cp;

1375
		if (sync >= rs->md.resync_max_sectors) {
1376 1377 1378
			/*
			 * Sync complete.
			 */
1379
			array_in_sync = 1;
N
NeilBrown 已提交
1380
			sync = rs->md.resync_max_sectors;
1381 1382 1383 1384 1385 1386 1387
		} else if (test_bit(MD_RECOVERY_REQUESTED, &rs->md.recovery)) {
			/*
			 * If "check" or "repair" is occurring, the array has
			 * undergone and initial sync and the health characters
			 * should not be 'a' anymore.
			 */
			array_in_sync = 1;
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
		} else {
			/*
			 * The array may be doing an initial sync, or it may
			 * be rebuilding individual components.  If all the
			 * devices are In_sync, then it is the array that is
			 * being initialized.
			 */
			for (i = 0; i < rs->md.raid_disks; i++)
				if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
					array_in_sync = 1;
		}
1399

1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
		/*
		 * Status characters:
		 *  'D' = Dead/Failed device
		 *  'a' = Alive but not in-sync
		 *  'A' = Alive and in-sync
		 */
		for (i = 0; i < rs->md.raid_disks; i++) {
			if (test_bit(Faulty, &rs->dev[i].rdev.flags))
				DMEMIT("D");
			else if (!array_in_sync ||
				 !test_bit(In_sync, &rs->dev[i].rdev.flags))
				DMEMIT("a");
			else
				DMEMIT("A");
		}
N
NeilBrown 已提交
1415

1416 1417 1418 1419 1420 1421 1422 1423
		/*
		 * In-sync ratio:
		 *  The in-sync ratio shows the progress of:
		 *   - Initializing the array
		 *   - Rebuilding a subset of devices of the array
		 *  The user can distinguish between the two by referring
		 *  to the status characters.
		 */
N
NeilBrown 已提交
1424 1425 1426 1427
		DMEMIT(" %llu/%llu",
		       (unsigned long long) sync,
		       (unsigned long long) rs->md.resync_max_sectors);

1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
		/*
		 * Sync action:
		 *   See Documentation/device-mapper/dm-raid.c for
		 *   information on each of these states.
		 */
		DMEMIT(" %s", decipher_sync_action(&rs->md));

		/*
		 * resync_mismatches/mismatch_cnt
		 *   This field shows the number of discrepancies found when
		 *   performing a "check" of the array.
		 */
		DMEMIT(" %llu",
1441
		       (strcmp(rs->md.last_sync_action, "check")) ? 0 :
1442 1443
		       (unsigned long long)
		       atomic64_read(&rs->md.resync_mismatches));
N
NeilBrown 已提交
1444 1445 1446
		break;
	case STATUSTYPE_TABLE:
		/* The string you would use to construct this array */
1447
		for (i = 0; i < rs->md.raid_disks; i++) {
H
Heinz Mauelshagen 已提交
1448
			if ((rs->ctr_flags & CTR_FLAG_REBUILD) &&
1449
			    rs->dev[i].data_dev &&
N
NeilBrown 已提交
1450
			    !test_bit(In_sync, &rs->dev[i].rdev.flags))
1451
				raid_param_cnt += 2; /* for rebuilds */
1452 1453 1454 1455
			if (rs->dev[i].data_dev &&
			    test_bit(WriteMostly, &rs->dev[i].rdev.flags))
				raid_param_cnt += 2;
		}
N
NeilBrown 已提交
1456

H
Heinz Mauelshagen 已提交
1457 1458
		raid_param_cnt += (hweight32(rs->ctr_flags & ~CTR_FLAG_REBUILD) * 2);
		if (rs->ctr_flags & (CTR_FLAG_SYNC | CTR_FLAG_NOSYNC))
N
NeilBrown 已提交
1459 1460 1461 1462 1463
			raid_param_cnt--;

		DMEMIT("%s %u %u", rs->raid_type->name,
		       raid_param_cnt, rs->md.chunk_sectors);

H
Heinz Mauelshagen 已提交
1464
		if ((rs->ctr_flags & CTR_FLAG_SYNC) &&
N
NeilBrown 已提交
1465 1466
		    (rs->md.recovery_cp == MaxSector))
			DMEMIT(" sync");
H
Heinz Mauelshagen 已提交
1467
		if (rs->ctr_flags & CTR_FLAG_NOSYNC)
N
NeilBrown 已提交
1468 1469 1470
			DMEMIT(" nosync");

		for (i = 0; i < rs->md.raid_disks; i++)
H
Heinz Mauelshagen 已提交
1471
			if ((rs->ctr_flags & CTR_FLAG_REBUILD) &&
1472
			    rs->dev[i].data_dev &&
N
NeilBrown 已提交
1473 1474 1475
			    !test_bit(In_sync, &rs->dev[i].rdev.flags))
				DMEMIT(" rebuild %u", i);

H
Heinz Mauelshagen 已提交
1476
		if (rs->ctr_flags & CTR_FLAG_DAEMON_SLEEP)
N
NeilBrown 已提交
1477 1478 1479
			DMEMIT(" daemon_sleep %lu",
			       rs->md.bitmap_info.daemon_sleep);

H
Heinz Mauelshagen 已提交
1480
		if (rs->ctr_flags & CTR_FLAG_MIN_RECOVERY_RATE)
N
NeilBrown 已提交
1481 1482
			DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);

H
Heinz Mauelshagen 已提交
1483
		if (rs->ctr_flags & CTR_FLAG_MAX_RECOVERY_RATE)
N
NeilBrown 已提交
1484 1485
			DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);

1486 1487 1488 1489 1490
		for (i = 0; i < rs->md.raid_disks; i++)
			if (rs->dev[i].data_dev &&
			    test_bit(WriteMostly, &rs->dev[i].rdev.flags))
				DMEMIT(" write_mostly %u", i);

H
Heinz Mauelshagen 已提交
1491
		if (rs->ctr_flags & CTR_FLAG_MAX_WRITE_BEHIND)
N
NeilBrown 已提交
1492 1493 1494
			DMEMIT(" max_write_behind %lu",
			       rs->md.bitmap_info.max_write_behind);

H
Heinz Mauelshagen 已提交
1495
		if (rs->ctr_flags & CTR_FLAG_STRIPE_CACHE) {
1496
			struct r5conf *conf = rs->md.private;
N
NeilBrown 已提交
1497 1498 1499 1500 1501 1502

			/* convert from kiB to sectors */
			DMEMIT(" stripe_cache %d",
			       conf ? conf->max_nr_stripes * 2 : 0);
		}

H
Heinz Mauelshagen 已提交
1503
		if (rs->ctr_flags & CTR_FLAG_REGION_SIZE)
1504 1505 1506
			DMEMIT(" region_size %lu",
			       rs->md.bitmap_info.chunksize >> 9);

H
Heinz Mauelshagen 已提交
1507
		if (rs->ctr_flags & CTR_FLAG_RAID10_COPIES)
1508 1509 1510
			DMEMIT(" raid10_copies %u",
			       raid10_md_layout_to_copies(rs->md.layout));

H
Heinz Mauelshagen 已提交
1511
		if (rs->ctr_flags & CTR_FLAG_RAID10_FORMAT)
1512 1513
			DMEMIT(" raid10_format %s",
			       raid10_md_layout_to_format(rs->md.layout));
1514

N
NeilBrown 已提交
1515 1516
		DMEMIT(" %d", rs->md.raid_disks);
		for (i = 0; i < rs->md.raid_disks; i++) {
1517 1518 1519 1520
			if (rs->dev[i].meta_dev)
				DMEMIT(" %s", rs->dev[i].meta_dev->name);
			else
				DMEMIT(" -");
N
NeilBrown 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529

			if (rs->dev[i].data_dev)
				DMEMIT(" %s", rs->dev[i].data_dev->name);
			else
				DMEMIT(" -");
		}
	}
}

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
{
	struct raid_set *rs = ti->private;
	struct mddev *mddev = &rs->md;

	if (!strcasecmp(argv[0], "reshape")) {
		DMERR("Reshape not supported.");
		return -EINVAL;
	}

	if (!mddev->pers || !mddev->pers->sync_request)
		return -EINVAL;

	if (!strcasecmp(argv[0], "frozen"))
		set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
	else
		clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);

	if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
		if (mddev->sync_thread) {
			set_bit(MD_RECOVERY_INTR, &mddev->recovery);
			md_reap_sync_thread(mddev);
		}
	} else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
		   test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
		return -EBUSY;
	else if (!strcasecmp(argv[0], "resync"))
		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
	else if (!strcasecmp(argv[0], "recover")) {
		set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
		set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
	} else {
		if (!strcasecmp(argv[0], "check"))
			set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
		else if (!!strcasecmp(argv[0], "repair"))
			return -EINVAL;
		set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
		set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
	}
	if (mddev->ro == 2) {
		/* A write to sync_action is enough to justify
		 * canceling read-auto mode
		 */
		mddev->ro = 0;
		if (!mddev->suspended)
			md_wakeup_thread(mddev->sync_thread);
	}
	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
	if (!mddev->suspended)
		md_wakeup_thread(mddev->thread);

	return 0;
}

static int raid_iterate_devices(struct dm_target *ti,
				iterate_devices_callout_fn fn, void *data)
N
NeilBrown 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
{
	struct raid_set *rs = ti->private;
	unsigned i;
	int ret = 0;

	for (i = 0; !ret && i < rs->md.raid_disks; i++)
		if (rs->dev[i].data_dev)
			ret = fn(ti,
				 rs->dev[i].data_dev,
				 0, /* No offset on data devs */
				 rs->md.dev_sectors,
				 data);

	return ret;
}

static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
{
	struct raid_set *rs = ti->private;
	unsigned chunk_size = rs->md.chunk_sectors << 9;
1606
	struct r5conf *conf = rs->md.private;
N
NeilBrown 已提交
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625

	blk_limits_io_min(limits, chunk_size);
	blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
}

static void raid_presuspend(struct dm_target *ti)
{
	struct raid_set *rs = ti->private;

	md_stop_writes(&rs->md);
}

static void raid_postsuspend(struct dm_target *ti)
{
	struct raid_set *rs = ti->private;

	mddev_suspend(&rs->md);
}

1626
static void attempt_restore_of_faulty_devices(struct raid_set *rs)
N
NeilBrown 已提交
1627
{
1628 1629 1630 1631 1632
	int i;
	uint64_t failed_devices, cleared_failed_devices = 0;
	unsigned long flags;
	struct dm_raid_superblock *sb;
	struct md_rdev *r;
N
NeilBrown 已提交
1633

1634 1635 1636 1637 1638 1639 1640
	for (i = 0; i < rs->md.raid_disks; i++) {
		r = &rs->dev[i].rdev;
		if (test_bit(Faulty, &r->flags) && r->sb_page &&
		    sync_page_io(r, 0, r->sb_size, r->sb_page, READ, 1)) {
			DMINFO("Faulty %s device #%d has readable super block."
			       "  Attempting to revive it.",
			       rs->raid_type->name, i);
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655

			/*
			 * Faulty bit may be set, but sometimes the array can
			 * be suspended before the personalities can respond
			 * by removing the device from the array (i.e. calling
			 * 'hot_remove_disk').  If they haven't yet removed
			 * the failed device, its 'raid_disk' number will be
			 * '>= 0' - meaning we must call this function
			 * ourselves.
			 */
			if ((r->raid_disk >= 0) &&
			    (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0))
				/* Failed to revive this device, try next */
				continue;

1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
			r->raid_disk = i;
			r->saved_raid_disk = i;
			flags = r->flags;
			clear_bit(Faulty, &r->flags);
			clear_bit(WriteErrorSeen, &r->flags);
			clear_bit(In_sync, &r->flags);
			if (r->mddev->pers->hot_add_disk(r->mddev, r)) {
				r->raid_disk = -1;
				r->saved_raid_disk = -1;
				r->flags = flags;
			} else {
				r->recovery_offset = 0;
				cleared_failed_devices |= 1 << i;
			}
		}
	}
	if (cleared_failed_devices) {
		rdev_for_each(r, &rs->md) {
			sb = page_address(r->sb_page);
			failed_devices = le64_to_cpu(sb->failed_devices);
			failed_devices &= ~cleared_failed_devices;
			sb->failed_devices = cpu_to_le64(failed_devices);
		}
	}
}

static void raid_resume(struct dm_target *ti)
{
	struct raid_set *rs = ti->private;

1686
	set_bit(MD_CHANGE_DEVS, &rs->md.flags);
1687 1688 1689
	if (!rs->bitmap_loaded) {
		bitmap_load(&rs->md);
		rs->bitmap_loaded = 1;
1690 1691 1692 1693 1694 1695
	} else {
		/*
		 * A secondary resume while the device is active.
		 * Take this opportunity to check whether any failed
		 * devices are reachable again.
		 */
1696
		attempt_restore_of_faulty_devices(rs);
1697
	}
1698

1699
	clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
N
NeilBrown 已提交
1700 1701 1702 1703 1704
	mddev_resume(&rs->md);
}

static struct target_type raid_target = {
	.name = "raid",
1705
	.version = {1, 6, 0},
N
NeilBrown 已提交
1706 1707 1708 1709 1710
	.module = THIS_MODULE,
	.ctr = raid_ctr,
	.dtr = raid_dtr,
	.map = raid_map,
	.status = raid_status,
1711
	.message = raid_message,
N
NeilBrown 已提交
1712 1713 1714 1715 1716 1717 1718 1719 1720
	.iterate_devices = raid_iterate_devices,
	.io_hints = raid_io_hints,
	.presuspend = raid_presuspend,
	.postsuspend = raid_postsuspend,
	.resume = raid_resume,
};

static int __init dm_raid_init(void)
{
1721 1722 1723 1724
	DMINFO("Loading target version %u.%u.%u",
	       raid_target.version[0],
	       raid_target.version[1],
	       raid_target.version[2]);
N
NeilBrown 已提交
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
	return dm_register_target(&raid_target);
}

static void __exit dm_raid_exit(void)
{
	dm_unregister_target(&raid_target);
}

module_init(dm_raid_init);
module_exit(dm_raid_exit);

1736 1737 1738 1739
module_param(devices_handle_discard_safely, bool, 0644);
MODULE_PARM_DESC(devices_handle_discard_safely,
		 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");

N
NeilBrown 已提交
1740
MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
1741 1742
MODULE_ALIAS("dm-raid1");
MODULE_ALIAS("dm-raid10");
N
NeilBrown 已提交
1743 1744 1745 1746 1747
MODULE_ALIAS("dm-raid4");
MODULE_ALIAS("dm-raid5");
MODULE_ALIAS("dm-raid6");
MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
MODULE_LICENSE("GPL");