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>



git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@5226 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
aliguori 2008-09-15 15:51:35 +00:00
parent d9cf15784f
commit 03ff3ca30f
11 changed files with 211 additions and 176 deletions

View file

@ -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)
{