blob: f5598322954d03f9bb8404dac84289beb83bbe7b [file] [log] [blame]
Jens Axboe86db1e22008-01-29 14:53:40 +01001/*
2 * Functions related to mapping data to requests
3 */
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/bio.h>
7#include <linux/blkdev.h>
8
9#include "blk.h"
10
11int blk_rq_append_bio(struct request_queue *q, struct request *rq,
12 struct bio *bio)
13{
14 if (!rq->bio)
15 blk_rq_bio_prep(q, rq, bio);
16 else if (!ll_back_merge_fn(q, rq, bio))
17 return -EINVAL;
18 else {
19 rq->biotail->bi_next = bio;
20 rq->biotail = bio;
21
22 rq->data_len += bio->bi_size;
23 }
24 return 0;
25}
26EXPORT_SYMBOL(blk_rq_append_bio);
27
28static int __blk_rq_unmap_user(struct bio *bio)
29{
30 int ret = 0;
31
32 if (bio) {
33 if (bio_flagged(bio, BIO_USER_MAPPED))
34 bio_unmap_user(bio);
35 else
36 ret = bio_uncopy_user(bio);
37 }
38
39 return ret;
40}
41
42static int __blk_rq_map_user(struct request_queue *q, struct request *rq,
43 void __user *ubuf, unsigned int len)
44{
45 unsigned long uaddr;
46 struct bio *bio, *orig_bio;
47 int reading, ret;
48
49 reading = rq_data_dir(rq) == READ;
50
51 /*
52 * if alignment requirement is satisfied, map in user pages for
53 * direct dma. else, set up kernel bounce buffers
54 */
55 uaddr = (unsigned long) ubuf;
Jens Axboe6728cb02008-01-31 13:03:55 +010056 if (!(uaddr & queue_dma_alignment(q)) &&
57 !(len & queue_dma_alignment(q)))
Jens Axboe86db1e22008-01-29 14:53:40 +010058 bio = bio_map_user(q, NULL, uaddr, len, reading);
59 else
60 bio = bio_copy_user(q, uaddr, len, reading);
61
62 if (IS_ERR(bio))
63 return PTR_ERR(bio);
64
65 orig_bio = bio;
66 blk_queue_bounce(q, &bio);
67
68 /*
69 * We link the bounce buffer in and could have to traverse it
70 * later so we have to get a ref to prevent it from being freed
71 */
72 bio_get(bio);
73
74 ret = blk_rq_append_bio(q, rq, bio);
75 if (!ret)
76 return bio->bi_size;
77
78 /* if it was boucned we must call the end io function */
79 bio_endio(bio, 0);
80 __blk_rq_unmap_user(orig_bio);
81 bio_put(bio);
82 return ret;
83}
84
85/**
86 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
87 * @q: request queue where request should be inserted
88 * @rq: request structure to fill
89 * @ubuf: the user buffer
90 * @len: length of user data
91 *
92 * Description:
93 * Data will be mapped directly for zero copy io, if possible. Otherwise
94 * a kernel bounce buffer is used.
95 *
96 * A matching blk_rq_unmap_user() must be issued at the end of io, while
97 * still in process context.
98 *
99 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
100 * before being submitted to the device, as pages mapped may be out of
101 * reach. It's the callers responsibility to make sure this happens. The
102 * original bio must be passed back in to blk_rq_unmap_user() for proper
103 * unmapping.
104 */
105int blk_rq_map_user(struct request_queue *q, struct request *rq,
106 void __user *ubuf, unsigned long len)
107{
108 unsigned long bytes_read = 0;
109 struct bio *bio = NULL;
110 int ret;
111
112 if (len > (q->max_hw_sectors << 9))
113 return -EINVAL;
114 if (!len || !ubuf)
115 return -EINVAL;
116
117 while (bytes_read != len) {
118 unsigned long map_len, end, start;
119
120 map_len = min_t(unsigned long, len - bytes_read, BIO_MAX_SIZE);
121 end = ((unsigned long)ubuf + map_len + PAGE_SIZE - 1)
122 >> PAGE_SHIFT;
123 start = (unsigned long)ubuf >> PAGE_SHIFT;
124
125 /*
126 * A bad offset could cause us to require BIO_MAX_PAGES + 1
127 * pages. If this happens we just lower the requested
128 * mapping len by a page so that we can fit
129 */
130 if (end - start > BIO_MAX_PAGES)
131 map_len -= PAGE_SIZE;
132
133 ret = __blk_rq_map_user(q, rq, ubuf, map_len);
134 if (ret < 0)
135 goto unmap_rq;
136 if (!bio)
137 bio = rq->bio;
138 bytes_read += ret;
139 ubuf += ret;
140 }
141
Tejun Heo40b01b92008-02-19 11:35:38 +0100142 /*
143 * __blk_rq_map_user() copies the buffers if starting address
144 * or length isn't aligned. As the copied buffer is always
145 * page aligned, we know that there's enough room for padding.
146 * Extend the last bio and update rq->data_len accordingly.
147 *
148 * On unmap, bio_uncopy_user() will use unmodified
149 * bio_map_data pointed to by bio->bi_private.
150 */
151 if (len & queue_dma_alignment(q)) {
152 unsigned int pad_len = (queue_dma_alignment(q) & ~len) + 1;
Tejun Heo40b01b92008-02-19 11:35:38 +0100153
FUJITA Tomonori7a85f882008-03-04 11:17:11 +0100154 rq->extra_len += pad_len;
Tejun Heo40b01b92008-02-19 11:35:38 +0100155 }
156
Jens Axboe86db1e22008-01-29 14:53:40 +0100157 rq->buffer = rq->data = NULL;
158 return 0;
159unmap_rq:
160 blk_rq_unmap_user(bio);
Jens Axboe84e9e032008-02-18 13:51:56 +0100161 rq->bio = NULL;
Jens Axboe86db1e22008-01-29 14:53:40 +0100162 return ret;
163}
Jens Axboe86db1e22008-01-29 14:53:40 +0100164EXPORT_SYMBOL(blk_rq_map_user);
165
166/**
167 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
168 * @q: request queue where request should be inserted
169 * @rq: request to map data to
170 * @iov: pointer to the iovec
171 * @iov_count: number of elements in the iovec
172 * @len: I/O byte count
173 *
174 * Description:
175 * Data will be mapped directly for zero copy io, if possible. Otherwise
176 * a kernel bounce buffer is used.
177 *
178 * A matching blk_rq_unmap_user() must be issued at the end of io, while
179 * still in process context.
180 *
181 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
182 * before being submitted to the device, as pages mapped may be out of
183 * reach. It's the callers responsibility to make sure this happens. The
184 * original bio must be passed back in to blk_rq_unmap_user() for proper
185 * unmapping.
186 */
187int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
188 struct sg_iovec *iov, int iov_count, unsigned int len)
189{
190 struct bio *bio;
191
192 if (!iov || iov_count <= 0)
193 return -EINVAL;
194
195 /* we don't allow misaligned data like bio_map_user() does. If the
196 * user is using sg, they're expected to know the alignment constraints
197 * and respect them accordingly */
Jens Axboe6728cb02008-01-31 13:03:55 +0100198 bio = bio_map_user_iov(q, NULL, iov, iov_count,
199 rq_data_dir(rq) == READ);
Jens Axboe86db1e22008-01-29 14:53:40 +0100200 if (IS_ERR(bio))
201 return PTR_ERR(bio);
202
203 if (bio->bi_size != len) {
204 bio_endio(bio, 0);
205 bio_unmap_user(bio);
206 return -EINVAL;
207 }
208
209 bio_get(bio);
210 blk_rq_bio_prep(q, rq, bio);
211 rq->buffer = rq->data = NULL;
212 return 0;
213}
Jens Axboe86db1e22008-01-29 14:53:40 +0100214EXPORT_SYMBOL(blk_rq_map_user_iov);
215
216/**
217 * blk_rq_unmap_user - unmap a request with user data
218 * @bio: start of bio list
219 *
220 * Description:
221 * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
222 * supply the original rq->bio from the blk_rq_map_user() return, since
223 * the io completion may have changed rq->bio.
224 */
225int blk_rq_unmap_user(struct bio *bio)
226{
227 struct bio *mapped_bio;
228 int ret = 0, ret2;
229
230 while (bio) {
231 mapped_bio = bio;
232 if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
233 mapped_bio = bio->bi_private;
234
235 ret2 = __blk_rq_unmap_user(mapped_bio);
236 if (ret2 && !ret)
237 ret = ret2;
238
239 mapped_bio = bio;
240 bio = bio->bi_next;
241 bio_put(mapped_bio);
242 }
243
244 return ret;
245}
Jens Axboe86db1e22008-01-29 14:53:40 +0100246EXPORT_SYMBOL(blk_rq_unmap_user);
247
248/**
249 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
250 * @q: request queue where request should be inserted
251 * @rq: request to fill
252 * @kbuf: the kernel buffer
253 * @len: length of user data
254 * @gfp_mask: memory allocation flags
255 */
256int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
257 unsigned int len, gfp_t gfp_mask)
258{
259 struct bio *bio;
260
261 if (len > (q->max_hw_sectors << 9))
262 return -EINVAL;
263 if (!len || !kbuf)
264 return -EINVAL;
265
266 bio = bio_map_kern(q, kbuf, len, gfp_mask);
267 if (IS_ERR(bio))
268 return PTR_ERR(bio);
269
270 if (rq_data_dir(rq) == WRITE)
271 bio->bi_rw |= (1 << BIO_RW);
272
273 blk_rq_bio_prep(q, rq, bio);
274 blk_queue_bounce(q, &rq->bio);
275 rq->buffer = rq->data = NULL;
276 return 0;
277}
Jens Axboe86db1e22008-01-29 14:53:40 +0100278EXPORT_SYMBOL(blk_rq_map_kern);