dm-io.c 12 KB

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