blk-map.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. static bool iovec_gap_to_prv(struct request_queue *q,
  11. struct iovec *prv, struct iovec *cur)
  12. {
  13. unsigned long prev_end;
  14. if (!queue_virt_boundary(q))
  15. return false;
  16. if (prv->iov_base == NULL && prv->iov_len == 0)
  17. /* prv is not set - don't check */
  18. return false;
  19. prev_end = (unsigned long)(prv->iov_base + prv->iov_len);
  20. return (((unsigned long)cur->iov_base & queue_virt_boundary(q)) ||
  21. prev_end & queue_virt_boundary(q));
  22. }
  23. int blk_rq_append_bio(struct request_queue *q, struct request *rq,
  24. struct bio *bio)
  25. {
  26. if (!rq->bio)
  27. blk_rq_bio_prep(q, rq, bio);
  28. else if (!ll_back_merge_fn(q, rq, bio))
  29. return -EINVAL;
  30. else {
  31. rq->biotail->bi_next = bio;
  32. rq->biotail = bio;
  33. rq->__data_len += bio->bi_iter.bi_size;
  34. }
  35. return 0;
  36. }
  37. static int __blk_rq_unmap_user(struct bio *bio)
  38. {
  39. int ret = 0;
  40. if (bio) {
  41. if (bio_flagged(bio, BIO_USER_MAPPED))
  42. bio_unmap_user(bio);
  43. else
  44. ret = bio_uncopy_user(bio);
  45. }
  46. return ret;
  47. }
  48. /**
  49. * blk_rq_map_user_iov - map user data to a request, for REQ_TYPE_BLOCK_PC usage
  50. * @q: request queue where request should be inserted
  51. * @rq: request to map data to
  52. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  53. * @iter: iovec iterator
  54. * @gfp_mask: memory allocation flags
  55. *
  56. * Description:
  57. * Data will be mapped directly for zero copy I/O, if possible. Otherwise
  58. * a kernel bounce buffer is used.
  59. *
  60. * A matching blk_rq_unmap_user() must be issued at the end of I/O, while
  61. * still in process context.
  62. *
  63. * Note: The mapped bio may need to be bounced through blk_queue_bounce()
  64. * before being submitted to the device, as pages mapped may be out of
  65. * reach. It's the callers responsibility to make sure this happens. The
  66. * original bio must be passed back in to blk_rq_unmap_user() for proper
  67. * unmapping.
  68. */
  69. int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
  70. struct rq_map_data *map_data,
  71. const struct iov_iter *iter, gfp_t gfp_mask)
  72. {
  73. struct bio *bio;
  74. int unaligned = 0;
  75. struct iov_iter i;
  76. struct iovec iov, prv = {.iov_base = NULL, .iov_len = 0};
  77. if (!iter || !iter->count)
  78. return -EINVAL;
  79. if (!iter_is_iovec(iter))
  80. return -EINVAL;
  81. iov_for_each(iov, i, *iter) {
  82. unsigned long uaddr = (unsigned long) iov.iov_base;
  83. if (!iov.iov_len)
  84. return -EINVAL;
  85. /*
  86. * Keep going so we check length of all segments
  87. */
  88. if ((uaddr & queue_dma_alignment(q)) ||
  89. iovec_gap_to_prv(q, &prv, &iov))
  90. unaligned = 1;
  91. prv.iov_base = iov.iov_base;
  92. prv.iov_len = iov.iov_len;
  93. }
  94. if (unaligned || (q->dma_pad_mask & iter->count) || map_data)
  95. bio = bio_copy_user_iov(q, map_data, iter, gfp_mask);
  96. else
  97. bio = bio_map_user_iov(q, iter, gfp_mask);
  98. if (IS_ERR(bio))
  99. return PTR_ERR(bio);
  100. if (map_data && map_data->null_mapped)
  101. bio_set_flag(bio, BIO_NULL_MAPPED);
  102. if (bio->bi_iter.bi_size != iter->count) {
  103. /*
  104. * Grab an extra reference to this bio, as bio_unmap_user()
  105. * expects to be able to drop it twice as it happens on the
  106. * normal IO completion path
  107. */
  108. bio_get(bio);
  109. bio_endio(bio);
  110. __blk_rq_unmap_user(bio);
  111. return -EINVAL;
  112. }
  113. if (!bio_flagged(bio, BIO_USER_MAPPED))
  114. rq->cmd_flags |= REQ_COPY_USER;
  115. blk_queue_bounce(q, &bio);
  116. bio_get(bio);
  117. blk_rq_bio_prep(q, rq, bio);
  118. return 0;
  119. }
  120. EXPORT_SYMBOL(blk_rq_map_user_iov);
  121. int blk_rq_map_user(struct request_queue *q, struct request *rq,
  122. struct rq_map_data *map_data, void __user *ubuf,
  123. unsigned long len, gfp_t gfp_mask)
  124. {
  125. struct iovec iov;
  126. struct iov_iter i;
  127. int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i);
  128. if (unlikely(ret < 0))
  129. return ret;
  130. return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask);
  131. }
  132. EXPORT_SYMBOL(blk_rq_map_user);
  133. /**
  134. * blk_rq_unmap_user - unmap a request with user data
  135. * @bio: start of bio list
  136. *
  137. * Description:
  138. * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
  139. * supply the original rq->bio from the blk_rq_map_user() return, since
  140. * the I/O completion may have changed rq->bio.
  141. */
  142. int blk_rq_unmap_user(struct bio *bio)
  143. {
  144. struct bio *mapped_bio;
  145. int ret = 0, ret2;
  146. while (bio) {
  147. mapped_bio = bio;
  148. if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
  149. mapped_bio = bio->bi_private;
  150. ret2 = __blk_rq_unmap_user(mapped_bio);
  151. if (ret2 && !ret)
  152. ret = ret2;
  153. mapped_bio = bio;
  154. bio = bio->bi_next;
  155. bio_put(mapped_bio);
  156. }
  157. return ret;
  158. }
  159. EXPORT_SYMBOL(blk_rq_unmap_user);
  160. /**
  161. * blk_rq_map_kern - map kernel data to a request, for REQ_TYPE_BLOCK_PC usage
  162. * @q: request queue where request should be inserted
  163. * @rq: request to fill
  164. * @kbuf: the kernel buffer
  165. * @len: length of user data
  166. * @gfp_mask: memory allocation flags
  167. *
  168. * Description:
  169. * Data will be mapped directly if possible. Otherwise a bounce
  170. * buffer is used. Can be called multiple times to append multiple
  171. * buffers.
  172. */
  173. int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
  174. unsigned int len, gfp_t gfp_mask)
  175. {
  176. int reading = rq_data_dir(rq) == READ;
  177. unsigned long addr = (unsigned long) kbuf;
  178. int do_copy = 0;
  179. struct bio *bio;
  180. int ret;
  181. if (len > (queue_max_hw_sectors(q) << 9))
  182. return -EINVAL;
  183. if (!len || !kbuf)
  184. return -EINVAL;
  185. do_copy = !blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf);
  186. if (do_copy)
  187. bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
  188. else
  189. bio = bio_map_kern(q, kbuf, len, gfp_mask);
  190. if (IS_ERR(bio))
  191. return PTR_ERR(bio);
  192. if (!reading)
  193. bio->bi_rw |= REQ_WRITE;
  194. if (do_copy)
  195. rq->cmd_flags |= REQ_COPY_USER;
  196. ret = blk_rq_append_bio(q, rq, bio);
  197. if (unlikely(ret)) {
  198. /* request is too big */
  199. bio_put(bio);
  200. return ret;
  201. }
  202. blk_queue_bounce(q, &rq->bio);
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(blk_rq_map_kern);