dm-io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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.h"
  8. #include <linux/device-mapper.h>
  9. #include <linux/bio.h>
  10. #include <linux/mempool.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/dm-io.h>
  15. #define DM_MSG_PREFIX "io"
  16. #define DM_IO_MAX_REGIONS BITS_PER_LONG
  17. struct dm_io_client {
  18. mempool_t *pool;
  19. struct bio_set *bios;
  20. };
  21. /*
  22. * Aligning 'struct io' reduces the number of bits required to store
  23. * its address. Refer to store_io_and_region_in_bio() below.
  24. */
  25. struct io {
  26. unsigned long error_bits;
  27. atomic_t count;
  28. struct task_struct *sleeper;
  29. struct dm_io_client *client;
  30. io_notify_fn callback;
  31. void *context;
  32. } __attribute__((aligned(DM_IO_MAX_REGIONS)));
  33. static struct kmem_cache *_dm_io_cache;
  34. /*
  35. * io contexts are only dynamically allocated for asynchronous
  36. * io. Since async io is likely to be the majority of io we'll
  37. * have the same number of io contexts as bios! (FIXME: must reduce this).
  38. */
  39. static unsigned int pages_to_ios(unsigned int pages)
  40. {
  41. return 4 * pages; /* too many ? */
  42. }
  43. /*
  44. * Create a client with mempool and bioset.
  45. */
  46. struct dm_io_client *dm_io_client_create(unsigned num_pages)
  47. {
  48. unsigned ios = pages_to_ios(num_pages);
  49. struct dm_io_client *client;
  50. client = kmalloc(sizeof(*client), GFP_KERNEL);
  51. if (!client)
  52. return ERR_PTR(-ENOMEM);
  53. client->pool = mempool_create_slab_pool(ios, _dm_io_cache);
  54. if (!client->pool)
  55. goto bad;
  56. client->bios = bioset_create(16, 0);
  57. if (!client->bios)
  58. goto bad;
  59. return client;
  60. bad:
  61. if (client->pool)
  62. mempool_destroy(client->pool);
  63. kfree(client);
  64. return ERR_PTR(-ENOMEM);
  65. }
  66. EXPORT_SYMBOL(dm_io_client_create);
  67. int dm_io_client_resize(unsigned num_pages, struct dm_io_client *client)
  68. {
  69. return mempool_resize(client->pool, pages_to_ios(num_pages),
  70. GFP_KERNEL);
  71. }
  72. EXPORT_SYMBOL(dm_io_client_resize);
  73. void dm_io_client_destroy(struct dm_io_client *client)
  74. {
  75. mempool_destroy(client->pool);
  76. bioset_free(client->bios);
  77. kfree(client);
  78. }
  79. EXPORT_SYMBOL(dm_io_client_destroy);
  80. /*-----------------------------------------------------------------
  81. * We need to keep track of which region a bio is doing io for.
  82. * To avoid a memory allocation to store just 5 or 6 bits, we
  83. * ensure the 'struct io' pointer is aligned so enough low bits are
  84. * always zero and then combine it with the region number directly in
  85. * bi_private.
  86. *---------------------------------------------------------------*/
  87. static void store_io_and_region_in_bio(struct bio *bio, struct io *io,
  88. unsigned region)
  89. {
  90. if (unlikely(!IS_ALIGNED((unsigned long)io, DM_IO_MAX_REGIONS))) {
  91. DMCRIT("Unaligned struct io pointer %p", io);
  92. BUG();
  93. }
  94. bio->bi_private = (void *)((unsigned long)io | region);
  95. }
  96. static void retrieve_io_and_region_from_bio(struct bio *bio, struct io **io,
  97. unsigned *region)
  98. {
  99. unsigned long val = (unsigned long)bio->bi_private;
  100. *io = (void *)(val & -(unsigned long)DM_IO_MAX_REGIONS);
  101. *region = val & (DM_IO_MAX_REGIONS - 1);
  102. }
  103. /*-----------------------------------------------------------------
  104. * We need an io object to keep track of the number of bios that
  105. * have been dispatched for a particular io.
  106. *---------------------------------------------------------------*/
  107. static void dec_count(struct io *io, unsigned int region, int error)
  108. {
  109. if (error)
  110. set_bit(region, &io->error_bits);
  111. if (atomic_dec_and_test(&io->count)) {
  112. if (io->sleeper)
  113. wake_up_process(io->sleeper);
  114. else {
  115. unsigned long r = io->error_bits;
  116. io_notify_fn fn = io->callback;
  117. void *context = io->context;
  118. mempool_free(io, io->client->pool);
  119. fn(r, context);
  120. }
  121. }
  122. }
  123. static void endio(struct bio *bio, int error)
  124. {
  125. struct io *io;
  126. unsigned region;
  127. if (error && bio_data_dir(bio) == READ)
  128. zero_fill_bio(bio);
  129. /*
  130. * The bio destructor in bio_put() may use the io object.
  131. */
  132. retrieve_io_and_region_from_bio(bio, &io, &region);
  133. bio_put(bio);
  134. dec_count(io, region, error);
  135. }
  136. /*-----------------------------------------------------------------
  137. * These little objects provide an abstraction for getting a new
  138. * destination page for io.
  139. *---------------------------------------------------------------*/
  140. struct dpages {
  141. void (*get_page)(struct dpages *dp,
  142. struct page **p, unsigned long *len, unsigned *offset);
  143. void (*next_page)(struct dpages *dp);
  144. unsigned context_u;
  145. void *context_ptr;
  146. };
  147. /*
  148. * Functions for getting the pages from a list.
  149. */
  150. static void list_get_page(struct dpages *dp,
  151. struct page **p, unsigned long *len, unsigned *offset)
  152. {
  153. unsigned o = dp->context_u;
  154. struct page_list *pl = (struct page_list *) dp->context_ptr;
  155. *p = pl->page;
  156. *len = PAGE_SIZE - o;
  157. *offset = o;
  158. }
  159. static void list_next_page(struct dpages *dp)
  160. {
  161. struct page_list *pl = (struct page_list *) dp->context_ptr;
  162. dp->context_ptr = pl->next;
  163. dp->context_u = 0;
  164. }
  165. static void list_dp_init(struct dpages *dp, struct page_list *pl, unsigned offset)
  166. {
  167. dp->get_page = list_get_page;
  168. dp->next_page = list_next_page;
  169. dp->context_u = offset;
  170. dp->context_ptr = pl;
  171. }
  172. /*
  173. * Functions for getting the pages from a bvec.
  174. */
  175. static void bvec_get_page(struct dpages *dp,
  176. struct page **p, unsigned long *len, unsigned *offset)
  177. {
  178. struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
  179. *p = bvec->bv_page;
  180. *len = bvec->bv_len;
  181. *offset = bvec->bv_offset;
  182. }
  183. static void bvec_next_page(struct dpages *dp)
  184. {
  185. struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
  186. dp->context_ptr = bvec + 1;
  187. }
  188. static void bvec_dp_init(struct dpages *dp, struct bio_vec *bvec)
  189. {
  190. dp->get_page = bvec_get_page;
  191. dp->next_page = bvec_next_page;
  192. dp->context_ptr = bvec;
  193. }
  194. /*
  195. * Functions for getting the pages from a VMA.
  196. */
  197. static void vm_get_page(struct dpages *dp,
  198. struct page **p, unsigned long *len, unsigned *offset)
  199. {
  200. *p = vmalloc_to_page(dp->context_ptr);
  201. *offset = dp->context_u;
  202. *len = PAGE_SIZE - dp->context_u;
  203. }
  204. static void vm_next_page(struct dpages *dp)
  205. {
  206. dp->context_ptr += PAGE_SIZE - dp->context_u;
  207. dp->context_u = 0;
  208. }
  209. static void vm_dp_init(struct dpages *dp, void *data)
  210. {
  211. dp->get_page = vm_get_page;
  212. dp->next_page = vm_next_page;
  213. dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
  214. dp->context_ptr = data;
  215. }
  216. static void dm_bio_destructor(struct bio *bio)
  217. {
  218. unsigned region;
  219. struct io *io;
  220. retrieve_io_and_region_from_bio(bio, &io, &region);
  221. bio_free(bio, io->client->bios);
  222. }
  223. /*
  224. * Functions for getting the pages from kernel memory.
  225. */
  226. static void km_get_page(struct dpages *dp, struct page **p, unsigned long *len,
  227. unsigned *offset)
  228. {
  229. *p = virt_to_page(dp->context_ptr);
  230. *offset = dp->context_u;
  231. *len = PAGE_SIZE - dp->context_u;
  232. }
  233. static void km_next_page(struct dpages *dp)
  234. {
  235. dp->context_ptr += PAGE_SIZE - dp->context_u;
  236. dp->context_u = 0;
  237. }
  238. static void km_dp_init(struct dpages *dp, void *data)
  239. {
  240. dp->get_page = km_get_page;
  241. dp->next_page = km_next_page;
  242. dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
  243. dp->context_ptr = data;
  244. }
  245. /*-----------------------------------------------------------------
  246. * IO routines that accept a list of pages.
  247. *---------------------------------------------------------------*/
  248. static void do_region(int rw, unsigned region, struct dm_io_region *where,
  249. struct dpages *dp, struct io *io)
  250. {
  251. struct bio *bio;
  252. struct page *page;
  253. unsigned long len;
  254. unsigned offset;
  255. unsigned num_bvecs;
  256. sector_t remaining = where->count;
  257. /*
  258. * where->count may be zero if rw holds a flush and we need to
  259. * send a zero-sized flush.
  260. */
  261. do {
  262. /*
  263. * Allocate a suitably sized-bio.
  264. */
  265. num_bvecs = dm_sector_div_up(remaining,
  266. (PAGE_SIZE >> SECTOR_SHIFT));
  267. num_bvecs = min_t(int, bio_get_nr_vecs(where->bdev), num_bvecs);
  268. bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, io->client->bios);
  269. bio->bi_sector = where->sector + (where->count - remaining);
  270. bio->bi_bdev = where->bdev;
  271. bio->bi_end_io = endio;
  272. bio->bi_destructor = dm_bio_destructor;
  273. store_io_and_region_in_bio(bio, io, region);
  274. /*
  275. * Try and add as many pages as possible.
  276. */
  277. while (remaining) {
  278. dp->get_page(dp, &page, &len, &offset);
  279. len = min(len, to_bytes(remaining));
  280. if (!bio_add_page(bio, page, len, offset))
  281. break;
  282. offset = 0;
  283. remaining -= to_sector(len);
  284. dp->next_page(dp);
  285. }
  286. atomic_inc(&io->count);
  287. submit_bio(rw, bio);
  288. } while (remaining);
  289. }
  290. static void dispatch_io(int rw, unsigned int num_regions,
  291. struct dm_io_region *where, struct dpages *dp,
  292. struct io *io, int sync)
  293. {
  294. int i;
  295. struct dpages old_pages = *dp;
  296. BUG_ON(num_regions > DM_IO_MAX_REGIONS);
  297. if (sync)
  298. rw |= REQ_SYNC | REQ_UNPLUG;
  299. /*
  300. * For multiple regions we need to be careful to rewind
  301. * the dp object for each call to do_region.
  302. */
  303. for (i = 0; i < num_regions; i++) {
  304. *dp = old_pages;
  305. if (where[i].count || (rw & REQ_FLUSH))
  306. do_region(rw, i, where + i, dp, io);
  307. }
  308. /*
  309. * Drop the extra reference that we were holding to avoid
  310. * the io being completed too early.
  311. */
  312. dec_count(io, 0, 0);
  313. }
  314. static int sync_io(struct dm_io_client *client, unsigned int num_regions,
  315. struct dm_io_region *where, int rw, struct dpages *dp,
  316. unsigned long *error_bits)
  317. {
  318. /*
  319. * gcc <= 4.3 can't do the alignment for stack variables, so we must
  320. * align it on our own.
  321. * volatile prevents the optimizer from removing or reusing
  322. * "io_" field from the stack frame (allowed in ANSI C).
  323. */
  324. volatile char io_[sizeof(struct io) + __alignof__(struct io) - 1];
  325. struct io *io = (struct io *)PTR_ALIGN(&io_, __alignof__(struct io));
  326. if (num_regions > 1 && (rw & RW_MASK) != WRITE) {
  327. WARN_ON(1);
  328. return -EIO;
  329. }
  330. io->error_bits = 0;
  331. atomic_set(&io->count, 1); /* see dispatch_io() */
  332. io->sleeper = current;
  333. io->client = client;
  334. dispatch_io(rw, num_regions, where, dp, io, 1);
  335. while (1) {
  336. set_current_state(TASK_UNINTERRUPTIBLE);
  337. if (!atomic_read(&io->count))
  338. break;
  339. io_schedule();
  340. }
  341. set_current_state(TASK_RUNNING);
  342. if (error_bits)
  343. *error_bits = io->error_bits;
  344. return io->error_bits ? -EIO : 0;
  345. }
  346. static int async_io(struct dm_io_client *client, unsigned int num_regions,
  347. struct dm_io_region *where, int rw, struct dpages *dp,
  348. io_notify_fn fn, void *context)
  349. {
  350. struct io *io;
  351. if (num_regions > 1 && (rw & RW_MASK) != WRITE) {
  352. WARN_ON(1);
  353. fn(1, context);
  354. return -EIO;
  355. }
  356. io = mempool_alloc(client->pool, GFP_NOIO);
  357. io->error_bits = 0;
  358. atomic_set(&io->count, 1); /* see dispatch_io() */
  359. io->sleeper = NULL;
  360. io->client = client;
  361. io->callback = fn;
  362. io->context = context;
  363. dispatch_io(rw, num_regions, where, dp, io, 0);
  364. return 0;
  365. }
  366. static int dp_init(struct dm_io_request *io_req, struct dpages *dp)
  367. {
  368. /* Set up dpages based on memory type */
  369. switch (io_req->mem.type) {
  370. case DM_IO_PAGE_LIST:
  371. list_dp_init(dp, io_req->mem.ptr.pl, io_req->mem.offset);
  372. break;
  373. case DM_IO_BVEC:
  374. bvec_dp_init(dp, io_req->mem.ptr.bvec);
  375. break;
  376. case DM_IO_VMA:
  377. vm_dp_init(dp, io_req->mem.ptr.vma);
  378. break;
  379. case DM_IO_KMEM:
  380. km_dp_init(dp, io_req->mem.ptr.addr);
  381. break;
  382. default:
  383. return -EINVAL;
  384. }
  385. return 0;
  386. }
  387. /*
  388. * New collapsed (a)synchronous interface.
  389. *
  390. * If the IO is asynchronous (i.e. it has notify.fn), you must either unplug
  391. * the queue with blk_unplug() some time later or set REQ_SYNC in
  392. io_req->bi_rw. If you fail to do one of these, the IO will be submitted to
  393. * the disk after q->unplug_delay, which defaults to 3ms in blk-settings.c.
  394. */
  395. int dm_io(struct dm_io_request *io_req, unsigned num_regions,
  396. struct dm_io_region *where, unsigned long *sync_error_bits)
  397. {
  398. int r;
  399. struct dpages dp;
  400. r = dp_init(io_req, &dp);
  401. if (r)
  402. return r;
  403. if (!io_req->notify.fn)
  404. return sync_io(io_req->client, num_regions, where,
  405. io_req->bi_rw, &dp, sync_error_bits);
  406. return async_io(io_req->client, num_regions, where, io_req->bi_rw,
  407. &dp, io_req->notify.fn, io_req->notify.context);
  408. }
  409. EXPORT_SYMBOL(dm_io);
  410. int __init dm_io_init(void)
  411. {
  412. _dm_io_cache = KMEM_CACHE(io, 0);
  413. if (!_dm_io_cache)
  414. return -ENOMEM;
  415. return 0;
  416. }
  417. void dm_io_exit(void)
  418. {
  419. kmem_cache_destroy(_dm_io_cache);
  420. _dm_io_cache = NULL;
  421. }