blk-map.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Functions related to mapping data to requests
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/bio.h>
  7. #include <linux/blkdev.h>
  8. #include "blk.h"
  9. int blk_rq_append_bio(struct request_queue *q, struct request *rq,
  10. struct bio *bio)
  11. {
  12. if (!rq->bio)
  13. blk_rq_bio_prep(q, rq, bio);
  14. else if (!ll_back_merge_fn(q, rq, bio))
  15. return -EINVAL;
  16. else {
  17. rq->biotail->bi_next = bio;
  18. rq->biotail = bio;
  19. rq->data_len += bio->bi_size;
  20. }
  21. return 0;
  22. }
  23. EXPORT_SYMBOL(blk_rq_append_bio);
  24. static int __blk_rq_unmap_user(struct bio *bio)
  25. {
  26. int ret = 0;
  27. if (bio) {
  28. if (bio_flagged(bio, BIO_USER_MAPPED))
  29. bio_unmap_user(bio);
  30. else
  31. ret = bio_uncopy_user(bio);
  32. }
  33. return ret;
  34. }
  35. static int __blk_rq_map_user(struct request_queue *q, struct request *rq,
  36. void __user *ubuf, unsigned int len)
  37. {
  38. unsigned long uaddr;
  39. unsigned int alignment;
  40. struct bio *bio, *orig_bio;
  41. int reading, ret;
  42. reading = rq_data_dir(rq) == READ;
  43. /*
  44. * if alignment requirement is satisfied, map in user pages for
  45. * direct dma. else, set up kernel bounce buffers
  46. */
  47. uaddr = (unsigned long) ubuf;
  48. alignment = queue_dma_alignment(q) | q->dma_pad_mask;
  49. if (!(uaddr & alignment) && !(len & alignment))
  50. bio = bio_map_user(q, NULL, uaddr, len, reading);
  51. else
  52. bio = bio_copy_user(q, uaddr, len, reading);
  53. if (IS_ERR(bio))
  54. return PTR_ERR(bio);
  55. orig_bio = bio;
  56. blk_queue_bounce(q, &bio);
  57. /*
  58. * We link the bounce buffer in and could have to traverse it
  59. * later so we have to get a ref to prevent it from being freed
  60. */
  61. bio_get(bio);
  62. ret = blk_rq_append_bio(q, rq, bio);
  63. if (!ret)
  64. return bio->bi_size;
  65. /* if it was boucned we must call the end io function */
  66. bio_endio(bio, 0);
  67. __blk_rq_unmap_user(orig_bio);
  68. bio_put(bio);
  69. return ret;
  70. }
  71. /**
  72. * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
  73. * @q: request queue where request should be inserted
  74. * @rq: request structure to fill
  75. * @ubuf: the user buffer
  76. * @len: length of user data
  77. *
  78. * Description:
  79. * Data will be mapped directly for zero copy io, if possible. Otherwise
  80. * a kernel bounce buffer is used.
  81. *
  82. * A matching blk_rq_unmap_user() must be issued at the end of io, while
  83. * still in process context.
  84. *
  85. * Note: The mapped bio may need to be bounced through blk_queue_bounce()
  86. * before being submitted to the device, as pages mapped may be out of
  87. * reach. It's the callers responsibility to make sure this happens. The
  88. * original bio must be passed back in to blk_rq_unmap_user() for proper
  89. * unmapping.
  90. */
  91. int blk_rq_map_user(struct request_queue *q, struct request *rq,
  92. void __user *ubuf, unsigned long len)
  93. {
  94. unsigned long bytes_read = 0;
  95. struct bio *bio = NULL;
  96. int ret;
  97. if (len > (q->max_hw_sectors << 9))
  98. return -EINVAL;
  99. if (!len || !ubuf)
  100. return -EINVAL;
  101. while (bytes_read != len) {
  102. unsigned long map_len, end, start;
  103. map_len = min_t(unsigned long, len - bytes_read, BIO_MAX_SIZE);
  104. end = ((unsigned long)ubuf + map_len + PAGE_SIZE - 1)
  105. >> PAGE_SHIFT;
  106. start = (unsigned long)ubuf >> PAGE_SHIFT;
  107. /*
  108. * A bad offset could cause us to require BIO_MAX_PAGES + 1
  109. * pages. If this happens we just lower the requested
  110. * mapping len by a page so that we can fit
  111. */
  112. if (end - start > BIO_MAX_PAGES)
  113. map_len -= PAGE_SIZE;
  114. ret = __blk_rq_map_user(q, rq, ubuf, map_len);
  115. if (ret < 0)
  116. goto unmap_rq;
  117. if (!bio)
  118. bio = rq->bio;
  119. bytes_read += ret;
  120. ubuf += ret;
  121. }
  122. /*
  123. * __blk_rq_map_user() copies the buffers if starting address
  124. * or length isn't aligned to dma_pad_mask. As the copied
  125. * buffer is always page aligned, we know that there's enough
  126. * room for padding. Extend the last bio and update
  127. * rq->data_len accordingly.
  128. *
  129. * On unmap, bio_uncopy_user() will use unmodified
  130. * bio_map_data pointed to by bio->bi_private.
  131. */
  132. if (len & q->dma_pad_mask) {
  133. unsigned int pad_len = (q->dma_pad_mask & ~len) + 1;
  134. struct bio *tail = rq->biotail;
  135. tail->bi_io_vec[tail->bi_vcnt - 1].bv_len += pad_len;
  136. tail->bi_size += pad_len;
  137. rq->extra_len += pad_len;
  138. }
  139. rq->buffer = rq->data = NULL;
  140. return 0;
  141. unmap_rq:
  142. blk_rq_unmap_user(bio);
  143. rq->bio = NULL;
  144. return ret;
  145. }
  146. EXPORT_SYMBOL(blk_rq_map_user);
  147. /**
  148. * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
  149. * @q: request queue where request should be inserted
  150. * @rq: request to map data to
  151. * @iov: pointer to the iovec
  152. * @iov_count: number of elements in the iovec
  153. * @len: I/O byte count
  154. *
  155. * Description:
  156. * Data will be mapped directly for zero copy io, if possible. Otherwise
  157. * a kernel bounce buffer is used.
  158. *
  159. * A matching blk_rq_unmap_user() must be issued at the end of io, while
  160. * still in process context.
  161. *
  162. * Note: The mapped bio may need to be bounced through blk_queue_bounce()
  163. * before being submitted to the device, as pages mapped may be out of
  164. * reach. It's the callers responsibility to make sure this happens. The
  165. * original bio must be passed back in to blk_rq_unmap_user() for proper
  166. * unmapping.
  167. */
  168. int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
  169. struct sg_iovec *iov, int iov_count, unsigned int len)
  170. {
  171. struct bio *bio;
  172. if (!iov || iov_count <= 0)
  173. return -EINVAL;
  174. /* we don't allow misaligned data like bio_map_user() does. If the
  175. * user is using sg, they're expected to know the alignment constraints
  176. * and respect them accordingly */
  177. bio = bio_map_user_iov(q, NULL, iov, iov_count,
  178. rq_data_dir(rq) == READ);
  179. if (IS_ERR(bio))
  180. return PTR_ERR(bio);
  181. if (bio->bi_size != len) {
  182. bio_endio(bio, 0);
  183. bio_unmap_user(bio);
  184. return -EINVAL;
  185. }
  186. bio_get(bio);
  187. blk_rq_bio_prep(q, rq, bio);
  188. rq->buffer = rq->data = NULL;
  189. return 0;
  190. }
  191. /**
  192. * blk_rq_unmap_user - unmap a request with user data
  193. * @bio: start of bio list
  194. *
  195. * Description:
  196. * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
  197. * supply the original rq->bio from the blk_rq_map_user() return, since
  198. * the io completion may have changed rq->bio.
  199. */
  200. int blk_rq_unmap_user(struct bio *bio)
  201. {
  202. struct bio *mapped_bio;
  203. int ret = 0, ret2;
  204. while (bio) {
  205. mapped_bio = bio;
  206. if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
  207. mapped_bio = bio->bi_private;
  208. ret2 = __blk_rq_unmap_user(mapped_bio);
  209. if (ret2 && !ret)
  210. ret = ret2;
  211. mapped_bio = bio;
  212. bio = bio->bi_next;
  213. bio_put(mapped_bio);
  214. }
  215. return ret;
  216. }
  217. EXPORT_SYMBOL(blk_rq_unmap_user);
  218. /**
  219. * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
  220. * @q: request queue where request should be inserted
  221. * @rq: request to fill
  222. * @kbuf: the kernel buffer
  223. * @len: length of user data
  224. * @gfp_mask: memory allocation flags
  225. */
  226. int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
  227. unsigned int len, gfp_t gfp_mask)
  228. {
  229. struct bio *bio;
  230. if (len > (q->max_hw_sectors << 9))
  231. return -EINVAL;
  232. if (!len || !kbuf)
  233. return -EINVAL;
  234. bio = bio_map_kern(q, kbuf, len, gfp_mask);
  235. if (IS_ERR(bio))
  236. return PTR_ERR(bio);
  237. if (rq_data_dir(rq) == WRITE)
  238. bio->bi_rw |= (1 << BIO_RW);
  239. blk_rq_bio_prep(q, rq, bio);
  240. blk_queue_bounce(q, &rq->bio);
  241. rq->buffer = rq->data = NULL;
  242. return 0;
  243. }
  244. EXPORT_SYMBOL(blk_rq_map_kern);