dm-flakey.c 9.5 KB

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