All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
To: xen-devel <xen-devel@lists.xensource.com>
Subject: [PATCH] enforce read only on disks
Date: Mon, 05 Jan 2009 17:25:02 +0000	[thread overview]
Message-ID: <4962426E.5060907@eu.citrix.com> (raw)

Hi all,
currently even if you specify 'r' for a disk on your VM config file,
qemu tries to open the file O_RDWR first.
This patch enforces that a disk with mode = 'r' on xenstore is actually
opened O_RDONLY.

Regards,

Stefano Stabellini

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>

diff -r d2ff8a90436e block-raw-posix.c
--- a/block-raw-posix.c	Tue Dec 16 16:03:12 2008 +0000
+++ b/block-raw-posix.c	Mon Jan 05 17:16:34 2009 +0000
@@ -133,7 +133,6 @@
         open_flags |= O_RDWR;
     } else {
         open_flags |= O_RDONLY;
-        bs->read_only = 1;
     }
     if (flags & BDRV_O_CREAT)
         open_flags |= O_CREAT | O_TRUNC;
diff -r d2ff8a90436e block.c
--- a/block.c	Tue Dec 16 16:03:12 2008 +0000
+++ b/block.c	Mon Jan 05 17:16:34 2009 +0000
@@ -376,7 +376,6 @@
     char tmp_filename[PATH_MAX];
     char backing_filename[PATH_MAX];
 
-    bs->read_only = 0;
     bs->is_temporary = 0;
     bs->encrypted = 0;
 
@@ -444,12 +443,14 @@
     bs->total_sectors = 0; /* driver will set if it does not do getlength */
     if (bs->opaque == NULL && drv->instance_size > 0)
         return -1;
-    /* Note: for compatibility, we open disk image files as RDWR, and
-       RDONLY as fallback */
     if (!(flags & BDRV_O_FILE))
-        open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
+        open_flags = flags & BDRV_O_CACHE_MASK;
     else
         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
+    if (bs->read_only)
+        open_flags &= ~BDRV_O_RDWR;
+    else
+        open_flags |= BDRV_O_RDWR;
     ret = drv->bdrv_open(bs, filename, open_flags);
     if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
         ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
@@ -867,6 +868,11 @@
 int bdrv_is_read_only(BlockDriverState *bs)
 {
     return bs->read_only;
+}
+
+void bdrv_set_read_only(BlockDriverState *bs)
+{
+    bs->read_only = 1;
 }
 
 int bdrv_is_sg(BlockDriverState *bs)
diff -r d2ff8a90436e block.h
--- a/block.h	Tue Dec 16 16:03:12 2008 +0000
+++ b/block.h	Mon Jan 05 17:16:34 2009 +0000
@@ -125,6 +125,7 @@
 int bdrv_get_translation_hint(BlockDriverState *bs);
 int bdrv_is_removable(BlockDriverState *bs);
 int bdrv_is_read_only(BlockDriverState *bs);
+void bdrv_set_read_only(BlockDriverState *bs);
 int bdrv_is_sg(BlockDriverState *bs);
 int bdrv_is_inserted(BlockDriverState *bs);
 int bdrv_media_changed(BlockDriverState *bs);
diff -r d2ff8a90436e xenstore.c
--- a/xenstore.c	Tue Dec 16 16:03:12 2008 +0000
+++ b/xenstore.c	Mon Jan 05 17:16:34 2009 +0000
@@ -297,7 +297,7 @@
     char **e_danger = NULL;
     char *buf = NULL;
     char *fpath = NULL, *bpath = NULL,
-        *dev = NULL, *params = NULL, *drv = NULL;
+        *dev = NULL, *params = NULL, *mode = NULL, *drv = NULL;
     int i, any_hdN = 0, ret;
     unsigned int len, num, hd_index, pci_devid = 0;
     BlockDriverState *bs;
@@ -413,6 +413,11 @@
             params = newparams;
 	    format = &bdrv_raw;
         }
+	free(mode);
+	if (pasprintf(&buf, "%s/mode", bpath) == -1)
+	    mode = NULL;
+	else
+	    mode = xs_read(xsh, XBT_NULL, buf, &len);
 
 #if 0
 	/* Phantom VBDs are disabled because the use of paths
@@ -454,6 +459,9 @@
             if (pasprintf(&buf, "%s/params", bpath) != -1)
                 xs_watch(xsh, buf, dev);
         }
+
+        if (mode && strchr(mode, 'w') == NULL)
+            bdrv_set_read_only(bs);
 
         /* open device now if media present */
 #ifdef CONFIG_STUBDOM
@@ -571,6 +579,7 @@
     }
 
  out:
+    free(mode);
     free(danger_type);
     free(params);
     free(dev);

                 reply	other threads:[~2009-01-05 17:25 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=4962426E.5060907@eu.citrix.com \
    --to=stefano.stabellini@eu.citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.