Skip to contentSkip to conversation

raw-posix: add a raw_open_common helper

PATCH In review Christoph Hellwig 2 participants 3 messages May 25, 2009 – Jun 11, 2009 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 2/4 raw-posix: add a raw_open_common helper

raw_open and hdev_open contain the same basic logic. Add a new raw_open_common helper containing the guts of the open routine and call it from raw_open and hdev_open.

We use the new open_flags field in BDRVRawState to allow passing additional open flags to raw_open_common from both.

block/raw-posix.c +19 -29
Index: qemu/block/raw-posix.c
===================================================================
@@ -124,7 +124,8 @@ static int cd_open(BlockDriverState *bs)
 
 static int raw_is_inserted(BlockDriverState *bs);
 
-static int raw_open(BlockDriverState *bs, const char *filename, int flags)
+static int raw_open_common(BlockDriverState *bs, const char *filename,
+        int flags)
 {
     BDRVRawState *s = bs->opaque;
     int fd, ret;
@@ -140,8 +141,6 @@ static int raw_open(BlockDriverState *bs
         s->open_flags |= O_RDONLY;
         bs->read_only = 1;
     }
-    if (flags & BDRV_O_CREAT)
-        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. */
@@ -150,8 +149,7 @@ static int raw_open(BlockDriverState *bs
     else if (!(flags & BDRV_O_CACHE_WB))
         s->open_flags |= O_DSYNC;
 
-    s->type = FTYPE_FILE;
-
+    s->fd = -1;
     fd = open(filename, s->open_flags, 0644);
     if (fd < 0) {
         ret = -errno;
@@ -172,6 +170,17 @@ static int raw_open(BlockDriverState *bs
     return 0;
 }
 
+static int raw_open(BlockDriverState *bs, const char *filename, int flags)
+{
+    BDRVRawState *s = bs->opaque;
+
+    s->type = FTYPE_FILE;
+    if (flags & BDRV_O_CREAT)
+        s->open_flags |= O_CREAT | O_TRUNC;
+
+    return raw_open_common(bs, filename, flags);
+}
+
 /* XXX: use host sector size if necessary with:
 #ifdef DIOCGSECTORSIZE
         {
@@ -942,9 +951,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, ret;
-
-    posix_aio_init();
+    int ret;
 
 #ifdef CONFIG_COCOA
     if (strstart(filename, "/dev/cdrom", NULL)) {
@@ -972,19 +979,6 @@ static int hdev_open(BlockDriverState *b
             IOObjectRelease( mediaIterator );
     }
 #endif
-    s->open_flags |= O_BINARY;
-    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
-        s->open_flags |= O_RDWR;
-    } else {
-        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))
-        s->open_flags |= O_DIRECT;
-    else if (!(flags & BDRV_O_CACHE_WB))
-        s->open_flags |= O_DSYNC;
 
     s->type = FTYPE_FILE;
 #if defined(__linux__)
@@ -1008,15 +1002,11 @@ static int hdev_open(BlockDriverState *b
         s->type = FTYPE_CD;
     }
 #endif
-    s->fd = -1;
-    fd = open(filename, s->open_flags, 0644);
-    if (fd < 0) {
-        ret = -errno;
-        if (ret == -EROFS)
-            ret = -EACCES;
+
+    ret = raw_open_common(bs, filename, flags);
+    if (ret)
         return ret;
-    }
-    s->fd = fd;
+
 #if defined(__FreeBSD__)
     /* make sure the door isnt locked at this time */
     if (s->type == FTYPE_CD)
#1 lore raw
Quoted 35 lines · Christoph Hellwig

raw_open and hdev_open contain the same basic logic. Add a new raw_open_common helper containing the guts of the open routine and call it from raw_open and hdev_open.

We use the new open_flags field in BDRVRawState to allow passing additional open flags to raw_open_common from both.

Index: qemu/block/raw-posix.c
===================================================================

block/raw-posix.c +2 -3
@@ -124,7 +124,8 @@ static int cd_open(BlockDriverState *bs)
 
 static int raw_is_inserted(BlockDriverState *bs);
 
-static int raw_open(BlockDriverState *bs, const char *filename, int flags)
+static int raw_open_common(BlockDriverState *bs, const char *filename,
+        int flags)
 {
     BDRVRawState *s = bs->opaque;
     int fd, ret;
@@ -140,8 +141,6 @@ static int raw_open(BlockDriverState *bs
         s->open_flags |= O_RDONLY;
         bs->read_only = 1;
     }
-    if (flags & BDRV_O_CREAT)
-        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. */

block/raw-posix.c static int raw_open(BlockDriverState *bs · line 155

Quoted 9 lines
block/raw-posix.c +1 -2
@@ -150,8 +149,7 @@ static int raw_open(BlockDriverState *bs
     else if (!(flags & BDRV_O_CACHE_WB))
         s->open_flags |= O_DSYNC;
 
-    s->type = FTYPE_FILE;
-
+    s->fd = -1;
     fd = open(filename, s->open_flags, 0644);
     if (fd < 0) {
         ret = -errno;

it is not equivalent to the current code, while you claim it is. this one will return EROFS to the external world, while old code would return EACCES. If there is any user relying on that return value, we're screwed. And as a matter of fact, it appears to be (bdrv_open2)

Quoted 10 lines
 #ifdef CONFIG_COCOA
     if (strstart(filename, "/dev/cdrom", NULL)) {
block/raw-posix.c +0 -4
@@ -972,19 +979,6 @@ static int hdev_open(BlockDriverState *b
             IOObjectRelease( mediaIterator );
     }
 #endif
-    s->open_flags |= O_BINARY;
-    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
-        s->open_flags |= O_RDWR;
-    } else {

block/raw-posix.c static int hdev_open(BlockDriverState *b · line 982

Quoted 10 lines
block/raw-posix.c +0 -9
-        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))
-        s->open_flags |= O_DIRECT;
-    else if (!(flags & BDRV_O_CACHE_WB))
-        s->open_flags |= O_DSYNC;
 

What happened to those flags? Are you just throwing them away?

#2 lore raw
Quoted 9 lines · Glauber Costa

block/raw-posix.c static int raw_open(BlockDriverState *bs · line 155

Quoted 5 lines
block/raw-posix.c +1 -1
-
+    s->fd = -1;
     fd = open(filename, s->open_flags, 0644);
     if (fd < 0) {
         ret = -errno;

it is not equivalent to the current code, while you claim it is. this one will return EROFS to the external world, while old code would return EACCES. If there is any user relying on that return value, we're screwed. And as a matter of fact, it appears to be (bdrv_open2)

The code to turn -EROFS into -EACCES is also present in raw_open (and thus the new raw_open_common), it just didn't make it into the visible patch hunk.

Quoted 14 lines
Quoted 4 lines
block/raw-posix.c +0 -3
-    s->open_flags |= O_BINARY;
-    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
-        s->open_flags |= O_RDWR;

block/raw-posix.c static int hdev_open(BlockDriverState *b · line 987

Quoted 10 lines
block/raw-posix.c +0 -10
-    } else {
-        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))
-        s->open_flags |= O_DIRECT;
-    else if (!(flags & BDRV_O_CACHE_WB))
-        s->open_flags |= O_DSYNC;
Quoted 1 line

What happened to those flags? Are you just throwing them away?

raw_open_common deals with those, we just consolidated those to one single site.