dm-io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. struct dm_io_client {
  18. mempool_t *pool;
  19. struct bio_set *bios;
  20. };
  21. /*
  22. * Aligning 'struct io' reduces the number of bits required to store
  23. * its address. Refer to store_io_and_region_in_bio() below.
  24. */
  25. struct io {
  26. unsigned long error_bits;
  27. atomic_t count;
  28. struct task_struct *sleeper;
  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 dec_count(struct io *io, unsigned int region, int error)
  95. {
  96. if (error)
  97. set_bit(region, &io->error_bits);
  98. if (atomic_dec_and_test(&io->count)) {
  99. if (io->vma_invalidate_size)
  100. invalidate_kernel_vmap_range(io->vma_invalidate_address,
  101. io->vma_invalidate_size);
  102. if (io->sleeper)
  103. wake_up_process(io->sleeper);
  104. else {
  105. unsigned long r = io->error_bits;
  106. io_notify_fn fn = io->callback;
  107. void *context = io->context;
  108. mempool_free(io, io->client->pool);
  109. fn(r, context);
  110. }
  111. }
  112. }
  113. static void endio(struct bio *bio, int error)
  114. {
  115. struct io *io;
  116. unsigned region;
  117. if (error && 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. 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 bvec_get_page(struct dpages *dp,
  168. struct page **p, unsigned long *len, unsigned *offset)
  169. {
  170. struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
  171. *p = bvec->bv_page;
  172. *len = bvec->bv_len;
  173. *offset = bvec->bv_offset;
  174. }
  175. static void bvec_next_page(struct dpages *dp)
  176. {
  177. struct bio_vec *bvec = (struct bio_vec *) dp->context_ptr;
  178. dp->context_ptr = bvec + 1;
  179. }
  180. static void bvec_dp_init(struct dpages *dp, struct bio_vec *bvec)
  181. {
  182. dp->get_page = bvec_get_page;
  183. dp->next_page = bvec_next_page;
  184. dp->context_ptr = bvec;
  185. }
  186. /*
  187. * Functions for getting the pages from a VMA.
  188. */
  189. static void vm_get_page(struct dpages *dp,
  190. struct page **p, unsigned long *len, unsigned *offset)
  191. {
  192. *p = vmalloc_to_page(dp->context_ptr);
  193. *offset = dp->context_u;
  194. *len = PAGE_SIZE - dp->context_u;
  195. }
  196. static void vm_next_page(struct dpages *dp)
  197. {
  198. dp->context_ptr += PAGE_SIZE - dp->context_u;
  199. dp->context_u = 0;
  200. }
  201. static void vm_dp_init(struct dpages *dp, void *data)
  202. {
  203. dp->get_page = vm_get_page;
  204. dp->next_page = vm_next_page;
  205. dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
  206. dp->context_ptr = data;
  207. }
  208. /*
  209. * Functions for getting the pages from kernel memory.
  210. */
  211. static void km_get_page(struct dpages *dp, struct page **p, unsigned long *len,
  212. unsigned *offset)
  213. {
  214. *p = virt_to_page(dp->context_ptr);
  215. *offset = dp->context_u;
  216. *len = PAGE_SIZE - dp->context_u;
  217. }
  218. static void km_next_page(struct dpages *dp)
  219. {
  220. dp->context_ptr += PAGE_SIZE - dp->context_u;
  221. dp->context_u = 0;
  222. }
  223. static void km_dp_init(struct dpages *dp, void *data)
  224. {
  225. dp->get_page = km_get_page;
  226. dp->next_page = km_next_page;
  227. dp->context_u = ((unsigned long) data) & (PAGE_SIZE - 1);
  228. dp->context_ptr = data;
  229. }
  230. /*-----------------------------------------------------------------
  231. * IO routines that accept a list of pages.
  232. *---------------------------------------------------------------*/
  233. static void do_region(int rw, unsigned region, struct dm_io_region *where,
  234. struct dpages *dp, struct io *io)
  235. {
  236. struct bio *bio;
  237. struct page *page;
  238. unsigned long len;
  239. unsigned offset;
  240. unsigned num_bvecs;
  241. sector_t remaining = where->count;
  242. struct request_queue *q = bdev_get_queue(where->bdev);
  243. unsigned short logical_block_size = queue_logical_block_size(q);
  244. sector_t num_sectors;
  245. /*
  246. * where->count may be zero if rw holds a flush and we need to
  247. * send a zero-sized flush.
  248. */
  249. do {
  250. /*
  251. * Allocate a suitably sized-bio.
  252. */
  253. if ((rw & REQ_DISCARD) || (rw & REQ_WRITE_SAME))
  254. num_bvecs = 1;
  255. else
  256. num_bvecs = min_t(int, bio_get_nr_vecs(where->bdev),
  257. dm_sector_div_up(remaining, (PAGE_SIZE >> SECTOR_SHIFT)));
  258. bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, io->client->bios);
  259. bio->bi_sector = where->sector + (where->count - remaining);
  260. bio->bi_bdev = where->bdev;
  261. bio->bi_end_io = endio;
  262. store_io_and_region_in_bio(bio, io, region);
  263. if (rw & REQ_DISCARD) {
  264. num_sectors = min_t(sector_t, q->limits.max_discard_sectors, remaining);
  265. bio->bi_size = num_sectors << SECTOR_SHIFT;
  266. remaining -= num_sectors;
  267. } else if (rw & REQ_WRITE_SAME) {
  268. /*
  269. * WRITE SAME only uses a single page.
  270. */
  271. dp->get_page(dp, &page, &len, &offset);
  272. bio_add_page(bio, page, logical_block_size, offset);
  273. num_sectors = min_t(sector_t, q->limits.max_write_same_sectors, remaining);
  274. bio->bi_size = num_sectors << SECTOR_SHIFT;
  275. offset = 0;
  276. remaining -= num_sectors;
  277. dp->next_page(dp);
  278. } else while (remaining) {
  279. /*
  280. * Try and add as many pages as possible.
  281. */
  282. dp->get_page(dp, &page, &len, &offset);
  283. len = min(len, to_bytes(remaining));
  284. if (!bio_add_page(bio, page, len, offset))
  285. break;
  286. offset = 0;
  287. remaining -= to_sector(len);
  288. dp->next_page(dp);
  289. }
  290. atomic_inc(&io->count);
  291. submit_bio(rw, bio);
  292. } while (remaining);
  293. }
  294. static void dispatch_io(int rw, unsigned int num_regions,
  295. struct dm_io_region *where, struct dpages *dp,
  296. struct io *io, int sync)
  297. {
  298. int i;
  299. struct dpages old_pages = *dp;
  300. BUG_ON(num_regions > DM_IO_MAX_REGIONS);
  301. if (sync)
  302. rw |= REQ_SYNC;
  303. /*
  304. * For multiple regions we need to be careful to rewind
  305. * the dp object for each call to do_region.
  306. */
  307. for (i = 0; i < num_regions; i++) {
  308. *dp = old_pages;
  309. if (where[i].count || (rw & REQ_FLUSH))
  310. do_region(rw, i, where + i, dp, io);
  311. }
  312. /*
  313. * Drop the extra reference that we were holding to avoid
  314. * the io being completed too early.
  315. */
  316. dec_count(io, 0, 0);
  317. }
  318. static int sync_io(struct dm_io_client *client, unsigned int num_regions,
  319. struct dm_io_region *where, int rw, struct dpages *dp,
  320. unsigned long *error_bits)
  321. {
  322. /*
  323. * gcc <= 4.3 can't do the alignment for stack variables, so we must
  324. * align it on our own.
  325. * volatile prevents the optimizer from removing or reusing
  326. * "io_" field from the stack frame (allowed in ANSI C).
  327. */
  328. volatile char io_[sizeof(struct io) + __alignof__(struct io) - 1];
  329. struct io *io = (struct io *)PTR_ALIGN(&io_, __alignof__(struct io));
  330. if (num_regions > 1 && (rw & RW_MASK) != WRITE) {
  331. WARN_ON(1);
  332. return -EIO;
  333. }
  334. io->error_bits = 0;
  335. atomic_set(&io->count, 1); /* see dispatch_io() */
  336. io->sleeper = current;
  337. io->client = client;
  338. io->vma_invalidate_address = dp->vma_invalidate_address;
  339. io->vma_invalidate_size = dp->vma_invalidate_size;
  340. dispatch_io(rw, num_regions, where, dp, io, 1);
  341. while (1) {
  342. set_current_state(TASK_UNINTERRUPTIBLE);
  343. if (!atomic_read(&io->count))
  344. break;
  345. io_schedule();
  346. }
  347. set_current_state(TASK_RUNNING);
  348. if (error_bits)
  349. *error_bits = io->error_bits;
  350. return io->error_bits ? -EIO : 0;
  351. }
  352. static int async_io(struct dm_io_client *client, unsigned int num_regions,
  353. struct dm_io_region *where, int rw, struct dpages *dp,
  354. io_notify_fn fn, void *context)
  355. {
  356. struct io *io;
  357. if (num_regions > 1 && (rw & RW_MASK) != WRITE) {
  358. WARN_ON(1);
  359. fn(1, context);
  360. return -EIO;
  361. }
  362. io = mempool_alloc(client->pool, GFP_NOIO);
  363. io->error_bits = 0;
  364. atomic_set(&io->count, 1); /* see dispatch_io() */
  365. io->sleeper = NULL;
  366. io->client = client;
  367. io->callback = fn;
  368. io->context = context;
  369. io->vma_invalidate_address = dp->vma_invalidate_address;
  370. io->vma_invalidate_size = dp->vma_invalidate_size;
  371. dispatch_io(rw, num_regions, where, dp, io, 0);
  372. return 0;
  373. }
  374. static int dp_init(struct dm_io_request *io_req, struct dpages *dp,
  375. unsigned long size)
  376. {
  377. /* Set up dpages based on memory type */
  378. dp->vma_invalidate_address = NULL;
  379. dp->vma_invalidate_size = 0;
  380. switch (io_req->mem.type) {
  381. case DM_IO_PAGE_LIST:
  382. list_dp_init(dp, io_req->mem.ptr.pl, io_req->mem.offset);
  383. break;
  384. case DM_IO_BVEC:
  385. bvec_dp_init(dp, io_req->mem.ptr.bvec);
  386. break;
  387. case DM_IO_VMA:
  388. flush_kernel_vmap_range(io_req->mem.ptr.vma, size);
  389. if ((io_req->bi_rw & RW_MASK) == READ) {
  390. dp->vma_invalidate_address = io_req->mem.ptr.vma;
  391. dp->vma_invalidate_size = size;
  392. }
  393. vm_dp_init(dp, io_req->mem.ptr.vma);
  394. break;
  395. case DM_IO_KMEM:
  396. km_dp_init(dp, io_req->mem.ptr.addr);
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. return 0;
  402. }
  403. /*
  404. * New collapsed (a)synchronous interface.
  405. *
  406. * If the IO is asynchronous (i.e. it has notify.fn), you must either unplug
  407. * the queue with blk_unplug() some time later or set REQ_SYNC in
  408. io_req->bi_rw. If you fail to do one of these, the IO will be submitted to
  409. * the disk after q->unplug_delay, which defaults to 3ms in blk-settings.c.
  410. */
  411. int dm_io(struct dm_io_request *io_req, unsigned num_regions,
  412. struct dm_io_region *where, unsigned long *sync_error_bits)
  413. {
  414. int r;
  415. struct dpages dp;
  416. r = dp_init(io_req, &dp, (unsigned long)where->count << SECTOR_SHIFT);
  417. if (r)
  418. return r;
  419. if (!io_req->notify.fn)
  420. return sync_io(io_req->client, num_regions, where,
  421. io_req->bi_rw, &dp, sync_error_bits);
  422. return async_io(io_req->client, num_regions, where, io_req->bi_rw,
  423. &dp, io_req->notify.fn, io_req->notify.context);
  424. }
  425. EXPORT_SYMBOL(dm_io);
  426. int __init dm_io_init(void)
  427. {
  428. _dm_io_cache = KMEM_CACHE(io, 0);
  429. if (!_dm_io_cache)
  430. return -ENOMEM;
  431. return 0;
  432. }
  433. void dm_io_exit(void)
  434. {
  435. kmem_cache_destroy(_dm_io_cache);
  436. _dm_io_cache = NULL;
  437. }