xref: /openbmc/linux/block/blk-map.c (revision 46348456c1791053dcbe5a9e21825b10a3c8a8fb)
186db1e29SJens Axboe /*
286db1e29SJens Axboe  * Functions related to mapping data to requests
386db1e29SJens Axboe  */
486db1e29SJens Axboe #include <linux/kernel.h>
586db1e29SJens Axboe #include <linux/module.h>
686db1e29SJens Axboe #include <linux/bio.h>
786db1e29SJens Axboe #include <linux/blkdev.h>
826e49cfcSKent Overstreet #include <linux/uio.h>
986db1e29SJens Axboe 
1086db1e29SJens Axboe #include "blk.h"
1186db1e29SJens Axboe 
12*46348456SSagi Grimberg static bool iovec_gap_to_prv(struct request_queue *q,
13*46348456SSagi Grimberg 			     struct iovec *prv, struct iovec *cur)
14*46348456SSagi Grimberg {
15*46348456SSagi Grimberg 	unsigned long prev_end;
16*46348456SSagi Grimberg 
17*46348456SSagi Grimberg 	if (!queue_virt_boundary(q))
18*46348456SSagi Grimberg 		return false;
19*46348456SSagi Grimberg 
20*46348456SSagi Grimberg 	if (prv->iov_base == NULL && prv->iov_len == 0)
21*46348456SSagi Grimberg 		/* prv is not set - don't check */
22*46348456SSagi Grimberg 		return false;
23*46348456SSagi Grimberg 
24*46348456SSagi Grimberg 	prev_end = (unsigned long)(prv->iov_base + prv->iov_len);
25*46348456SSagi Grimberg 
26*46348456SSagi Grimberg 	return (((unsigned long)cur->iov_base & queue_virt_boundary(q)) ||
27*46348456SSagi Grimberg 		prev_end & queue_virt_boundary(q));
28*46348456SSagi Grimberg }
29*46348456SSagi Grimberg 
3086db1e29SJens Axboe int blk_rq_append_bio(struct request_queue *q, struct request *rq,
3186db1e29SJens Axboe 		      struct bio *bio)
3286db1e29SJens Axboe {
3386db1e29SJens Axboe 	if (!rq->bio)
3486db1e29SJens Axboe 		blk_rq_bio_prep(q, rq, bio);
3586db1e29SJens Axboe 	else if (!ll_back_merge_fn(q, rq, bio))
3686db1e29SJens Axboe 		return -EINVAL;
3786db1e29SJens Axboe 	else {
3886db1e29SJens Axboe 		rq->biotail->bi_next = bio;
3986db1e29SJens Axboe 		rq->biotail = bio;
4086db1e29SJens Axboe 
414f024f37SKent Overstreet 		rq->__data_len += bio->bi_iter.bi_size;
4286db1e29SJens Axboe 	}
4386db1e29SJens Axboe 	return 0;
4486db1e29SJens Axboe }
4586db1e29SJens Axboe 
4686db1e29SJens Axboe static int __blk_rq_unmap_user(struct bio *bio)
4786db1e29SJens Axboe {
4886db1e29SJens Axboe 	int ret = 0;
4986db1e29SJens Axboe 
5086db1e29SJens Axboe 	if (bio) {
5186db1e29SJens Axboe 		if (bio_flagged(bio, BIO_USER_MAPPED))
5286db1e29SJens Axboe 			bio_unmap_user(bio);
5386db1e29SJens Axboe 		else
5486db1e29SJens Axboe 			ret = bio_uncopy_user(bio);
5586db1e29SJens Axboe 	}
5686db1e29SJens Axboe 
5786db1e29SJens Axboe 	return ret;
5886db1e29SJens Axboe }
5986db1e29SJens Axboe 
6086db1e29SJens Axboe /**
61710027a4SRandy Dunlap  * blk_rq_map_user_iov - map user data to a request, for REQ_TYPE_BLOCK_PC usage
6286db1e29SJens Axboe  * @q:		request queue where request should be inserted
6386db1e29SJens Axboe  * @rq:		request to map data to
64152e283fSFUJITA Tomonori  * @map_data:   pointer to the rq_map_data holding pages (if necessary)
6526e49cfcSKent Overstreet  * @iter:	iovec iterator
66a3bce90eSFUJITA Tomonori  * @gfp_mask:	memory allocation flags
6786db1e29SJens Axboe  *
6886db1e29SJens Axboe  * Description:
69710027a4SRandy Dunlap  *    Data will be mapped directly for zero copy I/O, if possible. Otherwise
7086db1e29SJens Axboe  *    a kernel bounce buffer is used.
7186db1e29SJens Axboe  *
72710027a4SRandy Dunlap  *    A matching blk_rq_unmap_user() must be issued at the end of I/O, while
7386db1e29SJens Axboe  *    still in process context.
7486db1e29SJens Axboe  *
7586db1e29SJens Axboe  *    Note: The mapped bio may need to be bounced through blk_queue_bounce()
7686db1e29SJens Axboe  *    before being submitted to the device, as pages mapped may be out of
7786db1e29SJens Axboe  *    reach. It's the callers responsibility to make sure this happens. The
7886db1e29SJens Axboe  *    original bio must be passed back in to blk_rq_unmap_user() for proper
7986db1e29SJens Axboe  *    unmapping.
8086db1e29SJens Axboe  */
8186db1e29SJens Axboe int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
8226e49cfcSKent Overstreet 			struct rq_map_data *map_data,
8326e49cfcSKent Overstreet 			const struct iov_iter *iter, gfp_t gfp_mask)
8486db1e29SJens Axboe {
8586db1e29SJens Axboe 	struct bio *bio;
86afdc1a78SFUJITA Tomonori 	int unaligned = 0;
8726e49cfcSKent Overstreet 	struct iov_iter i;
88*46348456SSagi Grimberg 	struct iovec iov, prv = {.iov_base = NULL, .iov_len = 0};
8986db1e29SJens Axboe 
9026e49cfcSKent Overstreet 	if (!iter || !iter->count)
9186db1e29SJens Axboe 		return -EINVAL;
9286db1e29SJens Axboe 
9326e49cfcSKent Overstreet 	iov_for_each(iov, i, *iter) {
9426e49cfcSKent Overstreet 		unsigned long uaddr = (unsigned long) iov.iov_base;
95afdc1a78SFUJITA Tomonori 
9626e49cfcSKent Overstreet 		if (!iov.iov_len)
9754787556SXiaotian Feng 			return -EINVAL;
9854787556SXiaotian Feng 
996b76106dSBen Hutchings 		/*
1006b76106dSBen Hutchings 		 * Keep going so we check length of all segments
1016b76106dSBen Hutchings 		 */
102*46348456SSagi Grimberg 		if ((uaddr & queue_dma_alignment(q)) ||
103*46348456SSagi Grimberg 		    iovec_gap_to_prv(q, &prv, &iov))
104afdc1a78SFUJITA Tomonori 			unaligned = 1;
105*46348456SSagi Grimberg 
106*46348456SSagi Grimberg 		prv.iov_base = iov.iov_base;
107*46348456SSagi Grimberg 		prv.iov_len = iov.iov_len;
108afdc1a78SFUJITA Tomonori 	}
109afdc1a78SFUJITA Tomonori 
11026e49cfcSKent Overstreet 	if (unaligned || (q->dma_pad_mask & iter->count) || map_data)
11126e49cfcSKent Overstreet 		bio = bio_copy_user_iov(q, map_data, iter, gfp_mask);
112afdc1a78SFUJITA Tomonori 	else
11337f19e57SChristoph Hellwig 		bio = bio_map_user_iov(q, iter, gfp_mask);
114afdc1a78SFUJITA Tomonori 
11586db1e29SJens Axboe 	if (IS_ERR(bio))
11686db1e29SJens Axboe 		return PTR_ERR(bio);
11786db1e29SJens Axboe 
118a0763b27SChristoph Hellwig 	if (map_data && map_data->null_mapped)
119b7c44ed9SJens Axboe 		bio_set_flag(bio, BIO_NULL_MAPPED);
120a0763b27SChristoph Hellwig 
12126e49cfcSKent Overstreet 	if (bio->bi_iter.bi_size != iter->count) {
122c26156b2SJens Axboe 		/*
123c26156b2SJens Axboe 		 * Grab an extra reference to this bio, as bio_unmap_user()
124c26156b2SJens Axboe 		 * expects to be able to drop it twice as it happens on the
125c26156b2SJens Axboe 		 * normal IO completion path
126c26156b2SJens Axboe 		 */
127c26156b2SJens Axboe 		bio_get(bio);
1284246a0b6SChristoph Hellwig 		bio_endio(bio);
12953cc0b29SPetr Vandrovec 		__blk_rq_unmap_user(bio);
13086db1e29SJens Axboe 		return -EINVAL;
13186db1e29SJens Axboe 	}
13286db1e29SJens Axboe 
133f18573abSFUJITA Tomonori 	if (!bio_flagged(bio, BIO_USER_MAPPED))
134f18573abSFUJITA Tomonori 		rq->cmd_flags |= REQ_COPY_USER;
135f18573abSFUJITA Tomonori 
13607359fc6SFUJITA Tomonori 	blk_queue_bounce(q, &bio);
13786db1e29SJens Axboe 	bio_get(bio);
13886db1e29SJens Axboe 	blk_rq_bio_prep(q, rq, bio);
13986db1e29SJens Axboe 	return 0;
14086db1e29SJens Axboe }
141152e283fSFUJITA Tomonori EXPORT_SYMBOL(blk_rq_map_user_iov);
14286db1e29SJens Axboe 
143ddad8dd0SChristoph Hellwig int blk_rq_map_user(struct request_queue *q, struct request *rq,
144ddad8dd0SChristoph Hellwig 		    struct rq_map_data *map_data, void __user *ubuf,
145ddad8dd0SChristoph Hellwig 		    unsigned long len, gfp_t gfp_mask)
146ddad8dd0SChristoph Hellwig {
14726e49cfcSKent Overstreet 	struct iovec iov;
14826e49cfcSKent Overstreet 	struct iov_iter i;
1498f7e885aSAl Viro 	int ret = import_single_range(rq_data_dir(rq), ubuf, len, &iov, &i);
150ddad8dd0SChristoph Hellwig 
1518f7e885aSAl Viro 	if (unlikely(ret < 0))
1528f7e885aSAl Viro 		return ret;
153ddad8dd0SChristoph Hellwig 
15426e49cfcSKent Overstreet 	return blk_rq_map_user_iov(q, rq, map_data, &i, gfp_mask);
155ddad8dd0SChristoph Hellwig }
156ddad8dd0SChristoph Hellwig EXPORT_SYMBOL(blk_rq_map_user);
157ddad8dd0SChristoph Hellwig 
15886db1e29SJens Axboe /**
15986db1e29SJens Axboe  * blk_rq_unmap_user - unmap a request with user data
16086db1e29SJens Axboe  * @bio:	       start of bio list
16186db1e29SJens Axboe  *
16286db1e29SJens Axboe  * Description:
16386db1e29SJens Axboe  *    Unmap a rq previously mapped by blk_rq_map_user(). The caller must
16486db1e29SJens Axboe  *    supply the original rq->bio from the blk_rq_map_user() return, since
165710027a4SRandy Dunlap  *    the I/O completion may have changed rq->bio.
16686db1e29SJens Axboe  */
16786db1e29SJens Axboe int blk_rq_unmap_user(struct bio *bio)
16886db1e29SJens Axboe {
16986db1e29SJens Axboe 	struct bio *mapped_bio;
17086db1e29SJens Axboe 	int ret = 0, ret2;
17186db1e29SJens Axboe 
17286db1e29SJens Axboe 	while (bio) {
17386db1e29SJens Axboe 		mapped_bio = bio;
17486db1e29SJens Axboe 		if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
17586db1e29SJens Axboe 			mapped_bio = bio->bi_private;
17686db1e29SJens Axboe 
17786db1e29SJens Axboe 		ret2 = __blk_rq_unmap_user(mapped_bio);
17886db1e29SJens Axboe 		if (ret2 && !ret)
17986db1e29SJens Axboe 			ret = ret2;
18086db1e29SJens Axboe 
18186db1e29SJens Axboe 		mapped_bio = bio;
18286db1e29SJens Axboe 		bio = bio->bi_next;
18386db1e29SJens Axboe 		bio_put(mapped_bio);
18486db1e29SJens Axboe 	}
18586db1e29SJens Axboe 
18686db1e29SJens Axboe 	return ret;
18786db1e29SJens Axboe }
18886db1e29SJens Axboe EXPORT_SYMBOL(blk_rq_unmap_user);
18986db1e29SJens Axboe 
19086db1e29SJens Axboe /**
191710027a4SRandy Dunlap  * blk_rq_map_kern - map kernel data to a request, for REQ_TYPE_BLOCK_PC usage
19286db1e29SJens Axboe  * @q:		request queue where request should be inserted
19386db1e29SJens Axboe  * @rq:		request to fill
19486db1e29SJens Axboe  * @kbuf:	the kernel buffer
19586db1e29SJens Axboe  * @len:	length of user data
19686db1e29SJens Axboe  * @gfp_mask:	memory allocation flags
19768154e90SFUJITA Tomonori  *
19868154e90SFUJITA Tomonori  * Description:
19968154e90SFUJITA Tomonori  *    Data will be mapped directly if possible. Otherwise a bounce
200e227867fSMasanari Iida  *    buffer is used. Can be called multiple times to append multiple
2013a5a3927SJames Bottomley  *    buffers.
20286db1e29SJens Axboe  */
20386db1e29SJens Axboe int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
20486db1e29SJens Axboe 		    unsigned int len, gfp_t gfp_mask)
20586db1e29SJens Axboe {
20668154e90SFUJITA Tomonori 	int reading = rq_data_dir(rq) == READ;
20714417799SNamhyung Kim 	unsigned long addr = (unsigned long) kbuf;
20868154e90SFUJITA Tomonori 	int do_copy = 0;
20986db1e29SJens Axboe 	struct bio *bio;
2103a5a3927SJames Bottomley 	int ret;
21186db1e29SJens Axboe 
212ae03bf63SMartin K. Petersen 	if (len > (queue_max_hw_sectors(q) << 9))
21386db1e29SJens Axboe 		return -EINVAL;
21486db1e29SJens Axboe 	if (!len || !kbuf)
21586db1e29SJens Axboe 		return -EINVAL;
21686db1e29SJens Axboe 
21714417799SNamhyung Kim 	do_copy = !blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf);
21868154e90SFUJITA Tomonori 	if (do_copy)
21968154e90SFUJITA Tomonori 		bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
22068154e90SFUJITA Tomonori 	else
22186db1e29SJens Axboe 		bio = bio_map_kern(q, kbuf, len, gfp_mask);
22268154e90SFUJITA Tomonori 
22386db1e29SJens Axboe 	if (IS_ERR(bio))
22486db1e29SJens Axboe 		return PTR_ERR(bio);
22586db1e29SJens Axboe 
226609f6ea1Smajianpeng 	if (!reading)
227a45dc2d2SBenny Halevy 		bio->bi_rw |= REQ_WRITE;
22886db1e29SJens Axboe 
22968154e90SFUJITA Tomonori 	if (do_copy)
23068154e90SFUJITA Tomonori 		rq->cmd_flags |= REQ_COPY_USER;
23168154e90SFUJITA Tomonori 
2323a5a3927SJames Bottomley 	ret = blk_rq_append_bio(q, rq, bio);
2333a5a3927SJames Bottomley 	if (unlikely(ret)) {
2343a5a3927SJames Bottomley 		/* request is too big */
2353a5a3927SJames Bottomley 		bio_put(bio);
2363a5a3927SJames Bottomley 		return ret;
2373a5a3927SJames Bottomley 	}
2383a5a3927SJames Bottomley 
23986db1e29SJens Axboe 	blk_queue_bounce(q, &rq->bio);
24086db1e29SJens Axboe 	return 0;
24186db1e29SJens Axboe }
24286db1e29SJens Axboe EXPORT_SYMBOL(blk_rq_map_kern);
243