2 * Functions related to mapping data to requests
4 #include <linux/kernel.h>
5 #include <linux/module.h>
7 #include <linux/blkdev.h>
8 #include <scsi/sg.h> /* for struct sg_iovec */
12 int blk_rq_append_bio(struct request_queue
*q
, struct request
*rq
,
16 blk_rq_bio_prep(q
, rq
, bio
);
17 else if (!ll_back_merge_fn(q
, rq
, bio
))
20 rq
->biotail
->bi_next
= bio
;
23 rq
->data_len
+= bio
->bi_size
;
27 EXPORT_SYMBOL(blk_rq_append_bio
);
29 static int __blk_rq_unmap_user(struct bio
*bio
)
34 if (bio_flagged(bio
, BIO_USER_MAPPED
))
37 ret
= bio_uncopy_user(bio
);
43 static int __blk_rq_map_user(struct request_queue
*q
, struct request
*rq
,
44 void __user
*ubuf
, unsigned int len
)
47 unsigned int alignment
;
48 struct bio
*bio
, *orig_bio
;
51 reading
= rq_data_dir(rq
) == READ
;
54 * if alignment requirement is satisfied, map in user pages for
55 * direct dma. else, set up kernel bounce buffers
57 uaddr
= (unsigned long) ubuf
;
58 alignment
= queue_dma_alignment(q
) | q
->dma_pad_mask
;
59 if (!(uaddr
& alignment
) && !(len
& alignment
))
60 bio
= bio_map_user(q
, NULL
, uaddr
, len
, reading
);
62 bio
= bio_copy_user(q
, uaddr
, len
, reading
);
68 blk_queue_bounce(q
, &bio
);
71 * We link the bounce buffer in and could have to traverse it
72 * later so we have to get a ref to prevent it from being freed
76 ret
= blk_rq_append_bio(q
, rq
, bio
);
80 /* if it was boucned we must call the end io function */
82 __blk_rq_unmap_user(orig_bio
);
88 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
89 * @q: request queue where request should be inserted
90 * @rq: request structure to fill
91 * @ubuf: the user buffer
92 * @len: length of user data
95 * Data will be mapped directly for zero copy io, if possible. Otherwise
96 * a kernel bounce buffer is used.
98 * A matching blk_rq_unmap_user() must be issued at the end of io, while
99 * still in process context.
101 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
102 * before being submitted to the device, as pages mapped may be out of
103 * reach. It's the callers responsibility to make sure this happens. The
104 * original bio must be passed back in to blk_rq_unmap_user() for proper
107 int blk_rq_map_user(struct request_queue
*q
, struct request
*rq
,
108 void __user
*ubuf
, unsigned long len
)
110 unsigned long bytes_read
= 0;
111 struct bio
*bio
= NULL
;
114 if (len
> (q
->max_hw_sectors
<< 9))
119 while (bytes_read
!= len
) {
120 unsigned long map_len
, end
, start
;
122 map_len
= min_t(unsigned long, len
- bytes_read
, BIO_MAX_SIZE
);
123 end
= ((unsigned long)ubuf
+ map_len
+ PAGE_SIZE
- 1)
125 start
= (unsigned long)ubuf
>> PAGE_SHIFT
;
128 * A bad offset could cause us to require BIO_MAX_PAGES + 1
129 * pages. If this happens we just lower the requested
130 * mapping len by a page so that we can fit
132 if (end
- start
> BIO_MAX_PAGES
)
133 map_len
-= PAGE_SIZE
;
135 ret
= __blk_rq_map_user(q
, rq
, ubuf
, map_len
);
144 if (!bio_flagged(bio
, BIO_USER_MAPPED
))
145 rq
->cmd_flags
|= REQ_COPY_USER
;
147 rq
->buffer
= rq
->data
= NULL
;
150 blk_rq_unmap_user(bio
);
154 EXPORT_SYMBOL(blk_rq_map_user
);
157 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
158 * @q: request queue where request should be inserted
159 * @rq: request to map data to
160 * @iov: pointer to the iovec
161 * @iov_count: number of elements in the iovec
162 * @len: I/O byte count
165 * Data will be mapped directly for zero copy io, if possible. Otherwise
166 * a kernel bounce buffer is used.
168 * A matching blk_rq_unmap_user() must be issued at the end of io, while
169 * still in process context.
171 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
172 * before being submitted to the device, as pages mapped may be out of
173 * reach. It's the callers responsibility to make sure this happens. The
174 * original bio must be passed back in to blk_rq_unmap_user() for proper
177 int blk_rq_map_user_iov(struct request_queue
*q
, struct request
*rq
,
178 struct sg_iovec
*iov
, int iov_count
, unsigned int len
)
181 int i
, read
= rq_data_dir(rq
) == READ
;
184 if (!iov
|| iov_count
<= 0)
187 for (i
= 0; i
< iov_count
; i
++) {
188 unsigned long uaddr
= (unsigned long)iov
[i
].iov_base
;
190 if (uaddr
& queue_dma_alignment(q
)) {
196 if (unaligned
|| (q
->dma_pad_mask
& len
))
197 bio
= bio_copy_user_iov(q
, iov
, iov_count
, read
);
199 bio
= bio_map_user_iov(q
, NULL
, iov
, iov_count
, read
);
204 if (bio
->bi_size
!= len
) {
210 if (!bio_flagged(bio
, BIO_USER_MAPPED
))
211 rq
->cmd_flags
|= REQ_COPY_USER
;
213 blk_queue_bounce(q
, &bio
);
215 blk_rq_bio_prep(q
, rq
, bio
);
216 rq
->buffer
= rq
->data
= NULL
;
221 * blk_rq_unmap_user - unmap a request with user data
222 * @bio: start of bio list
225 * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
226 * supply the original rq->bio from the blk_rq_map_user() return, since
227 * the io completion may have changed rq->bio.
229 int blk_rq_unmap_user(struct bio
*bio
)
231 struct bio
*mapped_bio
;
236 if (unlikely(bio_flagged(bio
, BIO_BOUNCED
)))
237 mapped_bio
= bio
->bi_private
;
239 ret2
= __blk_rq_unmap_user(mapped_bio
);
250 EXPORT_SYMBOL(blk_rq_unmap_user
);
253 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
254 * @q: request queue where request should be inserted
255 * @rq: request to fill
256 * @kbuf: the kernel buffer
257 * @len: length of user data
258 * @gfp_mask: memory allocation flags
261 * Data will be mapped directly if possible. Otherwise a bounce
264 int blk_rq_map_kern(struct request_queue
*q
, struct request
*rq
, void *kbuf
,
265 unsigned int len
, gfp_t gfp_mask
)
268 unsigned int alignment
;
269 int reading
= rq_data_dir(rq
) == READ
;
273 if (len
> (q
->max_hw_sectors
<< 9))
278 kaddr
= (unsigned long)kbuf
;
279 alignment
= queue_dma_alignment(q
) | q
->dma_pad_mask
;
280 do_copy
= ((kaddr
& alignment
) || (len
& alignment
) ||
281 object_is_on_stack(kbuf
));
284 bio
= bio_copy_kern(q
, kbuf
, len
, gfp_mask
, reading
);
286 bio
= bio_map_kern(q
, kbuf
, len
, gfp_mask
);
291 if (rq_data_dir(rq
) == WRITE
)
292 bio
->bi_rw
|= (1 << BIO_RW
);
295 rq
->cmd_flags
|= REQ_COPY_USER
;
297 blk_rq_bio_prep(q
, rq
, bio
);
298 blk_queue_bounce(q
, &rq
->bio
);
299 rq
->buffer
= rq
->data
= NULL
;
302 EXPORT_SYMBOL(blk_rq_map_kern
);