qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Samuel Thibault <samuel.thibault@eu.citrix.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH] partial reads/writes and block-raw.c
Date: Fri, 9 Nov 2007 17:17:02 +0000	[thread overview]
Message-ID: <20071109171702.GL3841@implementation.uk.xensource.com> (raw)

block-raw.c currently doesn't cope with partial reads and writes, here
is a patch.

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>

Index: block-raw.c
===================================================================
RCS file: /sources/qemu/qemu/block-raw.c,v
retrieving revision 1.25
diff -u -p -r1.25 block-raw.c
--- block-raw.c	20 Oct 2007 20:40:05 -0000	1.25
+++ block-raw.c	9 Nov 2007 17:16:35 -0000
@@ -142,7 +142,8 @@ static int raw_pread(BlockDriverState *b
                      uint8_t *buf, int count)
 {
     BDRVRawState *s = bs->opaque;
-    int ret;
+    int ret, tries;
+    uint64_t done;
 
     ret = fd_open(bs);
     if (ret < 0)
@@ -160,35 +161,32 @@ static int raw_pread(BlockDriverState *b
     }
     s->lseek_err_cnt=0;
 
-    ret = read(s->fd, buf, count);
-    if (ret == count)
-        goto label__raw_read__success;
-
-    DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
-                      "] read failed %d : %d = %s\n",
-                      s->fd, bs->filename, offset, buf, count,
-                      bs->total_sectors, ret, errno, strerror(errno));
-
-    /* Try harder for CDrom. */
-    if (bs->type == BDRV_TYPE_CDROM) {
-        lseek(s->fd, offset, SEEK_SET);
-        ret = read(s->fd, buf, count);
-        if (ret == count)
-            goto label__raw_read__success;
-        lseek(s->fd, offset, SEEK_SET);
-        ret = read(s->fd, buf, count);
-        if (ret == count)
-            goto label__raw_read__success;
-
-        DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
-                          "] retry read failed %d : %d = %s\n",
-                          s->fd, bs->filename, offset, buf, count,
-                          bs->total_sectors, ret, errno, strerror(errno));
+    tries = 0;
+    for (done = 0; done < count; done += ret) {
+        ret = read(s->fd, buf + done, count - done);
+        if (ret > 0) {
+            tries = 0;
+        } else if (ret == 0) {
+            DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
+                              "] read met end of file\n",
+                              s->fd, bs->filename, offset, buf, count,
+                              bs->total_sectors);
+            return -1;
+        } else {
+            /* Try harder for CDrom. */
+            if (errno == EINTR ||
+                (bs->type == BDRV_TYPE_CDROM && ++tries < 3)) {
+                ret = 0;
+                continue;
+            }
+            DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
+                              "] read failed %d : %d = %s\n",
+                              s->fd, bs->filename, offset, buf, count,
+                              bs->total_sectors, ret, errno, strerror(errno));
+            return -1;
+        }
     }
-
-label__raw_read__success:
-
-    return ret;
+    return count;
 }
 
 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
@@ -196,6 +194,7 @@ static int raw_pwrite(BlockDriverState *
 {
     BDRVRawState *s = bs->opaque;
     int ret;
+    uint64_t done;
 
     ret = fd_open(bs);
     if (ret < 0)
@@ -213,18 +212,21 @@ static int raw_pwrite(BlockDriverState *
     }
     s->lseek_err_cnt = 0;
 
-    ret = write(s->fd, buf, count);
-    if (ret == count)
-        goto label__raw_write__success;
-
-    DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
-                      "] write failed %d : %d = %s\n",
-                      s->fd, bs->filename, offset, buf, count,
-                      bs->total_sectors, ret, errno, strerror(errno));
-
-label__raw_write__success:
-
-    return ret;
+    for (done = 0; done < count; done += ret) {
+        ret = write(s->fd, buf + done, count - done);
+        if (ret == -1) {
+            if (errno == EINTR) {
+                ret = 0;
+                continue;
+            }
+            DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64
+                              "] write failed %d : %d = %s\n",
+                              s->fd, bs->filename, offset, buf, count,
+                              bs->total_sectors, ret, errno, strerror(errno));
+            return -1;
+        }
+    }
+    return count;
 }
 
 /***********************************************************/

                 reply	other threads:[~2007-11-09 17:45 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=20071109171702.GL3841@implementation.uk.xensource.com \
    --to=samuel.thibault@eu.citrix.com \
    --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).