dm-io.c 12 KB

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