blk-map.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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;
  103. if (map_data)
  104. copy = true;
  105. else if (iov_iter_alignment(iter) & align)
  106. copy = true;
  107. else if (queue_virt_boundary(q))
  108. copy = queue_virt_boundary(q) & iov_iter_gap_alignment(iter);
  109. i = *iter;
  110. do {
  111. ret =__blk_rq_map_user_iov(rq, map_data, &i, gfp_mask, copy);
  112. if (ret)
  113. goto unmap_rq;
  114. if (!bio)
  115. bio = rq->bio;
  116. } while (iov_iter_count(&i));
  117. if (!bio_flagged(bio, BIO_USER_MAPPED))
  118. rq->cmd_flags |= REQ_COPY_USER;
  119. return 0;
  120. unmap_rq:
  121. __blk_rq_unmap_user(bio);
  122. rq->bio = NULL;
  123. return -EINVAL;
  124. }
  125. EXPORT_SYMBOL(blk_rq_map_user_iov);
  126. int blk_rq_map_user(struct request_queue *q, struct request *rq,
  127. struct rq_map_data *map_data, void __user *ubuf,
  128. unsigned long len, gfp_t gfp_mask)
  129. {
  130. struct iovec iov;
  131. struct iov_iter i;
  132. int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i);
  133. if (unlikely(ret < 0))
  134. return ret;
  135. return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask);
  136. }
  137. EXPORT_SYMBOL(blk_rq_map_user);
  138. /**
  139. * blk_rq_unmap_user - unmap a request with user data
  140. * @bio: start of bio list
  141. *
  142. * Description:
  143. * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
  144. * supply the original rq->bio from the blk_rq_map_user() return, since
  145. * the I/O completion may have changed rq->bio.
  146. */
  147. int blk_rq_unmap_user(struct bio *bio)
  148. {
  149. struct bio *mapped_bio;
  150. int ret = 0, ret2;
  151. while (bio) {
  152. mapped_bio = bio;
  153. if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
  154. mapped_bio = bio->bi_private;
  155. ret2 = __blk_rq_unmap_user(mapped_bio);
  156. if (ret2 && !ret)
  157. ret = ret2;
  158. mapped_bio = bio;
  159. bio = bio->bi_next;
  160. bio_put(mapped_bio);
  161. }
  162. return ret;
  163. }
  164. EXPORT_SYMBOL(blk_rq_unmap_user);
  165. /**
  166. * blk_rq_map_kern - map kernel data to a request, for REQ_TYPE_BLOCK_PC usage
  167. * @q: request queue where request should be inserted
  168. * @rq: request to fill
  169. * @kbuf: the kernel buffer
  170. * @len: length of user data
  171. * @gfp_mask: memory allocation flags
  172. *
  173. * Description:
  174. * Data will be mapped directly if possible. Otherwise a bounce
  175. * buffer is used. Can be called multiple times to append multiple
  176. * buffers.
  177. */
  178. int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
  179. unsigned int len, gfp_t gfp_mask)
  180. {
  181. int reading = rq_data_dir(rq) == READ;
  182. unsigned long addr = (unsigned long) kbuf;
  183. int do_copy = 0;
  184. struct bio *bio;
  185. int ret;
  186. if (len > (queue_max_hw_sectors(q) << 9))
  187. return -EINVAL;
  188. if (!len || !kbuf)
  189. return -EINVAL;
  190. do_copy = !blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf);
  191. if (do_copy)
  192. bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
  193. else
  194. bio = bio_map_kern(q, kbuf, len, gfp_mask);
  195. if (IS_ERR(bio))
  196. return PTR_ERR(bio);
  197. if (!reading)
  198. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  199. if (do_copy)
  200. rq->cmd_flags |= REQ_COPY_USER;
  201. ret = blk_rq_append_bio(rq, bio);
  202. if (unlikely(ret)) {
  203. /* request is too big */
  204. bio_put(bio);
  205. return ret;
  206. }
  207. blk_queue_bounce(q, &rq->bio);
  208. return 0;
  209. }
  210. EXPORT_SYMBOL(blk_rq_map_kern);