dm-delay.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (C) 2005-2007 Red Hat GmbH
  3. *
  4. * A target that delays reads and/or writes and can send
  5. * them to different devices.
  6. *
  7. * This file is released under the GPL.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/bio.h>
  13. #include <linux/slab.h>
  14. #include <linux/device-mapper.h>
  15. #define DM_MSG_PREFIX "delay"
  16. struct delay_class {
  17. struct dm_dev *dev;
  18. sector_t start;
  19. unsigned delay;
  20. unsigned ops;
  21. };
  22. struct delay_c {
  23. struct timer_list delay_timer;
  24. struct mutex timer_lock;
  25. struct workqueue_struct *kdelayd_wq;
  26. struct work_struct flush_expired_bios;
  27. struct list_head delayed_bios;
  28. atomic_t may_delay;
  29. struct delay_class read;
  30. struct delay_class write;
  31. struct delay_class flush;
  32. int argc;
  33. };
  34. struct dm_delay_info {
  35. struct delay_c *context;
  36. struct delay_class *class;
  37. struct list_head list;
  38. unsigned long expires;
  39. };
  40. static DEFINE_MUTEX(delayed_bios_lock);
  41. static void handle_delayed_timer(struct timer_list *t)
  42. {
  43. struct delay_c *dc = from_timer(dc, t, delay_timer);
  44. queue_work(dc->kdelayd_wq, &dc->flush_expired_bios);
  45. }
  46. static void queue_timeout(struct delay_c *dc, unsigned long expires)
  47. {
  48. mutex_lock(&dc->timer_lock);
  49. if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
  50. mod_timer(&dc->delay_timer, expires);
  51. mutex_unlock(&dc->timer_lock);
  52. }
  53. static void flush_bios(struct bio *bio)
  54. {
  55. struct bio *n;
  56. while (bio) {
  57. n = bio->bi_next;
  58. bio->bi_next = NULL;
  59. generic_make_request(bio);
  60. bio = n;
  61. }
  62. }
  63. static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all)
  64. {
  65. struct dm_delay_info *delayed, *next;
  66. unsigned long next_expires = 0;
  67. unsigned long start_timer = 0;
  68. struct bio_list flush_bios = { };
  69. mutex_lock(&delayed_bios_lock);
  70. list_for_each_entry_safe(delayed, next, &dc->delayed_bios, list) {
  71. if (flush_all || time_after_eq(jiffies, delayed->expires)) {
  72. struct bio *bio = dm_bio_from_per_bio_data(delayed,
  73. sizeof(struct dm_delay_info));
  74. list_del(&delayed->list);
  75. bio_list_add(&flush_bios, bio);
  76. delayed->class->ops--;
  77. continue;
  78. }
  79. if (!start_timer) {
  80. start_timer = 1;
  81. next_expires = delayed->expires;
  82. } else
  83. next_expires = min(next_expires, delayed->expires);
  84. }
  85. mutex_unlock(&delayed_bios_lock);
  86. if (start_timer)
  87. queue_timeout(dc, next_expires);
  88. return bio_list_get(&flush_bios);
  89. }
  90. static void flush_expired_bios(struct work_struct *work)
  91. {
  92. struct delay_c *dc;
  93. dc = container_of(work, struct delay_c, flush_expired_bios);
  94. flush_bios(flush_delayed_bios(dc, 0));
  95. }
  96. static void delay_dtr(struct dm_target *ti)
  97. {
  98. struct delay_c *dc = ti->private;
  99. if (dc->kdelayd_wq)
  100. destroy_workqueue(dc->kdelayd_wq);
  101. if (dc->read.dev)
  102. dm_put_device(ti, dc->read.dev);
  103. if (dc->write.dev)
  104. dm_put_device(ti, dc->write.dev);
  105. if (dc->flush.dev)
  106. dm_put_device(ti, dc->flush.dev);
  107. mutex_destroy(&dc->timer_lock);
  108. kfree(dc);
  109. }
  110. static int delay_class_ctr(struct dm_target *ti, struct delay_class *c, char **argv)
  111. {
  112. int ret;
  113. unsigned long long tmpll;
  114. char dummy;
  115. if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
  116. ti->error = "Invalid device sector";
  117. return -EINVAL;
  118. }
  119. c->start = tmpll;
  120. if (sscanf(argv[2], "%u%c", &c->delay, &dummy) != 1) {
  121. ti->error = "Invalid delay";
  122. return -EINVAL;
  123. }
  124. ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &c->dev);
  125. if (ret) {
  126. ti->error = "Device lookup failed";
  127. return ret;
  128. }
  129. return 0;
  130. }
  131. /*
  132. * Mapping parameters:
  133. * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
  134. *
  135. * With separate write parameters, the first set is only used for reads.
  136. * Offsets are specified in sectors.
  137. * Delays are specified in milliseconds.
  138. */
  139. static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  140. {
  141. struct delay_c *dc;
  142. int ret;
  143. if (argc != 3 && argc != 6 && argc != 9) {
  144. ti->error = "Requires exactly 3, 6 or 9 arguments";
  145. return -EINVAL;
  146. }
  147. dc = kzalloc(sizeof(*dc), GFP_KERNEL);
  148. if (!dc) {
  149. ti->error = "Cannot allocate context";
  150. return -ENOMEM;
  151. }
  152. ti->private = dc;
  153. timer_setup(&dc->delay_timer, handle_delayed_timer, 0);
  154. INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
  155. INIT_LIST_HEAD(&dc->delayed_bios);
  156. mutex_init(&dc->timer_lock);
  157. atomic_set(&dc->may_delay, 1);
  158. dc->argc = argc;
  159. ret = delay_class_ctr(ti, &dc->read, argv);
  160. if (ret)
  161. goto bad;
  162. if (argc == 3) {
  163. ret = delay_class_ctr(ti, &dc->write, argv);
  164. if (ret)
  165. goto bad;
  166. ret = delay_class_ctr(ti, &dc->flush, argv);
  167. if (ret)
  168. goto bad;
  169. goto out;
  170. }
  171. ret = delay_class_ctr(ti, &dc->write, argv + 3);
  172. if (ret)
  173. goto bad;
  174. if (argc == 6) {
  175. ret = delay_class_ctr(ti, &dc->flush, argv + 3);
  176. if (ret)
  177. goto bad;
  178. goto out;
  179. }
  180. ret = delay_class_ctr(ti, &dc->flush, argv + 6);
  181. if (ret)
  182. goto bad;
  183. out:
  184. dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
  185. if (!dc->kdelayd_wq) {
  186. ret = -EINVAL;
  187. DMERR("Couldn't start kdelayd");
  188. goto bad;
  189. }
  190. ti->num_flush_bios = 1;
  191. ti->num_discard_bios = 1;
  192. ti->per_io_data_size = sizeof(struct dm_delay_info);
  193. return 0;
  194. bad:
  195. delay_dtr(ti);
  196. return ret;
  197. }
  198. static int delay_bio(struct delay_c *dc, struct delay_class *c, struct bio *bio)
  199. {
  200. struct dm_delay_info *delayed;
  201. unsigned long expires = 0;
  202. if (!c->delay || !atomic_read(&dc->may_delay))
  203. return DM_MAPIO_REMAPPED;
  204. delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
  205. delayed->context = dc;
  206. delayed->expires = expires = jiffies + msecs_to_jiffies(c->delay);
  207. mutex_lock(&delayed_bios_lock);
  208. c->ops++;
  209. list_add_tail(&delayed->list, &dc->delayed_bios);
  210. mutex_unlock(&delayed_bios_lock);
  211. queue_timeout(dc, expires);
  212. return DM_MAPIO_SUBMITTED;
  213. }
  214. static void delay_presuspend(struct dm_target *ti)
  215. {
  216. struct delay_c *dc = ti->private;
  217. atomic_set(&dc->may_delay, 0);
  218. del_timer_sync(&dc->delay_timer);
  219. flush_bios(flush_delayed_bios(dc, 1));
  220. }
  221. static void delay_resume(struct dm_target *ti)
  222. {
  223. struct delay_c *dc = ti->private;
  224. atomic_set(&dc->may_delay, 1);
  225. }
  226. static int delay_map(struct dm_target *ti, struct bio *bio)
  227. {
  228. struct delay_c *dc = ti->private;
  229. struct delay_class *c;
  230. struct dm_delay_info *delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
  231. if (bio_data_dir(bio) == WRITE) {
  232. if (unlikely(bio->bi_opf & REQ_PREFLUSH))
  233. c = &dc->flush;
  234. else
  235. c = &dc->write;
  236. } else {
  237. c = &dc->read;
  238. }
  239. delayed->class = c;
  240. bio_set_dev(bio, c->dev->bdev);
  241. if (bio_sectors(bio))
  242. bio->bi_iter.bi_sector = c->start + dm_target_offset(ti, bio->bi_iter.bi_sector);
  243. return delay_bio(dc, c, bio);
  244. }
  245. #define DMEMIT_DELAY_CLASS(c) \
  246. DMEMIT("%s %llu %u", (c)->dev->name, (unsigned long long)(c)->start, (c)->delay)
  247. static void delay_status(struct dm_target *ti, status_type_t type,
  248. unsigned status_flags, char *result, unsigned maxlen)
  249. {
  250. struct delay_c *dc = ti->private;
  251. int sz = 0;
  252. switch (type) {
  253. case STATUSTYPE_INFO:
  254. DMEMIT("%u %u %u", dc->read.ops, dc->write.ops, dc->flush.ops);
  255. break;
  256. case STATUSTYPE_TABLE:
  257. DMEMIT_DELAY_CLASS(&dc->read);
  258. if (dc->argc >= 6) {
  259. DMEMIT(" ");
  260. DMEMIT_DELAY_CLASS(&dc->write);
  261. }
  262. if (dc->argc >= 9) {
  263. DMEMIT(" ");
  264. DMEMIT_DELAY_CLASS(&dc->flush);
  265. }
  266. break;
  267. }
  268. }
  269. static int delay_iterate_devices(struct dm_target *ti,
  270. iterate_devices_callout_fn fn, void *data)
  271. {
  272. struct delay_c *dc = ti->private;
  273. int ret = 0;
  274. ret = fn(ti, dc->read.dev, dc->read.start, ti->len, data);
  275. if (ret)
  276. goto out;
  277. ret = fn(ti, dc->write.dev, dc->write.start, ti->len, data);
  278. if (ret)
  279. goto out;
  280. ret = fn(ti, dc->flush.dev, dc->flush.start, ti->len, data);
  281. if (ret)
  282. goto out;
  283. out:
  284. return ret;
  285. }
  286. static struct target_type delay_target = {
  287. .name = "delay",
  288. .version = {1, 2, 1},
  289. .features = DM_TARGET_PASSES_INTEGRITY,
  290. .module = THIS_MODULE,
  291. .ctr = delay_ctr,
  292. .dtr = delay_dtr,
  293. .map = delay_map,
  294. .presuspend = delay_presuspend,
  295. .resume = delay_resume,
  296. .status = delay_status,
  297. .iterate_devices = delay_iterate_devices,
  298. };
  299. static int __init dm_delay_init(void)
  300. {
  301. int r;
  302. r = dm_register_target(&delay_target);
  303. if (r < 0) {
  304. DMERR("register failed %d", r);
  305. goto bad_register;
  306. }
  307. return 0;
  308. bad_register:
  309. return r;
  310. }
  311. static void __exit dm_delay_exit(void)
  312. {
  313. dm_unregister_target(&delay_target);
  314. }
  315. /* Module hooks */
  316. module_init(dm_delay_init);
  317. module_exit(dm_delay_exit);
  318. MODULE_DESCRIPTION(DM_NAME " delay target");
  319. MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
  320. MODULE_LICENSE("GPL");