dm-flakey.c 12 KB

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