dm-raid.c 117.2 KB
Newer Older
N
NeilBrown 已提交
1 2
/*
 * Copyright (C) 2010-2011 Neil Brown
3
 * Copyright (C) 2010-2017 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"
15
#include "md-bitmap.h"
N
NeilBrown 已提交
16

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

N
NeilBrown 已提交
19
#define DM_MSG_PREFIX "raid"
20
#define	MAX_RAID_DEVICES	253 /* md-raid kernel limit */
N
NeilBrown 已提交
21

22 23 24 25 26
/*
 * Minimum sectors of free reshape space per raid device
 */
#define	MIN_FREE_RESHAPE_SPACE to_sector(4*4096)

27 28 29 30 31
/*
 * Minimum journal space 4 MiB in sectors.
 */
#define	MIN_RAID456_JOURNAL_SPACE (4*2048)

32
/* Global list of all raid sets */
33
static LIST_HEAD(raid_sets);
34

35 36
static bool devices_handle_discard_safely = false;

N
NeilBrown 已提交
37
/*
38 39
 * 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 已提交
40
 */
41
#define FirstUse 10		/* rdev flag */
N
NeilBrown 已提交
42 43 44 45

struct raid_dev {
	/*
	 * Two DM devices, one to hold metadata and one to hold the
46
	 * actual data/parity.	The reason for this is to not confuse
N
NeilBrown 已提交
47 48 49 50 51 52 53 54 55 56 57
	 * 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;
58
	struct md_rdev rdev;
N
NeilBrown 已提交
59 60 61
};

/*
62
 * Bits for establishing rs->ctr_flags
63 64 65
 *
 * 1 = no flag value
 * 2 = flag with value
N
NeilBrown 已提交
66
 */
67 68 69 70 71 72 73 74 75 76 77 78
#define __CTR_FLAG_SYNC			0  /* 1 */ /* Not with raid0! */
#define __CTR_FLAG_NOSYNC		1  /* 1 */ /* Not with raid0! */
#define __CTR_FLAG_REBUILD		2  /* 2 */ /* Not with raid0! */
#define __CTR_FLAG_DAEMON_SLEEP		3  /* 2 */ /* Not with raid0! */
#define __CTR_FLAG_MIN_RECOVERY_RATE	4  /* 2 */ /* Not with raid0! */
#define __CTR_FLAG_MAX_RECOVERY_RATE	5  /* 2 */ /* Not with raid0! */
#define __CTR_FLAG_MAX_WRITE_BEHIND	6  /* 2 */ /* Only with raid1! */
#define __CTR_FLAG_WRITE_MOSTLY		7  /* 2 */ /* Only with raid1! */
#define __CTR_FLAG_STRIPE_CACHE		8  /* 2 */ /* Only with raid4/5/6! */
#define __CTR_FLAG_REGION_SIZE		9  /* 2 */ /* Not with raid0! */
#define __CTR_FLAG_RAID10_COPIES	10 /* 2 */ /* Only with raid10 */
#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 82 83
#define __CTR_FLAG_DATA_OFFSET		13 /* 2 */ /* Only with reshapable raid4/5/6/10! */
#define __CTR_FLAG_RAID10_USE_NEAR_SETS 14 /* 2 */ /* Only with raid10! */

84
/* New for v1.10.0 */
85 86 87 88
#define __CTR_FLAG_JOURNAL_DEV		15 /* 2 */ /* Only with raid4/5/6 (journal device)! */

/* New for v1.11.1 */
#define __CTR_FLAG_JOURNAL_MODE		16 /* 2 */ /* Only with raid4/5/6 (journal mode)! */
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/*
 * Flags for rs->ctr_flags field.
 */
#define CTR_FLAG_SYNC			(1 << __CTR_FLAG_SYNC)
#define CTR_FLAG_NOSYNC			(1 << __CTR_FLAG_NOSYNC)
#define CTR_FLAG_REBUILD		(1 << __CTR_FLAG_REBUILD)
#define CTR_FLAG_DAEMON_SLEEP		(1 << __CTR_FLAG_DAEMON_SLEEP)
#define CTR_FLAG_MIN_RECOVERY_RATE	(1 << __CTR_FLAG_MIN_RECOVERY_RATE)
#define CTR_FLAG_MAX_RECOVERY_RATE	(1 << __CTR_FLAG_MAX_RECOVERY_RATE)
#define CTR_FLAG_MAX_WRITE_BEHIND	(1 << __CTR_FLAG_MAX_WRITE_BEHIND)
#define CTR_FLAG_WRITE_MOSTLY		(1 << __CTR_FLAG_WRITE_MOSTLY)
#define CTR_FLAG_STRIPE_CACHE		(1 << __CTR_FLAG_STRIPE_CACHE)
#define CTR_FLAG_REGION_SIZE		(1 << __CTR_FLAG_REGION_SIZE)
#define CTR_FLAG_RAID10_COPIES		(1 << __CTR_FLAG_RAID10_COPIES)
#define CTR_FLAG_RAID10_FORMAT		(1 << __CTR_FLAG_RAID10_FORMAT)
#define CTR_FLAG_DELTA_DISKS		(1 << __CTR_FLAG_DELTA_DISKS)
#define CTR_FLAG_DATA_OFFSET		(1 << __CTR_FLAG_DATA_OFFSET)
#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 113 114 115 116 117 118 119
/*
 * Definitions of various constructor flags to
 * be used in checks of valid / invalid flags
 * per raid level.
 */
/* Define all any sync flags */
#define	CTR_FLAGS_ANY_SYNC		(CTR_FLAG_SYNC | CTR_FLAG_NOSYNC)

/* Define flags for options without argument (e.g. 'nosync') */
120 121
#define	CTR_FLAG_OPTIONS_NO_ARGS	(CTR_FLAGS_ANY_SYNC | \
					 CTR_FLAG_RAID10_USE_NEAR_SETS)
122 123 124 125 126 127 128 129 130 131 132

/* Define flags for options with one argument (e.g. 'delta_disks +2') */
#define CTR_FLAG_OPTIONS_ONE_ARG (CTR_FLAG_REBUILD | \
				  CTR_FLAG_WRITE_MOSTLY | \
				  CTR_FLAG_DAEMON_SLEEP | \
				  CTR_FLAG_MIN_RECOVERY_RATE | \
				  CTR_FLAG_MAX_RECOVERY_RATE | \
				  CTR_FLAG_MAX_WRITE_BEHIND | \
				  CTR_FLAG_STRIPE_CACHE | \
				  CTR_FLAG_REGION_SIZE | \
				  CTR_FLAG_RAID10_COPIES | \
133 134 135
				  CTR_FLAG_RAID10_FORMAT | \
				  CTR_FLAG_DELTA_DISKS | \
				  CTR_FLAG_DATA_OFFSET)
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150
/* Valid options definitions per raid level... */

/* "raid0" does only accept data offset */
#define RAID0_VALID_FLAGS	(CTR_FLAG_DATA_OFFSET)

/* "raid1" does not accept stripe cache, data offset, delta_disks or any raid10 options */
#define RAID1_VALID_FLAGS	(CTR_FLAGS_ANY_SYNC | \
				 CTR_FLAG_REBUILD | \
				 CTR_FLAG_WRITE_MOSTLY | \
				 CTR_FLAG_DAEMON_SLEEP | \
				 CTR_FLAG_MIN_RECOVERY_RATE | \
				 CTR_FLAG_MAX_RECOVERY_RATE | \
				 CTR_FLAG_MAX_WRITE_BEHIND | \
				 CTR_FLAG_REGION_SIZE | \
151
				 CTR_FLAG_DELTA_DISKS | \
152
				 CTR_FLAG_DATA_OFFSET)
153

154 155 156 157 158 159 160
/* "raid10" does not accept any raid1 or stripe cache options */
#define RAID10_VALID_FLAGS	(CTR_FLAGS_ANY_SYNC | \
				 CTR_FLAG_REBUILD | \
				 CTR_FLAG_DAEMON_SLEEP | \
				 CTR_FLAG_MIN_RECOVERY_RATE | \
				 CTR_FLAG_MAX_RECOVERY_RATE | \
				 CTR_FLAG_REGION_SIZE | \
161
				 CTR_FLAG_RAID10_COPIES | \
162 163
				 CTR_FLAG_RAID10_FORMAT | \
				 CTR_FLAG_DELTA_DISKS | \
164 165
				 CTR_FLAG_DATA_OFFSET | \
				 CTR_FLAG_RAID10_USE_NEAR_SETS)
166 167 168 169 170 171 172 173

/*
 * "raid4/5/6" do not accept any raid1 or raid10 specific options
 *
 * "raid6" does not accept "nosync", because it is not guaranteed
 * that both parity and q-syndrome are being written properly with
 * any writes
 */
174 175 176 177 178 179 180 181
#define RAID45_VALID_FLAGS	(CTR_FLAGS_ANY_SYNC | \
				 CTR_FLAG_REBUILD | \
				 CTR_FLAG_DAEMON_SLEEP | \
				 CTR_FLAG_MIN_RECOVERY_RATE | \
				 CTR_FLAG_MAX_RECOVERY_RATE | \
				 CTR_FLAG_STRIPE_CACHE | \
				 CTR_FLAG_REGION_SIZE | \
				 CTR_FLAG_DELTA_DISKS | \
182
				 CTR_FLAG_DATA_OFFSET | \
183 184
				 CTR_FLAG_JOURNAL_DEV | \
				 CTR_FLAG_JOURNAL_MODE)
185 186 187 188 189 190 191 192 193

#define RAID6_VALID_FLAGS	(CTR_FLAG_SYNC | \
				 CTR_FLAG_REBUILD | \
				 CTR_FLAG_DAEMON_SLEEP | \
				 CTR_FLAG_MIN_RECOVERY_RATE | \
				 CTR_FLAG_MAX_RECOVERY_RATE | \
				 CTR_FLAG_STRIPE_CACHE | \
				 CTR_FLAG_REGION_SIZE | \
				 CTR_FLAG_DELTA_DISKS | \
194
				 CTR_FLAG_DATA_OFFSET | \
195 196
				 CTR_FLAG_JOURNAL_DEV | \
				 CTR_FLAG_JOURNAL_MODE)
197
/* ...valid options definitions per raid level */
198

199 200 201 202 203 204 205 206
/*
 * Flags for rs->runtime_flags field
 * (RT_FLAG prefix meaning "runtime flag")
 *
 * These are all internal and used to define runtime state,
 * e.g. to prevent another resume from preresume processing
 * the raid set all over again.
 */
207 208 209 210
#define RT_FLAG_RS_PRERESUMED		0
#define RT_FLAG_RS_RESUMED		1
#define RT_FLAG_RS_BITMAP_LOADED	2
#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 218
#define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)

219 220 221 222 223 224 225 226 227
/*
 * raid set level, layout and chunk sectors backup/restore
 */
struct rs_layout {
	int new_level;
	int new_layout;
	int new_chunk_sectors;
};

N
NeilBrown 已提交
228 229
struct raid_set {
	struct dm_target *ti;
230
	struct list_head list;
N
NeilBrown 已提交
231

232
	uint32_t stripe_cache_entries;
233 234
	unsigned long ctr_flags;
	unsigned long runtime_flags;
235 236

	uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
N
NeilBrown 已提交
237

238 239
	int raid_disks;
	int delta_disks;
240
	int data_offset;
241
	int raid10_copies;
242
	int requested_bitmap_chunk_sectors;
243

244
	struct mddev md;
N
NeilBrown 已提交
245 246 247
	struct raid_type *raid_type;
	struct dm_target_callbacks callbacks;

248 249 250 251
	/* Optional raid4/5/6 journal device */
	struct journal_dev {
		struct dm_dev *dev;
		struct md_rdev rdev;
252
		int mode;
253 254
	} journal_dev;

N
NeilBrown 已提交
255 256 257
	struct raid_dev dev[0];
};

258
static void rs_config_backup(struct raid_set *rs, struct rs_layout *l)
259 260 261 262 263 264 265 266
{
	struct mddev *mddev = &rs->md;

	l->new_level = mddev->new_level;
	l->new_layout = mddev->new_layout;
	l->new_chunk_sectors = mddev->new_chunk_sectors;
}

267
static void rs_config_restore(struct raid_set *rs, struct rs_layout *l)
268 269 270 271 272 273 274 275
{
	struct mddev *mddev = &rs->md;

	mddev->new_level = l->new_level;
	mddev->new_layout = l->new_layout;
	mddev->new_chunk_sectors = l->new_chunk_sectors;
}

276 277 278 279 280 281 282 283 284 285 286 287 288
/* Find any raid_set in active slot for @rs on global list */
static struct raid_set *rs_find_active(struct raid_set *rs)
{
	struct raid_set *r;
	struct mapped_device *md = dm_table_get_md(rs->ti->table);

	list_for_each_entry(r, &raid_sets, list)
		if (r != rs && dm_table_get_md(r->ti->table) == md)
			return r;

	return NULL;
}

289 290 291 292 293 294
/* raid10 algorithms (i.e. formats) */
#define	ALGORITHM_RAID10_DEFAULT	0
#define	ALGORITHM_RAID10_NEAR		1
#define	ALGORITHM_RAID10_OFFSET		2
#define	ALGORITHM_RAID10_FAR		3

N
NeilBrown 已提交
295 296 297 298
/* Supported raid types and properties. */
static struct raid_type {
	const char *name;		/* RAID algorithm. */
	const char *descr;		/* Descriptor text for logging. */
299 300 301 302
	const unsigned int parity_devs;	/* # of parity devices. */
	const unsigned int minimal_devs;/* minimal # of devices in set. */
	const unsigned int level;	/* RAID level. */
	const unsigned int algorithm;	/* RAID algorithm. */
N
NeilBrown 已提交
303
} raid_types[] = {
304 305 306
	{"raid0",	  "raid0 (striping)",			    0, 2, 0,  0 /* NONE */},
	{"raid1",	  "raid1 (mirroring)",			    0, 2, 1,  0 /* NONE */},
	{"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 309
	{"raid10_near",	  "raid10 near (striped mirrors)",	    0, 2, 10, ALGORITHM_RAID10_NEAR},
	{"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 312 313 314 315 316 317 318 319 320 321 322 323
	{"raid5_n",	  "raid5 (dedicated last parity disk)",	    1, 2, 5,  ALGORITHM_PARITY_N},
	{"raid5_ls",	  "raid5 (left symmetric)",		    1, 2, 5,  ALGORITHM_LEFT_SYMMETRIC},
	{"raid5_rs",	  "raid5 (right symmetric)",		    1, 2, 5,  ALGORITHM_RIGHT_SYMMETRIC},
	{"raid5_la",	  "raid5 (left asymmetric)",		    1, 2, 5,  ALGORITHM_LEFT_ASYMMETRIC},
	{"raid5_ra",	  "raid5 (right asymmetric)",		    1, 2, 5,  ALGORITHM_RIGHT_ASYMMETRIC},
	{"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},
	{"raid6_n_6",	  "raid6 (dedicated parity/Q n/6)",	    2, 4, 6,  ALGORITHM_PARITY_N_6},
	{"raid6_ls_6",	  "raid6 (left symmetric dedicated Q 6)",   2, 4, 6,  ALGORITHM_LEFT_SYMMETRIC_6},
	{"raid6_rs_6",	  "raid6 (right symmetric dedicated Q 6)",  2, 4, 6,  ALGORITHM_RIGHT_SYMMETRIC_6},
	{"raid6_la_6",	  "raid6 (left asymmetric dedicated Q 6)",  2, 4, 6,  ALGORITHM_LEFT_ASYMMETRIC_6},
	{"raid6_ra_6",	  "raid6 (right asymmetric dedicated Q 6)", 2, 4, 6,  ALGORITHM_RIGHT_ASYMMETRIC_6}
N
NeilBrown 已提交
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 330 331
{
	return v >= min && v <= max;
}

332 333
/* All table line arguments are defined here */
static struct arg_name_flag {
334
	const unsigned long flag;
335
	const char *name;
M
Mike Snitzer 已提交
336
} __arg_name_flags[] = {
337 338 339 340 341 342 343
	{ CTR_FLAG_SYNC, "sync"},
	{ CTR_FLAG_NOSYNC, "nosync"},
	{ CTR_FLAG_REBUILD, "rebuild"},
	{ CTR_FLAG_DAEMON_SLEEP, "daemon_sleep"},
	{ CTR_FLAG_MIN_RECOVERY_RATE, "min_recovery_rate"},
	{ CTR_FLAG_MAX_RECOVERY_RATE, "max_recovery_rate"},
	{ CTR_FLAG_MAX_WRITE_BEHIND, "max_write_behind"},
344
	{ CTR_FLAG_WRITE_MOSTLY, "write_mostly"},
345 346 347 348
	{ CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
	{ CTR_FLAG_REGION_SIZE, "region_size"},
	{ CTR_FLAG_RAID10_COPIES, "raid10_copies"},
	{ CTR_FLAG_RAID10_FORMAT, "raid10_format"},
349 350 351
	{ CTR_FLAG_DATA_OFFSET, "data_offset"},
	{ CTR_FLAG_DELTA_DISKS, "delta_disks"},
	{ 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) {
M
Mike Snitzer 已提交
360
		struct arg_name_flag *anf = __arg_name_flags + ARRAY_SIZE(__arg_name_flags);
361

M
Mike Snitzer 已提交
362
		while (anf-- > __arg_name_flags)
363
			if (flag & anf->flag)
364 365 366 367 368 369 370 371
				return anf->name;

	} else
		DMERR("%s called with more than one flag!", __func__);

	return NULL;
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
/* Define correlation of raid456 journal cache modes and dm-raid target line parameters */
static struct {
	const int mode;
	const char *param;
} _raid456_journal_mode[] = {
	{ R5C_JOURNAL_MODE_WRITE_THROUGH , "writethrough" },
	{ R5C_JOURNAL_MODE_WRITE_BACK    , "writeback" }
};

/* Return MD raid4/5/6 journal mode for dm @journal_mode one */
static int dm_raid_journal_mode_to_md(const char *mode)
{
	int m = ARRAY_SIZE(_raid456_journal_mode);

	while (m--)
		if (!strcasecmp(mode, _raid456_journal_mode[m].param))
			return _raid456_journal_mode[m].mode;

	return -EINVAL;
}

/* Return dm-raid raid4/5/6 journal mode string for @mode */
static const char *md_journal_mode_to_dm_raid(const int mode)
{
	int m = ARRAY_SIZE(_raid456_journal_mode);

	while (m--)
		if (mode == _raid456_journal_mode[m].mode)
			return _raid456_journal_mode[m].param;

	return "unknown";
}

405
/*
406 407
 * Bool helpers to test for various raid levels of a raid set.
 * It's level as reported by the superblock rather than
408 409 410 411 412 413 414 415
 * the requested raid_type passed to the constructor.
 */
/* Return true, if raid set in @rs is raid0 */
static bool rs_is_raid0(struct raid_set *rs)
{
	return !rs->md.level;
}

416 417 418 419 420 421
/* Return true, if raid set in @rs is raid1 */
static bool rs_is_raid1(struct raid_set *rs)
{
	return rs->md.level == 1;
}

422 423 424 425 426 427
/* Return true, if raid set in @rs is raid10 */
static bool rs_is_raid10(struct raid_set *rs)
{
	return rs->md.level == 10;
}

428 429 430 431 432 433
/* Return true, if raid set in @rs is level 6 */
static bool rs_is_raid6(struct raid_set *rs)
{
	return rs->md.level == 6;
}

434 435 436 437 438 439 440
/* Return true, if raid set in @rs is level 4, 5 or 6 */
static bool rs_is_raid456(struct raid_set *rs)
{
	return __within_range(rs->md.level, 4, 6);
}

/* Return true, if raid set in @rs is reshapable */
441
static bool __is_raid10_far(int layout);
442 443 444 445 446 447
static bool rs_is_reshapable(struct raid_set *rs)
{
	return rs_is_raid456(rs) ||
	       (rs_is_raid10(rs) && !__is_raid10_far(rs->md.new_layout));
}

448 449 450
/* Return true, if raid set in @rs is recovering */
static bool rs_is_recovering(struct raid_set *rs)
{
451
	return rs->md.recovery_cp < rs->md.dev_sectors;
452 453 454 455 456 457 458 459
}

/* Return true, if raid set in @rs is reshaping */
static bool rs_is_reshaping(struct raid_set *rs)
{
	return rs->md.reshape_position != MaxSector;
}

460
/*
461
 * bool helpers to test for various raid levels of a raid type @rt
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
 */

/* Return true, if raid type in @rt is raid0 */
static bool rt_is_raid0(struct raid_type *rt)
{
	return !rt->level;
}

/* Return true, if raid type in @rt is raid1 */
static bool rt_is_raid1(struct raid_type *rt)
{
	return rt->level == 1;
}

/* Return true, if raid type in @rt is raid10 */
static bool rt_is_raid10(struct raid_type *rt)
{
	return rt->level == 10;
}

/* Return true, if raid type in @rt is raid4/5 */
static bool rt_is_raid45(struct raid_type *rt)
{
485
	return __within_range(rt->level, 4, 5);
486 487 488 489 490 491 492
}

/* Return true, if raid type in @rt is raid6 */
static bool rt_is_raid6(struct raid_type *rt)
{
	return rt->level == 6;
}
493 494 495 496

/* Return true, if raid type in @rt is raid4/5/6 */
static bool rt_is_raid456(struct raid_type *rt)
{
497
	return __within_range(rt->level, 4, 6);
498
}
499 500
/* END: raid level bools */

501 502
/* Return valid ctr flags for the raid level of @rs */
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 522
 *
 * Has to be called after parsing of the ctr flags!
 */
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 528
		return -EINVAL;
	}
529 530 531 532

	return 0;
}

533 534 535 536 537 538 539
/* MD raid10 bit definitions and helpers */
#define RAID10_OFFSET			(1 << 16) /* stripes with data copies area adjacent on devices */
#define RAID10_BROCKEN_USE_FAR_SETS	(1 << 17) /* Broken in raid10.c: use sets instead of whole stripe rotation */
#define RAID10_USE_FAR_SETS		(1 << 18) /* Use sets instead of whole stripe rotation */
#define RAID10_FAR_COPIES_SHIFT		8	  /* raid10 # far copies shift (2nd byte of layout) */

/* Return md raid10 near copies for @layout */
M
Mike Snitzer 已提交
540
static unsigned int __raid10_near_copies(int layout)
541 542 543 544 545
{
	return layout & 0xFF;
}

/* Return md raid10 far copies for @layout */
M
Mike Snitzer 已提交
546
static unsigned int __raid10_far_copies(int layout)
547
{
M
Mike Snitzer 已提交
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
{
M
Mike Snitzer 已提交
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
{
M
Mike Snitzer 已提交
566
	return !__is_raid10_offset(layout) && __raid10_far_copies(layout) > 1;
567 568 569 570
}

/* Return md raid10 layout string for @layout */
static const char *raid10_md_layout_to_format(int layout)
571 572
{
	/*
573 574 575
	 * Bit 16 stands for "offset"
	 * (i.e. adjacent stripes hold copies)
	 *
576 577
	 * Refer to MD's raid10.c for details
	 */
M
Mike Snitzer 已提交
578
	if (__is_raid10_offset(layout))
579 580
		return "offset";

M
Mike Snitzer 已提交
581
	if (__raid10_near_copies(layout) > 1)
582 583
		return "near";

584 585
	if (__raid10_far_copies(layout) > 1)
		return "far";
586

587
	return "unknown";
588 589
}

590
/* Return md raid10 algorithm for @name */
H
Heinz Mauelshagen 已提交
591
static const int raid10_name_to_format(const char *name)
592 593 594 595 596 597 598 599 600 601 602 603 604
{
	if (!strcasecmp(name, "near"))
		return ALGORITHM_RAID10_NEAR;
	else if (!strcasecmp(name, "offset"))
		return ALGORITHM_RAID10_OFFSET;
	else if (!strcasecmp(name, "far"))
		return ALGORITHM_RAID10_FAR;

	return -EINVAL;
}

/* Return md raid10 copies for @layout */
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 610 611 612
/* Return md raid10 format id for @format string */
static int raid10_format_to_md_layout(struct raid_set *rs,
				      unsigned int algorithm,
				      unsigned int copies)
613
{
614
	unsigned int n = 1, f = 1, r = 0;
615

616 617 618 619 620 621 622 623 624 625
	/*
	 * MD resilienece flaw:
	 *
	 * enabling use_far_sets for far/offset formats causes copies
	 * to be colocated on the same devs together with their origins!
	 *
	 * -> disable it for now in the definition above
	 */
	if (algorithm == ALGORITHM_RAID10_DEFAULT ||
	    algorithm == ALGORITHM_RAID10_NEAR)
626
		n = copies;
627 628 629 630

	else if (algorithm == ALGORITHM_RAID10_OFFSET) {
		f = copies;
		r = RAID10_OFFSET;
631
		if (!test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags))
632 633 634
			r |= RAID10_USE_FAR_SETS;

	} 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 641 642 643 644 645
	} else
		return -EINVAL;

	return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
}
/* 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 651 652 653
{
	if (rtp->level == 10) {
		switch (rtp->algorithm) {
		case ALGORITHM_RAID10_DEFAULT:
		case ALGORITHM_RAID10_NEAR:
M
Mike Snitzer 已提交
654
			return __is_raid10_near(layout);
655
		case ALGORITHM_RAID10_OFFSET:
M
Mike Snitzer 已提交
656
			return __is_raid10_offset(layout);
657
		case ALGORITHM_RAID10_FAR:
M
Mike Snitzer 已提交
658
			return __is_raid10_far(layout);
659 660 661 662
		default:
			break;
		}
	}
663

664
	return false;
665 666
}

667
/* Return raid_type for @name */
668
static struct raid_type *get_raid_type(const char *name)
N
NeilBrown 已提交
669
{
670
	struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
N
NeilBrown 已提交
671

672 673 674
	while (rtp-- > raid_types)
		if (!strcasecmp(rtp->name, name))
			return rtp;
N
NeilBrown 已提交
675 676 677 678

	return NULL;
}

679 680 681 682 683 684 685 686
/* Return raid_type for @name based derived from @level and @layout */
static struct raid_type *get_raid_type_by_ll(const int level, const int layout)
{
	struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);

	while (rtp-- > raid_types) {
		/* RAID10 special checks based on @layout flags/properties */
		if (rtp->level == level &&
M
Mike Snitzer 已提交
687
		    (__got_raid10(rtp, layout) || rtp->algorithm == layout))
688 689 690 691 692 693
			return rtp;
	}

	return NULL;
}

694 695
/* Adjust rdev sectors */
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 702 703 704
	/*
	 * raid10 sets rdev->sector to the device size, which
	 * is unintended in case of out-of-place reshaping
	 */
	rdev_for_each(rdev, mddev)
705 706
		if (!test_bit(Journal, &rdev->flags))
			rdev->sectors = mddev->dev_sectors;
707
}
708

709 710 711 712 713 714 715 716
/*
 * Change bdev capacity of @rs in case of a disk add/remove reshape
 */
static void rs_set_capacity(struct raid_set *rs)
{
	struct gendisk *gendisk = dm_disk(dm_table_get_md(rs->ti->table));

	set_capacity(gendisk, rs->md.array_sectors);
717
	revalidate_disk(gendisk);
718 719
}

720 721 722 723 724 725 726 727 728 729 730 731 732
/*
 * Set the mddev properties in @rs to the current
 * ones retrieved from the freshest superblock
 */
static void rs_set_cur(struct raid_set *rs)
{
	struct mddev *mddev = &rs->md;

	mddev->new_level = mddev->level;
	mddev->new_layout = mddev->layout;
	mddev->new_chunk_sectors = mddev->chunk_sectors;
}

733 734 735 736 737 738 739 740 741 742 743
/*
 * Set the mddev properties in @rs to the new
 * ones requested by the ctr
 */
static void rs_set_new(struct raid_set *rs)
{
	struct mddev *mddev = &rs->md;

	mddev->level = mddev->new_level;
	mddev->layout = mddev->new_layout;
	mddev->chunk_sectors = mddev->new_chunk_sectors;
744
	mddev->raid_disks = rs->raid_disks;
745 746 747
	mddev->delta_disks = 0;
}

748
static struct raid_set *raid_set_alloc(struct dm_target *ti, struct raid_type *raid_type,
749
				       unsigned int raid_devs)
N
NeilBrown 已提交
750
{
751
	unsigned int i;
N
NeilBrown 已提交
752 753
	struct raid_set *rs;

754 755 756 757
	if (raid_devs <= raid_type->parity_devs) {
		ti->error = "Insufficient number of devices";
		return ERR_PTR(-EINVAL);
	}
N
NeilBrown 已提交
758 759

	rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
760 761 762 763
	if (!rs) {
		ti->error = "Cannot allocate raid context";
		return ERR_PTR(-ENOMEM);
	}
N
NeilBrown 已提交
764 765 766

	mddev_init(&rs->md);

767
	INIT_LIST_HEAD(&rs->list);
768 769 770
	rs->raid_disks = raid_devs;
	rs->delta_disks = 0;

N
NeilBrown 已提交
771 772
	rs->ti = ti;
	rs->raid_type = raid_type;
773
	rs->stripe_cache_entries = 256;
N
NeilBrown 已提交
774 775 776 777 778 779
	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;
780
	rs->md.recovery_cp = MaxSector;
N
NeilBrown 已提交
781 782 783 784

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

785 786 787
	/* Add @rs to global list. */
	list_add(&rs->list, &raid_sets);

N
NeilBrown 已提交
788 789 790 791 792 793
	/*
	 * Remaining items to be initialized by further RAID params:
	 *  rs->md.persistent
	 *  rs->md.external
	 *  rs->md.chunk_sectors
	 *  rs->md.new_chunk_sectors
794
	 *  rs->md.dev_sectors
N
NeilBrown 已提交
795 796 797 798 799
	 */

	return rs;
}

800
/* Free all @rs allocations and remove it from global list. */
801
static void raid_set_free(struct raid_set *rs)
N
NeilBrown 已提交
802 803 804
{
	int i;

805 806 807 808 809
	if (rs->journal_dev.dev) {
		md_rdev_clear(&rs->journal_dev.rdev);
		dm_put_device(rs->ti, rs->journal_dev.dev);
	}

810
	for (i = 0; i < rs->raid_disks; i++) {
811 812
		if (rs->dev[i].meta_dev)
			dm_put_device(rs->ti, rs->dev[i].meta_dev);
813
		md_rdev_clear(&rs->dev[i].rdev);
N
NeilBrown 已提交
814 815
		if (rs->dev[i].data_dev)
			dm_put_device(rs->ti, rs->dev[i].data_dev);
816
	}
N
NeilBrown 已提交
817

818 819
	list_del(&rs->list);

N
NeilBrown 已提交
820 821 822 823 824 825 826 827
	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
 *
828 829 830 831 832 833 834 835 836
 * 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,
837
 * the caller must use raid_set_free() to unwind the operations.
N
NeilBrown 已提交
838
 */
839
static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
N
NeilBrown 已提交
840 841 842 843
{
	int i;
	int rebuild = 0;
	int metadata_available = 0;
844
	int r = 0;
845
	const char *arg;
N
NeilBrown 已提交
846

847 848 849 850 851
	/* Put off the number of raid devices argument to get to dev pairs */
	arg = dm_shift_arg(as);
	if (!arg)
		return -EINVAL;

852
	for (i = 0; i < rs->raid_disks; i++) {
N
NeilBrown 已提交
853 854 855 856 857 858
		rs->dev[i].rdev.raid_disk = i;

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

		/*
859 860
		 * There are no offsets initially.
		 * Out of place reshape will set them accordingly.
N
NeilBrown 已提交
861 862
		 */
		rs->dev[i].rdev.data_offset = 0;
863
		rs->dev[i].rdev.new_data_offset = 0;
N
NeilBrown 已提交
864 865
		rs->dev[i].rdev.mddev = &rs->md;

866 867 868 869 870
		arg = dm_shift_arg(as);
		if (!arg)
			return -EINVAL;

		if (strcmp(arg, "-")) {
871 872 873 874 875 876
			r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
					  &rs->dev[i].meta_dev);
			if (r) {
				rs->ti->error = "RAID metadata device lookup failure";
				return r;
			}
877 878

			rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
879 880 881 882
			if (!rs->dev[i].rdev.sb_page) {
				rs->ti->error = "Failed to allocate superblock page";
				return -ENOMEM;
			}
N
NeilBrown 已提交
883 884
		}

885 886 887 888 889
		arg = dm_shift_arg(as);
		if (!arg)
			return -EINVAL;

		if (!strcmp(arg, "-")) {
N
NeilBrown 已提交
890
			if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
891 892 893 894
			    (!rs->dev[i].rdev.recovery_offset)) {
				rs->ti->error = "Drive designated for rebuild not specified";
				return -EINVAL;
			}
N
NeilBrown 已提交
895

896 897 898 899
			if (rs->dev[i].meta_dev) {
				rs->ti->error = "No data device supplied with metadata device";
				return -EINVAL;
			}
900

N
NeilBrown 已提交
901 902 903
			continue;
		}

904 905 906 907 908 909
		r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
				  &rs->dev[i].data_dev);
		if (r) {
			rs->ti->error = "RAID device lookup failure";
			return r;
		}
N
NeilBrown 已提交
910

911 912 913 914
		if (rs->dev[i].meta_dev) {
			metadata_available = 1;
			rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
		}
N
NeilBrown 已提交
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);
N
NeilBrown 已提交
917 918 919 920
		if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
			rebuild++;
	}

921 922 923
	if (rs->journal_dev.dev)
		list_add_tail(&rs->journal_dev.rdev.same_set, &rs->md.disks);

N
NeilBrown 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
	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.
		 */
940 941
		rs->ti->error = "Unable to rebuild drive while array is not in-sync";
		return -EINVAL;
N
NeilBrown 已提交
942 943 944 945 946
	}

	return 0;
}

947 948 949 950 951 952 953 954 955 956 957 958 959 960
/*
 * 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);

961 962 963
	if (rs_is_raid0(rs))
		return 0;

964 965
	if (!region_size) {
		/*
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 972 973 974 975 976 977 978 979 980
			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.
		 */
981 982 983 984
		if (region_size > rs->ti->len) {
			rs->ti->error = "Supplied region size is too large";
			return -EINVAL;
		}
985 986 987 988

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

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

998 999 1000 1001
		if (region_size < rs->md.chunk_sectors) {
			rs->ti->error = "Region size is smaller than the chunk size";
			return -EINVAL;
		}
1002 1003 1004 1005 1006
	}

	/*
	 * Convert sectors to bytes.
	 */
1007
	rs->md.bitmap_info.chunksize = to_bytes(region_size);
1008 1009 1010 1011

	return 0;
}

1012
/*
1013
 * validate_raid_redundancy
1014 1015
 * @rs
 *
1016 1017
 * Determine if there are enough devices in the array that haven't
 * failed (or are being rebuilt) to form a usable array.
1018 1019 1020
 *
 * Returns: 0 on success, -EINVAL on failure.
 */
1021
static int validate_raid_redundancy(struct raid_set *rs)
1022
{
1023 1024 1025
	unsigned int i, rebuild_cnt = 0;
	unsigned int rebuilds_per_group = 0, copies;
	unsigned int group_size, last_group_start;
1026 1027

	for (i = 0; i < rs->md.raid_disks; i++)
1028 1029
		if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
		    !rs->dev[i].rdev.sb_page)
1030 1031
			rebuild_cnt++;

1032
	switch (rs->md.level) {
1033 1034
	case 0:
		break;
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
	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:
1046
		copies = raid10_md_layout_to_copies(rs->md.new_layout);
1047 1048 1049 1050 1051
		if (copies < 2) {
			DMERR("Bogus raid10 data copies < 2!");
			return -EINVAL;
		}

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
		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.
1065 1066 1067
		 * E.g.	   dev1 dev2 dev3 dev4 dev5
		 *	    A	 A    B	   B	C
		 *	    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 1072
				if (!(i % copies))
					rebuilds_per_group = 0;
1073
				if ((!rs->dev[i].rdev.sb_page ||
1074
				    !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
				    (++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
1089
		 * for arrays that are not a multiple of (far) copies.	This
1090 1091 1092 1093 1094 1095 1096 1097
		 * 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))
1098
				rebuilds_per_group = 0;
1099 1100
			if ((!rs->dev[i].rdev.sb_page ||
			     !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
1101
			    (++rebuilds_per_group >= copies))
1102
					goto too_many;
1103 1104
		}
		break;
1105
	default:
1106 1107
		if (rebuild_cnt)
			return -EINVAL;
1108 1109 1110 1111 1112 1113 1114 1115
	}

	return 0;

too_many:
	return -EINVAL;
}

N
NeilBrown 已提交
1116 1117 1118 1119
/*
 * Possible arguments are...
 *	<chunk_size> [optional_args]
 *
J
Jonathan Brassow 已提交
1120 1121
 * Argument definitions
 *    <chunk_size>			The number of sectors per disk that
1122
 *					will form the "stripe"
J
Jonathan Brassow 已提交
1123
 *    [[no]sync]			Force or prevent recovery of the
1124
 *					entire array
N
NeilBrown 已提交
1125
 *    [rebuild <idx>]			Rebuild the drive indicated by the index
J
Jonathan Brassow 已提交
1126
 *    [daemon_sleep <ms>]		Time between bitmap daemon work to
1127
 *					clear bits
N
NeilBrown 已提交
1128 1129
 *    [min_recovery_rate <kB/sec/disk>]	Throttle RAID initialization
 *    [max_recovery_rate <kB/sec/disk>]	Throttle RAID initialization
1130
 *    [write_mostly <idx>]		Indicate a write mostly drive via index
N
NeilBrown 已提交
1131 1132
 *    [max_write_behind <sectors>]	See '-write-behind=' (man mdadm)
 *    [stripe_cache <sectors>]		Stripe cache size for higher RAIDs
1133
 *    [region_size <sectors>]		Defines granularity of bitmap
1134 1135
 *    [journal_dev <dev>]		raid4/5/6 journaling deviice
 *    					(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)
N
NeilBrown 已提交
1140
 */
1141
static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
1142
			     unsigned int num_raid_params)
N
NeilBrown 已提交
1143
{
1144
	int value, raid10_format = ALGORITHM_RAID10_DEFAULT;
1145 1146 1147
	unsigned int raid10_copies = 2;
	unsigned int i, write_mostly = 0;
	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 1154 1155

	arg = dm_shift_arg(as);
	num_raid_params--; /* Account for chunk_size argument */

1156
	if (kstrtoint(arg, 10, &value) < 0) {
1157 1158 1159
		rs->ti->error = "Bad numerical argument given for chunk_size";
		return -EINVAL;
	}
N
NeilBrown 已提交
1160 1161 1162

	/*
	 * First, parse the in-order required arguments
J
Jonathan Brassow 已提交
1163
	 * "chunk_size" is the only argument of this type.
N
NeilBrown 已提交
1164
	 */
1165
	if (rt_is_raid1(rt)) {
J
Jonathan Brassow 已提交
1166 1167 1168
		if (value)
			DMERR("Ignoring chunk size parameter for RAID 1");
		value = 0;
1169 1170 1171 1172 1173 1174 1175
	} 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 已提交
1176 1177 1178 1179

	rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;

	/*
1180 1181 1182 1183 1184
	 * 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'.
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 1194
	 *
	 * What is found in the superblocks of the devices is always
	 * authoritative, unless 'rebuild' or '[no]sync' was specified.
N
NeilBrown 已提交
1195
	 */
1196
	for (i = 0; i < rs->raid_disks; i++) {
N
NeilBrown 已提交
1197
		set_bit(In_sync, &rs->dev[i].rdev.flags);
1198 1199
		rs->dev[i].rdev.recovery_offset = MaxSector;
	}
N
NeilBrown 已提交
1200

1201 1202 1203
	/*
	 * Second, parse the unordered optional arguments
	 */
N
NeilBrown 已提交
1204
	for (i = 0; i < num_raid_params; i++) {
1205
		key = dm_shift_arg(as);
1206 1207 1208 1209
		if (!key) {
			rs->ti->error = "Not enough raid parameters given";
			return -EINVAL;
		}
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 1214 1215
				rs->ti->error = "Only one 'nosync' argument allowed";
				return -EINVAL;
			}
N
NeilBrown 已提交
1216 1217
			continue;
		}
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 1221 1222
				rs->ti->error = "Only one 'sync' argument allowed";
				return -EINVAL;
			}
1223 1224
			continue;
		}
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 1228 1229
				rs->ti->error = "Only one 'raid10_use_new_sets' argument allowed";
				return -EINVAL;
			}
N
NeilBrown 已提交
1230 1231 1232
			continue;
		}

1233 1234
		arg = dm_shift_arg(as);
		i++; /* Account for the argument pairs */
1235 1236 1237 1238
		if (!arg) {
			rs->ti->error = "Wrong number of raid parameters given";
			return -EINVAL;
		}
1239

1240 1241 1242
		/*
		 * Parameters that take a string value are checked here.
		 */
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 1247 1248 1249 1250 1251 1252
				rs->ti->error = "Only one 'raid10_format' argument pair allowed";
				return -EINVAL;
			}
			if (!rt_is_raid10(rt)) {
				rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
				return -EINVAL;
			}
1253
			raid10_format = raid10_name_to_format(arg);
1254 1255 1256 1257
			if (raid10_format < 0) {
				rs->ti->error = "Invalid 'raid10_format' value given";
				return raid10_format;
			}
1258 1259 1260
			continue;
		}

1261
		/* "journal_dev <dev>" */
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
		if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV))) {
			int r;
			struct md_rdev *jdev;

			if (test_and_set_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
				rs->ti->error = "Only one raid4/5/6 set journaling device allowed";
				return -EINVAL;
			}
			if (!rt_is_raid456(rt)) {
				rs->ti->error = "'journal_dev' is an invalid parameter for this RAID type";
				return -EINVAL;
			}
			r = dm_get_device(rs->ti, arg, dm_table_get_mode(rs->ti->table),
					  &rs->journal_dev.dev);
			if (r) {
				rs->ti->error = "raid4/5/6 journal device lookup failure";
				return r;
			}
			jdev = &rs->journal_dev.rdev;
			md_rdev_init(jdev);
			jdev->mddev = &rs->md;
			jdev->bdev = rs->journal_dev.dev->bdev;
			jdev->sectors = to_sector(i_size_read(jdev->bdev->bd_inode));
			if (jdev->sectors < MIN_RAID456_JOURNAL_SPACE) {
				rs->ti->error = "No space for raid4/5/6 journal";
				return -ENOSPC;
			}
1289
			rs->journal_dev.mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
1290 1291 1292 1293
			set_bit(Journal, &jdev->flags);
			continue;
		}

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
		/* "journal_mode <mode>" ("journal_dev" mandatory!) */
		if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE))) {
			int r;

			if (!test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
				rs->ti->error = "raid4/5/6 'journal_mode' is invalid without 'journal_dev'";
				return -EINVAL;
			}
			if (test_and_set_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
				rs->ti->error = "Only one raid4/5/6 'journal_mode' argument allowed";
				return -EINVAL;
			}
			r = dm_raid_journal_mode_to_md(arg);
			if (r < 0) {
				rs->ti->error = "Invalid 'journal_mode' argument";
				return r;
			}
			rs->journal_dev.mode = r;
			continue;
		}

1315 1316 1317
		/*
		 * Parameters with number values from here on.
		 */
1318
		if (kstrtoint(arg, 10, &value) < 0) {
1319 1320 1321
			rs->ti->error = "Bad numerical argument given in raid params";
			return -EINVAL;
		}
1322

1323
		if (!strcasecmp(key, dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD))) {
1324 1325 1326 1327
			/*
			 * "rebuild" is being passed in by userspace to provide
			 * indexes of replaced devices and to set up additional
			 * devices on raid level takeover.
1328
			 */
1329
			if (!__within_range(value, 0, rs->raid_disks - 1)) {
1330 1331 1332
				rs->ti->error = "Invalid rebuild index given";
				return -EINVAL;
			}
1333

1334 1335 1336 1337
			if (test_and_set_bit(value, (void *) rs->rebuild_disks)) {
				rs->ti->error = "rebuild for this index already given";
				return -EINVAL;
			}
1338

1339 1340 1341 1342
			rd = rs->dev + value;
			clear_bit(In_sync, &rd->rdev.flags);
			clear_bit(Faulty, &rd->rdev.flags);
			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 1346 1347 1348
			if (!rt_is_raid1(rt)) {
				rs->ti->error = "write_mostly option is only valid for RAID1";
				return -EINVAL;
			}
1349

1350
			if (!__within_range(value, 0, rs->md.raid_disks - 1)) {
1351 1352 1353
				rs->ti->error = "Invalid write_mostly index given";
				return -EINVAL;
			}
N
NeilBrown 已提交
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 1360 1361 1362
			if (!rt_is_raid1(rt)) {
				rs->ti->error = "max_write_behind option is only valid for RAID1";
				return -EINVAL;
			}
1363

1364
			if (test_and_set_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags)) {
1365 1366 1367
				rs->ti->error = "Only one max_write_behind argument pair allowed";
				return -EINVAL;
			}
N
NeilBrown 已提交
1368 1369 1370 1371 1372 1373

			/*
			 * In device-mapper, we specify things in sectors, but
			 * MD records this value in kB
			 */
			value /= 2;
1374 1375 1376 1377
			if (value > COUNTER_MAX) {
				rs->ti->error = "Max write-behind limit out of range";
				return -EINVAL;
			}
1378

N
NeilBrown 已提交
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 1383 1384 1385 1386 1387 1388
				rs->ti->error = "Only one daemon_sleep argument pair allowed";
				return -EINVAL;
			}
			if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
				rs->ti->error = "daemon sleep period out of range";
				return -EINVAL;
			}
N
NeilBrown 已提交
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 1394 1395
				rs->ti->error = "Only one data_offset argument pair allowed";
				return -EINVAL;
			}
1396
			/* Ensure sensible data offset */
1397 1398
			if (value < 0 ||
			    (value && (value < MIN_FREE_RESHAPE_SPACE || value % to_sector(PAGE_SIZE)))) {
1399 1400 1401
				rs->ti->error = "Bogus data_offset value";
				return -EINVAL;
			}
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 1407 1408
				rs->ti->error = "Only one delta_disks argument pair allowed";
				return -EINVAL;
			}
1409
			/* Ensure MAX_RAID_DEVICES and raid type minimal_devs! */
1410
			if (!__within_range(abs(value), 1, MAX_RAID_DEVICES - rt->minimal_devs)) {
1411 1412 1413
				rs->ti->error = "Too many delta_disk requested";
				return -EINVAL;
			}
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 1419 1420 1421 1422 1423 1424 1425
				rs->ti->error = "Only one stripe_cache argument pair allowed";
				return -EINVAL;
			}

			if (!rt_is_raid456(rt)) {
				rs->ti->error = "Inappropriate argument: stripe_cache";
				return -EINVAL;
			}
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 1431 1432 1433 1434 1435 1436
				rs->ti->error = "Only one min_recovery_rate argument pair allowed";
				return -EINVAL;
			}
			if (value > INT_MAX) {
				rs->ti->error = "min_recovery_rate out of range";
				return -EINVAL;
			}
N
NeilBrown 已提交
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 1441 1442 1443 1444 1445 1446
				rs->ti->error = "Only one max_recovery_rate argument pair allowed";
				return -EINVAL;
			}
			if (value > INT_MAX) {
				rs->ti->error = "max_recovery_rate out of range";
				return -EINVAL;
			}
N
NeilBrown 已提交
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 1451 1452
				rs->ti->error = "Only one region_size argument pair allowed";
				return -EINVAL;
			}
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 1459 1460
				rs->ti->error = "Only one raid10_copies argument pair allowed";
				return -EINVAL;
			}
1461

1462
			if (!__within_range(value, 2, rs->md.raid_disks)) {
1463 1464 1465
				rs->ti->error = "Bad value for 'raid10_copies'";
				return -EINVAL;
			}
1466

1467
			raid10_copies = value;
N
NeilBrown 已提交
1468 1469
		} else {
			DMERR("Unable to parse RAID parameter: %s", key);
1470 1471
			rs->ti->error = "Unable to parse RAID parameter";
			return -EINVAL;
N
NeilBrown 已提交
1472 1473 1474
		}
	}

1475 1476 1477 1478 1479 1480
	if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) &&
	    test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
		rs->ti->error = "sync and nosync are mutually exclusive";
		return -EINVAL;
	}

1481 1482 1483 1484 1485 1486 1487
	if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) &&
	    (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ||
	     test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))) {
		rs->ti->error = "sync/nosync and rebuild are mutually exclusive";
		return -EINVAL;
	}

1488 1489 1490 1491 1492
	if (write_mostly >= rs->md.raid_disks) {
		rs->ti->error = "Can't set all raid1 devices to write_mostly";
		return -EINVAL;
	}

1493 1494 1495 1496
	if (validate_region_size(rs, region_size))
		return -EINVAL;

	if (rs->md.chunk_sectors)
1497
		max_io_len = rs->md.chunk_sectors;
1498
	else
1499
		max_io_len = region_size;
1500

1501 1502
	if (dm_set_target_max_io_len(rs->ti, max_io_len))
		return -EINVAL;
J
Jonathan Brassow 已提交
1503

1504
	if (rt_is_raid10(rt)) {
1505 1506 1507 1508
		if (raid10_copies > rs->md.raid_disks) {
			rs->ti->error = "Not enough devices to satisfy specification";
			return -EINVAL;
		}
1509

1510
		rs->md.new_layout = raid10_format_to_md_layout(rs, raid10_format, raid10_copies);
1511 1512 1513 1514
		if (rs->md.new_layout < 0) {
			rs->ti->error = "Error getting raid10 format";
			return rs->md.new_layout;
		}
1515 1516

		rt = get_raid_type_by_ll(10, rs->md.new_layout);
1517 1518 1519 1520
		if (!rt) {
			rs->ti->error = "Failed to recognize new raid10 layout";
			return -EINVAL;
		}
1521 1522 1523

		if ((rt->algorithm == ALGORITHM_RAID10_DEFAULT ||
		     rt->algorithm == ALGORITHM_RAID10_NEAR) &&
1524
		    test_bit(__CTR_FLAG_RAID10_USE_NEAR_SETS, &rs->ctr_flags)) {
1525 1526 1527 1528
			rs->ti->error = "RAID10 format 'near' and 'raid10_use_near_sets' are incompatible";
			return -EINVAL;
		}
	}
1529

1530
	rs->raid10_copies = raid10_copies;
1531

N
NeilBrown 已提交
1532 1533 1534 1535
	/* Assume there are no metadata devices until the drives are parsed */
	rs->md.persistent = 0;
	rs->md.external = 1;

1536
	/* Check, if any invalid ctr arguments have been passed in for the raid level */
1537
	return rs_check_for_valid_flags(rs);
N
NeilBrown 已提交
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
/* Set raid4/5/6 cache size */
static int rs_set_raid456_stripe_cache(struct raid_set *rs)
{
	int r;
	struct r5conf *conf;
	struct mddev *mddev = &rs->md;
	uint32_t min_stripes = max(mddev->chunk_sectors, mddev->new_chunk_sectors) / 2;
	uint32_t nr_stripes = rs->stripe_cache_entries;

	if (!rt_is_raid456(rs->raid_type)) {
		rs->ti->error = "Inappropriate raid level; cannot change stripe_cache size";
		return -EINVAL;
	}

	if (nr_stripes < min_stripes) {
		DMINFO("Adjusting requested %u stripe cache entries to %u to suit stripe size",
		       nr_stripes, min_stripes);
		nr_stripes = min_stripes;
	}

	conf = mddev->private;
	if (!conf) {
		rs->ti->error = "Cannot change stripe_cache size on inactive RAID set";
		return -EINVAL;
	}

	/* Try setting number of stripes in raid456 stripe cache */
	if (conf->min_nr_stripes != nr_stripes) {
		r = raid5_set_cache_size(mddev, nr_stripes);
		if (r) {
			rs->ti->error = "Failed to set raid4/5/6 stripe cache size";
			return r;
		}

		DMINFO("%u stripe cache entries", nr_stripes);
	}

	return 0;
}

1580 1581 1582 1583 1584 1585
/* Return # of data stripes as kept in mddev as of @rs (i.e. as of superblock) */
static unsigned int mddev_data_stripes(struct raid_set *rs)
{
	return rs->md.raid_disks - rs->raid_type->parity_devs;
}

1586 1587 1588 1589 1590 1591
/* Return # of data stripes of @rs (i.e. as of ctr) */
static unsigned int rs_data_stripes(struct raid_set *rs)
{
	return rs->raid_disks - rs->raid_type->parity_devs;
}

1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
/*
 * Retrieve rdev->sectors from any valid raid device of @rs
 * to allow userpace to pass in arbitray "- -" device tupples.
 */
static sector_t __rdev_sectors(struct raid_set *rs)
{
	int i;

	for (i = 0; i < rs->md.raid_disks; i++) {
		struct md_rdev *rdev = &rs->dev[i].rdev;

1603 1604
		if (!test_bit(Journal, &rdev->flags) &&
		    rdev->bdev && rdev->sectors)
1605 1606 1607
			return rdev->sectors;
	}

1608
	return 0;
1609 1610
}

1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
/* Check that calculated dev_sectors fits all component devices. */
static int _check_data_dev_sectors(struct raid_set *rs)
{
	sector_t ds = ~0;
	struct md_rdev *rdev;

	rdev_for_each(rdev, &rs->md)
		if (!test_bit(Journal, &rdev->flags) && rdev->bdev) {
			ds = min(ds, to_sector(i_size_read(rdev->bdev->bd_inode)));
			if (ds < rs->md.dev_sectors) {
				rs->ti->error = "Component device(s) too small";
				return -EINVAL;
			}
		}

	return 0;
}

1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
/* Calculate the sectors per device and per array used for @rs */
static int rs_set_dev_and_array_sectors(struct raid_set *rs, bool use_mddev)
{
	int delta_disks;
	unsigned int data_stripes;
	struct mddev *mddev = &rs->md;
	struct md_rdev *rdev;
	sector_t array_sectors = rs->ti->len, dev_sectors = rs->ti->len;

	if (use_mddev) {
		delta_disks = mddev->delta_disks;
		data_stripes = mddev_data_stripes(rs);
	} else {
		delta_disks = rs->delta_disks;
		data_stripes = rs_data_stripes(rs);
	}

	/* Special raid1 case w/o delta_disks support (yet) */
	if (rt_is_raid1(rs->raid_type))
		;
	else if (rt_is_raid10(rs->raid_type)) {
		if (rs->raid10_copies < 2 ||
		    delta_disks < 0) {
			rs->ti->error = "Bogus raid10 data copies or delta disks";
1653
			return -EINVAL;
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
		}

		dev_sectors *= rs->raid10_copies;
		if (sector_div(dev_sectors, data_stripes))
			goto bad;

		array_sectors = (data_stripes + delta_disks) * dev_sectors;
		if (sector_div(array_sectors, rs->raid10_copies))
			goto bad;

	} else if (sector_div(dev_sectors, data_stripes))
		goto bad;

	else
		/* Striped layouts */
		array_sectors = (data_stripes + delta_disks) * dev_sectors;

	rdev_for_each(rdev, mddev)
1672 1673
		if (!test_bit(Journal, &rdev->flags))
			rdev->sectors = dev_sectors;
1674 1675 1676 1677

	mddev->array_sectors = array_sectors;
	mddev->dev_sectors = dev_sectors;

1678
	return _check_data_dev_sectors(rs);
1679 1680
bad:
	rs->ti->error = "Target length not divisible by number of data devices";
1681
	return -EINVAL;
1682 1683
}

1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
/* Setup recovery on @rs */
static void __rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
{
	/* raid0 does not recover */
	if (rs_is_raid0(rs))
		rs->md.recovery_cp = MaxSector;
	/*
	 * A raid6 set has to be recovered either
	 * completely or for the grown part to
	 * ensure proper parity and Q-Syndrome
	 */
	else if (rs_is_raid6(rs))
		rs->md.recovery_cp = dev_sectors;
	/*
	 * Other raid set types may skip recovery
	 * depending on the 'nosync' flag.
	 */
	else
		rs->md.recovery_cp = test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)
				     ? MaxSector : dev_sectors;
}

/* Setup recovery on @rs based on raid type, device size and 'nosync' flag */
static void rs_setup_recovery(struct raid_set *rs, sector_t dev_sectors)
{
	if (!dev_sectors)
		/* New raid set or 'sync' flag provided */
		__rs_setup_recovery(rs, 0);
	else if (dev_sectors == MaxSector)
		/* Prevent recovery */
		__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 1719 1720 1721
	else
		__rs_setup_recovery(rs, MaxSector);
}

N
NeilBrown 已提交
1722 1723 1724 1725
static void do_table_event(struct work_struct *ws)
{
	struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);

1726
	smp_rmb(); /* Make sure we access most actual mddev properties */
1727 1728 1729
	if (!rs_is_reshaping(rs)) {
		if (rs_is_raid10(rs))
			rs_set_rdev_sectors(rs);
1730
		rs_set_capacity(rs);
1731
	}
N
NeilBrown 已提交
1732 1733 1734 1735 1736 1737 1738
	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);

1739
	return mddev_congested(&rs->md, bits);
N
NeilBrown 已提交
1740 1741
}

1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
/*
 * Make sure a valid takover (level switch) is being requested on @rs
 *
 * Conversions of raid sets from one MD personality to another
 * have to conform to restrictions which are enforced here.
 */
static int rs_check_takeover(struct raid_set *rs)
{
	struct mddev *mddev = &rs->md;
	unsigned int near_copies;

1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
	if (rs->md.degraded) {
		rs->ti->error = "Can't takeover degraded raid set";
		return -EPERM;
	}

	if (rs_is_reshaping(rs)) {
		rs->ti->error = "Can't takeover reshaping raid set";
		return -EPERM;
	}

1763 1764 1765 1766 1767 1768 1769 1770 1771
	switch (mddev->level) {
	case 0:
		/* raid0 -> raid1/5 with one disk */
		if ((mddev->new_level == 1 || mddev->new_level == 5) &&
		    mddev->raid_disks == 1)
			return 0;

		/* raid0 -> raid10 */
		if (mddev->new_level == 10 &&
1772
		    !(rs->raid_disks % mddev->raid_disks))
1773 1774 1775
			return 0;

		/* raid0 with multiple disks -> raid4/5/6 */
1776
		if (__within_range(mddev->new_level, 4, 6) &&
1777 1778 1779 1780 1781 1782 1783 1784
		    mddev->new_layout == ALGORITHM_PARITY_N &&
		    mddev->raid_disks > 1)
			return 0;

		break;

	case 10:
		/* Can't takeover raid10_offset! */
M
Mike Snitzer 已提交
1785
		if (__is_raid10_offset(mddev->layout))
1786 1787
			break;

M
Mike Snitzer 已提交
1788
		near_copies = __raid10_near_copies(mddev->layout);
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801

		/* raid10* -> raid0 */
		if (mddev->new_level == 0) {
			/* Can takeover raid10_near with raid disks divisable by data copies! */
			if (near_copies > 1 &&
			    !(mddev->raid_disks % near_copies)) {
				mddev->raid_disks /= near_copies;
				mddev->delta_disks = mddev->raid_disks;
				return 0;
			}

			/* Can takeover raid10_far */
			if (near_copies == 1 &&
M
Mike Snitzer 已提交
1802
			    __raid10_far_copies(mddev->layout) > 1)
1803 1804 1805 1806 1807 1808 1809
				return 0;

			break;
		}

		/* raid10_{near,far} -> raid1 */
		if (mddev->new_level == 1 &&
M
Mike Snitzer 已提交
1810
		    max(near_copies, __raid10_far_copies(mddev->layout)) == mddev->raid_disks)
1811 1812 1813
			return 0;

		/* raid10_{near,far} with 2 disks -> raid4/5 */
1814
		if (__within_range(mddev->new_level, 4, 5) &&
1815 1816 1817 1818 1819 1820
		    mddev->raid_disks == 2)
			return 0;
		break;

	case 1:
		/* raid1 with 2 disks -> raid4/5 */
1821
		if (__within_range(mddev->new_level, 4, 5) &&
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
		    mddev->raid_disks == 2) {
			mddev->degraded = 1;
			return 0;
		}

		/* raid1 -> raid0 */
		if (mddev->new_level == 0 &&
		    mddev->raid_disks == 1)
			return 0;

		/* raid1 -> raid10 */
		if (mddev->new_level == 10)
			return 0;
		break;

	case 4:
		/* raid4 -> raid0 */
		if (mddev->new_level == 0)
			return 0;

		/* raid4 -> raid1/5 with 2 disks */
		if ((mddev->new_level == 1 || mddev->new_level == 5) &&
		    mddev->raid_disks == 2)
			return 0;

		/* raid4 -> raid5/6 with parity N */
1848
		if (__within_range(mddev->new_level, 5, 6) &&
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
		    mddev->layout == ALGORITHM_PARITY_N)
			return 0;
		break;

	case 5:
		/* raid5 with parity N -> raid0 */
		if (mddev->new_level == 0 &&
		    mddev->layout == ALGORITHM_PARITY_N)
			return 0;

		/* raid5 with parity N -> raid4 */
		if (mddev->new_level == 4 &&
		    mddev->layout == ALGORITHM_PARITY_N)
			return 0;

		/* raid5 with 2 disks -> raid1/4/10 */
		if ((mddev->new_level == 1 || mddev->new_level == 4 || mddev->new_level == 10) &&
		    mddev->raid_disks == 2)
			return 0;

1869
		/* raid5_* ->  raid6_*_6 with Q-Syndrome N (e.g. raid5_ra -> raid6_ra_6 */
1870 1871
		if (mddev->new_level == 6 &&
		    ((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 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
			return 0;
		break;

	case 6:
		/* raid6 with parity N -> raid0 */
		if (mddev->new_level == 0 &&
		    mddev->layout == ALGORITHM_PARITY_N)
			return 0;

		/* raid6 with parity N -> raid4 */
		if (mddev->new_level == 4 &&
		    mddev->layout == ALGORITHM_PARITY_N)
			return 0;

1887
		/* raid6_*_n with Q-Syndrome N -> raid5_* */
1888 1889
		if (mddev->new_level == 5 &&
		    ((mddev->layout == ALGORITHM_PARITY_N && mddev->new_layout == ALGORITHM_PARITY_N) ||
1890
		     __within_range(mddev->new_layout, ALGORITHM_LEFT_ASYMMETRIC, ALGORITHM_RIGHT_SYMMETRIC)))
1891 1892 1893 1894 1895 1896
			return 0;

	default:
		break;
	}

1897 1898
	rs->ti->error = "takeover not possible";
	return -EINVAL;
1899 1900 1901 1902 1903 1904 1905 1906
}

/* True if @rs requested to be taken over */
static bool rs_takeover_requested(struct raid_set *rs)
{
	return rs->md.new_level != rs->md.level;
}

1907 1908 1909
/* True if @rs is requested to reshape by ctr */
static bool rs_reshape_requested(struct raid_set *rs)
{
1910
	bool change;
1911 1912
	struct mddev *mddev = &rs->md;

1913 1914 1915
	if (rs_takeover_requested(rs))
		return false;

H
Heinz Mauelshagen 已提交
1916
	if (rs_is_raid0(rs))
1917 1918
		return false;

1919 1920 1921 1922 1923
	change = mddev->new_layout != mddev->layout ||
		 mddev->new_chunk_sectors != mddev->chunk_sectors ||
		 rs->delta_disks;

	/* Historical case to support raid1 reshape without delta disks */
H
Heinz Mauelshagen 已提交
1924
	if (rs_is_raid1(rs)) {
1925 1926 1927
		if (rs->delta_disks)
			return !!rs->delta_disks;

1928 1929
		return !change &&
		       mddev->raid_disks != rs->raid_disks;
1930
	}
1931

H
Heinz Mauelshagen 已提交
1932
	if (rs_is_raid10(rs))
1933 1934 1935 1936 1937
		return change &&
		       !__is_raid10_far(mddev->new_layout) &&
		       rs->delta_disks >= 0;

	return change;
1938 1939
}

1940
/*  Features */
1941
#define	FEATURE_FLAG_SUPPORTS_V190	0x1 /* Supports extended superblock */
1942 1943 1944 1945 1946

/* State flags for sb->flags */
#define	SB_FLAG_RESHAPE_ACTIVE		0x1
#define	SB_FLAG_RESHAPE_BACKWARDS	0x2

1947 1948 1949 1950 1951 1952 1953
/*
 * 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" */
1954
	__le32 compat_features;	/* Used to indicate compatible features (like 1.9.0 ondisk metadata extension) */
1955

1956 1957
	__le32 num_devices;	/* Number of devices in this raid set. (Max 64) */
	__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 1965 1966 1967 1968 1969

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

	/*
1970
	 * This offset tracks the progress of the initial raid set
1971 1972 1973 1974 1975
	 * synchronisation/parity calculation.
	 */
	__le64 array_resync_offset;

	/*
1976
	 * raid characteristics
1977 1978 1979 1980 1981
	 */
	__le32 level;
	__le32 layout;
	__le32 stripe_sectors;

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 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
	 */

	__le32 flags; /* Flags defining array states for reshaping */

	/*
	 * This offset tracks the progress of a raid
	 * set reshape in order to be able to restart it
	 */
	__le64 reshape_position;

	/*
	 * These define the properties of the array in case of an interrupted reshape
	 */
	__le32 new_level;
	__le32 new_layout;
	__le32 new_stripe_sectors;
	__le32 delta_disks;

	__le64 array_sectors; /* Array size in sectors */

	/*
	 * Sector offsets to data on devices (reshaping).
	 * Needed to support out of place reshaping, thus
	 * not writing over any stripes whilst converting
	 * them from old to new layout
	 */
	__le64 data_offset;
	__le64 new_data_offset;

	__le64 sectors; /* Used device size in sectors */

	/*
	 * 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 2022 2023 2024 2025
	 */
	__le64 extended_failed_devices[DISKS_ARRAY_ELEMS - 1];

	__le32 incompat_features;	/* Used to indicate any incompatible features */

	/* Always set rest up to logical block size to 0 when writing (see get_metadata_device() below). */
2026 2027
} __packed;

2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
/*
 * Check for reshape constraints on raid set @rs:
 *
 * - reshape function non-existent
 * - degraded set
 * - ongoing recovery
 * - ongoing reshape
 *
 * Returns 0 if none or -EPERM if given constraint
 * and error message reference in @errmsg
 */
static int rs_check_reshape(struct raid_set *rs)
{
	struct mddev *mddev = &rs->md;

	if (!mddev->pers || !mddev->pers->check_reshape)
		rs->ti->error = "Reshape not supported";
	else if (mddev->degraded)
		rs->ti->error = "Can't reshape degraded raid set";
	else if (rs_is_recovering(rs))
		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 2052
	else if (!(rs_is_raid1(rs) || rs_is_raid10(rs) || rs_is_raid456(rs)))
		rs->ti->error = "Reshaping only supported for raid1/4/5/6/10";
2053 2054 2055 2056 2057 2058
	else
		return 0;

	return -EPERM;
}

2059
static int read_disk_sb(struct md_rdev *rdev, int size, bool force_reload)
2060 2061 2062
{
	BUG_ON(!rdev->sb_page);

2063
	if (rdev->sb_loaded && !force_reload)
2064 2065
		return 0;

2066 2067
	rdev->sb_loaded = 0;

2068
	if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) {
2069 2070
		DMERR("Failed to read superblock of device at position %d",
		      rdev->raid_disk);
2071
		md_error(rdev->mddev, rdev);
2072 2073
		set_bit(Faulty, &rdev->flags);
		return -EIO;
2074 2075 2076 2077 2078 2079 2080
	}

	rdev->sb_loaded = 1;

	return 0;
}

2081 2082 2083 2084 2085
static void sb_retrieve_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
{
	failed_devices[0] = le64_to_cpu(sb->failed_devices);
	memset(failed_devices + 1, 0, sizeof(sb->extended_failed_devices));

2086
	if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
2087 2088 2089 2090 2091 2092 2093
		int i = ARRAY_SIZE(sb->extended_failed_devices);

		while (i--)
			failed_devices[i+1] = le64_to_cpu(sb->extended_failed_devices[i]);
	}
}

2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
static void sb_update_failed_devices(struct dm_raid_superblock *sb, uint64_t *failed_devices)
{
	int i = ARRAY_SIZE(sb->extended_failed_devices);

	sb->failed_devices = cpu_to_le64(failed_devices[0]);
	while (i--)
		sb->extended_failed_devices[i] = cpu_to_le64(failed_devices[i+1]);
}

/*
 * Synchronize the superblock members with the raid set properties
 *
 * All superblock data is little endian.
 */
2108
static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
2109
{
2110 2111 2112
	bool update_failed_devices = false;
	unsigned int i;
	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 2117 2118 2119 2120 2121
	/* No metadata device, no superblock */
	if (!rdev->meta_bdev)
		return;

	BUG_ON(!rdev->sb_page);

2122 2123
	sb = page_address(rdev->sb_page);

2124
	sb_retrieve_failed_devices(sb, failed_devices);
2125

2126 2127 2128 2129 2130 2131 2132 2133
	for (i = 0; i < rs->raid_disks; i++)
		if (!rs->dev[i].data_dev || test_bit(Faulty, &rs->dev[i].rdev.flags)) {
			update_failed_devices = true;
			set_bit(i, (void *) failed_devices);
		}

	if (update_failed_devices)
		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 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148

	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->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);
2149

2150 2151 2152 2153 2154
	/********************************************************************
	 * BELOW FOLLOW V1.9.0 EXTENSIONS TO THE PRISTINE SUPERBLOCK FORMAT!!!
	 *
	 * FEATURE_FLAG_SUPPORTS_V190 in the compat_features member indicates that those exist
	 */
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
	sb->new_level = cpu_to_le32(mddev->new_level);
	sb->new_layout = cpu_to_le32(mddev->new_layout);
	sb->new_stripe_sectors = cpu_to_le32(mddev->new_chunk_sectors);

	sb->delta_disks = cpu_to_le32(mddev->delta_disks);

	smp_rmb(); /* Make sure we access most recent reshape position */
	sb->reshape_position = cpu_to_le64(mddev->reshape_position);
	if (le64_to_cpu(sb->reshape_position) != MaxSector) {
		/* Flag ongoing reshape */
		sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE);

		if (mddev->delta_disks < 0 || mddev->reshape_backwards)
			sb->flags |= cpu_to_le32(SB_FLAG_RESHAPE_BACKWARDS);
2169 2170 2171 2172
	} else {
		/* Clear reshape flags */
		sb->flags &= ~(cpu_to_le32(SB_FLAG_RESHAPE_ACTIVE|SB_FLAG_RESHAPE_BACKWARDS));
	}
2173 2174 2175 2176 2177

	sb->array_sectors = cpu_to_le64(mddev->array_sectors);
	sb->data_offset = cpu_to_le64(rdev->data_offset);
	sb->new_data_offset = cpu_to_le64(rdev->new_data_offset);
	sb->sectors = cpu_to_le64(rdev->sectors);
2178
	sb->incompat_features = cpu_to_le32(0);
2179 2180 2181

	/* Zero out the rest of the payload after the size of the superblock */
	memset(sb + 1, 0, rdev->sb_size - sizeof(*sb));
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
}

/*
 * 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
 */
2192
static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
2193
{
2194
	int r;
2195 2196 2197 2198
	struct dm_raid_superblock *sb;
	struct dm_raid_superblock *refsb;
	uint64_t events_sb, events_refsb;

2199
	r = read_disk_sb(rdev, rdev->sb_size, false);
2200 2201
	if (r)
		return r;
2202 2203

	sb = page_address(rdev->sb_page);
2204 2205 2206 2207 2208 2209 2210 2211

	/*
	 * 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)) {
2212 2213 2214
		super_sync(rdev->mddev, rdev);

		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 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234

		/* 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;
}

2235
static int super_init_validation(struct raid_set *rs, struct md_rdev *rdev)
2236 2237
{
	int role;
2238 2239
	unsigned int d;
	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;
N
NeilBrown 已提交
2244
	struct md_rdev *r;
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
	struct dm_raid_superblock *sb2;

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

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

2255 2256
	mddev->reshape_position = MaxSector;

2257 2258 2259 2260 2261
	mddev->raid_disks = le32_to_cpu(sb->num_devices);
	mddev->level = le32_to_cpu(sb->level);
	mddev->layout = le32_to_cpu(sb->layout);
	mddev->chunk_sectors = le32_to_cpu(sb->stripe_sectors);

2262
	/*
2263 2264
	 * Reshaping is supported, e.g. reshape_position is valid
	 * in superblock and superblock content is authoritative.
2265
	 */
2266
	if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190) {
2267 2268 2269 2270 2271 2272 2273 2274
		/* Superblock is authoritative wrt given raid set layout! */
		mddev->new_level = le32_to_cpu(sb->new_level);
		mddev->new_layout = le32_to_cpu(sb->new_layout);
		mddev->new_chunk_sectors = le32_to_cpu(sb->new_stripe_sectors);
		mddev->delta_disks = le32_to_cpu(sb->delta_disks);
		mddev->array_sectors = le64_to_cpu(sb->array_sectors);

		/* raid was reshaping and got interrupted */
2275 2276
		if (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_ACTIVE) {
			if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags)) {
2277 2278 2279
				DMERR("Reshape requested but raid set is still reshaping");
				return -EINVAL;
			}
2280

2281
			if (mddev->delta_disks < 0 ||
2282
			    (!mddev->delta_disks && (le32_to_cpu(sb->flags) & SB_FLAG_RESHAPE_BACKWARDS)))
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
				mddev->reshape_backwards = 1;
			else
				mddev->reshape_backwards = 0;

			mddev->reshape_position = le64_to_cpu(sb->reshape_position);
			rs->raid_type = get_raid_type_by_ll(mddev->level, mddev->layout);
		}

	} else {
		/*
2293
		 * No takeover/reshaping, because we don't have the extended v1.9.0 metadata
2294
		 */
2295 2296
		struct raid_type *rt_cur = get_raid_type_by_ll(mddev->level, mddev->layout);
		struct raid_type *rt_new = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
2297

2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
		if (rs_takeover_requested(rs)) {
			if (rt_cur && rt_new)
				DMERR("Takeover raid sets from %s to %s not yet supported by metadata. (raid level change)",
				      rt_cur->name, rt_new->name);
			else
				DMERR("Takeover raid sets not yet supported by metadata. (raid level change)");
			return -EINVAL;
		} else if (rs_reshape_requested(rs)) {
			DMERR("Reshaping raid sets not yet supported by metadata. (raid layout change keeping level)");
			if (mddev->layout != mddev->new_layout) {
				if (rt_cur && rt_new)
					DMERR("	 current layout %s vs new layout %s",
					      rt_cur->name, rt_new->name);
				else
					DMERR("	 current layout 0x%X vs new layout 0x%X",
					      le32_to_cpu(sb->layout), mddev->new_layout);
			}
			if (mddev->chunk_sectors != mddev->new_chunk_sectors)
				DMERR("	 current stripe sectors %u vs new stripe sectors %u",
				      mddev->chunk_sectors, mddev->new_chunk_sectors);
			if (rs->delta_disks)
				DMERR("	 current %u disks vs new %u disks",
				      mddev->raid_disks, mddev->raid_disks + rs->delta_disks);
			if (rs_is_raid10(rs)) {
				DMERR("	 Old layout: %s w/ %u copies",
				      raid10_md_layout_to_format(mddev->layout),
				      raid10_md_layout_to_copies(mddev->layout));
				DMERR("	 New layout: %s w/ %u copies",
				      raid10_md_layout_to_format(mddev->new_layout),
				      raid10_md_layout_to_copies(mddev->new_layout));
			}
2329 2330 2331
			return -EINVAL;
		}

2332
		DMINFO("Discovered old metadata format; upgrading to extended metadata format");
2333 2334
	}

2335
	if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
2336 2337 2338 2339 2340
		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:
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 2346 2347
	 *    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.
2348 2349 2350 2351
	 * 3) This is/are a new device(s) being added to an old
	 *    raid set during takeover to a higher raid level
	 *    to provide capacity for redundancy or during reshape
	 *    to add capacity to grow the raid set.
2352
	 */
2353
	d = 0;
N
NeilBrown 已提交
2354
	rdev_for_each(r, mddev) {
2355 2356 2357
		if (test_bit(Journal, &rdev->flags))
			continue;

2358 2359 2360
		if (test_bit(FirstUse, &r->flags))
			new_devs++;

2361
		if (!test_bit(In_sync, &r->flags)) {
2362 2363
			DMINFO("Device %d specified for rebuild; clearing superblock",
				r->raid_disk);
2364
			rebuilds++;
2365 2366 2367 2368 2369 2370

			if (test_bit(FirstUse, &r->flags))
				rebuild_and_new++;
		}

		d++;
2371 2372
	}

2373 2374 2375 2376 2377 2378
	if (new_devs == rs->raid_disks || !rebuilds) {
		/* Replace a broken device */
		if (new_devs == 1 && !rs->delta_disks)
			;
		if (new_devs == rs->raid_disks) {
			DMINFO("Superblocks created for new raid set");
2379
			set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
2380 2381
		} else if (new_devs != rebuilds &&
			   new_devs != rs->delta_disks) {
2382 2383
			DMERR("New device injected into existing raid set without "
			      "'delta_disks' or 'rebuild' parameter specified");
2384 2385
			return -EINVAL;
		}
2386 2387 2388 2389
	} else if (new_devs && new_devs != rebuilds) {
		DMERR("%u 'rebuild' devices cannot be injected into"
		      " a raid set with %u other first-time devices",
		      rebuilds, new_devs);
2390
		return -EINVAL;
2391 2392 2393 2394 2395
	} else if (rebuilds) {
		if (rebuild_and_new && rebuilds != rebuild_and_new) {
			DMERR("new device%s provided without 'rebuild'",
			      new_devs > 1 ? "s" : "");
			return -EINVAL;
2396
		} else if (!test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags) && rs_is_recovering(rs)) {
2397 2398 2399
			DMERR("'rebuild' specified while raid set is not in-sync (recovery_cp=%llu)",
			      (unsigned long long) mddev->recovery_cp);
			return -EINVAL;
2400 2401 2402
		} else if (rs_is_reshaping(rs)) {
			DMERR("'rebuild' specified while raid set is being reshaped (reshape_position=%llu)",
			      (unsigned long long) mddev->reshape_position);
2403 2404
			return -EINVAL;
		}
2405 2406 2407 2408 2409 2410
	}

	/*
	 * Now we set the Faulty bit for those devices that are
	 * recorded in the superblock as failed.
	 */
2411
	sb_retrieve_failed_devices(sb, failed_devices);
N
NeilBrown 已提交
2412
	rdev_for_each(r, mddev) {
2413 2414
		if (test_bit(Journal, &rdev->flags) ||
		    !r->sb_page)
2415 2416 2417
			continue;
		sb2 = page_address(r->sb_page);
		sb2->failed_devices = 0;
2418
		memset(sb2->extended_failed_devices, 0, sizeof(sb2->extended_failed_devices));
2419 2420 2421 2422 2423 2424

		/*
		 * Check for any device re-ordering.
		 */
		if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
			role = le32_to_cpu(sb2->array_position);
2425 2426 2427
			if (role < 0)
				continue;

2428
			if (role != r->raid_disk) {
2429
				if (rs_is_raid10(rs) && __is_raid10_near(mddev->layout)) {
M
Mike Snitzer 已提交
2430
					if (mddev->raid_disks % __raid10_near_copies(mddev->layout) ||
2431 2432 2433 2434 2435
					    rs->raid_disks % rs->raid10_copies) {
						rs->ti->error =
							"Cannot change raid10 near set to odd # of devices!";
						return -EINVAL;
					}
2436 2437 2438 2439

					sb2->array_position = cpu_to_le32(r->raid_disk);

				} else if (!(rs_is_raid10(rs) && rt_is_raid0(rs->raid_type)) &&
2440 2441 2442 2443 2444
					   !(rs_is_raid0(rs) && rt_is_raid10(rs->raid_type)) &&
					   !rt_is_raid1(rs->raid_type)) {
					rs->ti->error = "Cannot change device positions in raid set";
					return -EINVAL;
				}
2445

2446
				DMINFO("raid device #%d now at position #%d", role, r->raid_disk);
2447 2448 2449 2450 2451 2452
			}

			/*
			 * Partial recovery is performed on
			 * returning failed devices.
			 */
2453
			if (test_bit(role, (void *) failed_devices))
2454 2455 2456 2457 2458 2459 2460
				set_bit(Faulty, &r->flags);
		}
	}

	return 0;
}

2461
static int super_validate(struct raid_set *rs, struct md_rdev *rdev)
2462
{
2463
	struct mddev *mddev = &rs->md;
2464 2465
	struct dm_raid_superblock *sb;

2466
	if (rs_is_raid0(rs) || !rdev->sb_page || rdev->raid_disk < 0)
2467 2468 2469
		return 0;

	sb = page_address(rdev->sb_page);
2470 2471 2472 2473 2474

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

2478 2479
	if (le32_to_cpu(sb->compat_features) &&
	    le32_to_cpu(sb->compat_features) != FEATURE_FLAG_SUPPORTS_V190) {
2480 2481 2482 2483 2484
		rs->ti->error = "Unable to assemble array: Unknown flag(s) in compatible feature flags";
		return -EINVAL;
	}

	if (sb->incompat_features) {
2485
		rs->ti->error = "Unable to assemble array: No incompatible feature flags supported yet";
2486 2487 2488
		return -EINVAL;
	}

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 2496 2497 2498 2499 2500 2501
		/*
		 * Retrieve rdev size stored in superblock to be prepared for shrink.
		 * Check extended superblock members are present otherwise the size
		 * will not be set!
		 */
		if (le32_to_cpu(sb->compat_features) & FEATURE_FLAG_SUPPORTS_V190)
			rdev->sectors = le64_to_cpu(sb->sectors);

2502
		rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
2503 2504 2505 2506 2507 2508
		if (rdev->recovery_offset == MaxSector)
			set_bit(In_sync, &rdev->flags);
		/*
		 * If no reshape in progress -> we're recovering single
		 * disk(s) and have to set the device(s) to out-of-sync
		 */
2509
		else if (!rs_is_reshaping(rs))
2510
			clear_bit(In_sync, &rdev->flags); /* Mandatory for recovery */
2511 2512 2513 2514 2515
	}

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

2522 2523 2524
	/* Reshape support -> restore repective data offsets */
	rdev->data_offset = le64_to_cpu(sb->data_offset);
	rdev->new_data_offset = le64_to_cpu(sb->new_data_offset);
2525 2526 2527 2528 2529 2530 2531 2532 2533

	return 0;
}

/*
 * Analyse superblocks and select the freshest.
 */
static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
{
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 2541 2542
		if (test_bit(Journal, &rdev->flags))
			continue;

2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
		if (!rdev->meta_bdev)
			continue;

		/* Set superblock offset/size for metadata device. */
		rdev->sb_start = 0;
		rdev->sb_size = bdev_logical_block_size(rdev->meta_bdev);
		if (rdev->sb_size < sizeof(struct dm_raid_superblock) || rdev->sb_size > PAGE_SIZE) {
			DMERR("superblock size of a logical block is no longer valid");
			return -EINVAL;
		}

2554
		/*
H
Heinz Mauelshagen 已提交
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 2559
		 * of the "sync" directive.
		 *
2560 2561
		 * With reshaping capability added, we must ensure that
		 * that the "sync" directive is disallowed during the reshape.
2562
		 */
2563
		if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
2564 2565
			continue;

2566
		r = super_load(rdev, freshest);
2567

2568
		switch (r) {
2569 2570 2571 2572 2573 2574
		case 1:
			freshest = rdev;
			break;
		case 0:
			break;
		default:
2575
			/* This is a failure to read the superblock from the metadata device. */
2576 2577 2578 2579 2580 2581 2582
			/*
			 * We have to keep any raid0 data/metadata device pairs or
			 * the MD raid0 personality will fail to start the array.
			 */
			if (rs_is_raid0(rs))
				continue;

2583
			/*
2584 2585 2586 2587 2588 2589
			 * We keep the dm_devs to be able to emit the device tuple
			 * properly on the table line in raid_status() (rather than
			 * mistakenly acting as if '- -' got passed into the constructor).
			 *
			 * The rdev has to stay on the same_set list to allow for
			 * the attempt to restore faulty devices on second resume.
2590
			 */
2591 2592
			rdev->raid_disk = rdev->saved_raid_disk = -1;
			break;
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
		}
	}

	if (!freshest)
		return 0;

	/*
	 * Validation of the freshest device provides the source of
	 * validation for the remaining devices.
	 */
2603 2604
	rs->ti->error = "Unable to assemble array: Invalid superblocks";
	if (super_validate(rs, freshest))
2605
		return -EINVAL;
2606

2607 2608 2609 2610 2611
	if (validate_raid_redundancy(rs)) {
		rs->ti->error = "Insufficient redundancy to activate array";
		return -EINVAL;
	}

N
NeilBrown 已提交
2612
	rdev_for_each(rdev, mddev)
2613 2614 2615
		if (!test_bit(Journal, &rdev->flags) &&
		    rdev != freshest &&
		    super_validate(rs, rdev))
2616 2617 2618 2619
			return -EINVAL;
	return 0;
}

2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
/*
 * Adjust data_offset and new_data_offset on all disk members of @rs
 * for out of place reshaping if requested by contructor
 *
 * We need free space at the beginning of each raid disk for forward
 * and at the end for backward reshapes which userspace has to provide
 * via remapping/reordering of space.
 */
static int rs_adjust_data_offsets(struct raid_set *rs)
{
	sector_t data_offset = 0, new_data_offset = 0;
	struct md_rdev *rdev;

	/* Constructor did not request data offset change */
	if (!test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags)) {
		if (!rs_is_reshapable(rs))
			goto out;

		return 0;
	}

	/* HM FIXME: get InSync raid_dev? */
	rdev = &rs->dev[0].rdev;

	if (rs->delta_disks < 0) {
		/*
		 * Removing disks (reshaping backwards):
		 *
		 * - before reshape: data is at offset 0 and free space
		 *		     is at end of each component LV
		 *
		 * - after reshape: data is at offset rs->data_offset != 0 on each component LV
		 */
		data_offset = 0;
		new_data_offset = rs->data_offset;

	} else if (rs->delta_disks > 0) {
		/*
		 * Adding disks (reshaping forwards):
		 *
		 * - before reshape: data is at offset rs->data_offset != 0 and
		 *		     free space is at begin of each component LV
		 *
		 * - after reshape: data is at offset 0 on each component LV
		 */
		data_offset = rs->data_offset;
		new_data_offset = 0;

	} else {
		/*
		 * User space passes in 0 for data offset after having removed reshape space
		 *
		 * - or - (data offset != 0)
		 *
		 * Changing RAID layout or chunk size -> toggle offsets
		 *
		 * - before reshape: data is at offset rs->data_offset 0 and
		 *		     free space is at end of each component LV
		 *		     -or-
		 *                   data is at offset rs->data_offset != 0 and
		 *		     free space is at begin of each component LV
		 *
2682 2683
		 * - after reshape: data is at offset 0 if it was at offset != 0
		 *                  or at offset != 0 if it was at offset 0
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695
		 *                  on each component LV
		 *
		 */
		data_offset = rs->data_offset ? rdev->data_offset : 0;
		new_data_offset = data_offset ? 0 : rs->data_offset;
		set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
	}

	/*
	 * Make sure we got a minimum amount of free sectors per device
	 */
	if (rs->data_offset &&
2696
	    to_sector(i_size_read(rdev->bdev->bd_inode)) - rs->md.dev_sectors < MIN_FREE_RESHAPE_SPACE) {
2697 2698 2699 2700 2701
		rs->ti->error = data_offset ? "No space for forward reshape" :
					      "No space for backward reshape";
		return -ENOSPC;
	}
out:
2702 2703 2704 2705 2706 2707 2708
	/*
	 * Raise recovery_cp in case data_offset != 0 to
	 * avoid false recovery positives in the constructor.
	 */
	if (rs->md.recovery_cp < rs->md.dev_sectors)
		rs->md.recovery_cp += rs->dev[0].rdev.data_offset;

2709
	/* Adjust data offsets on all rdevs but on any raid4/5/6 journal device */
2710
	rdev_for_each(rdev, &rs->md) {
2711 2712 2713 2714
		if (!test_bit(Journal, &rdev->flags)) {
			rdev->data_offset = data_offset;
			rdev->new_data_offset = new_data_offset;
		}
2715 2716 2717 2718 2719
	}

	return 0;
}

2720
/* Userpace reordered disks -> adjust raid_disk indexes in @rs */
M
Mike Snitzer 已提交
2721
static void __reorder_raid_disk_indexes(struct raid_set *rs)
2722 2723 2724 2725 2726
{
	int i = 0;
	struct md_rdev *rdev;

	rdev_for_each(rdev, &rs->md) {
2727 2728 2729 2730
		if (!test_bit(Journal, &rdev->flags)) {
			rdev->raid_disk = i++;
			rdev->saved_raid_disk = rdev->new_raid_disk = -1;
		}
2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
	}
}

/*
 * Setup @rs for takeover by a different raid level
 */
static int rs_setup_takeover(struct raid_set *rs)
{
	struct mddev *mddev = &rs->md;
	struct md_rdev *rdev;
	unsigned int d = mddev->raid_disks = rs->raid_disks;
	sector_t new_data_offset = rs->dev[0].rdev.data_offset ? 0 : rs->data_offset;

	if (rt_is_raid10(rs->raid_type)) {
H
Heinz Mauelshagen 已提交
2745
		if (rs_is_raid0(rs)) {
2746
			/* Userpace reordered disks -> adjust raid_disk indexes */
M
Mike Snitzer 已提交
2747
			__reorder_raid_disk_indexes(rs);
2748 2749 2750 2751

			/* raid0 -> raid10_far layout */
			mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_FAR,
								   rs->raid10_copies);
H
Heinz Mauelshagen 已提交
2752
		} else if (rs_is_raid1(rs))
2753 2754 2755
			/* raid1 -> raid10_near layout */
			mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
								   rs->raid_disks);
2756
		else
2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780
			return -EINVAL;

	}

	clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
	mddev->recovery_cp = MaxSector;

	while (d--) {
		rdev = &rs->dev[d].rdev;

		if (test_bit(d, (void *) rs->rebuild_disks)) {
			clear_bit(In_sync, &rdev->flags);
			clear_bit(Faulty, &rdev->flags);
			mddev->recovery_cp = rdev->recovery_offset = 0;
			/* Bitmap has to be created when we do an "up" takeover */
			set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
		}

		rdev->new_data_offset = new_data_offset;
	}

	return 0;
}

2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
/* Prepare @rs for reshape */
static int rs_prepare_reshape(struct raid_set *rs)
{
	bool reshape;
	struct mddev *mddev = &rs->md;

	if (rs_is_raid10(rs)) {
		if (rs->raid_disks != mddev->raid_disks &&
		    __is_raid10_near(mddev->layout) &&
		    rs->raid10_copies &&
		    rs->raid10_copies != __raid10_near_copies(mddev->layout)) {
			/*
			 * raid disk have to be multiple of data copies to allow this conversion,
			 *
			 * This is actually not a reshape it is a
			 * rebuild of any additional mirrors per group
			 */
			if (rs->raid_disks % rs->raid10_copies) {
				rs->ti->error = "Can't reshape raid10 mirror groups";
				return -EINVAL;
			}

			/* Userpace reordered disks to add/remove mirrors -> adjust raid_disk indexes */
			__reorder_raid_disk_indexes(rs);
			mddev->layout = raid10_format_to_md_layout(rs, ALGORITHM_RAID10_NEAR,
								   rs->raid10_copies);
			mddev->new_layout = mddev->layout;
			reshape = false;
		} else
			reshape = true;

	} else if (rs_is_raid456(rs))
		reshape = true;

	else if (rs_is_raid1(rs)) {
2816 2817 2818 2819 2820 2821 2822 2823 2824
		if (rs->delta_disks) {
			/* Process raid1 via delta_disks */
			mddev->degraded = rs->delta_disks < 0 ? -rs->delta_disks : rs->delta_disks;
			reshape = true;
		} else {
			/* Process raid1 without delta_disks */
			mddev->raid_disks = rs->raid_disks;
			reshape = false;
		}
2825 2826 2827 2828 2829 2830 2831 2832
	} else {
		rs->ti->error = "Called with bogus raid type";
		return -EINVAL;
	}

	if (reshape) {
		set_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags);
		set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
2833 2834
	} else if (mddev->raid_disks < rs->raid_disks)
		/* Create new superblocks and bitmaps, if any new disks */
2835 2836 2837 2838 2839
		set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);

	return 0;
}

2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
/* Get reshape sectors from data_offsets or raid set */
static sector_t _get_reshape_sectors(struct raid_set *rs)
{
	struct md_rdev *rdev;
	sector_t reshape_sectors = 0;

	rdev_for_each(rdev, &rs->md)
		if (!test_bit(Journal, &rdev->flags)) {
			reshape_sectors = (rdev->data_offset > rdev->new_data_offset) ?
					rdev->data_offset - rdev->new_data_offset :
					rdev->new_data_offset - rdev->data_offset;
			break;
		}

	return max(reshape_sectors, (sector_t) rs->data_offset);
}

2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867
/*
 *
 * - change raid layout
 * - change chunk size
 * - add disks
 * - remove disks
 */
static int rs_setup_reshape(struct raid_set *rs)
{
	int r = 0;
	unsigned int cur_raid_devs, d;
2868
	sector_t reshape_sectors = _get_reshape_sectors(rs);
2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884
	struct mddev *mddev = &rs->md;
	struct md_rdev *rdev;

	mddev->delta_disks = rs->delta_disks;
	cur_raid_devs = mddev->raid_disks;

	/* Ignore impossible layout change whilst adding/removing disks */
	if (mddev->delta_disks &&
	    mddev->layout != mddev->new_layout) {
		DMINFO("Ignoring invalid layout change with delta_disks=%d", rs->delta_disks);
		mddev->new_layout = mddev->layout;
	}

	/*
	 * Adjust array size:
	 *
2885
	 * - in case of adding disk(s), array size has
2886 2887 2888 2889 2890
	 *   to grow after the disk adding reshape,
	 *   which'll hapen in the event handler;
	 *   reshape will happen forward, so space has to
	 *   be available at the beginning of each disk
	 *
2891
	 * - in case of removing disk(s), array size
2892 2893 2894 2895 2896 2897
	 *   has to shrink before starting the reshape,
	 *   which'll happen here;
	 *   reshape will happen backward, so space has to
	 *   be available at the end of each disk
	 *
	 * - data_offset and new_data_offset are
2898
	 *   adjusted for aforementioned out of place
2899 2900
	 *   reshaping based on userspace passing in
	 *   the "data_offset <sectors>" key/value
2901
	 *   pair via the constructor
2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
	 */

	/* Add disk(s) */
	if (rs->delta_disks > 0) {
		/* Prepare disks for check in raid4/5/6/10 {check|start}_reshape */
		for (d = cur_raid_devs; d < rs->raid_disks; d++) {
			rdev = &rs->dev[d].rdev;
			clear_bit(In_sync, &rdev->flags);

			/*
			 * save_raid_disk needs to be -1, or recovery_offset will be set to 0
			 * by md, which'll store that erroneously in the superblock on reshape
			 */
			rdev->saved_raid_disk = -1;
			rdev->raid_disk = d;

			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 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954

	/* Remove disk(s) */
	} else if (rs->delta_disks < 0) {
		r = rs_set_dev_and_array_sectors(rs, true);
		mddev->reshape_backwards = 1; /* removing disk(s) -> backward reshape */

	/* Change layout and/or chunk size */
	} else {
		/*
		 * Reshape layout (e.g. raid5_ls -> raid5_n) and/or chunk size:
		 *
		 * keeping number of disks and do layout change ->
		 *
		 * toggle reshape_backward depending on data_offset:
		 *
		 * - free space upfront -> reshape forward
		 *
		 * - free space at the end -> reshape backward
		 *
		 *
		 * This utilizes free reshape space avoiding the need
		 * for userspace to move (parts of) LV segments in
		 * case of layout/chunksize change  (for disk
		 * adding/removing reshape space has to be at
		 * the proper address (see above with delta_disks):
		 *
		 * add disk(s)   -> begin
		 * remove disk(s)-> end
		 */
		mddev->reshape_backwards = rs->dev[0].rdev.data_offset ? 0 : 1;
	}

2955 2956 2957 2958 2959 2960 2961 2962 2963
	/*
	 * Adjust device size for forward reshape
	 * because md_finish_reshape() reduces it.
	 */
	if (!mddev->reshape_backwards)
		rdev_for_each(rdev, &rs->md)
			if (!test_bit(Journal, &rdev->flags))
				rdev->sectors += reshape_sectors;

2964 2965 2966
	return r;
}

2967
/*
2968 2969
 * Enable/disable discard support on RAID set depending on
 * RAID level and discard properties of underlying RAID members.
2970
 */
2971
static void configure_discard_support(struct raid_set *rs)
2972
{
2973 2974
	int i;
	bool raid456;
2975
	struct dm_target *ti = rs->ti;
2976

2977 2978 2979
	/*
	 * XXX: RAID level 4,5,6 require zeroing for safety.
	 */
H
Heinz Mauelshagen 已提交
2980
	raid456 = rs_is_raid456(rs);
2981

2982
	for (i = 0; i < rs->raid_disks; i++) {
2983
		struct request_queue *q;
2984

2985 2986 2987 2988
		if (!rs->dev[i].rdev.bdev)
			continue;

		q = bdev_get_queue(rs->dev[i].rdev.bdev);
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
		if (!q || !blk_queue_discard(q))
			return;

		if (raid456) {
			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;
			}
		}
	}

3001 3002
	/*
	 * RAID1 and RAID10 personalities require bio splitting,
3003
	 * RAID0/4/5/6 don't and process large discard bios properly.
3004
	 */
H
Heinz Mauelshagen 已提交
3005
	ti->split_discard_bios = !!(rs_is_raid1(rs) || rs_is_raid10(rs));
3006 3007 3008
	ti->num_discard_bios = 1;
}

N
NeilBrown 已提交
3009
/*
3010
 * Construct a RAID0/1/10/4/5/6 mapping:
N
NeilBrown 已提交
3011
 * Args:
3012 3013
 *	<raid_type> <#raid_params> <raid_params>{0,}	\
 *	<#raid_devs> [<meta_dev1> <dev1>]{1,}
N
NeilBrown 已提交
3014
 *
3015
 * <raid_params> varies by <raid_type>.	 See 'parse_raid_params' for
N
NeilBrown 已提交
3016
 * details on possible <raid_params>.
3017 3018 3019 3020
 *
 * Userspace is free to initialize the metadata devices, hence the superblocks to
 * enforce recreation based on the passed in table parameters.
 *
N
NeilBrown 已提交
3021
 */
3022
static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv)
N
NeilBrown 已提交
3023
{
3024
	int r;
3025
	bool resize = false;
N
NeilBrown 已提交
3026
	struct raid_type *rt;
3027
	unsigned int num_raid_params, num_raid_devs;
3028
	sector_t calculated_dev_sectors, rdev_sectors, reshape_sectors;
N
NeilBrown 已提交
3029
	struct raid_set *rs = NULL;
3030
	const char *arg;
3031
	struct rs_layout rs_layout;
3032 3033 3034 3035 3036 3037 3038 3039
	struct dm_arg_set as = { argc, argv }, as_nrd;
	struct dm_arg _args[] = {
		{ 0, as.argc, "Cannot understand number of raid parameters" },
		{ 1, 254, "Cannot understand number of raid devices parameters" }
	};

	/* Must have <raid_type> */
	arg = dm_shift_arg(&as);
3040 3041 3042 3043
	if (!arg) {
		ti->error = "No arguments";
		return -EINVAL;
	}
N
NeilBrown 已提交
3044

3045
	rt = get_raid_type(arg);
3046 3047 3048 3049
	if (!rt) {
		ti->error = "Unrecognised raid_type";
		return -EINVAL;
	}
N
NeilBrown 已提交
3050

3051 3052
	/* Must have <#raid_params> */
	if (dm_read_arg_group(_args, &as, &num_raid_params, &ti->error))
3053
		return -EINVAL;
N
NeilBrown 已提交
3054

3055 3056 3057 3058 3059
	/* number of raid device tupples <meta_dev data_dev> */
	as_nrd = as;
	dm_consume_args(&as_nrd, num_raid_params);
	_args[1].max = (as_nrd.argc - 1) / 2;
	if (dm_read_arg(_args + 1, &as_nrd, &num_raid_devs, &ti->error))
3060
		return -EINVAL;
N
NeilBrown 已提交
3061

3062
	if (!__within_range(num_raid_devs, 1, MAX_RAID_DEVICES)) {
3063 3064 3065
		ti->error = "Invalid number of supplied raid devices";
		return -EINVAL;
	}
3066

3067
	rs = raid_set_alloc(ti, rt, num_raid_devs);
N
NeilBrown 已提交
3068 3069 3070
	if (IS_ERR(rs))
		return PTR_ERR(rs);

3071
	r = parse_raid_params(rs, &as, num_raid_params);
3072
	if (r)
N
NeilBrown 已提交
3073 3074
		goto bad;

3075
	r = parse_dev_params(rs, &as);
3076
	if (r)
N
NeilBrown 已提交
3077 3078
		goto bad;

3079
	rs->md.sync_super = super_sync;
3080

3081 3082 3083 3084 3085 3086
	/*
	 * Calculate ctr requested array and device sizes to allow
	 * for superblock analysis needing device sizes defined.
	 *
	 * Any existing superblock will overwrite the array and device sizes
	 */
3087 3088
	r = rs_set_dev_and_array_sectors(rs, false);
	if (r)
3089
		goto bad;
3090

3091
	calculated_dev_sectors = rs->md.dev_sectors;
3092

3093 3094 3095 3096 3097
	/*
	 * Backup any new raid set level, layout, ...
	 * requested to be able to compare to superblock
	 * members for conversion decisions.
	 */
3098
	rs_config_backup(rs, &rs_layout);
3099

3100 3101
	r = analyse_superblocks(ti, rs);
	if (r)
3102 3103
		goto bad;

3104 3105 3106 3107 3108 3109 3110
	rdev_sectors = __rdev_sectors(rs);
	if (!rdev_sectors) {
		ti->error = "Invalid rdev size";
		r = -EINVAL;
		goto bad;
	}

3111 3112 3113 3114

	reshape_sectors = _get_reshape_sectors(rs);
	if (calculated_dev_sectors != rdev_sectors)
		resize = calculated_dev_sectors != (reshape_sectors ? rdev_sectors - reshape_sectors : rdev_sectors);
3115

N
NeilBrown 已提交
3116 3117
	INIT_WORK(&rs->md.event_work, do_table_event);
	ti->private = rs;
3118
	ti->num_flush_bios = 1;
N
NeilBrown 已提交
3119

3120
	/* Restore any requested new layout for conversion decision */
3121
	rs_config_restore(rs, &rs_layout);
3122

3123 3124 3125 3126 3127 3128
	/*
	 * Now that we have any superblock metadata available,
	 * check for new, recovering, reshaping, to be taken over,
	 * to be reshaped or an existing, unchanged raid set to
	 * run in sequence.
	 */
3129
	if (test_bit(MD_ARRAY_FIRST_USE, &rs->md.flags)) {
3130 3131 3132 3133
		/* A new raid6 set has to be recovered to ensure proper parity and Q-Syndrome */
		if (rs_is_raid6(rs) &&
		    test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags)) {
			ti->error = "'nosync' not allowed for new raid6 set";
3134 3135
			r = -EINVAL;
			goto bad;
3136 3137
		}
		rs_setup_recovery(rs, 0);
3138 3139 3140
		set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
		rs_set_new(rs);
	} else if (rs_is_recovering(rs)) {
3141
		/* A recovering raid set may be resized */
3142 3143 3144 3145 3146
		; /* skip setup rs */
	} else if (rs_is_reshaping(rs)) {
		/* Have to reject size change request during reshape */
		if (resize) {
			ti->error = "Can't resize a reshaping raid set";
3147 3148
			r = -EPERM;
			goto bad;
3149
		}
3150
		/* skip setup rs */
3151
	} else if (rs_takeover_requested(rs)) {
3152 3153
		if (rs_is_reshaping(rs)) {
			ti->error = "Can't takeover a reshaping raid set";
3154 3155
			r = -EPERM;
			goto bad;
3156 3157
		}

3158 3159 3160 3161 3162 3163 3164
		/* We can't takeover a journaled raid4/5/6 */
		if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
			ti->error = "Can't takeover a journaled raid4/5/6 set";
			r = -EPERM;
			goto bad;
		}

3165
		/*
3166
		 * If a takeover is needed, userspace sets any additional
3167 3168 3169
		 * devices to rebuild and we can check for a valid request here.
		 *
		 * If acceptible, set the level to the new requested
3170 3171
		 * one, prohibit requesting recovery, allow the raid
		 * set to run and store superblocks during resume.
3172
		 */
3173 3174
		r = rs_check_takeover(rs);
		if (r)
3175
			goto bad;
3176 3177 3178

		r = rs_setup_takeover(rs);
		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 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197
		/*
		 * No need to check for 'ongoing' takeover here, because takeover
		 * is an instant operation as oposed to an ongoing reshape.
		 */

		/* We can't reshape a journaled raid4/5/6 */
		if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags)) {
			ti->error = "Can't reshape a journaled raid4/5/6 set";
			r = -EPERM;
			goto bad;
		}

3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209
		/* Out-of-place space has to be available to allow for a reshape unless raid1! */
		if (reshape_sectors || rs_is_raid1(rs)) {
			/*
			  * We can only prepare for a reshape here, because the
			  * raid set needs to run to provide the repective reshape
			  * check functions via its MD personality instance.
			  *
			  * So do the reshape check after md_run() succeeded.
			  */
			r = rs_prepare_reshape(rs);
			if (r)
				return r;
3210

3211 3212 3213
			/* Reshaping ain't recovery, so disable recovery */
			rs_setup_recovery(rs, MaxSector);
		}
3214
		rs_set_cur(rs);
3215 3216
	} else {
		/* May not set recovery when a device rebuild is requested */
3217 3218 3219 3220 3221
		if (test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) {
			rs_setup_recovery(rs, MaxSector);
			set_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags);
		} else
			rs_setup_recovery(rs, test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags) ?
3222 3223
					      0 : (resize ? calculated_dev_sectors : MaxSector));
		rs_set_cur(rs);
3224
	}
3225

3226 3227 3228
	/* If constructor requested it, change data and new_data offsets */
	r = rs_adjust_data_offsets(rs);
	if (r)
3229
		goto bad;
3230

3231 3232 3233 3234
	/* Start raid set read-only and assumed clean to change in raid_resume() */
	rs->md.ro = 1;
	rs->md.in_sync = 1;
	set_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
3235

3236 3237
	/* Has to be held on running the array */
	mddev_lock_nointr(&rs->md);
3238
	r = md_run(&rs->md);
N
NeilBrown 已提交
3239
	rs->md.in_sync = 0; /* Assume already marked dirty */
3240
	if (r) {
3241 3242
		ti->error = "Failed to run raid array";
		mddev_unlock(&rs->md);
N
NeilBrown 已提交
3243 3244 3245
		goto bad;
	}

3246 3247 3248 3249 3250 3251 3252 3253
	r = md_start(&rs->md);

	if (r) {
		ti->error = "Failed to start raid array";
		mddev_unlock(&rs->md);
		goto bad_md_start;
	}

N
NeilBrown 已提交
3254 3255 3256
	rs->callbacks.congested_fn = raid_is_congested;
	dm_table_add_target_callbacks(ti->table, &rs->callbacks);

3257 3258 3259 3260 3261 3262 3263 3264 3265 3266
	/* If raid4/5/6 journal mode explictely requested (only possible with journal dev) -> set it */
	if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags)) {
		r = r5c_journal_mode_set(&rs->md, rs->journal_dev.mode);
		if (r) {
			ti->error = "Failed to set raid4/5/6 journal mode";
			mddev_unlock(&rs->md);
			goto bad_journal_mode_set;
		}
	}

J
Jonathan Brassow 已提交
3267
	mddev_suspend(&rs->md);
3268
	set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags);
3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280

	/* Try to adjust the raid4/5/6 stripe cache size to the stripe size */
	if (rs_is_raid456(rs)) {
		r = rs_set_raid456_stripe_cache(rs);
		if (r)
			goto bad_stripe_cache;
	}

	/* Now do an early reshape check */
	if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
		r = rs_check_reshape(rs);
		if (r)
3281
			goto bad_check_reshape;
3282 3283 3284 3285

		/* Restore new, ctr requested layout to perform check */
		rs_config_restore(rs, &rs_layout);

3286 3287 3288 3289 3290 3291
		if (rs->md.pers->start_reshape) {
			r = rs->md.pers->check_reshape(&rs->md);
			if (r) {
				ti->error = "Reshape check failed";
				goto bad_check_reshape;
			}
3292 3293 3294
		}
	}

3295 3296 3297
	/* Disable/enable discard support on raid set. */
	configure_discard_support(rs);

3298
	mddev_unlock(&rs->md);
N
NeilBrown 已提交
3299 3300
	return 0;

3301
bad_md_start:
3302
bad_journal_mode_set:
3303 3304
bad_stripe_cache:
bad_check_reshape:
3305
	md_stop(&rs->md);
N
NeilBrown 已提交
3306
bad:
3307
	raid_set_free(rs);
N
NeilBrown 已提交
3308

3309
	return r;
N
NeilBrown 已提交
3310 3311 3312 3313 3314 3315 3316 3317
}

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

	list_del_init(&rs->callbacks.list);
	md_stop(&rs->md);
3318
	raid_set_free(rs);
N
NeilBrown 已提交
3319 3320
}

M
Mikulas Patocka 已提交
3321
static int raid_map(struct dm_target *ti, struct bio *bio)
N
NeilBrown 已提交
3322 3323
{
	struct raid_set *rs = ti->private;
3324
	struct mddev *mddev = &rs->md;
N
NeilBrown 已提交
3325

3326 3327 3328 3329 3330
	/*
	 * If we're reshaping to add disk(s)), ti->len and
	 * mddev->array_sectors will differ during the process
	 * (ti->len > mddev->array_sectors), so we have to requeue
	 * bios with addresses > mddev->array_sectors here or
3331
	 * there will occur accesses past EOD of the component
3332 3333 3334 3335 3336
	 * data images thus erroring the raid set.
	 */
	if (unlikely(bio_end_sector(bio) > mddev->array_sectors))
		return DM_MAPIO_REQUEUE;

3337
	md_handle_request(mddev, bio);
N
NeilBrown 已提交
3338 3339 3340 3341

	return DM_MAPIO_SUBMITTED;
}

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 3347
		return "frozen";

3348 3349 3350 3351
	/* The MD sync thread can be done with io but still be running */
	if (!test_bit(MD_RECOVERY_DONE, &recovery) &&
	    (test_bit(MD_RECOVERY_RUNNING, &recovery) ||
	     (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery)))) {
3352
		if (test_bit(MD_RECOVERY_RESHAPE, &recovery))
3353 3354
			return "reshape";

3355 3356
		if (test_bit(MD_RECOVERY_SYNC, &recovery)) {
			if (!test_bit(MD_RECOVERY_REQUESTED, &recovery))
3357
				return "resync";
3358
			else if (test_bit(MD_RECOVERY_CHECK, &recovery))
3359 3360 3361 3362
				return "check";
			return "repair";
		}

3363
		if (test_bit(MD_RECOVERY_RECOVER, &recovery))
3364 3365 3366 3367 3368 3369
			return "recover";
	}

	return "idle";
}

3370
/*
3371
 * Return status string for @rdev
3372 3373 3374
 *
 * Status characters:
 *
3375
 *  'D' = Dead/Failed raid set component or raid4/5/6 journal device
3376 3377
 *  'a' = Alive but not in-sync raid set component _or_ alive raid4/5/6 'write_back' journal device
 *  '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)
N
NeilBrown 已提交
3381
{
3382 3383 3384
	if (!rdev->bdev)
		return "-";
	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 3389 3390
	else if (test_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags) ||
		 (!test_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags) &&
		  !test_bit(In_sync, &rdev->flags)))
3391 3392 3393 3394
		return "a";
	else
		return "A";
}
N
NeilBrown 已提交
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;
N
NeilBrown 已提交
3402

3403
	clear_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
3404
	clear_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);
3405 3406 3407

	if (rs_is_raid0(rs)) {
		r = resync_max_sectors;
3408
		set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
3409 3410

	} else {
H
Heinz Mauelshagen 已提交
3411 3412
		if (!test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags) &&
		    !test_bit(MD_RECOVERY_INTR, &recovery) &&
3413 3414 3415
		    (test_bit(MD_RECOVERY_NEEDED, &recovery) ||
		     test_bit(MD_RECOVERY_RESHAPE, &recovery) ||
		     test_bit(MD_RECOVERY_RUNNING, &recovery)))
3416
			r = mddev->curr_resync_completed;
3417
		else
3418
			r = mddev->recovery_cp;
3419

3420 3421 3422 3423 3424
		if (r >= resync_max_sectors &&
		    (!test_bit(MD_RECOVERY_REQUESTED, &recovery) ||
		     (!test_bit(MD_RECOVERY_FROZEN, &recovery) &&
		      !test_bit(MD_RECOVERY_NEEDED, &recovery) &&
		      !test_bit(MD_RECOVERY_RUNNING, &recovery)))) {
3425 3426 3427
			/*
			 * Sync complete.
			 */
3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447
			/* In case we have finished recovering, the array is in sync. */
			if (test_bit(MD_RECOVERY_RECOVER, &recovery))
				set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);

		} else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) {
			/*
			 * In case we are recovering, the array is not in sync
			 * and health chars should show the recovering legs.
			 */
			;

		} else if (test_bit(MD_RECOVERY_SYNC, &recovery) &&
			   !test_bit(MD_RECOVERY_REQUESTED, &recovery)) {
			/*
			 * If "resync" is occurring, the raid set
			 * is or may be out of sync hence the health
			 * characters shall be 'a'.
			 */
			set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);

3448 3449 3450 3451 3452 3453 3454 3455 3456
		} else if (test_bit(MD_RECOVERY_RESHAPE, &recovery) &&
			   !test_bit(MD_RECOVERY_REQUESTED, &recovery)) {
			/*
			 * If "reshape" is occurring, the raid set
			 * is or may be out of sync hence the health
			 * characters shall be 'a'.
			 */
			set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);

3457
		} else if (test_bit(MD_RECOVERY_REQUESTED, &recovery)) {
3458 3459 3460 3461 3462
			/*
			 * If "check" or "repair" is occurring, the raid set has
			 * undergone an initial sync and the health characters
			 * should not be 'a' anymore.
			 */
3463
			set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
3464

3465
		} else {
3466
			struct md_rdev *rdev;
3467

3468 3469 3470 3471 3472 3473 3474
			/*
			 * We are idle and recovery is needed, prevent 'A' chars race
			 * caused by components still set to in-sync by constrcuctor.
			 */
			if (test_bit(MD_RECOVERY_NEEDED, &recovery))
				set_bit(RT_FLAG_RS_RESYNCING, &rs->runtime_flags);

3475 3476
			/*
			 * The raid set may be doing an initial sync, or it may
3477
			 * be rebuilding individual components.	 If all the
3478 3479 3480
			 * devices are In_sync, then it is the raid set that is
			 * being initialized.
			 */
3481
			set_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
3482
			rdev_for_each(rdev, mddev)
3483
				if (!test_bit(Journal, &rdev->flags) &&
3484 3485 3486 3487
				    !test_bit(In_sync, &rdev->flags)) {
					clear_bit(RT_FLAG_RS_IN_SYNC, &rs->runtime_flags);
					break;
				}
3488
		}
3489 3490
	}

3491
	return min(r, resync_max_sectors);
3492 3493 3494
}

/* Helper to return @dev name or "-" if !@dev */
M
Mike Snitzer 已提交
3495
static const char *__get_dev_name(struct dm_dev *dev)
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505
{
	return dev ? dev->name : "-";
}

static void raid_status(struct dm_target *ti, status_type_t type,
			unsigned int status_flags, char *result, unsigned int maxlen)
{
	struct raid_set *rs = ti->private;
	struct mddev *mddev = &rs->md;
	struct r5conf *conf = mddev->private;
3506
	int i, max_nr_stripes = conf ? conf->max_nr_stripes : 0;
3507
	unsigned long recovery;
3508 3509
	unsigned int raid_param_cnt = 1; /* at least 1 for chunksize */
	unsigned int sz = 0;
3510
	unsigned int rebuild_disks;
3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522
	unsigned int write_mostly_params = 0;
	sector_t progress, resync_max_sectors, resync_mismatches;
	const char *sync_action;
	struct raid_type *rt;

	switch (type) {
	case STATUSTYPE_INFO:
		/* *Should* always succeed */
		rt = get_raid_type_by_ll(mddev->new_level, mddev->new_layout);
		if (!rt)
			return;

3523
		DMEMIT("%s %d ", rt->name, mddev->raid_disks);
3524 3525 3526

		/* Access most recent mddev properties for status output */
		smp_rmb();
3527
		recovery = rs->md.recovery;
3528
		/* Get sensible max sectors even if raid set not yet started */
3529
		resync_max_sectors = test_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags) ?
3530
				      mddev->resync_max_sectors : mddev->dev_sectors;
3531
		progress = rs_get_progress(rs, recovery, resync_max_sectors);
3532
		resync_mismatches = (mddev->last_sync_action && !strcasecmp(mddev->last_sync_action, "check")) ?
3533
				    atomic64_read(&mddev->resync_mismatches) : 0;
3534
		sync_action = decipher_sync_action(&rs->md, recovery);
3535

3536 3537
		/* HM FIXME: do we want another state char for raid0? It shows 'D'/'A'/'-' now */
		for (i = 0; i < rs->raid_disks; i++)
3538
			DMEMIT(__raid_dev_status(rs, &rs->dev[i].rdev));
N
NeilBrown 已提交
3539

3540
		/*
3541
		 * In-sync/Reshape ratio:
3542
		 *  The in-sync ratio shows the progress of:
3543 3544
		 *   - Initializing the raid set
		 *   - Rebuilding a subset of devices of the raid set
3545 3546
		 *  The user can distinguish between the two by referring
		 *  to the status characters.
3547 3548 3549 3550
		 *
		 *  The reshape ratio shows the progress of
		 *  changing the raid layout or the number of
		 *  disks of a raid set
3551
		 */
3552 3553
		DMEMIT(" %llu/%llu", (unsigned long long) progress,
				     (unsigned long long) resync_max_sectors);
N
NeilBrown 已提交
3554

3555
		/*
3556 3557
		 * v1.5.0+:
		 *
3558
		 * Sync action:
3559
		 *   See Documentation/device-mapper/dm-raid.txt for
3560 3561
		 *   information on each of these states.
		 */
3562
		DMEMIT(" %s", sync_action);
3563 3564

		/*
3565 3566
		 * v1.5.0+:
		 *
3567 3568
		 * resync_mismatches/mismatch_cnt
		 *   This field shows the number of discrepancies found when
3569
		 *   performing a "check" of the raid set.
3570
		 */
3571
		DMEMIT(" %llu", (unsigned long long) resync_mismatches);
N
NeilBrown 已提交
3572

3573
		/*
3574
		 * v1.9.0+:
3575 3576 3577 3578 3579 3580 3581 3582 3583
		 *
		 * data_offset (needed for out of space reshaping)
		 *   This field shows the data offset into the data
		 *   image LV where the first stripes data starts.
		 *
		 * We keep data_offset equal on all raid disks of the set,
		 * so retrieving it from the first raid disk is sufficient.
		 */
		DMEMIT(" %llu", (unsigned long long) rs->dev[0].rdev.data_offset);
3584 3585 3586 3587 3588

		/*
		 * v1.10.0+:
		 */
		DMEMIT(" %s", test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ?
3589
			      __raid_dev_status(rs, &rs->journal_dev.rdev) : "-");
3590
		break;
N
NeilBrown 已提交
3591

3592 3593 3594 3595
	case STATUSTYPE_TABLE:
		/* Report the table line string you would use to construct this raid set */

		/* Calculate raid parameter count */
3596 3597
		for (i = 0; i < rs->raid_disks; i++)
			if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
3598
				write_mostly_params += 2;
3599 3600
		rebuild_disks = memweight(rs->rebuild_disks, DISKS_ARRAY_ELEMS * sizeof(*rs->rebuild_disks));
		raid_param_cnt += rebuild_disks * 2 +
3601 3602
				  write_mostly_params +
				  hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_NO_ARGS) +
3603
				  hweight32(rs->ctr_flags & CTR_FLAG_OPTIONS_ONE_ARG) * 2 +
3604 3605
				  (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags) ? 2 : 0) +
				  (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags) ? 2 : 0);
3606

3607
		/* Emit table line */
3608
		/* This has to be in the documented order for userspace! */
3609
		DMEMIT("%s %u %u", rs->raid_type->name, raid_param_cnt, mddev->new_chunk_sectors);
3610
		if (test_bit(__CTR_FLAG_SYNC, &rs->ctr_flags))
3611
			DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_SYNC));
3612 3613
		if (test_bit(__CTR_FLAG_NOSYNC, &rs->ctr_flags))
			DMEMIT(" %s", dm_raid_arg_name_by_flag(CTR_FLAG_NOSYNC));
3614 3615 3616 3617 3618
		if (rebuild_disks)
			for (i = 0; i < rs->raid_disks; i++)
				if (test_bit(rs->dev[i].rdev.raid_disk, (void *) rs->rebuild_disks))
					DMEMIT(" %s %u", dm_raid_arg_name_by_flag(CTR_FLAG_REBUILD),
							 rs->dev[i].rdev.raid_disk);
3619 3620 3621 3622 3623 3624 3625 3626 3627
		if (test_bit(__CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
			DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_DAEMON_SLEEP),
					  mddev->bitmap_info.daemon_sleep);
		if (test_bit(__CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
			DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MIN_RECOVERY_RATE),
					 mddev->sync_speed_min);
		if (test_bit(__CTR_FLAG_MAX_RECOVERY_RATE, &rs->ctr_flags))
			DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_RECOVERY_RATE),
					 mddev->sync_speed_max);
3628 3629 3630 3631 3632
		if (write_mostly_params)
			for (i = 0; i < rs->raid_disks; i++)
				if (test_bit(WriteMostly, &rs->dev[i].rdev.flags))
					DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_WRITE_MOSTLY),
					       rs->dev[i].rdev.raid_disk);
3633
		if (test_bit(__CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
3634
			DMEMIT(" %s %lu", dm_raid_arg_name_by_flag(CTR_FLAG_MAX_WRITE_BEHIND),
3635
					  mddev->bitmap_info.max_write_behind);
3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653
		if (test_bit(__CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
			DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_STRIPE_CACHE),
					 max_nr_stripes);
		if (test_bit(__CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
			DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_REGION_SIZE),
					   (unsigned long long) to_sector(mddev->bitmap_info.chunksize));
		if (test_bit(__CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
			DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_COPIES),
					 raid10_md_layout_to_copies(mddev->layout));
		if (test_bit(__CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
			DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_RAID10_FORMAT),
					 raid10_md_layout_to_format(mddev->layout));
		if (test_bit(__CTR_FLAG_DELTA_DISKS, &rs->ctr_flags))
			DMEMIT(" %s %d", dm_raid_arg_name_by_flag(CTR_FLAG_DELTA_DISKS),
					 max(rs->delta_disks, mddev->delta_disks));
		if (test_bit(__CTR_FLAG_DATA_OFFSET, &rs->ctr_flags))
			DMEMIT(" %s %llu", dm_raid_arg_name_by_flag(CTR_FLAG_DATA_OFFSET),
					   (unsigned long long) rs->data_offset);
3654 3655 3656
		if (test_bit(__CTR_FLAG_JOURNAL_DEV, &rs->ctr_flags))
			DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_DEV),
					__get_dev_name(rs->journal_dev.dev));
3657 3658 3659
		if (test_bit(__CTR_FLAG_JOURNAL_MODE, &rs->ctr_flags))
			DMEMIT(" %s %s", dm_raid_arg_name_by_flag(CTR_FLAG_JOURNAL_MODE),
					 md_journal_mode_to_dm_raid(rs->journal_dev.mode));
3660
		DMEMIT(" %d", rs->raid_disks);
3661 3662 3663
		for (i = 0; i < rs->raid_disks; i++)
			DMEMIT(" %s %s", __get_dev_name(rs->dev[i].meta_dev),
					 __get_dev_name(rs->dev[i].data_dev));
N
NeilBrown 已提交
3664 3665 3666
	}
}

3667 3668
static int raid_message(struct dm_target *ti, unsigned int argc, char **argv,
			char *result, unsigned maxlen)
3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689
{
	struct raid_set *rs = ti->private;
	struct mddev *mddev = &rs->md;

	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"))
3690 3691
		; /* MD_RECOVERY_NEEDED set below */
	else if (!strcasecmp(argv[0], "recover"))
3692
		set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3693
	else {
3694
		if (!strcasecmp(argv[0], "check")) {
3695
			set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3696 3697 3698
			set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
			set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
		} else if (!strcasecmp(argv[0], "repair")) {
3699 3700 3701
			set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
			set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
		} else
3702 3703 3704 3705 3706 3707 3708
			return -EINVAL;
	}
	if (mddev->ro == 2) {
		/* A write to sync_action is enough to justify
		 * canceling read-auto mode
		 */
		mddev->ro = 0;
3709
		if (!mddev->suspended && mddev->sync_thread)
3710 3711 3712
			md_wakeup_thread(mddev->sync_thread);
	}
	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3713
	if (!mddev->suspended && mddev->thread)
3714 3715 3716 3717 3718 3719 3720
		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 已提交
3721 3722
{
	struct raid_set *rs = ti->private;
3723
	unsigned int i;
3724
	int r = 0;
N
NeilBrown 已提交
3725

3726
	for (i = 0; !r && i < rs->md.raid_disks; i++)
N
NeilBrown 已提交
3727
		if (rs->dev[i].data_dev)
3728
			r = fn(ti,
N
NeilBrown 已提交
3729 3730 3731 3732 3733
				 rs->dev[i].data_dev,
				 0, /* No offset on data devs */
				 rs->md.dev_sectors,
				 data);

3734
	return r;
N
NeilBrown 已提交
3735 3736 3737 3738 3739
}

static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
{
	struct raid_set *rs = ti->private;
3740
	unsigned int chunk_size = to_bytes(rs->md.chunk_sectors);
N
NeilBrown 已提交
3741 3742

	blk_limits_io_min(limits, chunk_size);
3743
	blk_limits_io_opt(limits, chunk_size * mddev_data_stripes(rs));
N
NeilBrown 已提交
3744 3745 3746 3747 3748 3749
}

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

3750
	if (!test_and_set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags)) {
3751 3752 3753 3754
		/* Writes have to be stopped before suspending to avoid deadlocks. */
		if (!test_bit(MD_RECOVERY_FROZEN, &rs->md.recovery))
			md_stop_writes(&rs->md);

3755
		mddev_lock_nointr(&rs->md);
3756
		mddev_suspend(&rs->md);
3757 3758
		mddev_unlock(&rs->md);
	}
N
NeilBrown 已提交
3759 3760
}

3761
static void attempt_restore_of_faulty_devices(struct raid_set *rs)
N
NeilBrown 已提交
3762
{
3763
	int i;
3764
	uint64_t cleared_failed_devices[DISKS_ARRAY_ELEMS];
3765
	unsigned long flags;
3766
	bool cleared = false;
3767
	struct dm_raid_superblock *sb;
3768
	struct mddev *mddev = &rs->md;
3769
	struct md_rdev *r;
N
NeilBrown 已提交
3770

3771 3772 3773 3774 3775 3776
	/* RAID personalities have to provide hot add/remove methods or we need to bail out. */
	if (!mddev->pers || !mddev->pers->hot_add_disk || !mddev->pers->hot_remove_disk)
		return;

	memset(cleared_failed_devices, 0, sizeof(cleared_failed_devices));

3777
	for (i = 0; i < mddev->raid_disks; i++) {
3778
		r = &rs->dev[i].rdev;
3779 3780 3781 3782
		/* HM FIXME: enhance journal device recovery processing */
		if (test_bit(Journal, &r->flags))
			continue;

3783 3784
		if (test_bit(Faulty, &r->flags) &&
		    r->meta_bdev && !read_disk_sb(r, r->sb_size, true)) {
3785 3786 3787
			DMINFO("Faulty %s device #%d has readable super block."
			       "  Attempting to revive it.",
			       rs->raid_type->name, i);
3788 3789 3790 3791 3792

			/*
			 * 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
3793
			 * 'hot_remove_disk').	If they haven't yet removed
3794 3795 3796 3797
			 * the failed device, its 'raid_disk' number will be
			 * '>= 0' - meaning we must call this function
			 * ourselves.
			 */
3798
			flags = r->flags;
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808
			clear_bit(In_sync, &r->flags); /* Mandatory for hot remove. */
			if (r->raid_disk >= 0) {
				if (mddev->pers->hot_remove_disk(mddev, r)) {
					/* Failed to revive this device, try next */
					r->flags = flags;
					continue;
				}
			} else
				r->raid_disk = r->saved_raid_disk = i;

3809 3810
			clear_bit(Faulty, &r->flags);
			clear_bit(WriteErrorSeen, &r->flags);
3811

3812
			if (mddev->pers->hot_add_disk(mddev, r)) {
3813 3814
				/* Failed to revive this device, try next */
				r->raid_disk = r->saved_raid_disk = -1;
3815 3816
				r->flags = flags;
			} else {
3817
				clear_bit(In_sync, &r->flags);
3818
				r->recovery_offset = 0;
3819 3820
				set_bit(i, (void *) cleared_failed_devices);
				cleared = true;
3821 3822 3823
			}
		}
	}
3824 3825 3826 3827 3828

	/* If any failed devices could be cleared, update all sbs failed_devices bits */
	if (cleared) {
		uint64_t failed_devices[DISKS_ARRAY_ELEMS];

3829
		rdev_for_each(r, &rs->md) {
3830 3831 3832
			if (test_bit(Journal, &r->flags))
				continue;

3833
			sb = page_address(r->sb_page);
3834 3835 3836 3837 3838 3839
			sb_retrieve_failed_devices(sb, failed_devices);

			for (i = 0; i < DISKS_ARRAY_ELEMS; i++)
				failed_devices[i] &= ~cleared_failed_devices[i];

			sb_update_failed_devices(sb, failed_devices);
3840 3841 3842 3843
		}
	}
}

M
Mike Snitzer 已提交
3844
static int __load_dirty_region_bitmap(struct raid_set *rs)
3845 3846 3847 3848 3849
{
	int r = 0;

	/* Try loading the bitmap unless "raid0", which does not have one */
	if (!rs_is_raid0(rs) &&
3850
	    !test_and_set_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags)) {
3851 3852 3853 3854 3855 3856 3857 3858
		r = bitmap_load(&rs->md);
		if (r)
			DMERR("Failed to load bitmap");
	}

	return r;
}

3859 3860 3861 3862 3863 3864
/* Enforce updating all superblocks */
static void rs_update_sbs(struct raid_set *rs)
{
	struct mddev *mddev = &rs->md;
	int ro = mddev->ro;

3865
	set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
3866 3867 3868 3869 3870
	mddev->ro = 0;
	md_update_sb(mddev, 1);
	mddev->ro = ro;
}

3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888
/*
 * Reshape changes raid algorithm of @rs to new one within personality
 * (e.g. raid6_zr -> raid6_nc), changes stripe size, adds/removes
 * disks from a raid set thus growing/shrinking it or resizes the set
 *
 * Call mddev_lock_nointr() before!
 */
static int rs_start_reshape(struct raid_set *rs)
{
	int r;
	struct mddev *mddev = &rs->md;
	struct md_personality *pers = mddev->pers;

	r = rs_setup_reshape(rs);
	if (r)
		return r;

	/* Need to be resumed to be able to start reshape, recovery is frozen until raid_resume() though */
3889
	if (test_and_clear_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags))
3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915
		mddev_resume(mddev);

	/*
	 * Check any reshape constraints enforced by the personalility
	 *
	 * May as well already kick the reshape off so that * pers->start_reshape() becomes optional.
	 */
	r = pers->check_reshape(mddev);
	if (r) {
		rs->ti->error = "pers->check_reshape() failed";
		return r;
	}

	/*
	 * Personality may not provide start reshape method in which
	 * case check_reshape above has already covered everything
	 */
	if (pers->start_reshape) {
		r = pers->start_reshape(mddev);
		if (r) {
			rs->ti->error = "pers->start_reshape() failed";
			return r;
		}
	}

	/* Suspend because a resume will happen in raid_resume() */
3916 3917
	set_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags);
	mddev_suspend(mddev);
3918

3919 3920 3921 3922 3923 3924
	/*
	 * Now reshape got set up, update superblocks to
	 * reflect the fact so that a table reload will
	 * access proper superblock content in the ctr.
	 */
	rs_update_sbs(rs);
3925 3926 3927 3928

	return 0;
}

3929 3930
static int raid_preresume(struct dm_target *ti)
{
3931
	int r;
3932 3933 3934
	struct raid_set *rs = ti->private;
	struct mddev *mddev = &rs->md;

3935
	/* This is a resume after a suspend of the set -> it's already started. */
3936
	if (test_and_set_bit(RT_FLAG_RS_PRERESUMED, &rs->runtime_flags))
3937 3938
		return 0;

3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961
	if (!test_bit(__CTR_FLAG_REBUILD, &rs->ctr_flags)) {
		struct raid_set *rs_active = rs_find_active(rs);

		if (rs_active) {
			/*
			 * In case no rebuilds have been requested
			 * and an active table slot exists, copy
			 * current resynchonization completed and
			 * reshape position pointers across from
			 * suspended raid set in the active slot.
			 *
			 * This resumes the new mapping at current
			 * offsets to continue recover/reshape without
			 * necessarily redoing a raid set partially or
			 * causing data corruption in case of a reshape.
			 */
			if (rs_active->md.curr_resync_completed != MaxSector)
				mddev->curr_resync_completed = rs_active->md.curr_resync_completed;
			if (rs_active->md.reshape_position != MaxSector)
				mddev->reshape_position = rs_active->md.reshape_position;
		}
	}

3962 3963
	/*
	 * The superblocks need to be updated on disk if the
3964 3965 3966
	 * array is new or new devices got added (thus zeroed
	 * out by userspace) or __load_dirty_region_bitmap
	 * will overwrite them in core with old data or fail.
3967
	 */
3968 3969
	if (test_bit(RT_FLAG_UPDATE_SBS, &rs->runtime_flags))
		rs_update_sbs(rs);
3970 3971

	/* Load the bitmap from disk unless raid0 */
3972 3973 3974 3975
	r = __load_dirty_region_bitmap(rs);
	if (r)
		return r;

3976
	/* Resize bitmap to adjust to changed region size (aka MD bitmap chunksize) */
3977
	if (test_bit(RT_FLAG_RS_BITMAP_LOADED, &rs->runtime_flags) && mddev->bitmap &&
3978 3979 3980 3981 3982 3983 3984
	    mddev->bitmap_info.chunksize != to_bytes(rs->requested_bitmap_chunk_sectors)) {
		r = bitmap_resize(mddev->bitmap, mddev->dev_sectors,
				  to_bytes(rs->requested_bitmap_chunk_sectors), 0);
		if (r)
			DMERR("Failed to resize bitmap");
	}

3985 3986 3987 3988 3989 3990 3991 3992
	/* Check for any resize/reshape on @rs and adjust/initiate */
	/* Be prepared for mddev_resume() in raid_resume() */
	set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
	if (mddev->recovery_cp && mddev->recovery_cp < MaxSector) {
		set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
		mddev->resync_min = mddev->recovery_cp;
	}

3993
	/* Check for any reshape request unless new raid set */
3994
	if (test_bit(RT_FLAG_RESHAPE_RS, &rs->runtime_flags)) {
3995
		/* Initiate a reshape. */
3996
		rs_set_rdev_sectors(rs);
3997 3998 3999 4000 4001 4002 4003 4004 4005
		mddev_lock_nointr(mddev);
		r = rs_start_reshape(rs);
		mddev_unlock(mddev);
		if (r)
			DMWARN("Failed to check/start reshape, continuing without change");
		r = 0;
	}

	return r;
4006 4007
}

4008 4009 4010
static void raid_resume(struct dm_target *ti)
{
	struct raid_set *rs = ti->private;
4011
	struct mddev *mddev = &rs->md;
4012

4013
	if (test_and_set_bit(RT_FLAG_RS_RESUMED, &rs->runtime_flags)) {
4014 4015 4016 4017 4018 4019
		/*
		 * A secondary resume while the device is active.
		 * Take this opportunity to check whether any failed
		 * devices are reachable again.
		 */
		attempt_restore_of_faulty_devices(rs);
4020
	}
4021

4022
	if (test_and_clear_bit(RT_FLAG_RS_SUSPENDED, &rs->runtime_flags)) {
4023 4024 4025 4026
		/* Only reduce raid set size before running a disk removing reshape. */
		if (mddev->delta_disks < 0)
			rs_set_capacity(rs);

4027
		mddev_lock_nointr(mddev);
4028
		clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4029 4030
		mddev->ro = 0;
		mddev->in_sync = 0;
4031
		mddev_resume(mddev);
4032 4033
		mddev_unlock(mddev);
	}
N
NeilBrown 已提交
4034 4035 4036 4037
}

static struct target_type raid_target = {
	.name = "raid",
4038
	.version = {1, 13, 2},
N
NeilBrown 已提交
4039 4040 4041 4042 4043
	.module = THIS_MODULE,
	.ctr = raid_ctr,
	.dtr = raid_dtr,
	.map = raid_map,
	.status = raid_status,
4044
	.message = raid_message,
N
NeilBrown 已提交
4045 4046 4047
	.iterate_devices = raid_iterate_devices,
	.io_hints = raid_io_hints,
	.postsuspend = raid_postsuspend,
4048
	.preresume = raid_preresume,
N
NeilBrown 已提交
4049 4050 4051 4052 4053
	.resume = raid_resume,
};

static int __init dm_raid_init(void)
{
4054 4055 4056 4057
	DMINFO("Loading target version %u.%u.%u",
	       raid_target.version[0],
	       raid_target.version[1],
	       raid_target.version[2]);
N
NeilBrown 已提交
4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068
	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);

4069 4070 4071 4072
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");

4073 4074
MODULE_DESCRIPTION(DM_NAME " raid0/1/10/4/5/6 target");
MODULE_ALIAS("dm-raid0");
4075 4076
MODULE_ALIAS("dm-raid1");
MODULE_ALIAS("dm-raid10");
N
NeilBrown 已提交
4077 4078 4079 4080
MODULE_ALIAS("dm-raid4");
MODULE_ALIAS("dm-raid5");
MODULE_ALIAS("dm-raid6");
MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
4081
MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
N
NeilBrown 已提交
4082
MODULE_LICENSE("GPL");