dm-flakey.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 up_interval;
  25. unsigned down_interval;
  26. unsigned long flags;
  27. unsigned corrupt_bio_byte;
  28. unsigned corrupt_bio_rw;
  29. unsigned corrupt_bio_value;
  30. unsigned 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 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. /*
  61. * drop_writes
  62. */
  63. if (!strcasecmp(arg_name, "drop_writes")) {
  64. if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
  65. ti->error = "Feature drop_writes duplicated";
  66. return -EINVAL;
  67. } else if (test_bit(ERROR_WRITES, &fc->flags)) {
  68. ti->error = "Feature drop_writes conflicts with feature error_writes";
  69. return -EINVAL;
  70. }
  71. continue;
  72. }
  73. /*
  74. * error_writes
  75. */
  76. if (!strcasecmp(arg_name, "error_writes")) {
  77. if (test_and_set_bit(ERROR_WRITES, &fc->flags)) {
  78. ti->error = "Feature error_writes duplicated";
  79. return -EINVAL;
  80. } else if (test_bit(DROP_WRITES, &fc->flags)) {
  81. ti->error = "Feature error_writes conflicts with feature drop_writes";
  82. return -EINVAL;
  83. }
  84. continue;
  85. }
  86. /*
  87. * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
  88. */
  89. if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
  90. if (!argc) {
  91. ti->error = "Feature corrupt_bio_byte requires parameters";
  92. return -EINVAL;
  93. }
  94. r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
  95. if (r)
  96. return r;
  97. argc--;
  98. /*
  99. * Direction r or w?
  100. */
  101. arg_name = dm_shift_arg(as);
  102. if (!strcasecmp(arg_name, "w"))
  103. fc->corrupt_bio_rw = WRITE;
  104. else if (!strcasecmp(arg_name, "r"))
  105. fc->corrupt_bio_rw = READ;
  106. else {
  107. ti->error = "Invalid corrupt bio direction (r or w)";
  108. return -EINVAL;
  109. }
  110. argc--;
  111. /*
  112. * Value of byte (0-255) to write in place of correct one.
  113. */
  114. r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
  115. if (r)
  116. return r;
  117. argc--;
  118. /*
  119. * Only corrupt bios with these flags set.
  120. */
  121. r = dm_read_arg(_args + 3, as, &fc->corrupt_bio_flags, &ti->error);
  122. if (r)
  123. return r;
  124. argc--;
  125. continue;
  126. }
  127. ti->error = "Unrecognised flakey feature requested";
  128. return -EINVAL;
  129. }
  130. if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
  131. ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
  132. return -EINVAL;
  133. } else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
  134. ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
  135. return -EINVAL;
  136. }
  137. return 0;
  138. }
  139. /*
  140. * Construct a flakey mapping:
  141. * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
  142. *
  143. * Feature args:
  144. * [drop_writes]
  145. * [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
  146. *
  147. * Nth_byte starts from 1 for the first byte.
  148. * Direction is r for READ or w for WRITE.
  149. * bio_flags is ignored if 0.
  150. */
  151. static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  152. {
  153. static const struct dm_arg _args[] = {
  154. {0, UINT_MAX, "Invalid up interval"},
  155. {0, UINT_MAX, "Invalid down interval"},
  156. };
  157. int r;
  158. struct flakey_c *fc;
  159. unsigned long long tmpll;
  160. struct dm_arg_set as;
  161. const char *devname;
  162. char dummy;
  163. as.argc = argc;
  164. as.argv = argv;
  165. if (argc < 4) {
  166. ti->error = "Invalid argument count";
  167. return -EINVAL;
  168. }
  169. fc = kzalloc(sizeof(*fc), GFP_KERNEL);
  170. if (!fc) {
  171. ti->error = "Cannot allocate context";
  172. return -ENOMEM;
  173. }
  174. fc->start_time = jiffies;
  175. devname = dm_shift_arg(&as);
  176. r = -EINVAL;
  177. if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1) {
  178. ti->error = "Invalid device sector";
  179. goto bad;
  180. }
  181. fc->start = tmpll;
  182. r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
  183. if (r)
  184. goto bad;
  185. r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
  186. if (r)
  187. goto bad;
  188. if (!(fc->up_interval + fc->down_interval)) {
  189. ti->error = "Total (up + down) interval is zero";
  190. r = -EINVAL;
  191. goto bad;
  192. }
  193. if (fc->up_interval + fc->down_interval < fc->up_interval) {
  194. ti->error = "Interval overflow";
  195. r = -EINVAL;
  196. goto bad;
  197. }
  198. r = parse_features(&as, fc, ti);
  199. if (r)
  200. goto bad;
  201. r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
  202. if (r) {
  203. ti->error = "Device lookup failed";
  204. goto bad;
  205. }
  206. ti->num_flush_bios = 1;
  207. ti->num_discard_bios = 1;
  208. ti->per_io_data_size = sizeof(struct per_bio_data);
  209. ti->private = fc;
  210. return 0;
  211. bad:
  212. kfree(fc);
  213. return r;
  214. }
  215. static void flakey_dtr(struct dm_target *ti)
  216. {
  217. struct flakey_c *fc = ti->private;
  218. dm_put_device(ti, fc->dev);
  219. kfree(fc);
  220. }
  221. static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
  222. {
  223. struct flakey_c *fc = ti->private;
  224. return fc->start + dm_target_offset(ti, bi_sector);
  225. }
  226. static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
  227. {
  228. struct flakey_c *fc = ti->private;
  229. bio_set_dev(bio, fc->dev->bdev);
  230. if (bio_sectors(bio) || bio_op(bio) == REQ_OP_ZONE_RESET)
  231. bio->bi_iter.bi_sector =
  232. flakey_map_sector(ti, bio->bi_iter.bi_sector);
  233. }
  234. static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
  235. {
  236. unsigned bio_bytes = bio_cur_bytes(bio);
  237. char *data = bio_data(bio);
  238. /*
  239. * Overwrite the Nth byte of the data returned.
  240. */
  241. if (data && bio_bytes >= fc->corrupt_bio_byte) {
  242. data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value;
  243. DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
  244. "(rw=%c bi_opf=%u bi_sector=%llu cur_bytes=%u)\n",
  245. bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
  246. (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
  247. (unsigned long long)bio->bi_iter.bi_sector, bio_bytes);
  248. }
  249. }
  250. static int flakey_map(struct dm_target *ti, struct bio *bio)
  251. {
  252. struct flakey_c *fc = ti->private;
  253. unsigned elapsed;
  254. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  255. pb->bio_submitted = false;
  256. /* Do not fail reset zone */
  257. if (bio_op(bio) == REQ_OP_ZONE_RESET)
  258. goto map_bio;
  259. /* We need to remap reported zones, so remember the BIO iter */
  260. if (bio_op(bio) == REQ_OP_ZONE_REPORT)
  261. goto map_bio;
  262. /* Are we alive ? */
  263. elapsed = (jiffies - fc->start_time) / HZ;
  264. if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
  265. /*
  266. * Flag this bio as submitted while down.
  267. */
  268. pb->bio_submitted = true;
  269. /*
  270. * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
  271. * Otherwise, flakey_end_io() will decide if the reads should be modified.
  272. */
  273. if (bio_data_dir(bio) == READ) {
  274. if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags) &&
  275. !test_bit(ERROR_WRITES, &fc->flags))
  276. return DM_MAPIO_KILL;
  277. goto map_bio;
  278. }
  279. /*
  280. * Drop or error writes?
  281. */
  282. if (test_bit(DROP_WRITES, &fc->flags)) {
  283. bio_endio(bio);
  284. return DM_MAPIO_SUBMITTED;
  285. }
  286. else if (test_bit(ERROR_WRITES, &fc->flags)) {
  287. bio_io_error(bio);
  288. return DM_MAPIO_SUBMITTED;
  289. }
  290. /*
  291. * Corrupt matching writes.
  292. */
  293. if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
  294. if (all_corrupt_bio_flags_match(bio, fc))
  295. corrupt_bio_data(bio, fc);
  296. goto map_bio;
  297. }
  298. /*
  299. * By default, error all I/O.
  300. */
  301. return DM_MAPIO_KILL;
  302. }
  303. map_bio:
  304. flakey_map_bio(ti, bio);
  305. return DM_MAPIO_REMAPPED;
  306. }
  307. static int flakey_end_io(struct dm_target *ti, struct bio *bio,
  308. blk_status_t *error)
  309. {
  310. struct flakey_c *fc = ti->private;
  311. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  312. if (bio_op(bio) == REQ_OP_ZONE_RESET)
  313. return DM_ENDIO_DONE;
  314. if (bio_op(bio) == REQ_OP_ZONE_REPORT) {
  315. dm_remap_zone_report(ti, bio, fc->start);
  316. return DM_ENDIO_DONE;
  317. }
  318. if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
  319. if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == READ) &&
  320. all_corrupt_bio_flags_match(bio, fc)) {
  321. /*
  322. * Corrupt successful matching READs while in down state.
  323. */
  324. corrupt_bio_data(bio, fc);
  325. } else if (!test_bit(DROP_WRITES, &fc->flags) &&
  326. !test_bit(ERROR_WRITES, &fc->flags)) {
  327. /*
  328. * Error read during the down_interval if drop_writes
  329. * and error_writes were not configured.
  330. */
  331. *error = BLK_STS_IOERR;
  332. }
  333. }
  334. return DM_ENDIO_DONE;
  335. }
  336. static void flakey_status(struct dm_target *ti, status_type_t type,
  337. unsigned status_flags, char *result, unsigned maxlen)
  338. {
  339. unsigned sz = 0;
  340. struct flakey_c *fc = ti->private;
  341. unsigned drop_writes, error_writes;
  342. switch (type) {
  343. case STATUSTYPE_INFO:
  344. result[0] = '\0';
  345. break;
  346. case STATUSTYPE_TABLE:
  347. DMEMIT("%s %llu %u %u ", fc->dev->name,
  348. (unsigned long long)fc->start, fc->up_interval,
  349. fc->down_interval);
  350. drop_writes = test_bit(DROP_WRITES, &fc->flags);
  351. error_writes = test_bit(ERROR_WRITES, &fc->flags);
  352. DMEMIT("%u ", drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5);
  353. if (drop_writes)
  354. DMEMIT("drop_writes ");
  355. else if (error_writes)
  356. DMEMIT("error_writes ");
  357. if (fc->corrupt_bio_byte)
  358. DMEMIT("corrupt_bio_byte %u %c %u %u ",
  359. fc->corrupt_bio_byte,
  360. (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
  361. fc->corrupt_bio_value, fc->corrupt_bio_flags);
  362. break;
  363. }
  364. }
  365. static int flakey_prepare_ioctl(struct dm_target *ti,
  366. struct block_device **bdev, fmode_t *mode)
  367. {
  368. struct flakey_c *fc = ti->private;
  369. *bdev = fc->dev->bdev;
  370. /*
  371. * Only pass ioctls through if the device sizes match exactly.
  372. */
  373. if (fc->start ||
  374. ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
  375. return 1;
  376. return 0;
  377. }
  378. static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
  379. {
  380. struct flakey_c *fc = ti->private;
  381. return fn(ti, fc->dev, fc->start, ti->len, data);
  382. }
  383. static struct target_type flakey_target = {
  384. .name = "flakey",
  385. .version = {1, 5, 0},
  386. .features = DM_TARGET_ZONED_HM,
  387. .module = THIS_MODULE,
  388. .ctr = flakey_ctr,
  389. .dtr = flakey_dtr,
  390. .map = flakey_map,
  391. .end_io = flakey_end_io,
  392. .status = flakey_status,
  393. .prepare_ioctl = flakey_prepare_ioctl,
  394. .iterate_devices = flakey_iterate_devices,
  395. };
  396. static int __init dm_flakey_init(void)
  397. {
  398. int r = dm_register_target(&flakey_target);
  399. if (r < 0)
  400. DMERR("register failed %d", r);
  401. return r;
  402. }
  403. static void __exit dm_flakey_exit(void)
  404. {
  405. dm_unregister_target(&flakey_target);
  406. }
  407. /* Module hooks */
  408. module_init(dm_flakey_init);
  409. module_exit(dm_flakey_exit);
  410. MODULE_DESCRIPTION(DM_NAME " flakey target");
  411. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  412. MODULE_LICENSE("GPL");