blk-map.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. rq->buffer = rq->data = NULL;
  122. return 0;
  123. unmap_rq:
  124. blk_rq_unmap_user(bio);
  125. return ret;
  126. }
  127. EXPORT_SYMBOL(blk_rq_map_user);
  128. /**
  129. * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
  130. * @q: request queue where request should be inserted
  131. * @rq: request to map data to
  132. * @iov: pointer to the iovec
  133. * @iov_count: number of elements in the iovec
  134. * @len: I/O byte count
  135. *
  136. * Description:
  137. * Data will be mapped directly for zero copy io, if possible. Otherwise
  138. * a kernel bounce buffer is used.
  139. *
  140. * A matching blk_rq_unmap_user() must be issued at the end of io, while
  141. * still in process context.
  142. *
  143. * Note: The mapped bio may need to be bounced through blk_queue_bounce()
  144. * before being submitted to the device, as pages mapped may be out of
  145. * reach. It's the callers responsibility to make sure this happens. The
  146. * original bio must be passed back in to blk_rq_unmap_user() for proper
  147. * unmapping.
  148. */
  149. int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
  150. struct sg_iovec *iov, int iov_count, unsigned int len)
  151. {
  152. struct bio *bio;
  153. if (!iov || iov_count <= 0)
  154. return -EINVAL;
  155. /* we don't allow misaligned data like bio_map_user() does. If the
  156. * user is using sg, they're expected to know the alignment constraints
  157. * and respect them accordingly */
  158. bio = bio_map_user_iov(q, NULL, iov, iov_count,
  159. rq_data_dir(rq) == READ);
  160. if (IS_ERR(bio))
  161. return PTR_ERR(bio);
  162. if (bio->bi_size != len) {
  163. bio_endio(bio, 0);
  164. bio_unmap_user(bio);
  165. return -EINVAL;
  166. }
  167. bio_get(bio);
  168. blk_rq_bio_prep(q, rq, bio);
  169. rq->buffer = rq->data = NULL;
  170. return 0;
  171. }
  172. EXPORT_SYMBOL(blk_rq_map_user_iov);
  173. /**
  174. * blk_rq_unmap_user - unmap a request with user data
  175. * @bio: start of bio list
  176. *
  177. * Description:
  178. * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
  179. * supply the original rq->bio from the blk_rq_map_user() return, since
  180. * the io completion may have changed rq->bio.
  181. */
  182. int blk_rq_unmap_user(struct bio *bio)
  183. {
  184. struct bio *mapped_bio;
  185. int ret = 0, ret2;
  186. while (bio) {
  187. mapped_bio = bio;
  188. if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
  189. mapped_bio = bio->bi_private;
  190. ret2 = __blk_rq_unmap_user(mapped_bio);
  191. if (ret2 && !ret)
  192. ret = ret2;
  193. mapped_bio = bio;
  194. bio = bio->bi_next;
  195. bio_put(mapped_bio);
  196. }
  197. return ret;
  198. }
  199. EXPORT_SYMBOL(blk_rq_unmap_user);
  200. /**
  201. * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
  202. * @q: request queue where request should be inserted
  203. * @rq: request to fill
  204. * @kbuf: the kernel buffer
  205. * @len: length of user data
  206. * @gfp_mask: memory allocation flags
  207. */
  208. int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
  209. unsigned int len, gfp_t gfp_mask)
  210. {
  211. struct bio *bio;
  212. if (len > (q->max_hw_sectors << 9))
  213. return -EINVAL;
  214. if (!len || !kbuf)
  215. return -EINVAL;
  216. bio = bio_map_kern(q, kbuf, len, gfp_mask);
  217. if (IS_ERR(bio))
  218. return PTR_ERR(bio);
  219. if (rq_data_dir(rq) == WRITE)
  220. bio->bi_rw |= (1 << BIO_RW);
  221. blk_rq_bio_prep(q, rq, bio);
  222. blk_queue_bounce(q, &rq->bio);
  223. rq->buffer = rq->data = NULL;
  224. return 0;
  225. }
  226. EXPORT_SYMBOL(blk_rq_map_kern);