dm-io.c 10.0 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 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].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].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->bi_max_vecs++;
  115. bio_put(bio);
  116. return 0;
  117. }
  118. /*-----------------------------------------------------------------
  119. * These little objects provide an abstraction for getting a new
  120. * destination page for io.
  121. *---------------------------------------------------------------*/
  122. struct dpages {
  123. void (*get_page)(struct dpages *dp,
  124. struct page **p, unsigned long *len, unsigned *offset);
  125. void (*next_page)(struct dpages *dp);
  126. unsigned context_u;
  127. void *context_ptr;
  128. };
  129. /*
  130. * Functions for getting the pages from a list.
  131. */
  132. static void list_get_page(struct dpages *dp,
  133. struct page **p, unsigned long *len, unsigned *offset)
  134. {
  135. unsigned o = dp->context_u;
  136. struct page_list *pl = (struct page_list *) dp->context_ptr;
  137. *p = pl->page;
  138. *len = PAGE_SIZE - o;
  139. *offset = o;
  140. }
  141. static void list_next_page(struct dpages *dp)
  142. {
  143. struct page_list *pl = (struct page_list *) dp->context_ptr;
  144. dp->context_ptr = pl->next;
  145. dp->context_u = 0;
  146. }
  147. static void list_dp_init(struct dpages *dp, struct page_list *pl, unsigned offset)
  148. {
  149. dp->get_page = list_get_page;
  150. dp->next_page = list_next_page;
  151. dp->context_u = offset;
  152. dp->context_ptr = pl;
  153. }
  154. /*
  155. * Functions for getting the pages from a bvec.
  156. */
  157. static void bvec_get_page(struct dpages *dp,
  158. struct page **p, unsigned long *len, unsigned *offset)
  159. {
  160. struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
  161. *p = bvec->bv_page;
  162. *len = bvec->bv_len;
  163. *offset = bvec->bv_offset;
  164. }
  165. static void bvec_next_page(struct dpages *dp)
  166. {
  167. struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
  168. dp->context_ptr = bvec + 1;
  169. }
  170. static void bvec_dp_init(struct dpages *dp, struct bio_vec *bvec)
  171. {
  172. dp->get_page = bvec_get_page;
  173. dp->next_page = bvec_next_page;
  174. dp->context_ptr = bvec;
  175. }
  176. static void vm_get_page(struct dpages *dp,
  177. struct page **p, unsigned long *len, unsigned *offset)
  178. {
  179. *p = vmalloc_to_page(dp->context_ptr);
  180. *offset = dp->context_u;
  181. *len = PAGE_SIZE - dp->context_u;
  182. }
  183. static void vm_next_page(struct dpages *dp)
  184. {
  185. dp->context_ptr += PAGE_SIZE - dp->context_u;
  186. dp->context_u = 0;
  187. }
  188. static void vm_dp_init(struct dpages *dp, void *data)
  189. {
  190. dp->get_page = vm_get_page;
  191. dp->next_page = vm_next_page;
  192. dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
  193. dp->context_ptr = data;
  194. }
  195. static void dm_bio_destructor(struct bio *bio)
  196. {
  197. bio_free(bio, _bios);
  198. }
  199. /*-----------------------------------------------------------------
  200. * IO routines that accept a list of pages.
  201. *---------------------------------------------------------------*/
  202. static void do_region(int rw, unsigned int region, struct io_region *where,
  203. struct dpages *dp, struct io *io)
  204. {
  205. struct bio *bio;
  206. struct page *page;
  207. unsigned long len;
  208. unsigned offset;
  209. unsigned num_bvecs;
  210. sector_t remaining = where->count;
  211. while (remaining) {
  212. /*
  213. * Allocate a suitably sized-bio: we add an extra
  214. * bvec for bio_get/set_region() and decrement bi_max_vecs
  215. * to hide it from bio_add_page().
  216. */
  217. num_bvecs = (remaining / (PAGE_SIZE >> SECTOR_SHIFT)) + 2;
  218. bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, _bios);
  219. bio->bi_sector = where->sector + (where->count - remaining);
  220. bio->bi_bdev = where->bdev;
  221. bio->bi_end_io = endio;
  222. bio->bi_private = io;
  223. bio->bi_destructor = dm_bio_destructor;
  224. bio->bi_max_vecs--;
  225. bio_set_region(bio, region);
  226. /*
  227. * Try and add as many pages as possible.
  228. */
  229. while (remaining) {
  230. dp->get_page(dp, &page, &len, &offset);
  231. len = min(len, to_bytes(remaining));
  232. if (!bio_add_page(bio, page, len, offset))
  233. break;
  234. offset = 0;
  235. remaining -= to_sector(len);
  236. dp->next_page(dp);
  237. }
  238. atomic_inc(&io->count);
  239. submit_bio(rw, bio);
  240. }
  241. }
  242. static void dispatch_io(int rw, unsigned int num_regions,
  243. struct io_region *where, struct dpages *dp,
  244. struct io *io, int sync)
  245. {
  246. int i;
  247. struct dpages old_pages = *dp;
  248. if (sync)
  249. rw |= (1 << BIO_RW_SYNC);
  250. /*
  251. * For multiple regions we need to be careful to rewind
  252. * the dp object for each call to do_region.
  253. */
  254. for (i = 0; i < num_regions; i++) {
  255. *dp = old_pages;
  256. if (where[i].count)
  257. do_region(rw, i, where + i, dp, io);
  258. }
  259. /*
  260. * Drop the extra reference that we were holding to avoid
  261. * the io being completed too early.
  262. */
  263. dec_count(io, 0, 0);
  264. }
  265. static int sync_io(unsigned int num_regions, struct io_region *where,
  266. int rw, struct dpages *dp, unsigned long *error_bits)
  267. {
  268. struct io io;
  269. if (num_regions > 1 && rw != WRITE) {
  270. WARN_ON(1);
  271. return -EIO;
  272. }
  273. io.error = 0;
  274. atomic_set(&io.count, 1); /* see dispatch_io() */
  275. io.sleeper = current;
  276. dispatch_io(rw, num_regions, where, dp, &io, 1);
  277. while (1) {
  278. set_current_state(TASK_UNINTERRUPTIBLE);
  279. if (!atomic_read(&io.count) || signal_pending(current))
  280. break;
  281. io_schedule();
  282. }
  283. set_current_state(TASK_RUNNING);
  284. if (atomic_read(&io.count))
  285. return -EINTR;
  286. *error_bits = io.error;
  287. return io.error ? -EIO : 0;
  288. }
  289. static int async_io(unsigned int num_regions, struct io_region *where, int rw,
  290. struct dpages *dp, io_notify_fn fn, void *context)
  291. {
  292. struct io *io;
  293. if (num_regions > 1 && rw != WRITE) {
  294. WARN_ON(1);
  295. fn(1, context);
  296. return -EIO;
  297. }
  298. io = mempool_alloc(_io_pool, GFP_NOIO);
  299. io->error = 0;
  300. atomic_set(&io->count, 1); /* see dispatch_io() */
  301. io->sleeper = NULL;
  302. io->callback = fn;
  303. io->context = context;
  304. dispatch_io(rw, num_regions, where, dp, io, 0);
  305. return 0;
  306. }
  307. int dm_io_sync(unsigned int num_regions, struct io_region *where, int rw,
  308. struct page_list *pl, unsigned int offset,
  309. unsigned long *error_bits)
  310. {
  311. struct dpages dp;
  312. list_dp_init(&dp, pl, offset);
  313. return sync_io(num_regions, where, rw, &dp, error_bits);
  314. }
  315. int dm_io_sync_bvec(unsigned int num_regions, struct io_region *where, int rw,
  316. struct bio_vec *bvec, unsigned long *error_bits)
  317. {
  318. struct dpages dp;
  319. bvec_dp_init(&dp, bvec);
  320. return sync_io(num_regions, where, rw, &dp, error_bits);
  321. }
  322. int dm_io_sync_vm(unsigned int num_regions, struct io_region *where, int rw,
  323. void *data, unsigned long *error_bits)
  324. {
  325. struct dpages dp;
  326. vm_dp_init(&dp, data);
  327. return sync_io(num_regions, where, rw, &dp, error_bits);
  328. }
  329. int dm_io_async(unsigned int num_regions, struct io_region *where, int rw,
  330. struct page_list *pl, unsigned int offset,
  331. io_notify_fn fn, void *context)
  332. {
  333. struct dpages dp;
  334. list_dp_init(&dp, pl, offset);
  335. return async_io(num_regions, where, rw, &dp, fn, context);
  336. }
  337. int dm_io_async_bvec(unsigned int num_regions, struct io_region *where, int rw,
  338. struct bio_vec *bvec, io_notify_fn fn, void *context)
  339. {
  340. struct dpages dp;
  341. bvec_dp_init(&dp, bvec);
  342. return async_io(num_regions, where, rw, &dp, fn, context);
  343. }
  344. int dm_io_async_vm(unsigned int num_regions, struct io_region *where, int rw,
  345. void *data, io_notify_fn fn, void *context)
  346. {
  347. struct dpages dp;
  348. vm_dp_init(&dp, data);
  349. return async_io(num_regions, where, rw, &dp, fn, context);
  350. }
  351. EXPORT_SYMBOL(dm_io_get);
  352. EXPORT_SYMBOL(dm_io_put);
  353. EXPORT_SYMBOL(dm_io_sync);
  354. EXPORT_SYMBOL(dm_io_async);
  355. EXPORT_SYMBOL(dm_io_sync_bvec);
  356. EXPORT_SYMBOL(dm_io_async_bvec);
  357. EXPORT_SYMBOL(dm_io_sync_vm);
  358. EXPORT_SYMBOL(dm_io_async_vm);