qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 1/4] raw-posix: always store open flags
Date: Mon, 25 May 2009 09:59:01 +0200	[thread overview]
Message-ID: <20090525075901.GA2936@lst.de> (raw)


Both the Linux floppy and the FreeBSD CDROM host device need to store
the open flags so that they can re-open the device later.  Store the
open flags unconditionally to remove the ifdef mess and simply the
calling conventions for the later patches in the series.


Signed-off-by: Christoph Hellwig <hch@lst.de>

Index: qemu/block/raw-posix.c
===================================================================
--- qemu.orig/block/raw-posix.c	2009-05-25 09:20:12.188814196 +0200
+++ qemu/block/raw-posix.c	2009-05-25 09:20:13.949939473 +0200
@@ -103,17 +103,14 @@ typedef struct BDRVRawState {
     int fd;
     int type;
     unsigned int lseek_err_cnt;
+    int open_flags;
 #if defined(__linux__)
     /* linux floppy specific */
-    int fd_open_flags;
     int64_t fd_open_time;
     int64_t fd_error_time;
     int fd_got_error;
     int fd_media_changed;
 #endif
-#if defined(__FreeBSD__)
-    int cd_open_flags;
-#endif
     uint8_t* aligned_buf;
 } BDRVRawState;
 
@@ -130,32 +127,32 @@ static int raw_is_inserted(BlockDriverSt
 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
 {
     BDRVRawState *s = bs->opaque;
-    int fd, open_flags, ret;
+    int fd, ret;
 
     posix_aio_init();
 
     s->lseek_err_cnt = 0;
 
-    open_flags = O_BINARY;
+    s->open_flags |= O_BINARY;
     if ((flags & BDRV_O_ACCESS) == O_RDWR) {
-        open_flags |= O_RDWR;
+        s->open_flags |= O_RDWR;
     } else {
-        open_flags |= O_RDONLY;
+        s->open_flags |= O_RDONLY;
         bs->read_only = 1;
     }
     if (flags & BDRV_O_CREAT)
-        open_flags |= O_CREAT | O_TRUNC;
+        s->open_flags |= O_CREAT | O_TRUNC;
 
     /* Use O_DSYNC for write-through caching, no flags for write-back caching,
      * and O_DIRECT for no caching. */
     if ((flags & BDRV_O_NOCACHE))
-        open_flags |= O_DIRECT;
+        s->open_flags |= O_DIRECT;
     else if (!(flags & BDRV_O_CACHE_WB))
-        open_flags |= O_DSYNC;
+        s->open_flags |= O_DSYNC;
 
     s->type = FTYPE_FILE;
 
-    fd = open(filename, open_flags, 0644);
+    fd = open(filename, s->open_flags, 0644);
     if (fd < 0) {
         ret = -errno;
         if (ret == -EROFS)
@@ -945,7 +942,7 @@ kern_return_t GetBSDPath( io_iterator_t 
 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
 {
     BDRVRawState *s = bs->opaque;
-    int fd, open_flags, ret;
+    int fd, ret;
 
     posix_aio_init();
 
@@ -975,31 +972,30 @@ static int hdev_open(BlockDriverState *b
             IOObjectRelease( mediaIterator );
     }
 #endif
-    open_flags = O_BINARY;
+    s->open_flags |= O_BINARY;
     if ((flags & BDRV_O_ACCESS) == O_RDWR) {
-        open_flags |= O_RDWR;
+        s->open_flags |= O_RDWR;
     } else {
-        open_flags |= O_RDONLY;
+        s->open_flags |= O_RDONLY;
         bs->read_only = 1;
     }
     /* Use O_DSYNC for write-through caching, no flags for write-back caching,
      * and O_DIRECT for no caching. */
     if ((flags & BDRV_O_NOCACHE))
-        open_flags |= O_DIRECT;
+        s->open_flags |= O_DIRECT;
     else if (!(flags & BDRV_O_CACHE_WB))
-        open_flags |= O_DSYNC;
+        s->open_flags |= O_DSYNC;
 
     s->type = FTYPE_FILE;
 #if defined(__linux__)
     if (strstart(filename, "/dev/cd", NULL)) {
         /* open will not fail even if no CD is inserted */
-        open_flags |= O_NONBLOCK;
+        s->open_flags |= O_NONBLOCK;
         s->type = FTYPE_CD;
     } else if (strstart(filename, "/dev/fd", NULL)) {
         s->type = FTYPE_FD;
-        s->fd_open_flags = open_flags;
         /* open will not fail even if no floppy is inserted */
-        open_flags |= O_NONBLOCK;
+        s->open_flags |= O_NONBLOCK;
 #ifdef CONFIG_AIO
     } else if (strstart(filename, "/dev/sg", NULL)) {
         bs->sg = 1;
@@ -1010,11 +1006,10 @@ static int hdev_open(BlockDriverState *b
     if (strstart(filename, "/dev/cd", NULL) ||
         strstart(filename, "/dev/acd", NULL)) {
         s->type = FTYPE_CD;
-        s->cd_open_flags = open_flags;
     }
 #endif
     s->fd = -1;
-    fd = open(filename, open_flags, 0644);
+    fd = open(filename, s->open_flags, 0644);
     if (fd < 0) {
         ret = -errno;
         if (ret == -EROFS)
@@ -1066,7 +1061,7 @@ static int fd_open(BlockDriverState *bs)
 #endif
             return -EIO;
         }
-        s->fd = open(bs->filename, s->fd_open_flags);
+        s->fd = open(bs->filename, s->open_flags & ~O_NONBLOCK);
         if (s->fd < 0) {
             s->fd_error_time = qemu_get_clock(rt_clock);
             s->fd_got_error = 1;
@@ -1155,7 +1150,7 @@ static int raw_eject(BlockDriverState *b
                 close(s->fd);
                 s->fd = -1;
             }
-            fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
+            fd = open(bs->filename, s->open_flags | O_NONBLOCK);
             if (fd >= 0) {
                 if (ioctl(fd, FDEJECT, 0) < 0)
                     perror("FDEJECT");
@@ -1251,7 +1246,7 @@ static int cd_open(BlockDriverState *bs)
          * FreeBSD seems to not notice sometimes... */
         if (s->fd >= 0)
             close (s->fd);
-        fd = open(bs->filename, s->cd_open_flags, 0644);
+        fd = open(bs->filename, s->open_flags, 0644);
         if (fd < 0) {
             s->fd = -1;
             return -EIO;

                 reply	other threads:[~2009-05-25  7:59 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=20090525075901.GA2936@lst.de \
    --to=hch@lst.de \
    --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).