qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] raw_getlength, was [patch] NetBSD and Dragonfly support


From: Christoph Hellwig
Subject: [Qemu-devel] raw_getlength, was [patch] NetBSD and Dragonfly support
Date: Tue, 8 Sep 2009 16:51:42 +0200
User-agent: Mutt/1.3.28i

On Fri, Sep 04, 2009 at 11:30:09AM +0400, Aleksej Saushev wrote:
> --- block-raw-posix.c.orig    2009-07-17 03:56:22 +0300
> +++ block-raw-posix.c 2009-08-30 15:51:48 +0300
> @@ -63,6 +63,11 @@
>  #include <sys/dkio.h>
>  #endif
>  
> +#ifdef __DragonFly__
> +#include <sys/ioctl.h>
> +#include <sys/diskslice.h>
> +#endif

This code already exists in current gemu git HEAD.

>  //#define DEBUG_BLOCK
> @@ -766,6 +771,15 @@ static int64_t  raw_getlength(BlockDrive
>      if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
>  #ifdef DIOCGMEDIASIZE
>       if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
> +#elif defined(DIOCGPART)
> +        {
> +                struct partinfo pi;
> +                if (ioctl(fd, DIOCGPART, &pi) == 0)
> +                        size = pi.media_size;
> +                else
> +                        size = 0;
> +        }
> +        if (size == 0)
>  #endif

Same for this.  But raw_getlength has been turned into an utter
mess.  Any chance you could see if the patch below works on the various
BSD platforms?


Signed-off-by: Christoph Hellwig <address@hidden>

Index: qemu/block/raw-posix.c
===================================================================
--- qemu.orig/block/raw-posix.c 2009-09-08 11:43:05.933253874 -0300
+++ qemu/block/raw-posix.c      2009-09-08 11:45:01.000031738 -0300
@@ -124,9 +124,6 @@ typedef struct BDRVRawState {
 static int fd_open(BlockDriverState *bs);
 static int64_t raw_getlength(BlockDriverState *bs);
 
-#if defined(__FreeBSD__)
-static int cdrom_reopen(BlockDriverState *bs);
-#endif
 
 static int raw_open_common(BlockDriverState *bs, const char *filename,
                            int bdrv_flags, int open_flags)
@@ -614,80 +611,74 @@ static int64_t raw_getlength(BlockDriver
     } else
         return st.st_size;
 }
-#else /* !__OpenBSD__ */
-static int64_t  raw_getlength(BlockDriverState *bs)
+#elif defined(CONFIG_BSD)
+static int64_t raw_getlength(BlockDriverState *bs)
 {
     BDRVRawState *s = bs->opaque;
     int fd = s->fd;
-    int64_t size;
-#ifdef CONFIG_BSD
+    int64_t size = 0;
     struct stat sb;
-#ifdef __FreeBSD__
-    int reopened = 0;
-#endif
-#endif
-#ifdef __sun__
-    struct dk_minfo minfo;
-    int rv;
-#endif
     int ret;
 
     ret = fd_open(bs);
     if (ret < 0)
         return ret;
 
-#ifdef CONFIG_BSD
-#ifdef __FreeBSD__
-again:
-#endif
-    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
+    if (fstat(fd, &sb) != 0 || !(S_IFCHR & sb.st_mode)) {
+        return lseek(fd, 0, SEEK_END);
+
 #ifdef DIOCGMEDIASIZE
-       if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
+    if (!ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
+        return size;
 #elif defined(DIOCGPART)
-        {
-                struct partinfo pi;
-                if (ioctl(fd, DIOCGPART, &pi) == 0)
-                        size = pi.media_size;
-                else
-                        size = 0;
-        }
-        if (size == 0)
+    {
+        struct partinfo pi;
+        if (ioctl(fd, DIOCGPART, &pi) == 0)
+            return pi.media_size;
+    }
 #endif
+
 #ifdef CONFIG_COCOA
-        size = LONG_LONG_MAX;
+    return LONG_LONG_MAX;
 #else
-        size = lseek(fd, 0LL, SEEK_END);
-#endif
-#ifdef __FreeBSD__
-        switch(s->type) {
-        case FTYPE_CD:
-            /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
-            if (size == 2048LL * (unsigned)-1)
-                size = 0;
-            /* XXX no disc?  maybe we need to reopen... */
-            if (size <= 0 && !reopened && cdrom_reopen(bs) >= 0) {
-                reopened = 1;
-                goto again;
-            }
-        }
-#endif
-    } else
+    return lseek(fd, 0LL, SEEK_END);
 #endif
-#ifdef __sun__
+}
+#elif defined(__sun__)
+static int64_t raw_getlength(BlockDriverState *bs)
+{
+    BDRVRawState *s = bs->opaque;
+    struct dk_minfo minfo;
+    int ret;
+
+    ret = fd_open(bs);
+    if (ret < 0)
+        return ret;
+
     /*
-     * use the DKIOCGMEDIAINFO ioctl to read the size.
+     * Use the DKIOCGMEDIAINFO ioctl to read the size.
      */
-    rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
-    if ( rv != -1 ) {
-        size = minfo.dki_lbsize * minfo.dki_capacity;
-    } else /* there are reports that lseek on some devices
-              fails, but irc discussion said that contingency
-              on contingency was overkill */
-#endif
-    {
-        size = lseek(fd, 0, SEEK_END);
-    }
-    return size;
+    ret = ioctl(s->fd, DKIOCGMEDIAINFO, &minfo);
+    if (ret != -1) {
+        return minfo.dki_lbsize * minfo.dki_capacity;
+
+    /*
+     * There are reports that lseek on some devices fails, but irc
+     * irc discussion said that contingency on contingency was overkill.
+     */
+    return lseek(s->fd, 0, SEEK_END);
+}
+#else
+static int64_t raw_getlength(BlockDriverState *bs)
+{
+    BDRVRawState *s = bs->opaque;
+    int ret;
+
+    ret = fd_open(bs);
+    if (ret < 0)
+        return ret;
+
+    return lseek(s->fd, 0, SEEK_END);
 }
 #endif
 
@@ -1240,9 +1231,27 @@ static int cdrom_reopen(BlockDriverState
     return 0;
 }
 
+static int64_t cdrom_getlength(BlockDriverState *bs)
+{
+    int64_t size;
+    int reopened = 0;
+
+    for (;;) {
+        size = raw_getlength(bs);
+
+        /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
+        if (size == 2048LL * (unsigned)-1)
+            size = 0;
+        /* XXX no disc?  maybe we need to reopen... */
+        if (size > 0 || reopened || cdrom_reopen(bs) < 0)
+            return size;
+        reopened = 1;
+    }
+}
+
 static int cdrom_is_inserted(BlockDriverState *bs)
 {
-    return raw_getlength(bs) > 0;
+    return cdrom_getlength(bs) > 0;
 }
 
 static int cdrom_eject(BlockDriverState *bs, int eject_flag)
@@ -1298,7 +1307,7 @@ static BlockDriver bdrv_host_cdrom = {
 
     .bdrv_read          = raw_read,
     .bdrv_write         = raw_write,
-    .bdrv_getlength     = raw_getlength,
+    .bdrv_getlength     = cdrom_getlength,
 
     /* removable device support */
     .bdrv_is_inserted   = cdrom_is_inserted,




reply via email to

[Prev in Thread] Current Thread [Next in Thread]