qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Liguori <anthony@codemonkey.ws>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [6822] Add specialized block driver scsi generic API (Avi Kivity)
Date: Thu, 12 Mar 2009 19:57:09 +0000	[thread overview]
Message-ID: <E1Lhr1h-000302-Di@cvs.savannah.gnu.org> (raw)

Revision: 6822
          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6822
Author:   aliguori
Date:     2009-03-12 19:57:08 +0000 (Thu, 12 Mar 2009)
Log Message:
-----------
Add specialized block driver scsi generic API (Avi Kivity)

When a scsi device is backed by a scsi generic device instead of an
ordinary host block device, the block API is abused in a couple of annoying
ways:

 - nb_sectors is negative, and specifies a byte count instead of a sector count
 - offset is ignored, since scsi-generic is essentially a packet protocol

This overloading makes hacking the block layer difficult.  Remove it by
introducing a new explicit API for scsi-generic devices.  The new API
is still backed by the old implementation, but at least the users are
insulated.

Signed-off-by: Avi Kivity <avi@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

Modified Paths:
--------------
    trunk/block.c
    trunk/block.h
    trunk/hw/scsi-generic.c

Modified: trunk/block.c
===================================================================
--- trunk/block.c	2009-03-11 20:16:16 UTC (rev 6821)
+++ trunk/block.c	2009-03-12 19:57:08 UTC (rev 6822)
@@ -1675,3 +1675,25 @@
         return drv->bdrv_ioctl(bs, req, buf);
     return -ENOTSUP;
 }
+
+int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count)
+{
+    return bdrv_pwrite(bs, -1, buf, count);
+}
+
+int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count)
+{
+    return bdrv_pread(bs, -1, buf, count);
+}
+
+BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count,
+                                   BlockDriverCompletionFunc *cb, void *opaque)
+{
+    return bdrv_aio_read(bs, 0, buf, -(int64_t)count, cb, opaque);
+}
+
+BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count,
+                                    BlockDriverCompletionFunc *cb, void *opaque)
+{
+    return bdrv_aio_write(bs, 0, buf, -(int64_t)count, cb, opaque);
+}

Modified: trunk/block.h
===================================================================
--- trunk/block.h	2009-03-11 20:16:16 UTC (rev 6821)
+++ trunk/block.h	2009-03-12 19:57:08 UTC (rev 6822)
@@ -101,6 +101,14 @@
                                  BlockDriverCompletionFunc *cb, void *opaque);
 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);
+
 /* Ensure contents are flushed to disk.  */
 void bdrv_flush(BlockDriverState *bs);
 void bdrv_flush_all(void);

Modified: trunk/hw/scsi-generic.c
===================================================================
--- trunk/hw/scsi-generic.c	2009-03-11 20:16:16 UTC (rev 6821)
+++ trunk/hw/scsi-generic.c	2009-03-12 19:57:08 UTC (rev 6822)
@@ -201,6 +201,7 @@
                            SCSIRequest *r, int direction,
 			   BlockDriverCompletionFunc *complete)
 {
+    int ret;
 
     r->io_header.interface_id = 'S';
     r->io_header.dxfer_direction = direction;
@@ -214,25 +215,27 @@
     r->io_header.usr_ptr = r;
     r->io_header.flags |= SG_FLAG_DIRECT_IO;
 
-    if (bdrv_pwrite(bdrv, -1, &r->io_header, sizeof(r->io_header)) == -1) {
+    ret = bdrv_sg_send_command(bdrv, &r->io_header, sizeof(r->io_header));
+    if (ret < 0) {
         BADF("execute_command: write failed ! (%d)\n", errno);
         return -1;
     }
     if (complete == NULL) {
         int ret;
         r->aiocb = NULL;
-        while ((ret = bdrv_pread(bdrv, -1, &r->io_header,
-                                           sizeof(r->io_header))) == -1 &&
-                      errno == EINTR);
-        if (ret == -1) {
+        while ((ret = bdrv_sg_recv_response(bdrv, &r->io_header,
+                                            sizeof(r->io_header))) < 0 &&
+               ret == -EINTR)
+            ;
+        if (ret < 0) {
             BADF("execute_command: read failed !\n");
             return -1;
         }
         return 0;
     }
 
-    r->aiocb = bdrv_aio_read(bdrv, 0, (uint8_t*)&r->io_header,
-                          -(int64_t)sizeof(r->io_header), complete, r);
+    r->aiocb = bdrv_sg_aio_read(bdrv, (uint8_t*)&r->io_header,
+                                sizeof(r->io_header), complete, r);
     if (r->aiocb == NULL) {
         BADF("execute_command: read failed !\n");
         return -1;
@@ -634,14 +637,15 @@
     io_header.sbp = sensebuf;
     io_header.timeout = 6000; /* XXX */
 
-    ret = bdrv_pwrite(bdrv, -1, &io_header, sizeof(io_header));
-    if (ret == -1)
+    ret = bdrv_sg_send_command(bdrv, &io_header, sizeof(io_header));
+    if (ret < 0)
         return -1;
 
-    while ((ret = bdrv_pread(bdrv, -1, &io_header, sizeof(io_header))) == -1 &&
-           errno == EINTR);
+    while ((ret = bdrv_sg_recv_response(bdrv, &io_header, sizeof(io_header))) < 0 &&
+           ret == -EINTR)
+        ;
 
-    if (ret == -1)
+    if (ret < 0)
         return -1;
 
     return (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
@@ -671,14 +675,15 @@
     io_header.sbp = sensebuf;
     io_header.timeout = 6000; /* XXX */
 
-    ret = bdrv_pwrite(bdrv, -1, &io_header, sizeof(io_header));
-    if (ret == -1)
+    ret = bdrv_sg_send_command(bdrv, &io_header, sizeof(io_header));
+    if (ret < 0)
         return -1;
 
-    while ((ret = bdrv_pread(bdrv, -1, &io_header, sizeof(io_header))) == -1 &&
-           errno == EINTR);
+    while ((ret = bdrv_sg_recv_response(bdrv, &io_header, sizeof(io_header))) < 0 &&
+           ret == -EINTR)
+        ;
 
-    if (ret == -1)
+    if (ret < 0)
         return -1;
 
     return (buf[9] << 16) | (buf[10] << 8) | buf[11];

                 reply	other threads:[~2009-03-12 19:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1Lhr1h-000302-Di@cvs.savannah.gnu.org \
    --to=anthony@codemonkey.ws \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).