]>
Commit | Line | Data |
---|---|---|
9d09e663 N |
1 | /* |
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" | |
9d09e663 N |
12 | #include "bitmap.h" |
13 | ||
3e8dbb7f AK |
14 | #include <linux/device-mapper.h> |
15 | ||
9d09e663 N |
16 | #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 | ||
26 | struct 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 | */ | |
13c87583 JB |
47 | #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 | |
c1084561 | 55 | #define DMPF_REGION_SIZE 0X100 |
9d09e663 N |
56 | struct 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. */ | |
69 | static 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 | ||
87 | static 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 | ||
98 | static 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 | ||
148 | static 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 | */ | |
166 | static 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 | ||
c1084561 JB |
239 | /* |
240 | * 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 | */ | |
249 | static 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 | ||
9d09e663 N |
300 | /* |
301 | * 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 | |
311 | * [max_write_behind <sectors>] See '-write-behind=' (man mdadm) | |
312 | * [stripe_cache <sectors>] Stripe cache size for higher RAIDs | |
c1084561 | 313 | * [region_size <sectors>] Defines granularity of bitmap |
9d09e663 N |
314 | */ |
315 | static int parse_raid_params(struct raid_set *rs, char **argv, | |
316 | unsigned num_raid_params) | |
317 | { | |
318 | unsigned i, rebuild_cnt = 0; | |
c1084561 | 319 | unsigned long value, region_size = 0; |
9d09e663 N |
320 | char *key; |
321 | ||
322 | /* | |
323 | * First, parse the in-order required arguments | |
324 | */ | |
325 | if ((strict_strtoul(argv[0], 10, &value) < 0) || | |
326 | !is_power_of_2(value) || (value < 8)) { | |
327 | rs->ti->error = "Bad chunk size"; | |
328 | return -EINVAL; | |
329 | } | |
330 | ||
331 | rs->md.new_chunk_sectors = rs->md.chunk_sectors = value; | |
332 | argv++; | |
333 | num_raid_params--; | |
334 | ||
335 | /* | |
336 | * Second, parse the unordered optional arguments | |
337 | */ | |
338 | for (i = 0; i < rs->md.raid_disks; i++) | |
339 | set_bit(In_sync, &rs->dev[i].rdev.flags); | |
340 | ||
341 | for (i = 0; i < num_raid_params; i++) { | |
13c87583 | 342 | if (!strcasecmp(argv[i], "nosync")) { |
9d09e663 N |
343 | rs->md.recovery_cp = MaxSector; |
344 | rs->print_flags |= DMPF_NOSYNC; | |
345 | rs->md.flags |= MD_SYNC_STATE_FORCED; | |
346 | continue; | |
347 | } | |
13c87583 | 348 | if (!strcasecmp(argv[i], "sync")) { |
9d09e663 N |
349 | rs->md.recovery_cp = 0; |
350 | rs->print_flags |= DMPF_SYNC; | |
351 | rs->md.flags |= MD_SYNC_STATE_FORCED; | |
352 | continue; | |
353 | } | |
354 | ||
355 | /* The rest of the optional arguments come in key/value pairs */ | |
356 | if ((i + 1) >= num_raid_params) { | |
357 | rs->ti->error = "Wrong number of raid parameters given"; | |
358 | return -EINVAL; | |
359 | } | |
360 | ||
361 | key = argv[i++]; | |
362 | if (strict_strtoul(argv[i], 10, &value) < 0) { | |
363 | rs->ti->error = "Bad numerical argument given in raid params"; | |
364 | return -EINVAL; | |
365 | } | |
366 | ||
13c87583 | 367 | if (!strcasecmp(key, "rebuild")) { |
9d09e663 N |
368 | if (++rebuild_cnt > rs->raid_type->parity_devs) { |
369 | rs->ti->error = "Too many rebuild drives given"; | |
370 | return -EINVAL; | |
371 | } | |
372 | if (value > rs->md.raid_disks) { | |
373 | rs->ti->error = "Invalid rebuild index given"; | |
374 | return -EINVAL; | |
375 | } | |
376 | clear_bit(In_sync, &rs->dev[value].rdev.flags); | |
377 | rs->dev[value].rdev.recovery_offset = 0; | |
13c87583 JB |
378 | rs->print_flags |= DMPF_REBUILD; |
379 | } else if (!strcasecmp(key, "max_write_behind")) { | |
9d09e663 N |
380 | rs->print_flags |= DMPF_MAX_WRITE_BEHIND; |
381 | ||
382 | /* | |
383 | * In device-mapper, we specify things in sectors, but | |
384 | * MD records this value in kB | |
385 | */ | |
386 | value /= 2; | |
387 | if (value > COUNTER_MAX) { | |
388 | rs->ti->error = "Max write-behind limit out of range"; | |
389 | return -EINVAL; | |
390 | } | |
391 | rs->md.bitmap_info.max_write_behind = value; | |
13c87583 | 392 | } else if (!strcasecmp(key, "daemon_sleep")) { |
9d09e663 N |
393 | rs->print_flags |= DMPF_DAEMON_SLEEP; |
394 | if (!value || (value > MAX_SCHEDULE_TIMEOUT)) { | |
395 | rs->ti->error = "daemon sleep period out of range"; | |
396 | return -EINVAL; | |
397 | } | |
398 | rs->md.bitmap_info.daemon_sleep = value; | |
13c87583 | 399 | } else if (!strcasecmp(key, "stripe_cache")) { |
9d09e663 N |
400 | rs->print_flags |= DMPF_STRIPE_CACHE; |
401 | ||
402 | /* | |
403 | * In device-mapper, we specify things in sectors, but | |
404 | * MD records this value in kB | |
405 | */ | |
406 | value /= 2; | |
407 | ||
408 | if (rs->raid_type->level < 5) { | |
409 | rs->ti->error = "Inappropriate argument: stripe_cache"; | |
410 | return -EINVAL; | |
411 | } | |
412 | if (raid5_set_cache_size(&rs->md, (int)value)) { | |
413 | rs->ti->error = "Bad stripe_cache size"; | |
414 | return -EINVAL; | |
415 | } | |
13c87583 | 416 | } else if (!strcasecmp(key, "min_recovery_rate")) { |
9d09e663 N |
417 | rs->print_flags |= DMPF_MIN_RECOVERY_RATE; |
418 | if (value > INT_MAX) { | |
419 | rs->ti->error = "min_recovery_rate out of range"; | |
420 | return -EINVAL; | |
421 | } | |
422 | rs->md.sync_speed_min = (int)value; | |
13c87583 | 423 | } else if (!strcasecmp(key, "max_recovery_rate")) { |
9d09e663 N |
424 | rs->print_flags |= DMPF_MAX_RECOVERY_RATE; |
425 | if (value > INT_MAX) { | |
426 | rs->ti->error = "max_recovery_rate out of range"; | |
427 | return -EINVAL; | |
428 | } | |
429 | rs->md.sync_speed_max = (int)value; | |
c1084561 JB |
430 | } else if (!strcasecmp(key, "region_size")) { |
431 | rs->print_flags |= DMPF_REGION_SIZE; | |
432 | region_size = value; | |
9d09e663 N |
433 | } else { |
434 | DMERR("Unable to parse RAID parameter: %s", key); | |
435 | rs->ti->error = "Unable to parse RAID parameters"; | |
436 | return -EINVAL; | |
437 | } | |
438 | } | |
439 | ||
c1084561 JB |
440 | if (validate_region_size(rs, region_size)) |
441 | return -EINVAL; | |
442 | ||
443 | if (rs->md.chunk_sectors) | |
444 | rs->ti->split_io = rs->md.chunk_sectors; | |
445 | else | |
446 | rs->ti->split_io = region_size; | |
447 | ||
9d09e663 N |
448 | /* Assume there are no metadata devices until the drives are parsed */ |
449 | rs->md.persistent = 0; | |
450 | rs->md.external = 1; | |
451 | ||
452 | return 0; | |
453 | } | |
454 | ||
455 | static void do_table_event(struct work_struct *ws) | |
456 | { | |
457 | struct raid_set *rs = container_of(ws, struct raid_set, md.event_work); | |
458 | ||
459 | dm_table_event(rs->ti->table); | |
460 | } | |
461 | ||
462 | static int raid_is_congested(struct dm_target_callbacks *cb, int bits) | |
463 | { | |
464 | struct raid_set *rs = container_of(cb, struct raid_set, callbacks); | |
465 | ||
466 | return md_raid5_congested(&rs->md, bits); | |
467 | } | |
468 | ||
9d09e663 N |
469 | /* |
470 | * Construct a RAID4/5/6 mapping: | |
471 | * Args: | |
472 | * <raid_type> <#raid_params> <raid_params> \ | |
473 | * <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> } | |
474 | * | |
475 | * ** metadata devices are not supported yet, use '-' instead ** | |
476 | * | |
477 | * <raid_params> varies by <raid_type>. See 'parse_raid_params' for | |
478 | * details on possible <raid_params>. | |
479 | */ | |
480 | static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv) | |
481 | { | |
482 | int ret; | |
483 | struct raid_type *rt; | |
484 | unsigned long num_raid_params, num_raid_devs; | |
485 | struct raid_set *rs = NULL; | |
486 | ||
487 | /* Must have at least <raid_type> <#raid_params> */ | |
488 | if (argc < 2) { | |
489 | ti->error = "Too few arguments"; | |
490 | return -EINVAL; | |
491 | } | |
492 | ||
493 | /* raid type */ | |
494 | rt = get_raid_type(argv[0]); | |
495 | if (!rt) { | |
496 | ti->error = "Unrecognised raid_type"; | |
497 | return -EINVAL; | |
498 | } | |
499 | argc--; | |
500 | argv++; | |
501 | ||
502 | /* number of RAID parameters */ | |
503 | if (strict_strtoul(argv[0], 10, &num_raid_params) < 0) { | |
504 | ti->error = "Cannot understand number of RAID parameters"; | |
505 | return -EINVAL; | |
506 | } | |
507 | argc--; | |
508 | argv++; | |
509 | ||
510 | /* Skip over RAID params for now and find out # of devices */ | |
511 | if (num_raid_params + 1 > argc) { | |
512 | ti->error = "Arguments do not agree with counts given"; | |
513 | return -EINVAL; | |
514 | } | |
515 | ||
516 | if ((strict_strtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) || | |
517 | (num_raid_devs >= INT_MAX)) { | |
518 | ti->error = "Cannot understand number of raid devices"; | |
519 | return -EINVAL; | |
520 | } | |
521 | ||
522 | rs = context_alloc(ti, rt, (unsigned)num_raid_devs); | |
523 | if (IS_ERR(rs)) | |
524 | return PTR_ERR(rs); | |
525 | ||
526 | ret = parse_raid_params(rs, argv, (unsigned)num_raid_params); | |
527 | if (ret) | |
528 | goto bad; | |
529 | ||
530 | ret = -EINVAL; | |
531 | ||
532 | argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */ | |
533 | argv += num_raid_params + 1; | |
534 | ||
535 | if (argc != (num_raid_devs * 2)) { | |
536 | ti->error = "Supplied RAID devices does not match the count given"; | |
537 | goto bad; | |
538 | } | |
539 | ||
540 | ret = dev_parms(rs, argv); | |
541 | if (ret) | |
542 | goto bad; | |
543 | ||
544 | INIT_WORK(&rs->md.event_work, do_table_event); | |
9d09e663 N |
545 | ti->private = rs; |
546 | ||
547 | mutex_lock(&rs->md.reconfig_mutex); | |
548 | ret = md_run(&rs->md); | |
549 | rs->md.in_sync = 0; /* Assume already marked dirty */ | |
550 | mutex_unlock(&rs->md.reconfig_mutex); | |
551 | ||
552 | if (ret) { | |
553 | ti->error = "Fail to run raid array"; | |
554 | goto bad; | |
555 | } | |
556 | ||
557 | rs->callbacks.congested_fn = raid_is_congested; | |
9d09e663 N |
558 | dm_table_add_target_callbacks(ti->table, &rs->callbacks); |
559 | ||
560 | return 0; | |
561 | ||
562 | bad: | |
563 | context_free(rs); | |
564 | ||
565 | return ret; | |
566 | } | |
567 | ||
568 | static void raid_dtr(struct dm_target *ti) | |
569 | { | |
570 | struct raid_set *rs = ti->private; | |
571 | ||
572 | list_del_init(&rs->callbacks.list); | |
573 | md_stop(&rs->md); | |
574 | context_free(rs); | |
575 | } | |
576 | ||
577 | static int raid_map(struct dm_target *ti, struct bio *bio, union map_info *map_context) | |
578 | { | |
579 | struct raid_set *rs = ti->private; | |
580 | mddev_t *mddev = &rs->md; | |
581 | ||
582 | mddev->pers->make_request(mddev, bio); | |
583 | ||
584 | return DM_MAPIO_SUBMITTED; | |
585 | } | |
586 | ||
587 | static int raid_status(struct dm_target *ti, status_type_t type, | |
588 | char *result, unsigned maxlen) | |
589 | { | |
590 | struct raid_set *rs = ti->private; | |
591 | unsigned raid_param_cnt = 1; /* at least 1 for chunksize */ | |
592 | unsigned sz = 0; | |
593 | int i; | |
594 | sector_t sync; | |
595 | ||
596 | switch (type) { | |
597 | case STATUSTYPE_INFO: | |
598 | DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks); | |
599 | ||
600 | for (i = 0; i < rs->md.raid_disks; i++) { | |
601 | if (test_bit(Faulty, &rs->dev[i].rdev.flags)) | |
602 | DMEMIT("D"); | |
603 | else if (test_bit(In_sync, &rs->dev[i].rdev.flags)) | |
604 | DMEMIT("A"); | |
605 | else | |
606 | DMEMIT("a"); | |
607 | } | |
608 | ||
609 | if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery)) | |
610 | sync = rs->md.curr_resync_completed; | |
611 | else | |
612 | sync = rs->md.recovery_cp; | |
613 | ||
614 | if (sync > rs->md.resync_max_sectors) | |
615 | sync = rs->md.resync_max_sectors; | |
616 | ||
617 | DMEMIT(" %llu/%llu", | |
618 | (unsigned long long) sync, | |
619 | (unsigned long long) rs->md.resync_max_sectors); | |
620 | ||
621 | break; | |
622 | case STATUSTYPE_TABLE: | |
623 | /* The string you would use to construct this array */ | |
624 | for (i = 0; i < rs->md.raid_disks; i++) | |
13c87583 JB |
625 | if ((rs->print_flags & DMPF_REBUILD) && |
626 | rs->dev[i].data_dev && | |
9d09e663 | 627 | !test_bit(In_sync, &rs->dev[i].rdev.flags)) |
13c87583 | 628 | raid_param_cnt += 2; /* for rebuilds */ |
9d09e663 | 629 | |
13c87583 | 630 | raid_param_cnt += (hweight64(rs->print_flags & ~DMPF_REBUILD) * 2); |
9d09e663 N |
631 | if (rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC)) |
632 | raid_param_cnt--; | |
633 | ||
634 | DMEMIT("%s %u %u", rs->raid_type->name, | |
635 | raid_param_cnt, rs->md.chunk_sectors); | |
636 | ||
637 | if ((rs->print_flags & DMPF_SYNC) && | |
638 | (rs->md.recovery_cp == MaxSector)) | |
639 | DMEMIT(" sync"); | |
640 | if (rs->print_flags & DMPF_NOSYNC) | |
641 | DMEMIT(" nosync"); | |
642 | ||
643 | for (i = 0; i < rs->md.raid_disks; i++) | |
13c87583 JB |
644 | if ((rs->print_flags & DMPF_REBUILD) && |
645 | rs->dev[i].data_dev && | |
9d09e663 N |
646 | !test_bit(In_sync, &rs->dev[i].rdev.flags)) |
647 | DMEMIT(" rebuild %u", i); | |
648 | ||
649 | if (rs->print_flags & DMPF_DAEMON_SLEEP) | |
650 | DMEMIT(" daemon_sleep %lu", | |
651 | rs->md.bitmap_info.daemon_sleep); | |
652 | ||
653 | if (rs->print_flags & DMPF_MIN_RECOVERY_RATE) | |
654 | DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min); | |
655 | ||
656 | if (rs->print_flags & DMPF_MAX_RECOVERY_RATE) | |
657 | DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max); | |
658 | ||
659 | if (rs->print_flags & DMPF_MAX_WRITE_BEHIND) | |
660 | DMEMIT(" max_write_behind %lu", | |
661 | rs->md.bitmap_info.max_write_behind); | |
662 | ||
663 | if (rs->print_flags & DMPF_STRIPE_CACHE) { | |
664 | raid5_conf_t *conf = rs->md.private; | |
665 | ||
666 | /* convert from kiB to sectors */ | |
667 | DMEMIT(" stripe_cache %d", | |
668 | conf ? conf->max_nr_stripes * 2 : 0); | |
669 | } | |
670 | ||
c1084561 JB |
671 | if (rs->print_flags & DMPF_REGION_SIZE) |
672 | DMEMIT(" region_size %lu", | |
673 | rs->md.bitmap_info.chunksize >> 9); | |
674 | ||
9d09e663 N |
675 | DMEMIT(" %d", rs->md.raid_disks); |
676 | for (i = 0; i < rs->md.raid_disks; i++) { | |
677 | DMEMIT(" -"); /* metadata device */ | |
678 | ||
679 | if (rs->dev[i].data_dev) | |
680 | DMEMIT(" %s", rs->dev[i].data_dev->name); | |
681 | else | |
682 | DMEMIT(" -"); | |
683 | } | |
684 | } | |
685 | ||
686 | return 0; | |
687 | } | |
688 | ||
689 | static int raid_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data) | |
690 | { | |
691 | struct raid_set *rs = ti->private; | |
692 | unsigned i; | |
693 | int ret = 0; | |
694 | ||
695 | for (i = 0; !ret && i < rs->md.raid_disks; i++) | |
696 | if (rs->dev[i].data_dev) | |
697 | ret = fn(ti, | |
698 | rs->dev[i].data_dev, | |
699 | 0, /* No offset on data devs */ | |
700 | rs->md.dev_sectors, | |
701 | data); | |
702 | ||
703 | return ret; | |
704 | } | |
705 | ||
706 | static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits) | |
707 | { | |
708 | struct raid_set *rs = ti->private; | |
709 | unsigned chunk_size = rs->md.chunk_sectors << 9; | |
710 | raid5_conf_t *conf = rs->md.private; | |
711 | ||
712 | blk_limits_io_min(limits, chunk_size); | |
713 | blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded)); | |
714 | } | |
715 | ||
716 | static void raid_presuspend(struct dm_target *ti) | |
717 | { | |
718 | struct raid_set *rs = ti->private; | |
719 | ||
720 | md_stop_writes(&rs->md); | |
721 | } | |
722 | ||
723 | static void raid_postsuspend(struct dm_target *ti) | |
724 | { | |
725 | struct raid_set *rs = ti->private; | |
726 | ||
727 | mddev_suspend(&rs->md); | |
728 | } | |
729 | ||
730 | static void raid_resume(struct dm_target *ti) | |
731 | { | |
732 | struct raid_set *rs = ti->private; | |
733 | ||
734 | mddev_resume(&rs->md); | |
735 | } | |
736 | ||
737 | static struct target_type raid_target = { | |
738 | .name = "raid", | |
739 | .version = {1, 0, 0}, | |
740 | .module = THIS_MODULE, | |
741 | .ctr = raid_ctr, | |
742 | .dtr = raid_dtr, | |
743 | .map = raid_map, | |
744 | .status = raid_status, | |
745 | .iterate_devices = raid_iterate_devices, | |
746 | .io_hints = raid_io_hints, | |
747 | .presuspend = raid_presuspend, | |
748 | .postsuspend = raid_postsuspend, | |
749 | .resume = raid_resume, | |
750 | }; | |
751 | ||
752 | static int __init dm_raid_init(void) | |
753 | { | |
754 | return dm_register_target(&raid_target); | |
755 | } | |
756 | ||
757 | static void __exit dm_raid_exit(void) | |
758 | { | |
759 | dm_unregister_target(&raid_target); | |
760 | } | |
761 | ||
762 | module_init(dm_raid_init); | |
763 | module_exit(dm_raid_exit); | |
764 | ||
765 | MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target"); | |
766 | MODULE_ALIAS("dm-raid4"); | |
767 | MODULE_ALIAS("dm-raid5"); | |
768 | MODULE_ALIAS("dm-raid6"); | |
769 | MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>"); | |
770 | MODULE_LICENSE("GPL"); |