blk-map.c 8.2 KB
Newer Older
J
Jens Axboe 已提交
1 2 3 4 5 6 7
/*
 * Functions related to mapping data to requests
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/bio.h>
#include <linux/blkdev.h>
8
#include <scsi/sg.h>		/* for struct sg_iovec */
J
Jens Axboe 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22

#include "blk.h"

int blk_rq_append_bio(struct request_queue *q, struct request *rq,
		      struct bio *bio)
{
	if (!rq->bio)
		blk_rq_bio_prep(q, rq, bio);
	else if (!ll_back_merge_fn(q, rq, bio))
		return -EINVAL;
	else {
		rq->biotail->bi_next = bio;
		rq->biotail = bio;

23
		rq->__data_len += bio->bi_iter.bi_size;
J
Jens Axboe 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
	}
	return 0;
}

static int __blk_rq_unmap_user(struct bio *bio)
{
	int ret = 0;

	if (bio) {
		if (bio_flagged(bio, BIO_USER_MAPPED))
			bio_unmap_user(bio);
		else
			ret = bio_uncopy_user(bio);
	}

	return ret;
}

static int __blk_rq_map_user(struct request_queue *q, struct request *rq,
43
			     struct rq_map_data *map_data, void __user *ubuf,
44
			     unsigned int len, gfp_t gfp_mask)
J
Jens Axboe 已提交
45 46 47 48 49 50 51 52 53 54 55 56
{
	unsigned long uaddr;
	struct bio *bio, *orig_bio;
	int reading, ret;

	reading = rq_data_dir(rq) == READ;

	/*
	 * if alignment requirement is satisfied, map in user pages for
	 * direct dma. else, set up kernel bounce buffers
	 */
	uaddr = (unsigned long) ubuf;
57
	if (blk_rq_aligned(q, uaddr, len) && !map_data)
58
		bio = bio_map_user(q, NULL, uaddr, len, reading, gfp_mask);
J
Jens Axboe 已提交
59
	else
60
		bio = bio_copy_user(q, map_data, uaddr, len, reading, gfp_mask);
J
Jens Axboe 已提交
61 62 63 64

	if (IS_ERR(bio))
		return PTR_ERR(bio);

65
	if (map_data && map_data->null_mapped)
66 67
		bio->bi_flags |= (1 << BIO_NULL_MAPPED);

J
Jens Axboe 已提交
68 69 70 71 72 73 74 75 76 77 78
	orig_bio = bio;
	blk_queue_bounce(q, &bio);

	/*
	 * We link the bounce buffer in and could have to traverse it
	 * later so we have to get a ref to prevent it from being freed
	 */
	bio_get(bio);

	ret = blk_rq_append_bio(q, rq, bio);
	if (!ret)
79
		return bio->bi_iter.bi_size;
J
Jens Axboe 已提交
80 81 82 83 84 85 86 87 88

	/* if it was boucned we must call the end io function */
	bio_endio(bio, 0);
	__blk_rq_unmap_user(orig_bio);
	bio_put(bio);
	return ret;
}

/**
89
 * blk_rq_map_user - map user data to a request, for REQ_TYPE_BLOCK_PC usage
J
Jens Axboe 已提交
90 91
 * @q:		request queue where request should be inserted
 * @rq:		request structure to fill
92
 * @map_data:   pointer to the rq_map_data holding pages (if necessary)
J
Jens Axboe 已提交
93 94
 * @ubuf:	the user buffer
 * @len:	length of user data
95
 * @gfp_mask:	memory allocation flags
J
Jens Axboe 已提交
96 97
 *
 * Description:
98
 *    Data will be mapped directly for zero copy I/O, if possible. Otherwise
J
Jens Axboe 已提交
99 100
 *    a kernel bounce buffer is used.
 *
101
 *    A matching blk_rq_unmap_user() must be issued at the end of I/O, while
J
Jens Axboe 已提交
102 103 104 105 106 107 108 109 110
 *    still in process context.
 *
 *    Note: The mapped bio may need to be bounced through blk_queue_bounce()
 *    before being submitted to the device, as pages mapped may be out of
 *    reach. It's the callers responsibility to make sure this happens. The
 *    original bio must be passed back in to blk_rq_unmap_user() for proper
 *    unmapping.
 */
int blk_rq_map_user(struct request_queue *q, struct request *rq,
111 112
		    struct rq_map_data *map_data, void __user *ubuf,
		    unsigned long len, gfp_t gfp_mask)
J
Jens Axboe 已提交
113 114 115
{
	unsigned long bytes_read = 0;
	struct bio *bio = NULL;
116
	int ret;
J
Jens Axboe 已提交
117

118
	if (len > (queue_max_hw_sectors(q) << 9))
J
Jens Axboe 已提交
119
		return -EINVAL;
120
	if (!len)
J
Jens Axboe 已提交
121
		return -EINVAL;
122 123 124

	if (!ubuf && (!map_data || !map_data->null_mapped))
		return -EINVAL;
J
Jens Axboe 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

	while (bytes_read != len) {
		unsigned long map_len, end, start;

		map_len = min_t(unsigned long, len - bytes_read, BIO_MAX_SIZE);
		end = ((unsigned long)ubuf + map_len + PAGE_SIZE - 1)
								>> PAGE_SHIFT;
		start = (unsigned long)ubuf >> PAGE_SHIFT;

		/*
		 * A bad offset could cause us to require BIO_MAX_PAGES + 1
		 * pages. If this happens we just lower the requested
		 * mapping len by a page so that we can fit
		 */
		if (end - start > BIO_MAX_PAGES)
			map_len -= PAGE_SIZE;

142
		ret = __blk_rq_map_user(q, rq, map_data, ubuf, map_len,
143
					gfp_mask);
J
Jens Axboe 已提交
144 145 146 147 148 149
		if (ret < 0)
			goto unmap_rq;
		if (!bio)
			bio = rq->bio;
		bytes_read += ret;
		ubuf += ret;
150 151 152

		if (map_data)
			map_data->offset += ret;
J
Jens Axboe 已提交
153 154
	}

155 156
	if (!bio_flagged(bio, BIO_USER_MAPPED))
		rq->cmd_flags |= REQ_COPY_USER;
157

J
Jens Axboe 已提交
158 159 160
	return 0;
unmap_rq:
	blk_rq_unmap_user(bio);
161
	rq->bio = NULL;
J
Jens Axboe 已提交
162 163 164 165 166
	return ret;
}
EXPORT_SYMBOL(blk_rq_map_user);

/**
167
 * blk_rq_map_user_iov - map user data to a request, for REQ_TYPE_BLOCK_PC usage
J
Jens Axboe 已提交
168 169
 * @q:		request queue where request should be inserted
 * @rq:		request to map data to
170
 * @map_data:   pointer to the rq_map_data holding pages (if necessary)
J
Jens Axboe 已提交
171 172 173
 * @iov:	pointer to the iovec
 * @iov_count:	number of elements in the iovec
 * @len:	I/O byte count
174
 * @gfp_mask:	memory allocation flags
J
Jens Axboe 已提交
175 176
 *
 * Description:
177
 *    Data will be mapped directly for zero copy I/O, if possible. Otherwise
J
Jens Axboe 已提交
178 179
 *    a kernel bounce buffer is used.
 *
180
 *    A matching blk_rq_unmap_user() must be issued at the end of I/O, while
J
Jens Axboe 已提交
181 182 183 184 185 186 187 188 189
 *    still in process context.
 *
 *    Note: The mapped bio may need to be bounced through blk_queue_bounce()
 *    before being submitted to the device, as pages mapped may be out of
 *    reach. It's the callers responsibility to make sure this happens. The
 *    original bio must be passed back in to blk_rq_unmap_user() for proper
 *    unmapping.
 */
int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
190
			struct rq_map_data *map_data, const struct sg_iovec *iov,
191
			int iov_count, unsigned int len, gfp_t gfp_mask)
J
Jens Axboe 已提交
192 193
{
	struct bio *bio;
194 195
	int i, read = rq_data_dir(rq) == READ;
	int unaligned = 0;
J
Jens Axboe 已提交
196 197 198 199

	if (!iov || iov_count <= 0)
		return -EINVAL;

200 201 202
	for (i = 0; i < iov_count; i++) {
		unsigned long uaddr = (unsigned long)iov[i].iov_base;

203 204 205
		if (!iov[i].iov_len)
			return -EINVAL;

206 207 208 209
		/*
		 * Keep going so we check length of all segments
		 */
		if (uaddr & queue_dma_alignment(q))
210 211 212
			unaligned = 1;
	}

213 214 215
	if (unaligned || (q->dma_pad_mask & len) || map_data)
		bio = bio_copy_user_iov(q, map_data, iov, iov_count, read,
					gfp_mask);
216
	else
217
		bio = bio_map_user_iov(q, NULL, iov, iov_count, read, gfp_mask);
218

J
Jens Axboe 已提交
219 220 221
	if (IS_ERR(bio))
		return PTR_ERR(bio);

222
	if (bio->bi_iter.bi_size != len) {
223 224 225 226 227 228
		/*
		 * Grab an extra reference to this bio, as bio_unmap_user()
		 * expects to be able to drop it twice as it happens on the
		 * normal IO completion path
		 */
		bio_get(bio);
J
Jens Axboe 已提交
229
		bio_endio(bio, 0);
230
		__blk_rq_unmap_user(bio);
J
Jens Axboe 已提交
231 232 233
		return -EINVAL;
	}

234 235 236
	if (!bio_flagged(bio, BIO_USER_MAPPED))
		rq->cmd_flags |= REQ_COPY_USER;

237
	blk_queue_bounce(q, &bio);
J
Jens Axboe 已提交
238 239 240 241
	bio_get(bio);
	blk_rq_bio_prep(q, rq, bio);
	return 0;
}
242
EXPORT_SYMBOL(blk_rq_map_user_iov);
J
Jens Axboe 已提交
243 244 245 246 247 248 249 250

/**
 * blk_rq_unmap_user - unmap a request with user data
 * @bio:	       start of bio list
 *
 * Description:
 *    Unmap a rq previously mapped by blk_rq_map_user(). The caller must
 *    supply the original rq->bio from the blk_rq_map_user() return, since
251
 *    the I/O completion may have changed rq->bio.
J
Jens Axboe 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
 */
int blk_rq_unmap_user(struct bio *bio)
{
	struct bio *mapped_bio;
	int ret = 0, ret2;

	while (bio) {
		mapped_bio = bio;
		if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
			mapped_bio = bio->bi_private;

		ret2 = __blk_rq_unmap_user(mapped_bio);
		if (ret2 && !ret)
			ret = ret2;

		mapped_bio = bio;
		bio = bio->bi_next;
		bio_put(mapped_bio);
	}

	return ret;
}
EXPORT_SYMBOL(blk_rq_unmap_user);

/**
277
 * blk_rq_map_kern - map kernel data to a request, for REQ_TYPE_BLOCK_PC usage
J
Jens Axboe 已提交
278 279 280 281 282
 * @q:		request queue where request should be inserted
 * @rq:		request to fill
 * @kbuf:	the kernel buffer
 * @len:	length of user data
 * @gfp_mask:	memory allocation flags
283 284 285
 *
 * Description:
 *    Data will be mapped directly if possible. Otherwise a bounce
286
 *    buffer is used. Can be called multiple times to append multiple
287
 *    buffers.
J
Jens Axboe 已提交
288 289 290 291
 */
int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
		    unsigned int len, gfp_t gfp_mask)
{
292
	int reading = rq_data_dir(rq) == READ;
293
	unsigned long addr = (unsigned long) kbuf;
294
	int do_copy = 0;
J
Jens Axboe 已提交
295
	struct bio *bio;
296
	int ret;
J
Jens Axboe 已提交
297

298
	if (len > (queue_max_hw_sectors(q) << 9))
J
Jens Axboe 已提交
299 300 301 302
		return -EINVAL;
	if (!len || !kbuf)
		return -EINVAL;

303
	do_copy = !blk_rq_aligned(q, addr, len) || object_is_on_stack(kbuf);
304 305 306 307 308
	if (do_copy)
		bio = bio_copy_kern(q, kbuf, len, gfp_mask, reading);
	else
		bio = bio_map_kern(q, kbuf, len, gfp_mask);

J
Jens Axboe 已提交
309 310 311
	if (IS_ERR(bio))
		return PTR_ERR(bio);

312
	if (!reading)
313
		bio->bi_rw |= REQ_WRITE;
J
Jens Axboe 已提交
314

315 316 317
	if (do_copy)
		rq->cmd_flags |= REQ_COPY_USER;

318 319 320 321 322 323 324
	ret = blk_rq_append_bio(q, rq, bio);
	if (unlikely(ret)) {
		/* request is too big */
		bio_put(bio);
		return ret;
	}

J
Jens Axboe 已提交
325 326 327 328
	blk_queue_bounce(q, &rq->bio);
	return 0;
}
EXPORT_SYMBOL(blk_rq_map_kern);