dm-io.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. /*-----------------------------------------------------------------
  202. * IO routines that accept a list of pages.
  203. *---------------------------------------------------------------*/
  204. static void do_region(int rw, unsigned int region, struct io_region *where,
  205. struct dpages *dp, struct io *io)
  206. {
  207. struct bio *bio;
  208. struct page *page;
  209. unsigned long len;
  210. unsigned offset;
  211. unsigned num_bvecs;
  212. sector_t remaining = where->count;
  213. while (remaining) {
  214. /*
  215. * Allocate a suitably sized bio, we add an extra
  216. * bvec for bio_get/set_region().
  217. */
  218. num_bvecs = (remaining / (PAGE_SIZE >> 9)) + 2;
  219. bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, _bios);
  220. bio->bi_sector = where->sector + (where->count - remaining);
  221. bio->bi_bdev = where->bdev;
  222. bio->bi_end_io = endio;
  223. bio->bi_private = io;
  224. bio_set_region(bio, region);
  225. /*
  226. * Try and add as many pages as possible.
  227. */
  228. while (remaining) {
  229. dp->get_page(dp, &page, &len, &offset);
  230. len = min(len, to_bytes(remaining));
  231. if (!bio_add_page(bio, page, len, offset))
  232. break;
  233. offset = 0;
  234. remaining -= to_sector(len);
  235. dp->next_page(dp);
  236. }
  237. atomic_inc(&io->count);
  238. submit_bio(rw, bio);
  239. }
  240. }
  241. static void dispatch_io(int rw, unsigned int num_regions,
  242. struct io_region *where, struct dpages *dp,
  243. struct io *io, int sync)
  244. {
  245. int i;
  246. struct dpages old_pages = *dp;
  247. if (sync)
  248. rw |= (1 << BIO_RW_SYNC);
  249. /*
  250. * For multiple regions we need to be careful to rewind
  251. * the dp object for each call to do_region.
  252. */
  253. for (i = 0; i < num_regions; i++) {
  254. *dp = old_pages;
  255. if (where[i].count)
  256. do_region(rw, i, where + i, dp, io);
  257. }
  258. /*
  259. * Drop the extra refence that we were holding to avoid
  260. * the io being completed too early.
  261. */
  262. dec_count(io, 0, 0);
  263. }
  264. static int sync_io(unsigned int num_regions, struct io_region *where,
  265. int rw, struct dpages *dp, unsigned long *error_bits)
  266. {
  267. struct io io;
  268. if (num_regions > 1 && rw != WRITE) {
  269. WARN_ON(1);
  270. return -EIO;
  271. }
  272. io.error = 0;
  273. atomic_set(&io.count, 1); /* see dispatch_io() */
  274. io.sleeper = current;
  275. dispatch_io(rw, num_regions, where, dp, &io, 1);
  276. while (1) {
  277. set_current_state(TASK_UNINTERRUPTIBLE);
  278. if (!atomic_read(&io.count) || signal_pending(current))
  279. break;
  280. io_schedule();
  281. }
  282. set_current_state(TASK_RUNNING);
  283. if (atomic_read(&io.count))
  284. return -EINTR;
  285. *error_bits = io.error;
  286. return io.error ? -EIO : 0;
  287. }
  288. static int async_io(unsigned int num_regions, struct io_region *where, int rw,
  289. struct dpages *dp, io_notify_fn fn, void *context)
  290. {
  291. struct io *io;
  292. if (num_regions > 1 && rw != WRITE) {
  293. WARN_ON(1);
  294. fn(1, context);
  295. return -EIO;
  296. }
  297. io = mempool_alloc(_io_pool, GFP_NOIO);
  298. io->error = 0;
  299. atomic_set(&io->count, 1); /* see dispatch_io() */
  300. io->sleeper = NULL;
  301. io->callback = fn;
  302. io->context = context;
  303. dispatch_io(rw, num_regions, where, dp, io, 0);
  304. return 0;
  305. }
  306. int dm_io_sync(unsigned int num_regions, struct io_region *where, int rw,
  307. struct page_list *pl, unsigned int offset,
  308. unsigned long *error_bits)
  309. {
  310. struct dpages dp;
  311. list_dp_init(&dp, pl, offset);
  312. return sync_io(num_regions, where, rw, &dp, error_bits);
  313. }
  314. int dm_io_sync_bvec(unsigned int num_regions, struct io_region *where, int rw,
  315. struct bio_vec *bvec, unsigned long *error_bits)
  316. {
  317. struct dpages dp;
  318. bvec_dp_init(&dp, bvec);
  319. return sync_io(num_regions, where, rw, &dp, error_bits);
  320. }
  321. int dm_io_sync_vm(unsigned int num_regions, struct io_region *where, int rw,
  322. void *data, unsigned long *error_bits)
  323. {
  324. struct dpages dp;
  325. vm_dp_init(&dp, data);
  326. return sync_io(num_regions, where, rw, &dp, error_bits);
  327. }
  328. int dm_io_async(unsigned int num_regions, struct io_region *where, int rw,
  329. struct page_list *pl, unsigned int offset,
  330. io_notify_fn fn, void *context)
  331. {
  332. struct dpages dp;
  333. list_dp_init(&dp, pl, offset);
  334. return async_io(num_regions, where, rw, &dp, fn, context);
  335. }
  336. int dm_io_async_bvec(unsigned int num_regions, struct io_region *where, int rw,
  337. struct bio_vec *bvec, io_notify_fn fn, void *context)
  338. {
  339. struct dpages dp;
  340. bvec_dp_init(&dp, bvec);
  341. return async_io(num_regions, where, rw, &dp, fn, context);
  342. }
  343. int dm_io_async_vm(unsigned int num_regions, struct io_region *where, int rw,
  344. void *data, io_notify_fn fn, void *context)
  345. {
  346. struct dpages dp;
  347. vm_dp_init(&dp, data);
  348. return async_io(num_regions, where, rw, &dp, fn, context);
  349. }
  350. EXPORT_SYMBOL(dm_io_get);
  351. EXPORT_SYMBOL(dm_io_put);
  352. EXPORT_SYMBOL(dm_io_sync);
  353. EXPORT_SYMBOL(dm_io_async);
  354. EXPORT_SYMBOL(dm_io_sync_bvec);
  355. EXPORT_SYMBOL(dm_io_async_bvec);
  356. EXPORT_SYMBOL(dm_io_sync_vm);
  357. EXPORT_SYMBOL(dm_io_async_vm);