blk-map.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. iov_for_each(iov, i, *iter) {
  80. unsigned long uaddr = (unsigned long) iov.iov_base;
  81. if (!iov.iov_len)
  82. return -EINVAL;
  83. /*
  84. * Keep going so we check length of all segments
  85. */
  86. if ((uaddr & queue_dma_alignment(q)) ||
  87. iovec_gap_to_prv(q, &prv, &iov))
  88. unaligned = 1;
  89. prv.iov_base = iov.iov_base;
  90. prv.iov_len = iov.iov_len;
  91. }
  92. if (unaligned || (q->dma_pad_mask & iter->count) || map_data)
  93. bio = bio_copy_user_iov(q, map_data, iter, gfp_mask);
  94. else
  95. bio = bio_map_user_iov(q, iter, gfp_mask);
  96. if (IS_ERR(bio))
  97. return PTR_ERR(bio);
  98. if (map_data && map_data->null_mapped)
  99. bio_set_flag(bio, BIO_NULL_MAPPED);
  100. if (bio->bi_iter.bi_size != iter->count) {
  101. /*
  102. * Grab an extra reference to this bio, as bio_unmap_user()
  103. * expects to be able to drop it twice as it happens on the
  104. * normal IO completion path
  105. */
  106. bio_get(bio);
  107. bio_endio(bio);
  108. __blk_rq_unmap_user(bio);
  109. return -EINVAL;
  110. }
  111. if (!bio_flagged(bio, BIO_USER_MAPPED))
  112. rq->cmd_flags |= REQ_COPY_USER;
  113. blk_queue_bounce(q, &bio);
  114. bio_get(bio);
  115. blk_rq_bio_prep(q, rq, bio);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL(blk_rq_map_user_iov);
  119. int blk_rq_map_user(struct request_queue *q, struct request *rq,
  120. struct rq_map_data *map_data, void __user *ubuf,
  121. unsigned long len, gfp_t gfp_mask)
  122. {
  123. struct iovec iov;
  124. struct iov_iter i;
  125. int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i);
  126. if (unlikely(ret < 0))
  127. return ret;
  128. return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask);
  129. }
  130. EXPORT_SYMBOL(blk_rq_map_user);
  131. /**
  132. * blk_rq_unmap_user - unmap a request with user data
  133. * @bio: start of bio list
  134. *
  135. * Description:
  136. * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
  137. * supply the original rq->bio from the blk_rq_map_user() return, since
  138. * the I/O completion may have changed rq->bio.
  139. */
  140. int blk_rq_unmap_user(struct bio *bio)
  141. {
  142. struct bio *mapped_bio;
  143. int ret = 0, ret2;
  144. while (bio) {
  145. mapped_bio = bio;
  146. if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
  147. mapped_bio = bio->bi_private;
  148. ret2 = __blk_rq_unmap_user(mapped_bio);
  149. if (ret2 && !ret)
  150. ret = ret2;
  151. mapped_bio = bio;
  152. bio = bio->bi_next;
  153. bio_put(mapped_bio);
  154. }
  155. return ret;
  156. }
  157. EXPORT_SYMBOL(blk_rq_unmap_user);
  158. /**
  159. * blk_rq_map_kern - map kernel data to a request, for REQ_TYPE_BLOCK_PC usage
  160. * @q: request queue where request should be inserted
  161. * @rq: request to fill
  162. * @kbuf: the kernel buffer
  163. * @len: length of user data
  164. * @gfp_mask: memory allocation flags
  165. *
  166. * Description:
  167. * Data will be mapped directly if possible. Otherwise a bounce
  168. * buffer is used. Can be called multiple times to append multiple
  169. * buffers.
  170. */
  171. int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
  172. unsigned int len, gfp_t gfp_mask)
  173. {
  174. int reading = rq_data_dir(rq) == READ;
  175. unsigned long addr = (unsigned long) kbuf;
  176. int do_copy = 0;
  177. struct bio *bio;
  178. int ret;
  179. if (len > (queue_max_hw_sectors(q) << 9))
  180. return -EINVAL;
  181. if (!len || !kbuf)
  182. return -EINVAL;
  183. do_copy = !blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf);
  184. if (do_copy)
  185. bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
  186. else
  187. bio = bio_map_kern(q, kbuf, len, gfp_mask);
  188. if (IS_ERR(bio))
  189. return PTR_ERR(bio);
  190. if (!reading)
  191. bio->bi_rw |= REQ_WRITE;
  192. if (do_copy)
  193. rq->cmd_flags |= REQ_COPY_USER;
  194. ret = blk_rq_append_bio(q, rq, bio);
  195. if (unlikely(ret)) {
  196. /* request is too big */
  197. bio_put(bio);
  198. return ret;
  199. }
  200. blk_queue_bounce(q, &rq->bio);
  201. return 0;
  202. }
  203. EXPORT_SYMBOL(blk_rq_map_kern);