dm-delay.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. mempool_t *delayed_pool;
  24. struct dm_dev *dev_read;
  25. sector_t start_read;
  26. unsigned read_delay;
  27. unsigned reads;
  28. struct dm_dev *dev_write;
  29. sector_t start_write;
  30. unsigned write_delay;
  31. unsigned writes;
  32. };
  33. struct dm_delay_info {
  34. struct delay_c *context;
  35. struct list_head list;
  36. struct bio *bio;
  37. unsigned long expires;
  38. };
  39. static DEFINE_MUTEX(delayed_bios_lock);
  40. static struct kmem_cache *delayed_cache;
  41. static void handle_delayed_timer(unsigned long data)
  42. {
  43. struct delay_c *dc = (struct delay_c *)data;
  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. int 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. list_del(&delayed->list);
  73. bio_list_add(&flush_bios, delayed->bio);
  74. if ((bio_data_dir(delayed->bio) == WRITE))
  75. delayed->context->writes--;
  76. else
  77. delayed->context->reads--;
  78. mempool_free(delayed, dc->delayed_pool);
  79. continue;
  80. }
  81. if (!start_timer) {
  82. start_timer = 1;
  83. next_expires = delayed->expires;
  84. } else
  85. next_expires = min(next_expires, delayed->expires);
  86. }
  87. mutex_unlock(&delayed_bios_lock);
  88. if (start_timer)
  89. queue_timeout(dc, next_expires);
  90. return bio_list_get(&flush_bios);
  91. }
  92. static void flush_expired_bios(struct work_struct *work)
  93. {
  94. struct delay_c *dc;
  95. dc = container_of(work, struct delay_c, flush_expired_bios);
  96. flush_bios(flush_delayed_bios(dc, 0));
  97. }
  98. /*
  99. * Mapping parameters:
  100. * <device> <offset> <delay> [<write_device> <write_offset> <write_delay>]
  101. *
  102. * With separate write parameters, the first set is only used for reads.
  103. * Delays are specified in milliseconds.
  104. */
  105. static int delay_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  106. {
  107. struct delay_c *dc;
  108. unsigned long long tmpll;
  109. char dummy;
  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. if (sscanf(argv[1], "%llu%c", &tmpll, &dummy) != 1) {
  121. ti->error = "Invalid device sector";
  122. goto bad;
  123. }
  124. dc->start_read = tmpll;
  125. if (sscanf(argv[2], "%u%c", &dc->read_delay, &dummy) != 1) {
  126. ti->error = "Invalid delay";
  127. goto bad;
  128. }
  129. if (dm_get_device(ti, argv[0], dm_table_get_mode(ti->table),
  130. &dc->dev_read)) {
  131. ti->error = "Device lookup failed";
  132. goto bad;
  133. }
  134. dc->dev_write = NULL;
  135. if (argc == 3)
  136. goto out;
  137. if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
  138. ti->error = "Invalid write device sector";
  139. goto bad_dev_read;
  140. }
  141. dc->start_write = tmpll;
  142. if (sscanf(argv[5], "%u%c", &dc->write_delay, &dummy) != 1) {
  143. ti->error = "Invalid write delay";
  144. goto bad_dev_read;
  145. }
  146. if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table),
  147. &dc->dev_write)) {
  148. ti->error = "Write device lookup failed";
  149. goto bad_dev_read;
  150. }
  151. out:
  152. dc->delayed_pool = mempool_create_slab_pool(128, delayed_cache);
  153. if (!dc->delayed_pool) {
  154. DMERR("Couldn't create delayed bio pool.");
  155. goto bad_dev_write;
  156. }
  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_requests = 1;
  168. ti->num_discard_requests = 1;
  169. ti->private = dc;
  170. return 0;
  171. bad_queue:
  172. mempool_destroy(dc->delayed_pool);
  173. bad_dev_write:
  174. if (dc->dev_write)
  175. dm_put_device(ti, dc->dev_write);
  176. bad_dev_read:
  177. dm_put_device(ti, dc->dev_read);
  178. bad:
  179. kfree(dc);
  180. return -EINVAL;
  181. }
  182. static void delay_dtr(struct dm_target *ti)
  183. {
  184. struct delay_c *dc = ti->private;
  185. destroy_workqueue(dc->kdelayd_wq);
  186. dm_put_device(ti, dc->dev_read);
  187. if (dc->dev_write)
  188. dm_put_device(ti, dc->dev_write);
  189. mempool_destroy(dc->delayed_pool);
  190. kfree(dc);
  191. }
  192. static int delay_bio(struct delay_c *dc, int delay, struct bio *bio)
  193. {
  194. struct dm_delay_info *delayed;
  195. unsigned long expires = 0;
  196. if (!delay || !atomic_read(&dc->may_delay))
  197. return 1;
  198. delayed = mempool_alloc(dc->delayed_pool, GFP_NOIO);
  199. delayed->context = dc;
  200. delayed->bio = bio;
  201. delayed->expires = expires = jiffies + (delay * HZ / 1000);
  202. mutex_lock(&delayed_bios_lock);
  203. if (bio_data_dir(bio) == WRITE)
  204. dc->writes++;
  205. else
  206. dc->reads++;
  207. list_add_tail(&delayed->list, &dc->delayed_bios);
  208. mutex_unlock(&delayed_bios_lock);
  209. queue_timeout(dc, expires);
  210. return 0;
  211. }
  212. static void delay_presuspend(struct dm_target *ti)
  213. {
  214. struct delay_c *dc = ti->private;
  215. atomic_set(&dc->may_delay, 0);
  216. del_timer_sync(&dc->delay_timer);
  217. flush_bios(flush_delayed_bios(dc, 1));
  218. }
  219. static void delay_resume(struct dm_target *ti)
  220. {
  221. struct delay_c *dc = ti->private;
  222. atomic_set(&dc->may_delay, 1);
  223. }
  224. static int delay_map(struct dm_target *ti, struct bio *bio,
  225. union map_info *map_context)
  226. {
  227. struct delay_c *dc = ti->private;
  228. if ((bio_data_dir(bio) == WRITE) && (dc->dev_write)) {
  229. bio->bi_bdev = dc->dev_write->bdev;
  230. if (bio_sectors(bio))
  231. bio->bi_sector = dc->start_write +
  232. dm_target_offset(ti, bio->bi_sector);
  233. return delay_bio(dc, dc->write_delay, bio);
  234. }
  235. bio->bi_bdev = dc->dev_read->bdev;
  236. bio->bi_sector = dc->start_read + dm_target_offset(ti, bio->bi_sector);
  237. return delay_bio(dc, dc->read_delay, bio);
  238. }
  239. static void delay_status(struct dm_target *ti, status_type_t type,
  240. char *result, unsigned maxlen)
  241. {
  242. struct delay_c *dc = ti->private;
  243. int sz = 0;
  244. switch (type) {
  245. case STATUSTYPE_INFO:
  246. DMEMIT("%u %u", dc->reads, dc->writes);
  247. break;
  248. case STATUSTYPE_TABLE:
  249. DMEMIT("%s %llu %u", dc->dev_read->name,
  250. (unsigned long long) dc->start_read,
  251. dc->read_delay);
  252. if (dc->dev_write)
  253. DMEMIT(" %s %llu %u", dc->dev_write->name,
  254. (unsigned long long) dc->start_write,
  255. dc->write_delay);
  256. break;
  257. }
  258. }
  259. static int delay_iterate_devices(struct dm_target *ti,
  260. iterate_devices_callout_fn fn, void *data)
  261. {
  262. struct delay_c *dc = ti->private;
  263. int ret = 0;
  264. ret = fn(ti, dc->dev_read, dc->start_read, ti->len, data);
  265. if (ret)
  266. goto out;
  267. if (dc->dev_write)
  268. ret = fn(ti, dc->dev_write, dc->start_write, ti->len, data);
  269. out:
  270. return ret;
  271. }
  272. static struct target_type delay_target = {
  273. .name = "delay",
  274. .version = {1, 1, 0},
  275. .module = THIS_MODULE,
  276. .ctr = delay_ctr,
  277. .dtr = delay_dtr,
  278. .map = delay_map,
  279. .presuspend = delay_presuspend,
  280. .resume = delay_resume,
  281. .status = delay_status,
  282. .iterate_devices = delay_iterate_devices,
  283. };
  284. static int __init dm_delay_init(void)
  285. {
  286. int r = -ENOMEM;
  287. delayed_cache = KMEM_CACHE(dm_delay_info, 0);
  288. if (!delayed_cache) {
  289. DMERR("Couldn't create delayed bio cache.");
  290. goto bad_memcache;
  291. }
  292. r = dm_register_target(&delay_target);
  293. if (r < 0) {
  294. DMERR("register failed %d", r);
  295. goto bad_register;
  296. }
  297. return 0;
  298. bad_register:
  299. kmem_cache_destroy(delayed_cache);
  300. bad_memcache:
  301. return r;
  302. }
  303. static void __exit dm_delay_exit(void)
  304. {
  305. dm_unregister_target(&delay_target);
  306. kmem_cache_destroy(delayed_cache);
  307. }
  308. /* Module hooks */
  309. module_init(dm_delay_init);
  310. module_exit(dm_delay_exit);
  311. MODULE_DESCRIPTION(DM_NAME " delay target");
  312. MODULE_AUTHOR("Heinz Mauelshagen <mauelshagen@redhat.com>");
  313. MODULE_LICENSE("GPL");