blk-map.c 7.1 KB

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