qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH][RFC][RFT] Let qemu-nbd build on Windows and replace QEMU_IMG/QEMU_NBD with QEMU_TOOL
@ 2008-09-13  1:05 Anthony Liguori
  2008-09-13 15:30 ` [Qemu-devel] " Laurent Vivier
  0 siblings, 1 reply; 5+ messages in thread
From: Anthony Liguori @ 2008-09-13  1:05 UTC (permalink / raw)
  To: qemu-devel@nongnu.org; +Cc: Laurent Vivier

[-- Attachment #1: Type: text/plain, Size: 768 bytes --]

This patch attempts to clean up the mess with qemu-nbd in the Makefile 
in order to get it building and working on Windows.  I checked 
block-nbd, qemu-img, and qemu-nbd on Windows and Linux and everything 
seems to work.  However, there were some defined(QEMU_IMG) that didn't 
include defined(QEMU_NBD) that now are covered by QEMU_TOOL so I'd like 
Laurent to look them over and see if they were intentional.

Basically, there are no longer qemu-nbd-*.o objects.  qemu-img-*.o have 
-DQEMU_TOOL defined and qemu-img/qemu-nbd use identical copies of those 
objects.  A special object is no longer build for nbd.c either.

My longer goal would be to eliminate as many qemu-img-* objects as 
possible but let's take that one step at a time.

Regards,

Anthony Liguori

[-- Attachment #2: qemu-windows-nbd.patch --]
[-- Type: text/x-patch, Size: 15333 bytes --]

Index: block-raw-win32.c
===================================================================
--- block-raw-win32.c	(revision 5200)
+++ block-raw-win32.c	(working copy)
@@ -22,7 +22,7 @@
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
-#ifndef QEMU_IMG
+#ifndef QEMU_TOOL
 #include "qemu-timer.h"
 #include "exec-all.h"
 #endif
@@ -100,7 +100,7 @@
     } else {
         create_flags = OPEN_EXISTING;
     }
-#ifdef QEMU_IMG
+#ifdef QEMU_TOOL
     overlapped = FILE_ATTRIBUTE_NORMAL;
 #else
     overlapped = FILE_FLAG_OVERLAPPED;
@@ -165,7 +165,7 @@
 }
 
 #if 0
-#ifndef QEMU_IMG
+#ifndef QEMU_TOOL
 static void raw_aio_cb(void *opaque)
 {
     RawAIOCB *acb = opaque;
@@ -204,7 +204,7 @@
     acb->ov.OffsetHigh = offset >> 32;
     acb->ov.hEvent = acb->hEvent;
     acb->count = nb_sectors * 512;
-#ifndef QEMU_IMG
+#ifndef QEMU_TOOL
     qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
 #endif
     return acb;
@@ -226,7 +226,7 @@
         qemu_aio_release(acb);
         return NULL;
     }
-#ifdef QEMU_IMG
+#ifdef QEMU_TOOL
     qemu_aio_release(acb);
 #endif
     return (BlockDriverAIOCB *)acb;
@@ -248,7 +248,7 @@
         qemu_aio_release(acb);
         return NULL;
     }
-#ifdef QEMU_IMG
+#ifdef QEMU_TOOL
     qemu_aio_release(acb);
 #endif
     return (BlockDriverAIOCB *)acb;
@@ -256,7 +256,7 @@
 
 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
 {
-#ifndef QEMU_IMG
+#ifndef QEMU_TOOL
     RawAIOCB *acb = (RawAIOCB *)blockacb;
     BlockDriverState *bs = acb->common.bs;
     BDRVRawState *s = bs->opaque;
@@ -356,7 +356,7 @@
 
 void qemu_aio_wait(void)
 {
-#ifndef QEMU_IMG
+#ifndef QEMU_TOOL
     qemu_bh_poll();
 #endif
 }
@@ -458,7 +458,7 @@
     }
     create_flags = OPEN_EXISTING;
 
-#ifdef QEMU_IMG
+#ifdef QEMU_TOOL
     overlapped = FILE_ATTRIBUTE_NORMAL;
 #else
     overlapped = FILE_FLAG_OVERLAPPED;
Index: nbd.c
===================================================================
--- nbd.c	(revision 5200)
+++ nbd.c	(working copy)
@@ -21,28 +21,27 @@
 
 #include <errno.h>
 #include <string.h>
+#ifndef _WIN32
 #include <sys/ioctl.h>
+#endif
 #ifdef __sun__
 #include <sys/ioccom.h>
 #endif
 #include <ctype.h>
 #include <inttypes.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <arpa/inet.h>
-#include <netdb.h>
 
-#if defined(QEMU_NBD)
-extern int verbose;
-#else
-static int verbose = 0;
-#endif
+#include "qemu_socket.h"
 
+//#define DEBUG_NBD
+
+#ifdef DEBUG_NBD
 #define TRACE(msg, ...) do { \
-    if (verbose) LOG(msg, ## __VA_ARGS__); \
+    LOG(msg, ## __VA_ARGS__); \
 } while(0)
+#else
+#define TRACE(msg, ...) \
+    do { } while (0)
+#endif
 
 #define LOG(msg, ...) do { \
     fprintf(stderr, "%s:%s():L%d: " msg "\n", \
@@ -77,11 +76,14 @@
         ssize_t len;
 
         if (do_read) {
-            len = read(fd, buffer + offset, size - offset);
+            len = recv(fd, buffer + offset, size - offset, 0);
         } else {
-            len = write(fd, buffer + offset, size - offset);
+            len = send(fd, buffer + offset, size - offset, 0);
         }
 
+        if (len == -1)
+            errno = socket_error();
+
         /* recoverable error */
         if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
             continue;
@@ -108,7 +110,6 @@
     int s;
     struct in_addr in;
     struct sockaddr_in addr;
-    int serrno;
 
     s = socket(PF_INET, SOCK_STREAM, 0);
     if (s == -1) {
@@ -136,9 +137,7 @@
 
     return s;
 error:
-    serrno = errno;
-    close(s);
-    errno = serrno;
+    closesocket(s);
     return -1;
 }
 
@@ -147,7 +146,6 @@
     int s;
     struct in_addr in;
     struct sockaddr_in addr;
-    int serrno;
     int opt;
 
     s = socket(PF_INET, SOCK_STREAM, 0);
@@ -185,17 +183,15 @@
 
     return s;
 error:
-    serrno = errno;
-    close(s);
-    errno = serrno;
+    closesocket(s);
     return -1;
 }
 
+#ifndef _WIN32
 int unix_socket_incoming(const char *path)
 {
     int s;
     struct sockaddr_un addr;
-    int serrno;
 
     s = socket(PF_UNIX, SOCK_STREAM, 0);
     if (s == -1) {
@@ -216,9 +212,7 @@
 
     return s;
 error:
-    serrno = errno;
-    close(s);
-    errno = serrno;
+    closesocket(s);
     return -1;
 }
 
@@ -226,7 +220,6 @@
 {
     int s;
     struct sockaddr_un addr;
-    int serrno;
 
     s = socket(PF_UNIX, SOCK_STREAM, 0);
     if (s == -1) {
@@ -243,13 +236,24 @@
 
     return s;
 error:
-    serrno = errno;
-    close(s);
-    errno = serrno;
+    closesocket(s);
     return -1;
 }
+#else
+int unix_socket_incoming(const char *path)
+{
+    errno = ENOTSUP;
+    return -1;
+}
 
+int unix_socket_outgoing(const char *path)
+{
+    errno = ENOTSUP;
+    return -1;
+}
+#endif
 
+
 /* Basic flow
 
    Server         Client
@@ -337,6 +341,7 @@
         return 0;
 }
 
+#ifndef _WIN32
 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
 {
 	TRACE("Setting block size to %lu", (unsigned long)blocksize);
@@ -410,7 +415,26 @@
 	errno = serrno;
 	return ret;
 }
+#else
+int nbd_init(int fd, int csock, off_t size, size_t blocksize)
+{
+    errno = ENOTSUP;
+    return -1;
+}
 
+int nbd_disconnect(int fd)
+{
+    errno = ENOTSUP;
+    return -1;
+}
+
+int nbd_client(int fd, int csock)
+{
+    errno = ENOTSUP;
+    return -1;
+}
+#endif
+
 int nbd_send_request(int csock, struct nbd_request *request)
 {
 	uint8_t buf[4 + 4 + 8 + 8 + 4];
Index: qemu_socket.h
===================================================================
--- qemu_socket.h	(revision 5200)
+++ qemu_socket.h	(working copy)
@@ -14,11 +14,15 @@
 #define EINTR       WSAEINTR
 #define EINPROGRESS WSAEINPROGRESS
 
+int inet_aton(const char *cp, struct in_addr *ia);
+
 #else
 
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
+#include <arpa/inet.h>
+#include <netdb.h>
 #include <sys/un.h>
 
 #define socket_error() errno
Index: osdep.c
===================================================================
--- osdep.c	(revision 5200)
+++ osdep.c	(working copy)
@@ -45,6 +45,8 @@
 #include <malloc.h>
 #endif
 
+#include "qemu_socket.h"
+
 #if defined(_WIN32)
 void *qemu_memalign(size_t alignment, size_t size)
 {
@@ -283,3 +285,28 @@
   return 0;
 }
 #endif /* _WIN32 */
+
+
+#ifdef _WIN32
+void socket_set_nonblock(int fd)
+{
+    unsigned long opt = 1;
+    ioctlsocket(fd, FIONBIO, &opt);
+}
+
+int inet_aton(const char *cp, struct in_addr *ia)
+{
+    uint32_t addr = inet_addr(cp);
+    if (addr == 0xffffffff)
+	return 0;
+    ia->s_addr = addr;
+    return 1;
+}
+#else
+void socket_set_nonblock(int fd)
+{
+    int f;
+    f = fcntl(fd, F_GETFL);
+    fcntl(fd, F_SETFL, f | O_NONBLOCK);
+}
+#endif
Index: vl.c
===================================================================
--- vl.c	(revision 5200)
+++ vl.c	(working copy)
@@ -100,11 +100,10 @@
 #include <stropts.h>
 #endif
 #endif
-#else
-#include <winsock2.h>
-int inet_aton(const char *cp, struct in_addr *ia);
 #endif
 
+#include "qemu_socket.h"
+
 #if defined(CONFIG_SLIRP)
 #include "libslirp.h"
 #endif
@@ -125,8 +124,6 @@
 #define memalign(align, size) malloc(size)
 #endif
 
-#include "qemu_socket.h"
-
 #ifdef CONFIG_SDL
 #ifdef __APPLE__
 #include <SDL/SDL.h>
@@ -2131,12 +2128,6 @@
     return len1 - len;
 }
 
-void socket_set_nonblock(int fd)
-{
-    unsigned long opt = 1;
-    ioctlsocket(fd, FIONBIO, &opt);
-}
-
 #else
 
 static int unix_write(int fd, const uint8_t *buf, int len1)
@@ -2163,13 +2154,6 @@
 {
     return unix_write(fd, buf, len1);
 }
-
-void socket_set_nonblock(int fd)
-{
-    int f;
-    f = fcntl(fd, F_GETFL);
-    fcntl(fd, F_SETFL, f | O_NONBLOCK);
-}
 #endif /* !_WIN32 */
 
 #ifndef _WIN32
Index: block-raw-posix.c
===================================================================
--- block-raw-posix.c	(revision 5200)
+++ block-raw-posix.c	(working copy)
@@ -22,7 +22,7 @@
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
-#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if !defined(QEMU_TOOL)
 #include "qemu-timer.h"
 #include "exec-all.h"
 #include "qemu-char.h"
@@ -70,7 +70,7 @@
 //#define DEBUG_FLOPPY
 
 //#define DEBUG_BLOCK
-#if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if defined(DEBUG_BLOCK) && !defined(QEMU_TOOL)
 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0)	\
     { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
 #else
@@ -99,7 +99,7 @@
     int fd_got_error;
     int fd_media_changed;
 #endif
-#if defined(O_DIRECT) && !defined(QEMU_IMG)
+#if defined(O_DIRECT) && !defined(QEMU_TOOL)
     uint8_t* aligned_buf;
 #endif
 } BDRVRawState;
@@ -137,7 +137,7 @@
         return ret;
     }
     s->fd = fd;
-#if defined(O_DIRECT) && !defined(QEMU_IMG)
+#if defined(O_DIRECT) && !defined(QEMU_TOOL)
     s->aligned_buf = NULL;
     if (flags & BDRV_O_DIRECT) {
         s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
@@ -272,7 +272,7 @@
 }
 
 
-#if defined(O_DIRECT) && !defined(QEMU_IMG)
+#if defined(O_DIRECT) && !defined(QEMU_TOOL)
 /*
  * offset and count are in bytes and possibly not aligned. For files opened
  * with O_DIRECT, necessary alignments are ensured before calling
@@ -525,7 +525,7 @@
 
     fcntl(aio_sig_fd, F_SETFL, O_NONBLOCK);
 
-#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if !defined(QEMU_TOOL)
     qemu_set_fd_handler2(aio_sig_fd, NULL, qemu_aio_poll, NULL, NULL);
 #endif
 
@@ -556,7 +556,7 @@
 {
     int ret;
 
-#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if !defined(QEMU_TOOL)
     if (qemu_bh_poll())
         return;
 #endif
@@ -605,7 +605,7 @@
     return acb;
 }
 
-#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if !defined(QEMU_TOOL)
 static void raw_aio_em_cb(void* opaque)
 {
     RawAIOCB *acb = opaque;
@@ -624,7 +624,7 @@
      * If O_DIRECT is used and the buffer is not aligned fall back
      * to synchronous IO.
      */
-#if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if defined(O_DIRECT) && !defined(QEMU_TOOL)
     BDRVRawState *s = bs->opaque;
 
     if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
@@ -657,7 +657,7 @@
      * If O_DIRECT is used and the buffer is not aligned fall back
      * to synchronous IO.
      */
-#if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if defined(O_DIRECT) && !defined(QEMU_TOOL)
     BDRVRawState *s = bs->opaque;
 
     if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
@@ -719,7 +719,7 @@
 
 void qemu_aio_wait(void)
 {
-#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if !defined(QEMU_TOOL)
     qemu_bh_poll();
 #endif
 }
@@ -732,7 +732,7 @@
     if (s->fd >= 0) {
         close(s->fd);
         s->fd = -1;
-#if defined(O_DIRECT) && !defined(QEMU_IMG)
+#if defined(O_DIRECT) && !defined(QEMU_TOOL)
         if (s->aligned_buf != NULL)
             qemu_free(s->aligned_buf);
 #endif
@@ -1002,7 +1002,7 @@
     return 0;
 }
 
-#if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if defined(__linux__) && !defined(QEMU_TOOL)
 
 /* Note: we do not have a reliable method to detect if the floppy is
    present. The current method is to try to open the floppy at every
Index: Makefile
===================================================================
--- Makefile	(revision 5200)
+++ Makefile	(working copy)
@@ -28,6 +28,10 @@
 LIBS+=-lsocket -lnsl -lresolv
 endif
 
+ifdef CONFIG_WIN32
+LIBS+=-lwinmm -lws2_32 -liphlpapi
+endif
+
 all: $(TOOLS) $(DOCS) recurse-all 
 
 SUBDIR_RULES=$(patsubst %,subdir-%, $(TARGET_DIRS))
@@ -46,9 +50,11 @@
 BLOCK_OBJS=cutils.o qemu-malloc.o
 BLOCK_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o
 BLOCK_OBJS+=block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
-BLOCK_OBJS+=block-qcow2.o block-parallels.o
-ifndef CONFIG_WIN32
-BLOCK_OBJS+=block-nbd.o
+BLOCK_OBJS+=block-qcow2.o block-parallels.o block-nbd.o
+BLOCK_OBJS+=nbd.o
+
+ifdef CONFIG_AIO
+BLOCK_OBJS += compatfd.o
 endif
 
 ######################################################################
@@ -61,10 +67,6 @@
 OBJS+=readline.o console.o
 OBJS+=block.o
 
-ifndef CONFIG_WIN32
-OBJS+=nbd.o
-endif
-
 OBJS+=irq.o
 OBJS+=i2c.o smbus.o smbus_eeprom.o max7310.o max111x.o wm8750.o
 OBJS+=ssd0303.o ssd0323.o ads7846.o stellaris_input.o twl92230.o
@@ -177,29 +179,21 @@
 ifdef CONFIG_WIN32
 QEMU_IMG_BLOCK_OBJS += qemu-img-block-raw-win32.o
 else
-QEMU_IMG_BLOCK_OBJS += nbd.o qemu-img-block-raw-posix.o
+QEMU_IMG_BLOCK_OBJS += qemu-img-block-raw-posix.o
 endif
 
-ifdef CONFIG_AIO
-QEMU_IMG_BLOCK_OBJS += compatfd.o
-endif
-
 ######################################################################
 
-qemu-img$(EXESUF): qemu-img.o qemu-img-block.o $(QEMU_IMG_BLOCK_OBJS)
+qemu-img$(EXESUF): qemu-img.o qemu-img-block.o osdep.o $(QEMU_IMG_BLOCK_OBJS)
 	$(CC) $(LDFLAGS) -o $@ $^ -lz $(LIBS)
 
 qemu-img-%.o: %.c
-	$(CC) $(CFLAGS) $(CPPFLAGS) -DQEMU_IMG -c -o $@ $<
+	$(CC) $(CFLAGS) $(CPPFLAGS) -DQEMU_TOOL -c -o $@ $<
 
 %.o: %.c
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
 
-qemu-nbd-%.o: %.c
-	$(CC) $(CFLAGS) $(CPPFLAGS) -DQEMU_NBD -c -o $@ $<
-
-qemu-nbd$(EXESUF):  qemu-nbd.o qemu-nbd-nbd.o qemu-img-block.o \
-		    osdep.o qemu-nbd-block-raw-posix.o compatfd.o $(BLOCK_OBJS)
+qemu-nbd$(EXESUF):  qemu-nbd.o qemu-img-block.o osdep.o $(QEMU_IMG_BLOCK_OBJS)
 	$(CC) $(LDFLAGS) -o $@ $^ -lz $(LIBS)
 
 # dyngen host tool
Index: slirp/misc.c
===================================================================
--- slirp/misc.c	(revision 5200)
+++ slirp/misc.c	(working copy)
@@ -66,20 +66,6 @@
 }
 #endif
 
-#ifndef HAVE_INET_ATON
-int
-inet_aton(cp, ia)
-	const char *cp;
-	struct in_addr *ia;
-{
-	u_int32_t addr = inet_addr(cp);
-	if (addr == 0xffffffff)
-		return 0;
-	ia->s_addr = addr;
-	return 1;
-}
-#endif
-
 /*
  * Get our IP address and put it in our_addr
  */
Index: block.c
===================================================================
--- block.c	(revision 5200)
+++ block.c	(working copy)
@@ -22,7 +22,7 @@
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
-#ifndef QEMU_IMG
+#ifndef QEMU_TOOL
 #include "console.h"
 #endif
 #include "block_int.h"
@@ -922,7 +922,7 @@
     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
 }
 
-#ifndef QEMU_IMG
+#ifndef QEMU_TOOL
 void bdrv_info(void)
 {
     BlockDriverState *bs;
@@ -1203,7 +1203,7 @@
 /**************************************************************/
 /* async block device emulation */
 
-#ifdef QEMU_IMG
+#ifdef QEMU_TOOL
 static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
         int64_t sector_num, uint8_t *buf, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque)
@@ -1273,7 +1273,7 @@
     qemu_bh_cancel(acb->bh);
     qemu_aio_release(acb);
 }
-#endif /* !QEMU_IMG */
+#endif /* !QEMU_TOOL */
 
 /**************************************************************/
 /* sync block device emulation */
@@ -1337,9 +1337,7 @@
     bdrv_register(&bdrv_vvfat);
     bdrv_register(&bdrv_qcow2);
     bdrv_register(&bdrv_parallels);
-#ifndef _WIN32
     bdrv_register(&bdrv_nbd);
-#endif
 
     qemu_aio_init();
 }
Index: block.h
===================================================================
--- block.h	(revision 5200)
+++ block.h	(working copy)
@@ -47,7 +47,7 @@
                                      bdrv_file_open()) */
 #define BDRV_O_DIRECT      0x0020
 
-#ifndef QEMU_IMG
+#ifndef QEMU_TOOL
 void bdrv_info(void);
 void bdrv_info_stats(void);
 #endif

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Qemu-devel] Re: [PATCH][RFC][RFT] Let qemu-nbd build on Windows and replace QEMU_IMG/QEMU_NBD with QEMU_TOOL
  2008-09-13  1:05 [Qemu-devel] [PATCH][RFC][RFT] Let qemu-nbd build on Windows and replace QEMU_IMG/QEMU_NBD with QEMU_TOOL Anthony Liguori
@ 2008-09-13 15:30 ` Laurent Vivier
  2008-09-13 16:23   ` Anthony Liguori
  2008-09-13 17:25   ` Anthony Liguori
  0 siblings, 2 replies; 5+ messages in thread
From: Laurent Vivier @ 2008-09-13 15:30 UTC (permalink / raw)
  To: Anthony Liguori; +Cc: qemu-devel@nongnu.org

Le vendredi 12 septembre 2008 à 20:05 -0500, Anthony Liguori a écrit :
> This patch attempts to clean up the mess with qemu-nbd in the Makefile 
> in order to get it building and working on Windows.  I checked 
> block-nbd, qemu-img, and qemu-nbd on Windows and Linux and everything 
> seems to work.  However, there were some defined(QEMU_IMG) that didn't 
> include defined(QEMU_NBD) that now are covered by QEMU_TOOL so I'd like 
> Laurent to look them over and see if they were intentional.

If I remember correctly they were intentional:
- some of them because qemu-nbd didn't support windows (but you have
corrected this)
- others because qemu-img doesn't need to open file with O_DIRECT
whereas qemu-nbd must (--nocache option) (and I think you break this)

Did you test "qemu-nbd --nocache" ?

> Basically, there are no longer qemu-nbd-*.o objects.  qemu-img-*.o have 
> -DQEMU_TOOL defined and qemu-img/qemu-nbd use identical copies of those 
> objects.  A special object is no longer build for nbd.c either.
> 
> My longer goal would be to eliminate as many qemu-img-* objects as 
> possible but let's take that one step at a time.

I think it's a good idea...

Regards,
Laurent
-- 
----------------- Laurent.Vivier@bull.net  ------------------
  "La perfection est atteinte non quand il ne reste rien à
ajouter mais quand il ne reste rien à enlever." Saint Exupéry

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Qemu-devel] Re: [PATCH][RFC][RFT] Let qemu-nbd build on Windows and replace QEMU_IMG/QEMU_NBD with QEMU_TOOL
  2008-09-13 15:30 ` [Qemu-devel] " Laurent Vivier
@ 2008-09-13 16:23   ` Anthony Liguori
  2008-09-13 16:48     ` Anthony Liguori
  2008-09-13 17:25   ` Anthony Liguori
  1 sibling, 1 reply; 5+ messages in thread
From: Anthony Liguori @ 2008-09-13 16:23 UTC (permalink / raw)
  To: Laurent Vivier; +Cc: qemu-devel@nongnu.org

Laurent Vivier wrote:
> Le vendredi 12 septembre 2008 à 20:05 -0500, Anthony Liguori a écrit :
>   
>> This patch attempts to clean up the mess with qemu-nbd in the Makefile 
>> in order to get it building and working on Windows.  I checked 
>> block-nbd, qemu-img, and qemu-nbd on Windows and Linux and everything 
>> seems to work.  However, there were some defined(QEMU_IMG) that didn't 
>> include defined(QEMU_NBD) that now are covered by QEMU_TOOL so I'd like 
>> Laurent to look them over and see if they were intentional.
>>     
>
> If I remember correctly they were intentional:
> - some of them because qemu-nbd didn't support windows (but you have
> corrected this)
> - others because qemu-img doesn't need to open file with O_DIRECT
> whereas qemu-nbd must (--nocache option) (and I think you break this)
>
> Did you test "qemu-nbd --nocache" ?
>   

No, but this is why I asked :-)

So this begs the question, why does qemu-nbd need to open files with 
O_DIRECT and why doesn't qemu-img?

Can we just enable the code in both?

Regards,

Anthony Liguori

>> Basically, there are no longer qemu-nbd-*.o objects.  qemu-img-*.o have 
>> -DQEMU_TOOL defined and qemu-img/qemu-nbd use identical copies of those 
>> objects.  A special object is no longer build for nbd.c either.
>>
>> My longer goal would be to eliminate as many qemu-img-* objects as 
>> possible but let's take that one step at a time.
>>     
>
> I think it's a good idea...
>
> Regards,
> Laurent
>   

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Qemu-devel] Re: [PATCH][RFC][RFT] Let qemu-nbd build on Windows and replace QEMU_IMG/QEMU_NBD with QEMU_TOOL
  2008-09-13 16:23   ` Anthony Liguori
@ 2008-09-13 16:48     ` Anthony Liguori
  0 siblings, 0 replies; 5+ messages in thread
From: Anthony Liguori @ 2008-09-13 16:48 UTC (permalink / raw)
  To: Laurent Vivier; +Cc: qemu-devel@nongnu.org

Anthony Liguori wrote:
> Laurent Vivier wrote:
>> Le vendredi 12 septembre 2008 à 20:05 -0500, Anthony Liguori a écrit :
>>  
>>> This patch attempts to clean up the mess with qemu-nbd in the 
>>> Makefile in order to get it building and working on Windows.  I 
>>> checked block-nbd, qemu-img, and qemu-nbd on Windows and Linux and 
>>> everything seems to work.  However, there were some 
>>> defined(QEMU_IMG) that didn't include defined(QEMU_NBD) that now are 
>>> covered by QEMU_TOOL so I'd like Laurent to look them over and see 
>>> if they were intentional.
>>>     
>>
>> If I remember correctly they were intentional:
>> - some of them because qemu-nbd didn't support windows (but you have
>> corrected this)
>> - others because qemu-img doesn't need to open file with O_DIRECT
>> whereas qemu-nbd must (--nocache option) (and I think you break this)
>>
>> Did you test "qemu-nbd --nocache" ?
>>   
>
> No, but this is why I asked :-)
>
> So this begs the question, why does qemu-nbd need to open files with 
> O_DIRECT and why doesn't qemu-img?
>
> Can we just enable the code in both?

Oh, I see.  It's because we don't have bottom halves with QEMU_TOOL.  
That's easy enough to fix.  I'll update the patch.

Regards,

Anthony Liguori

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Qemu-devel] Re: [PATCH][RFC][RFT] Let qemu-nbd build on Windows and replace QEMU_IMG/QEMU_NBD with QEMU_TOOL
  2008-09-13 15:30 ` [Qemu-devel] " Laurent Vivier
  2008-09-13 16:23   ` Anthony Liguori
@ 2008-09-13 17:25   ` Anthony Liguori
  1 sibling, 0 replies; 5+ messages in thread
From: Anthony Liguori @ 2008-09-13 17:25 UTC (permalink / raw)
  To: Laurent Vivier; +Cc: qemu-devel@nongnu.org

[-- Attachment #1: Type: text/plain, Size: 909 bytes --]

Laurent Vivier wrote:
>> Basically, there are no longer qemu-nbd-*.o objects.  qemu-img-*.o have 
>> -DQEMU_TOOL defined and qemu-img/qemu-nbd use identical copies of those 
>> objects.  A special object is no longer build for nbd.c either.
>>
>> My longer goal would be to eliminate as many qemu-img-* objects as 
>> possible but let's take that one step at a time.
>>     
>
> I think it's a good idea...
>   

Okay, the attached patch takes things to the logical conclusion.  It 
introduces a qemu-tool.c file that has enough stub code to completely 
eliminate the QEMU_IMG and QEMU_NBD defines.  The result is that we no 
longer need special qemu-img- or qemu-nbd- objects.

I've done some light testing of Windows and Linux and it seems to work 
well.  I'd like some other folks to give it some testing too though 
because it touches a lot of code.

Regards,

Anthony Liguori

> Regards,
> Laurent
>   


[-- Attachment #2: qemu-windows-nbd.patch --]
[-- Type: text/x-patch, Size: 22636 bytes --]

Subject: [PATCH] Use common objects for qemu-img and qemu-nbd

Right now, we sprinkle #if defined(QEMU_IMG) && defined(QEMU_NBD) all over the
code.  It's ugly and causes us to have to build multiple object files for
linking against qemu and the tools.

This patch introduces a new file, qemu-tool.c which contains enough for
qemu-img, qemu-nbd, and QEMU to all share the same objects.

This also required getting qemu-nbd to be a bit more Windows friendly.  I also
changed the Windows block-raw to use normal IO instead of overlapping IO since
we don't actually do AIO yet on Windows.  I changed the various #if 0's to
 #if WIN32_AIO to make it easier for someone to eventually fix AIO on Windows.

After this patch, there are no longer any #ifdef's related to qemu-img and
qemu-nbd.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

Index: block-raw-win32.c
===================================================================
diff --git a/Makefile b/Makefile
index 708941c..7bb2bf2 100644
--- a/Makefile
+++ b/Makefile
@@ -28,6 +28,10 @@ ifdef CONFIG_SOLARIS
 LIBS+=-lsocket -lnsl -lresolv
 endif
 
+ifdef CONFIG_WIN32
+LIBS+=-lwinmm -lws2_32 -liphlpapi
+endif
+
 all: $(TOOLS) $(DOCS) recurse-all 
 
 SUBDIR_RULES=$(patsubst %,subdir-%, $(TARGET_DIRS))
@@ -46,9 +50,17 @@ recurse-all: $(SUBDIR_RULES)
 BLOCK_OBJS=cutils.o qemu-malloc.o
 BLOCK_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o
 BLOCK_OBJS+=block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
-BLOCK_OBJS+=block-qcow2.o block-parallels.o
-ifndef CONFIG_WIN32
-BLOCK_OBJS+=block-nbd.o
+BLOCK_OBJS+=block-qcow2.o block-parallels.o block-nbd.o
+BLOCK_OBJS+=nbd.o block.o
+
+ifdef CONFIG_WIN32
+BLOCK_OBJS += block-raw-win32.o
+else
+BLOCK_OBJS += block-raw-posix.o
+endif
+
+ifdef CONFIG_AIO
+BLOCK_OBJS += compatfd.o
 endif
 
 ######################################################################
@@ -59,11 +71,6 @@ endif
 
 OBJS=$(BLOCK_OBJS)
 OBJS+=readline.o console.o
-OBJS+=block.o
-
-ifndef CONFIG_WIN32
-OBJS+=nbd.o
-endif
 
 OBJS+=irq.o
 OBJS+=i2c.o smbus.o smbus_eeprom.o max7310.o max111x.o wm8750.o
@@ -173,33 +180,15 @@ libqemu_user.a: $(USER_OBJS)
 	rm -f $@ 
 	$(AR) rcs $@ $(USER_OBJS)
 
-QEMU_IMG_BLOCK_OBJS = $(BLOCK_OBJS)
-ifdef CONFIG_WIN32
-QEMU_IMG_BLOCK_OBJS += qemu-img-block-raw-win32.o
-else
-QEMU_IMG_BLOCK_OBJS += nbd.o qemu-img-block-raw-posix.o
-endif
-
-ifdef CONFIG_AIO
-QEMU_IMG_BLOCK_OBJS += compatfd.o
-endif
-
 ######################################################################
 
-qemu-img$(EXESUF): qemu-img.o qemu-img-block.o $(QEMU_IMG_BLOCK_OBJS)
+qemu-img$(EXESUF): qemu-img.o qemu-tool.o osdep.o $(BLOCK_OBJS)
 	$(CC) $(LDFLAGS) -o $@ $^ -lz $(LIBS)
 
-qemu-img-%.o: %.c
-	$(CC) $(CFLAGS) $(CPPFLAGS) -DQEMU_IMG -c -o $@ $<
-
 %.o: %.c
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
 
-qemu-nbd-%.o: %.c
-	$(CC) $(CFLAGS) $(CPPFLAGS) -DQEMU_NBD -c -o $@ $<
-
-qemu-nbd$(EXESUF):  qemu-nbd.o qemu-nbd-nbd.o qemu-img-block.o \
-		    osdep.o qemu-nbd-block-raw-posix.o compatfd.o $(BLOCK_OBJS)
+qemu-nbd$(EXESUF):  qemu-nbd.o qemu-tool.o osdep.o $(BLOCK_OBJS)
 	$(CC) $(LDFLAGS) -o $@ $^ -lz $(LIBS)
 
 # dyngen host tool
diff --git a/block-raw-posix.c b/block-raw-posix.c
index eaf3bf2..e314f4a 100644
--- a/block-raw-posix.c
+++ b/block-raw-posix.c
@@ -22,11 +22,8 @@
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
-#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
 #include "qemu-timer.h"
-#include "exec-all.h"
 #include "qemu-char.h"
-#endif
 #include "block_int.h"
 #include "compatfd.h"
 #include <assert.h>
@@ -70,7 +67,7 @@
 //#define DEBUG_FLOPPY
 
 //#define DEBUG_BLOCK
-#if defined(DEBUG_BLOCK) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if defined(DEBUG_BLOCK)
 #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0)	\
     { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0)
 #else
@@ -99,7 +96,7 @@ typedef struct BDRVRawState {
     int fd_got_error;
     int fd_media_changed;
 #endif
-#if defined(O_DIRECT) && !defined(QEMU_IMG)
+#if defined(O_DIRECT)
     uint8_t* aligned_buf;
 #endif
 } BDRVRawState;
@@ -137,7 +134,7 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
         return ret;
     }
     s->fd = fd;
-#if defined(O_DIRECT) && !defined(QEMU_IMG)
+#if defined(O_DIRECT)
     s->aligned_buf = NULL;
     if (flags & BDRV_O_DIRECT) {
         s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
@@ -272,7 +269,7 @@ label__raw_write__success:
 }
 
 
-#if defined(O_DIRECT) && !defined(QEMU_IMG)
+#if defined(O_DIRECT)
 /*
  * offset and count are in bytes and possibly not aligned. For files opened
  * with O_DIRECT, necessary alignments are ensured before calling
@@ -525,9 +522,7 @@ void qemu_aio_init(void)
 
     fcntl(aio_sig_fd, F_SETFL, O_NONBLOCK);
 
-#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
     qemu_set_fd_handler2(aio_sig_fd, NULL, qemu_aio_poll, NULL, NULL);
-#endif
 
 #if defined(__GLIBC__) && defined(__linux__)
     {
@@ -556,10 +551,8 @@ void qemu_aio_wait(void)
 {
     int ret;
 
-#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
     if (qemu_bh_poll())
         return;
-#endif
 
     if (!first_aio)
         return;
@@ -605,14 +598,12 @@ static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
     return acb;
 }
 
-#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
 static void raw_aio_em_cb(void* opaque)
 {
     RawAIOCB *acb = opaque;
     acb->common.cb(acb->common.opaque, acb->ret);
     qemu_aio_release(acb);
 }
-#endif
 
 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
         int64_t sector_num, uint8_t *buf, int nb_sectors,
@@ -624,7 +615,7 @@ static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
      * If O_DIRECT is used and the buffer is not aligned fall back
      * to synchronous IO.
      */
-#if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if defined(O_DIRECT)
     BDRVRawState *s = bs->opaque;
 
     if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
@@ -657,7 +648,7 @@ static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
      * If O_DIRECT is used and the buffer is not aligned fall back
      * to synchronous IO.
      */
-#if defined(O_DIRECT) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if defined(O_DIRECT)
     BDRVRawState *s = bs->opaque;
 
     if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) {
@@ -719,9 +710,7 @@ void qemu_aio_flush(void)
 
 void qemu_aio_wait(void)
 {
-#if !defined(QEMU_IMG) && !defined(QEMU_NBD)
     qemu_bh_poll();
-#endif
 }
 
 #endif /* CONFIG_AIO */
@@ -732,7 +721,7 @@ static void raw_close(BlockDriverState *bs)
     if (s->fd >= 0) {
         close(s->fd);
         s->fd = -1;
-#if defined(O_DIRECT) && !defined(QEMU_IMG)
+#if defined(O_DIRECT)
         if (s->aligned_buf != NULL)
             qemu_free(s->aligned_buf);
 #endif
@@ -1002,7 +991,7 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
     return 0;
 }
 
-#if defined(__linux__) && !defined(QEMU_IMG) && !defined(QEMU_NBD)
+#if defined(__linux__)
 
 /* Note: we do not have a reliable method to detect if the floppy is
    present. The current method is to try to open the floppy at every
@@ -1052,14 +1041,6 @@ static int fd_open(BlockDriverState *bs)
     s->fd_got_error = 0;
     return 0;
 }
-#else
-static int fd_open(BlockDriverState *bs)
-{
-    return 0;
-}
-#endif
-
-#if defined(__linux__)
 
 static int raw_is_inserted(BlockDriverState *bs)
 {
diff --git a/block-raw-win32.c b/block-raw-win32.c
index 7b88dcf..2c3f88f 100644
--- a/block-raw-win32.c
+++ b/block-raw-win32.c
@@ -22,14 +22,13 @@
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
-#ifndef QEMU_IMG
 #include "qemu-timer.h"
-#include "exec-all.h"
-#endif
 #include "block_int.h"
 #include <assert.h>
 #include <winioctl.h>
 
+//#define WIN32_AIO
+
 #define FTYPE_FILE 0
 #define FTYPE_CD     1
 #define FTYPE_HARDDISK 2
@@ -100,10 +99,10 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
     } else {
         create_flags = OPEN_EXISTING;
     }
-#ifdef QEMU_IMG
-    overlapped = FILE_ATTRIBUTE_NORMAL;
-#else
+#ifdef WIN32_AIO
     overlapped = FILE_FLAG_OVERLAPPED;
+#else
+    overlapped = FILE_ATTRIBUTE_NORMAL;
 #endif
     if (flags & BDRV_O_DIRECT)
         overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
@@ -133,10 +132,12 @@ static int raw_pread(BlockDriverState *bs, int64_t offset,
     ov.OffsetHigh = offset >> 32;
     ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
     if (!ret) {
+#ifdef WIN32_AIO
         ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
         if (!ret)
             return -EIO;
         else
+#endif
             return ret_count;
     }
     return ret_count;
@@ -155,17 +156,18 @@ static int raw_pwrite(BlockDriverState *bs, int64_t offset,
     ov.OffsetHigh = offset >> 32;
     ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
     if (!ret) {
+#ifdef WIN32_AIO
         ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
         if (!ret)
             return -EIO;
         else
+#endif
             return ret_count;
     }
     return ret_count;
 }
 
-#if 0
-#ifndef QEMU_IMG
+#ifdef WIN32_AIO
 static void raw_aio_cb(void *opaque)
 {
     RawAIOCB *acb = opaque;
@@ -181,7 +183,6 @@ static void raw_aio_cb(void *opaque)
         acb->common.cb(acb->common.opaque, 0);
     }
 }
-#endif
 
 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
         int64_t sector_num, uint8_t *buf, int nb_sectors,
@@ -204,9 +205,7 @@ static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
     acb->ov.OffsetHigh = offset >> 32;
     acb->ov.hEvent = acb->hEvent;
     acb->count = nb_sectors * 512;
-#ifndef QEMU_IMG
     qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
-#endif
     return acb;
 }
 
@@ -226,9 +225,7 @@ static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
         qemu_aio_release(acb);
         return NULL;
     }
-#ifdef QEMU_IMG
     qemu_aio_release(acb);
-#endif
     return (BlockDriverAIOCB *)acb;
 }
 
@@ -248,15 +245,12 @@ static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
         qemu_aio_release(acb);
         return NULL;
     }
-#ifdef QEMU_IMG
     qemu_aio_release(acb);
-#endif
     return (BlockDriverAIOCB *)acb;
 }
 
 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
 {
-#ifndef QEMU_IMG
     RawAIOCB *acb = (RawAIOCB *)blockacb;
     BlockDriverState *bs = acb->common.bs;
     BDRVRawState *s = bs->opaque;
@@ -265,9 +259,8 @@ static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
     /* XXX: if more than one async I/O it is not correct */
     CancelIo(s->hfile);
     qemu_aio_release(acb);
-#endif
 }
-#endif /* #if 0 */
+#endif /* #if WIN32_AIO */
 
 static void raw_flush(BlockDriverState *bs)
 {
@@ -356,9 +349,7 @@ void qemu_aio_flush(void)
 
 void qemu_aio_wait(void)
 {
-#ifndef QEMU_IMG
     qemu_bh_poll();
-#endif
 }
 
 BlockDriver bdrv_raw = {
@@ -372,7 +363,7 @@ BlockDriver bdrv_raw = {
     raw_create,
     raw_flush,
 
-#if 0
+#ifdef WIN32_AIO
     .bdrv_aio_read = raw_aio_read,
     .bdrv_aio_write = raw_aio_write,
     .bdrv_aio_cancel = raw_aio_cancel,
@@ -458,10 +449,10 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
     }
     create_flags = OPEN_EXISTING;
 
-#ifdef QEMU_IMG
-    overlapped = FILE_ATTRIBUTE_NORMAL;
-#else
+#ifdef WIN32_AIO
     overlapped = FILE_FLAG_OVERLAPPED;
+#else
+    overlapped = FILE_ATTRIBUTE_NORMAL;
 #endif
     if (flags & BDRV_O_DIRECT)
         overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
@@ -524,7 +515,7 @@ BlockDriver bdrv_host_device = {
     NULL,
     raw_flush,
 
-#if 0
+#ifdef WIN32_AIO
     .bdrv_aio_read = raw_aio_read,
     .bdrv_aio_write = raw_aio_write,
     .bdrv_aio_cancel = raw_aio_cancel,
diff --git a/block.c b/block.c
index 52c8f37..4811797 100644
--- a/block.c
+++ b/block.c
@@ -22,9 +22,7 @@
  * THE SOFTWARE.
  */
 #include "qemu-common.h"
-#ifndef QEMU_IMG
 #include "console.h"
-#endif
 #include "block_int.h"
 
 #ifdef _BSD
@@ -922,7 +920,6 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
 }
 
-#ifndef QEMU_IMG
 void bdrv_info(void)
 {
     BlockDriverState *bs;
@@ -980,7 +977,6 @@ void bdrv_info_stats (void)
 		     bs->rd_ops, bs->wr_ops);
     }
 }
-#endif
 
 void bdrv_get_backing_filename(BlockDriverState *bs,
                                char *filename, int filename_size)
@@ -1203,31 +1199,6 @@ void bdrv_aio_cancel(BlockDriverAIOCB *acb)
 /**************************************************************/
 /* async block device emulation */
 
-#ifdef QEMU_IMG
-static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
-        int64_t sector_num, uint8_t *buf, int nb_sectors,
-        BlockDriverCompletionFunc *cb, void *opaque)
-{
-    int ret;
-    ret = bdrv_read(bs, sector_num, buf, nb_sectors);
-    cb(opaque, ret);
-    return NULL;
-}
-
-static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
-        int64_t sector_num, const uint8_t *buf, int nb_sectors,
-        BlockDriverCompletionFunc *cb, void *opaque)
-{
-    int ret;
-    ret = bdrv_write(bs, sector_num, buf, nb_sectors);
-    cb(opaque, ret);
-    return NULL;
-}
-
-static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
-{
-}
-#else
 static void bdrv_aio_bh_cb(void *opaque)
 {
     BlockDriverAIOCBSync *acb = opaque;
@@ -1273,7 +1244,6 @@ static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
     qemu_bh_cancel(acb->bh);
     qemu_aio_release(acb);
 }
-#endif /* !QEMU_IMG */
 
 /**************************************************************/
 /* sync block device emulation */
@@ -1337,9 +1307,7 @@ void bdrv_init(void)
     bdrv_register(&bdrv_vvfat);
     bdrv_register(&bdrv_qcow2);
     bdrv_register(&bdrv_parallels);
-#ifndef _WIN32
     bdrv_register(&bdrv_nbd);
-#endif
 
     qemu_aio_init();
 }
diff --git a/block.h b/block.h
index 0443585..d774a2e 100644
--- a/block.h
+++ b/block.h
@@ -47,10 +47,8 @@ typedef struct QEMUSnapshotInfo {
                                      bdrv_file_open()) */
 #define BDRV_O_DIRECT      0x0020
 
-#ifndef QEMU_IMG
 void bdrv_info(void);
 void bdrv_info_stats(void);
-#endif
 
 void bdrv_init(void);
 BlockDriver *bdrv_find_format(const char *format_name);
diff --git a/nbd.c b/nbd.c
index ee5427c..bb4f5d4 100644
--- a/nbd.c
+++ b/nbd.c
@@ -21,28 +21,27 @@
 
 #include <errno.h>
 #include <string.h>
+#ifndef _WIN32
 #include <sys/ioctl.h>
+#endif
 #ifdef __sun__
 #include <sys/ioccom.h>
 #endif
 #include <ctype.h>
 #include <inttypes.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-
-#if defined(QEMU_NBD)
-extern int verbose;
-#else
-static int verbose = 0;
-#endif
 
+#include "qemu_socket.h"
+
+//#define DEBUG_NBD
+
+#ifdef DEBUG_NBD
 #define TRACE(msg, ...) do { \
-    if (verbose) LOG(msg, ## __VA_ARGS__); \
+    LOG(msg, ## __VA_ARGS__); \
 } while(0)
+#else
+#define TRACE(msg, ...) \
+    do { } while (0)
+#endif
 
 #define LOG(msg, ...) do { \
     fprintf(stderr, "%s:%s():L%d: " msg "\n", \
@@ -77,11 +76,14 @@ size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
         ssize_t len;
 
         if (do_read) {
-            len = read(fd, buffer + offset, size - offset);
+            len = recv(fd, buffer + offset, size - offset, 0);
         } else {
-            len = write(fd, buffer + offset, size - offset);
+            len = send(fd, buffer + offset, size - offset, 0);
         }
 
+        if (len == -1)
+            errno = socket_error();
+
         /* recoverable error */
         if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
             continue;
@@ -108,7 +110,6 @@ int tcp_socket_outgoing(const char *address, uint16_t port)
     int s;
     struct in_addr in;
     struct sockaddr_in addr;
-    int serrno;
 
     s = socket(PF_INET, SOCK_STREAM, 0);
     if (s == -1) {
@@ -136,9 +137,7 @@ int tcp_socket_outgoing(const char *address, uint16_t port)
 
     return s;
 error:
-    serrno = errno;
-    close(s);
-    errno = serrno;
+    closesocket(s);
     return -1;
 }
 
@@ -147,7 +146,6 @@ int tcp_socket_incoming(const char *address, uint16_t port)
     int s;
     struct in_addr in;
     struct sockaddr_in addr;
-    int serrno;
     int opt;
 
     s = socket(PF_INET, SOCK_STREAM, 0);
@@ -185,17 +183,15 @@ int tcp_socket_incoming(const char *address, uint16_t port)
 
     return s;
 error:
-    serrno = errno;
-    close(s);
-    errno = serrno;
+    closesocket(s);
     return -1;
 }
 
+#ifndef _WIN32
 int unix_socket_incoming(const char *path)
 {
     int s;
     struct sockaddr_un addr;
-    int serrno;
 
     s = socket(PF_UNIX, SOCK_STREAM, 0);
     if (s == -1) {
@@ -216,9 +212,7 @@ int unix_socket_incoming(const char *path)
 
     return s;
 error:
-    serrno = errno;
-    close(s);
-    errno = serrno;
+    closesocket(s);
     return -1;
 }
 
@@ -226,7 +220,6 @@ int unix_socket_outgoing(const char *path)
 {
     int s;
     struct sockaddr_un addr;
-    int serrno;
 
     s = socket(PF_UNIX, SOCK_STREAM, 0);
     if (s == -1) {
@@ -243,12 +236,23 @@ int unix_socket_outgoing(const char *path)
 
     return s;
 error:
-    serrno = errno;
-    close(s);
-    errno = serrno;
+    closesocket(s);
+    return -1;
+}
+#else
+int unix_socket_incoming(const char *path)
+{
+    errno = ENOTSUP;
     return -1;
 }
 
+int unix_socket_outgoing(const char *path)
+{
+    errno = ENOTSUP;
+    return -1;
+}
+#endif
+
 
 /* Basic flow
 
@@ -337,6 +341,7 @@ int nbd_receive_negotiate(int csock, off_t *size, size_t *blocksize)
         return 0;
 }
 
+#ifndef _WIN32
 int nbd_init(int fd, int csock, off_t size, size_t blocksize)
 {
 	TRACE("Setting block size to %lu", (unsigned long)blocksize);
@@ -410,6 +415,25 @@ int nbd_client(int fd, int csock)
 	errno = serrno;
 	return ret;
 }
+#else
+int nbd_init(int fd, int csock, off_t size, size_t blocksize)
+{
+    errno = ENOTSUP;
+    return -1;
+}
+
+int nbd_disconnect(int fd)
+{
+    errno = ENOTSUP;
+    return -1;
+}
+
+int nbd_client(int fd, int csock)
+{
+    errno = ENOTSUP;
+    return -1;
+}
+#endif
 
 int nbd_send_request(int csock, struct nbd_request *request)
 {
diff --git a/osdep.c b/osdep.c
index a49efba..683aad0 100644
--- a/osdep.c
+++ b/osdep.c
@@ -45,6 +45,8 @@
 #include <malloc.h>
 #endif
 
+#include "qemu_socket.h"
+
 #if defined(_WIN32)
 void *qemu_memalign(size_t alignment, size_t size)
 {
@@ -283,3 +285,28 @@ int qemu_gettimeofday(qemu_timeval *tp)
   return 0;
 }
 #endif /* _WIN32 */
+
+
+#ifdef _WIN32
+void socket_set_nonblock(int fd)
+{
+    unsigned long opt = 1;
+    ioctlsocket(fd, FIONBIO, &opt);
+}
+
+int inet_aton(const char *cp, struct in_addr *ia)
+{
+    uint32_t addr = inet_addr(cp);
+    if (addr == 0xffffffff)
+	return 0;
+    ia->s_addr = addr;
+    return 1;
+}
+#else
+void socket_set_nonblock(int fd)
+{
+    int f;
+    f = fcntl(fd, F_GETFL);
+    fcntl(fd, F_SETFL, f | O_NONBLOCK);
+}
+#endif
diff --git a/qemu-tool.c b/qemu-tool.c
new file mode 100644
index 0000000..63e2056
--- /dev/null
+++ b/qemu-tool.c
@@ -0,0 +1,83 @@
+/*
+ * Compatibility for qemu-img/qemu-nbd
+ *
+ * Copyright IBM, Corp. 2008
+ *
+ * Authors:
+ *  Anthony Liguori   <aliguori@us.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "console.h"
+#include "sysemu.h"
+#include "qemu-timer.h"
+
+#include <sys/time.h>
+
+QEMUClock *rt_clock;
+
+struct QEMUBH
+{
+    QEMUBHFunc *cb;
+    void *opaque;
+};
+
+void term_printf(const char *fmt, ...)
+{
+}
+
+void term_print_filename(const char *filename)
+{
+}
+
+QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
+{
+    QEMUBH *bh;
+
+    bh = qemu_malloc(sizeof(*bh));
+    if (bh) {
+        bh->cb = cb;
+        bh->opaque = opaque;
+    }
+
+    return bh;
+}
+
+int qemu_bh_poll(void)
+{
+    return 0;
+}
+
+void qemu_bh_schedule(QEMUBH *bh)
+{
+    bh->cb(bh->opaque);
+}
+
+void qemu_bh_cancel(QEMUBH *bh)
+{
+}
+
+void qemu_bh_delete(QEMUBH *bh)
+{
+    qemu_free(bh);
+}
+
+int qemu_set_fd_handler2(int fd,
+                         IOCanRWHandler *fd_read_poll,
+                         IOHandler *fd_read,
+                         IOHandler *fd_write,
+                         void *opaque)
+{
+    return 0;
+}
+
+int64_t qemu_get_clock(QEMUClock *clock)
+{
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    return (tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000)) / 1000000;
+}
diff --git a/qemu_socket.h b/qemu_socket.h
index 5229c24..ef2d88b 100644
--- a/qemu_socket.h
+++ b/qemu_socket.h
@@ -14,11 +14,15 @@
 #define EINTR       WSAEINTR
 #define EINPROGRESS WSAEINPROGRESS
 
+int inet_aton(const char *cp, struct in_addr *ia);
+
 #else
 
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
+#include <arpa/inet.h>
+#include <netdb.h>
 #include <sys/un.h>
 
 #define socket_error() errno
diff --git a/slirp/misc.c b/slirp/misc.c
index 032a1f7..09b9c54 100644
--- a/slirp/misc.c
+++ b/slirp/misc.c
@@ -66,20 +66,6 @@ redir_x(inaddr, start_port, display, screen)
 }
 #endif
 
-#ifndef HAVE_INET_ATON
-int
-inet_aton(cp, ia)
-	const char *cp;
-	struct in_addr *ia;
-{
-	u_int32_t addr = inet_addr(cp);
-	if (addr == 0xffffffff)
-		return 0;
-	ia->s_addr = addr;
-	return 1;
-}
-#endif
-
 /*
  * Get our IP address and put it in our_addr
  */
diff --git a/vl.c b/vl.c
index 18bcc1f..70f8645 100644
--- a/vl.c
+++ b/vl.c
@@ -100,11 +100,10 @@
 #include <stropts.h>
 #endif
 #endif
-#else
-#include <winsock2.h>
-int inet_aton(const char *cp, struct in_addr *ia);
 #endif
 
+#include "qemu_socket.h"
+
 #if defined(CONFIG_SLIRP)
 #include "libslirp.h"
 #endif
@@ -125,8 +124,6 @@ int inet_aton(const char *cp, struct in_addr *ia);
 #define memalign(align, size) malloc(size)
 #endif
 
-#include "qemu_socket.h"
-
 #ifdef CONFIG_SDL
 #ifdef __APPLE__
 #include <SDL/SDL.h>
@@ -2131,12 +2128,6 @@ static int send_all(int fd, const uint8_t *buf, int len1)
     return len1 - len;
 }
 
-void socket_set_nonblock(int fd)
-{
-    unsigned long opt = 1;
-    ioctlsocket(fd, FIONBIO, &opt);
-}
-
 #else
 
 static int unix_write(int fd, const uint8_t *buf, int len1)
@@ -2163,13 +2154,6 @@ static inline int send_all(int fd, const uint8_t *buf, int len1)
 {
     return unix_write(fd, buf, len1);
 }
-
-void socket_set_nonblock(int fd)
-{
-    int f;
-    f = fcntl(fd, F_GETFL);
-    fcntl(fd, F_SETFL, f | O_NONBLOCK);
-}
 #endif /* !_WIN32 */
 
 #ifndef _WIN32

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-09-13 17:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-13  1:05 [Qemu-devel] [PATCH][RFC][RFT] Let qemu-nbd build on Windows and replace QEMU_IMG/QEMU_NBD with QEMU_TOOL Anthony Liguori
2008-09-13 15:30 ` [Qemu-devel] " Laurent Vivier
2008-09-13 16:23   ` Anthony Liguori
2008-09-13 16:48     ` Anthony Liguori
2008-09-13 17:25   ` Anthony Liguori

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).