dm-flakey.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 linear 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_sector = flakey_map_sector(ti, bio->bi_sector);
  208. }
  209. static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
  210. {
  211. unsigned bio_bytes = bio_cur_bytes(bio);
  212. char *data = bio_data(bio);
  213. /*
  214. * Overwrite the Nth byte of the data returned.
  215. */
  216. if (data && bio_bytes >= fc->corrupt_bio_byte) {
  217. data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value;
  218. DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
  219. "(rw=%c bi_rw=%lu bi_sector=%llu cur_bytes=%u)\n",
  220. bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
  221. (bio_data_dir(bio) == WRITE) ? 'w' : 'r',
  222. bio->bi_rw, (unsigned long long)bio->bi_sector, bio_bytes);
  223. }
  224. }
  225. static int flakey_map(struct dm_target *ti, struct bio *bio)
  226. {
  227. struct flakey_c *fc = ti->private;
  228. unsigned elapsed;
  229. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  230. pb->bio_submitted = false;
  231. /* Are we alive ? */
  232. elapsed = (jiffies - fc->start_time) / HZ;
  233. if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
  234. /*
  235. * Flag this bio as submitted while down.
  236. */
  237. pb->bio_submitted = true;
  238. /*
  239. * Map reads as normal.
  240. */
  241. if (bio_data_dir(bio) == READ)
  242. goto map_bio;
  243. /*
  244. * Drop writes?
  245. */
  246. if (test_bit(DROP_WRITES, &fc->flags)) {
  247. bio_endio(bio, 0);
  248. return DM_MAPIO_SUBMITTED;
  249. }
  250. /*
  251. * Corrupt matching writes.
  252. */
  253. if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
  254. if (all_corrupt_bio_flags_match(bio, fc))
  255. corrupt_bio_data(bio, fc);
  256. goto map_bio;
  257. }
  258. /*
  259. * By default, error all I/O.
  260. */
  261. return -EIO;
  262. }
  263. map_bio:
  264. flakey_map_bio(ti, bio);
  265. return DM_MAPIO_REMAPPED;
  266. }
  267. static int flakey_end_io(struct dm_target *ti, struct bio *bio, int error)
  268. {
  269. struct flakey_c *fc = ti->private;
  270. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  271. /*
  272. * Corrupt successful READs while in down state.
  273. * If flags were specified, only corrupt those that match.
  274. */
  275. if (fc->corrupt_bio_byte && !error && pb->bio_submitted &&
  276. (bio_data_dir(bio) == READ) && (fc->corrupt_bio_rw == READ) &&
  277. all_corrupt_bio_flags_match(bio, fc))
  278. corrupt_bio_data(bio, fc);
  279. return error;
  280. }
  281. static void flakey_status(struct dm_target *ti, status_type_t type,
  282. unsigned status_flags, char *result, unsigned maxlen)
  283. {
  284. unsigned sz = 0;
  285. struct flakey_c *fc = ti->private;
  286. unsigned drop_writes;
  287. switch (type) {
  288. case STATUSTYPE_INFO:
  289. result[0] = '\0';
  290. break;
  291. case STATUSTYPE_TABLE:
  292. DMEMIT("%s %llu %u %u ", fc->dev->name,
  293. (unsigned long long)fc->start, fc->up_interval,
  294. fc->down_interval);
  295. drop_writes = test_bit(DROP_WRITES, &fc->flags);
  296. DMEMIT("%u ", drop_writes + (fc->corrupt_bio_byte > 0) * 5);
  297. if (drop_writes)
  298. DMEMIT("drop_writes ");
  299. if (fc->corrupt_bio_byte)
  300. DMEMIT("corrupt_bio_byte %u %c %u %u ",
  301. fc->corrupt_bio_byte,
  302. (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
  303. fc->corrupt_bio_value, fc->corrupt_bio_flags);
  304. break;
  305. }
  306. }
  307. static int flakey_ioctl(struct dm_target *ti, unsigned int cmd, unsigned long arg)
  308. {
  309. struct flakey_c *fc = ti->private;
  310. struct dm_dev *dev = fc->dev;
  311. int r = 0;
  312. /*
  313. * Only pass ioctls through if the device sizes match exactly.
  314. */
  315. if (fc->start ||
  316. ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
  317. r = scsi_verify_blk_ioctl(NULL, cmd);
  318. return r ? : __blkdev_driver_ioctl(dev->bdev, dev->mode, cmd, arg);
  319. }
  320. static int flakey_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  321. struct bio_vec *biovec, int max_size)
  322. {
  323. struct flakey_c *fc = ti->private;
  324. struct request_queue *q = bdev_get_queue(fc->dev->bdev);
  325. if (!q->merge_bvec_fn)
  326. return max_size;
  327. bvm->bi_bdev = fc->dev->bdev;
  328. bvm->bi_sector = flakey_map_sector(ti, bvm->bi_sector);
  329. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  330. }
  331. static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
  332. {
  333. struct flakey_c *fc = ti->private;
  334. return fn(ti, fc->dev, fc->start, ti->len, data);
  335. }
  336. static struct target_type flakey_target = {
  337. .name = "flakey",
  338. .version = {1, 3, 1},
  339. .module = THIS_MODULE,
  340. .ctr = flakey_ctr,
  341. .dtr = flakey_dtr,
  342. .map = flakey_map,
  343. .end_io = flakey_end_io,
  344. .status = flakey_status,
  345. .ioctl = flakey_ioctl,
  346. .merge = flakey_merge,
  347. .iterate_devices = flakey_iterate_devices,
  348. };
  349. static int __init dm_flakey_init(void)
  350. {
  351. int r = dm_register_target(&flakey_target);
  352. if (r < 0)
  353. DMERR("register failed %d", r);
  354. return r;
  355. }
  356. static void __exit dm_flakey_exit(void)
  357. {
  358. dm_unregister_target(&flakey_target);
  359. }
  360. /* Module hooks */
  361. module_init(dm_flakey_init);
  362. module_exit(dm_flakey_exit);
  363. MODULE_DESCRIPTION(DM_NAME " flakey target");
  364. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  365. MODULE_LICENSE("GPL");