blk-map.c 7.8 KB

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