Skip to contentSkip to conversation

new scsi-generic abstraction, use SG_IO

PATCH Applied Christoph Hellwig 3 participants 4 messages Mar 18, 2009 – Mar 28, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 new scsi-generic abstraction, use SG_IO

Okay, I started looking into how to handle scsi-generic I/O in the new world order.

I think the best is to use the SG_IO ioctl instead of the read/write interface as that allows us to support scsi passthrough on disk/cdrom devices, too. See Hannes patch on the kvm list from August for an example.

Now that we always do ioctls we don't need another abstraction than bdrv_ioctl for the synchronous requests for now, and for asynchronous requests I've added a aio_ioctl abstraction keeping it simple.

Long-term we might want to move the ops to a higher-level abstraction and let the low-level code fill out the request header, but I'm lazy enough to leave that to the people trying to support scsi-passthrough on a non-Linux OS.

Tested lightly by issuing various sg_ commands from sg3-utils in a guest to a host CDROM device.

block-raw-posix.c +26 -27
Index: qemu/block-raw-posix.c
===================================================================
@@ -1164,6 +1164,26 @@ static int raw_ioctl(BlockDriverState *b
 
     return ioctl(s->fd, req, buf);
 }
+
+static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
+        unsigned long int req, void *buf,
+        BlockDriverCompletionFunc *cb, void *opaque)
+{
+    RawAIOCB *acb;
+
+    acb = raw_aio_setup(bs, 0, buf, 0, cb, opaque);
+    if (!acb)
+        return NULL;
+
+    acb->aiocb.aio_ioctl_cmd = req;
+    if (qemu_paio_ioctl(&acb->aiocb) < 0) {
+        raw_aio_remove(acb);
+        return NULL;
+    }
+
+    return &acb->common;
+}
+
 #else
 
 static int fd_open(BlockDriverState *bs)
@@ -1195,33 +1215,15 @@ static int raw_ioctl(BlockDriverState *b
 {
     return -ENOTSUP;
 }
-#endif /* !linux */
-
-static int raw_sg_send_command(BlockDriverState *bs, void *buf, int count)
-{
-    return raw_pwrite(bs, -1, buf, count);
-}
 
-static int raw_sg_recv_response(BlockDriverState *bs, void *buf, int count)
+static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
+        unsigned long int req, void *buf,
+        BlockDriverCompletionFunc *cb, void *opaque)
 {
-    return raw_pread(bs, -1, buf, count);
-}
-
-static BlockDriverAIOCB *raw_sg_aio_read(BlockDriverState *bs,
-                                         void *buf, int count,
-                                         BlockDriverCompletionFunc *cb,
-                                         void *opaque)
-{
-    return raw_aio_read(bs, 0, buf, -(int64_t)count, cb, opaque);
+    return -ENOTSUP;
 }
 
-static BlockDriverAIOCB *raw_sg_aio_write(BlockDriverState *bs,
-                                          void *buf, int count,
-                                          BlockDriverCompletionFunc *cb,
-                                          void *opaque)
-{
-    return raw_aio_write(bs, 0, buf, -(int64_t)count, cb, opaque);
-}
+#endif /* !linux */
 
 BlockDriver bdrv_host_device = {
     .format_name	= "host_device",
@@ -1248,8 +1250,5 @@ BlockDriver bdrv_host_device = {
     .bdrv_set_locked	= raw_set_locked,
     /* generic scsi device */
     .bdrv_ioctl		= raw_ioctl,
-    .bdrv_sg_send_command  = raw_sg_send_command,
-    .bdrv_sg_recv_response = raw_sg_recv_response,
-    .bdrv_sg_aio_read      = raw_sg_aio_read,
-    .bdrv_sg_aio_write     = raw_sg_aio_write,
+    .bdrv_aio_ioctl	= raw_aio_ioctl,
 };
block.c +7 -18
Index: qemu/block.c
===================================================================
@@ -1601,24 +1601,13 @@ int bdrv_ioctl(BlockDriverState *bs, uns
     return -ENOTSUP;
 }
 
-int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count)
+BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
+        unsigned long int req, void *buf,
+        BlockDriverCompletionFunc *cb, void *opaque)
 {
-    return bs->drv->bdrv_sg_send_command(bs, buf, count);
-}
-
-int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count)
-{
-    return bs->drv->bdrv_sg_recv_response(bs, buf, count);
-}
+    BlockDriver *drv = bs->drv;
 
-BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count,
-                                   BlockDriverCompletionFunc *cb, void *opaque)
-{
-    return bs->drv->bdrv_sg_aio_read(bs, buf, count, cb, opaque);
-}
-
-BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count,
-                                    BlockDriverCompletionFunc *cb, void *opaque)
-{
-    return bs->drv->bdrv_sg_aio_write(bs, buf, count, cb, opaque);
+    if (drv && drv->bdrv_aio_ioctl)
+        return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
+    return NULL;
 }
block.h +4 -7
Index: qemu/block.h
===================================================================
@@ -102,12 +102,10 @@ BlockDriverAIOCB *bdrv_aio_write(BlockDr
 void bdrv_aio_cancel(BlockDriverAIOCB *acb);
 
 /* sg packet commands */
-int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count);
-int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count);
-BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count,
-                                   BlockDriverCompletionFunc *cb, void *opaque);
-BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count,
-                                    BlockDriverCompletionFunc *cb, void *opaque);
+int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf);
+BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
+        unsigned long int req, void *buf,
+        BlockDriverCompletionFunc *cb, void *opaque);
 
 /* Ensure contents are flushed to disk.  */
 void bdrv_flush(BlockDriverState *bs);
@@ -169,7 +167,6 @@ int bdrv_snapshot_delete(BlockDriverStat
 int bdrv_snapshot_list(BlockDriverState *bs,
                        QEMUSnapshotInfo **psn_info);
 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
-int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf);
 
 char *get_human_readable_size(char *buf, int buf_size, int64_t size);
 int path_is_absolute(const char *path);
block_int.h +3 -10
Index: qemu/block_int.h
===================================================================
@@ -80,16 +80,9 @@ struct BlockDriver {
 
     /* to control generic scsi devices */
     int (*bdrv_ioctl)(BlockDriverState *bs, unsigned long int req, void *buf);
-    int (*bdrv_sg_send_command)(BlockDriverState *bs, void *buf, int count);
-    int (*bdrv_sg_recv_response)(BlockDriverState *bs, void *buf, int count);
-    BlockDriverAIOCB *(*bdrv_sg_aio_read)(BlockDriverState *bs,
-                                          void *buf, int count,
-                                          BlockDriverCompletionFunc *cb,
-                                          void *opaque);
-    BlockDriverAIOCB *(*bdrv_sg_aio_write)(BlockDriverState *bs,
-                                           void *buf, int count,
-                                           BlockDriverCompletionFunc *cb,
-                                           void *opaque);
+    BlockDriverAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs,
+        unsigned long int req, void *buf,
+        BlockDriverCompletionFunc *cb, void *opaque);
 
     BlockDriverAIOCB *free_aiocb;
     struct BlockDriver *next;
hw/scsi-generic.c +3 -39

66 lines not on this page · raw message

posix-aio-compat.c +66 -34

147 lines not on this page · raw message

posix-aio-compat.h +6 -1
Index: qemu/posix-aio-compat.h
===================================================================
@@ -29,12 +29,16 @@ struct qemu_paiocb
     int aio_fildes;
     void *aio_buf;
     size_t aio_nbytes;
+#define aio_ioctl_cmd   aio_nbytes /* for QEMU_PAIO_IOCTL */
     int ev_signo;
     off_t aio_offset;
 
     /* private */
     TAILQ_ENTRY(qemu_paiocb) node;
-    int is_write;
+    int aio_type;
+#define QEMU_PAIO_READ         0x01
+#define QEMU_PAIO_WRITE        0x02
+#define QEMU_PAIO_IOCTL        0x03
     ssize_t ret;
     int active;
 };
@@ -49,6 +53,7 @@ struct qemu_paioinit
 int qemu_paio_init(struct qemu_paioinit *aioinit);
 int qemu_paio_read(struct qemu_paiocb *aiocb);
 int qemu_paio_write(struct qemu_paiocb *aiocb);
+int qemu_paio_ioctl(struct qemu_paiocb *aiocb);
 int qemu_paio_error(struct qemu_paiocb *aiocb);
 ssize_t qemu_paio_return(struct qemu_paiocb *aiocb);
 int qemu_paio_cancel(int fd, struct qemu_paiocb *aiocb);
#1 lore raw
Quoted 12 lines · Christoph Hellwig

Okay, I started looking into how to handle scsi-generic I/O in the new world order.

I think the best is to use the SG_IO ioctl instead of the read/write interface as that allows us to support scsi passthrough on disk/cdrom devices, too. See Hannes patch on the kvm list from August for an example.

Now that we always do ioctls we don't need another abstraction than bdrv_ioctl for the synchronous requests for now, and for asynchronous requests I've added a aio_ioctl abstraction keeping it simple.

Quoted 8 lines

Long-term we might want to move the ops to a higher-level abstraction and let the low-level code fill out the request header, but I'm lazy enough to leave that to the people trying to support scsi-passthrough on a non-Linux OS.

Tested lightly by issuing various sg_ commands from sg3-utils in a guest to a host CDROM device.

I'm not familiar with SG_IO. Does it allow the device to dma to/from userspace?

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 2 lines · Avi Kivity

I'm not familiar with SG_IO. Does it allow the device to dma to/from userspace?

Yes.

#3 lore raw
In reply to Christoph Hellwig
Quoted 17 lines · Christoph Hellwig

Okay, I started looking into how to handle scsi-generic I/O in the new world order.

I think the best is to use the SG_IO ioctl instead of the read/write interface as that allows us to support scsi passthrough on disk/cdrom devices, too. See Hannes patch on the kvm list from August for an example.

Now that we always do ioctls we don't need another abstraction than bdrv_ioctl for the synchronous requests for now, and for asynchronous requests I've added a aio_ioctl abstraction keeping it simple.

Long-term we might want to move the ops to a higher-level abstraction and let the low-level code fill out the request header, but I'm lazy enough to leave that to the people trying to support scsi-passthrough on a non-Linux OS.

Quoted 6 lines

Tested lightly by issuing various sg_ commands from sg3-utils in a guest to a host CDROM device.

Applied. Thanks.

Regards,

Anthony Liguori