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. {
  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. if (!bio_flagged(bio, BIO_USER_MAPPED))
  124. rq->cmd_flags |= REQ_COPY_USER;
  125. rq->buffer = rq->data = NULL;
  126. return 0;
  127. unmap_rq:
  128. blk_rq_unmap_user(bio);
  129. rq->bio = NULL;
  130. return ret;
  131. }
  132. EXPORT_SYMBOL(blk_rq_map_user);
  133. /**
  134. * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
  135. * @q: request queue where request should be inserted
  136. * @rq: request to map data to
  137. * @iov: pointer to the iovec
  138. * @iov_count: number of elements in the iovec
  139. * @len: I/O byte count
  140. *
  141. * Description:
  142. * Data will be mapped directly for zero copy io, if possible. Otherwise
  143. * a kernel bounce buffer is used.
  144. *
  145. * A matching blk_rq_unmap_user() must be issued at the end of io, while
  146. * still in process context.
  147. *
  148. * Note: The mapped bio may need to be bounced through blk_queue_bounce()
  149. * before being submitted to the device, as pages mapped may be out of
  150. * reach. It's the callers responsibility to make sure this happens. The
  151. * original bio must be passed back in to blk_rq_unmap_user() for proper
  152. * unmapping.
  153. */
  154. int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
  155. struct sg_iovec *iov, int iov_count, unsigned int len)
  156. {
  157. struct bio *bio;
  158. int i, read = rq_data_dir(rq) == READ;
  159. int unaligned = 0;
  160. if (!iov || iov_count <= 0)
  161. return -EINVAL;
  162. for (i = 0; i < iov_count; i++) {
  163. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  164. if (uaddr & queue_dma_alignment(q)) {
  165. unaligned = 1;
  166. break;
  167. }
  168. }
  169. if (unaligned || (q->dma_pad_mask & len))
  170. bio = bio_copy_user_iov(q, iov, iov_count, read);
  171. else
  172. bio = bio_map_user_iov(q, NULL, iov, iov_count, read);
  173. if (IS_ERR(bio))
  174. return PTR_ERR(bio);
  175. if (bio->bi_size != len) {
  176. bio_endio(bio, 0);
  177. bio_unmap_user(bio);
  178. return -EINVAL;
  179. }
  180. if (!bio_flagged(bio, BIO_USER_MAPPED))
  181. rq->cmd_flags |= REQ_COPY_USER;
  182. blk_queue_bounce(q, &bio);
  183. bio_get(bio);
  184. blk_rq_bio_prep(q, rq, bio);
  185. rq->buffer = rq->data = NULL;
  186. return 0;
  187. }
  188. /**
  189. * blk_rq_unmap_user - unmap a request with user data
  190. * @bio: start of bio list
  191. *
  192. * Description:
  193. * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
  194. * supply the original rq->bio from the blk_rq_map_user() return, since
  195. * the io completion may have changed rq->bio.
  196. */
  197. int blk_rq_unmap_user(struct bio *bio)
  198. {
  199. struct bio *mapped_bio;
  200. int ret = 0, ret2;
  201. while (bio) {
  202. mapped_bio = bio;
  203. if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
  204. mapped_bio = bio->bi_private;
  205. ret2 = __blk_rq_unmap_user(mapped_bio);
  206. if (ret2 && !ret)
  207. ret = ret2;
  208. mapped_bio = bio;
  209. bio = bio->bi_next;
  210. bio_put(mapped_bio);
  211. }
  212. return ret;
  213. }
  214. EXPORT_SYMBOL(blk_rq_unmap_user);
  215. /**
  216. * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
  217. * @q: request queue where request should be inserted
  218. * @rq: request to fill
  219. * @kbuf: the kernel buffer
  220. * @len: length of user data
  221. * @gfp_mask: memory allocation flags
  222. *
  223. * Description:
  224. * Data will be mapped directly if possible. Otherwise a bounce
  225. * buffer is used.
  226. */
  227. int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
  228. unsigned int len, gfp_t gfp_mask)
  229. {
  230. unsigned long kaddr;
  231. unsigned int alignment;
  232. int reading = rq_data_dir(rq) == READ;
  233. int do_copy = 0;
  234. struct bio *bio;
  235. unsigned long stack_mask = ~(THREAD_SIZE - 1);
  236. if (len > (q->max_hw_sectors << 9))
  237. return -EINVAL;
  238. if (!len || !kbuf)
  239. return -EINVAL;
  240. kaddr = (unsigned long)kbuf;
  241. alignment = queue_dma_alignment(q) | q->dma_pad_mask;
  242. do_copy = ((kaddr & alignment) || (len & alignment));
  243. if (!((kaddr & stack_mask) ^
  244. ((unsigned long)current->stack & stack_mask)))
  245. do_copy = 1;
  246. if (do_copy)
  247. bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
  248. else
  249. bio = bio_map_kern(q, kbuf, len, gfp_mask);
  250. if (IS_ERR(bio))
  251. return PTR_ERR(bio);
  252. if (rq_data_dir(rq) == WRITE)
  253. bio->bi_rw |= (1 << BIO_RW);
  254. if (do_copy)
  255. rq->cmd_flags |= REQ_COPY_USER;
  256. blk_rq_bio_prep(q, rq, bio);
  257. blk_queue_bounce(q, &rq->bio);
  258. rq->buffer = rq->data = NULL;
  259. return 0;
  260. }
  261. EXPORT_SYMBOL(blk_rq_map_kern);