/drivers/md/dm-flakey.c

http://github.com/mirrors/linux · C · 519 lines · 358 code · 88 blank · 73 comment · 78 complexity · 8325c12a5ef5f37c4e72d6f5bfec5789 MD5 · raw file

  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. 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 (!strcasecmp(arg_name, "w"))
  107. fc->corrupt_bio_rw = WRITE;
  108. else if (!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. r = dm_read_arg(_args + 3, as, &fc->corrupt_bio_flags, &ti->error);
  126. if (r)
  127. return r;
  128. argc--;
  129. continue;
  130. }
  131. ti->error = "Unrecognised flakey feature requested";
  132. return -EINVAL;
  133. }
  134. if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
  135. ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
  136. return -EINVAL;
  137. } else if (test_bit(ERROR_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
  138. ti->error = "error_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
  139. return -EINVAL;
  140. }
  141. return 0;
  142. }
  143. /*
  144. * Construct a flakey mapping:
  145. * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
  146. *
  147. * Feature args:
  148. * [drop_writes]
  149. * [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
  150. *
  151. * Nth_byte starts from 1 for the first byte.
  152. * Direction is r for READ or w for WRITE.
  153. * bio_flags is ignored if 0.
  154. */
  155. static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  156. {
  157. static const struct dm_arg _args[] = {
  158. {0, UINT_MAX, "Invalid up interval"},
  159. {0, UINT_MAX, "Invalid down interval"},
  160. };
  161. int r;
  162. struct flakey_c *fc;
  163. unsigned long long tmpll;
  164. struct dm_arg_set as;
  165. const char *devname;
  166. char dummy;
  167. as.argc = argc;
  168. as.argv = argv;
  169. if (argc < 4) {
  170. ti->error = "Invalid argument count";
  171. return -EINVAL;
  172. }
  173. fc = kzalloc(sizeof(*fc), GFP_KERNEL);
  174. if (!fc) {
  175. ti->error = "Cannot allocate context";
  176. return -ENOMEM;
  177. }
  178. fc->start_time = jiffies;
  179. devname = dm_shift_arg(&as);
  180. r = -EINVAL;
  181. if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
  182. ti->error = "Invalid device sector";
  183. goto bad;
  184. }
  185. fc->start = tmpll;
  186. r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
  187. if (r)
  188. goto bad;
  189. r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
  190. if (r)
  191. goto bad;
  192. if (!(fc->up_interval + fc->down_interval)) {
  193. ti->error = "Total (up + down) interval is zero";
  194. r = -EINVAL;
  195. goto bad;
  196. }
  197. if (fc->up_interval + fc->down_interval < fc->up_interval) {
  198. ti->error = "Interval overflow";
  199. r = -EINVAL;
  200. goto bad;
  201. }
  202. r = parse_features(&as, fc, ti);
  203. if (r)
  204. goto bad;
  205. r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
  206. if (r) {
  207. ti->error = "Device lookup failed";
  208. goto bad;
  209. }
  210. ti->num_flush_bios = 1;
  211. ti->num_discard_bios = 1;
  212. ti->per_io_data_size = sizeof(struct per_bio_data);
  213. ti->private = fc;
  214. return 0;
  215. bad:
  216. kfree(fc);
  217. return r;
  218. }
  219. static void flakey_dtr(struct dm_target *ti)
  220. {
  221. struct flakey_c *fc = ti->private;
  222. dm_put_device(ti, fc->dev);
  223. kfree(fc);
  224. }
  225. static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
  226. {
  227. struct flakey_c *fc = ti->private;
  228. return fc->start + dm_target_offset(ti, bi_sector);
  229. }
  230. static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
  231. {
  232. struct flakey_c *fc = ti->private;
  233. bio_set_dev(bio, fc->dev->bdev);
  234. if (bio_sectors(bio) || op_is_zone_mgmt(bio_op(bio)))
  235. bio->bi_iter.bi_sector =
  236. flakey_map_sector(ti, bio->bi_iter.bi_sector);
  237. }
  238. static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
  239. {
  240. unsigned int corrupt_bio_byte = fc->corrupt_bio_byte - 1;
  241. struct bvec_iter iter;
  242. struct bio_vec bvec;
  243. if (!bio_has_data(bio))
  244. return;
  245. /*
  246. * Overwrite the Nth byte of the bio's data, on whichever page
  247. * it falls.
  248. */
  249. bio_for_each_segment(bvec, bio, iter) {
  250. if (bio_iter_len(bio, iter) > corrupt_bio_byte) {
  251. char *segment = (page_address(bio_iter_page(bio, iter))
  252. + bio_iter_offset(bio, iter));
  253. segment[corrupt_bio_byte] = fc->corrupt_bio_value;
  254. DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
  255. "(rw=%c bi_opf=%u bi_sector=%llu size=%u)\n",
  256. bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
  257. (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_opf,
  258. (unsigned long long)bio->bi_iter.bi_sector, bio->bi_iter.bi_size);
  259. break;
  260. }
  261. corrupt_bio_byte -= bio_iter_len(bio, iter);
  262. }
  263. }
  264. static int flakey_map(struct dm_target *ti, struct bio *bio)
  265. {
  266. struct flakey_c *fc = ti->private;
  267. unsigned elapsed;
  268. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  269. pb->bio_submitted = false;
  270. if (op_is_zone_mgmt(bio_op(bio)))
  271. goto map_bio;
  272. /* Are we alive ? */
  273. elapsed = (jiffies - fc->start_time) / HZ;
  274. if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
  275. /*
  276. * Flag this bio as submitted while down.
  277. */
  278. pb->bio_submitted = true;
  279. /*
  280. * Error reads if neither corrupt_bio_byte or drop_writes or error_writes are set.
  281. * Otherwise, flakey_end_io() will decide if the reads should be modified.
  282. */
  283. if (bio_data_dir(bio) == READ) {
  284. if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags) &&
  285. !test_bit(ERROR_WRITES, &fc->flags))
  286. return DM_MAPIO_KILL;
  287. goto map_bio;
  288. }
  289. /*
  290. * Drop or error writes?
  291. */
  292. if (test_bit(DROP_WRITES, &fc->flags)) {
  293. bio_endio(bio);
  294. return DM_MAPIO_SUBMITTED;
  295. }
  296. else if (test_bit(ERROR_WRITES, &fc->flags)) {
  297. bio_io_error(bio);
  298. return DM_MAPIO_SUBMITTED;
  299. }
  300. /*
  301. * Corrupt matching writes.
  302. */
  303. if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
  304. if (all_corrupt_bio_flags_match(bio, fc))
  305. corrupt_bio_data(bio, fc);
  306. goto map_bio;
  307. }
  308. /*
  309. * By default, error all I/O.
  310. */
  311. return DM_MAPIO_KILL;
  312. }
  313. map_bio:
  314. flakey_map_bio(ti, bio);
  315. return DM_MAPIO_REMAPPED;
  316. }
  317. static int flakey_end_io(struct dm_target *ti, struct bio *bio,
  318. blk_status_t *error)
  319. {
  320. struct flakey_c *fc = ti->private;
  321. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  322. if (op_is_zone_mgmt(bio_op(bio)))
  323. return DM_ENDIO_DONE;
  324. if (!*error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
  325. if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == READ) &&
  326. all_corrupt_bio_flags_match(bio, fc)) {
  327. /*
  328. * Corrupt successful matching READs while in down state.
  329. */
  330. corrupt_bio_data(bio, fc);
  331. } else if (!test_bit(DROP_WRITES, &fc->flags) &&
  332. !test_bit(ERROR_WRITES, &fc->flags)) {
  333. /*
  334. * Error read during the down_interval if drop_writes
  335. * and error_writes were not configured.
  336. */
  337. *error = BLK_STS_IOERR;
  338. }
  339. }
  340. return DM_ENDIO_DONE;
  341. }
  342. static void flakey_status(struct dm_target *ti, status_type_t type,
  343. unsigned status_flags, char *result, unsigned maxlen)
  344. {
  345. unsigned sz = 0;
  346. struct flakey_c *fc = ti->private;
  347. unsigned drop_writes, error_writes;
  348. switch (type) {
  349. case STATUSTYPE_INFO:
  350. result[0] = '\0';
  351. break;
  352. case STATUSTYPE_TABLE:
  353. DMEMIT("%s %llu %u %u ", fc->dev->name,
  354. (unsigned long long)fc->start, fc->up_interval,
  355. fc->down_interval);
  356. drop_writes = test_bit(DROP_WRITES, &fc->flags);
  357. error_writes = test_bit(ERROR_WRITES, &fc->flags);
  358. DMEMIT("%u ", drop_writes + error_writes + (fc->corrupt_bio_byte > 0) * 5);
  359. if (drop_writes)
  360. DMEMIT("drop_writes ");
  361. else if (error_writes)
  362. DMEMIT("error_writes ");
  363. if (fc->corrupt_bio_byte)
  364. DMEMIT("corrupt_bio_byte %u %c %u %u ",
  365. fc->corrupt_bio_byte,
  366. (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
  367. fc->corrupt_bio_value, fc->corrupt_bio_flags);
  368. break;
  369. }
  370. }
  371. static int flakey_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
  372. {
  373. struct flakey_c *fc = ti->private;
  374. *bdev = fc->dev->bdev;
  375. /*
  376. * Only pass ioctls through if the device sizes match exactly.
  377. */
  378. if (fc->start ||
  379. ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
  380. return 1;
  381. return 0;
  382. }
  383. #ifdef CONFIG_BLK_DEV_ZONED
  384. static int flakey_report_zones(struct dm_target *ti,
  385. struct dm_report_zones_args *args, unsigned int nr_zones)
  386. {
  387. struct flakey_c *fc = ti->private;
  388. sector_t sector = flakey_map_sector(ti, args->next_sector);
  389. args->start = fc->start;
  390. return blkdev_report_zones(fc->dev->bdev, sector, nr_zones,
  391. dm_report_zones_cb, args);
  392. }
  393. #endif
  394. static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
  395. {
  396. struct flakey_c *fc = ti->private;
  397. return fn(ti, fc->dev, fc->start, ti->len, data);
  398. }
  399. static struct target_type flakey_target = {
  400. .name = "flakey",
  401. .version = {1, 5, 0},
  402. #ifdef CONFIG_BLK_DEV_ZONED
  403. .features = DM_TARGET_ZONED_HM,
  404. .report_zones = flakey_report_zones,
  405. #endif
  406. .module = THIS_MODULE,
  407. .ctr = flakey_ctr,
  408. .dtr = flakey_dtr,
  409. .map = flakey_map,
  410. .end_io = flakey_end_io,
  411. .status = flakey_status,
  412. .prepare_ioctl = flakey_prepare_ioctl,
  413. .iterate_devices = flakey_iterate_devices,
  414. };
  415. static int __init dm_flakey_init(void)
  416. {
  417. int r = dm_register_target(&flakey_target);
  418. if (r < 0)
  419. DMERR("register failed %d", r);
  420. return r;
  421. }
  422. static void __exit dm_flakey_exit(void)
  423. {
  424. dm_unregister_target(&flakey_target);
  425. }
  426. /* Module hooks */
  427. module_init(dm_flakey_init);
  428. module_exit(dm_flakey_exit);
  429. MODULE_DESCRIPTION(DM_NAME " flakey target");
  430. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  431. MODULE_LICENSE("GPL");