blob: 88143a0303d2ac768095caeb3c8c81549dd1a81b [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>
9
10#include "md.h"
11#include "raid5.h"
NeilBrown9d09e662011-01-13 20:00:02 +000012#include "bitmap.h"
13
Alasdair G Kergon3e8dbb72011-08-02 12:32:03 +010014#include <linux/device-mapper.h>
15
NeilBrown9d09e662011-01-13 20:00:02 +000016#define DM_MSG_PREFIX "raid"
17
18/*
19 * If the MD doesn't support MD_SYNC_STATE_FORCED yet, then
20 * make it so the flag doesn't set anything.
21 */
22#ifndef MD_SYNC_STATE_FORCED
23#define MD_SYNC_STATE_FORCED 0
24#endif
25
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;
41 struct mdk_rdev_s rdev;
42};
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
59 uint64_t print_flags;
60
61 struct mddev_s md;
62 struct raid_type *raid_type;
63 struct dm_target_callbacks callbacks;
64
65 struct raid_dev dev[0];
66};
67
68/* Supported raid types and properties. */
69static struct raid_type {
70 const char *name; /* RAID algorithm. */
71 const char *descr; /* Descriptor text for logging. */
72 const unsigned parity_devs; /* # of parity devices. */
73 const unsigned minimal_devs; /* minimal # of devices in set. */
74 const unsigned level; /* RAID level. */
75 const unsigned algorithm; /* RAID algorithm. */
76} raid_types[] = {
77 {"raid4", "RAID4 (dedicated parity disk)", 1, 2, 5, ALGORITHM_PARITY_0},
78 {"raid5_la", "RAID5 (left asymmetric)", 1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
79 {"raid5_ra", "RAID5 (right asymmetric)", 1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
80 {"raid5_ls", "RAID5 (left symmetric)", 1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
81 {"raid5_rs", "RAID5 (right symmetric)", 1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
82 {"raid6_zr", "RAID6 (zero restart)", 2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
83 {"raid6_nr", "RAID6 (N restart)", 2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
84 {"raid6_nc", "RAID6 (N continue)", 2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
85};
86
87static struct raid_type *get_raid_type(char *name)
88{
89 int i;
90
91 for (i = 0; i < ARRAY_SIZE(raid_types); i++)
92 if (!strcmp(raid_types[i].name, name))
93 return &raid_types[i];
94
95 return NULL;
96}
97
98static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
99{
100 unsigned i;
101 struct raid_set *rs;
102 sector_t sectors_per_dev;
103
104 if (raid_devs <= raid_type->parity_devs) {
105 ti->error = "Insufficient number of devices";
106 return ERR_PTR(-EINVAL);
107 }
108
109 sectors_per_dev = ti->len;
110 if (sector_div(sectors_per_dev, (raid_devs - raid_type->parity_devs))) {
111 ti->error = "Target length not divisible by number of data devices";
112 return ERR_PTR(-EINVAL);
113 }
114
115 rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
116 if (!rs) {
117 ti->error = "Cannot allocate raid context";
118 return ERR_PTR(-ENOMEM);
119 }
120
121 mddev_init(&rs->md);
122
123 rs->ti = ti;
124 rs->raid_type = raid_type;
125 rs->md.raid_disks = raid_devs;
126 rs->md.level = raid_type->level;
127 rs->md.new_level = rs->md.level;
128 rs->md.dev_sectors = sectors_per_dev;
129 rs->md.layout = raid_type->algorithm;
130 rs->md.new_layout = rs->md.layout;
131 rs->md.delta_disks = 0;
132 rs->md.recovery_cp = 0;
133
134 for (i = 0; i < raid_devs; i++)
135 md_rdev_init(&rs->dev[i].rdev);
136
137 /*
138 * Remaining items to be initialized by further RAID params:
139 * rs->md.persistent
140 * rs->md.external
141 * rs->md.chunk_sectors
142 * rs->md.new_chunk_sectors
143 */
144
145 return rs;
146}
147
148static void context_free(struct raid_set *rs)
149{
150 int i;
151
152 for (i = 0; i < rs->md.raid_disks; i++)
153 if (rs->dev[i].data_dev)
154 dm_put_device(rs->ti, rs->dev[i].data_dev);
155
156 kfree(rs);
157}
158
159/*
160 * For every device we have two words
161 * <meta_dev>: meta device name or '-' if missing
162 * <data_dev>: data device name or '-' if missing
163 *
164 * This code parses those words.
165 */
166static int dev_parms(struct raid_set *rs, char **argv)
167{
168 int i;
169 int rebuild = 0;
170 int metadata_available = 0;
171 int ret = 0;
172
173 for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {
174 rs->dev[i].rdev.raid_disk = i;
175
176 rs->dev[i].meta_dev = NULL;
177 rs->dev[i].data_dev = NULL;
178
179 /*
180 * There are no offsets, since there is a separate device
181 * for data and metadata.
182 */
183 rs->dev[i].rdev.data_offset = 0;
184 rs->dev[i].rdev.mddev = &rs->md;
185
186 if (strcmp(argv[0], "-")) {
187 rs->ti->error = "Metadata devices not supported";
188 return -EINVAL;
189 }
190
191 if (!strcmp(argv[1], "-")) {
192 if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
193 (!rs->dev[i].rdev.recovery_offset)) {
194 rs->ti->error = "Drive designated for rebuild not specified";
195 return -EINVAL;
196 }
197
198 continue;
199 }
200
201 ret = dm_get_device(rs->ti, argv[1],
202 dm_table_get_mode(rs->ti->table),
203 &rs->dev[i].data_dev);
204 if (ret) {
205 rs->ti->error = "RAID device lookup failure";
206 return ret;
207 }
208
209 rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
210 list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
211 if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
212 rebuild++;
213 }
214
215 if (metadata_available) {
216 rs->md.external = 0;
217 rs->md.persistent = 1;
218 rs->md.major_version = 2;
219 } else if (rebuild && !rs->md.recovery_cp) {
220 /*
221 * Without metadata, we will not be able to tell if the array
222 * is in-sync or not - we must assume it is not. Therefore,
223 * it is impossible to rebuild a drive.
224 *
225 * Even if there is metadata, the on-disk information may
226 * indicate that the array is not in-sync and it will then
227 * fail at that time.
228 *
229 * User could specify 'nosync' option if desperate.
230 */
231 DMERR("Unable to rebuild drive while array is not in-sync");
232 rs->ti->error = "RAID device lookup failure";
233 return -EINVAL;
234 }
235
236 return 0;
237}
238
239/*
Jonathan Brassowc1084562011-08-02 12:32:07 +0100240 * validate_region_size
241 * @rs
242 * @region_size: region size in sectors. If 0, pick a size (4MiB default).
243 *
244 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
245 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
246 *
247 * Returns: 0 on success, -EINVAL on failure.
248 */
249static int validate_region_size(struct raid_set *rs, unsigned long region_size)
250{
251 unsigned long min_region_size = rs->ti->len / (1 << 21);
252
253 if (!region_size) {
254 /*
255 * Choose a reasonable default. All figures in sectors.
256 */
257 if (min_region_size > (1 << 13)) {
258 DMINFO("Choosing default region size of %lu sectors",
259 region_size);
260 region_size = min_region_size;
261 } else {
262 DMINFO("Choosing default region size of 4MiB");
263 region_size = 1 << 13; /* sectors */
264 }
265 } else {
266 /*
267 * Validate user-supplied value.
268 */
269 if (region_size > rs->ti->len) {
270 rs->ti->error = "Supplied region size is too large";
271 return -EINVAL;
272 }
273
274 if (region_size < min_region_size) {
275 DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
276 region_size, min_region_size);
277 rs->ti->error = "Supplied region size is too small";
278 return -EINVAL;
279 }
280
281 if (!is_power_of_2(region_size)) {
282 rs->ti->error = "Region size is not a power of 2";
283 return -EINVAL;
284 }
285
286 if (region_size < rs->md.chunk_sectors) {
287 rs->ti->error = "Region size is smaller than the chunk size";
288 return -EINVAL;
289 }
290 }
291
292 /*
293 * Convert sectors to bytes.
294 */
295 rs->md.bitmap_info.chunksize = (region_size << 9);
296
297 return 0;
298}
299
300/*
NeilBrown9d09e662011-01-13 20:00:02 +0000301 * Possible arguments are...
302 * RAID456:
303 * <chunk_size> [optional_args]
304 *
305 * Optional args:
306 * [[no]sync] Force or prevent recovery of the entire array
307 * [rebuild <idx>] Rebuild the drive indicated by the index
308 * [daemon_sleep <ms>] Time between bitmap daemon work to clear bits
309 * [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
310 * [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100311 * [write_mostly <idx>] Indicate a write mostly drive via index
NeilBrown9d09e662011-01-13 20:00:02 +0000312 * [max_write_behind <sectors>] See '-write-behind=' (man mdadm)
313 * [stripe_cache <sectors>] Stripe cache size for higher RAIDs
Jonathan Brassowc1084562011-08-02 12:32:07 +0100314 * [region_size <sectors>] Defines granularity of bitmap
NeilBrown9d09e662011-01-13 20:00:02 +0000315 */
316static int parse_raid_params(struct raid_set *rs, char **argv,
317 unsigned num_raid_params)
318{
319 unsigned i, rebuild_cnt = 0;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100320 unsigned long value, region_size = 0;
NeilBrown9d09e662011-01-13 20:00:02 +0000321 char *key;
322
323 /*
324 * First, parse the in-order required arguments
325 */
326 if ((strict_strtoul(argv[0], 10, &value) < 0) ||
327 !is_power_of_2(value) || (value < 8)) {
328 rs->ti->error = "Bad chunk size";
329 return -EINVAL;
330 }
331
332 rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
333 argv++;
334 num_raid_params--;
335
336 /*
337 * Second, parse the unordered optional arguments
338 */
339 for (i = 0; i < rs->md.raid_disks; i++)
340 set_bit(In_sync, &rs->dev[i].rdev.flags);
341
342 for (i = 0; i < num_raid_params; i++) {
Jonathan Brassow13c87582011-08-02 12:32:03 +0100343 if (!strcasecmp(argv[i], "nosync")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000344 rs->md.recovery_cp = MaxSector;
345 rs->print_flags |= DMPF_NOSYNC;
346 rs->md.flags |= MD_SYNC_STATE_FORCED;
347 continue;
348 }
Jonathan Brassow13c87582011-08-02 12:32:03 +0100349 if (!strcasecmp(argv[i], "sync")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000350 rs->md.recovery_cp = 0;
351 rs->print_flags |= DMPF_SYNC;
352 rs->md.flags |= MD_SYNC_STATE_FORCED;
353 continue;
354 }
355
356 /* The rest of the optional arguments come in key/value pairs */
357 if ((i + 1) >= num_raid_params) {
358 rs->ti->error = "Wrong number of raid parameters given";
359 return -EINVAL;
360 }
361
362 key = argv[i++];
363 if (strict_strtoul(argv[i], 10, &value) < 0) {
364 rs->ti->error = "Bad numerical argument given in raid params";
365 return -EINVAL;
366 }
367
Jonathan Brassow13c87582011-08-02 12:32:03 +0100368 if (!strcasecmp(key, "rebuild")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000369 if (++rebuild_cnt > rs->raid_type->parity_devs) {
370 rs->ti->error = "Too many rebuild drives given";
371 return -EINVAL;
372 }
373 if (value > rs->md.raid_disks) {
374 rs->ti->error = "Invalid rebuild index given";
375 return -EINVAL;
376 }
377 clear_bit(In_sync, &rs->dev[value].rdev.flags);
378 rs->dev[value].rdev.recovery_offset = 0;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100379 rs->print_flags |= DMPF_REBUILD;
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100380 } else if (!strcasecmp(key, "write_mostly")) {
381 if (rs->raid_type->level != 1) {
382 rs->ti->error = "write_mostly option is only valid for RAID1";
383 return -EINVAL;
384 }
385 if (value > rs->md.raid_disks) {
386 rs->ti->error = "Invalid write_mostly drive index given";
387 return -EINVAL;
388 }
389 set_bit(WriteMostly, &rs->dev[value].rdev.flags);
Jonathan Brassow13c87582011-08-02 12:32:03 +0100390 } else if (!strcasecmp(key, "max_write_behind")) {
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100391 if (rs->raid_type->level != 1) {
392 rs->ti->error = "max_write_behind option is only valid for RAID1";
393 return -EINVAL;
394 }
NeilBrown9d09e662011-01-13 20:00:02 +0000395 rs->print_flags |= DMPF_MAX_WRITE_BEHIND;
396
397 /*
398 * In device-mapper, we specify things in sectors, but
399 * MD records this value in kB
400 */
401 value /= 2;
402 if (value > COUNTER_MAX) {
403 rs->ti->error = "Max write-behind limit out of range";
404 return -EINVAL;
405 }
406 rs->md.bitmap_info.max_write_behind = value;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100407 } else if (!strcasecmp(key, "daemon_sleep")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000408 rs->print_flags |= DMPF_DAEMON_SLEEP;
409 if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
410 rs->ti->error = "daemon sleep period out of range";
411 return -EINVAL;
412 }
413 rs->md.bitmap_info.daemon_sleep = value;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100414 } else if (!strcasecmp(key, "stripe_cache")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000415 rs->print_flags |= DMPF_STRIPE_CACHE;
416
417 /*
418 * In device-mapper, we specify things in sectors, but
419 * MD records this value in kB
420 */
421 value /= 2;
422
423 if (rs->raid_type->level < 5) {
424 rs->ti->error = "Inappropriate argument: stripe_cache";
425 return -EINVAL;
426 }
427 if (raid5_set_cache_size(&rs->md, (int)value)) {
428 rs->ti->error = "Bad stripe_cache size";
429 return -EINVAL;
430 }
Jonathan Brassow13c87582011-08-02 12:32:03 +0100431 } else if (!strcasecmp(key, "min_recovery_rate")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000432 rs->print_flags |= DMPF_MIN_RECOVERY_RATE;
433 if (value > INT_MAX) {
434 rs->ti->error = "min_recovery_rate out of range";
435 return -EINVAL;
436 }
437 rs->md.sync_speed_min = (int)value;
Jonathan Brassow13c87582011-08-02 12:32:03 +0100438 } else if (!strcasecmp(key, "max_recovery_rate")) {
NeilBrown9d09e662011-01-13 20:00:02 +0000439 rs->print_flags |= DMPF_MAX_RECOVERY_RATE;
440 if (value > INT_MAX) {
441 rs->ti->error = "max_recovery_rate out of range";
442 return -EINVAL;
443 }
444 rs->md.sync_speed_max = (int)value;
Jonathan Brassowc1084562011-08-02 12:32:07 +0100445 } else if (!strcasecmp(key, "region_size")) {
446 rs->print_flags |= DMPF_REGION_SIZE;
447 region_size = value;
NeilBrown9d09e662011-01-13 20:00:02 +0000448 } else {
449 DMERR("Unable to parse RAID parameter: %s", key);
450 rs->ti->error = "Unable to parse RAID parameters";
451 return -EINVAL;
452 }
453 }
454
Jonathan Brassowc1084562011-08-02 12:32:07 +0100455 if (validate_region_size(rs, region_size))
456 return -EINVAL;
457
458 if (rs->md.chunk_sectors)
459 rs->ti->split_io = rs->md.chunk_sectors;
460 else
461 rs->ti->split_io = region_size;
462
NeilBrown9d09e662011-01-13 20:00:02 +0000463 /* Assume there are no metadata devices until the drives are parsed */
464 rs->md.persistent = 0;
465 rs->md.external = 1;
466
467 return 0;
468}
469
470static void do_table_event(struct work_struct *ws)
471{
472 struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
473
474 dm_table_event(rs->ti->table);
475}
476
477static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
478{
479 struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
480
481 return md_raid5_congested(&rs->md, bits);
482}
483
NeilBrown9d09e662011-01-13 20:00:02 +0000484/*
485 * Construct a RAID4/5/6 mapping:
486 * Args:
487 * <raid_type> <#raid_params> <raid_params> \
488 * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
489 *
490 * ** metadata devices are not supported yet, use '-' instead **
491 *
492 * <raid_params> varies by <raid_type>. See 'parse_raid_params' for
493 * details on possible <raid_params>.
494 */
495static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
496{
497 int ret;
498 struct raid_type *rt;
499 unsigned long num_raid_params, num_raid_devs;
500 struct raid_set *rs = NULL;
501
502 /* Must have at least <raid_type> <#raid_params> */
503 if (argc < 2) {
504 ti->error = "Too few arguments";
505 return -EINVAL;
506 }
507
508 /* raid type */
509 rt = get_raid_type(argv[0]);
510 if (!rt) {
511 ti->error = "Unrecognised raid_type";
512 return -EINVAL;
513 }
514 argc--;
515 argv++;
516
517 /* number of RAID parameters */
518 if (strict_strtoul(argv[0], 10, &num_raid_params) < 0) {
519 ti->error = "Cannot understand number of RAID parameters";
520 return -EINVAL;
521 }
522 argc--;
523 argv++;
524
525 /* Skip over RAID params for now and find out # of devices */
526 if (num_raid_params + 1 > argc) {
527 ti->error = "Arguments do not agree with counts given";
528 return -EINVAL;
529 }
530
531 if ((strict_strtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) ||
532 (num_raid_devs >= INT_MAX)) {
533 ti->error = "Cannot understand number of raid devices";
534 return -EINVAL;
535 }
536
537 rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
538 if (IS_ERR(rs))
539 return PTR_ERR(rs);
540
541 ret = parse_raid_params(rs, argv, (unsigned)num_raid_params);
542 if (ret)
543 goto bad;
544
545 ret = -EINVAL;
546
547 argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
548 argv += num_raid_params + 1;
549
550 if (argc != (num_raid_devs * 2)) {
551 ti->error = "Supplied RAID devices does not match the count given";
552 goto bad;
553 }
554
555 ret = dev_parms(rs, argv);
556 if (ret)
557 goto bad;
558
559 INIT_WORK(&rs->md.event_work, do_table_event);
NeilBrown9d09e662011-01-13 20:00:02 +0000560 ti->private = rs;
561
562 mutex_lock(&rs->md.reconfig_mutex);
563 ret = md_run(&rs->md);
564 rs->md.in_sync = 0; /* Assume already marked dirty */
565 mutex_unlock(&rs->md.reconfig_mutex);
566
567 if (ret) {
568 ti->error = "Fail to run raid array";
569 goto bad;
570 }
571
572 rs->callbacks.congested_fn = raid_is_congested;
NeilBrown9d09e662011-01-13 20:00:02 +0000573 dm_table_add_target_callbacks(ti->table, &rs->callbacks);
574
575 return 0;
576
577bad:
578 context_free(rs);
579
580 return ret;
581}
582
583static void raid_dtr(struct dm_target *ti)
584{
585 struct raid_set *rs = ti->private;
586
587 list_del_init(&rs->callbacks.list);
588 md_stop(&rs->md);
589 context_free(rs);
590}
591
592static int raid_map(struct dm_target *ti, struct bio *bio, union map_info *map_context)
593{
594 struct raid_set *rs = ti->private;
595 mddev_t *mddev = &rs->md;
596
597 mddev->pers->make_request(mddev, bio);
598
599 return DM_MAPIO_SUBMITTED;
600}
601
602static int raid_status(struct dm_target *ti, status_type_t type,
603 char *result, unsigned maxlen)
604{
605 struct raid_set *rs = ti->private;
606 unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
607 unsigned sz = 0;
608 int i;
609 sector_t sync;
610
611 switch (type) {
612 case STATUSTYPE_INFO:
613 DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
614
615 for (i = 0; i < rs->md.raid_disks; i++) {
616 if (test_bit(Faulty, &rs->dev[i].rdev.flags))
617 DMEMIT("D");
618 else if (test_bit(In_sync, &rs->dev[i].rdev.flags))
619 DMEMIT("A");
620 else
621 DMEMIT("a");
622 }
623
624 if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
625 sync = rs->md.curr_resync_completed;
626 else
627 sync = rs->md.recovery_cp;
628
629 if (sync > rs->md.resync_max_sectors)
630 sync = rs->md.resync_max_sectors;
631
632 DMEMIT(" %llu/%llu",
633 (unsigned long long) sync,
634 (unsigned long long) rs->md.resync_max_sectors);
635
636 break;
637 case STATUSTYPE_TABLE:
638 /* The string you would use to construct this array */
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100639 for (i = 0; i < rs->md.raid_disks; i++) {
Jonathan Brassow13c87582011-08-02 12:32:03 +0100640 if ((rs->print_flags & DMPF_REBUILD) &&
641 rs->dev[i].data_dev &&
NeilBrown9d09e662011-01-13 20:00:02 +0000642 !test_bit(In_sync, &rs->dev[i].rdev.flags))
Jonathan Brassow13c87582011-08-02 12:32:03 +0100643 raid_param_cnt += 2; /* for rebuilds */
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100644 if (rs->dev[i].data_dev &&
645 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
646 raid_param_cnt += 2;
647 }
NeilBrown9d09e662011-01-13 20:00:02 +0000648
Jonathan Brassow13c87582011-08-02 12:32:03 +0100649 raid_param_cnt += (hweight64(rs->print_flags & ~DMPF_REBUILD) * 2);
NeilBrown9d09e662011-01-13 20:00:02 +0000650 if (rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC))
651 raid_param_cnt--;
652
653 DMEMIT("%s %u %u", rs->raid_type->name,
654 raid_param_cnt, rs->md.chunk_sectors);
655
656 if ((rs->print_flags & DMPF_SYNC) &&
657 (rs->md.recovery_cp == MaxSector))
658 DMEMIT(" sync");
659 if (rs->print_flags & DMPF_NOSYNC)
660 DMEMIT(" nosync");
661
662 for (i = 0; i < rs->md.raid_disks; i++)
Jonathan Brassow13c87582011-08-02 12:32:03 +0100663 if ((rs->print_flags & DMPF_REBUILD) &&
664 rs->dev[i].data_dev &&
NeilBrown9d09e662011-01-13 20:00:02 +0000665 !test_bit(In_sync, &rs->dev[i].rdev.flags))
666 DMEMIT(" rebuild %u", i);
667
668 if (rs->print_flags & DMPF_DAEMON_SLEEP)
669 DMEMIT(" daemon_sleep %lu",
670 rs->md.bitmap_info.daemon_sleep);
671
672 if (rs->print_flags & DMPF_MIN_RECOVERY_RATE)
673 DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
674
675 if (rs->print_flags & DMPF_MAX_RECOVERY_RATE)
676 DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
677
Jonathan Brassow46bed2b2011-08-02 12:32:07 +0100678 for (i = 0; i < rs->md.raid_disks; i++)
679 if (rs->dev[i].data_dev &&
680 test_bit(WriteMostly, &rs->dev[i].rdev.flags))
681 DMEMIT(" write_mostly %u", i);
682
NeilBrown9d09e662011-01-13 20:00:02 +0000683 if (rs->print_flags & DMPF_MAX_WRITE_BEHIND)
684 DMEMIT(" max_write_behind %lu",
685 rs->md.bitmap_info.max_write_behind);
686
687 if (rs->print_flags & DMPF_STRIPE_CACHE) {
688 raid5_conf_t *conf = rs->md.private;
689
690 /* convert from kiB to sectors */
691 DMEMIT(" stripe_cache %d",
692 conf ? conf->max_nr_stripes * 2 : 0);
693 }
694
Jonathan Brassowc1084562011-08-02 12:32:07 +0100695 if (rs->print_flags & DMPF_REGION_SIZE)
696 DMEMIT(" region_size %lu",
697 rs->md.bitmap_info.chunksize >> 9);
698
NeilBrown9d09e662011-01-13 20:00:02 +0000699 DMEMIT(" %d", rs->md.raid_disks);
700 for (i = 0; i < rs->md.raid_disks; i++) {
701 DMEMIT(" -"); /* metadata device */
702
703 if (rs->dev[i].data_dev)
704 DMEMIT(" %s", rs->dev[i].data_dev->name);
705 else
706 DMEMIT(" -");
707 }
708 }
709
710 return 0;
711}
712
713static int raid_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
714{
715 struct raid_set *rs = ti->private;
716 unsigned i;
717 int ret = 0;
718
719 for (i = 0; !ret && i < rs->md.raid_disks; i++)
720 if (rs->dev[i].data_dev)
721 ret = fn(ti,
722 rs->dev[i].data_dev,
723 0, /* No offset on data devs */
724 rs->md.dev_sectors,
725 data);
726
727 return ret;
728}
729
730static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
731{
732 struct raid_set *rs = ti->private;
733 unsigned chunk_size = rs->md.chunk_sectors << 9;
734 raid5_conf_t *conf = rs->md.private;
735
736 blk_limits_io_min(limits, chunk_size);
737 blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
738}
739
740static void raid_presuspend(struct dm_target *ti)
741{
742 struct raid_set *rs = ti->private;
743
744 md_stop_writes(&rs->md);
745}
746
747static void raid_postsuspend(struct dm_target *ti)
748{
749 struct raid_set *rs = ti->private;
750
751 mddev_suspend(&rs->md);
752}
753
754static void raid_resume(struct dm_target *ti)
755{
756 struct raid_set *rs = ti->private;
757
758 mddev_resume(&rs->md);
759}
760
761static struct target_type raid_target = {
762 .name = "raid",
763 .version = {1, 0, 0},
764 .module = THIS_MODULE,
765 .ctr = raid_ctr,
766 .dtr = raid_dtr,
767 .map = raid_map,
768 .status = raid_status,
769 .iterate_devices = raid_iterate_devices,
770 .io_hints = raid_io_hints,
771 .presuspend = raid_presuspend,
772 .postsuspend = raid_postsuspend,
773 .resume = raid_resume,
774};
775
776static int __init dm_raid_init(void)
777{
778 return dm_register_target(&raid_target);
779}
780
781static void __exit dm_raid_exit(void)
782{
783 dm_unregister_target(&raid_target);
784}
785
786module_init(dm_raid_init);
787module_exit(dm_raid_exit);
788
789MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
790MODULE_ALIAS("dm-raid4");
791MODULE_ALIAS("dm-raid5");
792MODULE_ALIAS("dm-raid6");
793MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
794MODULE_LICENSE("GPL");