dm-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. atomic_inc(&io->count);
  259. dec_count(io, region, -EOPNOTSUPP);
  260. return;
  261. }
  262. /*
  263. * where->count may be zero if op holds a flush and we need to
  264. * send a zero-sized flush.
  265. */
  266. do {
  267. /*
  268. * Allocate a suitably sized-bio.
  269. */
  270. if ((op == REQ_OP_DISCARD) || (op == REQ_OP_WRITE_SAME))
  271. num_bvecs = 1;
  272. else
  273. num_bvecs = min_t(int, BIO_MAX_PAGES,
  274. dm_sector_div_up(remaining, (PAGE_SIZE >> SECTOR_SHIFT)));
  275. bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, io->client->bios);
  276. bio->bi_iter.bi_sector = where->sector + (where->count - remaining);
  277. bio->bi_bdev = where->bdev;
  278. bio->bi_end_io = endio;
  279. bio_set_op_attrs(bio, op, op_flags);
  280. store_io_and_region_in_bio(bio, io, region);
  281. if (op == REQ_OP_DISCARD) {
  282. num_sectors = min_t(sector_t, special_cmd_max_sectors, remaining);
  283. bio->bi_iter.bi_size = num_sectors << SECTOR_SHIFT;
  284. remaining -= num_sectors;
  285. } else if (op == REQ_OP_WRITE_SAME) {
  286. /*
  287. * WRITE SAME only uses a single page.
  288. */
  289. dp->get_page(dp, &page, &len, &offset);
  290. bio_add_page(bio, page, logical_block_size, offset);
  291. num_sectors = min_t(sector_t, special_cmd_max_sectors, remaining);
  292. bio->bi_iter.bi_size = num_sectors << SECTOR_SHIFT;
  293. offset = 0;
  294. remaining -= num_sectors;
  295. dp->next_page(dp);
  296. } else while (remaining) {
  297. /*
  298. * Try and add as many pages as possible.
  299. */
  300. dp->get_page(dp, &page, &len, &offset);
  301. len = min(len, to_bytes(remaining));
  302. if (!bio_add_page(bio, page, len, offset))
  303. break;
  304. offset = 0;
  305. remaining -= to_sector(len);
  306. dp->next_page(dp);
  307. }
  308. atomic_inc(&io->count);
  309. submit_bio(bio);
  310. } while (remaining);
  311. }
  312. static void dispatch_io(int op, int op_flags, unsigned int num_regions,
  313. struct dm_io_region *where, struct dpages *dp,
  314. struct io *io, int sync)
  315. {
  316. int i;
  317. struct dpages old_pages = *dp;
  318. BUG_ON(num_regions > DM_IO_MAX_REGIONS);
  319. if (sync)
  320. op_flags |= REQ_SYNC;
  321. /*
  322. * For multiple regions we need to be careful to rewind
  323. * the dp object for each call to do_region.
  324. */
  325. for (i = 0; i < num_regions; i++) {
  326. *dp = old_pages;
  327. if (where[i].count || (op_flags & REQ_PREFLUSH))
  328. do_region(op, op_flags, i, where + i, dp, io);
  329. }
  330. /*
  331. * Drop the extra reference that we were holding to avoid
  332. * the io being completed too early.
  333. */
  334. dec_count(io, 0, 0);
  335. }
  336. struct sync_io {
  337. unsigned long error_bits;
  338. struct completion wait;
  339. };
  340. static void sync_io_complete(unsigned long error, void *context)
  341. {
  342. struct sync_io *sio = context;
  343. sio->error_bits = error;
  344. complete(&sio->wait);
  345. }
  346. static int sync_io(struct dm_io_client *client, unsigned int num_regions,
  347. struct dm_io_region *where, int op, int op_flags,
  348. struct dpages *dp, unsigned long *error_bits)
  349. {
  350. struct io *io;
  351. struct sync_io sio;
  352. if (num_regions > 1 && !op_is_write(op)) {
  353. WARN_ON(1);
  354. return -EIO;
  355. }
  356. init_completion(&sio.wait);
  357. io = mempool_alloc(client->pool, GFP_NOIO);
  358. io->error_bits = 0;
  359. atomic_set(&io->count, 1); /* see dispatch_io() */
  360. io->client = client;
  361. io->callback = sync_io_complete;
  362. io->context = &sio;
  363. io->vma_invalidate_address = dp->vma_invalidate_address;
  364. io->vma_invalidate_size = dp->vma_invalidate_size;
  365. dispatch_io(op, op_flags, num_regions, where, dp, io, 1);
  366. wait_for_completion_io(&sio.wait);
  367. if (error_bits)
  368. *error_bits = sio.error_bits;
  369. return sio.error_bits ? -EIO : 0;
  370. }
  371. static int async_io(struct dm_io_client *client, unsigned int num_regions,
  372. struct dm_io_region *where, int op, int op_flags,
  373. struct dpages *dp, io_notify_fn fn, void *context)
  374. {
  375. struct io *io;
  376. if (num_regions > 1 && !op_is_write(op)) {
  377. WARN_ON(1);
  378. fn(1, context);
  379. return -EIO;
  380. }
  381. io = mempool_alloc(client->pool, GFP_NOIO);
  382. io->error_bits = 0;
  383. atomic_set(&io->count, 1); /* see dispatch_io() */
  384. io->client = client;
  385. io->callback = fn;
  386. io->context = context;
  387. io->vma_invalidate_address = dp->vma_invalidate_address;
  388. io->vma_invalidate_size = dp->vma_invalidate_size;
  389. dispatch_io(op, op_flags, num_regions, where, dp, io, 0);
  390. return 0;
  391. }
  392. static int dp_init(struct dm_io_request *io_req, struct dpages *dp,
  393. unsigned long size)
  394. {
  395. /* Set up dpages based on memory type */
  396. dp->vma_invalidate_address = NULL;
  397. dp->vma_invalidate_size = 0;
  398. switch (io_req->mem.type) {
  399. case DM_IO_PAGE_LIST:
  400. list_dp_init(dp, io_req->mem.ptr.pl, io_req->mem.offset);
  401. break;
  402. case DM_IO_BIO:
  403. bio_dp_init(dp, io_req->mem.ptr.bio);
  404. break;
  405. case DM_IO_VMA:
  406. flush_kernel_vmap_range(io_req->mem.ptr.vma, size);
  407. if (io_req->bi_op == REQ_OP_READ) {
  408. dp->vma_invalidate_address = io_req->mem.ptr.vma;
  409. dp->vma_invalidate_size = size;
  410. }
  411. vm_dp_init(dp, io_req->mem.ptr.vma);
  412. break;
  413. case DM_IO_KMEM:
  414. km_dp_init(dp, io_req->mem.ptr.addr);
  415. break;
  416. default:
  417. return -EINVAL;
  418. }
  419. return 0;
  420. }
  421. /*
  422. * New collapsed (a)synchronous interface.
  423. *
  424. * If the IO is asynchronous (i.e. it has notify.fn), you must either unplug
  425. * the queue with blk_unplug() some time later or set REQ_SYNC in
  426. * io_req->bi_opf. If you fail to do one of these, the IO will be submitted to
  427. * the disk after q->unplug_delay, which defaults to 3ms in blk-settings.c.
  428. */
  429. int dm_io(struct dm_io_request *io_req, unsigned num_regions,
  430. struct dm_io_region *where, unsigned long *sync_error_bits)
  431. {
  432. int r;
  433. struct dpages dp;
  434. r = dp_init(io_req, &dp, (unsigned long)where->count << SECTOR_SHIFT);
  435. if (r)
  436. return r;
  437. if (!io_req->notify.fn)
  438. return sync_io(io_req->client, num_regions, where,
  439. io_req->bi_op, io_req->bi_op_flags, &dp,
  440. sync_error_bits);
  441. return async_io(io_req->client, num_regions, where, io_req->bi_op,
  442. io_req->bi_op_flags, &dp, io_req->notify.fn,
  443. io_req->notify.context);
  444. }
  445. EXPORT_SYMBOL(dm_io);
  446. int __init dm_io_init(void)
  447. {
  448. _dm_io_cache = KMEM_CACHE(io, 0);
  449. if (!_dm_io_cache)
  450. return -ENOMEM;
  451. return 0;
  452. }
  453. void dm_io_exit(void)
  454. {
  455. kmem_cache_destroy(_dm_io_cache);
  456. _dm_io_cache = NULL;
  457. }