block: rename bio_map_put to blk_mq_map_bio_put
[linux-block.git] / block / blk-map.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
86db1e29
JA
2/*
3 * Functions related to mapping data to requests
4 */
5#include <linux/kernel.h>
68db0cf1 6#include <linux/sched/task_stack.h>
86db1e29
JA
7#include <linux/module.h>
8#include <linux/bio.h>
9#include <linux/blkdev.h>
26e49cfc 10#include <linux/uio.h>
86db1e29
JA
11
12#include "blk.h"
13
130879f1 14struct bio_map_data {
f3256075
CH
15 bool is_our_pages : 1;
16 bool is_null_mapped : 1;
130879f1
CH
17 struct iov_iter iter;
18 struct iovec iov[];
19};
20
21static struct bio_map_data *bio_alloc_map_data(struct iov_iter *data,
22 gfp_t gfp_mask)
23{
24 struct bio_map_data *bmd;
25
26 if (data->nr_segs > UIO_MAXIOV)
27 return NULL;
28
29 bmd = kmalloc(struct_size(bmd, iov, data->nr_segs), gfp_mask);
30 if (!bmd)
31 return NULL;
32 memcpy(bmd->iov, data->iov, sizeof(struct iovec) * data->nr_segs);
33 bmd->iter = *data;
34 bmd->iter.iov = bmd->iov;
35 return bmd;
36}
37
38/**
39 * bio_copy_from_iter - copy all pages from iov_iter to bio
40 * @bio: The &struct bio which describes the I/O as destination
41 * @iter: iov_iter as source
42 *
43 * Copy all pages from iov_iter to bio.
44 * Returns 0 on success, or error on failure.
45 */
46static int bio_copy_from_iter(struct bio *bio, struct iov_iter *iter)
47{
48 struct bio_vec *bvec;
49 struct bvec_iter_all iter_all;
50
51 bio_for_each_segment_all(bvec, bio, iter_all) {
52 ssize_t ret;
53
54 ret = copy_page_from_iter(bvec->bv_page,
55 bvec->bv_offset,
56 bvec->bv_len,
57 iter);
58
59 if (!iov_iter_count(iter))
60 break;
61
62 if (ret < bvec->bv_len)
63 return -EFAULT;
64 }
65
66 return 0;
67}
68
69/**
70 * bio_copy_to_iter - copy all pages from bio to iov_iter
71 * @bio: The &struct bio which describes the I/O as source
72 * @iter: iov_iter as destination
73 *
74 * Copy all pages from bio to iov_iter.
75 * Returns 0 on success, or error on failure.
76 */
77static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter)
78{
79 struct bio_vec *bvec;
80 struct bvec_iter_all iter_all;
81
82 bio_for_each_segment_all(bvec, bio, iter_all) {
83 ssize_t ret;
84
85 ret = copy_page_to_iter(bvec->bv_page,
86 bvec->bv_offset,
87 bvec->bv_len,
88 &iter);
89
90 if (!iov_iter_count(&iter))
91 break;
92
93 if (ret < bvec->bv_len)
94 return -EFAULT;
95 }
96
97 return 0;
98}
99
100/**
101 * bio_uncopy_user - finish previously mapped bio
102 * @bio: bio being terminated
103 *
104 * Free pages allocated from bio_copy_user_iov() and write back data
105 * to user space in case of a read.
106 */
107static int bio_uncopy_user(struct bio *bio)
108{
109 struct bio_map_data *bmd = bio->bi_private;
110 int ret = 0;
111
3310eeba 112 if (!bmd->is_null_mapped) {
130879f1
CH
113 /*
114 * if we're in a workqueue, the request is orphaned, so
115 * don't copy into a random user address space, just free
116 * and return -EINTR so user space doesn't expect any data.
117 */
118 if (!current->mm)
119 ret = -EINTR;
120 else if (bio_data_dir(bio) == READ)
121 ret = bio_copy_to_iter(bio, bmd->iter);
122 if (bmd->is_our_pages)
123 bio_free_pages(bio);
124 }
125 kfree(bmd);
130879f1
CH
126 return ret;
127}
128
7589ad67
CH
129static int bio_copy_user_iov(struct request *rq, struct rq_map_data *map_data,
130 struct iov_iter *iter, gfp_t gfp_mask)
130879f1
CH
131{
132 struct bio_map_data *bmd;
133 struct page *page;
393bb12e 134 struct bio *bio;
130879f1
CH
135 int i = 0, ret;
136 int nr_pages;
137 unsigned int len = iter->count;
138 unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0;
139
140 bmd = bio_alloc_map_data(iter, gfp_mask);
141 if (!bmd)
7589ad67 142 return -ENOMEM;
130879f1
CH
143
144 /*
145 * We need to do a deep copy of the iov_iter including the iovecs.
146 * The caller provided iov might point to an on-stack or otherwise
147 * shortlived one.
148 */
f3256075 149 bmd->is_our_pages = !map_data;
03859717 150 bmd->is_null_mapped = (map_data && map_data->null_mapped);
130879f1 151
5f7136db 152 nr_pages = bio_max_segs(DIV_ROUND_UP(offset + len, PAGE_SIZE));
130879f1
CH
153
154 ret = -ENOMEM;
066ff571 155 bio = bio_kmalloc(nr_pages, gfp_mask);
130879f1
CH
156 if (!bio)
157 goto out_bmd;
066ff571 158 bio_init(bio, NULL, bio->bi_inline_vecs, nr_pages, req_op(rq));
130879f1
CH
159
160 if (map_data) {
f5d632d1 161 nr_pages = 1U << map_data->page_order;
130879f1
CH
162 i = map_data->offset / PAGE_SIZE;
163 }
164 while (len) {
165 unsigned int bytes = PAGE_SIZE;
166
167 bytes -= offset;
168
169 if (bytes > len)
170 bytes = len;
171
172 if (map_data) {
173 if (i == map_data->nr_entries * nr_pages) {
174 ret = -ENOMEM;
7589ad67 175 goto cleanup;
130879f1
CH
176 }
177
178 page = map_data->pages[i / nr_pages];
179 page += (i % nr_pages);
180
181 i++;
182 } else {
ce288e05 183 page = alloc_page(GFP_NOIO | gfp_mask);
130879f1
CH
184 if (!page) {
185 ret = -ENOMEM;
7589ad67 186 goto cleanup;
130879f1
CH
187 }
188 }
189
7589ad67 190 if (bio_add_pc_page(rq->q, bio, page, bytes, offset) < bytes) {
130879f1
CH
191 if (!map_data)
192 __free_page(page);
193 break;
194 }
195
196 len -= bytes;
197 offset = 0;
198 }
199
130879f1
CH
200 if (map_data)
201 map_data->offset += bio->bi_iter.bi_size;
202
203 /*
204 * success
205 */
206 if ((iov_iter_rw(iter) == WRITE &&
207 (!map_data || !map_data->null_mapped)) ||
208 (map_data && map_data->from_user)) {
209 ret = bio_copy_from_iter(bio, iter);
210 if (ret)
211 goto cleanup;
212 } else {
213 if (bmd->is_our_pages)
214 zero_fill_bio(bio);
215 iov_iter_advance(iter, bio->bi_iter.bi_size);
216 }
217
218 bio->bi_private = bmd;
7589ad67 219
393bb12e 220 ret = blk_rq_append_bio(rq, bio);
7589ad67
CH
221 if (ret)
222 goto cleanup;
7589ad67 223 return 0;
130879f1
CH
224cleanup:
225 if (!map_data)
226 bio_free_pages(bio);
066ff571
CH
227 bio_uninit(bio);
228 kfree(bio);
130879f1
CH
229out_bmd:
230 kfree(bmd);
7589ad67 231 return ret;
130879f1
CH
232}
233
32f1c71b 234static void blk_mq_map_bio_put(struct bio *bio)
8af870aa
JA
235{
236 if (bio->bi_opf & REQ_ALLOC_CACHE) {
237 bio_put(bio);
238 } else {
239 bio_uninit(bio);
240 kfree(bio);
241 }
242}
243
7589ad67
CH
244static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
245 gfp_t gfp_mask)
130879f1 246{
7589ad67 247 unsigned int max_sectors = queue_max_hw_sectors(rq->q);
066ff571 248 unsigned int nr_vecs = iov_iter_npages(iter, BIO_MAX_VECS);
393bb12e 249 struct bio *bio;
130879f1 250 int ret;
7589ad67 251 int j;
130879f1
CH
252
253 if (!iov_iter_count(iter))
7589ad67 254 return -EINVAL;
130879f1 255
8af870aa
JA
256 if (rq->cmd_flags & REQ_POLLED) {
257 blk_opf_t opf = rq->cmd_flags | REQ_ALLOC_CACHE;
258
259 bio = bio_alloc_bioset(NULL, nr_vecs, opf, gfp_mask,
260 &fs_bio_set);
261 if (!bio)
262 return -ENOMEM;
263 } else {
264 bio = bio_kmalloc(nr_vecs, gfp_mask);
265 if (!bio)
266 return -ENOMEM;
267 bio_init(bio, NULL, bio->bi_inline_vecs, nr_vecs, req_op(rq));
268 }
130879f1
CH
269
270 while (iov_iter_count(iter)) {
e88811bc 271 struct page **pages, *stack_pages[UIO_FASTIOV];
130879f1 272 ssize_t bytes;
91e5adda 273 size_t offs;
130879f1
CH
274 int npages;
275
e88811bc
JA
276 if (nr_vecs <= ARRAY_SIZE(stack_pages)) {
277 pages = stack_pages;
278 bytes = iov_iter_get_pages2(iter, pages, LONG_MAX,
279 nr_vecs, &offs);
280 } else {
281 bytes = iov_iter_get_pages_alloc2(iter, &pages,
282 LONG_MAX, &offs);
283 }
130879f1
CH
284 if (unlikely(bytes <= 0)) {
285 ret = bytes ? bytes : -EFAULT;
286 goto out_unmap;
287 }
288
289 npages = DIV_ROUND_UP(offs + bytes, PAGE_SIZE);
290
7ab89db9 291 if (unlikely(offs & queue_dma_alignment(rq->q)))
130879f1 292 j = 0;
7ab89db9 293 else {
130879f1
CH
294 for (j = 0; j < npages; j++) {
295 struct page *page = pages[j];
296 unsigned int n = PAGE_SIZE - offs;
297 bool same_page = false;
298
299 if (n > bytes)
300 n = bytes;
301
7589ad67 302 if (!bio_add_hw_page(rq->q, bio, page, n, offs,
e4581105 303 max_sectors, &same_page)) {
130879f1
CH
304 if (same_page)
305 put_page(page);
306 break;
307 }
308
130879f1
CH
309 bytes -= n;
310 offs = 0;
311 }
130879f1
CH
312 }
313 /*
314 * release the pages we didn't map into the bio, if any
315 */
316 while (j < npages)
317 put_page(pages[j++]);
e88811bc
JA
318 if (pages != stack_pages)
319 kvfree(pages);
130879f1 320 /* couldn't stuff something into bio? */
480cb846
AV
321 if (bytes) {
322 iov_iter_revert(iter, bytes);
130879f1 323 break;
480cb846 324 }
130879f1
CH
325 }
326
393bb12e 327 ret = blk_rq_append_bio(rq, bio);
7589ad67 328 if (ret)
393bb12e 329 goto out_unmap;
7589ad67
CH
330 return 0;
331
130879f1
CH
332 out_unmap:
333 bio_release_pages(bio, false);
32f1c71b 334 blk_mq_map_bio_put(bio);
7589ad67 335 return ret;
130879f1
CH
336}
337
130879f1
CH
338static void bio_invalidate_vmalloc_pages(struct bio *bio)
339{
f358afc5 340#ifdef ARCH_IMPLEMENTS_FLUSH_KERNEL_VMAP_RANGE
130879f1
CH
341 if (bio->bi_private && !op_is_write(bio_op(bio))) {
342 unsigned long i, len = 0;
343
344 for (i = 0; i < bio->bi_vcnt; i++)
345 len += bio->bi_io_vec[i].bv_len;
346 invalidate_kernel_vmap_range(bio->bi_private, len);
347 }
348#endif
349}
350
351static void bio_map_kern_endio(struct bio *bio)
352{
353 bio_invalidate_vmalloc_pages(bio);
066ff571
CH
354 bio_uninit(bio);
355 kfree(bio);
130879f1
CH
356}
357
358/**
359 * bio_map_kern - map kernel address into bio
360 * @q: the struct request_queue for the bio
361 * @data: pointer to buffer to map
362 * @len: length in bytes
363 * @gfp_mask: allocation flags for bio allocation
364 *
365 * Map the kernel address into a bio suitable for io to a block
366 * device. Returns an error pointer in case of error.
367 */
368static struct bio *bio_map_kern(struct request_queue *q, void *data,
369 unsigned int len, gfp_t gfp_mask)
370{
371 unsigned long kaddr = (unsigned long)data;
372 unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
373 unsigned long start = kaddr >> PAGE_SHIFT;
374 const int nr_pages = end - start;
375 bool is_vmalloc = is_vmalloc_addr(data);
376 struct page *page;
377 int offset, i;
378 struct bio *bio;
379
066ff571 380 bio = bio_kmalloc(nr_pages, gfp_mask);
130879f1
CH
381 if (!bio)
382 return ERR_PTR(-ENOMEM);
066ff571 383 bio_init(bio, NULL, bio->bi_inline_vecs, nr_pages, 0);
130879f1
CH
384
385 if (is_vmalloc) {
386 flush_kernel_vmap_range(data, len);
387 bio->bi_private = data;
388 }
389
390 offset = offset_in_page(kaddr);
391 for (i = 0; i < nr_pages; i++) {
392 unsigned int bytes = PAGE_SIZE - offset;
393
394 if (len <= 0)
395 break;
396
397 if (bytes > len)
398 bytes = len;
399
400 if (!is_vmalloc)
401 page = virt_to_page(data);
402 else
403 page = vmalloc_to_page(data);
404 if (bio_add_pc_page(q, bio, page, bytes,
405 offset) < bytes) {
406 /* we don't support partial mappings */
066ff571
CH
407 bio_uninit(bio);
408 kfree(bio);
130879f1
CH
409 return ERR_PTR(-EINVAL);
410 }
411
412 data += bytes;
413 len -= bytes;
414 offset = 0;
415 }
416
417 bio->bi_end_io = bio_map_kern_endio;
418 return bio;
419}
420
421static void bio_copy_kern_endio(struct bio *bio)
422{
423 bio_free_pages(bio);
066ff571
CH
424 bio_uninit(bio);
425 kfree(bio);
130879f1
CH
426}
427
428static void bio_copy_kern_endio_read(struct bio *bio)
429{
430 char *p = bio->bi_private;
431 struct bio_vec *bvec;
432 struct bvec_iter_all iter_all;
433
434 bio_for_each_segment_all(bvec, bio, iter_all) {
d24920e2 435 memcpy_from_bvec(p, bvec);
130879f1
CH
436 p += bvec->bv_len;
437 }
438
439 bio_copy_kern_endio(bio);
440}
441
442/**
443 * bio_copy_kern - copy kernel address into bio
444 * @q: the struct request_queue for the bio
445 * @data: pointer to buffer to copy
446 * @len: length in bytes
447 * @gfp_mask: allocation flags for bio and page allocation
448 * @reading: data direction is READ
449 *
450 * copy the kernel address into a bio suitable for io to a block
451 * device. Returns an error pointer in case of error.
452 */
453static struct bio *bio_copy_kern(struct request_queue *q, void *data,
454 unsigned int len, gfp_t gfp_mask, int reading)
455{
456 unsigned long kaddr = (unsigned long)data;
457 unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
458 unsigned long start = kaddr >> PAGE_SHIFT;
459 struct bio *bio;
460 void *p = data;
461 int nr_pages = 0;
462
463 /*
464 * Overflow, abort
465 */
466 if (end < start)
467 return ERR_PTR(-EINVAL);
468
469 nr_pages = end - start;
066ff571 470 bio = bio_kmalloc(nr_pages, gfp_mask);
130879f1
CH
471 if (!bio)
472 return ERR_PTR(-ENOMEM);
066ff571 473 bio_init(bio, NULL, bio->bi_inline_vecs, nr_pages, 0);
130879f1
CH
474
475 while (len) {
476 struct page *page;
477 unsigned int bytes = PAGE_SIZE;
478
479 if (bytes > len)
480 bytes = len;
481
cc8f7fe1 482 page = alloc_page(GFP_NOIO | __GFP_ZERO | gfp_mask);
130879f1
CH
483 if (!page)
484 goto cleanup;
485
486 if (!reading)
487 memcpy(page_address(page), p, bytes);
488
489 if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
490 break;
491
492 len -= bytes;
493 p += bytes;
494 }
495
496 if (reading) {
497 bio->bi_end_io = bio_copy_kern_endio_read;
498 bio->bi_private = data;
499 } else {
500 bio->bi_end_io = bio_copy_kern_endio;
501 }
502
503 return bio;
504
505cleanup:
506 bio_free_pages(bio);
066ff571
CH
507 bio_uninit(bio);
508 kfree(bio);
130879f1
CH
509 return ERR_PTR(-ENOMEM);
510}
511
98d61d5b 512/*
0abc2a10
JA
513 * Append a bio to a passthrough request. Only works if the bio can be merged
514 * into the request based on the driver constraints.
98d61d5b 515 */
393bb12e 516int blk_rq_append_bio(struct request *rq, struct bio *bio)
86db1e29 517{
14ccb66b
CH
518 struct bvec_iter iter;
519 struct bio_vec bv;
520 unsigned int nr_segs = 0;
0abc2a10 521
393bb12e 522 bio_for_each_bvec(bv, bio, iter)
14ccb66b
CH
523 nr_segs++;
524
98d61d5b 525 if (!rq->bio) {
393bb12e 526 blk_rq_bio_prep(rq, bio, nr_segs);
98d61d5b 527 } else {
393bb12e 528 if (!ll_back_merge_fn(rq, bio, nr_segs))
98d61d5b 529 return -EINVAL;
393bb12e
CH
530 rq->biotail->bi_next = bio;
531 rq->biotail = bio;
532 rq->__data_len += (bio)->bi_iter.bi_size;
533 bio_crypt_free_ctx(bio);
86db1e29 534 }
98d61d5b 535
86db1e29
JA
536 return 0;
537}
98d61d5b 538EXPORT_SYMBOL(blk_rq_append_bio);
86db1e29 539
86db1e29 540/**
aebf526b 541 * blk_rq_map_user_iov - map user data to a request, for passthrough requests
86db1e29
JA
542 * @q: request queue where request should be inserted
543 * @rq: request to map data to
152e283f 544 * @map_data: pointer to the rq_map_data holding pages (if necessary)
26e49cfc 545 * @iter: iovec iterator
a3bce90e 546 * @gfp_mask: memory allocation flags
86db1e29
JA
547 *
548 * Description:
710027a4 549 * Data will be mapped directly for zero copy I/O, if possible. Otherwise
86db1e29
JA
550 * a kernel bounce buffer is used.
551 *
710027a4 552 * A matching blk_rq_unmap_user() must be issued at the end of I/O, while
86db1e29 553 * still in process context.
86db1e29
JA
554 */
555int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
26e49cfc
KO
556 struct rq_map_data *map_data,
557 const struct iov_iter *iter, gfp_t gfp_mask)
86db1e29 558{
357f435d
AV
559 bool copy = false;
560 unsigned long align = q->dma_pad_mask | queue_dma_alignment(q);
4d6af73d
CH
561 struct bio *bio = NULL;
562 struct iov_iter i;
69e0927b 563 int ret = -EINVAL;
86db1e29 564
a0ac402c
LT
565 if (!iter_is_iovec(iter))
566 goto fail;
567
357f435d
AV
568 if (map_data)
569 copy = true;
393bb12e
CH
570 else if (blk_queue_may_bounce(q))
571 copy = true;
357f435d
AV
572 else if (iov_iter_alignment(iter) & align)
573 copy = true;
574 else if (queue_virt_boundary(q))
575 copy = queue_virt_boundary(q) & iov_iter_gap_alignment(iter);
afdc1a78 576
4d6af73d
CH
577 i = *iter;
578 do {
7589ad67
CH
579 if (copy)
580 ret = bio_copy_user_iov(rq, map_data, &i, gfp_mask);
581 else
582 ret = bio_map_user_iov(rq, &i, gfp_mask);
4d6af73d
CH
583 if (ret)
584 goto unmap_rq;
585 if (!bio)
586 bio = rq->bio;
587 } while (iov_iter_count(&i));
86db1e29 588
86db1e29 589 return 0;
4d6af73d
CH
590
591unmap_rq:
3b7995a9 592 blk_rq_unmap_user(bio);
a0ac402c 593fail:
4d6af73d 594 rq->bio = NULL;
69e0927b 595 return ret;
86db1e29 596}
152e283f 597EXPORT_SYMBOL(blk_rq_map_user_iov);
86db1e29 598
ddad8dd0
CH
599int blk_rq_map_user(struct request_queue *q, struct request *rq,
600 struct rq_map_data *map_data, void __user *ubuf,
601 unsigned long len, gfp_t gfp_mask)
602{
26e49cfc
KO
603 struct iovec iov;
604 struct iov_iter i;
8f7e885a 605 int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i);
ddad8dd0 606
8f7e885a
AV
607 if (unlikely(ret < 0))
608 return ret;
ddad8dd0 609
26e49cfc 610 return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask);
ddad8dd0
CH
611}
612EXPORT_SYMBOL(blk_rq_map_user);
613
55765402
AG
614int blk_rq_map_user_io(struct request *req, struct rq_map_data *map_data,
615 void __user *ubuf, unsigned long buf_len, gfp_t gfp_mask,
616 bool vec, int iov_count, bool check_iter_count, int rw)
617{
618 int ret = 0;
619
620 if (vec) {
621 struct iovec fast_iov[UIO_FASTIOV];
622 struct iovec *iov = fast_iov;
623 struct iov_iter iter;
624
625 ret = import_iovec(rw, ubuf, iov_count ? iov_count : buf_len,
626 UIO_FASTIOV, &iov, &iter);
627 if (ret < 0)
628 return ret;
629
630 if (iov_count) {
631 /* SG_IO howto says that the shorter of the two wins */
632 iov_iter_truncate(&iter, buf_len);
633 if (check_iter_count && !iov_iter_count(&iter)) {
634 kfree(iov);
635 return -EINVAL;
636 }
637 }
638
639 ret = blk_rq_map_user_iov(req->q, req, map_data, &iter,
640 gfp_mask);
641 kfree(iov);
642 } else if (buf_len) {
643 ret = blk_rq_map_user(req->q, req, map_data, ubuf, buf_len,
644 gfp_mask);
645 }
646 return ret;
647}
648EXPORT_SYMBOL(blk_rq_map_user_io);
649
86db1e29
JA
650/**
651 * blk_rq_unmap_user - unmap a request with user data
652 * @bio: start of bio list
653 *
654 * Description:
655 * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
656 * supply the original rq->bio from the blk_rq_map_user() return, since
710027a4 657 * the I/O completion may have changed rq->bio.
86db1e29
JA
658 */
659int blk_rq_unmap_user(struct bio *bio)
660{
393bb12e 661 struct bio *next_bio;
86db1e29
JA
662 int ret = 0, ret2;
663
664 while (bio) {
3310eeba 665 if (bio->bi_private) {
393bb12e 666 ret2 = bio_uncopy_user(bio);
7b63c052
CH
667 if (ret2 && !ret)
668 ret = ret2;
3310eeba 669 } else {
393bb12e 670 bio_release_pages(bio, bio_data_dir(bio) == READ);
7b63c052 671 }
86db1e29 672
393bb12e 673 next_bio = bio;
86db1e29 674 bio = bio->bi_next;
32f1c71b 675 blk_mq_map_bio_put(next_bio);
86db1e29
JA
676 }
677
678 return ret;
679}
86db1e29
JA
680EXPORT_SYMBOL(blk_rq_unmap_user);
681
682/**
aebf526b 683 * blk_rq_map_kern - map kernel data to a request, for passthrough requests
86db1e29
JA
684 * @q: request queue where request should be inserted
685 * @rq: request to fill
686 * @kbuf: the kernel buffer
687 * @len: length of user data
688 * @gfp_mask: memory allocation flags
68154e90
FT
689 *
690 * Description:
691 * Data will be mapped directly if possible. Otherwise a bounce
e227867f 692 * buffer is used. Can be called multiple times to append multiple
3a5a3927 693 * buffers.
86db1e29
JA
694 */
695int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
696 unsigned int len, gfp_t gfp_mask)
697{
68154e90 698 int reading = rq_data_dir(rq) == READ;
14417799 699 unsigned long addr = (unsigned long) kbuf;
393bb12e 700 struct bio *bio;
3a5a3927 701 int ret;
86db1e29 702
ae03bf63 703 if (len > (queue_max_hw_sectors(q) << 9))
86db1e29
JA
704 return -EINVAL;
705 if (!len || !kbuf)
706 return -EINVAL;
707
393bb12e
CH
708 if (!blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf) ||
709 blk_queue_may_bounce(q))
68154e90
FT
710 bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
711 else
712 bio = bio_map_kern(q, kbuf, len, gfp_mask);
713
86db1e29
JA
714 if (IS_ERR(bio))
715 return PTR_ERR(bio);
716
aebf526b
CH
717 bio->bi_opf &= ~REQ_OP_MASK;
718 bio->bi_opf |= req_op(rq);
86db1e29 719
393bb12e 720 ret = blk_rq_append_bio(rq, bio);
066ff571
CH
721 if (unlikely(ret)) {
722 bio_uninit(bio);
723 kfree(bio);
724 }
393bb12e 725 return ret;
86db1e29 726}
86db1e29 727EXPORT_SYMBOL(blk_rq_map_kern);