blk-map.c 7.3 KB

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