dm-io.c 9.9 KB

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