blob: f4275a8e860c310ed6e59eb3f4592c045ec2cb2d [file] [log] [blame]
NeilBrown9d09e662011-01-13 20:00:02 +00001/*
2 * Copyright (C) 2010-2011 Neil Brown
3 * Copyright (C) 2010-2011 Red Hat, Inc. All rights reserved.
4 *
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"
NeilBrown9d09e662011-01-13 20:00:02 +000014#include "bitmap.h"
15
Alasdair G Kergon3e8dbb72011-08-02 12:32:03 +010016#include <linux/device-mapper.h>
17
NeilBrown9d09e662011-01-13 20:00:02 +000018#define DM_MSG_PREFIX "raid"
19
20/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +010021 * The following flags are used by dm-raid.c to set up the array state.
22 * They must be cleared before md_run is called.
NeilBrown9d09e662011-01-13 20:00:02 +000023 */
Jonathan Brassowb12d4372011-08-02 12:32:07 +010024#define FirstUse 10 /* rdev flag */
NeilBrown9d09e662011-01-13 20:00:02 +000025
26struct raid_dev {
27 /*
28 * Two DM devices, one to hold metadata and one to hold the
29 * actual data/parity. The reason for this is to not confuse
30 * ti->len and give more flexibility in altering size and
31 * characteristics.
32 *
33 * While it is possible for this device to be associated
34 * with a different physical device than the data_dev, it
35 * is intended for it to be the same.
36 * |--------- Physical Device ---------|
37 * |- meta_dev -|------ data_dev ------|
38 */
39 struct dm_dev *meta_dev;
40 struct dm_dev *data_dev;
NeilBrown3cb03002011-10-11 16:45:26 +110041 struct md_rdev rdev;
NeilBrown9d09e662011-01-13 20:00:02 +000042};
43
44/*
45 * Flags for rs->print_flags field.
46 */
Jonathan Brassow13c87582011-08-02 12:32:03 +010047#define DMPF_SYNC 0x1
48#define DMPF_NOSYNC 0x2
49#define DMPF_REBUILD 0x4
50#define DMPF_DAEMON_SLEEP 0x8
51#define DMPF_MIN_RECOVERY_RATE 0x10
52#define DMPF_MAX_RECOVERY_RATE 0x20
53#define DMPF_MAX_WRITE_BEHIND 0x40
54#define DMPF_STRIPE_CACHE 0x80
Jonathan Brassowc1084562011-08-02 12:32:07 +010055#define DMPF_REGION_SIZE 0X100
NeilBrown9d09e662011-01-13 20:00:02 +000056struct raid_set {
57 struct dm_target *ti;
58
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -060059 uint32_t bitmap_loaded;
60 uint32_t print_flags;
NeilBrown9d09e662011-01-13 20:00:02 +000061
NeilBrownfd01b882011-10-11 16:47:53 +110062 struct mddev md;
NeilBrown9d09e662011-01-13 20:00:02 +000063 struct raid_type *raid_type;
64 struct dm_target_callbacks callbacks;
65
66 struct raid_dev dev[0];
67};
68
69/* Supported raid types and properties. */
70static struct raid_type {
71 const char *name; /* RAID algorithm. */
72 const char *descr; /* Descriptor text for logging. */
73 const unsigned parity_devs; /* # of parity devices. */
74 const unsigned minimal_devs; /* minimal # of devices in set. */
75 const unsigned level; /* RAID level. */
76 const unsigned algorithm; /* RAID algorithm. */
77} raid_types[] = {
Jonathan Brassow32737272011-08-02 12:32:07 +010078 {"raid1", "RAID1 (mirroring)", 0, 2, 1, 0 /* NONE */},
NeilBrown9d09e662011-01-13 20:00:02 +000079 {"raid4", "RAID4 (dedicated parity disk)", 1, 2, 5, ALGORITHM_PARITY_0},
80 {"raid5_la", "RAID5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
81 {"raid5_ra", "RAID5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
82 {"raid5_ls", "RAID5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
83 {"raid5_rs", "RAID5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
84 {"raid6_zr", "RAID6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
85 {"raid6_nr", "RAID6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
86 {"raid6_nc", "RAID6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
87};
88
89static struct raid_type *get_raid_type(char *name)
90{
91 int i;
92
93 for (i = 0; i < ARRAY_SIZE(raid_types); i++)
94 if (!strcmp(raid_types[i].name, name))
95 return &raid_types[i];
96
97 return NULL;
98}
99
100static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
101{
102 unsigned i;
103 struct raid_set *rs;
NeilBrown9d09e662011-01-13 20:00:02 +0000104
105 if (raid_devs <= raid_type->parity_devs) {
106 ti->error = "Insufficient number of devices";
107 return ERR_PTR(-EINVAL);
108 }
109
NeilBrown9d09e662011-01-13 20:00:02 +0000110 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
111 if (!rs) {
112 ti->error = "Cannot allocate raid context";
113 return ERR_PTR(-ENOMEM);
114 }
115
116 mddev_init(&rs->md);
117
118 rs->ti = ti;
119 rs->raid_type = raid_type;
120 rs->md.raid_disks = raid_devs;
121 rs->md.level = raid_type->level;
122 rs->md.new_level = rs->md.level;
NeilBrown9d09e662011-01-13 20:00:02 +0000123 rs->md.layout = raid_type->algorithm;
124 rs->md.new_layout = rs->md.layout;
125 rs->md.delta_disks = 0;
126 rs->md.recovery_cp = 0;
127
128 for (i = 0; i < raid_devs; i++)
129 md_rdev_init(&rs->dev[i].rdev);
130
131 /*
132 * Remaining items to be initialized by further RAID params:
133 * rs->md.persistent
134 * rs->md.external
135 * rs->md.chunk_sectors
136 * rs->md.new_chunk_sectors
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100137 * rs->md.dev_sectors
NeilBrown9d09e662011-01-13 20:00:02 +0000138 */
139
140 return rs;
141}
142
143static void context_free(struct raid_set *rs)
144{
145 int i;
146
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100147 for (i = 0; i < rs->md.raid_disks; i++) {
148 if (rs->dev[i].meta_dev)
149 dm_put_device(rs->ti, rs->dev[i].meta_dev);
NeilBrown545c8792012-05-22 13:54:30 +1000150 md_rdev_clear(&rs->dev[i].rdev);
NeilBrown9d09e662011-01-13 20:00:02 +0000151 if (rs->dev[i].data_dev)
152 dm_put_device(rs->ti, rs->dev[i].data_dev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100153 }
NeilBrown9d09e662011-01-13 20:00:02 +0000154
155 kfree(rs);
156}
157
158/*
159 * For every device we have two words
160 * <meta_dev>: meta device name or '-' if missing
161 * <data_dev>: data device name or '-' if missing
162 *
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100163 * The following are permitted:
164 * - -
165 * - <data_dev>
166 * <meta_dev> <data_dev>
167 *
168 * The following is not allowed:
169 * <meta_dev> -
170 *
171 * This code parses those words. If there is a failure,
172 * the caller must use context_free to unwind the operations.
NeilBrown9d09e662011-01-13 20:00:02 +0000173 */
174static int dev_parms(struct raid_set *rs, char **argv)
175{
176 int i;
177 int rebuild = 0;
178 int metadata_available = 0;
179 int ret = 0;
180
181 for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {
182 rs->dev[i].rdev.raid_disk = i;
183
184 rs->dev[i].meta_dev = NULL;
185 rs->dev[i].data_dev = NULL;
186
187 /*
188 * There are no offsets, since there is a separate device
189 * for data and metadata.
190 */
191 rs->dev[i].rdev.data_offset = 0;
192 rs->dev[i].rdev.mddev = &rs->md;
193
194 if (strcmp(argv[0], "-")) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100195 ret = dm_get_device(rs->ti, argv[0],
196 dm_table_get_mode(rs->ti->table),
197 &rs->dev[i].meta_dev);
198 rs->ti->error = "RAID metadata device lookup failure";
199 if (ret)
200 return ret;
201
202 rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
203 if (!rs->dev[i].rdev.sb_page)
204 return -ENOMEM;
NeilBrown9d09e662011-01-13 20:00:02 +0000205 }
206
207 if (!strcmp(argv[1], "-")) {
208 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
209 (!rs->dev[i].rdev.recovery_offset)) {
210 rs->ti->error = "Drive designated for rebuild not specified";
211 return -EINVAL;
212 }
213
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100214 rs->ti->error = "No data device supplied with metadata device";
215 if (rs->dev[i].meta_dev)
216 return -EINVAL;
217
NeilBrown9d09e662011-01-13 20:00:02 +0000218 continue;
219 }
220
221 ret = dm_get_device(rs->ti, argv[1],
222 dm_table_get_mode(rs->ti->table),
223 &rs->dev[i].data_dev);
224 if (ret) {
225 rs->ti->error = "RAID device lookup failure";
226 return ret;
227 }
228
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100229 if (rs->dev[i].meta_dev) {
230 metadata_available = 1;
231 rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
232 }
NeilBrown9d09e662011-01-13 20:00:02 +0000233 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
234 list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
235 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
236 rebuild++;
237 }
238
239 if (metadata_available) {
240 rs->md.external = 0;
241 rs->md.persistent = 1;
242 rs->md.major_version = 2;
243 } else if (rebuild && !rs->md.recovery_cp) {
244 /*
245 * Without metadata, we will not be able to tell if the array
246 * is in-sync or not - we must assume it is not. Therefore,
247 * it is impossible to rebuild a drive.
248 *
249 * Even if there is metadata, the on-disk information may
250 * indicate that the array is not in-sync and it will then
251 * fail at that time.
252 *
253 * User could specify 'nosync' option if desperate.
254 */
255 DMERR("Unable to rebuild drive while array is not in-sync");
256 rs->ti->error = "RAID device lookup failure";
257 return -EINVAL;
258 }
259
260 return 0;
261}
262
263/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100264 * validate_region_size
265 * @rs
266 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
267 *
268 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
269 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
270 *
271 * Returns: 0 on success, -EINVAL on failure.
272 */
273static int validate_region_size(struct raid_set *rs, unsigned long region_size)
274{
275 unsigned long min_region_size = rs->ti->len / (1 << 21);
276
277 if (!region_size) {
278 /*
279 * Choose a reasonable default. All figures in sectors.
280 */
281 if (min_region_size > (1 << 13)) {
282 DMINFO("Choosing default region size of %lu sectors",
283 region_size);
284 region_size = min_region_size;
285 } else {
286 DMINFO("Choosing default region size of 4MiB");
287 region_size = 1 << 13; /* sectors */
288 }
289 } else {
290 /*
291 * Validate user-supplied value.
292 */
293 if (region_size > rs->ti->len) {
294 rs->ti->error = "Supplied region size is too large";
295 return -EINVAL;
296 }
297
298 if (region_size < min_region_size) {
299 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
300 region_size, min_region_size);
301 rs->ti->error = "Supplied region size is too small";
302 return -EINVAL;
303 }
304
305 if (!is_power_of_2(region_size)) {
306 rs->ti->error = "Region size is not a power of 2";
307 return -EINVAL;
308 }
309
310 if (region_size < rs->md.chunk_sectors) {
311 rs->ti->error = "Region size is smaller than the chunk size";
312 return -EINVAL;
313 }
314 }
315
316 /*
317 * Convert sectors to bytes.
318 */
319 rs->md.bitmap_info.chunksize = (region_size << 9);
320
321 return 0;
322}
323
324/*
NeilBrown9d09e662011-01-13 20:00:02 +0000325 * Possible arguments are...
NeilBrown9d09e662011-01-13 20:00:02 +0000326 * <chunk_size> [optional_args]
327 *
Jonathan Brassow32737272011-08-02 12:32:07 +0100328 * Argument definitions
329 * <chunk_size> The number of sectors per disk that
330 * will form the "stripe"
331 * [[no]sync] Force or prevent recovery of the
332 * entire array
NeilBrown9d09e662011-01-13 20:00:02 +0000333 * [rebuild <idx>] Rebuild the drive indicated by the index
Jonathan Brassow32737272011-08-02 12:32:07 +0100334 * [daemon_sleep <ms>] Time between bitmap daemon work to
335 * clear bits
NeilBrown9d09e662011-01-13 20:00:02 +0000336 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
337 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100338 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +0000339 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
340 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Jonathan Brassowc1084562011-08-02 12:32:07 +0100341 * [region_size <sectors>] Defines granularity of bitmap
NeilBrown9d09e662011-01-13 20:00:02 +0000342 */
343static int parse_raid_params(struct raid_set *rs, char **argv,
344 unsigned num_raid_params)
345{
346 unsigned i, rebuild_cnt = 0;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100347 unsigned long value, region_size = 0;
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100348 sector_t sectors_per_dev = rs->ti->len;
Mike Snitzer542f9032012-07-27 15:08:00 +0100349 sector_t max_io_len;
NeilBrown9d09e662011-01-13 20:00:02 +0000350 char *key;
351
352 /*
353 * First, parse the in-order required arguments
Jonathan Brassow32737272011-08-02 12:32:07 +0100354 * "chunk_size" is the only argument of this type.
NeilBrown9d09e662011-01-13 20:00:02 +0000355 */
Jonathan Brassow32737272011-08-02 12:32:07 +0100356 if ((strict_strtoul(argv[0], 10, &value) < 0)) {
NeilBrown9d09e662011-01-13 20:00:02 +0000357 rs->ti->error = "Bad chunk size";
358 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +0100359 } else if (rs->raid_type->level == 1) {
360 if (value)
361 DMERR("Ignoring chunk size parameter for RAID 1");
362 value = 0;
363 } else if (!is_power_of_2(value)) {
364 rs->ti->error = "Chunk size must be a power of 2";
365 return -EINVAL;
366 } else if (value < 8) {
367 rs->ti->error = "Chunk size value is too small";
368 return -EINVAL;
NeilBrown9d09e662011-01-13 20:00:02 +0000369 }
370
371 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
372 argv++;
373 num_raid_params--;
374
375 /*
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100376 * We set each individual device as In_sync with a completed
377 * 'recovery_offset'. If there has been a device failure or
378 * replacement then one of the following cases applies:
379 *
380 * 1) User specifies 'rebuild'.
381 * - Device is reset when param is read.
382 * 2) A new device is supplied.
383 * - No matching superblock found, resets device.
384 * 3) Device failure was transient and returns on reload.
385 * - Failure noticed, resets device for bitmap replay.
386 * 4) Device hadn't completed recovery after previous failure.
387 * - Superblock is read and overrides recovery_offset.
388 *
389 * What is found in the superblocks of the devices is always
390 * authoritative, unless 'rebuild' or '[no]sync' was specified.
391 */
392 for (i = 0; i < rs->md.raid_disks; i++) {
393 set_bit(In_sync, &rs->dev[i].rdev.flags);
394 rs->dev[i].rdev.recovery_offset = MaxSector;
395 }
396
397 /*
NeilBrown9d09e662011-01-13 20:00:02 +0000398 * Second, parse the unordered optional arguments
399 */
NeilBrown9d09e662011-01-13 20:00:02 +0000400 for (i = 0; i < num_raid_params; i++) {
Jonathan Brassow13c87582011-08-02 12:32:03 +0100401 if (!strcasecmp(argv[i], "nosync")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000402 rs->md.recovery_cp = MaxSector;
403 rs->print_flags |= DMPF_NOSYNC;
NeilBrown9d09e662011-01-13 20:00:02 +0000404 continue;
405 }
Jonathan Brassow13c87582011-08-02 12:32:03 +0100406 if (!strcasecmp(argv[i], "sync")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000407 rs->md.recovery_cp = 0;
408 rs->print_flags |= DMPF_SYNC;
NeilBrown9d09e662011-01-13 20:00:02 +0000409 continue;
410 }
411
412 /* The rest of the optional arguments come in key/value pairs */
413 if ((i + 1) >= num_raid_params) {
414 rs->ti->error = "Wrong number of raid parameters given";
415 return -EINVAL;
416 }
417
418 key = argv[i++];
419 if (strict_strtoul(argv[i], 10, &value) < 0) {
420 rs->ti->error = "Bad numerical argument given in raid params";
421 return -EINVAL;
422 }
423
Jonathan Brassow13c87582011-08-02 12:32:03 +0100424 if (!strcasecmp(key, "rebuild")) {
Jonathan Brassow32737272011-08-02 12:32:07 +0100425 rebuild_cnt++;
Jonathan E Brassowf999e8f2012-07-27 15:08:04 +0100426
427 switch (rs->raid_type->level) {
428 case 1:
429 if (rebuild_cnt >= rs->md.raid_disks) {
430 rs->ti->error = "Too many rebuild devices specified";
431 return -EINVAL;
432 }
433 break;
434 case 4:
435 case 5:
436 case 6:
437 if (rebuild_cnt > rs->raid_type->parity_devs) {
438 rs->ti->error = "Too many rebuild devices specified for given RAID type";
439 return -EINVAL;
440 }
441 break;
442 default:
443 DMERR("The rebuild parameter is not supported for %s", rs->raid_type->name);
444 rs->ti->error = "Rebuild not supported for this RAID type";
NeilBrown9d09e662011-01-13 20:00:02 +0000445 return -EINVAL;
446 }
Jonathan E Brassowf999e8f2012-07-27 15:08:04 +0100447
NeilBrown9d09e662011-01-13 20:00:02 +0000448 if (value > rs->md.raid_disks) {
449 rs->ti->error = "Invalid rebuild index given";
450 return -EINVAL;
451 }
452 clear_bit(In_sync, &rs->dev[value].rdev.flags);
453 rs->dev[value].rdev.recovery_offset = 0;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100454 rs->print_flags |= DMPF_REBUILD;
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100455 } else if (!strcasecmp(key, "write_mostly")) {
456 if (rs->raid_type->level != 1) {
457 rs->ti->error = "write_mostly option is only valid for RAID1";
458 return -EINVAL;
459 }
Jonthan Brassow82324802011-09-25 23:26:19 +0100460 if (value >= rs->md.raid_disks) {
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100461 rs->ti->error = "Invalid write_mostly drive index given";
462 return -EINVAL;
463 }
464 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Jonathan Brassow13c87582011-08-02 12:32:03 +0100465 } else if (!strcasecmp(key, "max_write_behind")) {
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100466 if (rs->raid_type->level != 1) {
467 rs->ti->error = "max_write_behind option is only valid for RAID1";
468 return -EINVAL;
469 }
NeilBrown9d09e662011-01-13 20:00:02 +0000470 rs->print_flags |= DMPF_MAX_WRITE_BEHIND;
471
472 /*
473 * In device-mapper, we specify things in sectors, but
474 * MD records this value in kB
475 */
476 value /= 2;
477 if (value > COUNTER_MAX) {
478 rs->ti->error = "Max write-behind limit out of range";
479 return -EINVAL;
480 }
481 rs->md.bitmap_info.max_write_behind = value;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100482 } else if (!strcasecmp(key, "daemon_sleep")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000483 rs->print_flags |= DMPF_DAEMON_SLEEP;
484 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
485 rs->ti->error = "daemon sleep period out of range";
486 return -EINVAL;
487 }
488 rs->md.bitmap_info.daemon_sleep = value;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100489 } else if (!strcasecmp(key, "stripe_cache")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000490 rs->print_flags |= DMPF_STRIPE_CACHE;
491
492 /*
493 * In device-mapper, we specify things in sectors, but
494 * MD records this value in kB
495 */
496 value /= 2;
497
498 if (rs->raid_type->level < 5) {
499 rs->ti->error = "Inappropriate argument: stripe_cache";
500 return -EINVAL;
501 }
502 if (raid5_set_cache_size(&rs->md, (int)value)) {
503 rs->ti->error = "Bad stripe_cache size";
504 return -EINVAL;
505 }
Jonathan Brassow13c87582011-08-02 12:32:03 +0100506 } else if (!strcasecmp(key, "min_recovery_rate")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000507 rs->print_flags |= DMPF_MIN_RECOVERY_RATE;
508 if (value > INT_MAX) {
509 rs->ti->error = "min_recovery_rate out of range";
510 return -EINVAL;
511 }
512 rs->md.sync_speed_min = (int)value;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100513 } else if (!strcasecmp(key, "max_recovery_rate")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000514 rs->print_flags |= DMPF_MAX_RECOVERY_RATE;
515 if (value > INT_MAX) {
516 rs->ti->error = "max_recovery_rate out of range";
517 return -EINVAL;
518 }
519 rs->md.sync_speed_max = (int)value;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100520 } else if (!strcasecmp(key, "region_size")) {
521 rs->print_flags |= DMPF_REGION_SIZE;
522 region_size = value;
NeilBrown9d09e662011-01-13 20:00:02 +0000523 } else {
524 DMERR("Unable to parse RAID parameter: %s", key);
525 rs->ti->error = "Unable to parse RAID parameters";
526 return -EINVAL;
527 }
528 }
529
Jonathan Brassowc1084562011-08-02 12:32:07 +0100530 if (validate_region_size(rs, region_size))
531 return -EINVAL;
532
533 if (rs->md.chunk_sectors)
Mike Snitzer542f9032012-07-27 15:08:00 +0100534 max_io_len = rs->md.chunk_sectors;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100535 else
Mike Snitzer542f9032012-07-27 15:08:00 +0100536 max_io_len = region_size;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100537
Mike Snitzer542f9032012-07-27 15:08:00 +0100538 if (dm_set_target_max_io_len(rs->ti, max_io_len))
539 return -EINVAL;
Jonathan Brassow32737272011-08-02 12:32:07 +0100540
Jonathan E Brassowc039c332012-07-27 15:08:04 +0100541 if ((rs->raid_type->level > 1) &&
542 sector_div(sectors_per_dev, (rs->md.raid_disks - rs->raid_type->parity_devs))) {
543 rs->ti->error = "Target length not divisible by number of data devices";
544 return -EINVAL;
545 }
546 rs->md.dev_sectors = sectors_per_dev;
547
NeilBrown9d09e662011-01-13 20:00:02 +0000548 /* Assume there are no metadata devices until the drives are parsed */
549 rs->md.persistent = 0;
550 rs->md.external = 1;
551
552 return 0;
553}
554
555static void do_table_event(struct work_struct *ws)
556{
557 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
558
559 dm_table_event(rs->ti->table);
560}
561
562static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
563{
564 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
565
Jonathan Brassow32737272011-08-02 12:32:07 +0100566 if (rs->raid_type->level == 1)
567 return md_raid1_congested(&rs->md, bits);
568
NeilBrown9d09e662011-01-13 20:00:02 +0000569 return md_raid5_congested(&rs->md, bits);
570}
571
NeilBrown9d09e662011-01-13 20:00:02 +0000572/*
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100573 * This structure is never routinely used by userspace, unlike md superblocks.
574 * Devices with this superblock should only ever be accessed via device-mapper.
575 */
576#define DM_RAID_MAGIC 0x64526D44
577struct dm_raid_superblock {
578 __le32 magic; /* "DmRd" */
579 __le32 features; /* Used to indicate possible future changes */
580
581 __le32 num_devices; /* Number of devices in this array. (Max 64) */
582 __le32 array_position; /* The position of this drive in the array */
583
584 __le64 events; /* Incremented by md when superblock updated */
585 __le64 failed_devices; /* Bit field of devices to indicate failures */
586
587 /*
588 * This offset tracks the progress of the repair or replacement of
589 * an individual drive.
590 */
591 __le64 disk_recovery_offset;
592
593 /*
594 * This offset tracks the progress of the initial array
595 * synchronisation/parity calculation.
596 */
597 __le64 array_resync_offset;
598
599 /*
600 * RAID characteristics
601 */
602 __le32 level;
603 __le32 layout;
604 __le32 stripe_sectors;
605
606 __u8 pad[452]; /* Round struct to 512 bytes. */
607 /* Always set to 0 when writing. */
608} __packed;
609
NeilBrown3cb03002011-10-11 16:45:26 +1100610static int read_disk_sb(struct md_rdev *rdev, int size)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100611{
612 BUG_ON(!rdev->sb_page);
613
614 if (rdev->sb_loaded)
615 return 0;
616
617 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 1)) {
Jonathan E Brassow04475682012-03-28 18:41:26 +0100618 DMERR("Failed to read superblock of device at position %d",
619 rdev->raid_disk);
Jonathan Brassowc32fb9e2012-05-22 13:55:31 +1000620 md_error(rdev->mddev, rdev);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100621 return -EINVAL;
622 }
623
624 rdev->sb_loaded = 1;
625
626 return 0;
627}
628
NeilBrownfd01b882011-10-11 16:47:53 +1100629static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100630{
Jonathan Brassow81f382f2012-05-22 13:55:30 +1000631 int i;
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100632 uint64_t failed_devices;
633 struct dm_raid_superblock *sb;
Jonathan Brassow81f382f2012-05-22 13:55:30 +1000634 struct raid_set *rs = container_of(mddev, struct raid_set, md);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100635
636 sb = page_address(rdev->sb_page);
637 failed_devices = le64_to_cpu(sb->failed_devices);
638
Jonathan Brassow81f382f2012-05-22 13:55:30 +1000639 for (i = 0; i < mddev->raid_disks; i++)
640 if (!rs->dev[i].data_dev ||
641 test_bit(Faulty, &(rs->dev[i].rdev.flags)))
642 failed_devices |= (1ULL << i);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100643
644 memset(sb, 0, sizeof(*sb));
645
646 sb->magic = cpu_to_le32(DM_RAID_MAGIC);
647 sb->features = cpu_to_le32(0); /* No features yet */
648
649 sb->num_devices = cpu_to_le32(mddev->raid_disks);
650 sb->array_position = cpu_to_le32(rdev->raid_disk);
651
652 sb->events = cpu_to_le64(mddev->events);
653 sb->failed_devices = cpu_to_le64(failed_devices);
654
655 sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
656 sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
657
658 sb->level = cpu_to_le32(mddev->level);
659 sb->layout = cpu_to_le32(mddev->layout);
660 sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
661}
662
663/*
664 * super_load
665 *
666 * This function creates a superblock if one is not found on the device
667 * and will decide which superblock to use if there's a choice.
668 *
669 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
670 */
NeilBrown3cb03002011-10-11 16:45:26 +1100671static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100672{
673 int ret;
674 struct dm_raid_superblock *sb;
675 struct dm_raid_superblock *refsb;
676 uint64_t events_sb, events_refsb;
677
678 rdev->sb_start = 0;
679 rdev->sb_size = sizeof(*sb);
680
681 ret = read_disk_sb(rdev, rdev->sb_size);
682 if (ret)
683 return ret;
684
685 sb = page_address(rdev->sb_page);
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +0000686
687 /*
688 * Two cases that we want to write new superblocks and rebuild:
689 * 1) New device (no matching magic number)
690 * 2) Device specified for rebuild (!In_sync w/ offset == 0)
691 */
692 if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
693 (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100694 super_sync(rdev->mddev, rdev);
695
696 set_bit(FirstUse, &rdev->flags);
697
698 /* Force writing of superblocks to disk */
699 set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
700
701 /* Any superblock is better than none, choose that if given */
702 return refdev ? 0 : 1;
703 }
704
705 if (!refdev)
706 return 1;
707
708 events_sb = le64_to_cpu(sb->events);
709
710 refsb = page_address(refdev->sb_page);
711 events_refsb = le64_to_cpu(refsb->events);
712
713 return (events_sb > events_refsb) ? 1 : 0;
714}
715
NeilBrownfd01b882011-10-11 16:47:53 +1100716static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100717{
718 int role;
719 struct raid_set *rs = container_of(mddev, struct raid_set, md);
720 uint64_t events_sb;
721 uint64_t failed_devices;
722 struct dm_raid_superblock *sb;
723 uint32_t new_devs = 0;
724 uint32_t rebuilds = 0;
NeilBrowndafb20f2012-03-19 12:46:39 +1100725 struct md_rdev *r;
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100726 struct dm_raid_superblock *sb2;
727
728 sb = page_address(rdev->sb_page);
729 events_sb = le64_to_cpu(sb->events);
730 failed_devices = le64_to_cpu(sb->failed_devices);
731
732 /*
733 * Initialise to 1 if this is a new superblock.
734 */
735 mddev->events = events_sb ? : 1;
736
737 /*
738 * Reshaping is not currently allowed
739 */
740 if ((le32_to_cpu(sb->level) != mddev->level) ||
741 (le32_to_cpu(sb->layout) != mddev->layout) ||
742 (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors)) {
743 DMERR("Reshaping arrays not yet supported.");
744 return -EINVAL;
745 }
746
747 /* We can only change the number of devices in RAID1 right now */
748 if ((rs->raid_type->level != 1) &&
749 (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
750 DMERR("Reshaping arrays not yet supported.");
751 return -EINVAL;
752 }
753
754 if (!(rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC)))
755 mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
756
757 /*
758 * During load, we set FirstUse if a new superblock was written.
759 * There are two reasons we might not have a superblock:
760 * 1) The array is brand new - in which case, all of the
761 * devices must have their In_sync bit set. Also,
762 * recovery_cp must be 0, unless forced.
763 * 2) This is a new device being added to an old array
764 * and the new device needs to be rebuilt - in which
765 * case the In_sync bit will /not/ be set and
766 * recovery_cp must be MaxSector.
767 */
NeilBrowndafb20f2012-03-19 12:46:39 +1100768 rdev_for_each(r, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100769 if (!test_bit(In_sync, &r->flags)) {
Jonathan E Brassow3aa3b2b2012-03-07 19:09:47 +0000770 DMINFO("Device %d specified for rebuild: "
771 "Clearing superblock", r->raid_disk);
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100772 rebuilds++;
773 } else if (test_bit(FirstUse, &r->flags))
774 new_devs++;
775 }
776
777 if (!rebuilds) {
778 if (new_devs == mddev->raid_disks) {
779 DMINFO("Superblocks created for new array");
780 set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
781 } else if (new_devs) {
782 DMERR("New device injected "
783 "into existing array without 'rebuild' "
784 "parameter specified");
785 return -EINVAL;
786 }
787 } else if (new_devs) {
788 DMERR("'rebuild' devices cannot be "
789 "injected into an array with other first-time devices");
790 return -EINVAL;
791 } else if (mddev->recovery_cp != MaxSector) {
792 DMERR("'rebuild' specified while array is not in-sync");
793 return -EINVAL;
794 }
795
796 /*
797 * Now we set the Faulty bit for those devices that are
798 * recorded in the superblock as failed.
799 */
NeilBrowndafb20f2012-03-19 12:46:39 +1100800 rdev_for_each(r, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100801 if (!r->sb_page)
802 continue;
803 sb2 = page_address(r->sb_page);
804 sb2->failed_devices = 0;
805
806 /*
807 * Check for any device re-ordering.
808 */
809 if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
810 role = le32_to_cpu(sb2->array_position);
811 if (role != r->raid_disk) {
812 if (rs->raid_type->level != 1) {
813 rs->ti->error = "Cannot change device "
814 "positions in RAID array";
815 return -EINVAL;
816 }
817 DMINFO("RAID1 device #%d now at position #%d",
818 role, r->raid_disk);
819 }
820
821 /*
822 * Partial recovery is performed on
823 * returning failed devices.
824 */
825 if (failed_devices & (1 << role))
826 set_bit(Faulty, &r->flags);
827 }
828 }
829
830 return 0;
831}
832
NeilBrownfd01b882011-10-11 16:47:53 +1100833static int super_validate(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100834{
835 struct dm_raid_superblock *sb = page_address(rdev->sb_page);
836
837 /*
838 * If mddev->events is not set, we know we have not yet initialized
839 * the array.
840 */
841 if (!mddev->events && super_init_validation(mddev, rdev))
842 return -EINVAL;
843
844 mddev->bitmap_info.offset = 4096 >> 9; /* Enable bitmap creation */
845 rdev->mddev->bitmap_info.default_offset = 4096 >> 9;
846 if (!test_bit(FirstUse, &rdev->flags)) {
847 rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
848 if (rdev->recovery_offset != MaxSector)
849 clear_bit(In_sync, &rdev->flags);
850 }
851
852 /*
853 * If a device comes back, set it as not In_sync and no longer faulty.
854 */
855 if (test_bit(Faulty, &rdev->flags)) {
856 clear_bit(Faulty, &rdev->flags);
857 clear_bit(In_sync, &rdev->flags);
858 rdev->saved_raid_disk = rdev->raid_disk;
859 rdev->recovery_offset = 0;
860 }
861
862 clear_bit(FirstUse, &rdev->flags);
863
864 return 0;
865}
866
867/*
868 * Analyse superblocks and select the freshest.
869 */
870static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
871{
872 int ret;
Jonathan E Brassow04475682012-03-28 18:41:26 +0100873 unsigned redundancy = 0;
874 struct raid_dev *dev;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +1000875 struct md_rdev *rdev, *tmp, *freshest;
NeilBrownfd01b882011-10-11 16:47:53 +1100876 struct mddev *mddev = &rs->md;
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100877
Jonathan E Brassow04475682012-03-28 18:41:26 +0100878 switch (rs->raid_type->level) {
879 case 1:
880 redundancy = rs->md.raid_disks - 1;
881 break;
882 case 4:
883 case 5:
884 case 6:
885 redundancy = rs->raid_type->parity_devs;
886 break;
887 default:
888 ti->error = "Unknown RAID type";
889 return -EINVAL;
890 }
891
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100892 freshest = NULL;
Jonathan Brassowa9ad8522012-04-24 10:23:13 +1000893 rdev_for_each_safe(rdev, tmp, mddev) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100894 if (!rdev->meta_bdev)
895 continue;
896
897 ret = super_load(rdev, freshest);
898
899 switch (ret) {
900 case 1:
901 freshest = rdev;
902 break;
903 case 0:
904 break;
905 default:
Jonathan E Brassow04475682012-03-28 18:41:26 +0100906 dev = container_of(rdev, struct raid_dev, rdev);
907 if (redundancy--) {
908 if (dev->meta_dev)
909 dm_put_device(ti, dev->meta_dev);
910
911 dev->meta_dev = NULL;
912 rdev->meta_bdev = NULL;
913
914 if (rdev->sb_page)
915 put_page(rdev->sb_page);
916
917 rdev->sb_page = NULL;
918
919 rdev->sb_loaded = 0;
920
921 /*
922 * We might be able to salvage the data device
923 * even though the meta device has failed. For
924 * now, we behave as though '- -' had been
925 * set for this device in the table.
926 */
927 if (dev->data_dev)
928 dm_put_device(ti, dev->data_dev);
929
930 dev->data_dev = NULL;
931 rdev->bdev = NULL;
932
933 list_del(&rdev->same_set);
934
935 continue;
936 }
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100937 ti->error = "Failed to load superblock";
938 return ret;
939 }
940 }
941
942 if (!freshest)
943 return 0;
944
945 /*
946 * Validation of the freshest device provides the source of
947 * validation for the remaining devices.
948 */
949 ti->error = "Unable to assemble array: Invalid superblocks";
950 if (super_validate(mddev, freshest))
951 return -EINVAL;
952
NeilBrowndafb20f2012-03-19 12:46:39 +1100953 rdev_for_each(rdev, mddev)
Jonathan Brassowb12d4372011-08-02 12:32:07 +0100954 if ((rdev != freshest) && super_validate(mddev, rdev))
955 return -EINVAL;
956
957 return 0;
958}
959
960/*
NeilBrown9d09e662011-01-13 20:00:02 +0000961 * Construct a RAID4/5/6 mapping:
962 * Args:
963 * <raid_type> <#raid_params> <raid_params> \
964 * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
965 *
NeilBrown9d09e662011-01-13 20:00:02 +0000966 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
967 * details on possible <raid_params>.
968 */
969static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
970{
971 int ret;
972 struct raid_type *rt;
973 unsigned long num_raid_params, num_raid_devs;
974 struct raid_set *rs = NULL;
975
976 /* Must have at least <raid_type> <#raid_params> */
977 if (argc < 2) {
978 ti->error = "Too few arguments";
979 return -EINVAL;
980 }
981
982 /* raid type */
983 rt = get_raid_type(argv[0]);
984 if (!rt) {
985 ti->error = "Unrecognised raid_type";
986 return -EINVAL;
987 }
988 argc--;
989 argv++;
990
991 /* number of RAID parameters */
992 if (strict_strtoul(argv[0], 10, &num_raid_params) < 0) {
993 ti->error = "Cannot understand number of RAID parameters";
994 return -EINVAL;
995 }
996 argc--;
997 argv++;
998
999 /* Skip over RAID params for now and find out # of devices */
1000 if (num_raid_params + 1 > argc) {
1001 ti->error = "Arguments do not agree with counts given";
1002 return -EINVAL;
1003 }
1004
1005 if ((strict_strtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) ||
1006 (num_raid_devs >= INT_MAX)) {
1007 ti->error = "Cannot understand number of raid devices";
1008 return -EINVAL;
1009 }
1010
1011 rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
1012 if (IS_ERR(rs))
1013 return PTR_ERR(rs);
1014
1015 ret = parse_raid_params(rs, argv, (unsigned)num_raid_params);
1016 if (ret)
1017 goto bad;
1018
1019 ret = -EINVAL;
1020
1021 argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
1022 argv += num_raid_params + 1;
1023
1024 if (argc != (num_raid_devs * 2)) {
1025 ti->error = "Supplied RAID devices does not match the count given";
1026 goto bad;
1027 }
1028
1029 ret = dev_parms(rs, argv);
1030 if (ret)
1031 goto bad;
1032
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001033 rs->md.sync_super = super_sync;
1034 ret = analyse_superblocks(ti, rs);
1035 if (ret)
1036 goto bad;
1037
NeilBrown9d09e662011-01-13 20:00:02 +00001038 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +00001039 ti->private = rs;
Jonathan E Brassow0ca93de2012-03-07 19:09:48 +00001040 ti->num_flush_requests = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00001041
1042 mutex_lock(&rs->md.reconfig_mutex);
1043 ret = md_run(&rs->md);
1044 rs->md.in_sync = 0; /* Assume already marked dirty */
1045 mutex_unlock(&rs->md.reconfig_mutex);
1046
1047 if (ret) {
1048 ti->error = "Fail to run raid array";
1049 goto bad;
1050 }
1051
1052 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +00001053 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
1054
Jonathan Brassow32737272011-08-02 12:32:07 +01001055 mddev_suspend(&rs->md);
NeilBrown9d09e662011-01-13 20:00:02 +00001056 return 0;
1057
1058bad:
1059 context_free(rs);
1060
1061 return ret;
1062}
1063
1064static void raid_dtr(struct dm_target *ti)
1065{
1066 struct raid_set *rs = ti->private;
1067
1068 list_del_init(&rs->callbacks.list);
1069 md_stop(&rs->md);
1070 context_free(rs);
1071}
1072
1073static int raid_map(struct dm_target *ti, struct bio *bio, union map_info *map_context)
1074{
1075 struct raid_set *rs = ti->private;
NeilBrownfd01b882011-10-11 16:47:53 +11001076 struct mddev *mddev = &rs->md;
NeilBrown9d09e662011-01-13 20:00:02 +00001077
1078 mddev->pers->make_request(mddev, bio);
1079
1080 return DM_MAPIO_SUBMITTED;
1081}
1082
1083static int raid_status(struct dm_target *ti, status_type_t type,
1084 char *result, unsigned maxlen)
1085{
1086 struct raid_set *rs = ti->private;
1087 unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
1088 unsigned sz = 0;
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001089 int i, array_in_sync = 0;
NeilBrown9d09e662011-01-13 20:00:02 +00001090 sector_t sync;
1091
1092 switch (type) {
1093 case STATUSTYPE_INFO:
1094 DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
1095
NeilBrown9d09e662011-01-13 20:00:02 +00001096 if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
1097 sync = rs->md.curr_resync_completed;
1098 else
1099 sync = rs->md.recovery_cp;
1100
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001101 if (sync >= rs->md.resync_max_sectors) {
1102 array_in_sync = 1;
NeilBrown9d09e662011-01-13 20:00:02 +00001103 sync = rs->md.resync_max_sectors;
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001104 } else {
1105 /*
1106 * The array may be doing an initial sync, or it may
1107 * be rebuilding individual components. If all the
1108 * devices are In_sync, then it is the array that is
1109 * being initialized.
1110 */
1111 for (i = 0; i < rs->md.raid_disks; i++)
1112 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
1113 array_in_sync = 1;
1114 }
1115 /*
1116 * Status characters:
1117 * 'D' = Dead/Failed device
1118 * 'a' = Alive but not in-sync
1119 * 'A' = Alive and in-sync
1120 */
1121 for (i = 0; i < rs->md.raid_disks; i++) {
1122 if (test_bit(Faulty, &rs->dev[i].rdev.flags))
1123 DMEMIT("D");
1124 else if (!array_in_sync ||
1125 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1126 DMEMIT("a");
1127 else
1128 DMEMIT("A");
1129 }
NeilBrown9d09e662011-01-13 20:00:02 +00001130
Jonathan E Brassow2e727c32011-10-31 20:21:26 +00001131 /*
1132 * In-sync ratio:
1133 * The in-sync ratio shows the progress of:
1134 * - Initializing the array
1135 * - Rebuilding a subset of devices of the array
1136 * The user can distinguish between the two by referring
1137 * to the status characters.
1138 */
NeilBrown9d09e662011-01-13 20:00:02 +00001139 DMEMIT(" %llu/%llu",
1140 (unsigned long long) sync,
1141 (unsigned long long) rs->md.resync_max_sectors);
1142
1143 break;
1144 case STATUSTYPE_TABLE:
1145 /* The string you would use to construct this array */
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001146 for (i = 0; i < rs->md.raid_disks; i++) {
Jonathan Brassow13c87582011-08-02 12:32:03 +01001147 if ((rs->print_flags & DMPF_REBUILD) &&
1148 rs->dev[i].data_dev &&
NeilBrown9d09e662011-01-13 20:00:02 +00001149 !test_bit(In_sync, &rs->dev[i].rdev.flags))
Jonathan Brassow13c87582011-08-02 12:32:03 +01001150 raid_param_cnt += 2; /* for rebuilds */
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001151 if (rs->dev[i].data_dev &&
1152 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1153 raid_param_cnt += 2;
1154 }
NeilBrown9d09e662011-01-13 20:00:02 +00001155
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -06001156 raid_param_cnt += (hweight32(rs->print_flags & ~DMPF_REBUILD) * 2);
NeilBrown9d09e662011-01-13 20:00:02 +00001157 if (rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC))
1158 raid_param_cnt--;
1159
1160 DMEMIT("%s %u %u", rs->raid_type->name,
1161 raid_param_cnt, rs->md.chunk_sectors);
1162
1163 if ((rs->print_flags & DMPF_SYNC) &&
1164 (rs->md.recovery_cp == MaxSector))
1165 DMEMIT(" sync");
1166 if (rs->print_flags & DMPF_NOSYNC)
1167 DMEMIT(" nosync");
1168
1169 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow13c87582011-08-02 12:32:03 +01001170 if ((rs->print_flags & DMPF_REBUILD) &&
1171 rs->dev[i].data_dev &&
NeilBrown9d09e662011-01-13 20:00:02 +00001172 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1173 DMEMIT(" rebuild %u", i);
1174
1175 if (rs->print_flags & DMPF_DAEMON_SLEEP)
1176 DMEMIT(" daemon_sleep %lu",
1177 rs->md.bitmap_info.daemon_sleep);
1178
1179 if (rs->print_flags & DMPF_MIN_RECOVERY_RATE)
1180 DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
1181
1182 if (rs->print_flags & DMPF_MAX_RECOVERY_RATE)
1183 DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
1184
Jonathan Brassow46bed2b2011-08-02 12:32:07 +01001185 for (i = 0; i < rs->md.raid_disks; i++)
1186 if (rs->dev[i].data_dev &&
1187 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1188 DMEMIT(" write_mostly %u", i);
1189
NeilBrown9d09e662011-01-13 20:00:02 +00001190 if (rs->print_flags & DMPF_MAX_WRITE_BEHIND)
1191 DMEMIT(" max_write_behind %lu",
1192 rs->md.bitmap_info.max_write_behind);
1193
1194 if (rs->print_flags & DMPF_STRIPE_CACHE) {
NeilBrownd1688a62011-10-11 16:49:52 +11001195 struct r5conf *conf = rs->md.private;
NeilBrown9d09e662011-01-13 20:00:02 +00001196
1197 /* convert from kiB to sectors */
1198 DMEMIT(" stripe_cache %d",
1199 conf ? conf->max_nr_stripes * 2 : 0);
1200 }
1201
Jonathan Brassowc1084562011-08-02 12:32:07 +01001202 if (rs->print_flags & DMPF_REGION_SIZE)
1203 DMEMIT(" region_size %lu",
1204 rs->md.bitmap_info.chunksize >> 9);
1205
NeilBrown9d09e662011-01-13 20:00:02 +00001206 DMEMIT(" %d", rs->md.raid_disks);
1207 for (i = 0; i < rs->md.raid_disks; i++) {
Jonathan Brassowb12d4372011-08-02 12:32:07 +01001208 if (rs->dev[i].meta_dev)
1209 DMEMIT(" %s", rs->dev[i].meta_dev->name);
1210 else
1211 DMEMIT(" -");
NeilBrown9d09e662011-01-13 20:00:02 +00001212
1213 if (rs->dev[i].data_dev)
1214 DMEMIT(" %s", rs->dev[i].data_dev->name);
1215 else
1216 DMEMIT(" -");
1217 }
1218 }
1219
1220 return 0;
1221}
1222
1223static int raid_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
1224{
1225 struct raid_set *rs = ti->private;
1226 unsigned i;
1227 int ret = 0;
1228
1229 for (i = 0; !ret && i < rs->md.raid_disks; i++)
1230 if (rs->dev[i].data_dev)
1231 ret = fn(ti,
1232 rs->dev[i].data_dev,
1233 0, /* No offset on data devs */
1234 rs->md.dev_sectors,
1235 data);
1236
1237 return ret;
1238}
1239
1240static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
1241{
1242 struct raid_set *rs = ti->private;
1243 unsigned chunk_size = rs->md.chunk_sectors << 9;
NeilBrownd1688a62011-10-11 16:49:52 +11001244 struct r5conf *conf = rs->md.private;
NeilBrown9d09e662011-01-13 20:00:02 +00001245
1246 blk_limits_io_min(limits, chunk_size);
1247 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
1248}
1249
1250static void raid_presuspend(struct dm_target *ti)
1251{
1252 struct raid_set *rs = ti->private;
1253
1254 md_stop_writes(&rs->md);
1255}
1256
1257static void raid_postsuspend(struct dm_target *ti)
1258{
1259 struct raid_set *rs = ti->private;
1260
1261 mddev_suspend(&rs->md);
1262}
1263
1264static void raid_resume(struct dm_target *ti)
1265{
1266 struct raid_set *rs = ti->private;
1267
Jonathan Brassow81f382f2012-05-22 13:55:30 +10001268 set_bit(MD_CHANGE_DEVS, &rs->md.flags);
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -06001269 if (!rs->bitmap_loaded) {
1270 bitmap_load(&rs->md);
1271 rs->bitmap_loaded = 1;
Jonathan Brassow47525e52012-05-22 13:55:29 +10001272 }
Jonathan Brassow34f8ac6d2012-01-27 14:53:53 -06001273
Jonathan Brassow47525e52012-05-22 13:55:29 +10001274 clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
NeilBrown9d09e662011-01-13 20:00:02 +00001275 mddev_resume(&rs->md);
1276}
1277
1278static struct target_type raid_target = {
1279 .name = "raid",
Jonathan E Brassow04475682012-03-28 18:41:26 +01001280 .version = {1, 2, 0},
NeilBrown9d09e662011-01-13 20:00:02 +00001281 .module = THIS_MODULE,
1282 .ctr = raid_ctr,
1283 .dtr = raid_dtr,
1284 .map = raid_map,
1285 .status = raid_status,
1286 .iterate_devices = raid_iterate_devices,
1287 .io_hints = raid_io_hints,
1288 .presuspend = raid_presuspend,
1289 .postsuspend = raid_postsuspend,
1290 .resume = raid_resume,
1291};
1292
1293static int __init dm_raid_init(void)
1294{
1295 return dm_register_target(&raid_target);
1296}
1297
1298static void __exit dm_raid_exit(void)
1299{
1300 dm_unregister_target(&raid_target);
1301}
1302
1303module_init(dm_raid_init);
1304module_exit(dm_raid_exit);
1305
1306MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
1307MODULE_ALIAS("dm-raid4");
1308MODULE_ALIAS("dm-raid5");
1309MODULE_ALIAS("dm-raid6");
1310MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
1311MODULE_LICENSE("GPL");