diff mbox

block/raw-posix: get right partition size

Message ID 4DDA3153.4070003@amd.com
State New
Headers show

Commit Message

Christoph Egger May 23, 2011, 10:05 a.m. UTC
This does 2 things:
- use the correct way to get the size of a disk device or partition
   (from haad@NetBSD.org)
- if given a block device, use the character device instead.
   (from bouyer@NetBSD.org)

From: Adam Hamsik <haad@netbsd.org>
From: Manuel Bouyer <bouyer@netbsd.org>
Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
diff mbox

Patch

diff --git a/block/raw-posix.c b/block/raw-posix.c
index 6b72470..7cf6461 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -64,6 +64,13 @@ 
 #include <sys/dkio.h>
 #endif
 
+#ifdef __NetBSD__
+#include <sys/ioctl.h>
+#include <sys/disklabel.h>
+#include <sys/dkio.h>
+#include <sys/disk.h>
+#endif
+
 #ifdef __DragonFly__
 #include <sys/ioctl.h>
 #include <sys/diskslice.h>
@@ -141,7 +148,32 @@  static int raw_open_common(BlockDriverState *bs, const char *filename,
 {
     BDRVRawState *s = bs->opaque;
     int fd, ret;
+    struct stat sb;
 
+    if (lstat(filename, &sb) < 0) {
+        fprintf(stderr, "%s: stat failed: %s\n", filename, strerror(errno));
+        return -errno;
+    }
+    if (S_ISLNK(sb.st_mode)) {
+        fprintf(stderr, "%s: symbolic link not supported\n", filename);
+        return -EINVAL;
+    }
+#if defined(__NetBSD__)
+    if (S_ISBLK(sb.st_mode)) {
+        static char namebuf[PATH_MAX];
+        const char *dp = strrchr(filename, '/');
+
+        if (dp == NULL) {
+            snprintf(namebuf, PATH_MAX, "r%s", filename);
+        } else {
+            snprintf(namebuf, PATH_MAX, "%.*s/r%s",
+                (int)(dp - filename), filename, dp + 1);
+        }
+        fprintf(stderr, "%s is a block device", filename);
+        filename = namebuf;
+        fprintf(stderr, ", using %s\n", filename);
+    }
+#endif
     s->open_flags = open_flags | O_BINARY;
     s->open_flags &= ~O_ACCMODE;
     if (bdrv_flags & BDRV_O_RDWR) {
@@ -603,7 +635,7 @@  static int raw_truncate(BlockDriverState *bs, int64_t offset)
     return 0;
 }
 
-#ifdef __OpenBSD__
+#if defined(__OpenBSD__) || defined(__NetBSD__)
 static int64_t raw_getlength(BlockDriverState *bs)
 {
     BDRVRawState *s = bs->opaque;
@@ -613,12 +645,22 @@  static int64_t raw_getlength(BlockDriverState *bs)
     if (fstat(fd, &st))
         return -1;
     if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
-        struct disklabel dl;
+#if defined(__NetBSD__)
+        struct dkwedge_info dkw;
 
-        if (ioctl(fd, DIOCGDINFO, &dl))
-            return -1;
-        return (uint64_t)dl.d_secsize *
-            dl.d_partitions[DISKPART(st.st_rdev)].p_size;
+        if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
+            return dkw.dkw_size * 512;
+        } else {
+#endif
+            struct disklabel dl;
+
+            if (ioctl(fd, DIOCGDINFO, &dl))
+                return -1;
+            return (uint64_t)dl.d_secsize *
+                dl.d_partitions[DISKPART(st.st_rdev)].p_size;
+#if defined(__NetBSD__)
+        }
+#endif
     } else
         return st.st_size;
 }