dm-io.c 10 KB

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