dm-delay.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. * Offsets are specified in sectors.
  102. * Delays are specified in milliseconds.
  103. */
  104. static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  105. {
  106. struct delay_c *dc;
  107. unsigned long long tmpll;
  108. char dummy;
  109. int ret;
  110. if (argc != 3 && argc != 6) {
  111. ti->error = "Requires exactly 3 or 6 arguments";
  112. return -EINVAL;
  113. }
  114. dc = kmalloc(sizeof(*dc), GFP_KERNEL);
  115. if (!dc) {
  116. ti->error = "Cannot allocate context";
  117. return -ENOMEM;
  118. }
  119. dc->reads = dc->writes = 0;
  120. ret = -EINVAL;
  121. if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1) {
  122. ti->error = "Invalid device sector";
  123. goto bad;
  124. }
  125. dc->start_read = tmpll;
  126. if (sscanf(argv[2], "%u%c", &dc->read_delay, &dummy) != 1) {
  127. ti->error = "Invalid delay";
  128. goto bad;
  129. }
  130. ret = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
  131. &dc->dev_read);
  132. if (ret) {
  133. ti->error = "Device lookup failed";
  134. goto bad;
  135. }
  136. ret = -EINVAL;
  137. dc->dev_write = NULL;
  138. if (argc == 3)
  139. goto out;
  140. if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
  141. ti->error = "Invalid write device sector";
  142. goto bad_dev_read;
  143. }
  144. dc->start_write = tmpll;
  145. if (sscanf(argv[5], "%u%c", &dc->write_delay, &dummy) != 1) {
  146. ti->error = "Invalid write delay";
  147. goto bad_dev_read;
  148. }
  149. ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table),
  150. &dc->dev_write);
  151. if (ret) {
  152. ti->error = "Write device lookup failed";
  153. goto bad_dev_read;
  154. }
  155. out:
  156. ret = -EINVAL;
  157. dc->kdelayd_wq = alloc_workqueue("kdelayd", WQ_MEM_RECLAIM, 0);
  158. if (!dc->kdelayd_wq) {
  159. DMERR("Couldn't start kdelayd");
  160. goto bad_queue;
  161. }
  162. setup_timer(&dc->delay_timer, handle_delayed_timer, (unsigned long)dc);
  163. INIT_WORK(&dc->flush_expired_bios, flush_expired_bios);
  164. INIT_LIST_HEAD(&dc->delayed_bios);
  165. mutex_init(&dc->timer_lock);
  166. atomic_set(&dc->may_delay, 1);
  167. ti->num_flush_bios = 1;
  168. ti->num_discard_bios = 1;
  169. ti->per_io_data_size = sizeof(struct dm_delay_info);
  170. ti->private = dc;
  171. return 0;
  172. bad_queue:
  173. if (dc->dev_write)
  174. dm_put_device(ti, dc->dev_write);
  175. bad_dev_read:
  176. dm_put_device(ti, dc->dev_read);
  177. bad:
  178. kfree(dc);
  179. return ret;
  180. }
  181. static void delay_dtr(struct dm_target *ti)
  182. {
  183. struct delay_c *dc = ti->private;
  184. destroy_workqueue(dc->kdelayd_wq);
  185. dm_put_device(ti, dc->dev_read);
  186. if (dc->dev_write)
  187. dm_put_device(ti, dc->dev_write);
  188. kfree(dc);
  189. }
  190. static int delay_bio(struct delay_c *dc, int delay, struct bio *bio)
  191. {
  192. struct dm_delay_info *delayed;
  193. unsigned long expires = 0;
  194. if (!delay || !atomic_read(&dc->may_delay))
  195. return DM_MAPIO_REMAPPED;
  196. delayed = dm_per_bio_data(bio, sizeof(struct dm_delay_info));
  197. delayed->context = dc;
  198. delayed->expires = expires = jiffies + msecs_to_jiffies(delay);
  199. mutex_lock(&delayed_bios_lock);
  200. if (bio_data_dir(bio) == WRITE)
  201. dc->writes++;
  202. else
  203. dc->reads++;
  204. list_add_tail(&delayed->list, &dc->delayed_bios);
  205. mutex_unlock(&delayed_bios_lock);
  206. queue_timeout(dc, expires);
  207. return DM_MAPIO_SUBMITTED;
  208. }
  209. static void delay_presuspend(struct dm_target *ti)
  210. {
  211. struct delay_c *dc = ti->private;
  212. atomic_set(&dc->may_delay, 0);
  213. del_timer_sync(&dc->delay_timer);
  214. flush_bios(flush_delayed_bios(dc, 1));
  215. }
  216. static void delay_resume(struct dm_target *ti)
  217. {
  218. struct delay_c *dc = ti->private;
  219. atomic_set(&dc->may_delay, 1);
  220. }
  221. static int delay_map(struct dm_target *ti, struct bio *bio)
  222. {
  223. struct delay_c *dc = ti->private;
  224. if ((bio_data_dir(bio) == WRITE) && (dc->dev_write)) {
  225. bio->bi_bdev = dc->dev_write->bdev;
  226. if (bio_sectors(bio))
  227. bio->bi_iter.bi_sector = dc->start_write +
  228. dm_target_offset(ti, bio->bi_iter.bi_sector);
  229. return delay_bio(dc, dc->write_delay, bio);
  230. }
  231. bio->bi_bdev = dc->dev_read->bdev;
  232. bio->bi_iter.bi_sector = dc->start_read +
  233. dm_target_offset(ti, bio->bi_iter.bi_sector);
  234. return delay_bio(dc, dc->read_delay, bio);
  235. }
  236. static void delay_status(struct dm_target *ti, status_type_t type,
  237. unsigned status_flags, char *result, unsigned maxlen)
  238. {
  239. struct delay_c *dc = ti->private;
  240. int sz = 0;
  241. switch (type) {
  242. case STATUSTYPE_INFO:
  243. DMEMIT("%u %u", dc->reads, dc->writes);
  244. break;
  245. case STATUSTYPE_TABLE:
  246. DMEMIT("%s %llu %u", dc->dev_read->name,
  247. (unsigned long long) dc->start_read,
  248. dc->read_delay);
  249. if (dc->dev_write)
  250. DMEMIT(" %s %llu %u", dc->dev_write->name,
  251. (unsigned long long) dc->start_write,
  252. dc->write_delay);
  253. break;
  254. }
  255. }
  256. static int delay_iterate_devices(struct dm_target *ti,
  257. iterate_devices_callout_fn fn, void *data)
  258. {
  259. struct delay_c *dc = ti->private;
  260. int ret = 0;
  261. ret = fn(ti, dc->dev_read, dc->start_read, ti->len, data);
  262. if (ret)
  263. goto out;
  264. if (dc->dev_write)
  265. ret = fn(ti, dc->dev_write, dc->start_write, ti->len, data);
  266. out:
  267. return ret;
  268. }
  269. static struct target_type delay_target = {
  270. .name = "delay",
  271. .version = {1, 2, 1},
  272. .module = THIS_MODULE,
  273. .ctr = delay_ctr,
  274. .dtr = delay_dtr,
  275. .map = delay_map,
  276. .presuspend = delay_presuspend,
  277. .resume = delay_resume,
  278. .status = delay_status,
  279. .iterate_devices = delay_iterate_devices,
  280. };
  281. static int __init dm_delay_init(void)
  282. {
  283. int r;
  284. r = dm_register_target(&delay_target);
  285. if (r < 0) {
  286. DMERR("register failed %d", r);
  287. goto bad_register;
  288. }
  289. return 0;
  290. bad_register:
  291. return r;
  292. }
  293. static void __exit dm_delay_exit(void)
  294. {
  295. dm_unregister_target(&delay_target);
  296. }
  297. /* Module hooks */
  298. module_init(dm_delay_init);
  299. module_exit(dm_delay_exit);
  300. MODULE_DESCRIPTION(DM_NAME " delay target");
  301. MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
  302. MODULE_LICENSE("GPL");