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