blk-map.c 8.2 KB

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