dm-raid.c 55.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
33
34
35
36
37
38
39
40
41
42
43
44

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

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * 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') */
#define	CTR_FLAG_OPTIONS_NO_ARGS	CTR_FLAGS_ANY_SYNC

/* 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 | \
				  CTR_FLAG_RAID10_FORMAT)

/* 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 | \
				 CTR_FLAG_RAID10_FORMAT)

/* "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 | \
				 CTR_FLAG_RAID10_COPIES)
#define RAID6_INVALID_FLAGS	(CTR_FLAG_NOSYNC | RAID45_INVALID_FLAGS)
/* ...invalid options definitions per raid level */

NeilBrown's avatar
NeilBrown committed
122
123
124
struct raid_set {
	struct dm_target *ti;

125
	uint32_t bitmap_loaded;
Heinz Mauelshagen's avatar
Heinz Mauelshagen committed
126
	uint32_t ctr_flags;
NeilBrown's avatar
NeilBrown committed
127

128
	struct mddev md;
NeilBrown's avatar
NeilBrown committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
	struct raid_type *raid_type;
	struct dm_target_callbacks callbacks;

	struct raid_dev dev[0];
};

/* Supported raid types and properties. */
static struct raid_type {
	const char *name;		/* RAID algorithm. */
	const char *descr;		/* Descriptor text for logging. */
	const unsigned parity_devs;	/* # of parity devices. */
	const unsigned minimal_devs;	/* minimal # of devices in set. */
	const unsigned level;		/* RAID level. */
	const unsigned algorithm;	/* RAID algorithm. */
} raid_types[] = {
144
	{"raid0",    "RAID0 (striping)",                0, 2, 0, 0 /* NONE */},
145
	{"raid1",    "RAID1 (mirroring)",               0, 2, 1, 0 /* NONE */},
146
	{"raid10",   "RAID10 (striped mirrors)",        0, 2, 10, UINT_MAX /* Varies */},
NeilBrown's avatar
NeilBrown committed
147
148
149
150
151
152
153
154
155
156
	{"raid4",    "RAID4 (dedicated parity disk)",	1, 2, 5, ALGORITHM_PARITY_0},
	{"raid5_la", "RAID5 (left asymmetric)",		1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
	{"raid5_ra", "RAID5 (right asymmetric)",	1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
	{"raid5_ls", "RAID5 (left symmetric)",		1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
	{"raid5_rs", "RAID5 (right symmetric)",		1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
	{"raid6_zr", "RAID6 (zero restart)",		2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
	{"raid6_nr", "RAID6 (N restart)",		2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
	{"raid6_nc", "RAID6 (N continue)",		2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
};

157
158
159
160
161
162
/* True, if @v is in inclusive range [@min, @max] */
static bool _in_range(long v, long min, long max)
{
	return v >= min && v <= max;
}

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* 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;
}

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

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

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

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * 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;
}
/* END: raid level bools */

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Convenience functions to set ti->error to @errmsg and
 * return @r in order to shorten code in a lot of places
 */
static int ti_error_ret(struct dm_target *ti, const char *errmsg, int r)
{
	ti->error = (char *) errmsg;
	return r;
}

static int ti_error_einval(struct dm_target *ti, const char *errmsg)
{
	return ti_error_ret(ti, errmsg, -EINVAL);
}
/* END: convenience functions to set ti->error to @errmsg... */

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* 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)
{
	unsigned int ctr_flags = rs->ctr_flags, flag = 0;
	const uint32_t invalid_flags = _invalid_flags(rs);

	while ((ctr_flags &= ~flag)) {
		flag = 1 << __ffs(ctr_flags);

		if (_test_flag(flag, rs->ctr_flags) &&
		    _test_flag(flag, invalid_flags))
			return ti_error_einval(rs->ti, "Invalid flag combined");
	}

	return 0;
}

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
static char *raid10_md_layout_to_format(int layout)
{
	/*
	 * Bit 16 and 17 stand for "offset" and "use_far_sets"
	 * Refer to MD's raid10.c for details
	 */
	if ((layout & 0x10000) && (layout & 0x20000))
		return "offset";

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

	return "far";
}

328
329
static unsigned raid10_md_layout_to_copies(int layout)
{
330
331
332
	if ((layout & 0xFF) > 1)
		return layout & 0xFF;
	return (layout >> 8) & 0xFF;
333
334
335
336
}

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

Heinz Mauelshagen's avatar
Heinz Mauelshagen committed
339
	if (!strcasecmp("near", format))
340
341
342
343
		n = copies;
	else
		f = copies;

Heinz Mauelshagen's avatar
Heinz Mauelshagen committed
344
	if (!strcasecmp("offset", format))
345
346
		return 0x30000 | (f << 8) | n;

Heinz Mauelshagen's avatar
Heinz Mauelshagen committed
347
	if (!strcasecmp("far", format))
348
349
350
		return 0x20000 | (f << 8) | n;

	return (f << 8) | n;
351
352
}

353
static struct raid_type *get_raid_type(const char *name)
NeilBrown's avatar
NeilBrown committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
{
	int i;

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

	return NULL;
}

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

369
370
	if (raid_devs <= raid_type->parity_devs)
		return ERR_PTR(ti_error_einval(ti, "Insufficient number of devices"));
NeilBrown's avatar
NeilBrown committed
371
372

	rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
373
374
	if (!rs)
		return ERR_PTR(ti_error_ret(ti, "Cannot allocate raid context", -ENOMEM));
NeilBrown's avatar
NeilBrown committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

	mddev_init(&rs->md);

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

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

	/*
	 * Remaining items to be initialized by further RAID params:
	 *  rs->md.persistent
	 *  rs->md.external
	 *  rs->md.chunk_sectors
	 *  rs->md.new_chunk_sectors
397
	 *  rs->md.dev_sectors
NeilBrown's avatar
NeilBrown committed
398
399
400
401
402
403
404
405
406
	 */

	return rs;
}

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

407
408
409
	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);
410
		md_rdev_clear(&rs->dev[i].rdev);
NeilBrown's avatar
NeilBrown committed
411
412
		if (rs->dev[i].data_dev)
			dm_put_device(rs->ti, rs->dev[i].data_dev);
413
	}
NeilBrown's avatar
NeilBrown committed
414
415
416
417
418
419
420
421
422

	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
 *
423
424
425
426
427
428
429
430
431
432
 * 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
433
 */
434
static int parse_dev_params(struct raid_set *rs, struct dm_arg_set *as)
NeilBrown's avatar
NeilBrown committed
435
436
437
438
{
	int i;
	int rebuild = 0;
	int metadata_available = 0;
439
	int r = 0;
440
	const char *arg;
NeilBrown's avatar
NeilBrown committed
441

442
443
444
445
446
447
	/* 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
448
449
450
451
452
453
454
455
456
457
458
459
		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;

460
461
462
463
464
465
		arg = dm_shift_arg(as);
		if (!arg)
			return -EINVAL;

		if (strcmp(arg, "-")) {
			r = dm_get_device(rs->ti, arg,
466
467
					    dm_table_get_mode(rs->ti->table),
					    &rs->dev[i].meta_dev);
468
			if (r)
469
				return ti_error_ret(rs->ti, "RAID metadata device lookup failure", r);
470
471
472

			rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
			if (!rs->dev[i].rdev.sb_page)
473
				return ti_error_ret(rs->ti, "Failed to allocate superblock page", -ENOMEM);
NeilBrown's avatar
NeilBrown committed
474
475
		}

476
477
478
479
480
		arg = dm_shift_arg(as);
		if (!arg)
			return -EINVAL;

		if (!strcmp(arg, "-")) {
NeilBrown's avatar
NeilBrown committed
481
			if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
482
483
			    (!rs->dev[i].rdev.recovery_offset))
				return ti_error_einval(rs->ti, "Drive designated for rebuild not specified");
NeilBrown's avatar
NeilBrown committed
484

485
			if (rs->dev[i].meta_dev)
486
				return ti_error_einval(rs->ti, "No data device supplied with metadata device");
487

NeilBrown's avatar
NeilBrown committed
488
489
490
			continue;
		}

491
		r = dm_get_device(rs->ti, arg,
NeilBrown's avatar
NeilBrown committed
492
493
				    dm_table_get_mode(rs->ti->table),
				    &rs->dev[i].data_dev);
494
495
		if (r)
			return ti_error_ret(rs->ti, "RAID device lookup failure", r);
NeilBrown's avatar
NeilBrown committed
496

497
498
499
500
		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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
		rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
		list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
		if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
			rebuild++;
	}

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

	return 0;
}

530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
 * validate_region_size
 * @rs
 * @region_size:  region size in sectors.  If 0, pick a size (4MiB default).
 *
 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
 *
 * Returns: 0 on success, -EINVAL on failure.
 */
static int validate_region_size(struct raid_set *rs, unsigned long region_size)
{
	unsigned long min_region_size = rs->ti->len / (1 << 21);

	if (!region_size) {
		/*
		 * Choose a reasonable default.  All figures in sectors.
		 */
		if (min_region_size > (1 << 13)) {
549
			/* If not a power of 2, make it the next power of 2 */
550
			region_size = roundup_pow_of_two(min_region_size);
551
552
553
554
555
556
557
558
559
560
			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.
		 */
561
562
		if (region_size > rs->ti->len)
			return ti_error_einval(rs->ti, "Supplied region size is too large");
563
564
565
566

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

570
571
		if (!is_power_of_2(region_size))
			return ti_error_einval(rs->ti, "Region size is not a power of 2");
572

573
574
		if (region_size < rs->md.chunk_sectors)
			return ti_error_einval(rs->ti, "Region size is smaller than the chunk size");
575
576
577
578
579
580
581
582
583
584
	}

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

	return 0;
}

585
/*
586
 * validate_raid_redundancy
587
588
 * @rs
 *
589
590
 * Determine if there are enough devices in the array that haven't
 * failed (or are being rebuilt) to form a usable array.
591
592
593
 *
 * Returns: 0 on success, -EINVAL on failure.
 */
594
static int validate_raid_redundancy(struct raid_set *rs)
595
596
{
	unsigned i, rebuild_cnt = 0;
597
	unsigned rebuilds_per_group = 0, copies, d;
598
	unsigned group_size, last_group_start;
599
600

	for (i = 0; i < rs->md.raid_disks; i++)
601
602
		if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
		    !rs->dev[i].rdev.sb_page)
603
604
605
606
607
608
609
610
611
612
613
614
615
616
			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:
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
		copies = raid10_md_layout_to_copies(rs->md.layout);
		if (rebuild_cnt < copies)
			break;

		/*
		 * It is possible to have a higher rebuild count for RAID10,
		 * as long as the failed devices occur in different mirror
		 * groups (i.e. different stripes).
		 *
		 * When checking "near" format, make sure no adjacent devices
		 * have failed beyond what can be handled.  In addition to the
		 * simple case where the number of devices is a multiple of the
		 * number of copies, we must also handle cases where the number
		 * of devices is not a multiple of the number of copies.
		 * E.g.    dev1 dev2 dev3 dev4 dev5
		 *          A    A    B    B    C
		 *          C    D    D    E    E
		 */
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
		if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
			for (i = 0; i < rs->md.raid_disks * copies; i++) {
				if (!(i % copies))
					rebuilds_per_group = 0;
				d = i % rs->md.raid_disks;
				if ((!rs->dev[d].rdev.sb_page ||
				     !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
				    (++rebuilds_per_group >= copies))
					goto too_many;
			}
			break;
		}

		/*
		 * When checking "far" and "offset" formats, we need to ensure
		 * that the device that holds its copy is not also dead or
		 * being rebuilt.  (Note that "far" and "offset" formats only
		 * support two copies right now.  These formats also only ever
		 * use the 'use_far_sets' variant.)
		 *
		 * This check is somewhat complicated by the need to account
		 * for arrays that are not a multiple of (far) copies.  This
		 * results in the need to treat the last (potentially larger)
		 * set differently.
		 */
		group_size = (rs->md.raid_disks / copies);
		last_group_start = (rs->md.raid_disks / group_size) - 1;
		last_group_start *= group_size;
		for (i = 0; i < rs->md.raid_disks; i++) {
			if (!(i % copies) && !(i > last_group_start))
665
				rebuilds_per_group = 0;
666
667
			if ((!rs->dev[i].rdev.sb_page ||
			     !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
668
			    (++rebuilds_per_group >= copies))
669
					goto too_many;
670
671
		}
		break;
672
	default:
673
674
		if (rebuild_cnt)
			return -EINVAL;
675
676
677
678
679
680
681
682
	}

	return 0;

too_many:
	return -EINVAL;
}

NeilBrown's avatar
NeilBrown committed
683
684
685
686
/*
 * Possible arguments are...
 *	<chunk_size> [optional_args]
 *
687
688
689
690
691
 * Argument definitions
 *    <chunk_size>			The number of sectors per disk that
 *                                      will form the "stripe"
 *    [[no]sync]			Force or prevent recovery of the
 *                                      entire array
NeilBrown's avatar
NeilBrown committed
692
 *    [rebuild <idx>]			Rebuild the drive indicated by the index
693
694
 *    [daemon_sleep <ms>]		Time between bitmap daemon work to
 *                                      clear bits
NeilBrown's avatar
NeilBrown committed
695
696
 *    [min_recovery_rate <kB/sec/disk>]	Throttle RAID initialization
 *    [max_recovery_rate <kB/sec/disk>]	Throttle RAID initialization
697
 *    [write_mostly <idx>]		Indicate a write mostly drive via index
NeilBrown's avatar
NeilBrown committed
698
699
 *    [max_write_behind <sectors>]	See '-write-behind=' (man mdadm)
 *    [stripe_cache <sectors>]		Stripe cache size for higher RAIDs
700
 *    [region_size <sectors>]           Defines granularity of bitmap
701
702
703
 *
 * RAID10-only options:
 *    [raid10_copies <# copies>]        Number of copies.  (Default: 2)
704
 *    [raid10_format <near|far|offset>] Layout algorithm.  (Default: near)
NeilBrown's avatar
NeilBrown committed
705
 */
706
static int parse_raid_params(struct raid_set *rs, struct dm_arg_set *as,
NeilBrown's avatar
NeilBrown committed
707
708
			     unsigned num_raid_params)
{
709
710
	char *raid10_format = "near";
	unsigned raid10_copies = 2;
711
	unsigned i;
712
	unsigned value, region_size = 0;
713
	sector_t sectors_per_dev = rs->ti->len;
714
	sector_t max_io_len;
715
	const char *arg, *key;
716
	struct raid_dev *rd;
717
718
719
720

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

721
722
	if (kstrtouint(arg, 10, &value) < 0)
		return ti_error_einval(rs->ti, "Bad numerical argument given for chunk_size");
NeilBrown's avatar
NeilBrown committed
723
724
725

	/*
	 * First, parse the in-order required arguments
726
	 * "chunk_size" is the only argument of this type.
NeilBrown's avatar
NeilBrown committed
727
	 */
728
	if (rs->raid_type->level == 1) {
729
730
731
		if (value)
			DMERR("Ignoring chunk size parameter for RAID 1");
		value = 0;
732
733
734
735
	} else if (!is_power_of_2(value))
		return ti_error_einval(rs->ti, "Chunk size must be a power of 2");
	else if (value < 8)
		return ti_error_einval(rs->ti, "Chunk size value is too small");
NeilBrown's avatar
NeilBrown committed
736
737
738
739

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

	/*
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
	 * We set each individual device as In_sync with a completed
	 * 'recovery_offset'.  If there has been a device failure or
	 * replacement then one of the following cases applies:
	 *
	 *   1) User specifies 'rebuild'.
	 *      - Device is reset when param is read.
	 *   2) A new device is supplied.
	 *      - No matching superblock found, resets device.
	 *   3) Device failure was transient and returns on reload.
	 *      - Failure noticed, resets device for bitmap replay.
	 *   4) Device hadn't completed recovery after previous failure.
	 *      - Superblock is read and overrides recovery_offset.
	 *
	 * What is found in the superblocks of the devices is always
	 * authoritative, unless 'rebuild' or '[no]sync' was specified.
NeilBrown's avatar
NeilBrown committed
755
	 */
756
	for (i = 0; i < rs->md.raid_disks; i++) {
NeilBrown's avatar
NeilBrown committed
757
		set_bit(In_sync, &rs->dev[i].rdev.flags);
758
759
		rs->dev[i].rdev.recovery_offset = MaxSector;
	}
NeilBrown's avatar
NeilBrown committed
760

761
762
763
	/*
	 * Second, parse the unordered optional arguments
	 */
NeilBrown's avatar
NeilBrown committed
764
	for (i = 0; i < num_raid_params; i++) {
765
		arg = dm_shift_arg(as);
766
767
		if (!arg)
			return ti_error_einval(rs->ti, "Not enough raid parameters given");
768
769

		if (!strcasecmp(arg, "nosync")) {
NeilBrown's avatar
NeilBrown committed
770
			rs->md.recovery_cp = MaxSector;
771
			_set_flag(CTR_FLAG_NOSYNC, &rs->ctr_flags);
NeilBrown's avatar
NeilBrown committed
772
773
			continue;
		}
774
		if (!strcasecmp(arg, "sync")) {
NeilBrown's avatar
NeilBrown committed
775
			rs->md.recovery_cp = 0;
776
			_set_flag(CTR_FLAG_SYNC, &rs->ctr_flags);
NeilBrown's avatar
NeilBrown committed
777
778
779
			continue;
		}

780
781
782
		key = arg;
		arg = dm_shift_arg(as);
		i++; /* Account for the argument pairs */
783
784
		if (!arg)
			return ti_error_einval(rs->ti, "Wrong number of raid parameters given");
785

786
787
788
789
790
791
792
793
794
		/*
		 * Parameters that take a string value are checked here.
		 */

		if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_RAID10_FORMAT))) {
			if (_test_and_set_flag(CTR_FLAG_RAID10_FORMAT, &rs->ctr_flags))
				return ti_error_einval(rs->ti, "Only one raid10_format argument pair allowed");
			if (rs->raid_type->level != 10)
				return ti_error_einval(rs->ti, "'raid10_format' is an invalid parameter for this RAID type");
795
796
			if (strcmp("near", arg) &&
			    strcmp("far", arg) &&
797
798
799
			    strcmp("offset", arg))
				return ti_error_einval(rs->ti, "Invalid 'raid10_format' value given");

800
			raid10_format = (char *) arg;
801
802
803
			continue;
		}

804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
		if (kstrtouint(arg, 10, &value) < 0)
			return ti_error_einval(rs->ti, "Bad numerical argument given in raid params");

		if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_REBUILD))) {
			/*
			 * "rebuild" is being passed in by userspace to provide
			 * indexes of replaced devices and to set up additional
			 * devices on raid level takeover.
 			 */
			if (!_in_range(value, 0, rs->md.raid_disks - 1))
				return ti_error_einval(rs->ti, "Invalid rebuild index given");

			rd = rs->dev + value;
			clear_bit(In_sync, &rd->rdev.flags);
			clear_bit(Faulty, &rd->rdev.flags);
			rd->rdev.recovery_offset = 0;
			_set_flag(CTR_FLAG_REBUILD, &rs->ctr_flags);
		} else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_WRITE_MOSTLY))) {
			if (rs->raid_type->level != 1)
				return ti_error_einval(rs->ti, "write_mostly option is only valid for RAID1");

			if (!_in_range(value, 0, rs->md.raid_disks - 1))
				return ti_error_einval(rs->ti, "Invalid write_mostly index given");
NeilBrown's avatar
NeilBrown committed
827

828
			set_bit(WriteMostly, &rs->dev[value].rdev.flags);
829
830
831
832
833
834
835
			_set_flag(CTR_FLAG_WRITE_MOSTLY, &rs->ctr_flags);
		} else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MAX_WRITE_BEHIND))) {
			if (rs->raid_type->level != 1)
				return ti_error_einval(rs->ti, "max_write_behind option is only valid for RAID1");

			if (_test_and_set_flag(CTR_FLAG_MAX_WRITE_BEHIND, &rs->ctr_flags))
				return ti_error_einval(rs->ti, "Only one max_write_behind argument pair allowed");
NeilBrown's avatar
NeilBrown committed
836
837
838
839
840
841

			/*
			 * In device-mapper, we specify things in sectors, but
			 * MD records this value in kB
			 */
			value /= 2;
842
843
844
			if (value > COUNTER_MAX)
				return ti_error_einval(rs->ti, "Max write-behind limit out of range");

NeilBrown's avatar
NeilBrown committed
845
			rs->md.bitmap_info.max_write_behind = value;
846
847
848
849
850
		} else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_DAEMON_SLEEP))) {
			if (_test_and_set_flag(CTR_FLAG_DAEMON_SLEEP, &rs->ctr_flags))
				return ti_error_einval(rs->ti, "Only one daemon_sleep argument pair allowed");
			if (!value || (value > MAX_SCHEDULE_TIMEOUT))
				return ti_error_einval(rs->ti, "daemon sleep period out of range");
NeilBrown's avatar
NeilBrown committed
851
			rs->md.bitmap_info.daemon_sleep = value;
852
853
854
		} else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_STRIPE_CACHE))) {
			if (_test_and_set_flag(CTR_FLAG_STRIPE_CACHE, &rs->ctr_flags))
				return ti_error_einval(rs->ti, "Only one stripe_cache argument pair allowed");
NeilBrown's avatar
NeilBrown committed
855
856
857
858
859
860
			/*
			 * In device-mapper, we specify things in sectors, but
			 * MD records this value in kB
			 */
			value /= 2;

861
862
863
864
865
866
867
868
869
870
			if (!_in_range(rs->raid_type->level, 4, 6))
				return ti_error_einval(rs->ti, "Inappropriate argument: stripe_cache");
			if (raid5_set_cache_size(&rs->md, (int)value))
				return ti_error_einval(rs->ti, "Bad stripe_cache size");

		} else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MIN_RECOVERY_RATE))) {
			if (_test_and_set_flag(CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
				return ti_error_einval(rs->ti, "Only one min_recovery_rate argument pair allowed");
			if (value > INT_MAX)
				return ti_error_einval(rs->ti, "min_recovery_rate out of range");
NeilBrown's avatar
NeilBrown committed
871
			rs->md.sync_speed_min = (int)value;
872
873
874
875
876
		} else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_MAX_RECOVERY_RATE))) {
			if (_test_and_set_flag(CTR_FLAG_MIN_RECOVERY_RATE, &rs->ctr_flags))
				return ti_error_einval(rs->ti, "Only one max_recovery_rate argument pair allowed");
			if (value > INT_MAX)
				return ti_error_einval(rs->ti, "max_recovery_rate out of range");
NeilBrown's avatar
NeilBrown committed
877
			rs->md.sync_speed_max = (int)value;
878
879
880
881
		} else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_REGION_SIZE))) {
			if (_test_and_set_flag(CTR_FLAG_REGION_SIZE, &rs->ctr_flags))
				return ti_error_einval(rs->ti, "Only one region_size argument pair allowed");

882
			region_size = value;
883
884
885
886
887
888
889
		} else if (!strcasecmp(key, _argname_by_flag(CTR_FLAG_RAID10_COPIES))) {
			if (_test_and_set_flag(CTR_FLAG_RAID10_COPIES, &rs->ctr_flags))
				return ti_error_einval(rs->ti, "Only one raid10_copies argument pair allowed");

			if (!_in_range(value, 2, rs->md.raid_disks))
				return ti_error_einval(rs->ti, "Bad value for 'raid10_copies'");

890
			raid10_copies = value;
NeilBrown's avatar
NeilBrown committed
891
892
		} else {
			DMERR("Unable to parse RAID parameter: %s", key);
893
			return ti_error_einval(rs->ti, "Unable to parse RAID parameters");
NeilBrown's avatar
NeilBrown committed
894
895
896
		}
	}

897
898
899
900
	if (validate_region_size(rs, region_size))
		return -EINVAL;

	if (rs->md.chunk_sectors)
901
		max_io_len = rs->md.chunk_sectors;
902
	else
903
		max_io_len = region_size;
904

905
906
	if (dm_set_target_max_io_len(rs->ti, max_io_len))
		return -EINVAL;
907

908
	if (rs->raid_type->level == 10) {
909
910
		if (raid10_copies > rs->md.raid_disks)
			return ti_error_einval(rs->ti, "Not enough devices to satisfy specification");
911

912
913
914
915
		/*
		 * If the format is not "near", we only support
		 * two copies at the moment.
		 */
916
917
		if (strcmp("near", raid10_format) && (raid10_copies > 2))
			return ti_error_einval(rs->ti, "Too many copies for given RAID10 format.");
918

919
920
921
922
923
924
925
		/* (Len * #mirrors) / #devices */
		sectors_per_dev = rs->ti->len * raid10_copies;
		sector_div(sectors_per_dev, rs->md.raid_disks);

		rs->md.layout = raid10_format_to_md_layout(raid10_format,
							   raid10_copies);
		rs->md.new_layout = rs->md.layout;
926
	} else if ((!rs->raid_type->level || rs->raid_type->level > 1) &&
927
		   sector_div(sectors_per_dev,
928
929
930
			      (rs->md.raid_disks - rs->raid_type->parity_devs)))
		return ti_error_einval(rs->ti, "Target length not divisible by number of data devices");

931
932
	rs->md.dev_sectors = sectors_per_dev;

NeilBrown's avatar
NeilBrown committed
933
934
935
936
	/* Assume there are no metadata devices until the drives are parsed */
	rs->md.persistent = 0;
	rs->md.external = 1;

937
938
	/* Check, if any invalid ctr arguments have been passed in for the raid level */
	return rs_check_for_invalid_flags(rs);
NeilBrown's avatar
NeilBrown committed
939
940
941
942
943
944
945
946
947
948
949
950
951
}

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

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

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

952
	return mddev_congested(&rs->md, bits);
NeilBrown's avatar
NeilBrown committed
953
954
}

955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/*
 * This structure is never routinely used by userspace, unlike md superblocks.
 * Devices with this superblock should only ever be accessed via device-mapper.
 */
#define DM_RAID_MAGIC 0x64526D44
struct dm_raid_superblock {
	__le32 magic;		/* "DmRd" */
	__le32 features;	/* Used to indicate possible future changes */

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

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

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

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

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

989
	/* Remainder of a logical block is zero-filled when writing (see super_sync()). */
990
991
} __packed;

992
static int read_disk_sb(struct md_rdev *rdev, int size)
993
994
995
996
997
998
{
	BUG_ON(!rdev->sb_page);

	if (rdev->sb_loaded)
		return 0;

Mike Christie's avatar
Mike Christie committed
999
	if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, 1)) {
1000
		DMERR("Failed to read superblock of device at position %d",