blk-map.c 5.9 KB

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