dm-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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/completion.h>
  11. #include <linux/mempool.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/dm-io.h>
  16. #define DM_MSG_PREFIX "io"
  17. #define DM_IO_MAX_REGIONS BITS_PER_LONG
  18. struct dm_io_client {
  19. mempool_t *pool;
  20. struct bio_set *bios;
  21. };
  22. /*
  23. * Aligning 'struct io' reduces the number of bits required to store
  24. * its address. Refer to store_io_and_region_in_bio() below.
  25. */
  26. struct io {
  27. unsigned long error_bits;
  28. atomic_t count;
  29. struct dm_io_client *client;
  30. io_notify_fn callback;
  31. void *context;
  32. void *vma_invalidate_address;
  33. unsigned long vma_invalidate_size;
  34. } __attribute__((aligned(DM_IO_MAX_REGIONS)));
  35. static struct kmem_cache *_dm_io_cache;
  36. /*
  37. * Create a client with mempool and bioset.
  38. */
  39. struct dm_io_client *dm_io_client_create(void)
  40. {
  41. struct dm_io_client *client;
  42. unsigned min_ios = dm_get_reserved_bio_based_ios();
  43. client = kmalloc(sizeof(*client), GFP_KERNEL);
  44. if (!client)
  45. return ERR_PTR(-ENOMEM);
  46. client->pool = mempool_create_slab_pool(min_ios, _dm_io_cache);
  47. if (!client->pool)
  48. goto bad;
  49. client->bios = bioset_create(min_ios, 0);
  50. if (!client->bios)
  51. goto bad;
  52. return client;
  53. bad:
  54. if (client->pool)
  55. mempool_destroy(client->pool);
  56. kfree(client);
  57. return ERR_PTR(-ENOMEM);
  58. }
  59. EXPORT_SYMBOL(dm_io_client_create);
  60. void dm_io_client_destroy(struct dm_io_client *client)
  61. {
  62. mempool_destroy(client->pool);
  63. bioset_free(client->bios);
  64. kfree(client);
  65. }
  66. EXPORT_SYMBOL(dm_io_client_destroy);
  67. /*-----------------------------------------------------------------
  68. * We need to keep track of which region a bio is doing io for.
  69. * To avoid a memory allocation to store just 5 or 6 bits, we
  70. * ensure the 'struct io' pointer is aligned so enough low bits are
  71. * always zero and then combine it with the region number directly in
  72. * bi_private.
  73. *---------------------------------------------------------------*/
  74. static void store_io_and_region_in_bio(struct bio *bio, struct io *io,
  75. unsigned region)
  76. {
  77. if (unlikely(!IS_ALIGNED((unsigned long)io, DM_IO_MAX_REGIONS))) {
  78. DMCRIT("Unaligned struct io pointer %p", io);
  79. BUG();
  80. }
  81. bio->bi_private = (void *)((unsigned long)io | region);
  82. }
  83. static void retrieve_io_and_region_from_bio(struct bio *bio, struct io **io,
  84. unsigned *region)
  85. {
  86. unsigned long val = (unsigned long)bio->bi_private;
  87. *io = (void *)(val & -(unsigned long)DM_IO_MAX_REGIONS);
  88. *region = val & (DM_IO_MAX_REGIONS - 1);
  89. }
  90. /*-----------------------------------------------------------------
  91. * We need an io object to keep track of the number of bios that
  92. * have been dispatched for a particular io.
  93. *---------------------------------------------------------------*/
  94. static void complete_io(struct io *io)
  95. {
  96. unsigned long error_bits = io->error_bits;
  97. io_notify_fn fn = io->callback;
  98. void *context = io->context;
  99. if (io->vma_invalidate_size)
  100. invalidate_kernel_vmap_range(io->vma_invalidate_address,
  101. io->vma_invalidate_size);
  102. mempool_free(io, io->client->pool);
  103. fn(error_bits, context);
  104. }
  105. static void dec_count(struct io *io, unsigned int region, int error)
  106. {
  107. if (error)
  108. set_bit(region, &io->error_bits);
  109. if (atomic_dec_and_test(&io->count))
  110. complete_io(io);
  111. }
  112. static void endio(struct bio *bio, int error)
  113. {
  114. struct io *io;
  115. unsigned region;
  116. if (error && bio_data_dir(bio) == READ)
  117. zero_fill_bio(bio);
  118. /*
  119. * The bio destructor in bio_put() may use the io object.
  120. */
  121. retrieve_io_and_region_from_bio(bio, &io, &region);
  122. bio_put(bio);
  123. dec_count(io, region, error);
  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. void *vma_invalidate_address;
  136. unsigned long vma_invalidate_size;
  137. };
  138. /*
  139. * Functions for getting the pages from a list.
  140. */
  141. static void list_get_page(struct dpages *dp,
  142. struct page **p, unsigned long *len, unsigned *offset)
  143. {
  144. unsigned o = dp->context_u;
  145. struct page_list *pl = (struct page_list *) dp->context_ptr;
  146. *p = pl->page;
  147. *len = PAGE_SIZE - o;
  148. *offset = o;
  149. }
  150. static void list_next_page(struct dpages *dp)
  151. {
  152. struct page_list *pl = (struct page_list *) dp->context_ptr;
  153. dp->context_ptr = pl->next;
  154. dp->context_u = 0;
  155. }
  156. static void list_dp_init(struct dpages *dp, struct page_list *pl, unsigned offset)
  157. {
  158. dp->get_page = list_get_page;
  159. dp->next_page = list_next_page;
  160. dp->context_u = offset;
  161. dp->context_ptr = pl;
  162. }
  163. /*
  164. * Functions for getting the pages from a bvec.
  165. */
  166. static void bio_get_page(struct dpages *dp, struct page **p,
  167. unsigned long *len, unsigned *offset)
  168. {
  169. struct bio_vec *bvec = dp->context_ptr;
  170. *p = bvec->bv_page;
  171. *len = bvec->bv_len - dp->context_u;
  172. *offset = bvec->bv_offset + dp->context_u;
  173. }
  174. static void bio_next_page(struct dpages *dp)
  175. {
  176. struct bio_vec *bvec = dp->context_ptr;
  177. dp->context_ptr = bvec + 1;
  178. dp->context_u = 0;
  179. }
  180. static void bio_dp_init(struct dpages *dp, struct bio *bio)
  181. {
  182. dp->get_page = bio_get_page;
  183. dp->next_page = bio_next_page;
  184. dp->context_ptr = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
  185. dp->context_u = bio->bi_iter.bi_bvec_done;
  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. /*
  210. * Functions for getting the pages from kernel memory.
  211. */
  212. static void km_get_page(struct dpages *dp, struct page **p, unsigned long *len,
  213. unsigned *offset)
  214. {
  215. *p = virt_to_page(dp->context_ptr);
  216. *offset = dp->context_u;
  217. *len = PAGE_SIZE - dp->context_u;
  218. }
  219. static void km_next_page(struct dpages *dp)
  220. {
  221. dp->context_ptr += PAGE_SIZE - dp->context_u;
  222. dp->context_u = 0;
  223. }
  224. static void km_dp_init(struct dpages *dp, void *data)
  225. {
  226. dp->get_page = km_get_page;
  227. dp->next_page = km_next_page;
  228. dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
  229. dp->context_ptr = data;
  230. }
  231. /*-----------------------------------------------------------------
  232. * IO routines that accept a list of pages.
  233. *---------------------------------------------------------------*/
  234. static void do_region(int rw, unsigned region, struct dm_io_region *where,
  235. struct dpages *dp, struct io *io)
  236. {
  237. struct bio *bio;
  238. struct page *page;
  239. unsigned long len;
  240. unsigned offset;
  241. unsigned num_bvecs;
  242. sector_t remaining = where->count;
  243. struct request_queue *q = bdev_get_queue(where->bdev);
  244. unsigned short logical_block_size = queue_logical_block_size(q);
  245. sector_t num_sectors;
  246. unsigned int uninitialized_var(special_cmd_max_sectors);
  247. /*
  248. * Reject unsupported discard and write same requests.
  249. */
  250. if (rw & REQ_DISCARD)
  251. special_cmd_max_sectors = q->limits.max_discard_sectors;
  252. else if (rw & REQ_WRITE_SAME)
  253. special_cmd_max_sectors = q->limits.max_write_same_sectors;
  254. if ((rw & (REQ_DISCARD | REQ_WRITE_SAME)) && special_cmd_max_sectors == 0) {
  255. dec_count(io, region, -EOPNOTSUPP);
  256. return;
  257. }
  258. /*
  259. * where->count may be zero if rw holds a flush and we need to
  260. * send a zero-sized flush.
  261. */
  262. do {
  263. /*
  264. * Allocate a suitably sized-bio.
  265. */
  266. if ((rw & REQ_DISCARD) || (rw & REQ_WRITE_SAME))
  267. num_bvecs = 1;
  268. else
  269. num_bvecs = min_t(int, bio_get_nr_vecs(where->bdev),
  270. dm_sector_div_up(remaining, (PAGE_SIZE >> SECTOR_SHIFT)));
  271. bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, io->client->bios);
  272. bio->bi_iter.bi_sector = where->sector + (where->count - remaining);
  273. bio->bi_bdev = where->bdev;
  274. bio->bi_end_io = endio;
  275. store_io_and_region_in_bio(bio, io, region);
  276. if (rw & REQ_DISCARD) {
  277. num_sectors = min_t(sector_t, special_cmd_max_sectors, remaining);
  278. bio->bi_iter.bi_size = num_sectors << SECTOR_SHIFT;
  279. remaining -= num_sectors;
  280. } else if (rw & REQ_WRITE_SAME) {
  281. /*
  282. * WRITE SAME only uses a single page.
  283. */
  284. dp->get_page(dp, &page, &len, &offset);
  285. bio_add_page(bio, page, logical_block_size, offset);
  286. num_sectors = min_t(sector_t, special_cmd_max_sectors, remaining);
  287. bio->bi_iter.bi_size = num_sectors << SECTOR_SHIFT;
  288. offset = 0;
  289. remaining -= num_sectors;
  290. dp->next_page(dp);
  291. } else while (remaining) {
  292. /*
  293. * Try and add as many pages as possible.
  294. */
  295. dp->get_page(dp, &page, &len, &offset);
  296. len = min(len, to_bytes(remaining));
  297. if (!bio_add_page(bio, page, len, offset))
  298. break;
  299. offset = 0;
  300. remaining -= to_sector(len);
  301. dp->next_page(dp);
  302. }
  303. atomic_inc(&io->count);
  304. submit_bio(rw, bio);
  305. } while (remaining);
  306. }
  307. static void dispatch_io(int rw, unsigned int num_regions,
  308. struct dm_io_region *where, struct dpages *dp,
  309. struct io *io, int sync)
  310. {
  311. int i;
  312. struct dpages old_pages = *dp;
  313. BUG_ON(num_regions > DM_IO_MAX_REGIONS);
  314. if (sync)
  315. rw |= REQ_SYNC;
  316. /*
  317. * For multiple regions we need to be careful to rewind
  318. * the dp object for each call to do_region.
  319. */
  320. for (i = 0; i < num_regions; i++) {
  321. *dp = old_pages;
  322. if (where[i].count || (rw & REQ_FLUSH))
  323. do_region(rw, i, where + i, dp, io);
  324. }
  325. /*
  326. * Drop the extra reference that we were holding to avoid
  327. * the io being completed too early.
  328. */
  329. dec_count(io, 0, 0);
  330. }
  331. struct sync_io {
  332. unsigned long error_bits;
  333. struct completion wait;
  334. };
  335. static void sync_io_complete(unsigned long error, void *context)
  336. {
  337. struct sync_io *sio = context;
  338. sio->error_bits = error;
  339. complete(&sio->wait);
  340. }
  341. static int sync_io(struct dm_io_client *client, unsigned int num_regions,
  342. struct dm_io_region *where, int rw, struct dpages *dp,
  343. unsigned long *error_bits)
  344. {
  345. struct io *io;
  346. struct sync_io sio;
  347. if (num_regions > 1 && (rw & RW_MASK) != WRITE) {
  348. WARN_ON(1);
  349. return -EIO;
  350. }
  351. init_completion(&sio.wait);
  352. io = mempool_alloc(client->pool, GFP_NOIO);
  353. io->error_bits = 0;
  354. atomic_set(&io->count, 1); /* see dispatch_io() */
  355. io->client = client;
  356. io->callback = sync_io_complete;
  357. io->context = &sio;
  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, 1);
  361. wait_for_completion_io(&sio.wait);
  362. if (error_bits)
  363. *error_bits = sio.error_bits;
  364. return sio.error_bits ? -EIO : 0;
  365. }
  366. static int async_io(struct dm_io_client *client, unsigned int num_regions,
  367. struct dm_io_region *where, int rw, struct dpages *dp,
  368. io_notify_fn fn, void *context)
  369. {
  370. struct io *io;
  371. if (num_regions > 1 && (rw & RW_MASK) != WRITE) {
  372. WARN_ON(1);
  373. fn(1, context);
  374. return -EIO;
  375. }
  376. io = mempool_alloc(client->pool, GFP_NOIO);
  377. io->error_bits = 0;
  378. atomic_set(&io->count, 1); /* see dispatch_io() */
  379. io->client = client;
  380. io->callback = fn;
  381. io->context = context;
  382. io->vma_invalidate_address = dp->vma_invalidate_address;
  383. io->vma_invalidate_size = dp->vma_invalidate_size;
  384. dispatch_io(rw, num_regions, where, dp, io, 0);
  385. return 0;
  386. }
  387. static int dp_init(struct dm_io_request *io_req, struct dpages *dp,
  388. unsigned long size)
  389. {
  390. /* Set up dpages based on memory type */
  391. dp->vma_invalidate_address = NULL;
  392. dp->vma_invalidate_size = 0;
  393. switch (io_req->mem.type) {
  394. case DM_IO_PAGE_LIST:
  395. list_dp_init(dp, io_req->mem.ptr.pl, io_req->mem.offset);
  396. break;
  397. case DM_IO_BIO:
  398. bio_dp_init(dp, io_req->mem.ptr.bio);
  399. break;
  400. case DM_IO_VMA:
  401. flush_kernel_vmap_range(io_req->mem.ptr.vma, size);
  402. if ((io_req->bi_rw & RW_MASK) == READ) {
  403. dp->vma_invalidate_address = io_req->mem.ptr.vma;
  404. dp->vma_invalidate_size = size;
  405. }
  406. vm_dp_init(dp, io_req->mem.ptr.vma);
  407. break;
  408. case DM_IO_KMEM:
  409. km_dp_init(dp, io_req->mem.ptr.addr);
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. return 0;
  415. }
  416. /*
  417. * New collapsed (a)synchronous interface.
  418. *
  419. * If the IO is asynchronous (i.e. it has notify.fn), you must either unplug
  420. * the queue with blk_unplug() some time later or set REQ_SYNC in io_req->bi_rw.
  421. * If you fail to do one of these, the IO will be submitted to the disk after
  422. * q->unplug_delay, which defaults to 3ms in blk-settings.c.
  423. */
  424. int dm_io(struct dm_io_request *io_req, unsigned num_regions,
  425. struct dm_io_region *where, unsigned long *sync_error_bits)
  426. {
  427. int r;
  428. struct dpages dp;
  429. r = dp_init(io_req, &dp, (unsigned long)where->count << SECTOR_SHIFT);
  430. if (r)
  431. return r;
  432. if (!io_req->notify.fn)
  433. return sync_io(io_req->client, num_regions, where,
  434. io_req->bi_rw, &dp, sync_error_bits);
  435. return async_io(io_req->client, num_regions, where, io_req->bi_rw,
  436. &dp, io_req->notify.fn, io_req->notify.context);
  437. }
  438. EXPORT_SYMBOL(dm_io);
  439. int __init dm_io_init(void)
  440. {
  441. _dm_io_cache = KMEM_CACHE(io, 0);
  442. if (!_dm_io_cache)
  443. return -ENOMEM;
  444. return 0;
  445. }
  446. void dm_io_exit(void)
  447. {
  448. kmem_cache_destroy(_dm_io_cache);
  449. _dm_io_cache = NULL;
  450. }