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