qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Avi Kivity <avi@redhat.com>
To: Anthony Liguori <anthony@codemonkey.ws>, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 1/3] Add specialized block driver scsi generic API
Date: Sun,  8 Feb 2009 19:59:05 +0200	[thread overview]
Message-ID: <1234115947-31622-2-git-send-email-avi@redhat.com> (raw)
In-Reply-To: <1234115947-31622-1-git-send-email-avi@redhat.com>

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>
---
 block.c           |   22 ++++++++++++++++++++++
 block.h           |    8 ++++++++
 hw/scsi-generic.c |   39 ++++++++++++++++++++++-----------------
 3 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/block.c b/block.c
index 4f4bf7c..774ac2d 100644
--- a/block.c
+++ b/block.c
@@ -1594,3 +1594,25 @@ int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
         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);
+}
diff --git a/block.h b/block.h
index e1927dd..f06d98a 100644
--- a/block.h
+++ b/block.h
@@ -103,6 +103,14 @@ BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
                                  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);
+
 int qemu_key_check(BlockDriverState *bs, const char *name);
 
 /* Ensure contents are flushed to disk.  */
diff --git a/hw/scsi-generic.c b/hw/scsi-generic.c
index 5bf160a..53e8951 100644
--- a/hw/scsi-generic.c
+++ b/hw/scsi-generic.c
@@ -201,6 +201,7 @@ static int execute_command(BlockDriverState *bdrv,
                            SCSIRequest *r, int direction,
 			   BlockDriverCompletionFunc *complete)
 {
+    int ret;
 
     r->io_header.interface_id = 'S';
     r->io_header.dxfer_direction = direction;
@@ -214,25 +215,27 @@ static int execute_command(BlockDriverState *bdrv,
     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 @@ static int get_blocksize(BlockDriverState *bdrv)
     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 @@ static int get_stream_blocksize(BlockDriverState *bdrv)
     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];
-- 
1.6.1.1

  reply	other threads:[~2009-02-08 17:59 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-08 17:59 [Qemu-devel] [PATCH 0/3] Remove ->bdrv_pread() internal block layer API Avi Kivity
2009-02-08 17:59 ` Avi Kivity [this message]
2009-02-08 17:59 ` [Qemu-devel] [PATCH 2/3] Add internal scsi generic block API Avi Kivity
2009-02-08 17:59 ` [Qemu-devel] [PATCH 3/3] Drop internal bdrv_pread()/bdrv_pwrite() APIs Avi Kivity
2009-02-08 19:10   ` [Qemu-devel] " Anthony Liguori
2009-02-08 19:36     ` Avi Kivity
2009-02-08 19:37     ` Avi Kivity
2009-02-08 19:05 ` [Qemu-devel] Re: [PATCH 0/3] Remove ->bdrv_pread() internal block layer API Anthony Liguori
  -- strict thread matches above, loose matches on Subject: below --
2009-03-12 12:57 [Qemu-devel] [PATCH 0/3] Remove ->bdrv_pread() internal block layer API (v2) Avi Kivity
2009-03-12 12:57 ` [Qemu-devel] [PATCH 1/3] Add specialized block driver scsi generic API Avi Kivity
2009-03-14 14:33   ` Christoph Hellwig
2009-03-15 13:54     ` Avi Kivity
2009-03-15 14:43       ` Christoph Hellwig
2009-03-15 14:57         ` Avi Kivity
2009-03-15 15:06           ` Christoph Hellwig
2009-03-16 17:29           ` Jamie Lokier

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=1234115947-31622-2-git-send-email-avi@redhat.com \
    --to=avi@redhat.com \
    --cc=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).