Skip to contentSkip to conversation

block migration interaction fixes

PATCH Applied Marcelo Tosatti 2 participants 7 messages Jan 26, 2011 – Feb 7, 2011

lore
Share & tools
lore
#0 lore raw

cover block migration interaction fixes

See individual patches for details.

#1 lore raw

PATCH 1/5 block-migration: actually disable dirty tracking on cleanup

Call to set_dirty_tracking() is misplaced.

block-migration.c +2 -2
Index: qemu/block-migration.c
===================================================================
@@ -528,6 +528,8 @@ static void blk_mig_cleanup(Monitor *mon
     BlkMigDevState *bmds;
     BlkMigBlock *blk;
 
+    set_dirty_tracking(0);
+
     while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
         QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
         qemu_free(bmds->aio_bitmap);
@@ -540,8 +542,6 @@ static void blk_mig_cleanup(Monitor *mon
         qemu_free(blk);
     }
 
-    set_dirty_tracking(0);
-
     monitor_printf(mon, "\n");
 }
 
#2 lore raw

PATCH 2/5 blockdev: add refcount to DriveInfo

In reply to Marcelo Tosatti

The host part of a block device can be deleted with in progress block migration.

To fix this, add a reference count to DriveInfo, freeing resources on last reference.

CC Markus Armbruster <[email protected]>
blockdev.c +16 -2
Index: qemu/blockdev.c
===================================================================
@@ -40,7 +40,7 @@ void blockdev_auto_del(BlockDriverState 
     DriveInfo *dinfo = drive_get_by_blockdev(bs);
 
     if (dinfo && dinfo->auto_del) {
-        drive_uninit(dinfo);
+        drive_put_ref(dinfo);
     }
 }
 
@@ -110,7 +110,7 @@ static void bdrv_format_print(void *opaq
     fprintf(stderr, " %s", name);
 }
 
-void drive_uninit(DriveInfo *dinfo)
+static void drive_uninit(DriveInfo *dinfo)
 {
     qemu_opts_del(dinfo->opts);
     bdrv_delete(dinfo->bdrv);
@@ -118,6 +118,19 @@ void drive_uninit(DriveInfo *dinfo)
     qemu_free(dinfo);
 }
 
+void drive_put_ref(DriveInfo *dinfo)
+{
+    assert(dinfo->refcount);
+    if (--dinfo->refcount == 0) {
+        drive_uninit(dinfo);
+    }
+}
+
+void drive_get_ref(DriveInfo *dinfo)
+{
+    dinfo->refcount++;
+}
+
 static int parse_block_error_action(const char *buf, int is_read)
 {
     if (!strcmp(buf, "ignore")) {
@@ -421,6 +434,7 @@ DriveInfo *drive_init(QemuOpts *opts, in
     dinfo->bus = bus_id;
     dinfo->unit = unit_id;
     dinfo->opts = opts;
+    dinfo->refcount = 1;
     if (serial)
         strncpy(dinfo->serial, serial, sizeof(dinfo->serial) - 1);
     QTAILQ_INSERT_TAIL(&drives, dinfo, next);
blockdev.h +3 -1
Index: qemu/blockdev.h
===================================================================
@@ -29,6 +29,7 @@ struct DriveInfo {
     QemuOpts *opts;
     char serial[BLOCK_SERIAL_STRLEN + 1];
     QTAILQ_ENTRY(DriveInfo) next;
+    int refcount;
 };
 
 #define MAX_IDE_DEVS	2
@@ -36,7 +37,8 @@ struct DriveInfo {
 
 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit);
 int drive_get_max_bus(BlockInterfaceType type);
-void drive_uninit(DriveInfo *dinfo);
+void drive_get_ref(DriveInfo *dinfo);
+void drive_put_ref(DriveInfo *dinfo);
 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs);
 
 QemuOpts *drive_add(const char *file, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
hw/pci-hotplug.c +1 -1
Index: qemu/hw/pci-hotplug.c
===================================================================
@@ -146,7 +146,7 @@ void drive_hot_add(Monitor *mon, const Q
 
 err:
     if (dinfo)
-        drive_uninit(dinfo);
+        drive_put_ref(dinfo);
     return;
 }
 
#3 lore raw

PATCH 3/5 block-migration: add reference to target DriveInfo

In reply to Marcelo Tosatti

So that ejection of attached device by guest does not free data in use by block migration instance.

CC Markus Armbruster <[email protected]>
block-migration.c +3 -0
Index: qemu/block-migration.c
===================================================================
@@ -19,6 +19,7 @@
 #include "monitor.h"
 #include "block-migration.h"
 #include "migration.h"
+#include "blockdev.h"
 #include <assert.h>
 
 #define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << BDRV_SECTOR_BITS)
@@ -299,6 +300,7 @@ static void init_blk_migration_it(void *
         bmds->completed_sectors = 0;
         bmds->shared_base = block_mig_state.shared_base;
         alloc_aio_bitmap(bmds);
+        drive_get_ref(drive_get_by_blockdev(bs));
 
         block_mig_state.total_sector_sum += sectors;
 
@@ -532,6 +534,7 @@ static void blk_mig_cleanup(Monitor *mon
 
     while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
         QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
+        drive_put_ref(drive_get_by_blockdev(bmds->bs));
         qemu_free(bmds->aio_bitmap);
         qemu_free(bmds);
     }
#4 lore raw

PATCH 4/5 Add flag to indicate external users to block device

In reply to Marcelo Tosatti

Certain operations such as drive_del or resize cannot be performed while external users (eg. block migration) reference the block device.

Add a flag to indicate that.

block.c +11 -0
Index: qemu/block.c
===================================================================
@@ -2770,6 +2770,17 @@ int64_t bdrv_get_dirty_count(BlockDriver
     return bs->dirty_count;
 }
 
+void bdrv_set_in_use(BlockDriverState *bs, int in_use)
+{
+    assert(bs->in_use != in_use);
+    bs->in_use = in_use;
+}
+
+int bdrv_in_use(BlockDriverState *bs)
+{
+    return bs->in_use;
+}
+
 int bdrv_img_create(const char *filename, const char *fmt,
                     const char *base_filename, const char *base_fmt,
                     char *options, uint64_t img_size, int flags)
block_int.h +1 -0
Index: qemu/block_int.h
===================================================================
@@ -199,6 +199,7 @@ struct BlockDriverState {
     char device_name[32];
     unsigned long *dirty_bitmap;
     int64_t dirty_count;
+    int in_use; /* users other than guest access, eg. block migration */
     QTAILQ_ENTRY(BlockDriverState) list;
     void *private;
 };
block.h +2 -0
Index: qemu/block.h
===================================================================
@@ -240,6 +240,8 @@ void bdrv_reset_dirty(BlockDriverState *
                       int nr_sectors);
 int64_t bdrv_get_dirty_count(BlockDriverState *bs);
 
+void bdrv_set_in_use(BlockDriverState *bs, int in_use);
+int bdrv_in_use(BlockDriverState *bs);
 
 typedef enum {
     BLKDBG_L1_UPDATE,
#5 lore raw

PATCH 5/5 block: enable in_use flag

In reply to Marcelo Tosatti

Set block device in use during block migration, disallow drive_del and bdrv_truncate for in use devices.

blockdev.c +4 -0
Index: qemu/blockdev.c
===================================================================
@@ -690,6 +690,10 @@ int do_drive_del(Monitor *mon, const QDi
         qerror_report(QERR_DEVICE_NOT_FOUND, id);
         return -1;
     }
+    if (bdrv_in_use(bs)) {
+        qerror_report(QERR_DEVICE_IN_USE, id);
+        return -1;
+    }
 
     /* quiesce block driver; prevent further io */
     qemu_aio_flush();
block-migration.c +2 -0
Index: qemu/block-migration.c
===================================================================
@@ -301,6 +301,7 @@ static void init_blk_migration_it(void *
         bmds->shared_base = block_mig_state.shared_base;
         alloc_aio_bitmap(bmds);
         drive_get_ref(drive_get_by_blockdev(bs));
+        bdrv_set_in_use(bs, 1);
 
         block_mig_state.total_sector_sum += sectors;
 
@@ -534,6 +535,7 @@ static void blk_mig_cleanup(Monitor *mon
 
     while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
         QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
+        bdrv_set_in_use(bmds->bs, 0);
         drive_put_ref(drive_get_by_blockdev(bmds->bs));
         qemu_free(bmds->aio_bitmap);
         qemu_free(bmds);
block.c +2 -0
Index: qemu/block.c
===================================================================
@@ -1132,6 +1132,8 @@ int bdrv_truncate(BlockDriverState *bs, 
         return -ENOTSUP;
     if (bs->read_only)
         return -EACCES;
+    if (bdrv_in_use(bs))
+        return -EBUSY;
     ret = drv->bdrv_truncate(bs, offset);
     if (ret == 0) {
         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);