dm-flakey.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. r = -EINVAL;
  157. if (sscanf(dm_shift_arg(&as), "%llu%c", &tmpll, &dummy) != 1) {
  158. ti->error = "Invalid device sector";
  159. goto bad;
  160. }
  161. fc->start = tmpll;
  162. r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
  163. if (r)
  164. goto bad;
  165. r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
  166. if (r)
  167. goto bad;
  168. if (!(fc->up_interval + fc->down_interval)) {
  169. ti->error = "Total (up + down) interval is zero";
  170. r = -EINVAL;
  171. goto bad;
  172. }
  173. if (fc->up_interval + fc->down_interval < fc->up_interval) {
  174. ti->error = "Interval overflow";
  175. r = -EINVAL;
  176. goto bad;
  177. }
  178. r = parse_features(&as, fc, ti);
  179. if (r)
  180. goto bad;
  181. r = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev);
  182. if (r) {
  183. ti->error = "Device lookup failed";
  184. goto bad;
  185. }
  186. ti->num_flush_bios = 1;
  187. ti->num_discard_bios = 1;
  188. ti->per_bio_data_size = sizeof(struct per_bio_data);
  189. ti->private = fc;
  190. return 0;
  191. bad:
  192. kfree(fc);
  193. return r;
  194. }
  195. static void flakey_dtr(struct dm_target *ti)
  196. {
  197. struct flakey_c *fc = ti->private;
  198. dm_put_device(ti, fc->dev);
  199. kfree(fc);
  200. }
  201. static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
  202. {
  203. struct flakey_c *fc = ti->private;
  204. return fc->start + dm_target_offset(ti, bi_sector);
  205. }
  206. static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
  207. {
  208. struct flakey_c *fc = ti->private;
  209. bio->bi_bdev = fc->dev->bdev;
  210. if (bio_sectors(bio))
  211. bio->bi_iter.bi_sector =
  212. flakey_map_sector(ti, bio->bi_iter.bi_sector);
  213. }
  214. static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
  215. {
  216. unsigned bio_bytes = bio_cur_bytes(bio);
  217. char *data = bio_data(bio);
  218. /*
  219. * Overwrite the Nth byte of the data returned.
  220. */
  221. if (data && bio_bytes >= fc->corrupt_bio_byte) {
  222. data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value;
  223. DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
  224. "(rw=%c bi_rw=%lu bi_sector=%llu cur_bytes=%u)\n",
  225. bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
  226. (bio_data_dir(bio) == WRITE) ? 'w' : 'r', bio->bi_rw,
  227. (unsigned long long)bio->bi_iter.bi_sector, bio_bytes);
  228. }
  229. }
  230. static int flakey_map(struct dm_target *ti, struct bio *bio)
  231. {
  232. struct flakey_c *fc = ti->private;
  233. unsigned elapsed;
  234. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  235. pb->bio_submitted = false;
  236. /* Are we alive ? */
  237. elapsed = (jiffies - fc->start_time) / HZ;
  238. if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
  239. /*
  240. * Flag this bio as submitted while down.
  241. */
  242. pb->bio_submitted = true;
  243. /*
  244. * Error reads if neither corrupt_bio_byte or drop_writes are set.
  245. * Otherwise, flakey_end_io() will decide if the reads should be modified.
  246. */
  247. if (bio_data_dir(bio) == READ) {
  248. if (!fc->corrupt_bio_byte && !test_bit(DROP_WRITES, &fc->flags))
  249. return -EIO;
  250. goto map_bio;
  251. }
  252. /*
  253. * Drop writes?
  254. */
  255. if (test_bit(DROP_WRITES, &fc->flags)) {
  256. bio_endio(bio);
  257. return DM_MAPIO_SUBMITTED;
  258. }
  259. /*
  260. * Corrupt matching writes.
  261. */
  262. if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
  263. if (all_corrupt_bio_flags_match(bio, fc))
  264. corrupt_bio_data(bio, fc);
  265. goto map_bio;
  266. }
  267. /*
  268. * By default, error all I/O.
  269. */
  270. return -EIO;
  271. }
  272. map_bio:
  273. flakey_map_bio(ti, bio);
  274. return DM_MAPIO_REMAPPED;
  275. }
  276. static int flakey_end_io(struct dm_target *ti, struct bio *bio, int error)
  277. {
  278. struct flakey_c *fc = ti->private;
  279. struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
  280. if (!error && pb->bio_submitted && (bio_data_dir(bio) == READ)) {
  281. if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == READ) &&
  282. all_corrupt_bio_flags_match(bio, fc)) {
  283. /*
  284. * Corrupt successful matching READs while in down state.
  285. */
  286. corrupt_bio_data(bio, fc);
  287. } else if (!test_bit(DROP_WRITES, &fc->flags)) {
  288. /*
  289. * Error read during the down_interval if drop_writes
  290. * wasn't configured.
  291. */
  292. return -EIO;
  293. }
  294. }
  295. return error;
  296. }
  297. static void flakey_status(struct dm_target *ti, status_type_t type,
  298. unsigned status_flags, char *result, unsigned maxlen)
  299. {
  300. unsigned sz = 0;
  301. struct flakey_c *fc = ti->private;
  302. unsigned drop_writes;
  303. switch (type) {
  304. case STATUSTYPE_INFO:
  305. result[0] = '\0';
  306. break;
  307. case STATUSTYPE_TABLE:
  308. DMEMIT("%s %llu %u %u ", fc->dev->name,
  309. (unsigned long long)fc->start, fc->up_interval,
  310. fc->down_interval);
  311. drop_writes = test_bit(DROP_WRITES, &fc->flags);
  312. DMEMIT("%u ", drop_writes + (fc->corrupt_bio_byte > 0) * 5);
  313. if (drop_writes)
  314. DMEMIT("drop_writes ");
  315. if (fc->corrupt_bio_byte)
  316. DMEMIT("corrupt_bio_byte %u %c %u %u ",
  317. fc->corrupt_bio_byte,
  318. (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
  319. fc->corrupt_bio_value, fc->corrupt_bio_flags);
  320. break;
  321. }
  322. }
  323. static int flakey_prepare_ioctl(struct dm_target *ti,
  324. struct block_device **bdev, fmode_t *mode)
  325. {
  326. struct flakey_c *fc = ti->private;
  327. *bdev = fc->dev->bdev;
  328. /*
  329. * Only pass ioctls through if the device sizes match exactly.
  330. */
  331. if (fc->start ||
  332. ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
  333. return 1;
  334. return 0;
  335. }
  336. static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
  337. {
  338. struct flakey_c *fc = ti->private;
  339. return fn(ti, fc->dev, fc->start, ti->len, data);
  340. }
  341. static struct target_type flakey_target = {
  342. .name = "flakey",
  343. .version = {1, 3, 1},
  344. .module = THIS_MODULE,
  345. .ctr = flakey_ctr,
  346. .dtr = flakey_dtr,
  347. .map = flakey_map,
  348. .end_io = flakey_end_io,
  349. .status = flakey_status,
  350. .prepare_ioctl = flakey_prepare_ioctl,
  351. .iterate_devices = flakey_iterate_devices,
  352. };
  353. static int __init dm_flakey_init(void)
  354. {
  355. int r = dm_register_target(&flakey_target);
  356. if (r < 0)
  357. DMERR("register failed %d", r);
  358. return r;
  359. }
  360. static void __exit dm_flakey_exit(void)
  361. {
  362. dm_unregister_target(&flakey_target);
  363. }
  364. /* Module hooks */
  365. module_init(dm_flakey_init);
  366. module_exit(dm_flakey_exit);
  367. MODULE_DESCRIPTION(DM_NAME " flakey target");
  368. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  369. MODULE_LICENSE("GPL");