dm-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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-core.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. mempool_destroy(client->pool);
  55. kfree(client);
  56. return ERR_PTR(-ENOMEM);
  57. }
  58. EXPORT_SYMBOL(dm_io_client_create);
  59. void dm_io_client_destroy(struct dm_io_client *client)
  60. {
  61. mempool_destroy(client->pool);
  62. bioset_free(client->bios);
  63. kfree(client);
  64. }
  65. EXPORT_SYMBOL(dm_io_client_destroy);
  66. /*-----------------------------------------------------------------
  67. * We need to keep track of which region a bio is doing io for.
  68. * To avoid a memory allocation to store just 5 or 6 bits, we
  69. * ensure the 'struct io' pointer is aligned so enough low bits are
  70. * always zero and then combine it with the region number directly in
  71. * bi_private.
  72. *---------------------------------------------------------------*/
  73. static void store_io_and_region_in_bio(struct bio *bio, struct io *io,
  74. unsigned region)
  75. {
  76. if (unlikely(!IS_ALIGNED((unsigned long)io, DM_IO_MAX_REGIONS))) {
  77. DMCRIT("Unaligned struct io pointer %p", io);
  78. BUG();
  79. }
  80. bio->bi_private = (void *)((unsigned long)io | region);
  81. }
  82. static void retrieve_io_and_region_from_bio(struct bio *bio, struct io **io,
  83. unsigned *region)
  84. {
  85. unsigned long val = (unsigned long)bio->bi_private;
  86. *io = (void *)(val & -(unsigned long)DM_IO_MAX_REGIONS);
  87. *region = val & (DM_IO_MAX_REGIONS - 1);
  88. }
  89. /*-----------------------------------------------------------------
  90. * We need an io object to keep track of the number of bios that
  91. * have been dispatched for a particular io.
  92. *---------------------------------------------------------------*/
  93. static void complete_io(struct io *io)
  94. {
  95. unsigned long error_bits = io->error_bits;
  96. io_notify_fn fn = io->callback;
  97. void *context = io->context;
  98. if (io->vma_invalidate_size)
  99. invalidate_kernel_vmap_range(io->vma_invalidate_address,
  100. io->vma_invalidate_size);
  101. mempool_free(io, io->client->pool);
  102. fn(error_bits, context);
  103. }
  104. static void dec_count(struct io *io, unsigned int region, int error)
  105. {
  106. if (error)
  107. set_bit(region, &io->error_bits);
  108. if (atomic_dec_and_test(&io->count))
  109. complete_io(io);
  110. }
  111. static void endio(struct bio *bio)
  112. {
  113. struct io *io;
  114. unsigned region;
  115. int error;
  116. if (bio->bi_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. error = bio->bi_error;
  123. bio_put(bio);
  124. dec_count(io, region, error);
  125. }
  126. /*-----------------------------------------------------------------
  127. * These little objects provide an abstraction for getting a new
  128. * destination page for io.
  129. *---------------------------------------------------------------*/
  130. struct dpages {
  131. void (*get_page)(struct dpages *dp,
  132. struct page **p, unsigned long *len, unsigned *offset);
  133. void (*next_page)(struct dpages *dp);
  134. unsigned context_u;
  135. void *context_ptr;
  136. void *vma_invalidate_address;
  137. unsigned long vma_invalidate_size;
  138. };
  139. /*
  140. * Functions for getting the pages from a list.
  141. */
  142. static void list_get_page(struct dpages *dp,
  143. struct page **p, unsigned long *len, unsigned *offset)
  144. {
  145. unsigned o = dp->context_u;
  146. struct page_list *pl = (struct page_list *) dp->context_ptr;
  147. *p = pl->page;
  148. *len = PAGE_SIZE - o;
  149. *offset = o;
  150. }
  151. static void list_next_page(struct dpages *dp)
  152. {
  153. struct page_list *pl = (struct page_list *) dp->context_ptr;
  154. dp->context_ptr = pl->next;
  155. dp->context_u = 0;
  156. }
  157. static void list_dp_init(struct dpages *dp, struct page_list *pl, unsigned offset)
  158. {
  159. dp->get_page = list_get_page;
  160. dp->next_page = list_next_page;
  161. dp->context_u = offset;
  162. dp->context_ptr = pl;
  163. }
  164. /*
  165. * Functions for getting the pages from a bvec.
  166. */
  167. static void bio_get_page(struct dpages *dp, struct page **p,
  168. unsigned long *len, unsigned *offset)
  169. {
  170. struct bio_vec *bvec = dp->context_ptr;
  171. *p = bvec->bv_page;
  172. *len = bvec->bv_len - dp->context_u;
  173. *offset = bvec->bv_offset + dp->context_u;
  174. }
  175. static void bio_next_page(struct dpages *dp)
  176. {
  177. struct bio_vec *bvec = dp->context_ptr;
  178. dp->context_ptr = bvec + 1;
  179. dp->context_u = 0;
  180. }
  181. static void bio_dp_init(struct dpages *dp, struct bio *bio)
  182. {
  183. dp->get_page = bio_get_page;
  184. dp->next_page = bio_next_page;
  185. dp->context_ptr = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
  186. dp->context_u = bio->bi_iter.bi_bvec_done;
  187. }
  188. /*
  189. * Functions for getting the pages from a VMA.
  190. */
  191. static void vm_get_page(struct dpages *dp,
  192. struct page **p, unsigned long *len, unsigned *offset)
  193. {
  194. *p = vmalloc_to_page(dp->context_ptr);
  195. *offset = dp->context_u;
  196. *len = PAGE_SIZE - dp->context_u;
  197. }
  198. static void vm_next_page(struct dpages *dp)
  199. {
  200. dp->context_ptr += PAGE_SIZE - dp->context_u;
  201. dp->context_u = 0;
  202. }
  203. static void vm_dp_init(struct dpages *dp, void *data)
  204. {
  205. dp->get_page = vm_get_page;
  206. dp->next_page = vm_next_page;
  207. dp->context_u = offset_in_page(data);
  208. dp->context_ptr = data;
  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 = offset_in_page(data);
  230. dp->context_ptr = data;
  231. }
  232. /*-----------------------------------------------------------------
  233. * IO routines that accept a list of pages.
  234. *---------------------------------------------------------------*/
  235. static void do_region(int op, int op_flags, unsigned region,
  236. struct dm_io_region *where, struct dpages *dp,
  237. struct io *io)
  238. {
  239. struct bio *bio;
  240. struct page *page;
  241. unsigned long len;
  242. unsigned offset;
  243. unsigned num_bvecs;
  244. sector_t remaining = where->count;
  245. struct request_queue *q = bdev_get_queue(where->bdev);
  246. unsigned short logical_block_size = queue_logical_block_size(q);
  247. sector_t num_sectors;
  248. unsigned int uninitialized_var(special_cmd_max_sectors);
  249. /*
  250. * Reject unsupported discard and write same requests.
  251. */
  252. if (op == REQ_OP_DISCARD)
  253. special_cmd_max_sectors = q->limits.max_discard_sectors;
  254. else if (op == REQ_OP_WRITE_SAME)
  255. special_cmd_max_sectors = q->limits.max_write_same_sectors;
  256. if ((op == REQ_OP_DISCARD || op == REQ_OP_WRITE_SAME) &&
  257. special_cmd_max_sectors == 0) {
  258. dec_count(io, region, -EOPNOTSUPP);
  259. return;
  260. }
  261. /*
  262. * where->count may be zero if op holds a flush and we need to
  263. * send a zero-sized flush.
  264. */
  265. do {
  266. /*
  267. * Allocate a suitably sized-bio.
  268. */
  269. if ((op == REQ_OP_DISCARD) || (op == REQ_OP_WRITE_SAME))
  270. num_bvecs = 1;
  271. else
  272. num_bvecs = min_t(int, BIO_MAX_PAGES,
  273. dm_sector_div_up(remaining, (PAGE_SIZE >> SECTOR_SHIFT)));
  274. bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, io->client->bios);
  275. bio->bi_iter.bi_sector = where->sector + (where->count - remaining);
  276. bio->bi_bdev = where->bdev;
  277. bio->bi_end_io = endio;
  278. bio_set_op_attrs(bio, op, op_flags);
  279. store_io_and_region_in_bio(bio, io, region);
  280. if (op == REQ_OP_DISCARD) {
  281. num_sectors = min_t(sector_t, special_cmd_max_sectors, remaining);
  282. bio->bi_iter.bi_size = num_sectors << SECTOR_SHIFT;
  283. remaining -= num_sectors;
  284. } else if (op == REQ_OP_WRITE_SAME) {
  285. /*
  286. * WRITE SAME only uses a single page.
  287. */
  288. dp->get_page(dp, &page, &len, &offset);
  289. bio_add_page(bio, page, logical_block_size, offset);
  290. num_sectors = min_t(sector_t, special_cmd_max_sectors, remaining);
  291. bio->bi_iter.bi_size = num_sectors << SECTOR_SHIFT;
  292. offset = 0;
  293. remaining -= num_sectors;
  294. dp->next_page(dp);
  295. } else while (remaining) {
  296. /*
  297. * Try and add as many pages as possible.
  298. */
  299. dp->get_page(dp, &page, &len, &offset);
  300. len = min(len, to_bytes(remaining));
  301. if (!bio_add_page(bio, page, len, offset))
  302. break;
  303. offset = 0;
  304. remaining -= to_sector(len);
  305. dp->next_page(dp);
  306. }
  307. atomic_inc(&io->count);
  308. submit_bio(bio);
  309. } while (remaining);
  310. }
  311. static void dispatch_io(int op, int op_flags, unsigned int num_regions,
  312. struct dm_io_region *where, struct dpages *dp,
  313. struct io *io, int sync)
  314. {
  315. int i;
  316. struct dpages old_pages = *dp;
  317. BUG_ON(num_regions > DM_IO_MAX_REGIONS);
  318. if (sync)
  319. op_flags |= REQ_SYNC;
  320. /*
  321. * For multiple regions we need to be careful to rewind
  322. * the dp object for each call to do_region.
  323. */
  324. for (i = 0; i < num_regions; i++) {
  325. *dp = old_pages;
  326. if (where[i].count || (op_flags & REQ_PREFLUSH))
  327. do_region(op, op_flags, i, where + i, dp, io);
  328. }
  329. /*
  330. * Drop the extra reference that we were holding to avoid
  331. * the io being completed too early.
  332. */
  333. dec_count(io, 0, 0);
  334. }
  335. struct sync_io {
  336. unsigned long error_bits;
  337. struct completion wait;
  338. };
  339. static void sync_io_complete(unsigned long error, void *context)
  340. {
  341. struct sync_io *sio = context;
  342. sio->error_bits = error;
  343. complete(&sio->wait);
  344. }
  345. static int sync_io(struct dm_io_client *client, unsigned int num_regions,
  346. struct dm_io_region *where, int op, int op_flags,
  347. struct dpages *dp, unsigned long *error_bits)
  348. {
  349. struct io *io;
  350. struct sync_io sio;
  351. if (num_regions > 1 && !op_is_write(op)) {
  352. WARN_ON(1);
  353. return -EIO;
  354. }
  355. init_completion(&sio.wait);
  356. io = mempool_alloc(client->pool, GFP_NOIO);
  357. io->error_bits = 0;
  358. atomic_set(&io->count, 1); /* see dispatch_io() */
  359. io->client = client;
  360. io->callback = sync_io_complete;
  361. io->context = &sio;
  362. io->vma_invalidate_address = dp->vma_invalidate_address;
  363. io->vma_invalidate_size = dp->vma_invalidate_size;
  364. dispatch_io(op, op_flags, num_regions, where, dp, io, 1);
  365. wait_for_completion_io(&sio.wait);
  366. if (error_bits)
  367. *error_bits = sio.error_bits;
  368. return sio.error_bits ? -EIO : 0;
  369. }
  370. static int async_io(struct dm_io_client *client, unsigned int num_regions,
  371. struct dm_io_region *where, int op, int op_flags,
  372. struct dpages *dp, io_notify_fn fn, void *context)
  373. {
  374. struct io *io;
  375. if (num_regions > 1 && !op_is_write(op)) {
  376. WARN_ON(1);
  377. fn(1, context);
  378. return -EIO;
  379. }
  380. io = mempool_alloc(client->pool, GFP_NOIO);
  381. io->error_bits = 0;
  382. atomic_set(&io->count, 1); /* see dispatch_io() */
  383. io->client = client;
  384. io->callback = fn;
  385. io->context = context;
  386. io->vma_invalidate_address = dp->vma_invalidate_address;
  387. io->vma_invalidate_size = dp->vma_invalidate_size;
  388. dispatch_io(op, op_flags, num_regions, where, dp, io, 0);
  389. return 0;
  390. }
  391. static int dp_init(struct dm_io_request *io_req, struct dpages *dp,
  392. unsigned long size)
  393. {
  394. /* Set up dpages based on memory type */
  395. dp->vma_invalidate_address = NULL;
  396. dp->vma_invalidate_size = 0;
  397. switch (io_req->mem.type) {
  398. case DM_IO_PAGE_LIST:
  399. list_dp_init(dp, io_req->mem.ptr.pl, io_req->mem.offset);
  400. break;
  401. case DM_IO_BIO:
  402. bio_dp_init(dp, io_req->mem.ptr.bio);
  403. break;
  404. case DM_IO_VMA:
  405. flush_kernel_vmap_range(io_req->mem.ptr.vma, size);
  406. if (io_req->bi_op == REQ_OP_READ) {
  407. dp->vma_invalidate_address = io_req->mem.ptr.vma;
  408. dp->vma_invalidate_size = size;
  409. }
  410. vm_dp_init(dp, io_req->mem.ptr.vma);
  411. break;
  412. case DM_IO_KMEM:
  413. km_dp_init(dp, io_req->mem.ptr.addr);
  414. break;
  415. default:
  416. return -EINVAL;
  417. }
  418. return 0;
  419. }
  420. /*
  421. * New collapsed (a)synchronous interface.
  422. *
  423. * If the IO is asynchronous (i.e. it has notify.fn), you must either unplug
  424. * the queue with blk_unplug() some time later or set REQ_SYNC in
  425. * io_req->bi_opf. If you fail to do one of these, the IO will be submitted to
  426. * the disk after q->unplug_delay, which defaults to 3ms in blk-settings.c.
  427. */
  428. int dm_io(struct dm_io_request *io_req, unsigned num_regions,
  429. struct dm_io_region *where, unsigned long *sync_error_bits)
  430. {
  431. int r;
  432. struct dpages dp;
  433. r = dp_init(io_req, &dp, (unsigned long)where->count << SECTOR_SHIFT);
  434. if (r)
  435. return r;
  436. if (!io_req->notify.fn)
  437. return sync_io(io_req->client, num_regions, where,
  438. io_req->bi_op, io_req->bi_op_flags, &dp,
  439. sync_error_bits);
  440. return async_io(io_req->client, num_regions, where, io_req->bi_op,
  441. io_req->bi_op_flags, &dp, io_req->notify.fn,
  442. io_req->notify.context);
  443. }
  444. EXPORT_SYMBOL(dm_io);
  445. int __init dm_io_init(void)
  446. {
  447. _dm_io_cache = KMEM_CACHE(io, 0);
  448. if (!_dm_io_cache)
  449. return -ENOMEM;
  450. return 0;
  451. }
  452. void dm_io_exit(void)
  453. {
  454. kmem_cache_destroy(_dm_io_cache);
  455. _dm_io_cache = NULL;
  456. }