dm: add missing SPDX-License-Indentifiers
[linux-2.6-block.git] / drivers / md / dm-flakey.c
CommitLineData
3bd94003 1// SPDX-License-Identifier: GPL-2.0-only
3407ef52
JB
2/*
3 * Copyright (C) 2003 Sistina Software (UK) Limited.
a3998799 4 * Copyright (C) 2004, 2010-2011 Red Hat, Inc. All rights reserved.
3407ef52
JB
5 *
6 * This file is released under the GPL.
7 */
8
9#include <linux/device-mapper.h>
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/blkdev.h>
14#include <linux/bio.h>
15#include <linux/slab.h>
16
17#define DM_MSG_PREFIX "flakey"
18
a3998799 19#define all_corrupt_bio_flags_match(bio, fc) \
1eff9d32 20 (((bio)->bi_opf & (fc)->corrupt_bio_flags) == (fc)->corrupt_bio_flags)
a3998799 21
3407ef52
JB
22/*
23 * Flakey: Used for testing only, simulates intermittent,
24 * catastrophic device failure.
25 */
26struct flakey_c {
27 struct dm_dev *dev;
28 unsigned long start_time;
29 sector_t start;
30 unsigned up_interval;
31 unsigned down_interval;
b26f5e3d 32 unsigned long flags;
a3998799
MS
33 unsigned corrupt_bio_byte;
34 unsigned corrupt_bio_rw;
35 unsigned corrupt_bio_value;
eff17e51 36 blk_opf_t corrupt_bio_flags;
3407ef52
JB
37};
38
b26f5e3d 39enum feature_flag_bits {
ef548c55
MS
40 DROP_WRITES,
41 ERROR_WRITES
b26f5e3d
MS
42};
43
c7cfdf59
MP
44struct per_bio_data {
45 bool bio_submitted;
46};
47
b26f5e3d
MS
48static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
49 struct dm_target *ti)
dfd068b0
MS
50{
51 int r;
52 unsigned argc;
53 const char *arg_name;
54
5916a22b 55 static const struct dm_arg _args[] = {
a3998799
MS
56 {0, 6, "Invalid number of feature args"},
57 {1, UINT_MAX, "Invalid corrupt bio byte"},
58 {0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
59 {0, UINT_MAX, "Invalid corrupt bio flags mask"},
dfd068b0
MS
60 };
61
62 /* No feature arguments supplied. */
63 if (!as->argc)
64 return 0;
65
66 r = dm_read_arg_group(_args, as, &argc, &ti->error);
67 if (r)
a3998799 68 return r;
dfd068b0 69
a3998799 70 while (argc) {
dfd068b0
MS
71 arg_name = dm_shift_arg(as);
72 argc--;
73
7690e253
GR
74 if (!arg_name) {
75 ti->error = "Insufficient feature arguments";
76 return -EINVAL;
77 }
78
b26f5e3d
MS
79 /*
80 * drop_writes
81 */
82 if (!strcasecmp(arg_name, "drop_writes")) {
83 if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
84 ti->error = "Feature drop_writes duplicated";
85 return -EINVAL;
ef548c55
MS
86 } else if (test_bit(ERROR_WRITES, &fc->flags)) {
87 ti->error = "Feature drop_writes conflicts with feature error_writes";
88 return -EINVAL;
89 }
90
91 continue;
92 }
93
94 /*
95 * error_writes
96 */
97 if (!strcasecmp(arg_name, "error_writes")) {
98 if (test_and_set_bit(ERROR_WRITES, &fc->flags)) {
99 ti->error = "Feature error_writes duplicated";
100 return -EINVAL;
101
102 } else if (test_bit(DROP_WRITES, &fc->flags)) {
103 ti->error = "Feature error_writes conflicts with feature drop_writes";
104 return -EINVAL;
b26f5e3d
MS
105 }
106
107 continue;
108 }
109
a3998799
MS
110 /*
111 * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
112 */
113 if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
68e58a29 114 if (!argc) {
a3998799 115 ti->error = "Feature corrupt_bio_byte requires parameters";
68e58a29
MS
116 return -EINVAL;
117 }
a3998799
MS
118
119 r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
120 if (r)
121 return r;
122 argc--;
123
124 /*
125 * Direction r or w?
126 */
127 arg_name = dm_shift_arg(as);
128 if (!strcasecmp(arg_name, "w"))
129 fc->corrupt_bio_rw = WRITE;
130 else if (!strcasecmp(arg_name, "r"))
131 fc->corrupt_bio_rw = READ;
132 else {
133 ti->error = "Invalid corrupt bio direction (r or w)";
134 return -EINVAL;
135 }
136 argc--;
137
138 /*
139 * Value of byte (0-255) to write in place of correct one.
140 */
141 r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
142 if (r)
143 return r;
144 argc--;
145
146 /*
147 * Only corrupt bios with these flags set.
148 */
eff17e51
BVA
149 BUILD_BUG_ON(sizeof(fc->corrupt_bio_flags) !=
150 sizeof(unsigned int));
151 r = dm_read_arg(_args + 3, as,
152 (__force unsigned *)&fc->corrupt_bio_flags,
153 &ti->error);
a3998799
MS
154 if (r)
155 return r;
156 argc--;
157
158 continue;
159 }
160
dfd068b0 161 ti->error = "Unrecognised flakey feature requested";
a3998799 162 return -EINVAL;
dfd068b0
MS
163 }
164
a3998799
MS
165 if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
166 ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
167 return -EINVAL;
ef548c55
MS
168
169 } else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
170 ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
171 return -EINVAL;
a3998799
MS
172 }
173
174 return 0;
dfd068b0
MS
175}
176
3407ef52 177/*
dfd068b0
MS
178 * Construct a flakey mapping:
179 * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
b26f5e3d
MS
180 *
181 * Feature args:
182 * [drop_writes]
a3998799
MS
183 * [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
184 *
185 * Nth_byte starts from 1 for the first byte.
186 * Direction is r for READ or w for WRITE.
187 * bio_flags is ignored if 0.
3407ef52
JB
188 */
189static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
190{
5916a22b 191 static const struct dm_arg _args[] = {
dfd068b0
MS
192 {0, UINT_MAX, "Invalid up interval"},
193 {0, UINT_MAX, "Invalid down interval"},
194 };
195
196 int r;
3407ef52 197 struct flakey_c *fc;
dfd068b0
MS
198 unsigned long long tmpll;
199 struct dm_arg_set as;
200 const char *devname;
31998ef1 201 char dummy;
3407ef52 202
dfd068b0
MS
203 as.argc = argc;
204 as.argv = argv;
205
206 if (argc < 4) {
207 ti->error = "Invalid argument count";
3407ef52
JB
208 return -EINVAL;
209 }
210
b26f5e3d 211 fc = kzalloc(sizeof(*fc), GFP_KERNEL);
3407ef52 212 if (!fc) {
75e3a0f5 213 ti->error = "Cannot allocate context";
3407ef52
JB
214 return -ENOMEM;
215 }
216 fc->start_time = jiffies;
217
dfd068b0
MS
218 devname = dm_shift_arg(&as);
219
e80d1c80 220 r = -EINVAL;
ef87bfc2 221 if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
dfd068b0 222 ti->error = "Invalid device sector";
3407ef52
JB
223 goto bad;
224 }
dfd068b0 225 fc->start = tmpll;
3407ef52 226
dfd068b0
MS
227 r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
228 if (r)
3407ef52 229 goto bad;
3407ef52 230
dfd068b0
MS
231 r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
232 if (r)
3407ef52 233 goto bad;
3407ef52
JB
234
235 if (!(fc->up_interval + fc->down_interval)) {
dfd068b0 236 ti->error = "Total (up + down) interval is zero";
bff7e067 237 r = -EINVAL;
3407ef52
JB
238 goto bad;
239 }
240
241 if (fc->up_interval + fc->down_interval < fc->up_interval) {
dfd068b0 242 ti->error = "Interval overflow";
bff7e067 243 r = -EINVAL;
3407ef52
JB
244 goto bad;
245 }
246
b26f5e3d 247 r = parse_features(&as, fc, ti);
dfd068b0
MS
248 if (r)
249 goto bad;
250
e80d1c80
VG
251 r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
252 if (r) {
dfd068b0 253 ti->error = "Device lookup failed";
3407ef52
JB
254 goto bad;
255 }
256
55a62eef
AK
257 ti->num_flush_bios = 1;
258 ti->num_discard_bios = 1;
30187e1d 259 ti->per_io_data_size = sizeof(struct per_bio_data);
3407ef52
JB
260 ti->private = fc;
261 return 0;
262
263bad:
264 kfree(fc);
e80d1c80 265 return r;
3407ef52
JB
266}
267
268static void flakey_dtr(struct dm_target *ti)
269{
270 struct flakey_c *fc = ti->private;
271
272 dm_put_device(ti, fc->dev);
273 kfree(fc);
274}
275
276static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
277{
278 struct flakey_c *fc = ti->private;
279
30e4171b 280 return fc->start + dm_target_offset(ti, bi_sector);
3407ef52
JB
281}
282
283static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
284{
285 struct flakey_c *fc = ti->private;
286
74d46992 287 bio_set_dev(bio, fc->dev->bdev);
e86f2b00 288 bio->bi_iter.bi_sector = flakey_map_sector(ti, bio->bi_iter.bi_sector);
3407ef52
JB
289}
290
a3998799
MS
291static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
292{
a00f5276
ST
293 unsigned int corrupt_bio_byte = fc->corrupt_bio_byte - 1;
294
295 struct bvec_iter iter;
296 struct bio_vec bvec;
297
298 if (!bio_has_data(bio))
299 return;
a3998799
MS
300
301 /*
a00f5276
ST
302 * Overwrite the Nth byte of the bio's data, on whichever page
303 * it falls.
a3998799 304 */
a00f5276
ST
305 bio_for_each_segment(bvec, bio, iter) {
306 if (bio_iter_len(bio, iter) > corrupt_bio_byte) {
f50714b5
MP
307 char *segment;
308 struct page *page = bio_iter_page(bio, iter);
309 if (unlikely(page == ZERO_PAGE(0)))
310 break;
8eb29c4f 311 segment = bvec_kmap_local(&bvec);
a00f5276 312 segment[corrupt_bio_byte] = fc->corrupt_bio_value;
8eb29c4f 313 kunmap_local(segment);
a00f5276
ST
314 DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
315 "(rw=%c bi_opf=%u bi_sector=%llu size=%u)\n",
316 bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
317 (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
318 (unsigned long long)bio->bi_iter.bi_sector, bio->bi_iter.bi_size);
319 break;
320 }
321 corrupt_bio_byte -= bio_iter_len(bio, iter);
a3998799
MS
322 }
323}
324
7de3ee57 325static int flakey_map(struct dm_target *ti, struct bio *bio)
3407ef52
JB
326{
327 struct flakey_c *fc = ti->private;
328 unsigned elapsed;
c7cfdf59
MP
329 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
330 pb->bio_submitted = false;
3407ef52 331
2e2d6f7e 332 if (op_is_zone_mgmt(bio_op(bio)))
124c4454
DLM
333 goto map_bio;
334
3407ef52
JB
335 /* Are we alive ? */
336 elapsed = (jiffies - fc->start_time) / HZ;
b26f5e3d 337 if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
a3998799
MS
338 /*
339 * Flag this bio as submitted while down.
340 */
c7cfdf59 341 pb->bio_submitted = true;
a3998799
MS
342
343 /*
ef548c55 344 * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
299f6230 345 * Otherwise, flakey_end_io() will decide if the reads should be modified.
a3998799 346 */
99f3c90d 347 if (bio_data_dir(bio) == READ) {
ef548c55
MS
348 if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags) &&
349 !test_bit(ERROR_WRITES, &fc->flags))
846785e6 350 return DM_MAPIO_KILL;
299f6230 351 goto map_bio;
99f3c90d 352 }
b26f5e3d
MS
353
354 /*
ef548c55 355 * Drop or error writes?
b26f5e3d
MS
356 */
357 if (test_bit(DROP_WRITES, &fc->flags)) {
4246a0b6 358 bio_endio(bio);
a3998799
MS
359 return DM_MAPIO_SUBMITTED;
360 }
ef548c55
MS
361 else if (test_bit(ERROR_WRITES, &fc->flags)) {
362 bio_io_error(bio);
363 return DM_MAPIO_SUBMITTED;
364 }
a3998799
MS
365
366 /*
367 * Corrupt matching writes.
368 */
aa56b9b7
MP
369 if (fc->corrupt_bio_byte) {
370 if (fc->corrupt_bio_rw == WRITE) {
371 if (all_corrupt_bio_flags_match(bio, fc))
372 corrupt_bio_data(bio, fc);
373 }
b26f5e3d
MS
374 goto map_bio;
375 }
376
377 /*
a3998799 378 * By default, error all I/O.
b26f5e3d 379 */
846785e6 380 return DM_MAPIO_KILL;
b26f5e3d 381 }
3407ef52 382
b26f5e3d 383map_bio:
3407ef52
JB
384 flakey_map_bio(ti, bio);
385
386 return DM_MAPIO_REMAPPED;
387}
388
4e4cbee9 389static int flakey_end_io(struct dm_target *ti, struct bio *bio,
124c4454 390 blk_status_t *error)
a3998799
MS
391{
392 struct flakey_c *fc = ti->private;
c7cfdf59 393 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
a3998799 394
2e2d6f7e 395 if (op_is_zone_mgmt(bio_op(bio)))
124c4454
DLM
396 return DM_ENDIO_DONE;
397
1be56909 398 if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
aa56b9b7
MP
399 if (fc->corrupt_bio_byte) {
400 if ((fc->corrupt_bio_rw == READ) &&
401 all_corrupt_bio_flags_match(bio, fc)) {
402 /*
403 * Corrupt successful matching READs while in down state.
404 */
405 corrupt_bio_data(bio, fc);
406 }
ef548c55
MS
407 } else if (!test_bit(DROP_WRITES, &fc->flags) &&
408 !test_bit(ERROR_WRITES, &fc->flags)) {
299f6230
MS
409 /*
410 * Error read during the down_interval if drop_writes
ef548c55 411 * and error_writes were not configured.
299f6230 412 */
4e4cbee9 413 *error = BLK_STS_IOERR;
299f6230 414 }
99f3c90d 415 }
a3998799 416
1be56909 417 return DM_ENDIO_DONE;
a3998799
MS
418}
419
fd7c092e
MP
420static void flakey_status(struct dm_target *ti, status_type_t type,
421 unsigned status_flags, char *result, unsigned maxlen)
3407ef52 422{
b26f5e3d 423 unsigned sz = 0;
3407ef52 424 struct flakey_c *fc = ti->private;
ef548c55 425 unsigned drop_writes, error_writes;
3407ef52
JB
426
427 switch (type) {
428 case STATUSTYPE_INFO:
429 result[0] = '\0';
430 break;
431
432 case STATUSTYPE_TABLE:
b26f5e3d
MS
433 DMEMIT("%s %llu %u %u ", fc->dev->name,
434 (unsigned long long)fc->start, fc->up_interval,
435 fc->down_interval);
436
437 drop_writes = test_bit(DROP_WRITES, &fc->flags);
ef548c55
MS
438 error_writes = test_bit(ERROR_WRITES, &fc->flags);
439 DMEMIT("%u ", drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5);
a3998799 440
b26f5e3d
MS
441 if (drop_writes)
442 DMEMIT("drop_writes ");
ef548c55
MS
443 else if (error_writes)
444 DMEMIT("error_writes ");
a3998799
MS
445
446 if (fc->corrupt_bio_byte)
447 DMEMIT("corrupt_bio_byte %u %c %u %u ",
448 fc->corrupt_bio_byte,
449 (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
450 fc->corrupt_bio_value, fc->corrupt_bio_flags);
451
3407ef52 452 break;
8ec45662
TS
453
454 case STATUSTYPE_IMA:
455 result[0] = '\0';
456 break;
3407ef52 457 }
3407ef52
JB
458}
459
5bd5e8d8 460static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
3407ef52
JB
461{
462 struct flakey_c *fc = ti->private;
e56f81e0
CH
463
464 *bdev = fc->dev->bdev;
3407ef52 465
ec8013be
PB
466 /*
467 * Only pass ioctls through if the device sizes match exactly.
468 */
6dcbb52c 469 if (fc->start || ti->len != bdev_nr_sectors((*bdev)))
e56f81e0
CH
470 return 1;
471 return 0;
3407ef52
JB
472}
473
e76239a3 474#ifdef CONFIG_BLK_DEV_ZONED
d4100351
CH
475static int flakey_report_zones(struct dm_target *ti,
476 struct dm_report_zones_args *args, unsigned int nr_zones)
e76239a3
CH
477{
478 struct flakey_c *fc = ti->private;
e76239a3 479
912e8875
DLM
480 return dm_report_zones(fc->dev->bdev, fc->start,
481 flakey_map_sector(ti, args->next_sector),
482 args, nr_zones);
e76239a3 483}
e3290b94
MS
484#else
485#define flakey_report_zones NULL
e76239a3
CH
486#endif
487
3407ef52
JB
488static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
489{
490 struct flakey_c *fc = ti->private;
491
492 return fn(ti, fc->dev, fc->start, ti->len, data);
493}
494
495static struct target_type flakey_target = {
496 .name = "flakey",
124c4454 497 .version = {1, 5, 0},
3db564b4 498 .features = DM_TARGET_ZONED_HM | DM_TARGET_PASSES_CRYPTO,
e76239a3 499 .report_zones = flakey_report_zones,
3407ef52
JB
500 .module = THIS_MODULE,
501 .ctr = flakey_ctr,
502 .dtr = flakey_dtr,
503 .map = flakey_map,
a3998799 504 .end_io = flakey_end_io,
3407ef52 505 .status = flakey_status,
e56f81e0 506 .prepare_ioctl = flakey_prepare_ioctl,
3407ef52
JB
507 .iterate_devices = flakey_iterate_devices,
508};
509
510static int __init dm_flakey_init(void)
511{
512 int r = dm_register_target(&flakey_target);
513
514 if (r < 0)
515 DMERR("register failed %d", r);
516
517 return r;
518}
519
520static void __exit dm_flakey_exit(void)
521{
522 dm_unregister_target(&flakey_target);
523}
524
525/* Module hooks */
526module_init(dm_flakey_init);
527module_exit(dm_flakey_exit);
528
529MODULE_DESCRIPTION(DM_NAME " flakey target");
530MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
531MODULE_LICENSE("GPL");