dm-flakey.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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_rw & (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. };
  35. struct per_bio_data {
  36. bool bio_submitted;
  37. };
  38. static int parse_features(struct dm_arg_set *as, struct flakey_c *fc,
  39. struct dm_target *ti)
  40. {
  41. int r;
  42. unsigned argc;
  43. const char *arg_name;
  44. static struct dm_arg _args[] = {
  45. {0, 6, "Invalid number of feature args"},
  46. {1, UINT_MAX, "Invalid corrupt bio byte"},
  47. {0, 255, "Invalid corrupt value to write into bio byte (0-255)"},
  48. {0, UINT_MAX, "Invalid corrupt bio flags mask"},
  49. };
  50. /* No feature arguments supplied. */
  51. if (!as->argc)
  52. return 0;
  53. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  54. if (r)
  55. return r;
  56. while (argc) {
  57. arg_name = dm_shift_arg(as);
  58. argc--;
  59. /*
  60. * drop_writes
  61. */
  62. if (!strcasecmp(arg_name, "drop_writes")) {
  63. if (test_and_set_bit(DROP_WRITES, &fc->flags)) {
  64. ti->error = "Feature drop_writes duplicated";
  65. return -EINVAL;
  66. }
  67. continue;
  68. }
  69. /*
  70. * corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>
  71. */
  72. if (!strcasecmp(arg_name, "corrupt_bio_byte")) {
  73. if (!argc) {
  74. ti->error = "Feature corrupt_bio_byte requires parameters";
  75. return -EINVAL;
  76. }
  77. r = dm_read_arg(_args + 1, as, &fc->corrupt_bio_byte, &ti->error);
  78. if (r)
  79. return r;
  80. argc--;
  81. /*
  82. * Direction r or w?
  83. */
  84. arg_name = dm_shift_arg(as);
  85. if (!strcasecmp(arg_name, "w"))
  86. fc->corrupt_bio_rw = WRITE;
  87. else if (!strcasecmp(arg_name, "r"))
  88. fc->corrupt_bio_rw = READ;
  89. else {
  90. ti->error = "Invalid corrupt bio direction (r or w)";
  91. return -EINVAL;
  92. }
  93. argc--;
  94. /*
  95. * Value of byte (0-255) to write in place of correct one.
  96. */
  97. r = dm_read_arg(_args + 2, as, &fc->corrupt_bio_value, &ti->error);
  98. if (r)
  99. return r;
  100. argc--;
  101. /*
  102. * Only corrupt bios with these flags set.
  103. */
  104. r = dm_read_arg(_args + 3, as, &fc->corrupt_bio_flags, &ti->error);
  105. if (r)
  106. return r;
  107. argc--;
  108. continue;
  109. }
  110. ti->error = "Unrecognised flakey feature requested";
  111. return -EINVAL;
  112. }
  113. if (test_bit(DROP_WRITES, &fc->flags) && (fc->corrupt_bio_rw == WRITE)) {
  114. ti->error = "drop_writes is incompatible with corrupt_bio_byte with the WRITE flag set";
  115. return -EINVAL;
  116. }
  117. return 0;
  118. }
  119. /*
  120. * Construct a flakey mapping:
  121. * <dev_path> <offset> <up interval> <down interval> [<#feature args> [<arg>]*]
  122. *
  123. * Feature args:
  124. * [drop_writes]
  125. * [corrupt_bio_byte <Nth_byte> <direction> <value> <bio_flags>]
  126. *
  127. * Nth_byte starts from 1 for the first byte.
  128. * Direction is r for READ or w for WRITE.
  129. * bio_flags is ignored if 0.
  130. */
  131. static int flakey_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  132. {
  133. static struct dm_arg _args[] = {
  134. {0, UINT_MAX, "Invalid up interval"},
  135. {0, UINT_MAX, "Invalid down interval"},
  136. };
  137. int r;
  138. struct flakey_c *fc;
  139. unsigned long long tmpll;
  140. struct dm_arg_set as;
  141. const char *devname;
  142. char dummy;
  143. as.argc = argc;
  144. as.argv = argv;
  145. if (argc < 4) {
  146. ti->error = "Invalid argument count";
  147. return -EINVAL;
  148. }
  149. fc = kzalloc(sizeof(*fc), GFP_KERNEL);
  150. if (!fc) {
  151. ti->error = "Cannot allocate context";
  152. return -ENOMEM;
  153. }
  154. fc->start_time = jiffies;
  155. devname = dm_shift_arg(&as);
  156. if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1) {
  157. ti->error = "Invalid device sector";
  158. goto bad;
  159. }
  160. fc->start = tmpll;
  161. r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
  162. if (r)
  163. goto bad;
  164. r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
  165. if (r)
  166. goto bad;
  167. if (!(fc->up_interval + fc->down_interval)) {
  168. ti->error = "Total (up + down) interval is zero";
  169. goto bad;
  170. }
  171. if (fc->up_interval + fc->down_interval < fc->up_interval) {
  172. ti->error = "Interval overflow";
  173. goto bad;
  174. }
  175. r = parse_features(&as, fc, ti);
  176. if (r)
  177. goto bad;
  178. if (dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev)) {
  179. ti->error = "Device lookup failed";
  180. goto bad;
  181. }
  182. ti->num_flush_bios = 1;
  183. ti->num_discard_bios = 1;
  184. ti->per_bio_data_size = sizeof(struct per_bio_data);
  185. ti->private = fc;
  186. return 0;
  187. bad:
  188. kfree(fc);
  189. return -EINVAL;
  190. }
  191. static void flakey_dtr(struct dm_target *ti)
  192. {
  193. struct flakey_c *fc = ti->private;
  194. dm_put_device(ti, fc->dev);
  195. kfree(fc);
  196. }
  197. static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
  198. {
  199. struct flakey_c *fc = ti->private;
  200. return fc->start + dm_target_offset(ti, bi_sector);
  201. }
  202. static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
  203. {
  204. struct flakey_c *fc = ti->private;
  205. bio->bi_bdev = fc->dev->bdev;
  206. if (bio_sectors(bio))
  207. bio->bi_iter.bi_sector =
  208. flakey_map_sector(ti, bio->bi_iter.bi_sector);
  209. }
  210. static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
  211. {
  212. unsigned bio_bytes = bio_cur_bytes(bio);
  213. char *data = bio_data(bio);
  214. /*
  215. * Overwrite the Nth byte of the data returned.
  216. */
  217. if (data && bio_bytes >= fc->corrupt_bio_byte) {
  218. data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value;
  219. DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
  220. "(rw=%c bi_rw=%lu bi_sector=%llu cur_bytes=%u)\n",
  221. bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
  222. (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_rw,
  223. (unsigned long long)bio->bi_iter.bi_sector, bio_bytes);
  224. }
  225. }
  226. static int flakey_map(struct dm_target *ti, struct bio *bio)
  227. {
  228. struct flakey_c *fc = ti->private;
  229. unsigned elapsed;
  230. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  231. pb->bio_submitted = false;
  232. /* Are we alive ? */
  233. elapsed = (jiffies - fc->start_time) / HZ;
  234. if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
  235. /*
  236. * Flag this bio as submitted while down.
  237. */
  238. pb->bio_submitted = true;
  239. /*
  240. * Map reads as normal.
  241. */
  242. if (bio_data_dir(bio) == READ)
  243. goto map_bio;
  244. /*
  245. * Drop writes?
  246. */
  247. if (test_bit(DROP_WRITES, &fc->flags)) {
  248. bio_endio(bio, 0);
  249. return DM_MAPIO_SUBMITTED;
  250. }
  251. /*
  252. * Corrupt matching writes.
  253. */
  254. if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
  255. if (all_corrupt_bio_flags_match(bio, fc))
  256. corrupt_bio_data(bio, fc);
  257. goto map_bio;
  258. }
  259. /*
  260. * By default, error all I/O.
  261. */
  262. return -EIO;
  263. }
  264. map_bio:
  265. flakey_map_bio(ti, bio);
  266. return DM_MAPIO_REMAPPED;
  267. }
  268. static int flakey_end_io(struct dm_target *ti, struct bio *bio, int error)
  269. {
  270. struct flakey_c *fc = ti->private;
  271. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  272. /*
  273. * Corrupt successful READs while in down state.
  274. * If flags were specified, only corrupt those that match.
  275. */
  276. if (fc->corrupt_bio_byte && !error && pb->bio_submitted &&
  277. (bio_data_dir(bio) == READ) && (fc->corrupt_bio_rw == READ) &&
  278. all_corrupt_bio_flags_match(bio, fc))
  279. corrupt_bio_data(bio, fc);
  280. return error;
  281. }
  282. static void flakey_status(struct dm_target *ti, status_type_t type,
  283. unsigned status_flags, char *result, unsigned maxlen)
  284. {
  285. unsigned sz = 0;
  286. struct flakey_c *fc = ti->private;
  287. unsigned drop_writes;
  288. switch (type) {
  289. case STATUSTYPE_INFO:
  290. result[0] = '\0';
  291. break;
  292. case STATUSTYPE_TABLE:
  293. DMEMIT("%s %llu %u %u ", fc->dev->name,
  294. (unsigned long long)fc->start, fc->up_interval,
  295. fc->down_interval);
  296. drop_writes = test_bit(DROP_WRITES, &fc->flags);
  297. DMEMIT("%u ", drop_writes + (fc->corrupt_bio_byte > 0) * 5);
  298. if (drop_writes)
  299. DMEMIT("drop_writes ");
  300. if (fc->corrupt_bio_byte)
  301. DMEMIT("corrupt_bio_byte %u %c %u %u ",
  302. fc->corrupt_bio_byte,
  303. (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
  304. fc->corrupt_bio_value, fc->corrupt_bio_flags);
  305. break;
  306. }
  307. }
  308. static int flakey_ioctl(struct dm_target *ti, unsigned int cmd, unsigned long arg)
  309. {
  310. struct flakey_c *fc = ti->private;
  311. struct dm_dev *dev = fc->dev;
  312. int r = 0;
  313. /*
  314. * Only pass ioctls through if the device sizes match exactly.
  315. */
  316. if (fc->start ||
  317. ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
  318. r = scsi_verify_blk_ioctl(NULL, cmd);
  319. return r ? : __blkdev_driver_ioctl(dev->bdev, dev->mode, cmd, arg);
  320. }
  321. static int flakey_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  322. struct bio_vec *biovec, int max_size)
  323. {
  324. struct flakey_c *fc = ti->private;
  325. struct request_queue *q = bdev_get_queue(fc->dev->bdev);
  326. if (!q->merge_bvec_fn)
  327. return max_size;
  328. bvm->bi_bdev = fc->dev->bdev;
  329. bvm->bi_sector = flakey_map_sector(ti, bvm->bi_sector);
  330. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  331. }
  332. static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
  333. {
  334. struct flakey_c *fc = ti->private;
  335. return fn(ti, fc->dev, fc->start, ti->len, data);
  336. }
  337. static struct target_type flakey_target = {
  338. .name = "flakey",
  339. .version = {1, 3, 1},
  340. .module = THIS_MODULE,
  341. .ctr = flakey_ctr,
  342. .dtr = flakey_dtr,
  343. .map = flakey_map,
  344. .end_io = flakey_end_io,
  345. .status = flakey_status,
  346. .ioctl = flakey_ioctl,
  347. .merge = flakey_merge,
  348. .iterate_devices = flakey_iterate_devices,
  349. };
  350. static int __init dm_flakey_init(void)
  351. {
  352. int r = dm_register_target(&flakey_target);
  353. if (r < 0)
  354. DMERR("register failed %d", r);
  355. return r;
  356. }
  357. static void __exit dm_flakey_exit(void)
  358. {
  359. dm_unregister_target(&flakey_target);
  360. }
  361. /* Module hooks */
  362. module_init(dm_flakey_init);
  363. module_exit(dm_flakey_exit);
  364. MODULE_DESCRIPTION(DM_NAME " flakey target");
  365. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  366. MODULE_LICENSE("GPL");