Patchwork [v3,09/15] nbd: fix error handling in the server

login
register
mail settings
Submitter Paolo Bonzini
Date Oct. 5, 2011, 7:17 a.m.
Message ID <1317799065-29668-10-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/117766/
State New
Headers show

Comments

Paolo Bonzini - Oct. 5, 2011, 7:17 a.m.
bdrv_read and bdrv_write return negative errno values, not -1.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 nbd.c |   21 ++++++++++++---------
 1 files changed, 12 insertions(+), 9 deletions(-)

Patch

diff --git a/nbd.c b/nbd.c
index fb5e424..5df9745 100644
--- a/nbd.c
+++ b/nbd.c
@@ -580,6 +580,7 @@  int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
 {
     struct nbd_request request;
     struct nbd_reply reply;
+    int ret;
 
     TRACE("Reading request.");
 
@@ -618,12 +619,13 @@  int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
     case NBD_CMD_READ:
         TRACE("Request type is READ");
 
-        if (bdrv_read(bs, (request.from + dev_offset) / 512,
-                  data + NBD_REPLY_SIZE,
-                  request.len / 512) == -1) {
+        ret = bdrv_read(bs, (request.from + dev_offset) / 512,
+                        data + NBD_REPLY_SIZE,
+                        request.len / 512);
+        if (ret < 0) {
             LOG("reading from file failed");
-            errno = EINVAL;
-            return -1;
+            reply.error = -ret;
+            request.len = 0;
         }
         *offset += request.len;
 
@@ -666,11 +668,12 @@  int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
         } else {
             TRACE("Writing to device");
 
-            if (bdrv_write(bs, (request.from + dev_offset) / 512,
-                       data, request.len / 512) == -1) {
+            ret = bdrv_write(bs, (request.from + dev_offset) / 512,
+                             data, request.len / 512);
+            if (ret < 0) {
                 LOG("writing to file failed");
-                errno = EINVAL;
-                return -1;
+                reply.error = -ret;
+                request.len = 0;
             }
 
             *offset += request.len;