dm-raid.c 83.2 KB
Newer Older
NeilBrown's avatar
NeilBrown committed
1
2
/*
 * Copyright (C) 2010-2011 Neil Brown
3
 * Copyright (C) 2010-2016 Red Hat, Inc. All rights reserved.
NeilBrown's avatar
NeilBrown committed
4
5
6
7
8
 *
 * This file is released under the GPL.
 */

#include <linux/slab.h>
9
#include <linux/module.h>
NeilBrown's avatar
NeilBrown committed
10
11

#include "md.h"
12
#include "raid1.h"
NeilBrown's avatar
NeilBrown committed
13
#include "raid5.h"
14
#include "raid10.h"
NeilBrown's avatar
NeilBrown committed
15
16
#include "bitmap.h"

Alasdair G Kergon's avatar
Alasdair G Kergon committed
17
18
#include <linux/device-mapper.h>

NeilBrown's avatar
NeilBrown committed
19
#define DM_MSG_PREFIX "raid"
20
#define	MAX_RAID_DEVICES	253 /* md-raid kernel limit */
NeilBrown's avatar
NeilBrown committed
21

22
23
static bool devices_handle_discard_safely = false;

NeilBrown's avatar
NeilBrown committed
24
/*
25
26
 * The following flags are used by dm-raid.c to set up the array state.
 * They must be cleared before md_run is called.
NeilBrown's avatar
NeilBrown committed
27
 */
28
#define FirstUse 10		/* rdev flag */
NeilBrown's avatar
NeilBrown committed
29
30
31
32

struct raid_dev {
	/*
	 * Two DM devices, one to hold metadata and one to hold the
33
	 * actual data/parity.	The reason for this is to not confuse
NeilBrown's avatar
NeilBrown committed
34
35
36
37
38
39
40
41
42
43
44
	 * ti->len and give more flexibility in altering size and
	 * characteristics.
	 *
	 * While it is possible for this device to be associated
	 * with a different physical device than the data_dev, it
	 * is intended for it to be the same.
	 *    |--------- Physical Device ---------|
	 *    |- meta_dev -|------ data_dev ------|
	 */
	struct dm_dev *meta_dev;
	struct dm_dev *data_dev;
45
	struct md_rdev rdev;
NeilBrown's avatar
NeilBrown committed
46
47
48
};

/*
Heinz Mauelshagen's avatar
Heinz Mauelshagen committed
49
 * Flags for rs->ctr_flags field.
50
51
52
 *
 * 1 = no flag value
 * 2 = flag with value
NeilBrown's avatar
NeilBrown committed
53
 */
54
55
56
57
58
59
60
61
62
63
64
65
#define CTR_FLAG_SYNC		   0x1	 /* 1 */ /* Not with raid0! */
#define CTR_FLAG_NOSYNC		   0x2	 /* 1 */ /* Not with raid0! */
#define CTR_FLAG_REBUILD	   0x4	 /* 2 */ /* Not with raid0! */
#define CTR_FLAG_DAEMON_SLEEP	   0x8	 /* 2 */ /* Not with raid0! */
#define CTR_FLAG_MIN_RECOVERY_RATE 0x10	 /* 2 */ /* Not with raid0! */
#define CTR_FLAG_MAX_RECOVERY_RATE 0x20	 /* 2 */ /* Not with raid0! */
#define CTR_FLAG_MAX_WRITE_BEHIND  0x40	 /* 2 */ /* Only with raid1! */
#define CTR_FLAG_WRITE_MOSTLY	   0x80	 /* 2 */ /* Only with raid1! */
#define CTR_FLAG_STRIPE_CACHE	   0x100 /* 2 */ /* Only with raid4/5/6! */
#define CTR_FLAG_REGION_SIZE	   0x200 /* 2 */ /* Not with raid0! */
#define CTR_FLAG_RAID10_COPIES	   0x400 /* 2 */ /* Only with raid10 */
#define CTR_FLAG_RAID10_FORMAT	   0x800 /* 2 */ /* Only with raid10 */
66
/* New for v1.9.0 */
67
68
#define CTR_FLAG_DELTA_DISKS	      0x1000 /* 2 */ /* Only with reshapable raid4/5/6/10! */
#define CTR_FLAG_DATA_OFFSET	      0x2000 /* 2 */ /* Only with reshapable raid4/5/6/10! */
69
#define CTR_FLAG_RAID10_USE_NEAR_SETS 0x4000 /* 2 */ /* Only with raid10! */
70

71
72
73
74
75
76
77
78
79
/*
 * 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') */
80
81
#define	CTR_FLAG_OPTIONS_NO_ARGS	(CTR_FLAGS_ANY_SYNC | \
					 CTR_FLAG_RAID10_USE_NEAR_SETS)
82
83
84
85
86
87
88
89
90
91
92

/* 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 | \
93
94
95
				  CTR_FLAG_RAID10_FORMAT | \
				  CTR_FLAG_DELTA_DISKS | \
				  CTR_FLAG_DATA_OFFSET)
96
97
98
99
100
101
102
103
104
105
106
107
108

/* All ctr optional arguments */
#define ALL_CTR_FLAGS		(CTR_FLAG_OPTIONS_NO_ARGS | \
				 CTR_FLAG_OPTIONS_ONE_ARG)

/* Invalid options definitions per raid level... */

/* "raid0" does not accept any options */
#define RAID0_INVALID_FLAGS ALL_CTR_FLAGS

/* "raid1" does not accept stripe cache or any raid10 options */
#define RAID1_INVALID_FLAGS	(CTR_FLAG_STRIPE_CACHE | \
				 CTR_FLAG_RAID10_COPIES | \
109
110
111
				 CTR_FLAG_RAID10_FORMAT | \
				 CTR_FLAG_DELTA_DISKS | \
				 CTR_FLAG_DATA_OFFSET)
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

/* "raid10" does not accept any raid1 or stripe cache options */
#define RAID10_INVALID_FLAGS	(CTR_FLAG_WRITE_MOSTLY | \
				 CTR_FLAG_MAX_WRITE_BEHIND | \
				 CTR_FLAG_STRIPE_CACHE)
/*
 * "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
 */
#define RAID45_INVALID_FLAGS	(CTR_FLAG_WRITE_MOSTLY | \
				 CTR_FLAG_MAX_WRITE_BEHIND | \
				 CTR_FLAG_RAID10_FORMAT | \
127
128
				 CTR_FLAG_RAID10_COPIES | \
				 CTR_FLAG_RAID10_USE_NEAR_SETS)
129
130
131
#define RAID6_INVALID_FLAGS	(CTR_FLAG_NOSYNC | RAID45_INVALID_FLAGS)
/* ...invalid options definitions per raid level */

132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * 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.
 */
#define RT_FLAG_RS_PRERESUMED		0x1
#define RT_FLAG_RS_RESUMED		0x2
#define RT_FLAG_RS_BITMAP_LOADED	0x4
#define RT_FLAG_UPDATE_SBS		0x8

145
146
147
/* Array elements of 64 bit needed for rebuild/write_mostly bits */
#define DISKS_ARRAY_ELEMS ((MAX_RAID_DEVICES + (sizeof(uint64_t) * 8 - 1)) / sizeof(uint64_t) / 8)

148
149
150
151
152
153
154
155
156
/*
 * raid set level, layout and chunk sectors backup/restore
 */
struct rs_layout {
	int new_level;
	int new_layout;
	int new_chunk_sectors;
};

NeilBrown's avatar
NeilBrown committed
157
158
159
struct raid_set {
	struct dm_target *ti;

160
	uint32_t bitmap_loaded;
Heinz Mauelshagen's avatar
Heinz Mauelshagen committed
161
	uint32_t ctr_flags;
162
163
164
	uint32_t runtime_flags;

	uint64_t rebuild_disks[DISKS_ARRAY_ELEMS];
NeilBrown's avatar
NeilBrown committed
165

166
167
	int raid_disks;
	int delta_disks;
168
	int data_offset;
169
170
	int raid10_copies;

171
	struct mddev md;
NeilBrown's avatar
NeilBrown committed
172
173
	struct raid_type *raid_type;
	struct dm_target_callbacks callbacks;
174
	struct rs_layout rs_layout;
NeilBrown's avatar
NeilBrown committed
175
176
177
178

	struct raid_dev dev[0];
};

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* Backup/restore raid set configuration helpers */
static void _rs_config_backup(struct raid_set *rs, struct rs_layout *l)
{
	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;
}

static void rs_config_backup(struct raid_set *rs)
{
	return _rs_config_backup(rs, &rs->rs_layout);
}

static void _rs_config_restore(struct raid_set *rs, struct rs_layout *l)
{
	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;
}

static void rs_config_restore(struct raid_set *rs)
{
	return _rs_config_restore(rs, &rs->rs_layout);
}
/* END: backup/restore raid set configuration helpers */

209
210
211
212
213
214
/* 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

NeilBrown's avatar
NeilBrown committed
215
216
217
218
219
220
221
222
223
/* Supported raid types and properties. */
static struct raid_type {
	const char *name;		/* RAID algorithm. */
	const char *descr;		/* Descriptor text for logging. */
	const unsigned parity_devs;	/* # of parity devices. */
	const unsigned minimal_devs;	/* minimal # of devices in set. */
	const unsigned level;		/* RAID level. */
	const unsigned algorithm;	/* RAID algorithm. */
} raid_types[] = {
224
225
226
	{"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},
227
	{"raid10_offset", "raid10 offset (striped mirrors)",	    0, 2, 10, ALGORITHM_RAID10_OFFSET},
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
	{"raid10_near",	  "raid10 near (striped mirrors)",	    0, 2, 10, ALGORITHM_RAID10_NEAR},
	{"raid10",	  "raid10 (striped mirrors)",		    0, 2, 10, ALGORITHM_RAID10_DEFAULT},
	{"raid4",	  "raid4 (dedicated last parity disk)",	    1, 2, 4,  ALGORITHM_PARITY_N}, /* raid4 layout = raid5_n */
	{"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}
NeilBrown's avatar
NeilBrown committed
244
245
};

246
247
248
249
250
251
/* True, if @v is in inclusive range [@min, @max] */
static bool _in_range(long v, long min, long max)
{
	return v >= min && v <= max;
}

252
253
254
255
256
257
258
259
/* ctr flag bit manipulation... */
/* Set single @flag in @flags */
static void _set_flag(uint32_t flag, uint32_t *flags)
{
	WARN_ON_ONCE(hweight32(flag) != 1);
	*flags |= flag;
}

260
261
262
263
264
265
266
/* Clear single @flag in @flags */
static void _clear_flag(uint32_t flag, uint32_t *flags)
{
	WARN_ON_ONCE(hweight32(flag) != 1);
	*flags &= ~flag;
}

267
268
269
270
271
272
273
/* Test single @flag in @flags */
static bool _test_flag(uint32_t flag, uint32_t flags)
{
	WARN_ON_ONCE(hweight32(flag) != 1);
	return (flag & flags) ? true : false;
}

274
275
276
277
278
279
/* Test multiple @flags in @all_flags */
static bool _test_flags(uint32_t flags, uint32_t all_flags)
{
	return (flags & all_flags) ? true : false;
}

280
281
282
283
284
285
/* Clear (multiple) @flags in @all_flags */
static void _clear_flags(uint32_t flags, uint32_t *all_flags)
{
	*all_flags &= ~flags;
}

286
287
288
289
290
291
292
293
294
/* Return true if single @flag is set in @*flags, else set it and return false */
static bool _test_and_set_flag(uint32_t flag, uint32_t *flags)
{
	if (_test_flag(flag, *flags))
		return true;

	_set_flag(flag, flags);
	return false;
}
295
296
297
298
299
300
301
302
303
304
305

/* Return true if single @flag is set in @*flags and clear it, else return false */
static bool _test_and_clear_flag(uint32_t flag, uint32_t *flags)
{
	if (_test_flag(flag, *flags)) {
		_clear_flag(flag, flags);
		return true;
	}

	return false;
}
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* ...ctr and runtime flag bit manipulation */

/* All table line arguments are defined here */
static struct arg_name_flag {
	const uint32_t flag;
	const char *name;
} _arg_name_flags[] = {
	{ 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"},
	{ CTR_FLAG_WRITE_MOSTLY, "writemostly"},
	{ CTR_FLAG_STRIPE_CACHE, "stripe_cache"},
	{ CTR_FLAG_REGION_SIZE, "region_size"},
	{ CTR_FLAG_RAID10_COPIES, "raid10_copies"},
	{ CTR_FLAG_RAID10_FORMAT, "raid10_format"},
325
326
327
	{ CTR_FLAG_DATA_OFFSET, "data_offset"},
	{ CTR_FLAG_DELTA_DISKS, "delta_disks"},
	{ CTR_FLAG_RAID10_USE_NEAR_SETS, "raid10_use_near_sets"},
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
};

/* Return argument name string for given @flag */
static const char *_argname_by_flag(const uint32_t flag)
{
	if (hweight32(flag) == 1) {
		struct arg_name_flag *anf = _arg_name_flags + ARRAY_SIZE(_arg_name_flags);

		while (anf-- > _arg_name_flags)
			if (_test_flag(flag, anf->flag))
				return anf->name;

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

	return NULL;
}

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * bool helpers to test for various raid levels of a raid set,
 * is. it's level as reported by the superblock rather than
 * 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;
}

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

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * bool helpers to test for various raid levels of a raid type
 */

/* 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)
{
	return _in_range(rt->level, 4, 5);
}

/* Return true, if raid type in @rt is raid6 */
static bool rt_is_raid6(struct raid_type *rt)
{
	return rt->level == 6;
}
396
397
398
399
400
401

/* Return true, if raid type in @rt is raid4/5/6 */
static bool rt_is_raid456(struct raid_type *rt)
{
	return _in_range(rt->level, 4, 6);
}
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* END: raid level bools */

/* Return invalid ctr flags for the raid level of @rs */
static uint32_t _invalid_flags(struct raid_set *rs)
{
	if (rt_is_raid0(rs->raid_type))
		return RAID0_INVALID_FLAGS;
	else if (rt_is_raid1(rs->raid_type))
		return RAID1_INVALID_FLAGS;
	else if (rt_is_raid10(rs->raid_type))
		return RAID10_INVALID_FLAGS;
	else if (rt_is_raid45(rs->raid_type))
		return RAID45_INVALID_FLAGS;
	else if (rt_is_raid6(rs->raid_type))
		return RAID6_INVALID_FLAGS;

	return ~0;
}

/*
 * Check for any invalid flags set on @rs defined by bitset @invalid_flags
 *
 * Has to be called after parsing of the ctr flags!
 */
static int rs_check_for_invalid_flags(struct raid_set *rs)
{
428
429
430
431
	if (_test_flags(rs->ctr_flags, _invalid_flags(rs))) {
		rs->ti->error = "Invalid flag combined";
		return -EINVAL;
	}
432
433
434
435

	return 0;
}

436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474

/* 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 */
static unsigned int _raid10_near_copies(int layout)
{
	return layout & 0xFF;
}

/* Return md raid10 far copies for @layout */
static unsigned int _raid10_far_copies(int layout)
{
	return _raid10_near_copies(layout >> RAID10_FAR_COPIES_SHIFT);
}

/* Return true if md raid10 offset for @layout */
static unsigned int _is_raid10_offset(int layout)
{
	return layout & RAID10_OFFSET;
}

/* Return true if md raid10 near for @layout */
static unsigned int _is_raid10_near(int layout)
{
	return !_is_raid10_offset(layout) && _raid10_near_copies(layout) > 1;
}

/* Return true if md raid10 far for @layout */
static unsigned int _is_raid10_far(int layout)
{
	return !_is_raid10_offset(layout) && _raid10_far_copies(layout) > 1;
}

/* Return md raid10 layout string for @layout */
static const char *raid10_md_layout_to_format(int layout)
475
476
{
	/*
477
478
479
	 * Bit 16 stands for "offset"
	 * (i.e. adjacent stripes hold copies)
	 *
480
481
	 * Refer to MD's raid10.c for details
	 */
482
	if (_is_raid10_offset(layout))
483
484
		return "offset";

485
	if (_raid10_near_copies(layout) > 1)
486
487
		return "near";

488
489
	WARN_ON(_raid10_far_copies(layout) < 2);

490
491
492
	return "far";
}

493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/* Return md raid10 algorithm for @name */
static const int raid10_name_to_format(const char *name)
{
	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)
509
{
510
511
	return _raid10_near_copies(layout) > 1 ?
	       _raid10_near_copies(layout) : _raid10_far_copies(layout);
512
513
}

514
515
516
517
/* 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)
518
{
519
	unsigned int n = 1, f = 1, r = 0;
520

521
522
523
524
525
526
527
528
529
530
	/*
	 * 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)
531
		n = copies;
532
533
534
535
536
537
538
539

	else if (algorithm == ALGORITHM_RAID10_OFFSET) {
		f = copies;
		r = RAID10_OFFSET;
		if (!_test_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, rs->ctr_flags))
			r |= RAID10_USE_FAR_SETS;

	} else if (algorithm == ALGORITHM_RAID10_FAR) {
540
		f = copies;
541
542
543
		r = !RAID10_OFFSET;
		if (!_test_flag(CTR_FLAG_RAID10_USE_NEAR_SETS, rs->ctr_flags))
			r |= RAID10_USE_FAR_SETS;
544

545
546
547
548
549
550
	} else
		return -EINVAL;

	return r | (f << RAID10_FAR_COPIES_SHIFT) | n;
}
/* END: MD raid10 bit definitions and helpers */
551

552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* Check for any of the raid10 algorithms */
static int _got_raid10(struct raid_type *rtp, const int layout)
{
	if (rtp->level == 10) {
		switch (rtp->algorithm) {
		case ALGORITHM_RAID10_DEFAULT:
		case ALGORITHM_RAID10_NEAR:
			return _is_raid10_near(layout);
		case ALGORITHM_RAID10_OFFSET:
			return _is_raid10_offset(layout);
		case ALGORITHM_RAID10_FAR:
			return _is_raid10_far(layout);
		default:
			break;
		}
	}
568

569
	return 0;
570
571
}

572
/* Return raid_type for @name */
573
static struct raid_type *get_raid_type(const char *name)
NeilBrown's avatar
NeilBrown committed
574
{
575
	struct raid_type *rtp = raid_types + ARRAY_SIZE(raid_types);
NeilBrown's avatar
NeilBrown committed
576

577
578
579
	while (rtp-- > raid_types)
		if (!strcasecmp(rtp->name, name))
			return rtp;
NeilBrown's avatar
NeilBrown committed
580
581
582
583

	return NULL;
}

584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* 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 &&
		    (_got_raid10(rtp, layout) || rtp->algorithm == layout))
			return rtp;
	}

	return NULL;
}

599
600
601
602
603
604
605
606
607
608
609
610
611
/*
 * 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;
}

612
613
614
615
616
617
618
619
620
621
622
/*
 * 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;
623
	mddev->raid_disks = rs->raid_disks;
624
625
626
627
	mddev->delta_disks = 0;
}


NeilBrown's avatar
NeilBrown committed
628
629
630
631
632
static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
{
	unsigned i;
	struct raid_set *rs;

633
634
635
636
	if (raid_devs <= raid_type->parity_devs) {
		ti->error = "Insufficient number of devices";
		return ERR_PTR(-EINVAL);
	}
NeilBrown's avatar
NeilBrown committed
637
638

	rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
639
640
641
642
	if (!rs) {
		ti->error = "Cannot allocate raid context";
		return ERR_PTR(-ENOMEM);
	}
NeilBrown's avatar
NeilBrown committed
643
644
645

	mddev_init(&rs->md);

646
647
648
	rs->raid_disks = raid_devs;
	rs->delta_disks = 0;

NeilBrown's avatar
NeilBrown committed
649
650
651
652
653
654
655
656
	rs->ti = ti;
	rs->raid_type = raid_type;
	rs->md.raid_disks = raid_devs;
	rs->md.level = raid_type->level;
	rs->md.new_level = rs->md.level;
	rs->md.layout = raid_type->algorithm;
	rs->md.new_layout = rs->md.layout;
	rs->md.delta_disks = 0;
657
	rs->md.recovery_cp = rs_is_raid0(rs) ? MaxSector : 0;
NeilBrown's avatar
NeilBrown committed
658
659
660
661
662
663
664
665
666
667

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

	/*
	 * Remaining items to be initialized by further RAID params:
	 *  rs->md.persistent
	 *  rs->md.external
	 *  rs->md.chunk_sectors
	 *  rs->md.new_chunk_sectors
668
	 *  rs->md.dev_sectors
NeilBrown's avatar
NeilBrown committed
669
670
671
672
673
674
675
676
677
	 */

	return rs;
}

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

678
679
680
	for (i = 0; i < rs->md.raid_disks; i++) {
		if (rs->dev[i].meta_dev)
			dm_put_device(rs->ti, rs->dev[i].meta_dev);
681
		md_rdev_clear(&rs->dev[i].rdev);
NeilBrown's avatar
NeilBrown committed
682
683
		if (rs->dev[i].data_dev)
			dm_put_device(rs->ti, rs->dev[i].data_dev);
684
	}
NeilBrown's avatar
NeilBrown committed
685
686
687
688
689
690
691
692
693

	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
 *
694
695
696
697
698
699
700
701
702
703
 * The following are permitted:
 *    - -
 *    - <data_dev>
 *    <meta_dev> <data_dev>
 *
 * The following is not allowed:
 *    <meta_dev> -
 *
 * This code parses those words.  If there is a failure,
 * the caller must use context_free to unwind the operations.
NeilBrown's avatar
NeilBrown committed
704
 */
705
static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
NeilBrown's avatar
NeilBrown committed
706
707
708
709
{
	int i;
	int rebuild = 0;
	int metadata_available = 0;
710
	int r = 0;
711
	const char *arg;
NeilBrown's avatar
NeilBrown committed
712

713
714
715
716
717
718
	/* Put off the number of raid devices argument to get to dev pairs */
	arg = dm_shift_arg(as);
	if (!arg)
		return -EINVAL;

	for (i = 0; i < rs->md.raid_disks; i++) {
NeilBrown's avatar
NeilBrown committed
719
720
721
722
723
724
725
726
727
728
729
730
		rs->dev[i].rdev.raid_disk = i;

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

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

731
732
733
734
735
		arg = dm_shift_arg(as);
		if (!arg)
			return -EINVAL;

		if (strcmp(arg, "-")) {
736
737
738
739
740
741
			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;
			}
742
743

			rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
744
745
746
747
			if (!rs->dev[i].rdev.sb_page) {
				rs->ti->error = "Failed to allocate superblock page";
				return -ENOMEM;
			}
NeilBrown's avatar
NeilBrown committed
748
749
		}

750
751
752
753
754
		arg = dm_shift_arg(as);
		if (!arg)
			return -EINVAL;

		if (!strcmp(arg, "-")) {
NeilBrown's avatar
NeilBrown committed
755
			if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
756
757
758
759
			    (!rs->dev[i].rdev.recovery_offset)) {
				rs->ti->error = "Drive designated for rebuild not specified";
				return -EINVAL;
			}
NeilBrown's avatar
NeilBrown committed
760

761
762
763
764
			if (rs->dev[i].meta_dev) {
				rs->ti->error = "No data device supplied with metadata device";
				return -EINVAL;
			}
765

NeilBrown's avatar
NeilBrown committed
766
767
768
			continue;
		}

769
770
771
772
773
774
		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;
		}
NeilBrown's avatar
NeilBrown committed
775

776
777
778
779
		if (rs->dev[i].meta_dev) {
			metadata_available = 1;
			rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
		}
NeilBrown's avatar
NeilBrown committed
780
		rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
781
		list_add_tail(&rs->dev[i].rdev.same_set, &rs->md.disks);
NeilBrown's avatar
NeilBrown committed
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
		if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
			rebuild++;
	}

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

	return 0;
}

809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/*
 * validate_region_size
 * @rs
 * @region_size:  region size in sectors.  If 0, pick a size (4MiB default).
 *
 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
 *
 * Returns: 0 on success, -EINVAL on failure.
 */
static int validate_region_size(struct raid_set *rs, unsigned long region_size)
{
	unsigned long min_region_size = rs->ti->len / (1 << 21);

	if (!region_size) {
		/*
825
		 * Choose a reasonable default.	 All figures in sectors.
826
827
		 */
		if (min_region_size > (1 << 13)) {
828
			/* If not a power of 2, make it the next power of 2 */
829
			region_size = roundup_pow_of_two(min_region_size);
830
831
832
833
834
835
836
837
838
839
			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.
		 */
840
841
842
843
		if (region_size > rs->ti->len) {
			rs->ti->error = "Supplied region size is too large";
			return -EINVAL;
		}
844
845
846
847

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

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

857
858
859
860
		if (region_size < rs->md.chunk_sectors) {
			rs->ti->error = "Region size is smaller than the chunk size";
			return -EINVAL;
		}
861
862
863
864
865
866
867
868
869
870
	}

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

	return 0;
}

871
/*
872
 * validate_raid_redundancy
873
874
 * @rs
 *
875
876
 * Determine if there are enough devices in the array that haven't
 * failed (or are being rebuilt) to form a usable array.
877
878
879
 *
 * Returns: 0 on success, -EINVAL on failure.
 */
880
static int validate_raid_redundancy(struct raid_set *rs)
881
882
{
	unsigned i, rebuild_cnt = 0;
883
	unsigned rebuilds_per_group = 0, copies, d;
884
	unsigned group_size, last_group_start;
885
886

	for (i = 0; i < rs->md.raid_disks; i++)
887
888
		if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
		    !rs->dev[i].rdev.sb_page)
889
890
891
892
893
894
895
896
897
898
899
900
901
902
			rebuild_cnt++;

	switch (rs->raid_type->level) {
	case 1:
		if (rebuild_cnt >= rs->md.raid_disks)
			goto too_many;
		break;
	case 4:
	case 5:
	case 6:
		if (rebuild_cnt > rs->raid_type->parity_devs)
			goto too_many;
		break;
	case 10:
903
904
905
906
907
908
909
910
911
912
913
914
915
916
		copies = raid10_md_layout_to_copies(rs->md.layout);
		if (rebuild_cnt < copies)
			break;

		/*
		 * It is possible to have a higher rebuild count for RAID10,
		 * as long as the failed devices occur in different mirror
		 * groups (i.e. different stripes).
		 *
		 * When checking "near" format, make sure no adjacent devices
		 * have failed beyond what can be handled.  In addition to the
		 * simple case where the number of devices is a multiple of the
		 * number of copies, we must also handle cases where the number
		 * of devices is not a multiple of the number of copies.
917
918
919
		 * E.g.	   dev1 dev2 dev3 dev4 dev5
		 *	    A	 A    B	   B	C
		 *	    C	 D    D	   E	E
920
		 */
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
		if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
			for (i = 0; i < rs->md.raid_disks * copies; i++) {
				if (!(i % copies))
					rebuilds_per_group = 0;
				d = i % rs->md.raid_disks;
				if ((!rs->dev[d].rdev.sb_page ||
				     !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
				    (++rebuilds_per_group >= copies))
					goto too_many;
			}
			break;
		}

		/*
		 * When checking "far" and "offset" formats, we need to ensure
		 * that the device that holds its copy is not also dead or
		 * being rebuilt.  (Note that "far" and "offset" formats only
		 * support two copies right now.  These formats also only ever
		 * use the 'use_far_sets' variant.)
		 *
		 * This check is somewhat complicated by the need to account
942
		 * for arrays that are not a multiple of (far) copies.	This
943
944
945
946
947
948
949
950
		 * 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))
951
				rebuilds_per_group = 0;
952
953
			if ((!rs->dev[i].rdev.sb_page ||
			     !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
954
			    (++rebuilds_per_group >= copies))
955
					goto too_many;
956
957
		}
		break;
958
	default:
959
960
		if (rebuild_cnt)
			return -EINVAL;
961
962
963
964
965
966
967
968
	}

	return 0;

too_many:
	return -EINVAL;
}

NeilBrown's avatar
NeilBrown committed
969
970
971
972
/*
 * Possible arguments are...
 *	<chunk_size> [optional_args]
 *
973
974
 * Argument definitions
 *    <chunk_size>			The number of sectors per disk that
975
 *					will form the "stripe"
976
 *    [[no]sync]			Force or prevent recovery of the
977
 *					entire array
NeilBrown's avatar
NeilBrown committed
978
 *    [rebuild <idx>]			Rebuild the drive indicated by the index
979
 *    [daemon_sleep <ms>]		Time between bitmap daemon work to
980
 *					clear bits
NeilBrown's avatar
NeilBrown committed
981
982
 *    [min_recovery_rate <kB/sec/disk>]	Throttle RAID initialization
 *    [max_recovery_rate <kB/sec/disk>]	Throttle RAID initialization
983
 *    [write_mostly <idx>]		Indicate a write mostly drive via index
NeilBrown's avatar
NeilBrown committed
984
985
 *    [max_write_behind <sectors>]	See '-write-behind=' (man mdadm)
 *    [stripe_cache <sectors>]		Stripe cache size for higher RAIDs
986
 *    [region_size <sectors>]		Defines granularity of bitmap
987
988
 *
 * RAID10-only options:
989
 *    [raid10_copies <# copies>]	Number of copies.  (Default: 2)
990
 *    [raid10_format <near|far|offset>] Layout algorithm.  (Default: near)
NeilBrown's avatar
NeilBrown committed
991
 */
992
static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
NeilBrown's avatar
NeilBrown committed
993
994
			     unsigned num_raid_params)
{
995
	int raid10_format = ALGORITHM_RAID10_DEFAULT;
996
	unsigned raid10_copies = 2;
997
	unsigned i;
998
	unsigned value, region_size = 0;
999
	sector_t sectors_per_dev = rs->ti->len;
1000
	sector_t max_io_len;