dm-flakey.c 9.8 KB

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