dm-io.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. int ret;
  44. client = kzalloc(sizeof(*client), GFP_KERNEL);
  45. if (!client)
  46. return ERR_PTR(-ENOMEM);
  47. ret = mempool_init_slab_pool(&client->pool, min_ios, _dm_io_cache);
  48. if (ret)
  49. goto bad;
  50. ret = bioset_init(&client->bios, min_ios, 0, BIOSET_NEED_BVECS);
  51. if (ret)
  52. goto bad;
  53. return client;
  54. bad:
  55. mempool_exit(&client->pool);
  56. kfree(client);
  57. return ERR_PTR(ret);
  58. }
  59. EXPORT_SYMBOL(dm_io_client_create);
  60. void dm_io_client_destroy(struct dm_io_client *client)
  61. {
  62. mempool_exit(&client->pool);
  63. bioset_exit(&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, blk_status_t 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)
  113. {
  114. struct io *io;
  115. unsigned region;
  116. blk_status_t error;
  117. if (bio->bi_status && bio_data_dir(bio) == READ)
  118. zero_fill_bio(bio);
  119. /*
  120. * The bio destructor in bio_put() may use the io object.
  121. */
  122. retrieve_io_and_region_from_bio(bio, &io, &region);
  123. error = bio->bi_status;
  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. union {
  136. unsigned context_u;
  137. struct bvec_iter context_bi;
  138. };
  139. void *context_ptr;
  140. void *vma_invalidate_address;
  141. unsigned long vma_invalidate_size;
  142. };
  143. /*
  144. * Functions for getting the pages from a list.
  145. */
  146. static void list_get_page(struct dpages *dp,
  147. struct page **p, unsigned long *len, unsigned *offset)
  148. {
  149. unsigned o = dp->context_u;
  150. struct page_list *pl = (struct page_list *) dp->context_ptr;
  151. *p = pl->page;
  152. *len = PAGE_SIZE - o;
  153. *offset = o;
  154. }
  155. static void list_next_page(struct dpages *dp)
  156. {
  157. struct page_list *pl = (struct page_list *) dp->context_ptr;
  158. dp->context_ptr = pl->next;
  159. dp->context_u = 0;
  160. }
  161. static void list_dp_init(struct dpages *dp, struct page_list *pl, unsigned offset)
  162. {
  163. dp->get_page = list_get_page;
  164. dp->next_page = list_next_page;
  165. dp->context_u = offset;
  166. dp->context_ptr = pl;
  167. }
  168. /*
  169. * Functions for getting the pages from a bvec.
  170. */
  171. static void bio_get_page(struct dpages *dp, struct page **p,
  172. unsigned long *len, unsigned *offset)
  173. {
  174. struct bio_vec bvec = bvec_iter_bvec((struct bio_vec *)dp->context_ptr,
  175. dp->context_bi);
  176. *p = bvec.bv_page;
  177. *len = bvec.bv_len;
  178. *offset = bvec.bv_offset;
  179. /* avoid figuring it out again in bio_next_page() */
  180. dp->context_bi.bi_sector = (sector_t)bvec.bv_len;
  181. }
  182. static void bio_next_page(struct dpages *dp)
  183. {
  184. unsigned int len = (unsigned int)dp->context_bi.bi_sector;
  185. bvec_iter_advance((struct bio_vec *)dp->context_ptr,
  186. &dp->context_bi, len);
  187. }
  188. static void bio_dp_init(struct dpages *dp, struct bio *bio)
  189. {
  190. dp->get_page = bio_get_page;
  191. dp->next_page = bio_next_page;
  192. /*
  193. * We just use bvec iterator to retrieve pages, so it is ok to
  194. * access the bvec table directly here
  195. */
  196. dp->context_ptr = bio->bi_io_vec;
  197. dp->context_bi = bio->bi_iter;
  198. }
  199. /*
  200. * Functions for getting the pages from a VMA.
  201. */
  202. static void vm_get_page(struct dpages *dp,
  203. struct page **p, unsigned long *len, unsigned *offset)
  204. {
  205. *p = vmalloc_to_page(dp->context_ptr);
  206. *offset = dp->context_u;
  207. *len = PAGE_SIZE - dp->context_u;
  208. }
  209. static void vm_next_page(struct dpages *dp)
  210. {
  211. dp->context_ptr += PAGE_SIZE - dp->context_u;
  212. dp->context_u = 0;
  213. }
  214. static void vm_dp_init(struct dpages *dp, void *data)
  215. {
  216. dp->get_page = vm_get_page;
  217. dp->next_page = vm_next_page;
  218. dp->context_u = offset_in_page(data);
  219. dp->context_ptr = data;
  220. }
  221. /*
  222. * Functions for getting the pages from kernel memory.
  223. */
  224. static void km_get_page(struct dpages *dp, struct page **p, unsigned long *len,
  225. unsigned *offset)
  226. {
  227. *p = virt_to_page(dp->context_ptr);
  228. *offset = dp->context_u;
  229. *len = PAGE_SIZE - dp->context_u;
  230. }
  231. static void km_next_page(struct dpages *dp)
  232. {
  233. dp->context_ptr += PAGE_SIZE - dp->context_u;
  234. dp->context_u = 0;
  235. }
  236. static void km_dp_init(struct dpages *dp, void *data)
  237. {
  238. dp->get_page = km_get_page;
  239. dp->next_page = km_next_page;
  240. dp->context_u = offset_in_page(data);
  241. dp->context_ptr = data;
  242. }
  243. /*-----------------------------------------------------------------
  244. * IO routines that accept a list of pages.
  245. *---------------------------------------------------------------*/
  246. static void do_region(int op, int op_flags, unsigned region,
  247. struct dm_io_region *where, struct dpages *dp,
  248. struct io *io)
  249. {
  250. struct bio *bio;
  251. struct page *page;
  252. unsigned long len;
  253. unsigned offset;
  254. unsigned num_bvecs;
  255. sector_t remaining = where->count;
  256. struct request_queue *q = bdev_get_queue(where->bdev);
  257. unsigned short logical_block_size = queue_logical_block_size(q);
  258. sector_t num_sectors;
  259. unsigned int uninitialized_var(special_cmd_max_sectors);
  260. /*
  261. * Reject unsupported discard and write same requests.
  262. */
  263. if (op == REQ_OP_DISCARD)
  264. special_cmd_max_sectors = q->limits.max_discard_sectors;
  265. else if (op == REQ_OP_WRITE_ZEROES)
  266. special_cmd_max_sectors = q->limits.max_write_zeroes_sectors;
  267. else if (op == REQ_OP_WRITE_SAME)
  268. special_cmd_max_sectors = q->limits.max_write_same_sectors;
  269. if ((op == REQ_OP_DISCARD || op == REQ_OP_WRITE_ZEROES ||
  270. op == REQ_OP_WRITE_SAME) && special_cmd_max_sectors == 0) {
  271. atomic_inc(&io->count);
  272. dec_count(io, region, BLK_STS_NOTSUPP);
  273. return;
  274. }
  275. /*
  276. * where->count may be zero if op holds a flush and we need to
  277. * send a zero-sized flush.
  278. */
  279. do {
  280. /*
  281. * Allocate a suitably sized-bio.
  282. */
  283. switch (op) {
  284. case REQ_OP_DISCARD:
  285. case REQ_OP_WRITE_ZEROES:
  286. num_bvecs = 0;
  287. break;
  288. case REQ_OP_WRITE_SAME:
  289. num_bvecs = 1;
  290. break;
  291. default:
  292. num_bvecs = min_t(int, BIO_MAX_PAGES,
  293. dm_sector_div_up(remaining, (PAGE_SIZE >> SECTOR_SHIFT)));
  294. }
  295. bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, &io->client->bios);
  296. bio->bi_iter.bi_sector = where->sector + (where->count - remaining);
  297. bio_set_dev(bio, where->bdev);
  298. bio->bi_end_io = endio;
  299. bio_set_op_attrs(bio, op, op_flags);
  300. store_io_and_region_in_bio(bio, io, region);
  301. if (op == REQ_OP_DISCARD || op == REQ_OP_WRITE_ZEROES) {
  302. num_sectors = min_t(sector_t, special_cmd_max_sectors, remaining);
  303. bio->bi_iter.bi_size = num_sectors << SECTOR_SHIFT;
  304. remaining -= num_sectors;
  305. } else if (op == REQ_OP_WRITE_SAME) {
  306. /*
  307. * WRITE SAME only uses a single page.
  308. */
  309. dp->get_page(dp, &page, &len, &offset);
  310. bio_add_page(bio, page, logical_block_size, offset);
  311. num_sectors = min_t(sector_t, special_cmd_max_sectors, remaining);
  312. bio->bi_iter.bi_size = num_sectors << SECTOR_SHIFT;
  313. offset = 0;
  314. remaining -= num_sectors;
  315. dp->next_page(dp);
  316. } else while (remaining) {
  317. /*
  318. * Try and add as many pages as possible.
  319. */
  320. dp->get_page(dp, &page, &len, &offset);
  321. len = min(len, to_bytes(remaining));
  322. if (!bio_add_page(bio, page, len, offset))
  323. break;
  324. offset = 0;
  325. remaining -= to_sector(len);
  326. dp->next_page(dp);
  327. }
  328. atomic_inc(&io->count);
  329. submit_bio(bio);
  330. } while (remaining);
  331. }
  332. static void dispatch_io(int op, int op_flags, unsigned int num_regions,
  333. struct dm_io_region *where, struct dpages *dp,
  334. struct io *io, int sync)
  335. {
  336. int i;
  337. struct dpages old_pages = *dp;
  338. BUG_ON(num_regions > DM_IO_MAX_REGIONS);
  339. if (sync)
  340. op_flags |= REQ_SYNC;
  341. /*
  342. * For multiple regions we need to be careful to rewind
  343. * the dp object for each call to do_region.
  344. */
  345. for (i = 0; i < num_regions; i++) {
  346. *dp = old_pages;
  347. if (where[i].count || (op_flags & REQ_PREFLUSH))
  348. do_region(op, op_flags, i, where + i, dp, io);
  349. }
  350. /*
  351. * Drop the extra reference that we were holding to avoid
  352. * the io being completed too early.
  353. */
  354. dec_count(io, 0, 0);
  355. }
  356. struct sync_io {
  357. unsigned long error_bits;
  358. struct completion wait;
  359. };
  360. static void sync_io_complete(unsigned long error, void *context)
  361. {
  362. struct sync_io *sio = context;
  363. sio->error_bits = error;
  364. complete(&sio->wait);
  365. }
  366. static int sync_io(struct dm_io_client *client, unsigned int num_regions,
  367. struct dm_io_region *where, int op, int op_flags,
  368. struct dpages *dp, unsigned long *error_bits)
  369. {
  370. struct io *io;
  371. struct sync_io sio;
  372. if (num_regions > 1 && !op_is_write(op)) {
  373. WARN_ON(1);
  374. return -EIO;
  375. }
  376. init_completion(&sio.wait);
  377. io = mempool_alloc(&client->pool, GFP_NOIO);
  378. io->error_bits = 0;
  379. atomic_set(&io->count, 1); /* see dispatch_io() */
  380. io->client = client;
  381. io->callback = sync_io_complete;
  382. io->context = &sio;
  383. io->vma_invalidate_address = dp->vma_invalidate_address;
  384. io->vma_invalidate_size = dp->vma_invalidate_size;
  385. dispatch_io(op, op_flags, num_regions, where, dp, io, 1);
  386. wait_for_completion_io(&sio.wait);
  387. if (error_bits)
  388. *error_bits = sio.error_bits;
  389. return sio.error_bits ? -EIO : 0;
  390. }
  391. static int async_io(struct dm_io_client *client, unsigned int num_regions,
  392. struct dm_io_region *where, int op, int op_flags,
  393. struct dpages *dp, io_notify_fn fn, void *context)
  394. {
  395. struct io *io;
  396. if (num_regions > 1 && !op_is_write(op)) {
  397. WARN_ON(1);
  398. fn(1, context);
  399. return -EIO;
  400. }
  401. io = mempool_alloc(&client->pool, GFP_NOIO);
  402. io->error_bits = 0;
  403. atomic_set(&io->count, 1); /* see dispatch_io() */
  404. io->client = client;
  405. io->callback = fn;
  406. io->context = context;
  407. io->vma_invalidate_address = dp->vma_invalidate_address;
  408. io->vma_invalidate_size = dp->vma_invalidate_size;
  409. dispatch_io(op, op_flags, num_regions, where, dp, io, 0);
  410. return 0;
  411. }
  412. static int dp_init(struct dm_io_request *io_req, struct dpages *dp,
  413. unsigned long size)
  414. {
  415. /* Set up dpages based on memory type */
  416. dp->vma_invalidate_address = NULL;
  417. dp->vma_invalidate_size = 0;
  418. switch (io_req->mem.type) {
  419. case DM_IO_PAGE_LIST:
  420. list_dp_init(dp, io_req->mem.ptr.pl, io_req->mem.offset);
  421. break;
  422. case DM_IO_BIO:
  423. bio_dp_init(dp, io_req->mem.ptr.bio);
  424. break;
  425. case DM_IO_VMA:
  426. flush_kernel_vmap_range(io_req->mem.ptr.vma, size);
  427. if (io_req->bi_op == REQ_OP_READ) {
  428. dp->vma_invalidate_address = io_req->mem.ptr.vma;
  429. dp->vma_invalidate_size = size;
  430. }
  431. vm_dp_init(dp, io_req->mem.ptr.vma);
  432. break;
  433. case DM_IO_KMEM:
  434. km_dp_init(dp, io_req->mem.ptr.addr);
  435. break;
  436. default:
  437. return -EINVAL;
  438. }
  439. return 0;
  440. }
  441. /*
  442. * New collapsed (a)synchronous interface.
  443. *
  444. * If the IO is asynchronous (i.e. it has notify.fn), you must either unplug
  445. * the queue with blk_unplug() some time later or set REQ_SYNC in
  446. * io_req->bi_opf. If you fail to do one of these, the IO will be submitted to
  447. * the disk after q->unplug_delay, which defaults to 3ms in blk-settings.c.
  448. */
  449. int dm_io(struct dm_io_request *io_req, unsigned num_regions,
  450. struct dm_io_region *where, unsigned long *sync_error_bits)
  451. {
  452. int r;
  453. struct dpages dp;
  454. r = dp_init(io_req, &dp, (unsigned long)where->count << SECTOR_SHIFT);
  455. if (r)
  456. return r;
  457. if (!io_req->notify.fn)
  458. return sync_io(io_req->client, num_regions, where,
  459. io_req->bi_op, io_req->bi_op_flags, &dp,
  460. sync_error_bits);
  461. return async_io(io_req->client, num_regions, where, io_req->bi_op,
  462. io_req->bi_op_flags, &dp, io_req->notify.fn,
  463. io_req->notify.context);
  464. }
  465. EXPORT_SYMBOL(dm_io);
  466. int __init dm_io_init(void)
  467. {
  468. _dm_io_cache = KMEM_CACHE(io, 0);
  469. if (!_dm_io_cache)
  470. return -ENOMEM;
  471. return 0;
  472. }
  473. void dm_io_exit(void)
  474. {
  475. kmem_cache_destroy(_dm_io_cache);
  476. _dm_io_cache = NULL;
  477. }