blk-map.c 7.5 KB

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