Various blktap related patches (qemu-xen part)
cover Various blktap related patches (qemu-xen part)
The following patch series is the qemu-xen part of some blktap related patches that accumulated during SLES 11 development. The xen part (on which this series depends) has already been comitted yesterday.
This series is about restoring the possibility to use qemu-dm as a backend for blktap devices (and mid-term letting tapdisk die with its duplicated code).
Kevin Wolf (6):
ioemu: Initialize blktap backend for FV machines
ioemu: Use the image format sent by blktapctrl
ioemu: Build tapdisk-ioemu binary
tapdisk-ioemu: Write messages to a logfile
ioemu: Fail on too small blktap disks
tapdisk-ioemu: Fix shutdown condition
Makefile | 22 +++++++++++++++------- configure | 2 +- hw/xen_blktap.c | 33 +++++++++++++++++++++++++++++---- hw/xen_blktap.h | 14 ++++++++++++++ hw/xen_machine_fv.c | 6 ++++++ qemu-tool.c | 2 +- tapdisk-ioemu.c | 49 +++++++++++++++++++++++-------------------------- 7 files changed, 89 insertions(+), 39 deletions(-)
PATCH 1/6 ioemu: Initialize blktap backend for FV machines
PV drivers (and thus blktap) are not only used by PV domains, but also by HVM guests. Adding a call to the init functions allows them to have their qemu-dm instance as blktap backend.
--- hw/xen_machine_fv.c | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
hw/xen_machine_fv.c +6 -0
@@ -185,6 +185,7 @@ void qemu_invalidate_map_cache(void)#endif /* defined(MAPCACHE) */ +extern void init_blktap (void ); static void xen_init_fv(ram_addr_t ram_size, int vga_ram_size, const char *boot_device, DisplayState *ds,
@@ -210,6 +211,11 @@ static void xen_init_fv(ram_addr_t ram_size, int vga_ram_size, }#endif +#ifndef CONFIG_STUBDOM +/* Initialize tapdisk client */ +init_blktap (); +#endif +#ifdef CONFIG_STUBDOM /* the hvmop is not supported on older hypervisors */ xc_set_hvm_param (xc_handle, domid,HVM_PARAM_DM_DOMAIN ,DOMID_SELF );#endif
Signature · 1 line
1.6.0.2
PATCH 2/6 ioemu: Use the image format sent by blktapctrl
In reply to Kevin WolfCurrently the blktap backend in ioemu lets qemu guess which format an image is in. This was a security problem and the blktap backend doesn't work any more since this was fixed in qemu.
This patch changes ioemu to respect the format it gets from blktapctrl.
--- hw/xen_blktap.c | 22 +++++++++++++++++++--- hw/xen_blktap.h | 14 ++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-)
hw/xen_blktap.c +19 -3
@@ -220,9 +220,10 @@ static int map_new_dev(struct td_state *s, int minor)return -1 ; } -static int open_disk (struct td_state *s,char *path,int readonly) +static int open_disk (struct td_state *s,char *path,int driver,int readonly) {BlockDriverState * bs; +BlockDriver * drv;char * devname;static int devnumber =0 ;int i;
@@ -232,7 +233,22 @@ static int open_disk(struct td_state *s, char *path, int readonly) bs =bdrv_new (devname);free (devname); -if (bdrv_open (bs, path,0 ) !=0 ) { +/* Search for disk driver */ +for (i =0 ; blktap_drivers[i].idnum >=0 ; i++) { +if (blktap_drivers[i].idnum == driver) +break ; + } + +if (blktap_drivers[i].idnum <0 ) { +fprintf (stderr,"Could not find image format id %d\n" , driver); +return -ENOMEM ; + } + + drv = blktap_drivers[i].drv; +DPRINTF ("%s driver specified\n" , drv ? drv->format_name :"No" ); + +/* Open the image */ +if (bdrv_open2 (bs, path,0 , drv) !=0 ) {fprintf (stderr,"Could not open image file %s\n" , path);return -ENOMEM ; }
@@ -523,7 +539,7 @@ static void handle_blktap_ctrlmsg(void* private) s =state_init ();/*Open file*/ -if (s ==NULL ||open_disk (s, path, msg->readonly)) { +if (s ==NULL ||open_disk (s, path, msg->drivertype, msg->readonly)) { msglen =sizeof (msg_hdr_t); msg->type =CTLMSG_IMG_FAIL ; msg->len = msglen;
hw/xen_blktap.h +14 -0
@@ -50,4 +50,18 @@ typedef struct fd_list_entry { struct fd_list_entry **pprev, *next; } fd_list_entry_t; +typedef struct disk_info { +int idnum; +struct BlockDriver *drv; +}disk_info_t ; + +static disk_info_t blktap_drivers[] = { + {DISK_TYPE_AIO , &bdrv_raw }, + {DISK_TYPE_SYNC , &bdrv_raw }, + {DISK_TYPE_VMDK , &bdrv_vmdk }, + {DISK_TYPE_QCOW , &bdrv_qcow }, + {DISK_TYPE_QCOW2 , &bdrv_qcow2 }, + {-1 ,NULL } +}; +#endif /*XEN_BLKTAP_H_*/
Signature · 1 line
1.6.0.2
PATCH 3/6 ioemu: Build tapdisk-ioemu binary
In reply to Kevin WolfWhen changing away from the old ioemu, changes in the Makefiles resulted in tapdisk-ioemu appearing there, but actually not being built. This patch re-enables the build of tapdisk-ioemu.
--- Makefile | 22 +++++++++++++++------- configure | 2 +- qemu-tool.c | 2 +- tapdisk-ioemu.c | 17 ----------------- 4 files changed, 17 insertions(+), 26 deletions(-)
Makefile +15 -7
@@ -44,13 +44,6 @@ $(filter %-user,$(SUBDIR_RULES)): libqemu_user.a recurse-all: $(SUBDIR_RULES) -tapdisk-ioemu: CPPFLAGS += -I$(XEN_ROOT)/tools/libxc -tapdisk-ioemu: CPPFLAGS += -I$(XEN_ROOT)/tools/blktap/lib -tapdisk-ioemu: CPPFLAGS += -I$(XEN_ROOT)/tools/xenstore -tapdisk-ioemu: CPPFLAGS += -I$(XEN_ROOT)/tools/include -tapdisk-ioemu: tapdisk-ioemu.c cutils.c block.c block-raw.c block-cow.c block-qcow.c aes.c block-vmdk.c block-cloop.c block-dmg.c block-bochs.c block-vpc.c block-vvfat.c block-qcow2.c hw/xen_blktap.c osdep.c - $(CC) -DQEMU_TOOL $(CFLAGS) $(CPPFLAGS) $(BASE_CFLAGS) $(LDFLAGS) $(BASE_LDFLAGS) -o $@ $^ -lz $(LIBS) - ####################################################################### # BLOCK_OBJS is code used by both qemu system emulation and qemu-img
@@ -66,6 +59,21 @@ else BLOCK_OBJS += block-raw-posix.o endif +####################################################################### +# tapdisk-ioemu + +hw/tapdisk-xen_blktap.o: hw/xen_blktap.c + $(CC) $(CFLAGS) $(CPPFLAGS) -DQEMU_IMG -DQEMU_TOOL -c -o $@ $< +tapdisk-ioemu.o: tapdisk-ioemu.c + $(CC) $(CFLAGS) $(CPPFLAGS) -DQEMU_IMG -DQEMU_TOOL -c -o $@ $< + +tapdisk-ioemu: CPPFLAGS += -I$(XEN_ROOT)/tools/libxc +tapdisk-ioemu: CPPFLAGS += -I$(XEN_ROOT)/tools/blktap/lib +tapdisk-ioemu: CPPFLAGS += -I$(XEN_ROOT)/tools/xenstore +tapdisk-ioemu: CPPFLAGS += -I$(XEN_ROOT)/tools/include +tapdisk-ioemu: tapdisk-ioemu.o $(BLOCK_OBJS) qemu-tool.o hw/tapdisk-xen_blktap.o + $(CC) $(LDFLAGS) -o $@ $^ -lz $(LIBS) + ###################################################################### # libqemu_common.a: Target independent part of system emulation. The # long term path is to suppress *all* target specific code in case of
configure +1 -1
@@ -1326,7 +1326,7 @@ fi echo "#define CONFIG_UNAME_RELEASE \"$uname_release\"" >> $config_h -tools= +tools="tapdisk-ioemu" if test `expr "$target_list" : ".*softmmu.*"` != 0 ; then tools="qemu-img\$(EXESUF) $tools" if [ "$linux" = "yes" ] ; then
qemu-tool.c +1 -1
@@ -70,7 +70,7 @@ void qemu_bh_delete(QEMUBH *bh)qemu_free (bh); } -int qemu_set_fd_handler2(int fd, +int __attribute__((weak)) qemu_set_fd_handler2(int fd, IOCanRWHandler *fd_read_poll, IOHandler *fd_read, IOHandler *fd_write,
tapdisk-ioemu.c +0 -17
@@ -22,20 +22,6 @@ extern void *fd_start;int domid =0 ;FILE * logfile; -void term_printf (const char *fmt, ...) -{ -va_list ap; -va_start (ap, fmt); -vprintf (fmt, ap); -va_end (ap); -} - -void term_print_filename (const char *filename) -{ -term_printf (filename); -} - -typedef void IOReadHandler (void *opaque,const uint8_t *buf,int size);typedef int IOCanRWHandler (void *opaque);typedef void IOHandler (void *opaque);
@@ -103,7 +89,6 @@ int main(void) logfile = stderr;bdrv_init (); -qemu_aio_init ();init_blktap ();/* Daemonize */
@@ -115,8 +100,6 @@ int main(void)* completed aio operations. */ while (1 ) { -qemu_aio_poll (); - max_fd =-1 ;FD_ZERO (&rfds);for (ioh = first_io_handler; ioh !=NULL ; ioh = ioh->next)
Signature · 1 line
1.6.0.2
PATCH 4/6 tapdisk-ioemu: Write messages to a logfile
In reply to Kevin WolfTypically, tapdisk-ioemu runs as a daemon and messages to stderr are simply lost. Write them to a logfile instead.
--- tapdisk-ioemu.c | 19 +++++++++++++------ 1 files changed, 13 insertions(+), 6 deletions(-)
tapdisk-ioemu.c +13 -6
@@ -86,15 +86,22 @@ int main(void)struct timeval tv;void *old_fd_start =NULL ; - logfile = stderr; - +/* Daemonize */ +if (fork () !=0 ) +exit (0 ); +bdrv_init ();init_blktap (); -/* Daemonize */ -if (fork () !=0 ) -exit (0 ); - + logfile =fopen ("/var/log/xen/tapdisk-ioemu.log" ,"a" ); +if (logfile) { +setbuf (logfile,NULL ); +fclose (stderr); + stderr = logfile; + }else { + logfile = stderr; + } +/* * Main loop: Pass events to the corrsponding handlers and check for * completed aio operations.
Signature · 1 line
1.6.0.2
PATCH 5/6 ioemu: Fail on too small blktap disks
In reply to Kevin WolfThe blktap infrastructure doesn't seems to be able to cope with images that are smaller than a sector, it produced hangs for me. Such an image isn't really useful anyway, so just fail gracefully.
--- hw/xen_blktap.c | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
hw/xen_blktap.c +6 -0
@@ -258,6 +258,12 @@ static int open_disk(struct td_state *s, char *path, int driver, int readonly) s->size = bs->total_sectors; s->sector_size =512 ; +if (s->size ==0 ) { +fprintf (stderr,"Error: Disk image %s is too small\n" , + path); +return -ENOMEM ; + } + s->info = ((s->flags &TD_RDONLY ) ?VDISK_READONLY :0 );#ifndef QEMU_TOOL
Signature · 1 line
1.6.0.2
PATCH 6/6 tapdisk-ioemu: Fix shutdown condition
In reply to Kevin WolfEven when opening the only image a tapdisk-ioemu instance is responsible for fails, it can't immediately shut down. blktapctrl still wants to communicate with tapdisk-ioemu and close the disk.
This patch changes tapdisk-ioemu to count the connections to blktapctrl rather than the number of opened disk images.
--- hw/xen_blktap.c | 5 ++++- tapdisk-ioemu.c | 13 ++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-)
hw/xen_blktap.c +4 -1
@@ -67,6 +67,7 @@ int read_fd;int write_fd;static pid_t process; +int connected_disks =0 ;fd_list_entry_t *fd_start =NULL ;static void handle_blktap_iomsg (void * private);
@@ -543,6 +544,7 @@ static void handle_blktap_ctrlmsg(void* private)/* Allocate the disk structs */ s =state_init (); + connected_disks++;/*Open file*/ if (s ==NULL ||open_disk (s, path, msg->drivertype, msg->readonly)) {
@@ -593,7 +595,8 @@ static void handle_blktap_ctrlmsg(void* private)case CTLMSG_CLOSE : s =get_state (msg->cookie);if (s)unmap_disk (s); -break ; + connected_disks--; +break ;case CTLMSG_PID :memset (buf,0x00 ,MSG_SIZE );
tapdisk-ioemu.c +10 -3
@@ -18,6 +18,7 @@ extern void *qemu_mallocz(size_t size);extern void qemu_free (void *ptr);extern void *fd_start; +extern int connected_disks;int domid =0 ;FILE * logfile;
@@ -84,7 +85,7 @@ int main(void)int max_fd;fd_set rfds;struct timeval tv; -void *old_fd_start =NULL ; +int old_connected_disks =0 ;/* Daemonize */ if (fork () !=0 )
@@ -136,11 +137,17 @@ int main(void) pioh = &ioh->next; } +if (old_connected_disks != connected_disks) +fprintf (stderr,"connected disks: %d => %d\n" , + old_connected_disks, connected_disks); +/* Exit when the last image has been closed */ -if (old_fd_start !=NULL && fd_start ==NULL ) +if (old_connected_disks !=0 && connected_disks ==0 ) { +fprintf (stderr,"Last image is closed, exiting.\n" );exit (0 ); + } - old_fd_start = fd_start; + old_connected_disks = connected_disks; }return 0 ; }
Signature · 1 line
1.6.0.2