blob: 030e2d6bc261434ba2747bbae9aab07f16f278b3 [file] [log] [blame]
NeilBrown9d09e662011-01-13 20:00:02 +00001/*
2 * Copyright (C) 2010-2011 Neil Brown
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02003 * Copyright (C) 2010-2014 Red Hat, Inc. All rights reserved.
NeilBrown9d09e662011-01-13 20:00:02 +00004 *
5 * This file is released under the GPL.
6 */
7
8#include <linux/slab.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -04009#include <linux/module.h>
NeilBrown9d09e662011-01-13 20:00:02 +000010
11#include "md.h"
Jonathan Brassow32737272011-08-02 12:32:07 +010012#include "raid1.h"
NeilBrown9d09e662011-01-13 20:00:02 +000013#include "raid5.h"
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -050014#include "raid10.h"
NeilBrown9d09e662011-01-13 20:00:02 +000015#include "bitmap.h"
16
Alasdair G Kergon3e8dbb72011-08-02 12:32:03 +010017#include <linux/device-mapper.h>
18
NeilBrown9d09e662011-01-13 20:00:02 +000019#define DM_MSG_PREFIX "raid"
20
21/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +010022 * The following flags are used by dm-raid.c to set up the array state.
23 * They must be cleared before md_run is called.
NeilBrown9d09e662011-01-13 20:00:02 +000024 */
Jonathan Brassowb12d4372011-08-02 12:32:07 +010025#define FirstUse 10 /* rdev flag */
NeilBrown9d09e662011-01-13 20:00:02 +000026
27struct raid_dev {
28 /*
29 * Two DM devices, one to hold metadata and one to hold the
30 * actual data/parity. The reason for this is to not confuse
31 * ti->len and give more flexibility in altering size and
32 * characteristics.
33 *
34 * While it is possible for this device to be associated
35 * with a different physical device than the data_dev, it
36 * is intended for it to be the same.
37 * |--------- Physical Device ---------|
38 * |- meta_dev -|------ data_dev ------|
39 */
40 struct dm_dev *meta_dev;
41 struct dm_dev *data_dev;
NeilBrown3cb03002011-10-11 16:45:26 +110042 struct md_rdev rdev;
NeilBrown9d09e662011-01-13 20:00:02 +000043};
44
45/*
46 * Flags for rs->print_flags field.
47 */
Jonathan Brassow13c87582011-08-02 12:32:03 +010048#define DMPF_SYNC 0x1
49#define DMPF_NOSYNC 0x2
50#define DMPF_REBUILD 0x4
51#define DMPF_DAEMON_SLEEP 0x8
52#define DMPF_MIN_RECOVERY_RATE 0x10
53#define DMPF_MAX_RECOVERY_RATE 0x20
54#define DMPF_MAX_WRITE_BEHIND 0x40
55#define DMPF_STRIPE_CACHE 0x80
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -050056#define DMPF_REGION_SIZE 0x100
57#define DMPF_RAID10_COPIES 0x200
58#define DMPF_RAID10_FORMAT 0x400
59
NeilBrown9d09e662011-01-13 20:00:02 +000060struct raid_set {
61 struct dm_target *ti;
62
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -060063 uint32_t bitmap_loaded;
64 uint32_t print_flags;
NeilBrown9d09e662011-01-13 20:00:02 +000065
NeilBrownfd01b882011-10-11 16:47:53 +110066 struct mddev md;
NeilBrown9d09e662011-01-13 20:00:02 +000067 struct raid_type *raid_type;
68 struct dm_target_callbacks callbacks;
69
70 struct raid_dev dev[0];
71};
72
73/* Supported raid types and properties. */
74static struct raid_type {
75 const char *name; /* RAID algorithm. */
76 const char *descr; /* Descriptor text for logging. */
77 const unsigned parity_devs; /* # of parity devices. */
78 const unsigned minimal_devs; /* minimal # of devices in set. */
79 const unsigned level; /* RAID level. */
80 const unsigned algorithm; /* RAID algorithm. */
81} raid_types[] = {
Jonathan Brassow32737272011-08-02 12:32:07 +010082 {"raid1", "RAID1 (mirroring)", 0, 2, 1, 0 /* NONE */},
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -050083 {"raid10", "RAID10 (striped mirrors)", 0, 2, 10, UINT_MAX /* Varies */},
NeilBrown9d09e662011-01-13 20:00:02 +000084 {"raid4", "RAID4 (dedicated parity disk)", 1, 2, 5, ALGORITHM_PARITY_0},
85 {"raid5_la", "RAID5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
86 {"raid5_ra", "RAID5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
87 {"raid5_ls", "RAID5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
88 {"raid5_rs", "RAID5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
89 {"raid6_zr", "RAID6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
90 {"raid6_nr", "RAID6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
91 {"raid6_nc", "RAID6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
92};
93
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +110094static char *raid10_md_layout_to_format(int layout)
95{
96 /*
97 * Bit 16 and 17 stand for "offset" and "use_far_sets"
98 * Refer to MD's raid10.c for details
99 */
100 if ((layout & 0x10000) && (layout & 0x20000))
101 return "offset";
102
103 if ((layout & 0xFF) > 1)
104 return "near";
105
106 return "far";
107}
108
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500109static unsigned raid10_md_layout_to_copies(int layout)
110{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100111 if ((layout & 0xFF) > 1)
112 return layout & 0xFF;
113 return (layout >> 8) & 0xFF;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500114}
115
116static int raid10_format_to_md_layout(char *format, unsigned copies)
117{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100118 unsigned n = 1, f = 1;
119
120 if (!strcmp("near", format))
121 n = copies;
122 else
123 f = copies;
124
125 if (!strcmp("offset", format))
126 return 0x30000 | (f << 8) | n;
127
128 if (!strcmp("far", format))
129 return 0x20000 | (f << 8) | n;
130
131 return (f << 8) | n;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500132}
133
NeilBrown9d09e662011-01-13 20:00:02 +0000134static struct raid_type *get_raid_type(char *name)
135{
136 int i;
137
138 for (i = 0; i < ARRAY_SIZE(raid_types); i++)
139 if (!strcmp(raid_types[i].name, name))
140 return &raid_types[i];
141
142 return NULL;
143}
144
145static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
146{
147 unsigned i;
148 struct raid_set *rs;
NeilBrown9d09e662011-01-13 20:00:02 +0000149
150 if (raid_devs <= raid_type->parity_devs) {
151 ti->error = "Insufficient number of devices";
152 return ERR_PTR(-EINVAL);
153 }
154
NeilBrown9d09e662011-01-13 20:00:02 +0000155 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
156 if (!rs) {
157 ti->error = "Cannot allocate raid context";
158 return ERR_PTR(-ENOMEM);
159 }
160
161 mddev_init(&rs->md);
162
163 rs->ti = ti;
164 rs->raid_type = raid_type;
165 rs->md.raid_disks = raid_devs;
166 rs->md.level = raid_type->level;
167 rs->md.new_level = rs->md.level;
NeilBrown9d09e662011-01-13 20:00:02 +0000168 rs->md.layout = raid_type->algorithm;
169 rs->md.new_layout = rs->md.layout;
170 rs->md.delta_disks = 0;
171 rs->md.recovery_cp = 0;
172
173 for (i = 0; i < raid_devs; i++)
174 md_rdev_init(&rs->dev[i].rdev);
175
176 /*
177 * Remaining items to be initialized by further RAID params:
178 * rs->md.persistent
179 * rs->md.external
180 * rs->md.chunk_sectors
181 * rs->md.new_chunk_sectors
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100182 * rs->md.dev_sectors
NeilBrown9d09e662011-01-13 20:00:02 +0000183 */
184
185 return rs;
186}
187
188static void context_free(struct raid_set *rs)
189{
190 int i;
191
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100192 for (i = 0; i < rs->md.raid_disks; i++) {
193 if (rs->dev[i].meta_dev)
194 dm_put_device(rs->ti, rs->dev[i].meta_dev);
NeilBrown545c8792012-05-22 13:54:30 +1000195 md_rdev_clear(&rs->dev[i].rdev);
NeilBrown9d09e662011-01-13 20:00:02 +0000196 if (rs->dev[i].data_dev)
197 dm_put_device(rs->ti, rs->dev[i].data_dev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100198 }
NeilBrown9d09e662011-01-13 20:00:02 +0000199
200 kfree(rs);
201}
202
203/*
204 * For every device we have two words
205 * <meta_dev>: meta device name or '-' if missing
206 * <data_dev>: data device name or '-' if missing
207 *
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100208 * The following are permitted:
209 * - -
210 * - <data_dev>
211 * <meta_dev> <data_dev>
212 *
213 * The following is not allowed:
214 * <meta_dev> -
215 *
216 * This code parses those words. If there is a failure,
217 * the caller must use context_free to unwind the operations.
NeilBrown9d09e662011-01-13 20:00:02 +0000218 */
219static int dev_parms(struct raid_set *rs, char **argv)
220{
221 int i;
222 int rebuild = 0;
223 int metadata_available = 0;
224 int ret = 0;
225
226 for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {
227 rs->dev[i].rdev.raid_disk = i;
228
229 rs->dev[i].meta_dev = NULL;
230 rs->dev[i].data_dev = NULL;
231
232 /*
233 * There are no offsets, since there is a separate device
234 * for data and metadata.
235 */
236 rs->dev[i].rdev.data_offset = 0;
237 rs->dev[i].rdev.mddev = &rs->md;
238
239 if (strcmp(argv[0], "-")) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100240 ret = dm_get_device(rs->ti, argv[0],
241 dm_table_get_mode(rs->ti->table),
242 &rs->dev[i].meta_dev);
243 rs->ti->error = "RAID metadata device lookup failure";
244 if (ret)
245 return ret;
246
247 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
248 if (!rs->dev[i].rdev.sb_page)
249 return -ENOMEM;
NeilBrown9d09e662011-01-13 20:00:02 +0000250 }
251
252 if (!strcmp(argv[1], "-")) {
253 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
254 (!rs->dev[i].rdev.recovery_offset)) {
255 rs->ti->error = "Drive designated for rebuild not specified";
256 return -EINVAL;
257 }
258
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100259 rs->ti->error = "No data device supplied with metadata device";
260 if (rs->dev[i].meta_dev)
261 return -EINVAL;
262
NeilBrown9d09e662011-01-13 20:00:02 +0000263 continue;
264 }
265
266 ret = dm_get_device(rs->ti, argv[1],
267 dm_table_get_mode(rs->ti->table),
268 &rs->dev[i].data_dev);
269 if (ret) {
270 rs->ti->error = "RAID device lookup failure";
271 return ret;
272 }
273
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100274 if (rs->dev[i].meta_dev) {
275 metadata_available = 1;
276 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
277 }
NeilBrown9d09e662011-01-13 20:00:02 +0000278 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
279 list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
280 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
281 rebuild++;
282 }
283
284 if (metadata_available) {
285 rs->md.external = 0;
286 rs->md.persistent = 1;
287 rs->md.major_version = 2;
288 } else if (rebuild && !rs->md.recovery_cp) {
289 /*
290 * Without metadata, we will not be able to tell if the array
291 * is in-sync or not - we must assume it is not. Therefore,
292 * it is impossible to rebuild a drive.
293 *
294 * Even if there is metadata, the on-disk information may
295 * indicate that the array is not in-sync and it will then
296 * fail at that time.
297 *
298 * User could specify 'nosync' option if desperate.
299 */
300 DMERR("Unable to rebuild drive while array is not in-sync");
301 rs->ti->error = "RAID device lookup failure";
302 return -EINVAL;
303 }
304
305 return 0;
306}
307
308/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100309 * validate_region_size
310 * @rs
311 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
312 *
313 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
314 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
315 *
316 * Returns: 0 on success, -EINVAL on failure.
317 */
318static int validate_region_size(struct raid_set *rs, unsigned long region_size)
319{
320 unsigned long min_region_size = rs->ti->len / (1 << 21);
321
322 if (!region_size) {
323 /*
324 * Choose a reasonable default. All figures in sectors.
325 */
326 if (min_region_size > (1 << 13)) {
Jonathan Brassow3a0f9aa2012-12-21 20:23:33 +0000327 /* If not a power of 2, make it the next power of 2 */
328 if (min_region_size & (min_region_size - 1))
329 region_size = 1 << fls(region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100330 DMINFO("Choosing default region size of %lu sectors",
331 region_size);
Jonathan Brassowc1084562011-08-02 12:32:07 +0100332 } else {
333 DMINFO("Choosing default region size of 4MiB");
334 region_size = 1 << 13; /* sectors */
335 }
336 } else {
337 /*
338 * Validate user-supplied value.
339 */
340 if (region_size > rs->ti->len) {
341 rs->ti->error = "Supplied region size is too large";
342 return -EINVAL;
343 }
344
345 if (region_size < min_region_size) {
346 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
347 region_size, min_region_size);
348 rs->ti->error = "Supplied region size is too small";
349 return -EINVAL;
350 }
351
352 if (!is_power_of_2(region_size)) {
353 rs->ti->error = "Region size is not a power of 2";
354 return -EINVAL;
355 }
356
357 if (region_size < rs->md.chunk_sectors) {
358 rs->ti->error = "Region size is smaller than the chunk size";
359 return -EINVAL;
360 }
361 }
362
363 /*
364 * Convert sectors to bytes.
365 */
366 rs->md.bitmap_info.chunksize = (region_size << 9);
367
368 return 0;
369}
370
371/*
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600372 * validate_raid_redundancy
Jonathan Brassoweb649122012-10-11 13:40:09 +1100373 * @rs
374 *
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600375 * Determine if there are enough devices in the array that haven't
376 * failed (or are being rebuilt) to form a usable array.
Jonathan Brassoweb649122012-10-11 13:40:09 +1100377 *
378 * Returns: 0 on success, -EINVAL on failure.
379 */
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600380static int validate_raid_redundancy(struct raid_set *rs)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100381{
382 unsigned i, rebuild_cnt = 0;
NeilBrown3f6bbd32013-05-09 10:27:49 +1000383 unsigned rebuilds_per_group = 0, copies, d;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100384 unsigned group_size, last_group_start;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100385
Jonathan Brassoweb649122012-10-11 13:40:09 +1100386 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600387 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
388 !rs->dev[i].rdev.sb_page)
Jonathan Brassoweb649122012-10-11 13:40:09 +1100389 rebuild_cnt++;
390
391 switch (rs->raid_type->level) {
392 case 1:
393 if (rebuild_cnt >= rs->md.raid_disks)
394 goto too_many;
395 break;
396 case 4:
397 case 5:
398 case 6:
399 if (rebuild_cnt > rs->raid_type->parity_devs)
400 goto too_many;
401 break;
402 case 10:
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100403 copies = raid10_md_layout_to_copies(rs->md.layout);
404 if (rebuild_cnt < copies)
405 break;
406
407 /*
408 * It is possible to have a higher rebuild count for RAID10,
409 * as long as the failed devices occur in different mirror
410 * groups (i.e. different stripes).
411 *
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100412 * When checking "near" format, make sure no adjacent devices
413 * have failed beyond what can be handled. In addition to the
414 * simple case where the number of devices is a multiple of the
415 * number of copies, we must also handle cases where the number
416 * of devices is not a multiple of the number of copies.
417 * E.g. dev1 dev2 dev3 dev4 dev5
418 * A A B B C
419 * C D D E E
420 */
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100421 if (!strcmp("near", raid10_md_layout_to_format(rs->md.layout))) {
422 for (i = 0; i < rs->md.raid_disks * copies; i++) {
423 if (!(i % copies))
424 rebuilds_per_group = 0;
425 d = i % rs->md.raid_disks;
426 if ((!rs->dev[d].rdev.sb_page ||
427 !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
428 (++rebuilds_per_group >= copies))
429 goto too_many;
430 }
431 break;
432 }
433
434 /*
435 * When checking "far" and "offset" formats, we need to ensure
436 * that the device that holds its copy is not also dead or
437 * being rebuilt. (Note that "far" and "offset" formats only
438 * support two copies right now. These formats also only ever
439 * use the 'use_far_sets' variant.)
440 *
441 * This check is somewhat complicated by the need to account
442 * for arrays that are not a multiple of (far) copies. This
443 * results in the need to treat the last (potentially larger)
444 * set differently.
445 */
446 group_size = (rs->md.raid_disks / copies);
447 last_group_start = (rs->md.raid_disks / group_size) - 1;
448 last_group_start *= group_size;
449 for (i = 0; i < rs->md.raid_disks; i++) {
450 if (!(i % copies) && !(i > last_group_start))
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600451 rebuilds_per_group = 0;
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100452 if ((!rs->dev[i].rdev.sb_page ||
453 !test_bit(In_sync, &rs->dev[i].rdev.flags)) &&
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100454 (++rebuilds_per_group >= copies))
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100455 goto too_many;
Jonathan Brassow4ec1e362012-10-11 13:40:24 +1100456 }
457 break;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100458 default:
Jonathan Brassow55ebbb52013-01-22 21:42:18 -0600459 if (rebuild_cnt)
460 return -EINVAL;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100461 }
462
463 return 0;
464
465too_many:
Jonathan Brassoweb649122012-10-11 13:40:09 +1100466 return -EINVAL;
467}
468
469/*
NeilBrown9d09e662011-01-13 20:00:02 +0000470 * Possible arguments are...
NeilBrown9d09e662011-01-13 20:00:02 +0000471 * <chunk_size> [optional_args]
472 *
Jonathan Brassow32737272011-08-02 12:32:07 +0100473 * Argument definitions
474 * <chunk_size> The number of sectors per disk that
475 * will form the "stripe"
476 * [[no]sync] Force or prevent recovery of the
477 * entire array
NeilBrown9d09e662011-01-13 20:00:02 +0000478 * [rebuild <idx>] Rebuild the drive indicated by the index
Jonathan Brassow32737272011-08-02 12:32:07 +0100479 * [daemon_sleep <ms>] Time between bitmap daemon work to
480 * clear bits
NeilBrown9d09e662011-01-13 20:00:02 +0000481 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
482 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100483 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +0000484 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
485 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Jonathan Brassowc1084562011-08-02 12:32:07 +0100486 * [region_size <sectors>] Defines granularity of bitmap
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500487 *
488 * RAID10-only options:
489 * [raid10_copies <# copies>] Number of copies. (Default: 2)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100490 * [raid10_format <near|far|offset>] Layout algorithm. (Default: near)
NeilBrown9d09e662011-01-13 20:00:02 +0000491 */
492static int parse_raid_params(struct raid_set *rs, char **argv,
493 unsigned num_raid_params)
494{
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500495 char *raid10_format = "near";
496 unsigned raid10_copies = 2;
Jonathan Brassoweb649122012-10-11 13:40:09 +1100497 unsigned i;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100498 unsigned long value, region_size = 0;
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100499 sector_t sectors_per_dev = rs->ti->len;
Mike Snitzer542f9032012-07-27 15:08:00 +0100500 sector_t max_io_len;
NeilBrown9d09e662011-01-13 20:00:02 +0000501 char *key;
502
503 /*
504 * First, parse the in-order required arguments
Jonathan Brassow32737272011-08-02 12:32:07 +0100505 * "chunk_size" is the only argument of this type.
NeilBrown9d09e662011-01-13 20:00:02 +0000506 */
Jingoo Hanb29bebd2013-06-01 16:15:16 +0900507 if ((kstrtoul(argv[0], 10, &value) < 0)) {
NeilBrown9d09e662011-01-13 20:00:02 +0000508 rs->ti->error = "Bad chunk size";
509 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +0100510 } else if (rs->raid_type->level == 1) {
511 if (value)
512 DMERR("Ignoring chunk size parameter for RAID 1");
513 value = 0;
514 } else if (!is_power_of_2(value)) {
515 rs->ti->error = "Chunk size must be a power of 2";
516 return -EINVAL;
517 } else if (value < 8) {
518 rs->ti->error = "Chunk size value is too small";
519 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +0000520 }
521
522 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
523 argv++;
524 num_raid_params--;
525
526 /*
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100527 * We set each individual device as In_sync with a completed
528 * 'recovery_offset'. If there has been a device failure or
529 * replacement then one of the following cases applies:
530 *
531 * 1) User specifies 'rebuild'.
532 * - Device is reset when param is read.
533 * 2) A new device is supplied.
534 * - No matching superblock found, resets device.
535 * 3) Device failure was transient and returns on reload.
536 * - Failure noticed, resets device for bitmap replay.
537 * 4) Device hadn't completed recovery after previous failure.
538 * - Superblock is read and overrides recovery_offset.
539 *
540 * What is found in the superblocks of the devices is always
541 * authoritative, unless 'rebuild' or '[no]sync' was specified.
542 */
543 for (i = 0; i < rs->md.raid_disks; i++) {
544 set_bit(In_sync, &rs->dev[i].rdev.flags);
545 rs->dev[i].rdev.recovery_offset = MaxSector;
546 }
547
548 /*
NeilBrown9d09e662011-01-13 20:00:02 +0000549 * Second, parse the unordered optional arguments
550 */
NeilBrown9d09e662011-01-13 20:00:02 +0000551 for (i = 0; i < num_raid_params; i++) {
Jonathan Brassow13c87582011-08-02 12:32:03 +0100552 if (!strcasecmp(argv[i], "nosync")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000553 rs->md.recovery_cp = MaxSector;
554 rs->print_flags |= DMPF_NOSYNC;
NeilBrown9d09e662011-01-13 20:00:02 +0000555 continue;
556 }
Jonathan Brassow13c87582011-08-02 12:32:03 +0100557 if (!strcasecmp(argv[i], "sync")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000558 rs->md.recovery_cp = 0;
559 rs->print_flags |= DMPF_SYNC;
NeilBrown9d09e662011-01-13 20:00:02 +0000560 continue;
561 }
562
563 /* The rest of the optional arguments come in key/value pairs */
564 if ((i + 1) >= num_raid_params) {
565 rs->ti->error = "Wrong number of raid parameters given";
566 return -EINVAL;
567 }
568
569 key = argv[i++];
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500570
571 /* Parameters that take a string value are checked here. */
572 if (!strcasecmp(key, "raid10_format")) {
573 if (rs->raid_type->level != 10) {
574 rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
575 return -EINVAL;
576 }
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100577 if (strcmp("near", argv[i]) &&
578 strcmp("far", argv[i]) &&
579 strcmp("offset", argv[i])) {
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500580 rs->ti->error = "Invalid 'raid10_format' value given";
581 return -EINVAL;
582 }
583 raid10_format = argv[i];
584 rs->print_flags |= DMPF_RAID10_FORMAT;
585 continue;
586 }
587
Jingoo Hanb29bebd2013-06-01 16:15:16 +0900588 if (kstrtoul(argv[i], 10, &value) < 0) {
NeilBrown9d09e662011-01-13 20:00:02 +0000589 rs->ti->error = "Bad numerical argument given in raid params";
590 return -EINVAL;
591 }
592
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500593 /* Parameters that take a numeric value are checked here */
Jonathan Brassow13c87582011-08-02 12:32:03 +0100594 if (!strcasecmp(key, "rebuild")) {
Jonathan Brassow73861992012-10-11 13:40:36 +1100595 if (value >= rs->md.raid_disks) {
NeilBrown9d09e662011-01-13 20:00:02 +0000596 rs->ti->error = "Invalid rebuild index given";
597 return -EINVAL;
598 }
599 clear_bit(In_sync, &rs->dev[value].rdev.flags);
600 rs->dev[value].rdev.recovery_offset = 0;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100601 rs->print_flags |= DMPF_REBUILD;
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100602 } else if (!strcasecmp(key, "write_mostly")) {
603 if (rs->raid_type->level != 1) {
604 rs->ti->error = "write_mostly option is only valid for RAID1";
605 return -EINVAL;
606 }
Jonthan Brassow82324802011-09-25 23:26:19 +0100607 if (value >= rs->md.raid_disks) {
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100608 rs->ti->error = "Invalid write_mostly drive index given";
609 return -EINVAL;
610 }
611 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Jonathan Brassow13c87582011-08-02 12:32:03 +0100612 } else if (!strcasecmp(key, "max_write_behind")) {
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100613 if (rs->raid_type->level != 1) {
614 rs->ti->error = "max_write_behind option is only valid for RAID1";
615 return -EINVAL;
616 }
NeilBrown9d09e662011-01-13 20:00:02 +0000617 rs->print_flags |= DMPF_MAX_WRITE_BEHIND;
618
619 /*
620 * In device-mapper, we specify things in sectors, but
621 * MD records this value in kB
622 */
623 value /= 2;
624 if (value > COUNTER_MAX) {
625 rs->ti->error = "Max write-behind limit out of range";
626 return -EINVAL;
627 }
628 rs->md.bitmap_info.max_write_behind = value;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100629 } else if (!strcasecmp(key, "daemon_sleep")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000630 rs->print_flags |= DMPF_DAEMON_SLEEP;
631 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
632 rs->ti->error = "daemon sleep period out of range";
633 return -EINVAL;
634 }
635 rs->md.bitmap_info.daemon_sleep = value;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100636 } else if (!strcasecmp(key, "stripe_cache")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000637 rs->print_flags |= DMPF_STRIPE_CACHE;
638
639 /*
640 * In device-mapper, we specify things in sectors, but
641 * MD records this value in kB
642 */
643 value /= 2;
644
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500645 if ((rs->raid_type->level != 5) &&
646 (rs->raid_type->level != 6)) {
NeilBrown9d09e662011-01-13 20:00:02 +0000647 rs->ti->error = "Inappropriate argument: stripe_cache";
648 return -EINVAL;
649 }
650 if (raid5_set_cache_size(&rs->md, (int)value)) {
651 rs->ti->error = "Bad stripe_cache size";
652 return -EINVAL;
653 }
Jonathan Brassow13c87582011-08-02 12:32:03 +0100654 } else if (!strcasecmp(key, "min_recovery_rate")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000655 rs->print_flags |= DMPF_MIN_RECOVERY_RATE;
656 if (value > INT_MAX) {
657 rs->ti->error = "min_recovery_rate out of range";
658 return -EINVAL;
659 }
660 rs->md.sync_speed_min = (int)value;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100661 } else if (!strcasecmp(key, "max_recovery_rate")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000662 rs->print_flags |= DMPF_MAX_RECOVERY_RATE;
663 if (value > INT_MAX) {
664 rs->ti->error = "max_recovery_rate out of range";
665 return -EINVAL;
666 }
667 rs->md.sync_speed_max = (int)value;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100668 } else if (!strcasecmp(key, "region_size")) {
669 rs->print_flags |= DMPF_REGION_SIZE;
670 region_size = value;
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500671 } else if (!strcasecmp(key, "raid10_copies") &&
672 (rs->raid_type->level == 10)) {
673 if ((value < 2) || (value > 0xFF)) {
674 rs->ti->error = "Bad value for 'raid10_copies'";
675 return -EINVAL;
676 }
677 rs->print_flags |= DMPF_RAID10_COPIES;
678 raid10_copies = value;
NeilBrown9d09e662011-01-13 20:00:02 +0000679 } else {
680 DMERR("Unable to parse RAID parameter: %s", key);
681 rs->ti->error = "Unable to parse RAID parameters";
682 return -EINVAL;
683 }
684 }
685
Jonathan Brassowc1084562011-08-02 12:32:07 +0100686 if (validate_region_size(rs, region_size))
687 return -EINVAL;
688
689 if (rs->md.chunk_sectors)
Mike Snitzer542f9032012-07-27 15:08:00 +0100690 max_io_len = rs->md.chunk_sectors;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100691 else
Mike Snitzer542f9032012-07-27 15:08:00 +0100692 max_io_len = region_size;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100693
Mike Snitzer542f9032012-07-27 15:08:00 +0100694 if (dm_set_target_max_io_len(rs->ti, max_io_len))
695 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +0100696
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500697 if (rs->raid_type->level == 10) {
698 if (raid10_copies > rs->md.raid_disks) {
699 rs->ti->error = "Not enough devices to satisfy specification";
700 return -EINVAL;
701 }
702
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100703 /*
704 * If the format is not "near", we only support
705 * two copies at the moment.
706 */
707 if (strcmp("near", raid10_format) && (raid10_copies > 2)) {
708 rs->ti->error = "Too many copies for given RAID10 format.";
709 return -EINVAL;
710 }
711
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500712 /* (Len * #mirrors) / #devices */
713 sectors_per_dev = rs->ti->len * raid10_copies;
714 sector_div(sectors_per_dev, rs->md.raid_disks);
715
716 rs->md.layout = raid10_format_to_md_layout(raid10_format,
717 raid10_copies);
718 rs->md.new_layout = rs->md.layout;
719 } else if ((rs->raid_type->level > 1) &&
720 sector_div(sectors_per_dev,
721 (rs->md.raid_disks - rs->raid_type->parity_devs))) {
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100722 rs->ti->error = "Target length not divisible by number of data devices";
723 return -EINVAL;
724 }
725 rs->md.dev_sectors = sectors_per_dev;
726
NeilBrown9d09e662011-01-13 20:00:02 +0000727 /* Assume there are no metadata devices until the drives are parsed */
728 rs->md.persistent = 0;
729 rs->md.external = 1;
730
731 return 0;
732}
733
734static void do_table_event(struct work_struct *ws)
735{
736 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
737
738 dm_table_event(rs->ti->table);
739}
740
741static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
742{
743 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
744
Jonathan Brassow32737272011-08-02 12:32:07 +0100745 if (rs->raid_type->level == 1)
746 return md_raid1_congested(&rs->md, bits);
747
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -0500748 if (rs->raid_type->level == 10)
749 return md_raid10_congested(&rs->md, bits);
750
NeilBrown9d09e662011-01-13 20:00:02 +0000751 return md_raid5_congested(&rs->md, bits);
752}
753
NeilBrown9d09e662011-01-13 20:00:02 +0000754/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100755 * This structure is never routinely used by userspace, unlike md superblocks.
756 * Devices with this superblock should only ever be accessed via device-mapper.
757 */
758#define DM_RAID_MAGIC 0x64526D44
759struct dm_raid_superblock {
760 __le32 magic; /* "DmRd" */
761 __le32 features; /* Used to indicate possible future changes */
762
763 __le32 num_devices; /* Number of devices in this array. (Max 64) */
764 __le32 array_position; /* The position of this drive in the array */
765
766 __le64 events; /* Incremented by md when superblock updated */
767 __le64 failed_devices; /* Bit field of devices to indicate failures */
768
769 /*
770 * This offset tracks the progress of the repair or replacement of
771 * an individual drive.
772 */
773 __le64 disk_recovery_offset;
774
775 /*
776 * This offset tracks the progress of the initial array
777 * synchronisation/parity calculation.
778 */
779 __le64 array_resync_offset;
780
781 /*
782 * RAID characteristics
783 */
784 __le32 level;
785 __le32 layout;
786 __le32 stripe_sectors;
787
788 __u8 pad[452]; /* Round struct to 512 bytes. */
789 /* Always set to 0 when writing. */
790} __packed;
791
NeilBrown3cb03002011-10-11 16:45:26 +1100792static int read_disk_sb(struct md_rdev *rdev, int size)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100793{
794 BUG_ON(!rdev->sb_page);
795
796 if (rdev->sb_loaded)
797 return 0;
798
799 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 1)) {
Jonathan E Brassow04475682012-03-28 18:41:26 +0100800 DMERR("Failed to read superblock of device at position %d",
801 rdev->raid_disk);
Jonathan Brassowc32fb9e2012-05-22 13:55:31 +1000802 md_error(rdev->mddev, rdev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100803 return -EINVAL;
804 }
805
806 rdev->sb_loaded = 1;
807
808 return 0;
809}
810
NeilBrownfd01b882011-10-11 16:47:53 +1100811static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100812{
Jonathan Brassow81f382f2012-05-22 13:55:30 +1000813 int i;
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100814 uint64_t failed_devices;
815 struct dm_raid_superblock *sb;
Jonathan Brassow81f382f2012-05-22 13:55:30 +1000816 struct raid_set *rs = container_of(mddev, struct raid_set, md);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100817
818 sb = page_address(rdev->sb_page);
819 failed_devices = le64_to_cpu(sb->failed_devices);
820
Jonathan Brassow81f382f2012-05-22 13:55:30 +1000821 for (i = 0; i < mddev->raid_disks; i++)
822 if (!rs->dev[i].data_dev ||
823 test_bit(Faulty, &(rs->dev[i].rdev.flags)))
824 failed_devices |= (1ULL << i);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100825
826 memset(sb, 0, sizeof(*sb));
827
828 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
829 sb->features = cpu_to_le32(0); /* No features yet */
830
831 sb->num_devices = cpu_to_le32(mddev->raid_disks);
832 sb->array_position = cpu_to_le32(rdev->raid_disk);
833
834 sb->events = cpu_to_le64(mddev->events);
835 sb->failed_devices = cpu_to_le64(failed_devices);
836
837 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
838 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
839
840 sb->level = cpu_to_le32(mddev->level);
841 sb->layout = cpu_to_le32(mddev->layout);
842 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
843}
844
845/*
846 * super_load
847 *
848 * This function creates a superblock if one is not found on the device
849 * and will decide which superblock to use if there's a choice.
850 *
851 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
852 */
NeilBrown3cb03002011-10-11 16:45:26 +1100853static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100854{
855 int ret;
856 struct dm_raid_superblock *sb;
857 struct dm_raid_superblock *refsb;
858 uint64_t events_sb, events_refsb;
859
860 rdev->sb_start = 0;
861 rdev->sb_size = sizeof(*sb);
862
863 ret = read_disk_sb(rdev, rdev->sb_size);
864 if (ret)
865 return ret;
866
867 sb = page_address(rdev->sb_page);
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +0000868
869 /*
870 * Two cases that we want to write new superblocks and rebuild:
871 * 1) New device (no matching magic number)
872 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
873 */
874 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
875 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100876 super_sync(rdev->mddev, rdev);
877
878 set_bit(FirstUse, &rdev->flags);
879
880 /* Force writing of superblocks to disk */
881 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
882
883 /* Any superblock is better than none, choose that if given */
884 return refdev ? 0 : 1;
885 }
886
887 if (!refdev)
888 return 1;
889
890 events_sb = le64_to_cpu(sb->events);
891
892 refsb = page_address(refdev->sb_page);
893 events_refsb = le64_to_cpu(refsb->events);
894
895 return (events_sb > events_refsb) ? 1 : 0;
896}
897
NeilBrownfd01b882011-10-11 16:47:53 +1100898static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100899{
900 int role;
901 struct raid_set *rs = container_of(mddev, struct raid_set, md);
902 uint64_t events_sb;
903 uint64_t failed_devices;
904 struct dm_raid_superblock *sb;
905 uint32_t new_devs = 0;
906 uint32_t rebuilds = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +1100907 struct md_rdev *r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100908 struct dm_raid_superblock *sb2;
909
910 sb = page_address(rdev->sb_page);
911 events_sb = le64_to_cpu(sb->events);
912 failed_devices = le64_to_cpu(sb->failed_devices);
913
914 /*
915 * Initialise to 1 if this is a new superblock.
916 */
917 mddev->events = events_sb ? : 1;
918
919 /*
920 * Reshaping is not currently allowed
921 */
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100922 if (le32_to_cpu(sb->level) != mddev->level) {
923 DMERR("Reshaping arrays not yet supported. (RAID level change)");
924 return -EINVAL;
925 }
926 if (le32_to_cpu(sb->layout) != mddev->layout) {
927 DMERR("Reshaping arrays not yet supported. (RAID layout change)");
928 DMERR(" 0x%X vs 0x%X", le32_to_cpu(sb->layout), mddev->layout);
929 DMERR(" Old layout: %s w/ %d copies",
930 raid10_md_layout_to_format(le32_to_cpu(sb->layout)),
931 raid10_md_layout_to_copies(le32_to_cpu(sb->layout)));
932 DMERR(" New layout: %s w/ %d copies",
933 raid10_md_layout_to_format(mddev->layout),
934 raid10_md_layout_to_copies(mddev->layout));
935 return -EINVAL;
936 }
937 if (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors) {
938 DMERR("Reshaping arrays not yet supported. (stripe sectors change)");
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100939 return -EINVAL;
940 }
941
942 /* We can only change the number of devices in RAID1 right now */
943 if ((rs->raid_type->level != 1) &&
944 (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +1100945 DMERR("Reshaping arrays not yet supported. (device count change)");
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100946 return -EINVAL;
947 }
948
949 if (!(rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC)))
950 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
951
952 /*
953 * During load, we set FirstUse if a new superblock was written.
954 * There are two reasons we might not have a superblock:
955 * 1) The array is brand new - in which case, all of the
956 * devices must have their In_sync bit set. Also,
957 * recovery_cp must be 0, unless forced.
958 * 2) This is a new device being added to an old array
959 * and the new device needs to be rebuilt - in which
960 * case the In_sync bit will /not/ be set and
961 * recovery_cp must be MaxSector.
962 */
NeilBrowndafb20f2012-03-19 12:46:39 +1100963 rdev_for_each(r, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100964 if (!test_bit(In_sync, &r->flags)) {
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +0000965 DMINFO("Device %d specified for rebuild: "
966 "Clearing superblock", r->raid_disk);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100967 rebuilds++;
968 } else if (test_bit(FirstUse, &r->flags))
969 new_devs++;
970 }
971
972 if (!rebuilds) {
973 if (new_devs == mddev->raid_disks) {
974 DMINFO("Superblocks created for new array");
975 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
976 } else if (new_devs) {
977 DMERR("New device injected "
978 "into existing array without 'rebuild' "
979 "parameter specified");
980 return -EINVAL;
981 }
982 } else if (new_devs) {
983 DMERR("'rebuild' devices cannot be "
984 "injected into an array with other first-time devices");
985 return -EINVAL;
986 } else if (mddev->recovery_cp != MaxSector) {
987 DMERR("'rebuild' specified while array is not in-sync");
988 return -EINVAL;
989 }
990
991 /*
992 * Now we set the Faulty bit for those devices that are
993 * recorded in the superblock as failed.
994 */
NeilBrowndafb20f2012-03-19 12:46:39 +1100995 rdev_for_each(r, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100996 if (!r->sb_page)
997 continue;
998 sb2 = page_address(r->sb_page);
999 sb2->failed_devices = 0;
1000
1001 /*
1002 * Check for any device re-ordering.
1003 */
1004 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
1005 role = le32_to_cpu(sb2->array_position);
1006 if (role != r->raid_disk) {
1007 if (rs->raid_type->level != 1) {
1008 rs->ti->error = "Cannot change device "
1009 "positions in RAID array";
1010 return -EINVAL;
1011 }
1012 DMINFO("RAID1 device #%d now at position #%d",
1013 role, r->raid_disk);
1014 }
1015
1016 /*
1017 * Partial recovery is performed on
1018 * returning failed devices.
1019 */
1020 if (failed_devices & (1 << role))
1021 set_bit(Faulty, &r->flags);
1022 }
1023 }
1024
1025 return 0;
1026}
1027
NeilBrownfd01b882011-10-11 16:47:53 +11001028static int super_validate(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001029{
1030 struct dm_raid_superblock *sb = page_address(rdev->sb_page);
1031
1032 /*
1033 * If mddev->events is not set, we know we have not yet initialized
1034 * the array.
1035 */
1036 if (!mddev->events && super_init_validation(mddev, rdev))
1037 return -EINVAL;
1038
1039 mddev->bitmap_info.offset = 4096 >> 9; /* Enable bitmap creation */
1040 rdev->mddev->bitmap_info.default_offset = 4096 >> 9;
1041 if (!test_bit(FirstUse, &rdev->flags)) {
1042 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
1043 if (rdev->recovery_offset != MaxSector)
1044 clear_bit(In_sync, &rdev->flags);
1045 }
1046
1047 /*
1048 * If a device comes back, set it as not In_sync and no longer faulty.
1049 */
1050 if (test_bit(Faulty, &rdev->flags)) {
1051 clear_bit(Faulty, &rdev->flags);
1052 clear_bit(In_sync, &rdev->flags);
1053 rdev->saved_raid_disk = rdev->raid_disk;
1054 rdev->recovery_offset = 0;
1055 }
1056
1057 clear_bit(FirstUse, &rdev->flags);
1058
1059 return 0;
1060}
1061
1062/*
1063 * Analyse superblocks and select the freshest.
1064 */
1065static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
1066{
1067 int ret;
Jonathan E Brassow04475682012-03-28 18:41:26 +01001068 struct raid_dev *dev;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10001069 struct md_rdev *rdev, *tmp, *freshest;
NeilBrownfd01b882011-10-11 16:47:53 +11001070 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001071
1072 freshest = NULL;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +10001073 rdev_for_each_safe(rdev, tmp, mddev) {
Jonathan Brassow761becf2012-10-11 13:42:19 +11001074 /*
1075 * Skipping super_load due to DMPF_SYNC will cause
1076 * the array to undergo initialization again as
1077 * though it were new. This is the intended effect
1078 * of the "sync" directive.
1079 *
1080 * When reshaping capability is added, we must ensure
1081 * that the "sync" directive is disallowed during the
1082 * reshape.
1083 */
1084 if (rs->print_flags & DMPF_SYNC)
1085 continue;
1086
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001087 if (!rdev->meta_bdev)
1088 continue;
1089
1090 ret = super_load(rdev, freshest);
1091
1092 switch (ret) {
1093 case 1:
1094 freshest = rdev;
1095 break;
1096 case 0:
1097 break;
1098 default:
Jonathan E Brassow04475682012-03-28 18:41:26 +01001099 dev = container_of(rdev, struct raid_dev, rdev);
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001100 if (dev->meta_dev)
1101 dm_put_device(ti, dev->meta_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01001102
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001103 dev->meta_dev = NULL;
1104 rdev->meta_bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01001105
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001106 if (rdev->sb_page)
1107 put_page(rdev->sb_page);
Jonathan E Brassow04475682012-03-28 18:41:26 +01001108
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001109 rdev->sb_page = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01001110
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001111 rdev->sb_loaded = 0;
Jonathan E Brassow04475682012-03-28 18:41:26 +01001112
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001113 /*
1114 * We might be able to salvage the data device
1115 * even though the meta device has failed. For
1116 * now, we behave as though '- -' had been
1117 * set for this device in the table.
1118 */
1119 if (dev->data_dev)
1120 dm_put_device(ti, dev->data_dev);
Jonathan E Brassow04475682012-03-28 18:41:26 +01001121
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001122 dev->data_dev = NULL;
1123 rdev->bdev = NULL;
Jonathan E Brassow04475682012-03-28 18:41:26 +01001124
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001125 list_del(&rdev->same_set);
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001126 }
1127 }
1128
1129 if (!freshest)
1130 return 0;
1131
Jonathan Brassow55ebbb52013-01-22 21:42:18 -06001132 if (validate_raid_redundancy(rs)) {
1133 rs->ti->error = "Insufficient redundancy to activate array";
1134 return -EINVAL;
1135 }
1136
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001137 /*
1138 * Validation of the freshest device provides the source of
1139 * validation for the remaining devices.
1140 */
1141 ti->error = "Unable to assemble array: Invalid superblocks";
1142 if (super_validate(mddev, freshest))
1143 return -EINVAL;
1144
NeilBrowndafb20f2012-03-19 12:46:39 +11001145 rdev_for_each(rdev, mddev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001146 if ((rdev != freshest) && super_validate(mddev, rdev))
1147 return -EINVAL;
1148
1149 return 0;
1150}
1151
1152/*
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02001153 * Enable/disable discard support on RAID set depending on RAID level.
1154 */
1155static void configure_discard_support(struct dm_target *ti, struct raid_set *rs)
1156{
1157 /* Assume discards not supported until after checks below. */
1158 ti->discards_supported = false;
1159
1160 /* RAID level 4,5,6 require discard_zeroes_data for data integrity! */
1161 if (rs->md.level == 4 || rs->md.level == 5 || rs->md.level == 6)
1162 return; /* discard_zeroes_data cannot be trusted as reliable */
1163
1164 ti->discards_supported = true;
1165
1166 /*
1167 * RAID1 and RAID10 personalities require bio splitting,
1168 */
1169 ti->split_discard_bios = true;
1170 ti->num_discard_bios = 1;
1171}
1172
1173/*
NeilBrown9d09e662011-01-13 20:00:02 +00001174 * Construct a RAID4/5/6 mapping:
1175 * Args:
1176 * <raid_type> <#raid_params> <raid_params> \
1177 * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
1178 *
NeilBrown9d09e662011-01-13 20:00:02 +00001179 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
1180 * details on possible <raid_params>.
1181 */
1182static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
1183{
1184 int ret;
1185 struct raid_type *rt;
1186 unsigned long num_raid_params, num_raid_devs;
1187 struct raid_set *rs = NULL;
1188
1189 /* Must have at least <raid_type> <#raid_params> */
1190 if (argc < 2) {
1191 ti->error = "Too few arguments";
1192 return -EINVAL;
1193 }
1194
1195 /* raid type */
1196 rt = get_raid_type(argv[0]);
1197 if (!rt) {
1198 ti->error = "Unrecognised raid_type";
1199 return -EINVAL;
1200 }
1201 argc--;
1202 argv++;
1203
1204 /* number of RAID parameters */
Jingoo Hanb29bebd2013-06-01 16:15:16 +09001205 if (kstrtoul(argv[0], 10, &num_raid_params) < 0) {
NeilBrown9d09e662011-01-13 20:00:02 +00001206 ti->error = "Cannot understand number of RAID parameters";
1207 return -EINVAL;
1208 }
1209 argc--;
1210 argv++;
1211
1212 /* Skip over RAID params for now and find out # of devices */
1213 if (num_raid_params + 1 > argc) {
1214 ti->error = "Arguments do not agree with counts given";
1215 return -EINVAL;
1216 }
1217
Jingoo Hanb29bebd2013-06-01 16:15:16 +09001218 if ((kstrtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) ||
NeilBrown9d09e662011-01-13 20:00:02 +00001219 (num_raid_devs >= INT_MAX)) {
1220 ti->error = "Cannot understand number of raid devices";
1221 return -EINVAL;
1222 }
1223
1224 rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
1225 if (IS_ERR(rs))
1226 return PTR_ERR(rs);
1227
1228 ret = parse_raid_params(rs, argv, (unsigned)num_raid_params);
1229 if (ret)
1230 goto bad;
1231
1232 ret = -EINVAL;
1233
1234 argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
1235 argv += num_raid_params + 1;
1236
1237 if (argc != (num_raid_devs * 2)) {
1238 ti->error = "Supplied RAID devices does not match the count given";
1239 goto bad;
1240 }
1241
1242 ret = dev_parms(rs, argv);
1243 if (ret)
1244 goto bad;
1245
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001246 rs->md.sync_super = super_sync;
1247 ret = analyse_superblocks(ti, rs);
1248 if (ret)
1249 goto bad;
1250
NeilBrown9d09e662011-01-13 20:00:02 +00001251 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +00001252 ti->private = rs;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001253 ti->num_flush_bios = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00001254
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02001255 /*
1256 * Disable/enable discard support on RAID set.
1257 */
1258 configure_discard_support(ti, rs);
1259
NeilBrown9d09e662011-01-13 20:00:02 +00001260 mutex_lock(&rs->md.reconfig_mutex);
1261 ret = md_run(&rs->md);
1262 rs->md.in_sync = 0; /* Assume already marked dirty */
1263 mutex_unlock(&rs->md.reconfig_mutex);
1264
1265 if (ret) {
1266 ti->error = "Fail to run raid array";
1267 goto bad;
1268 }
1269
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001270 if (ti->len != rs->md.array_sectors) {
1271 ti->error = "Array size does not match requested target length";
1272 ret = -EINVAL;
1273 goto size_mismatch;
1274 }
NeilBrown9d09e662011-01-13 20:00:02 +00001275 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +00001276 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
1277
Jonathan Brassow32737272011-08-02 12:32:07 +01001278 mddev_suspend(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00001279 return 0;
1280
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001281size_mismatch:
1282 md_stop(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00001283bad:
1284 context_free(rs);
1285
1286 return ret;
1287}
1288
1289static void raid_dtr(struct dm_target *ti)
1290{
1291 struct raid_set *rs = ti->private;
1292
1293 list_del_init(&rs->callbacks.list);
1294 md_stop(&rs->md);
1295 context_free(rs);
1296}
1297
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001298static int raid_map(struct dm_target *ti, struct bio *bio)
NeilBrown9d09e662011-01-13 20:00:02 +00001299{
1300 struct raid_set *rs = ti->private;
NeilBrownfd01b882011-10-11 16:47:53 +11001301 struct mddev *mddev = &rs->md;
NeilBrown9d09e662011-01-13 20:00:02 +00001302
1303 mddev->pers->make_request(mddev, bio);
1304
1305 return DM_MAPIO_SUBMITTED;
1306}
1307
Jonathan Brassowbe836512013-04-24 11:42:43 +10001308static const char *decipher_sync_action(struct mddev *mddev)
1309{
1310 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
1311 return "frozen";
1312
1313 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1314 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
1315 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1316 return "reshape";
1317
1318 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1319 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1320 return "resync";
1321 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1322 return "check";
1323 return "repair";
1324 }
1325
1326 if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
1327 return "recover";
1328 }
1329
1330 return "idle";
1331}
1332
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001333static void raid_status(struct dm_target *ti, status_type_t type,
1334 unsigned status_flags, char *result, unsigned maxlen)
NeilBrown9d09e662011-01-13 20:00:02 +00001335{
1336 struct raid_set *rs = ti->private;
1337 unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
1338 unsigned sz = 0;
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001339 int i, array_in_sync = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00001340 sector_t sync;
1341
1342 switch (type) {
1343 case STATUSTYPE_INFO:
1344 DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
1345
NeilBrown9d09e662011-01-13 20:00:02 +00001346 if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
1347 sync = rs->md.curr_resync_completed;
1348 else
1349 sync = rs->md.recovery_cp;
1350
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001351 if (sync >= rs->md.resync_max_sectors) {
Jonathan Brassowbe836512013-04-24 11:42:43 +10001352 /*
1353 * Sync complete.
1354 */
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001355 array_in_sync = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00001356 sync = rs->md.resync_max_sectors;
Jonathan Brassowbe836512013-04-24 11:42:43 +10001357 } else if (test_bit(MD_RECOVERY_REQUESTED, &rs->md.recovery)) {
1358 /*
1359 * If "check" or "repair" is occurring, the array has
1360 * undergone and initial sync and the health characters
1361 * should not be 'a' anymore.
1362 */
1363 array_in_sync = 1;
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001364 } else {
1365 /*
1366 * The array may be doing an initial sync, or it may
1367 * be rebuilding individual components. If all the
1368 * devices are In_sync, then it is the array that is
1369 * being initialized.
1370 */
1371 for (i = 0; i < rs->md.raid_disks; i++)
1372 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
1373 array_in_sync = 1;
1374 }
Jonathan Brassowbe836512013-04-24 11:42:43 +10001375
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001376 /*
1377 * Status characters:
1378 * 'D' = Dead/Failed device
1379 * 'a' = Alive but not in-sync
1380 * 'A' = Alive and in-sync
1381 */
1382 for (i = 0; i < rs->md.raid_disks; i++) {
1383 if (test_bit(Faulty, &rs->dev[i].rdev.flags))
1384 DMEMIT("D");
1385 else if (!array_in_sync ||
1386 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1387 DMEMIT("a");
1388 else
1389 DMEMIT("A");
1390 }
NeilBrown9d09e662011-01-13 20:00:02 +00001391
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001392 /*
1393 * In-sync ratio:
1394 * The in-sync ratio shows the progress of:
1395 * - Initializing the array
1396 * - Rebuilding a subset of devices of the array
1397 * The user can distinguish between the two by referring
1398 * to the status characters.
1399 */
NeilBrown9d09e662011-01-13 20:00:02 +00001400 DMEMIT(" %llu/%llu",
1401 (unsigned long long) sync,
1402 (unsigned long long) rs->md.resync_max_sectors);
1403
Jonathan Brassowbe836512013-04-24 11:42:43 +10001404 /*
1405 * Sync action:
1406 * See Documentation/device-mapper/dm-raid.c for
1407 * information on each of these states.
1408 */
1409 DMEMIT(" %s", decipher_sync_action(&rs->md));
1410
1411 /*
1412 * resync_mismatches/mismatch_cnt
1413 * This field shows the number of discrepancies found when
1414 * performing a "check" of the array.
1415 */
1416 DMEMIT(" %llu",
Jonathan Brassowc4a39552013-06-25 01:23:59 -05001417 (strcmp(rs->md.last_sync_action, "check")) ? 0 :
Jonathan Brassowbe836512013-04-24 11:42:43 +10001418 (unsigned long long)
1419 atomic64_read(&rs->md.resync_mismatches));
NeilBrown9d09e662011-01-13 20:00:02 +00001420 break;
1421 case STATUSTYPE_TABLE:
1422 /* The string you would use to construct this array */
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001423 for (i = 0; i < rs->md.raid_disks; i++) {
Jonathan Brassow13c87582011-08-02 12:32:03 +01001424 if ((rs->print_flags & DMPF_REBUILD) &&
1425 rs->dev[i].data_dev &&
NeilBrown9d09e662011-01-13 20:00:02 +00001426 !test_bit(In_sync, &rs->dev[i].rdev.flags))
Jonathan Brassow13c87582011-08-02 12:32:03 +01001427 raid_param_cnt += 2; /* for rebuilds */
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001428 if (rs->dev[i].data_dev &&
1429 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1430 raid_param_cnt += 2;
1431 }
NeilBrown9d09e662011-01-13 20:00:02 +00001432
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -06001433 raid_param_cnt += (hweight32(rs->print_flags & ~DMPF_REBUILD) * 2);
NeilBrown9d09e662011-01-13 20:00:02 +00001434 if (rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC))
1435 raid_param_cnt--;
1436
1437 DMEMIT("%s %u %u", rs->raid_type->name,
1438 raid_param_cnt, rs->md.chunk_sectors);
1439
1440 if ((rs->print_flags & DMPF_SYNC) &&
1441 (rs->md.recovery_cp == MaxSector))
1442 DMEMIT(" sync");
1443 if (rs->print_flags & DMPF_NOSYNC)
1444 DMEMIT(" nosync");
1445
1446 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow13c87582011-08-02 12:32:03 +01001447 if ((rs->print_flags & DMPF_REBUILD) &&
1448 rs->dev[i].data_dev &&
NeilBrown9d09e662011-01-13 20:00:02 +00001449 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1450 DMEMIT(" rebuild %u", i);
1451
1452 if (rs->print_flags & DMPF_DAEMON_SLEEP)
1453 DMEMIT(" daemon_sleep %lu",
1454 rs->md.bitmap_info.daemon_sleep);
1455
1456 if (rs->print_flags & DMPF_MIN_RECOVERY_RATE)
1457 DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
1458
1459 if (rs->print_flags & DMPF_MAX_RECOVERY_RATE)
1460 DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
1461
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001462 for (i = 0; i < rs->md.raid_disks; i++)
1463 if (rs->dev[i].data_dev &&
1464 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1465 DMEMIT(" write_mostly %u", i);
1466
NeilBrown9d09e662011-01-13 20:00:02 +00001467 if (rs->print_flags & DMPF_MAX_WRITE_BEHIND)
1468 DMEMIT(" max_write_behind %lu",
1469 rs->md.bitmap_info.max_write_behind);
1470
1471 if (rs->print_flags & DMPF_STRIPE_CACHE) {
NeilBrownd1688a62011-10-11 16:49:52 +11001472 struct r5conf *conf = rs->md.private;
NeilBrown9d09e662011-01-13 20:00:02 +00001473
1474 /* convert from kiB to sectors */
1475 DMEMIT(" stripe_cache %d",
1476 conf ? conf->max_nr_stripes * 2 : 0);
1477 }
1478
Jonathan Brassowc1084562011-08-02 12:32:07 +01001479 if (rs->print_flags & DMPF_REGION_SIZE)
1480 DMEMIT(" region_size %lu",
1481 rs->md.bitmap_info.chunksize >> 9);
1482
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001483 if (rs->print_flags & DMPF_RAID10_COPIES)
1484 DMEMIT(" raid10_copies %u",
1485 raid10_md_layout_to_copies(rs->md.layout));
1486
1487 if (rs->print_flags & DMPF_RAID10_FORMAT)
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001488 DMEMIT(" raid10_format %s",
1489 raid10_md_layout_to_format(rs->md.layout));
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001490
NeilBrown9d09e662011-01-13 20:00:02 +00001491 DMEMIT(" %d", rs->md.raid_disks);
1492 for (i = 0; i < rs->md.raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001493 if (rs->dev[i].meta_dev)
1494 DMEMIT(" %s", rs->dev[i].meta_dev->name);
1495 else
1496 DMEMIT(" -");
NeilBrown9d09e662011-01-13 20:00:02 +00001497
1498 if (rs->dev[i].data_dev)
1499 DMEMIT(" %s", rs->dev[i].data_dev->name);
1500 else
1501 DMEMIT(" -");
1502 }
1503 }
NeilBrown9d09e662011-01-13 20:00:02 +00001504}
1505
Jonathan Brassowbe836512013-04-24 11:42:43 +10001506static int raid_message(struct dm_target *ti, unsigned argc, char **argv)
1507{
1508 struct raid_set *rs = ti->private;
1509 struct mddev *mddev = &rs->md;
1510
1511 if (!strcasecmp(argv[0], "reshape")) {
1512 DMERR("Reshape not supported.");
1513 return -EINVAL;
1514 }
1515
1516 if (!mddev->pers || !mddev->pers->sync_request)
1517 return -EINVAL;
1518
1519 if (!strcasecmp(argv[0], "frozen"))
1520 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
1521 else
1522 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
1523
1524 if (!strcasecmp(argv[0], "idle") || !strcasecmp(argv[0], "frozen")) {
1525 if (mddev->sync_thread) {
1526 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1527 md_reap_sync_thread(mddev);
1528 }
1529 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1530 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
1531 return -EBUSY;
1532 else if (!strcasecmp(argv[0], "resync"))
1533 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1534 else if (!strcasecmp(argv[0], "recover")) {
1535 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
1536 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1537 } else {
1538 if (!strcasecmp(argv[0], "check"))
1539 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1540 else if (!!strcasecmp(argv[0], "repair"))
1541 return -EINVAL;
1542 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1543 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1544 }
1545 if (mddev->ro == 2) {
1546 /* A write to sync_action is enough to justify
1547 * canceling read-auto mode
1548 */
1549 mddev->ro = 0;
1550 if (!mddev->suspended)
1551 md_wakeup_thread(mddev->sync_thread);
1552 }
1553 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1554 if (!mddev->suspended)
1555 md_wakeup_thread(mddev->thread);
1556
1557 return 0;
1558}
1559
1560static int raid_iterate_devices(struct dm_target *ti,
1561 iterate_devices_callout_fn fn, void *data)
NeilBrown9d09e662011-01-13 20:00:02 +00001562{
1563 struct raid_set *rs = ti->private;
1564 unsigned i;
1565 int ret = 0;
1566
1567 for (i = 0; !ret && i < rs->md.raid_disks; i++)
1568 if (rs->dev[i].data_dev)
1569 ret = fn(ti,
1570 rs->dev[i].data_dev,
1571 0, /* No offset on data devs */
1572 rs->md.dev_sectors,
1573 data);
1574
1575 return ret;
1576}
1577
1578static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
1579{
1580 struct raid_set *rs = ti->private;
1581 unsigned chunk_size = rs->md.chunk_sectors << 9;
NeilBrownd1688a62011-10-11 16:49:52 +11001582 struct r5conf *conf = rs->md.private;
NeilBrown9d09e662011-01-13 20:00:02 +00001583
1584 blk_limits_io_min(limits, chunk_size);
1585 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
1586}
1587
1588static void raid_presuspend(struct dm_target *ti)
1589{
1590 struct raid_set *rs = ti->private;
1591
1592 md_stop_writes(&rs->md);
1593}
1594
1595static void raid_postsuspend(struct dm_target *ti)
1596{
1597 struct raid_set *rs = ti->private;
1598
1599 mddev_suspend(&rs->md);
1600}
1601
Jonathan Brassowf381e712013-05-08 17:57:13 -05001602static void attempt_restore_of_faulty_devices(struct raid_set *rs)
NeilBrown9d09e662011-01-13 20:00:02 +00001603{
Jonathan Brassow9092c022013-05-02 14:19:24 -05001604 int i;
1605 uint64_t failed_devices, cleared_failed_devices = 0;
1606 unsigned long flags;
1607 struct dm_raid_superblock *sb;
Jonathan Brassow9092c022013-05-02 14:19:24 -05001608 struct md_rdev *r;
NeilBrown9d09e662011-01-13 20:00:02 +00001609
Jonathan Brassowf381e712013-05-08 17:57:13 -05001610 for (i = 0; i < rs->md.raid_disks; i++) {
1611 r = &rs->dev[i].rdev;
1612 if (test_bit(Faulty, &r->flags) && r->sb_page &&
1613 sync_page_io(r, 0, r->sb_size, r->sb_page, READ, 1)) {
1614 DMINFO("Faulty %s device #%d has readable super block."
1615 " Attempting to revive it.",
1616 rs->raid_type->name, i);
Jonathan Brassowa4dc1632013-05-08 18:00:54 -05001617
1618 /*
1619 * Faulty bit may be set, but sometimes the array can
1620 * be suspended before the personalities can respond
1621 * by removing the device from the array (i.e. calling
1622 * 'hot_remove_disk'). If they haven't yet removed
1623 * the failed device, its 'raid_disk' number will be
1624 * '>= 0' - meaning we must call this function
1625 * ourselves.
1626 */
1627 if ((r->raid_disk >= 0) &&
1628 (r->mddev->pers->hot_remove_disk(r->mddev, r) != 0))
1629 /* Failed to revive this device, try next */
1630 continue;
1631
Jonathan Brassowf381e712013-05-08 17:57:13 -05001632 r->raid_disk = i;
1633 r->saved_raid_disk = i;
1634 flags = r->flags;
1635 clear_bit(Faulty, &r->flags);
1636 clear_bit(WriteErrorSeen, &r->flags);
1637 clear_bit(In_sync, &r->flags);
1638 if (r->mddev->pers->hot_add_disk(r->mddev, r)) {
1639 r->raid_disk = -1;
1640 r->saved_raid_disk = -1;
1641 r->flags = flags;
1642 } else {
1643 r->recovery_offset = 0;
1644 cleared_failed_devices |= 1 << i;
1645 }
1646 }
1647 }
1648 if (cleared_failed_devices) {
1649 rdev_for_each(r, &rs->md) {
1650 sb = page_address(r->sb_page);
1651 failed_devices = le64_to_cpu(sb->failed_devices);
1652 failed_devices &= ~cleared_failed_devices;
1653 sb->failed_devices = cpu_to_le64(failed_devices);
1654 }
1655 }
1656}
1657
1658static void raid_resume(struct dm_target *ti)
1659{
1660 struct raid_set *rs = ti->private;
1661
Jonathan Brassow81f382f2012-05-22 13:55:30 +10001662 set_bit(MD_CHANGE_DEVS, &rs->md.flags);
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -06001663 if (!rs->bitmap_loaded) {
1664 bitmap_load(&rs->md);
1665 rs->bitmap_loaded = 1;
Jonathan Brassow9092c022013-05-02 14:19:24 -05001666 } else {
1667 /*
1668 * A secondary resume while the device is active.
1669 * Take this opportunity to check whether any failed
1670 * devices are reachable again.
1671 */
Jonathan Brassowf381e712013-05-08 17:57:13 -05001672 attempt_restore_of_faulty_devices(rs);
Jonathan Brassow47525e52012-05-22 13:55:29 +10001673 }
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -06001674
Jonathan Brassow47525e52012-05-22 13:55:29 +10001675 clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
NeilBrown9d09e662011-01-13 20:00:02 +00001676 mddev_resume(&rs->md);
1677}
1678
1679static struct target_type raid_target = {
1680 .name = "raid",
Heinz Mauelshagen75b8e042014-09-24 17:47:18 +02001681 .version = {1, 6, 0},
NeilBrown9d09e662011-01-13 20:00:02 +00001682 .module = THIS_MODULE,
1683 .ctr = raid_ctr,
1684 .dtr = raid_dtr,
1685 .map = raid_map,
1686 .status = raid_status,
Jonathan Brassowbe836512013-04-24 11:42:43 +10001687 .message = raid_message,
NeilBrown9d09e662011-01-13 20:00:02 +00001688 .iterate_devices = raid_iterate_devices,
1689 .io_hints = raid_io_hints,
1690 .presuspend = raid_presuspend,
1691 .postsuspend = raid_postsuspend,
1692 .resume = raid_resume,
1693};
1694
1695static int __init dm_raid_init(void)
1696{
Jonathan Brassowfe5d2f42013-02-21 13:28:10 +11001697 DMINFO("Loading target version %u.%u.%u",
1698 raid_target.version[0],
1699 raid_target.version[1],
1700 raid_target.version[2]);
NeilBrown9d09e662011-01-13 20:00:02 +00001701 return dm_register_target(&raid_target);
1702}
1703
1704static void __exit dm_raid_exit(void)
1705{
1706 dm_unregister_target(&raid_target);
1707}
1708
1709module_init(dm_raid_init);
1710module_exit(dm_raid_exit);
1711
1712MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
Jonathan Brassow63f33b8d2012-07-31 21:44:26 -05001713MODULE_ALIAS("dm-raid1");
1714MODULE_ALIAS("dm-raid10");
NeilBrown9d09e662011-01-13 20:00:02 +00001715MODULE_ALIAS("dm-raid4");
1716MODULE_ALIAS("dm-raid5");
1717MODULE_ALIAS("dm-raid6");
1718MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
1719MODULE_LICENSE("GPL");