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