Commit dd1cab95 authored by Jens Axboe's avatar Jens Axboe

[PATCH] Cleanup blk_rq_map_* interfaces

Change the blk_rq_map_user() and blk_rq_map_kern() interface to require
a previously allocated request to be passed in. This is both more efficient
for multiple iterations of mapping data to the same request, and it is also
a much nicer API.
Signed-off-by: default avatarJens Axboe <axboe@suse.de>
parent b823825e
...@@ -2107,21 +2107,19 @@ EXPORT_SYMBOL(blk_insert_request); ...@@ -2107,21 +2107,19 @@ EXPORT_SYMBOL(blk_insert_request);
* original bio must be passed back in to blk_rq_unmap_user() for proper * original bio must be passed back in to blk_rq_unmap_user() for proper
* unmapping. * unmapping.
*/ */
struct request *blk_rq_map_user(request_queue_t *q, int rw, void __user *ubuf, int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf,
unsigned int len) unsigned int len)
{ {
unsigned long uaddr; unsigned long uaddr;
struct request *rq;
struct bio *bio; struct bio *bio;
int reading;
if (len > (q->max_sectors << 9)) if (len > (q->max_sectors << 9))
return ERR_PTR(-EINVAL); return -EINVAL;
if ((!len && ubuf) || (len && !ubuf)) if (!len || !ubuf)
return ERR_PTR(-EINVAL); return -EINVAL;
rq = blk_get_request(q, rw, __GFP_WAIT); reading = rq_data_dir(rq) == READ;
if (!rq)
return ERR_PTR(-ENOMEM);
/* /*
* if alignment requirement is satisfied, map in user pages for * if alignment requirement is satisfied, map in user pages for
...@@ -2129,9 +2127,9 @@ struct request *blk_rq_map_user(request_queue_t *q, int rw, void __user *ubuf, ...@@ -2129,9 +2127,9 @@ struct request *blk_rq_map_user(request_queue_t *q, int rw, void __user *ubuf,
*/ */
uaddr = (unsigned long) ubuf; uaddr = (unsigned long) ubuf;
if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q))) if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q)))
bio = bio_map_user(q, NULL, uaddr, len, rw == READ); bio = bio_map_user(q, NULL, uaddr, len, reading);
else else
bio = bio_copy_user(q, uaddr, len, rw == READ); bio = bio_copy_user(q, uaddr, len, reading);
if (!IS_ERR(bio)) { if (!IS_ERR(bio)) {
rq->bio = rq->biotail = bio; rq->bio = rq->biotail = bio;
...@@ -2139,14 +2137,13 @@ struct request *blk_rq_map_user(request_queue_t *q, int rw, void __user *ubuf, ...@@ -2139,14 +2137,13 @@ struct request *blk_rq_map_user(request_queue_t *q, int rw, void __user *ubuf,
rq->buffer = rq->data = NULL; rq->buffer = rq->data = NULL;
rq->data_len = len; rq->data_len = len;
return rq; return 0;
} }
/* /*
* bio is the err-ptr * bio is the err-ptr
*/ */
blk_put_request(rq); return PTR_ERR(bio);
return (struct request *) bio;
} }
EXPORT_SYMBOL(blk_rq_map_user); EXPORT_SYMBOL(blk_rq_map_user);
...@@ -2160,7 +2157,7 @@ EXPORT_SYMBOL(blk_rq_map_user); ...@@ -2160,7 +2157,7 @@ EXPORT_SYMBOL(blk_rq_map_user);
* Description: * Description:
* Unmap a request previously mapped by blk_rq_map_user(). * Unmap a request previously mapped by blk_rq_map_user().
*/ */
int blk_rq_unmap_user(struct request *rq, struct bio *bio, unsigned int ulen) int blk_rq_unmap_user(struct bio *bio, unsigned int ulen)
{ {
int ret = 0; int ret = 0;
...@@ -2171,8 +2168,7 @@ int blk_rq_unmap_user(struct request *rq, struct bio *bio, unsigned int ulen) ...@@ -2171,8 +2168,7 @@ int blk_rq_unmap_user(struct request *rq, struct bio *bio, unsigned int ulen)
ret = bio_uncopy_user(bio); ret = bio_uncopy_user(bio);
} }
blk_put_request(rq); return 0;
return ret;
} }
EXPORT_SYMBOL(blk_rq_unmap_user); EXPORT_SYMBOL(blk_rq_unmap_user);
...@@ -2184,24 +2180,21 @@ EXPORT_SYMBOL(blk_rq_unmap_user); ...@@ -2184,24 +2180,21 @@ EXPORT_SYMBOL(blk_rq_unmap_user);
* @kbuf: the kernel buffer * @kbuf: the kernel buffer
* @len: length of user data * @len: length of user data
*/ */
struct request *blk_rq_map_kern(request_queue_t *q, int rw, void *kbuf, int blk_rq_map_kern(request_queue_t *q, struct request *rq, void *kbuf,
unsigned int len, unsigned int gfp_mask) unsigned int len, unsigned int gfp_mask)
{ {
struct request *rq;
struct bio *bio; struct bio *bio;
if (len > (q->max_sectors << 9)) if (len > (q->max_sectors << 9))
return ERR_PTR(-EINVAL); return -EINVAL;
if ((!len && kbuf) || (len && !kbuf)) if (!len || !kbuf)
return ERR_PTR(-EINVAL); return -EINVAL;
rq = blk_get_request(q, rw, gfp_mask);
if (!rq)
return ERR_PTR(-ENOMEM);
bio = bio_map_kern(q, kbuf, len, gfp_mask); bio = bio_map_kern(q, kbuf, len, gfp_mask);
if (!IS_ERR(bio)) { if (IS_ERR(bio))
if (rw) return PTR_ERR(bio);
if (rq_data_dir(rq) == WRITE)
bio->bi_rw |= (1 << BIO_RW); bio->bi_rw |= (1 << BIO_RW);
rq->bio = rq->biotail = bio; rq->bio = rq->biotail = bio;
...@@ -2209,14 +2202,7 @@ struct request *blk_rq_map_kern(request_queue_t *q, int rw, void *kbuf, ...@@ -2209,14 +2202,7 @@ struct request *blk_rq_map_kern(request_queue_t *q, int rw, void *kbuf,
rq->buffer = rq->data = NULL; rq->buffer = rq->data = NULL;
rq->data_len = len; rq->data_len = len;
return rq; return 0;
}
/*
* bio is the err-ptr
*/
blk_put_request(rq);
return (struct request *) bio;
} }
EXPORT_SYMBOL(blk_rq_map_kern); EXPORT_SYMBOL(blk_rq_map_kern);
......
...@@ -216,7 +216,7 @@ static int sg_io(struct file *file, request_queue_t *q, ...@@ -216,7 +216,7 @@ static int sg_io(struct file *file, request_queue_t *q,
struct gendisk *bd_disk, struct sg_io_hdr *hdr) struct gendisk *bd_disk, struct sg_io_hdr *hdr)
{ {
unsigned long start_time; unsigned long start_time;
int reading, writing; int reading, writing, ret;
struct request *rq; struct request *rq;
struct bio *bio; struct bio *bio;
char sense[SCSI_SENSE_BUFFERSIZE]; char sense[SCSI_SENSE_BUFFERSIZE];
...@@ -255,14 +255,17 @@ static int sg_io(struct file *file, request_queue_t *q, ...@@ -255,14 +255,17 @@ static int sg_io(struct file *file, request_queue_t *q,
reading = 1; reading = 1;
break; break;
} }
}
rq = blk_rq_map_user(q, writing ? WRITE : READ, hdr->dxferp, rq = blk_get_request(q, writing ? WRITE : READ, GFP_KERNEL);
hdr->dxfer_len); if (!rq)
return -ENOMEM;
if (IS_ERR(rq)) if (reading || writing) {
return PTR_ERR(rq); ret = blk_rq_map_user(q, rq, hdr->dxferp, hdr->dxfer_len);
} else if (ret)
rq = blk_get_request(q, READ, __GFP_WAIT); goto out;
}
/* /*
* fill in request structure * fill in request structure
...@@ -321,11 +324,13 @@ static int sg_io(struct file *file, request_queue_t *q, ...@@ -321,11 +324,13 @@ static int sg_io(struct file *file, request_queue_t *q,
} }
if (blk_rq_unmap_user(rq, bio, hdr->dxfer_len)) if (blk_rq_unmap_user(rq, bio, hdr->dxfer_len))
return -EFAULT; ret = -EFAULT;
/* may not have succeeded, but output values written to control /* may not have succeeded, but output values written to control
* structure (struct sg_io_hdr). */ * structure (struct sg_io_hdr). */
return 0; out:
blk_put_request(rq);
return ret;
} }
#define OMAX_SB_LEN 16 /* For backward compatibility */ #define OMAX_SB_LEN 16 /* For backward compatibility */
......
...@@ -2097,6 +2097,10 @@ static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf, ...@@ -2097,6 +2097,10 @@ static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
if (!q) if (!q)
return -ENXIO; return -ENXIO;
rq = blk_get_request(q, READ, GFP_KERNEL);
if (!rq)
return -ENOMEM;
cdi->last_sense = 0; cdi->last_sense = 0;
while (nframes) { while (nframes) {
...@@ -2108,9 +2112,9 @@ static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf, ...@@ -2108,9 +2112,9 @@ static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
len = nr * CD_FRAMESIZE_RAW; len = nr * CD_FRAMESIZE_RAW;
rq = blk_rq_map_user(q, READ, ubuf, len); ret = blk_rq_map_user(q, rq, ubuf, len);
if (IS_ERR(rq)) if (ret)
return PTR_ERR(rq); break;
memset(rq->cmd, 0, sizeof(rq->cmd)); memset(rq->cmd, 0, sizeof(rq->cmd));
rq->cmd[0] = GPCMD_READ_CD; rq->cmd[0] = GPCMD_READ_CD;
...@@ -2138,7 +2142,7 @@ static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf, ...@@ -2138,7 +2142,7 @@ static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
cdi->last_sense = s->sense_key; cdi->last_sense = s->sense_key;
} }
if (blk_rq_unmap_user(rq, bio, len)) if (blk_rq_unmap_user(bio, len))
ret = -EFAULT; ret = -EFAULT;
if (ret) if (ret)
...@@ -2149,6 +2153,7 @@ static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf, ...@@ -2149,6 +2153,7 @@ static int cdrom_read_cdda_bpc(struct cdrom_device_info *cdi, __u8 __user *ubuf,
ubuf += len; ubuf += len;
} }
blk_put_request(rq);
return ret; return ret;
} }
......
...@@ -558,10 +558,9 @@ extern void blk_sync_queue(struct request_queue *q); ...@@ -558,10 +558,9 @@ extern void blk_sync_queue(struct request_queue *q);
extern void __blk_stop_queue(request_queue_t *q); extern void __blk_stop_queue(request_queue_t *q);
extern void blk_run_queue(request_queue_t *); extern void blk_run_queue(request_queue_t *);
extern void blk_queue_activity_fn(request_queue_t *, activity_fn *, void *); extern void blk_queue_activity_fn(request_queue_t *, activity_fn *, void *);
extern struct request *blk_rq_map_user(request_queue_t *, int, void __user *, unsigned int); extern int blk_rq_map_user(request_queue_t *, struct request *, void __user *, unsigned int);
extern int blk_rq_unmap_user(struct request *, struct bio *, unsigned int); extern int blk_rq_unmap_user(struct bio *, unsigned int);
extern struct request *blk_rq_map_kern(request_queue_t *, int, void *, extern int blk_rq_map_kern(request_queue_t *, struct request *, void *, unsigned int, unsigned int);
unsigned int, unsigned int);
extern int blk_execute_rq(request_queue_t *, struct gendisk *, struct request *); extern int blk_execute_rq(request_queue_t *, struct gendisk *, struct request *);
static inline request_queue_t *bdev_get_queue(struct block_device *bdev) static inline request_queue_t *bdev_get_queue(struct block_device *bdev)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment