dm-io.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright (C) 2003 Sistina Software
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-io.h"
  7. #include <linux/bio.h>
  8. #include <linux/mempool.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. static struct bio_set *_bios;
  13. /* FIXME: can we shrink this ? */
  14. struct io {
  15. unsigned long error;
  16. atomic_t count;
  17. struct task_struct *sleeper;
  18. io_notify_fn callback;
  19. void *context;
  20. };
  21. /*
  22. * io contexts are only dynamically allocated for asynchronous
  23. * io. Since async io is likely to be the majority of io we'll
  24. * have the same number of io contexts as buffer heads ! (FIXME:
  25. * must reduce this).
  26. */
  27. static unsigned _num_ios;
  28. static mempool_t *_io_pool;
  29. static void *alloc_io(unsigned int __nocast gfp_mask, void *pool_data)
  30. {
  31. return kmalloc(sizeof(struct io), gfp_mask);
  32. }
  33. static void free_io(void *element, void *pool_data)
  34. {
  35. kfree(element);
  36. }
  37. static unsigned int pages_to_ios(unsigned int pages)
  38. {
  39. return 4 * pages; /* too many ? */
  40. }
  41. static int resize_pool(unsigned int new_ios)
  42. {
  43. int r = 0;
  44. if (_io_pool) {
  45. if (new_ios == 0) {
  46. /* free off the pool */
  47. mempool_destroy(_io_pool);
  48. _io_pool = NULL;
  49. bioset_free(_bios);
  50. } else {
  51. /* resize the pool */
  52. r = mempool_resize(_io_pool, new_ios, GFP_KERNEL);
  53. }
  54. } else {
  55. /* create new pool */
  56. _io_pool = mempool_create(new_ios, alloc_io, free_io, NULL);
  57. if (!_io_pool)
  58. return -ENOMEM;
  59. _bios = bioset_create(16, 16, 4);
  60. if (!_bios) {
  61. mempool_destroy(_io_pool);
  62. _io_pool = NULL;
  63. return -ENOMEM;
  64. }
  65. }
  66. if (!r)
  67. _num_ios = new_ios;
  68. return r;
  69. }
  70. int dm_io_get(unsigned int num_pages)
  71. {
  72. return resize_pool(_num_ios + pages_to_ios(num_pages));
  73. }
  74. void dm_io_put(unsigned int num_pages)
  75. {
  76. resize_pool(_num_ios - pages_to_ios(num_pages));
  77. }
  78. /*-----------------------------------------------------------------
  79. * We need to keep track of which region a bio is doing io for.
  80. * In order to save a memory allocation we store this the last
  81. * bvec which we know is unused (blech).
  82. * XXX This is ugly and can OOPS with some configs... find another way.
  83. *---------------------------------------------------------------*/
  84. static inline void bio_set_region(struct bio *bio, unsigned region)
  85. {
  86. bio->bi_io_vec[bio->bi_max_vecs - 1].bv_len = region;
  87. }
  88. static inline unsigned bio_get_region(struct bio *bio)
  89. {
  90. return bio->bi_io_vec[bio->bi_max_vecs - 1].bv_len;
  91. }
  92. /*-----------------------------------------------------------------
  93. * We need an io object to keep track of the number of bios that
  94. * have been dispatched for a particular io.
  95. *---------------------------------------------------------------*/
  96. static void dec_count(struct io *io, unsigned int region, int error)
  97. {
  98. if (error)
  99. set_bit(region, &io->error);
  100. if (atomic_dec_and_test(&io->count)) {
  101. if (io->sleeper)
  102. wake_up_process(io->sleeper);
  103. else {
  104. int r = io->error;
  105. io_notify_fn fn = io->callback;
  106. void *context = io->context;
  107. mempool_free(io, _io_pool);
  108. fn(r, context);
  109. }
  110. }
  111. }
  112. static int endio(struct bio *bio, unsigned int done, int error)
  113. {
  114. struct io *io = (struct io *) bio->bi_private;
  115. /* keep going until we've finished */
  116. if (bio->bi_size)
  117. return 1;
  118. if (error && bio_data_dir(bio) == READ)
  119. zero_fill_bio(bio);
  120. dec_count(io, bio_get_region(bio), error);
  121. bio_put(bio);
  122. return 0;
  123. }
  124. /*-----------------------------------------------------------------
  125. * These little objects provide an abstraction for getting a new
  126. * destination page for io.
  127. *---------------------------------------------------------------*/
  128. struct dpages {
  129. void (*get_page)(struct dpages *dp,
  130. struct page **p, unsigned long *len, unsigned *offset);
  131. void (*next_page)(struct dpages *dp);
  132. unsigned context_u;
  133. void *context_ptr;
  134. };
  135. /*
  136. * Functions for getting the pages from a list.
  137. */
  138. static void list_get_page(struct dpages *dp,
  139. struct page **p, unsigned long *len, unsigned *offset)
  140. {
  141. unsigned o = dp->context_u;
  142. struct page_list *pl = (struct page_list *) dp->context_ptr;
  143. *p = pl->page;
  144. *len = PAGE_SIZE - o;
  145. *offset = o;
  146. }
  147. static void list_next_page(struct dpages *dp)
  148. {
  149. struct page_list *pl = (struct page_list *) dp->context_ptr;
  150. dp->context_ptr = pl->next;
  151. dp->context_u = 0;
  152. }
  153. static void list_dp_init(struct dpages *dp, struct page_list *pl, unsigned offset)
  154. {
  155. dp->get_page = list_get_page;
  156. dp->next_page = list_next_page;
  157. dp->context_u = offset;
  158. dp->context_ptr = pl;
  159. }
  160. /*
  161. * Functions for getting the pages from a bvec.
  162. */
  163. static void bvec_get_page(struct dpages *dp,
  164. struct page **p, unsigned long *len, unsigned *offset)
  165. {
  166. struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
  167. *p = bvec->bv_page;
  168. *len = bvec->bv_len;
  169. *offset = bvec->bv_offset;
  170. }
  171. static void bvec_next_page(struct dpages *dp)
  172. {
  173. struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
  174. dp->context_ptr = bvec + 1;
  175. }
  176. static void bvec_dp_init(struct dpages *dp, struct bio_vec *bvec)
  177. {
  178. dp->get_page = bvec_get_page;
  179. dp->next_page = bvec_next_page;
  180. dp->context_ptr = bvec;
  181. }
  182. static void vm_get_page(struct dpages *dp,
  183. struct page **p, unsigned long *len, unsigned *offset)
  184. {
  185. *p = vmalloc_to_page(dp->context_ptr);
  186. *offset = dp->context_u;
  187. *len = PAGE_SIZE - dp->context_u;
  188. }
  189. static void vm_next_page(struct dpages *dp)
  190. {
  191. dp->context_ptr += PAGE_SIZE - dp->context_u;
  192. dp->context_u = 0;
  193. }
  194. static void vm_dp_init(struct dpages *dp, void *data)
  195. {
  196. dp->get_page = vm_get_page;
  197. dp->next_page = vm_next_page;
  198. dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
  199. dp->context_ptr = data;
  200. }
  201. static void dm_bio_destructor(struct bio *bio)
  202. {
  203. bio_free(bio, _bios);
  204. }
  205. /*-----------------------------------------------------------------
  206. * IO routines that accept a list of pages.
  207. *---------------------------------------------------------------*/
  208. static void do_region(int rw, unsigned int region, struct io_region *where,
  209. struct dpages *dp, struct io *io)
  210. {
  211. struct bio *bio;
  212. struct page *page;
  213. unsigned long len;
  214. unsigned offset;
  215. unsigned num_bvecs;
  216. sector_t remaining = where->count;
  217. while (remaining) {
  218. /*
  219. * Allocate a suitably sized bio, we add an extra
  220. * bvec for bio_get/set_region().
  221. */
  222. num_bvecs = (remaining / (PAGE_SIZE >> 9)) + 2;
  223. bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, _bios);
  224. bio->bi_sector = where->sector + (where->count - remaining);
  225. bio->bi_bdev = where->bdev;
  226. bio->bi_end_io = endio;
  227. bio->bi_private = io;
  228. bio->bi_destructor = dm_bio_destructor;
  229. bio_set_region(bio, region);
  230. /*
  231. * Try and add as many pages as possible.
  232. */
  233. while (remaining) {
  234. dp->get_page(dp, &page, &len, &offset);
  235. len = min(len, to_bytes(remaining));
  236. if (!bio_add_page(bio, page, len, offset))
  237. break;
  238. offset = 0;
  239. remaining -= to_sector(len);
  240. dp->next_page(dp);
  241. }
  242. atomic_inc(&io->count);
  243. submit_bio(rw, bio);
  244. }
  245. }
  246. static void dispatch_io(int rw, unsigned int num_regions,
  247. struct io_region *where, struct dpages *dp,
  248. struct io *io, int sync)
  249. {
  250. int i;
  251. struct dpages old_pages = *dp;
  252. if (sync)
  253. rw |= (1 << BIO_RW_SYNC);
  254. /*
  255. * For multiple regions we need to be careful to rewind
  256. * the dp object for each call to do_region.
  257. */
  258. for (i = 0; i < num_regions; i++) {
  259. *dp = old_pages;
  260. if (where[i].count)
  261. do_region(rw, i, where + i, dp, io);
  262. }
  263. /*
  264. * Drop the extra refence that we were holding to avoid
  265. * the io being completed too early.
  266. */
  267. dec_count(io, 0, 0);
  268. }
  269. static int sync_io(unsigned int num_regions, struct io_region *where,
  270. int rw, struct dpages *dp, unsigned long *error_bits)
  271. {
  272. struct io io;
  273. if (num_regions > 1 && rw != WRITE) {
  274. WARN_ON(1);
  275. return -EIO;
  276. }
  277. io.error = 0;
  278. atomic_set(&io.count, 1); /* see dispatch_io() */
  279. io.sleeper = current;
  280. dispatch_io(rw, num_regions, where, dp, &io, 1);
  281. while (1) {
  282. set_current_state(TASK_UNINTERRUPTIBLE);
  283. if (!atomic_read(&io.count) || signal_pending(current))
  284. break;
  285. io_schedule();
  286. }
  287. set_current_state(TASK_RUNNING);
  288. if (atomic_read(&io.count))
  289. return -EINTR;
  290. *error_bits = io.error;
  291. return io.error ? -EIO : 0;
  292. }
  293. static int async_io(unsigned int num_regions, struct io_region *where, int rw,
  294. struct dpages *dp, io_notify_fn fn, void *context)
  295. {
  296. struct io *io;
  297. if (num_regions > 1 && rw != WRITE) {
  298. WARN_ON(1);
  299. fn(1, context);
  300. return -EIO;
  301. }
  302. io = mempool_alloc(_io_pool, GFP_NOIO);
  303. io->error = 0;
  304. atomic_set(&io->count, 1); /* see dispatch_io() */
  305. io->sleeper = NULL;
  306. io->callback = fn;
  307. io->context = context;
  308. dispatch_io(rw, num_regions, where, dp, io, 0);
  309. return 0;
  310. }
  311. int dm_io_sync(unsigned int num_regions, struct io_region *where, int rw,
  312. struct page_list *pl, unsigned int offset,
  313. unsigned long *error_bits)
  314. {
  315. struct dpages dp;
  316. list_dp_init(&dp, pl, offset);
  317. return sync_io(num_regions, where, rw, &dp, error_bits);
  318. }
  319. int dm_io_sync_bvec(unsigned int num_regions, struct io_region *where, int rw,
  320. struct bio_vec *bvec, unsigned long *error_bits)
  321. {
  322. struct dpages dp;
  323. bvec_dp_init(&dp, bvec);
  324. return sync_io(num_regions, where, rw, &dp, error_bits);
  325. }
  326. int dm_io_sync_vm(unsigned int num_regions, struct io_region *where, int rw,
  327. void *data, unsigned long *error_bits)
  328. {
  329. struct dpages dp;
  330. vm_dp_init(&dp, data);
  331. return sync_io(num_regions, where, rw, &dp, error_bits);
  332. }
  333. int dm_io_async(unsigned int num_regions, struct io_region *where, int rw,
  334. struct page_list *pl, unsigned int offset,
  335. io_notify_fn fn, void *context)
  336. {
  337. struct dpages dp;
  338. list_dp_init(&dp, pl, offset);
  339. return async_io(num_regions, where, rw, &dp, fn, context);
  340. }
  341. int dm_io_async_bvec(unsigned int num_regions, struct io_region *where, int rw,
  342. struct bio_vec *bvec, io_notify_fn fn, void *context)
  343. {
  344. struct dpages dp;
  345. bvec_dp_init(&dp, bvec);
  346. return async_io(num_regions, where, rw, &dp, fn, context);
  347. }
  348. int dm_io_async_vm(unsigned int num_regions, struct io_region *where, int rw,
  349. void *data, io_notify_fn fn, void *context)
  350. {
  351. struct dpages dp;
  352. vm_dp_init(&dp, data);
  353. return async_io(num_regions, where, rw, &dp, fn, context);
  354. }
  355. EXPORT_SYMBOL(dm_io_get);
  356. EXPORT_SYMBOL(dm_io_put);
  357. EXPORT_SYMBOL(dm_io_sync);
  358. EXPORT_SYMBOL(dm_io_async);
  359. EXPORT_SYMBOL(dm_io_sync_bvec);
  360. EXPORT_SYMBOL(dm_io_async_bvec);
  361. EXPORT_SYMBOL(dm_io_sync_vm);
  362. EXPORT_SYMBOL(dm_io_async_vm);