dm-flakey.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. as.argc = argc;
  140. as.argv = argv;
  141. if (argc < 4) {
  142. ti->error = "Invalid argument count";
  143. return -EINVAL;
  144. }
  145. fc = kzalloc(sizeof(*fc), GFP_KERNEL);
  146. if (!fc) {
  147. ti->error = "Cannot allocate linear context";
  148. return -ENOMEM;
  149. }
  150. fc->start_time = jiffies;
  151. devname = dm_shift_arg(&as);
  152. if (sscanf(dm_shift_arg(&as), "%llu", &tmpll) != 1) {
  153. ti->error = "Invalid device sector";
  154. goto bad;
  155. }
  156. fc->start = tmpll;
  157. r = dm_read_arg(_args, &as, &fc->up_interval, &ti->error);
  158. if (r)
  159. goto bad;
  160. r = dm_read_arg(_args, &as, &fc->down_interval, &ti->error);
  161. if (r)
  162. goto bad;
  163. if (!(fc->up_interval + fc->down_interval)) {
  164. ti->error = "Total (up + down) interval is zero";
  165. goto bad;
  166. }
  167. if (fc->up_interval + fc->down_interval < fc->up_interval) {
  168. ti->error = "Interval overflow";
  169. goto bad;
  170. }
  171. r = parse_features(&as, fc, ti);
  172. if (r)
  173. goto bad;
  174. if (dm_get_device(ti, devname, dm_table_get_mode(ti->table), &fc->dev)) {
  175. ti->error = "Device lookup failed";
  176. goto bad;
  177. }
  178. ti->num_flush_requests = 1;
  179. ti->num_discard_requests = 1;
  180. ti->private = fc;
  181. return 0;
  182. bad:
  183. kfree(fc);
  184. return -EINVAL;
  185. }
  186. static void flakey_dtr(struct dm_target *ti)
  187. {
  188. struct flakey_c *fc = ti->private;
  189. dm_put_device(ti, fc->dev);
  190. kfree(fc);
  191. }
  192. static sector_t flakey_map_sector(struct dm_target *ti, sector_t bi_sector)
  193. {
  194. struct flakey_c *fc = ti->private;
  195. return fc->start + dm_target_offset(ti, bi_sector);
  196. }
  197. static void flakey_map_bio(struct dm_target *ti, struct bio *bio)
  198. {
  199. struct flakey_c *fc = ti->private;
  200. bio->bi_bdev = fc->dev->bdev;
  201. if (bio_sectors(bio))
  202. bio->bi_sector = flakey_map_sector(ti, bio->bi_sector);
  203. }
  204. static void corrupt_bio_data(struct bio *bio, struct flakey_c *fc)
  205. {
  206. unsigned bio_bytes = bio_cur_bytes(bio);
  207. char *data = bio_data(bio);
  208. /*
  209. * Overwrite the Nth byte of the data returned.
  210. */
  211. if (data && bio_bytes >= fc->corrupt_bio_byte) {
  212. data[fc->corrupt_bio_byte - 1] = fc->corrupt_bio_value;
  213. DMDEBUG("Corrupting data bio=%p by writing %u to byte %u "
  214. "(rw=%c bi_rw=%lu bi_sector=%llu cur_bytes=%u)\n",
  215. bio, fc->corrupt_bio_value, fc->corrupt_bio_byte,
  216. (bio_data_dir(bio) == WRITE) ? 'w' : 'r',
  217. bio->bi_rw, (unsigned long long)bio->bi_sector, bio_bytes);
  218. }
  219. }
  220. static int flakey_map(struct dm_target *ti, struct bio *bio,
  221. union map_info *map_context)
  222. {
  223. struct flakey_c *fc = ti->private;
  224. unsigned elapsed;
  225. /* Are we alive ? */
  226. elapsed = (jiffies - fc->start_time) / HZ;
  227. if (elapsed % (fc->up_interval + fc->down_interval) >= fc->up_interval) {
  228. /*
  229. * Flag this bio as submitted while down.
  230. */
  231. map_context->ll = 1;
  232. /*
  233. * Map reads as normal.
  234. */
  235. if (bio_data_dir(bio) == READ)
  236. goto map_bio;
  237. /*
  238. * Drop writes?
  239. */
  240. if (test_bit(DROP_WRITES, &fc->flags)) {
  241. bio_endio(bio, 0);
  242. return DM_MAPIO_SUBMITTED;
  243. }
  244. /*
  245. * Corrupt matching writes.
  246. */
  247. if (fc->corrupt_bio_byte && (fc->corrupt_bio_rw == WRITE)) {
  248. if (all_corrupt_bio_flags_match(bio, fc))
  249. corrupt_bio_data(bio, fc);
  250. goto map_bio;
  251. }
  252. /*
  253. * By default, error all I/O.
  254. */
  255. return -EIO;
  256. }
  257. map_bio:
  258. flakey_map_bio(ti, bio);
  259. return DM_MAPIO_REMAPPED;
  260. }
  261. static int flakey_end_io(struct dm_target *ti, struct bio *bio,
  262. int error, union map_info *map_context)
  263. {
  264. struct flakey_c *fc = ti->private;
  265. unsigned bio_submitted_while_down = map_context->ll;
  266. /*
  267. * Corrupt successful READs while in down state.
  268. * If flags were specified, only corrupt those that match.
  269. */
  270. if (!error && bio_submitted_while_down &&
  271. (bio_data_dir(bio) == READ) && (fc->corrupt_bio_rw == READ) &&
  272. all_corrupt_bio_flags_match(bio, fc))
  273. corrupt_bio_data(bio, fc);
  274. return error;
  275. }
  276. static int flakey_status(struct dm_target *ti, status_type_t type,
  277. char *result, unsigned int maxlen)
  278. {
  279. unsigned sz = 0;
  280. struct flakey_c *fc = ti->private;
  281. unsigned drop_writes;
  282. switch (type) {
  283. case STATUSTYPE_INFO:
  284. result[0] = '\0';
  285. break;
  286. case STATUSTYPE_TABLE:
  287. DMEMIT("%s %llu %u %u ", fc->dev->name,
  288. (unsigned long long)fc->start, fc->up_interval,
  289. fc->down_interval);
  290. drop_writes = test_bit(DROP_WRITES, &fc->flags);
  291. DMEMIT("%u ", drop_writes + (fc->corrupt_bio_byte > 0) * 5);
  292. if (drop_writes)
  293. DMEMIT("drop_writes ");
  294. if (fc->corrupt_bio_byte)
  295. DMEMIT("corrupt_bio_byte %u %c %u %u ",
  296. fc->corrupt_bio_byte,
  297. (fc->corrupt_bio_rw == WRITE) ? 'w' : 'r',
  298. fc->corrupt_bio_value, fc->corrupt_bio_flags);
  299. break;
  300. }
  301. return 0;
  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. return __blkdev_driver_ioctl(fc->dev->bdev, fc->dev->mode, cmd, arg);
  307. }
  308. static int flakey_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  309. struct bio_vec *biovec, int max_size)
  310. {
  311. struct flakey_c *fc = ti->private;
  312. struct request_queue *q = bdev_get_queue(fc->dev->bdev);
  313. if (!q->merge_bvec_fn)
  314. return max_size;
  315. bvm->bi_bdev = fc->dev->bdev;
  316. bvm->bi_sector = flakey_map_sector(ti, bvm->bi_sector);
  317. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  318. }
  319. static int flakey_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
  320. {
  321. struct flakey_c *fc = ti->private;
  322. return fn(ti, fc->dev, fc->start, ti->len, data);
  323. }
  324. static struct target_type flakey_target = {
  325. .name = "flakey",
  326. .version = {1, 2, 0},
  327. .module = THIS_MODULE,
  328. .ctr = flakey_ctr,
  329. .dtr = flakey_dtr,
  330. .map = flakey_map,
  331. .end_io = flakey_end_io,
  332. .status = flakey_status,
  333. .ioctl = flakey_ioctl,
  334. .merge = flakey_merge,
  335. .iterate_devices = flakey_iterate_devices,
  336. };
  337. static int __init dm_flakey_init(void)
  338. {
  339. int r = dm_register_target(&flakey_target);
  340. if (r < 0)
  341. DMERR("register failed %d", r);
  342. return r;
  343. }
  344. static void __exit dm_flakey_exit(void)
  345. {
  346. dm_unregister_target(&flakey_target);
  347. }
  348. /* Module hooks */
  349. module_init(dm_flakey_init);
  350. module_exit(dm_flakey_exit);
  351. MODULE_DESCRIPTION(DM_NAME " flakey target");
  352. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  353. MODULE_LICENSE("GPL");