f375e62
From bcbf10c15f43cfc079622c25ef282688965a7128 Mon Sep 17 00:00:00 2001
f375e62
From: Paolo Bonzini <pbonzini@redhat.com>
f375e62
Date: Tue, 13 Nov 2012 10:34:17 +0100
f375e62
Subject: [PATCH] nbd: fixes to read-only handling
f375e62
f375e62
We do not need BLKROSET if the kernel supports setting flags.
f375e62
Also, always do BLKROSET even for a read-write export, otherwise
f375e62
the read-only state remains "sticky" after the invocation of
f375e62
"qemu-nbd -r".
f375e62
f375e62
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
f375e62
(cherry picked from commit c8969eded252058e90e91f12f75f32aceae46ec9)
f375e62
f375e62
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
f375e62
---
f375e62
 nbd.c | 25 ++++++++++++-------------
f375e62
 1 file changed, 12 insertions(+), 13 deletions(-)
f375e62
f375e62
diff --git a/nbd.c b/nbd.c
f375e62
index 206f75c..19f6cd8 100644
f375e62
--- a/nbd.c
f375e62
+++ b/nbd.c
f375e62
@@ -399,24 +399,23 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
f375e62
         return -serrno;
f375e62
     }
f375e62
 
f375e62
-    if (flags & NBD_FLAG_READ_ONLY) {
f375e62
-        int read_only = 1;
f375e62
-        TRACE("Setting readonly attribute");
f375e62
-
f375e62
-        if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
f375e62
+    if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
f375e62
+        if (errno == ENOTTY) {
f375e62
+            int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
f375e62
+            TRACE("Setting readonly attribute");
f375e62
+
f375e62
+            if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
f375e62
+                int serrno = errno;
f375e62
+                LOG("Failed setting read-only attribute");
f375e62
+                return -serrno;
f375e62
+            }
f375e62
+        } else {
f375e62
             int serrno = errno;
f375e62
-            LOG("Failed setting read-only attribute");
f375e62
+            LOG("Failed setting flags");
f375e62
             return -serrno;
f375e62
         }
f375e62
     }
f375e62
 
f375e62
-    if (ioctl(fd, NBD_SET_FLAGS, flags) < 0
f375e62
-        && errno != ENOTTY) {
f375e62
-        int serrno = errno;
f375e62
-        LOG("Failed setting flags");
f375e62
-        return -serrno;
f375e62
-    }
f375e62
-
f375e62
     TRACE("Negotiation ended");
f375e62
 
f375e62
     return 0;