blk-map.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <linux/uio.h>
  9. #include "blk.h"
  10. /*
  11. * Append a bio to a passthrough request. Only works can be merged into
  12. * the request based on the driver constraints.
  13. */
  14. int blk_rq_append_bio(struct request *rq, struct bio *bio)
  15. {
  16. if (!rq->bio) {
  17. blk_rq_bio_prep(rq->q, rq, bio);
  18. } else {
  19. if (!ll_back_merge_fn(rq->q, rq, bio))
  20. return -EINVAL;
  21. rq->biotail->bi_next = bio;
  22. rq->biotail = bio;
  23. rq->__data_len += bio->bi_iter.bi_size;
  24. }
  25. return 0;
  26. }
  27. EXPORT_SYMBOL(blk_rq_append_bio);
  28. static int __blk_rq_unmap_user(struct bio *bio)
  29. {
  30. int ret = 0;
  31. if (bio) {
  32. if (bio_flagged(bio, BIO_USER_MAPPED))
  33. bio_unmap_user(bio);
  34. else
  35. ret = bio_uncopy_user(bio);
  36. }
  37. return ret;
  38. }
  39. static int __blk_rq_map_user_iov(struct request *rq,
  40. struct rq_map_data *map_data, struct iov_iter *iter,
  41. gfp_t gfp_mask, bool copy)
  42. {
  43. struct request_queue *q = rq->q;
  44. struct bio *bio, *orig_bio;
  45. int ret;
  46. if (copy)
  47. bio = bio_copy_user_iov(q, map_data, iter, gfp_mask);
  48. else
  49. bio = bio_map_user_iov(q, iter, gfp_mask);
  50. if (IS_ERR(bio))
  51. return PTR_ERR(bio);
  52. if (map_data && map_data->null_mapped)
  53. bio_set_flag(bio, BIO_NULL_MAPPED);
  54. iov_iter_advance(iter, bio->bi_iter.bi_size);
  55. if (map_data)
  56. map_data->offset += bio->bi_iter.bi_size;
  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(rq, bio);
  65. if (ret) {
  66. bio_endio(bio);
  67. __blk_rq_unmap_user(orig_bio);
  68. bio_put(bio);
  69. return ret;
  70. }
  71. return 0;
  72. }
  73. /**
  74. * blk_rq_map_user_iov - map user data to a request, for REQ_TYPE_BLOCK_PC usage
  75. * @q: request queue where request should be inserted
  76. * @rq: request to map data to
  77. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  78. * @iter: iovec iterator
  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_iov(struct request_queue *q, struct request *rq,
  95. struct rq_map_data *map_data,
  96. const struct iov_iter *iter, gfp_t gfp_mask)
  97. {
  98. bool copy = false;
  99. unsigned long align = q->dma_pad_mask | queue_dma_alignment(q);
  100. struct bio *bio = NULL;
  101. struct iov_iter i;
  102. int ret = -EINVAL;
  103. if (!iter_is_iovec(iter))
  104. goto fail;
  105. if (map_data)
  106. copy = true;
  107. else if (iov_iter_alignment(iter) & align)
  108. copy = true;
  109. else if (queue_virt_boundary(q))
  110. copy = queue_virt_boundary(q) & iov_iter_gap_alignment(iter);
  111. i = *iter;
  112. do {
  113. ret =__blk_rq_map_user_iov(rq, map_data, &i, gfp_mask, copy);
  114. if (ret)
  115. goto unmap_rq;
  116. if (!bio)
  117. bio = rq->bio;
  118. } while (iov_iter_count(&i));
  119. if (!bio_flagged(bio, BIO_USER_MAPPED))
  120. rq->cmd_flags |= REQ_COPY_USER;
  121. return 0;
  122. unmap_rq:
  123. __blk_rq_unmap_user(bio);
  124. fail:
  125. rq->bio = NULL;
  126. return ret;
  127. }
  128. EXPORT_SYMBOL(blk_rq_map_user_iov);
  129. int blk_rq_map_user(struct request_queue *q, struct request *rq,
  130. struct rq_map_data *map_data, void __user *ubuf,
  131. unsigned long len, gfp_t gfp_mask)
  132. {
  133. struct iovec iov;
  134. struct iov_iter i;
  135. int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i);
  136. if (unlikely(ret < 0))
  137. return ret;
  138. return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask);
  139. }
  140. EXPORT_SYMBOL(blk_rq_map_user);
  141. /**
  142. * blk_rq_unmap_user - unmap a request with user data
  143. * @bio: start of bio list
  144. *
  145. * Description:
  146. * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
  147. * supply the original rq->bio from the blk_rq_map_user() return, since
  148. * the I/O completion may have changed rq->bio.
  149. */
  150. int blk_rq_unmap_user(struct bio *bio)
  151. {
  152. struct bio *mapped_bio;
  153. int ret = 0, ret2;
  154. while (bio) {
  155. mapped_bio = bio;
  156. if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
  157. mapped_bio = bio->bi_private;
  158. ret2 = __blk_rq_unmap_user(mapped_bio);
  159. if (ret2 && !ret)
  160. ret = ret2;
  161. mapped_bio = bio;
  162. bio = bio->bi_next;
  163. bio_put(mapped_bio);
  164. }
  165. return ret;
  166. }
  167. EXPORT_SYMBOL(blk_rq_unmap_user);
  168. /**
  169. * blk_rq_map_kern - map kernel data to a request, for REQ_TYPE_BLOCK_PC usage
  170. * @q: request queue where request should be inserted
  171. * @rq: request to fill
  172. * @kbuf: the kernel buffer
  173. * @len: length of user data
  174. * @gfp_mask: memory allocation flags
  175. *
  176. * Description:
  177. * Data will be mapped directly if possible. Otherwise a bounce
  178. * buffer is used. Can be called multiple times to append multiple
  179. * buffers.
  180. */
  181. int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
  182. unsigned int len, gfp_t gfp_mask)
  183. {
  184. int reading = rq_data_dir(rq) == READ;
  185. unsigned long addr = (unsigned long) kbuf;
  186. int do_copy = 0;
  187. struct bio *bio;
  188. int ret;
  189. if (len > (queue_max_hw_sectors(q) << 9))
  190. return -EINVAL;
  191. if (!len || !kbuf)
  192. return -EINVAL;
  193. do_copy = !blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf);
  194. if (do_copy)
  195. bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
  196. else
  197. bio = bio_map_kern(q, kbuf, len, gfp_mask);
  198. if (IS_ERR(bio))
  199. return PTR_ERR(bio);
  200. if (!reading)
  201. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  202. if (do_copy)
  203. rq->cmd_flags |= REQ_COPY_USER;
  204. ret = blk_rq_append_bio(rq, bio);
  205. if (unlikely(ret)) {
  206. /* request is too big */
  207. bio_put(bio);
  208. return ret;
  209. }
  210. blk_queue_bounce(q, &rq->bio);
  211. return 0;
  212. }
  213. EXPORT_SYMBOL(blk_rq_map_kern);