dm-delay.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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_c {
  17. struct timer_list delay_timer;
  18. struct mutex timer_lock;
  19. struct workqueue_struct *kdelayd_wq;
  20. struct work_struct flush_expired_bios;
  21. struct list_head delayed_bios;
  22. atomic_t may_delay;
  23. struct dm_dev *dev_read;
  24. sector_t start_read;
  25. unsigned read_delay;
  26. unsigned reads;
  27. struct dm_dev *dev_write;
  28. sector_t start_write;
  29. unsigned write_delay;
  30. unsigned writes;
  31. };
  32. struct dm_delay_info {
  33. struct delay_c *context;
  34. struct list_head list;
  35. unsigned long expires;
  36. };
  37. static DEFINE_MUTEX(delayed_bios_lock);
  38. static void handle_delayed_timer(unsigned long data)
  39. {
  40. struct delay_c *dc = (struct delay_c *)data;
  41. queue_work(dc->kdelayd_wq, &dc->flush_expired_bios);
  42. }
  43. static void queue_timeout(struct delay_c *dc, unsigned long expires)
  44. {
  45. mutex_lock(&dc->timer_lock);
  46. if (!timer_pending(&dc->delay_timer) || expires < dc->delay_timer.expires)
  47. mod_timer(&dc->delay_timer, expires);
  48. mutex_unlock(&dc->timer_lock);
  49. }
  50. static void flush_bios(struct bio *bio)
  51. {
  52. struct bio *n;
  53. while (bio) {
  54. n = bio->bi_next;
  55. bio->bi_next = NULL;
  56. generic_make_request(bio);
  57. bio = n;
  58. }
  59. }
  60. static struct bio *flush_delayed_bios(struct delay_c *dc, int flush_all)
  61. {
  62. struct dm_delay_info *delayed, *next;
  63. unsigned long next_expires = 0;
  64. int start_timer = 0;
  65. struct bio_list flush_bios = { };
  66. mutex_lock(&delayed_bios_lock);
  67. list_for_each_entry_safe(delayed, next, &dc->delayed_bios, list) {
  68. if (flush_all || time_after_eq(jiffies, delayed->expires)) {
  69. struct bio *bio = dm_bio_from_per_bio_data(delayed,
  70. sizeof(struct dm_delay_info));
  71. list_del(&delayed->list);
  72. bio_list_add(&flush_bios, bio);
  73. if ((bio_data_dir(bio) == WRITE))
  74. delayed->context->writes--;
  75. else
  76. delayed->context->reads--;
  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. /*
  97. * Mapping parameters:
  98. * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
  99. *
  100. * With separate write parameters, the first set is only used for reads.
  101. * Delays are specified in milliseconds.
  102. */
  103. static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  104. {
  105. struct delay_c *dc;
  106. unsigned long long tmpll;
  107. char dummy;
  108. if (argc != 3 && argc != 6) {
  109. ti->error = "requires exactly 3 or 6 arguments";
  110. return -EINVAL;
  111. }
  112. dc = kmalloc(sizeof(*dc), GFP_KERNEL);
  113. if (!dc) {
  114. ti->error = "Cannot allocate context";
  115. return -ENOMEM;
  116. }
  117. dc->reads = dc->writes = 0;
  118. if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1) {
  119. ti->error = "Invalid device sector";
  120. goto bad;
  121. }
  122. dc->start_read = tmpll;
  123. if (sscanf(argv[2], "%u%c", &dc->read_delay, &dummy) != 1) {
  124. ti->error = "Invalid delay";
  125. goto bad;
  126. }
  127. if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
  128. &dc->dev_read)) {
  129. ti->error = "Device lookup failed";
  130. goto bad;
  131. }
  132. dc->dev_write = NULL;
  133. if (argc == 3)
  134. goto out;
  135. if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
  136. ti->error = "Invalid write device sector";
  137. goto bad_dev_read;
  138. }
  139. dc->start_write = tmpll;
  140. if (sscanf(argv[5], "%u%c", &dc->write_delay, &dummy) != 1) {
  141. ti->error = "Invalid write delay";
  142. goto bad_dev_read;
  143. }
  144. if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table),
  145. &dc->dev_write)) {
  146. ti->error = "Write device lookup failed";
  147. goto bad_dev_read;
  148. }
  149. out:
  150. dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
  151. if (!dc->kdelayd_wq) {
  152. DMERR("Couldn't start kdelayd");
  153. goto bad_queue;
  154. }
  155. setup_timer(&dc->delay_timer, handle_delayed_timer, (unsigned long)dc);
  156. INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
  157. INIT_LIST_HEAD(&dc->delayed_bios);
  158. mutex_init(&dc->timer_lock);
  159. atomic_set(&dc->may_delay, 1);
  160. ti->num_flush_bios = 1;
  161. ti->num_discard_bios = 1;
  162. ti->per_bio_data_size = sizeof(struct dm_delay_info);
  163. ti->private = dc;
  164. return 0;
  165. bad_queue:
  166. if (dc->dev_write)
  167. dm_put_device(ti, dc->dev_write);
  168. bad_dev_read:
  169. dm_put_device(ti, dc->dev_read);
  170. bad:
  171. kfree(dc);
  172. return -EINVAL;
  173. }
  174. static void delay_dtr(struct dm_target *ti)
  175. {
  176. struct delay_c *dc = ti->private;
  177. destroy_workqueue(dc->kdelayd_wq);
  178. dm_put_device(ti, dc->dev_read);
  179. if (dc->dev_write)
  180. dm_put_device(ti, dc->dev_write);
  181. kfree(dc);
  182. }
  183. static int delay_bio(struct delay_c *dc, int delay, struct bio *bio)
  184. {
  185. struct dm_delay_info *delayed;
  186. unsigned long expires = 0;
  187. if (!delay || !atomic_read(&dc->may_delay))
  188. return 1;
  189. delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
  190. delayed->context = dc;
  191. delayed->expires = expires = jiffies + msecs_to_jiffies(delay);
  192. mutex_lock(&delayed_bios_lock);
  193. if (bio_data_dir(bio) == WRITE)
  194. dc->writes++;
  195. else
  196. dc->reads++;
  197. list_add_tail(&delayed->list, &dc->delayed_bios);
  198. mutex_unlock(&delayed_bios_lock);
  199. queue_timeout(dc, expires);
  200. return 0;
  201. }
  202. static void delay_presuspend(struct dm_target *ti)
  203. {
  204. struct delay_c *dc = ti->private;
  205. atomic_set(&dc->may_delay, 0);
  206. del_timer_sync(&dc->delay_timer);
  207. flush_bios(flush_delayed_bios(dc, 1));
  208. }
  209. static void delay_resume(struct dm_target *ti)
  210. {
  211. struct delay_c *dc = ti->private;
  212. atomic_set(&dc->may_delay, 1);
  213. }
  214. static int delay_map(struct dm_target *ti, struct bio *bio)
  215. {
  216. struct delay_c *dc = ti->private;
  217. if ((bio_data_dir(bio) == WRITE) && (dc->dev_write)) {
  218. bio->bi_bdev = dc->dev_write->bdev;
  219. if (bio_sectors(bio))
  220. bio->bi_iter.bi_sector = dc->start_write +
  221. dm_target_offset(ti, bio->bi_iter.bi_sector);
  222. return delay_bio(dc, dc->write_delay, bio);
  223. }
  224. bio->bi_bdev = dc->dev_read->bdev;
  225. bio->bi_iter.bi_sector = dc->start_read +
  226. dm_target_offset(ti, bio->bi_iter.bi_sector);
  227. return delay_bio(dc, dc->read_delay, bio);
  228. }
  229. static void delay_status(struct dm_target *ti, status_type_t type,
  230. unsigned status_flags, char *result, unsigned maxlen)
  231. {
  232. struct delay_c *dc = ti->private;
  233. int sz = 0;
  234. switch (type) {
  235. case STATUSTYPE_INFO:
  236. DMEMIT("%u %u", dc->reads, dc->writes);
  237. break;
  238. case STATUSTYPE_TABLE:
  239. DMEMIT("%s %llu %u", dc->dev_read->name,
  240. (unsigned long long) dc->start_read,
  241. dc->read_delay);
  242. if (dc->dev_write)
  243. DMEMIT(" %s %llu %u", dc->dev_write->name,
  244. (unsigned long long) dc->start_write,
  245. dc->write_delay);
  246. break;
  247. }
  248. }
  249. static int delay_iterate_devices(struct dm_target *ti,
  250. iterate_devices_callout_fn fn, void *data)
  251. {
  252. struct delay_c *dc = ti->private;
  253. int ret = 0;
  254. ret = fn(ti, dc->dev_read, dc->start_read, ti->len, data);
  255. if (ret)
  256. goto out;
  257. if (dc->dev_write)
  258. ret = fn(ti, dc->dev_write, dc->start_write, ti->len, data);
  259. out:
  260. return ret;
  261. }
  262. static struct target_type delay_target = {
  263. .name = "delay",
  264. .version = {1, 2, 1},
  265. .module = THIS_MODULE,
  266. .ctr = delay_ctr,
  267. .dtr = delay_dtr,
  268. .map = delay_map,
  269. .presuspend = delay_presuspend,
  270. .resume = delay_resume,
  271. .status = delay_status,
  272. .iterate_devices = delay_iterate_devices,
  273. };
  274. static int __init dm_delay_init(void)
  275. {
  276. int r;
  277. r = dm_register_target(&delay_target);
  278. if (r < 0) {
  279. DMERR("register failed %d", r);
  280. goto bad_register;
  281. }
  282. return 0;
  283. bad_register:
  284. return r;
  285. }
  286. static void __exit dm_delay_exit(void)
  287. {
  288. dm_unregister_target(&delay_target);
  289. }
  290. /* Module hooks */
  291. module_init(dm_delay_init);
  292. module_exit(dm_delay_exit);
  293. MODULE_DESCRIPTION(DM_NAME " delay target");
  294. MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
  295. MODULE_LICENSE("GPL");