Skip to contentSkip to conversation

Qemu: Hostcache setting from cmdline and monitor

PATCH Superseded Supriya Kannery 4 participants 21 messages Jul 4, 2011 – Jul 26, 2011

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 cover Qemu: Hostcache setting from cmdline and monitor

Currently cache setting of a block device cannot be changed
without restarting a running VM. Following patchset is for enabling dynamic change of cache setting for block devices through qemu monitor. Code changes are based on patches from Christoph Hellwig and Prerna Saxena.

This patchset introduces monitor command 'block_set'
using which various parameters for block devices can be changed dynamically. Dynamic changing of host cache setting is implemented for now. This patchset also introduces 'hostcache', a new option for setting host cache from qemu command line. 'Hostcache and 'cache' options cannot be used simultaneously from commandline.

Changes from patchset V3:
1. Added 'hostcache' option to '-drive' commandline option.

New block command added:
"block_set"
-- Sets block device parameters while guest is running.
Usage:
block_set <device> <param> <value>
<device> = block device
<param> = parameter (say, "hostcache")
<value> = on/off

New 'hostcache' option added to -drive:
-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"

        ....    

" [,readonly=on|off][,hostcache=on|off]\n"

1/4 Enhance "info block" to display hostcache setting 2/4 New error classes for file reopen and device insertion 3/4 Command "block_set" for dynamic params change for block device 4/4 'hostcache' option added to -drive in qemu commandline

 qemu/block.c         |   73 +++++++++++++++++++++++++++++++++++++++++++++++----
 qemu/block.h         |    2 +
 qemu/blockdev.c      |   43 +++++++++++++++++++++++++++++++++++++++++++++++
 qemu/blockdev.h      |    1
 qemu/hmp-commands.hx |   15 ++++++++++++++
 qemu/qemu-config.c   |    4 +++
 qemu/qemu-options.hx |    2 -
 qemu/qemu.pod        |    5 ++++
 qemu/qerror.c        |    8 +++++++
 qemu/qerror.h        |    6 +++++
 qemu/qmp-commands.hx |   30 +++++++++++++++++++++++++++
 11 files changed, 184 insertions(+), 5 deletions(-)
#1 lore raw

PATCH v4 1/4 Qemu: Enhance "info block" to display host cache setting

Enhance "info block" to display hostcache setting for each block device.

Example:
(qemu) info block
ide0-hd0: type=hd removable=0 file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

Enhanced to display "hostcache" setting:
(qemu) info block
ide0-hd0: type=hd removable=0 hostcache=true file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

---
 block.c         |   21 +++++++++++++++++----
 qmp-commands.hx |    2 ++
 2 files changed, 19 insertions(+), 4 deletions(-)
block.c +17 -4
Index: qemu/block.c
===================================================================
@@ -1694,6 +1694,14 @@ static void bdrv_print_dict(QObject *obj
         monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
     }
 
+    if (qdict_haskey(bs_dict, "open_flags")) {
+        int open_flags = qdict_get_int(bs_dict, "open_flags");
+        if (open_flags & BDRV_O_NOCACHE)
+            monitor_printf(mon, " hostcache=false");
+        else
+            monitor_printf(mon, " hostcache=true");
+    }
+
     if (qdict_haskey(bs_dict, "inserted")) {
         QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
 
@@ -1730,13 +1738,18 @@ void bdrv_info(Monitor *mon, QObject **r
         QObject *bs_obj;
 
         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
-                                    "'removable': %i, 'locked': %i }",
-                                    bs->device_name, bs->removable,
-                                    bs->locked);
+                                     "'removable': %i, 'locked': %i, "
+                                     "'hostcache': %s }",
+                                     bs->device_name, bs->removable,
+                                     bs->locked,
+                                     (bs->open_flags & BDRV_O_NOCACHE) ?
+                                     "false" : "true");
+
+        QDict *bs_dict = qobject_to_qdict(bs_obj);
+        qdict_put(bs_dict, "open_flags", qint_from_int(bs->open_flags));
 
         if (bs->drv) {
             QObject *obj;
-            QDict *bs_dict = qobject_to_qdict(bs_obj);
 
             obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
                                      "'encrypted': %i }",
qmp-commands.hx +2 -0
Index: qemu/qmp-commands.hx
===================================================================
@@ -1070,6 +1070,7 @@ Each json-object contain the following:
          - Possible values: "unknown"
 - "removable": true if the device is removable, false otherwise (json-bool)
 - "locked": true if the device is locked, false otherwise (json-bool)
+- "hostcache": true if hostcache enabled, false otherwise (json-bool)
 - "inserted": only present if the device is inserted, it is a json-object
    containing the following:
          - "file": device file name (json-string)
@@ -1091,6 +1092,7 @@ Example:
          {
             "device":"ide0-hd0",
             "locked":false,
+            "hostcache":false,
             "removable":false,
             "inserted":{
                "ro":false,
#5 lore raw

PATCH v4 2/4 Qemu: Error classes for file reopen and data sync failure

In reply to Supriya Kannery

New error classes defined for cases where device not inserted and file reopen failed.

---
 qerror.c |    8 ++++++++
 qerror.h |    6 ++++++
 2 files changed, 14 insertions(+)
qerror.c +8 -0
Index: qemu/qerror.c
===================================================================
@@ -97,6 +97,14 @@ static const QErrorStringTable qerror_ta
         .desc      = "Device '%(device)' is not removable",
     },
     {
+        .error_fmt = QERR_DATA_SYNC_FAILED,
+        .desc      = "Syncing of data failed for device '%(device)'",
+    },
+    {
+        .error_fmt = QERR_REOPEN_FILE_FAILED,
+        .desc      = "Could not reopen '%(filename)'",
+    },
+    {
         .error_fmt = QERR_DEVICE_NO_BUS,
         .desc      = "Device '%(device)' has no child bus",
     },
qerror.h +6 -0
Index: qemu/qerror.h
===================================================================
@@ -85,6 +85,9 @@ QError *qobject_to_qerror(const QObject 
 #define QERR_DEVICE_NOT_FOUND \
     "{ 'class': 'DeviceNotFound', 'data': { 'device': %s } }"
 
+#define QERR_DATA_SYNC_FAILED \
+    "{ 'class': 'DataSyncFailed', 'data': { 'device': %s } }"
+
 #define QERR_DEVICE_NOT_REMOVABLE \
     "{ 'class': 'DeviceNotRemovable', 'data': { 'device': %s } }"
 
@@ -139,6 +142,9 @@ QError *qobject_to_qerror(const QObject 
 #define QERR_OPEN_FILE_FAILED \
     "{ 'class': 'OpenFileFailed', 'data': { 'filename': %s } }"
 
+#define QERR_REOPEN_FILE_FAILED \
+    "{ 'class': 'ReopenFileFailed', 'data': { 'filename': %s } }"
+
 #define QERR_PROPERTY_NOT_FOUND \
     "{ 'class': 'PropertyNotFound', 'data': { 'device': %s, 'property': %s } }"
 
#6 lore raw

PATCH v4 3/4 Qemu: Command "block_set" for dynamic block params change

In reply to Supriya Kannery

New command "block_set" added for dynamically changing any of the block device parameters. For now, dynamic setting of hostcache params using this command is implemented. Other block device parameters, can be integrated in similar lines.

---
 block.c         |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 block.h         |    2 ++
 blockdev.c      |   26 ++++++++++++++++++++++++++
 blockdev.h      |    1 +
 hmp-commands.hx |   15 +++++++++++++++
 qmp-commands.hx |   28 ++++++++++++++++++++++++++++
 6 files changed, 124 insertions(+)
block.c +52 -0
Index: qemu/block.c
===================================================================
@@ -651,6 +651,32 @@ unlink_and_fail:
     return ret;
 }
 
+int bdrv_reopen(BlockDriverState *bs, int bdrv_flags)
+{
+    BlockDriver *drv = bs->drv;
+    int ret = 0;
+
+    /* Quiesce IO for the given block device */
+    qemu_aio_flush();
+    if (bdrv_flush(bs)) {
+        qerror_report(QERR_DATA_SYNC_FAILED, bs->device_name);
+        return ret;
+    }
+    bdrv_close(bs);
+
+    ret = bdrv_open(bs, bs->filename, bdrv_flags, drv);
+
+    /*
+     * A failed attempt to reopen the image file must lead to 'abort()'
+    */
+    if (ret != 0) {
+        qerror_report(QERR_REOPEN_FILE_FAILED, bs->filename);
+        abort();
+    }
+
+    return ret;
+}
+
 void bdrv_close(BlockDriverState *bs)
 {
     if (bs->drv) {
@@ -691,6 +717,32 @@ void bdrv_close_all(void)
     }
 }
 
+int bdrv_change_hostcache(BlockDriverState *bs, bool enable_host_cache)
+{
+    int bdrv_flags = bs->open_flags;
+
+    /* set hostcache flags (without changing WCE/flush bits) */
+    if (enable_host_cache) {
+        bdrv_flags &= ~BDRV_O_NOCACHE;
+    } else {
+        bdrv_flags |= BDRV_O_NOCACHE;
+    }
+
+    /* If no change in flags, no need to reopen */
+    if (bdrv_flags == bs->open_flags) {
+        return 0;
+    }
+
+    if (bdrv_is_inserted(bs)) {
+        /* Reopen file with changed set of flags */
+        return bdrv_reopen(bs, bdrv_flags);
+    } else {
+        /* Save hostcache change for future use */
+        bs->open_flags = bdrv_flags;
+        return 0;
+    }
+}
+
 /* make a BlockDriverState anonymous by removing from bdrv_state list.
    Also, NULL terminate the device_name to prevent double remove */
 void bdrv_make_anon(BlockDriverState *bs)
block.h +2 -0
Index: qemu/block.h
===================================================================
@@ -71,6 +71,7 @@ void bdrv_delete(BlockDriverState *bs);
 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags);
 int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
               BlockDriver *drv);
+int bdrv_reopen(BlockDriverState *bs, int bdrv_flags);
 void bdrv_close(BlockDriverState *bs);
 int bdrv_attach(BlockDriverState *bs, DeviceState *qdev);
 void bdrv_detach(BlockDriverState *bs, DeviceState *qdev);
@@ -96,6 +97,7 @@ void bdrv_commit_all(void);
 int bdrv_change_backing_file(BlockDriverState *bs,
     const char *backing_file, const char *backing_fmt);
 void bdrv_register(BlockDriver *bdrv);
+int bdrv_change_hostcache(BlockDriverState *bs, bool enable_host_cache);
 
 
 typedef struct BdrvCheckResult {
blockdev.c +26 -0
Index: qemu/blockdev.c
===================================================================
@@ -797,3 +797,29 @@ int do_block_resize(Monitor *mon, const 
 
     return 0;
 }
+
+
+/*
+ * Handle changes to block device settings, like hostcache,
+ * while guest is running.
+*/
+int do_block_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    const char *device = qdict_get_str(qdict, "device");
+    const char *name = qdict_get_str(qdict, "name");
+    int enable = qdict_get_bool(qdict, "enable");
+    BlockDriverState *bs;
+
+    bs = bdrv_find(device);
+    if (!bs) {
+        qerror_report(QERR_DEVICE_NOT_FOUND, device);
+        return -1;
+    }
+
+    if (!strcmp(name, "hostcache")) {
+        return bdrv_change_hostcache(bs, enable);
+    }
+
+    return 0;
+}
+
blockdev.h +1 -0
Index: qemu/blockdev.h
===================================================================
@@ -65,5 +65,6 @@ int do_change_block(Monitor *mon, const 
 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data);
+int do_block_set(Monitor *mon, const QDict *qdict, QObject **ret_data);
 
 #endif
hmp-commands.hx +15 -0
Index: qemu/hmp-commands.hx
===================================================================
@@ -70,6 +70,21 @@ but should be used with extreme caution.
 resizes image files, it can not resize block devices like LVM volumes.
 ETEXI
 
+    {
+        .name       = "block_set",
+        .args_type  = "device:B,name:s,enable:b",
+        .params     = "device name enable",
+        .help       = "On/Off block device parameters like hostcache",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_block_set,
+    },
+
+STEXI
+@item block_set
+@findex block_set
+Change block device params (eg:"hostcache"=on/off) while guest is running.
+ETEXI
+
 
     {
         .name       = "eject",
qmp-commands.hx +28 -0
Index: qemu/qmp-commands.hx
===================================================================
@@ -694,6 +694,34 @@ Example:
 EQMP
 
     {
+        .name       = "block_set",
+        .args_type  = "device:B,name:s,enable:b",
+        .params     = "device name enable",
+        .help       = "Enable/Disable block device params like hostcache",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_block_set,
+    },
+
+SQMP
+block_set
+---------
+
+Change various block device parameters like hostcache.
+
+Arguments:
+
+- "device": the device's ID, must be unique (json-string)
+- "name": name of the parameter to be changed" (json-string)
+- "enable": value to be set for the parameter, 'true' or 'false' (json-bool)
+
+Example:
+
+-> { "execute": "block_set", "arguments": { "device": "ide0-hd0", "name": "hostcache", "enable": true } }
+<- { "return": {} }
+
+EQMP
+
+	{
         .name       = "balloon",
         .args_type  = "value:M",
         .params     = "target",
#17 lore raw

PATCH v4 4/4 Qemu: Add commandline -drive option 'hostcache'

In reply to Supriya Kannery

qemu command option 'hostcache' added to -drive for block devices. While starting a VM from qemu commandline, this option can be used for setting host cache usage for block data access. It is not allowed to specify both 'hostcache' and 'cache' options in the same commandline. User has to specify only one among these.

---
 blockdev.c      |   17 +++++++++++++++++
 qemu-config.c   |    4 ++++
 qemu-options.hx |    2 +-
 qemu.pod        |    5 +++++
 4 files changed, 27 insertions(+), 1 deletion(-)
blockdev.c +17 -0
Index: qemu/blockdev.c
===================================================================
@@ -238,6 +238,7 @@ DriveInfo *drive_init(QemuOpts *opts, in
     DriveInfo *dinfo;
     int snapshot = 0;
     int ret;
+    bool option_hostcache = false;
 
     translation = BIOS_ATA_TRANSLATION_AUTO;
 
@@ -324,7 +325,23 @@ DriveInfo *drive_init(QemuOpts *opts, in
 	}
     }
 
+    if ((buf = qemu_opt_get(opts, "hostcache")) != NULL) {
+        if (!strcmp(buf, "off")) {
+            bdrv_flags |= BDRV_O_NOCACHE;
+        } else if (!strcmp(buf, "on")) {
+            bdrv_flags &= ~BDRV_O_NOCACHE;
+        } else {
+            error_report("invalid hostcache option");
+            return NULL;
+        }
+        option_hostcache = true;
+    }
+
     if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
+        if (option_hostcache) {
+            error_report("'hostcache' and 'cache' cannot co-exist");
+            return NULL;
+        }
         if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
             bdrv_flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
         } else if (!strcmp(buf, "writeback")) {
qemu-options.hx +1 -1
Index: qemu/qemu-options.hx
===================================================================
@@ -120,7 +120,7 @@ DEF("drive", HAS_ARG, QEMU_OPTION_drive,
     "       [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
     "       [,cache=writethrough|writeback|none|unsafe][,format=f]\n"
     "       [,serial=s][,addr=A][,id=name][,aio=threads|native]\n"
-    "       [,readonly=on|off]\n"
+    "       [,readonly=on|off][,hostcache=on|off]\n"
     "                use 'file' as a drive image\n", QEMU_ARCH_ALL)
 STEXI
 @item -drive @var{option}[,@var{option}[,@var{option}[,...]]]
qemu-config.c +4 -0
Index: qemu/qemu-config.c
===================================================================
@@ -78,6 +78,10 @@ static QemuOptsList qemu_drive_opts = {
         },{
             .name = "readonly",
             .type = QEMU_OPT_BOOL,
+        },{
+            .name = "hostcache",
+            .type = QEMU_OPT_BOOL,
+            .help = "set or reset hostcache (on/off)"
         },
         { /* end of list */ }
     },
qemu.pod +5 -0
Index: qemu/qemu.pod
===================================================================
@@ -226,6 +226,11 @@ I<snapshot> is "on" or "off" and allows 
 
 I<cache> is "none", "writeback", "unsafe", or "writethrough" and controls how the host cache is used to access block data.
 
+=item B<hostcache=>I<hostcache>
+
+I<hostcache> is "on" or "off" and allows to enable or disable hostcache while accessing block data.
+Both 'cache=' and 'hostcache=' should not used in same command line. Use only one among them.
+
 =item B<aio=>I<aio>
 
 I<aio> is "threads", or "native" and selects between pthread based disk I/O and native Linux AIO.
#2 lore raw

Re: [Qemu-devel] [V4 Patch 1/4]Qemu: Enhance "info block" to display host cache setting

In reply to Supriya Kannery
Quoted 8 lines · Supriya Kannery

Enhance "info block" to display hostcache setting for each block device.

Example:
(qemu) info block
ide0-hd0: type=hd removable=0 file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

Quoted 4 lines

Enhanced to display "hostcache" setting:
(qemu) info block
ide0-hd0: type=hd removable=0 hostcache=true file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

The other boolean options use 0 or 1. Please use the same HMP output for consistency.

For QMP the true/false output is good (it's JSON).

Stefan

#7 lore raw

Re: [Qemu-devel] [V4 Patch 3/4]Qemu: Command "block_set" for dynamic block params change

In reply to Supriya Kannery
Quoted 12 lines · Supriya Kannery
blockdev.c +12 -0
+/*
+ * Handle changes to block device settings, like hostcache,
+ * while guest is running.
+*/
+int do_block_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    const char *device = qdict_get_str(qdict, "device");
+    const char *name = qdict_get_str(qdict, "name");
+    int enable = qdict_get_bool(qdict, "enable");
+    BlockDriverState *bs;
+
+    bs = bdrv_find(device);

blockdev.c int do_block_resize(Monitor *mon, const · line 823

Quoted 10 lines
blockdev.c +10 -0
+    if (!bs) {
+        qerror_report(QERR_DEVICE_NOT_FOUND, device);
+        return -1;
+    }
+
+    if (!strcmp(name, "hostcache")) {
+        return bdrv_change_hostcache(bs, enable);
+    }
+
+    return 0;

No error for unknown "name" argument?

Quoted 17 lines

 #endif
Index: qemu/hmp-commands.hx
===================================================================

hmp-commands.hx +0 -0
@@ -70,6 +70,21 @@ but should be used with extreme caution.

 resizes image files, it can not resize block devices like LVM volumes. ETEXI

+    {
+        .name       = "block_set",
+        .args_type  = "device:B,name:s,enable:b",
+        .params     = "device name enable",
+        .help       = "On/Off block device parameters like hostcache",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_block_set,
+    },

hmp-commands.hx but should be used with extreme caution. · line 86

Quoted 6 lines
hmp-commands.hx +6 -0
+
+STEXI
+@item block_set
+@findex block_set
+Change block device params (eg:"hostcache"=on/off) while guest is running.
+ETEXI

See QMP comments below.

Quoted 11 lines

+

    {
        .name       = "eject",
Index: qemu/qmp-commands.hx
===================================================================

qmp-commands.hx +0 -0
@@ -694,6 +694,34 @@ Example:

 EQMP

Quoted 4 lines

    {
+        .name       = "block_set",
+        .args_type  = "device:B,name:s,enable:b",
+        .params     = "device name enable",

Perhaps:

.args_type  = "device:B,name:s,enable:b?",
.params     = "device name [enable]",

But I am not sure what the best way to express this is.

Quoted 1 line

+        .help       = "Enable/Disable block device params like hostcache",

Arguments (like "enable") should depend on the block parameter that is being set:

.help = "Set block device parameter"

If there is no good way to support different optional arguments and types then a json-string "value" argument would be best.

Quoted 4 lines

+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_block_set,
+    },

qmp-commands.hx +1 -0
+

qmp-commands.hx Example: · line 714

Quoted 10 lines
qmp-commands.hx +10 -0
+SQMP
+block_set
+---------
+
+Change various block device parameters like hostcache.
+
+Arguments:
+
+- "device": the device's ID, must be unique (json-string)
+- "name": name of the parameter to be changed" (json-string)

Trailing '"'.

Quoted 1 line

+- "enable": value to be set for the parameter, 'true' or 'false' (json-bool)

The relevant arguments depend on which block parameter you are changing. I think "enable" should be documented specifically for "hostcache":

Block parameters:

- "hostcache": Enable/disable host buffer cache
- "enable": 'true' or 'false' (json-bool)

Stefan

#18 lore raw

Re: [Qemu-devel] [V4 Patch 4/4]Qemu: Add commandline -drive option 'hostcache'

In reply to Supriya Kannery

blockdev.c DriveInfo *drive_init(QemuOpts *opts, in · line 329

Quoted 6 lines · Supriya Kannery
blockdev.c +2 -0
@@ -324,7 +325,23 @@ DriveInfo *drive_init(QemuOpts *opts, in
        }
     }
 
+    if ((buf = qemu_opt_get(opts, "hostcache")) != NULL) {
+        if (!strcmp(buf, "off")) {

Please use qemu_opt_get_bool().

Stefan

#16 lore raw

Re: [Qemu-devel] [V4 Patch 3/4]Qemu: Command "block_set" for dynamic block params change

In reply to Supriya Kannery
Quoted 11 lines · Supriya Kannery
block.c +11 -0
+int bdrv_reopen(BlockDriverState *bs, int bdrv_flags)
+{
+    BlockDriver *drv = bs->drv;
+    int ret = 0;
+
+    /* Quiesce IO for the given block device */
+    qemu_aio_flush();
+    if (bdrv_flush(bs)) {
+        qerror_report(QERR_DATA_SYNC_FAILED, bs->device_name);
+        return ret;
+    }

block.c unlink_and_fail: · line 674

Quoted 10 lines
block.c +10 -0
+    bdrv_close(bs);
+
+    ret = bdrv_open(bs, bs->filename, bdrv_flags, drv);
+
+    /*
+     * A failed attempt to reopen the image file must lead to 'abort()'
+    */
+    if (ret != 0) {
+        qerror_report(QERR_REOPEN_FILE_FAILED, bs->filename);
+        abort();

I think it is still worth trying to reopen with the old flags. If you specialy hostcache=off for a file on tmpfs then the open will fail (tmpfs doesn't support O_DIRECT), but we can still save ourselves by reopening with the old flags.

Ideally we'd probably treat this condition just like ENOSPC and keep the VM paused until the administrator has taken action, and then retry.

BTW qerror_report() followed by abort(3) does not actually report the error because we exit before QMP has a chance to send out the error.

Stefan

#3 lore raw

Re: [Qemu-devel] [V4 Patch 1/4 -Updated]Qemu: Enhance "info block" to display host cache setting

In reply to Stefan Hajnoczi

Updated patch to display hostcache = 1/0 instead of true/false in monitor.

-------------------------------------------------------------------
Enhance "info block" to display hostcache setting for each block device.

Example:
(qemu) info block
ide0-hd0: type=hd removable=0 file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

Enhanced to display "hostcache" setting:
(qemu) info block
ide0-hd0: type=hd removable=0 hostcache=true file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

---
 block.c         |   21 +++++++++++++++++----
 qmp-commands.hx |    2 ++
 2 files changed, 19 insertions(+), 4 deletions(-)
block.c +0 -0
Index: qemu/block.c
===================================================================
@@ -1694,6 +1694,14 @@ static void bdrv_print_dict(QObject *obj
         monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, 
"locked"));
     }

+ if (qdict_haskey(bs_dict, "open_flags")) {
+ int open_flags = qdict_get_int(bs_dict, "open_flags");
+ if (open_flags & BDRV_O_NOCACHE)
+ monitor_printf(mon, " hostcache=0");
+ else
+ monitor_printf(mon, " hostcache=1");
+ }
+

     if (qdict_haskey(bs_dict, "inserted")) {
         QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));

@@ -1730,13 +1738,18 @@ void bdrv_info(Monitor *mon, QObject **r

         QObject *bs_obj;

         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
-                                    "'removable': %i, 'locked': %i }",
-                                    bs->device_name, bs->removable,
-                                    bs->locked);
+                                     "'removable': %i, 'locked': %i, "
+                                     "'hostcache': %s }",
+                                     bs->device_name, bs->removable,
+                                     bs->locked,
+                                     (bs->open_flags & BDRV_O_NOCACHE) ?
+                                     "false" : "true");

+
+ QDict *bs_dict = qobject_to_qdict(bs_obj);
+ qdict_put(bs_dict, "open_flags", qint_from_int(bs->open_flags));

         if (bs->drv) {
             QObject *obj;

- QDict *bs_dict = qobject_to_qdict(bs_obj);

             obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
                                      "'encrypted': %i }",
qmp-commands.hx +0 -0
Index: qemu/qmp-commands.hx
===================================================================
@@ -1070,6 +1070,7 @@ Each json-object contain the following:
          - Possible values: "unknown"
 - "removable": true if the device is removable, false otherwise 

(json-bool)
- "locked": true if the device is locked, false otherwise (json-bool)
+- "hostcache": true if hostcache enabled, false otherwise (json-bool)
- "inserted": only present if the device is inserted, it is a json-object containing the following:
- "file": device file name (json-string)
@@ -1091,6 +1092,7 @@ Example:

          {
             "device":"ide0-hd0",
             "locked":false,
+            "hostcache":false,
             "removable":false,
             "inserted":{
                "ro":false,
#8 lore raw

Re: [Qemu-devel] [V4 Patch 3/4]Qemu: Command "block_set" for dynamic block params change

In reply to Stefan Hajnoczi
Quoted 17 lines · Stefan Hajnoczi

hmp-commands.hx but should be used with extreme caution. · line 76

Quoted 5 lines · Supriya Kannery
     {
hmp-commands.hx +3 -0
+        .name       = "block_set",
+        .args_type  = "device:B,name:s,enable:b",
+        .params     = "device name enable",

Perhaps:

.args_type = "device:B,name:s,enable:b?",
.params = "device name [enable]",

But I am not sure what the best way to express this is.

Quoted 1 line

+ .help = "Enable/Disable block device params like hostcache",

Quoted 8 lines

Arguments (like "enable") should depend on the block parameter that is being set:

.help = "Set block device parameter"

If there is no good way to support different optional arguments and types then a json-string "value" argument would be best.

"device_add" is defined and implemented to handle multiple types of optional arguments. Will work on to see whether that approach can be adopted for block_set
{
.name = "device_add",
.args_type = "device:O",
.params = "driver[,prop=value][,...]",
.help = "add device, like -device on the command line",
.user_print = monitor_user_noop, .mhandler.cmd_new = do_device_add,
},

Quoted 2 lines

Stefan

#19 lore raw

Re: [Qemu-devel] [V4 Patch 4/4 - Updated]Qemu: Add commandline -drive option 'hostcache'

In reply to Stefan Hajnoczi

Updated patch to use qemu_opt_get_bool() instead of qemu_opt_get() to read 'hostcache'

-------------------------------------------------------------------
qemu command option 'hostcache' added to -drive for block devices. While starting a VM from qemu commandline, this option can be used for setting host cache usage for block data access. It is not allowed to specify both 'hostcache' and 'cache' options in the same commandline. User has to specify only one among these.

---
 blockdev.c      |   13 +++++++++++++
 qemu-config.c   |    4 ++++
 qemu-options.hx |    2 +-
 3 files changed, 18 insertions(+), 1 deletion(-)
blockdev.c +13 -0
Index: qemu/blockdev.c
===================================================================
@@ -238,6 +238,7 @@ DriveInfo *drive_init(QemuOpts *opts, in
     DriveInfo *dinfo;
     int snapshot = 0;
     int ret;
+    int hostcache = 0;
 
     translation = BIOS_ATA_TRANSLATION_AUTO;
 
@@ -324,7 +325,19 @@ DriveInfo *drive_init(QemuOpts *opts, in
 	}
     }
 
+    if ((hostcache = qemu_opt_get_bool(opts, "hostcache", -1)) != -1) {
+        if (!hostcache) {
+            bdrv_flags |= BDRV_O_NOCACHE;
+        } else {
+            bdrv_flags &= ~BDRV_O_NOCACHE;
+        }
+    }
+
     if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
+        if (hostcache != -1) {
+            error_report("'hostcache' and 'cache' cannot co-exist");
+            return NULL;
+        }
         if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
             bdrv_flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
         } else if (!strcmp(buf, "writeback")) {
qemu-options.hx +1 -1
Index: qemu/qemu-options.hx
===================================================================
@@ -120,7 +120,7 @@ DEF("drive", HAS_ARG, QEMU_OPTION_drive,
     "       [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
     "       [,cache=writethrough|writeback|none|unsafe][,format=f]\n"
     "       [,serial=s][,addr=A][,id=name][,aio=threads|native]\n"
-    "       [,readonly=on|off]\n"
+    "       [,readonly=on|off][,hostcache=on|off]\n"
     "                use 'file' as a drive image\n", QEMU_ARCH_ALL)
 STEXI
 @item -drive @var{option}[,@var{option}[,@var{option}[,...]]]
qemu-config.c +4 -0
Index: qemu/qemu-config.c
===================================================================
@@ -78,6 +78,10 @@ static QemuOptsList qemu_drive_opts = {
         },{
             .name = "readonly",
             .type = QEMU_OPT_BOOL,
+        },{
+            .name = "hostcache",
+            .type = QEMU_OPT_BOOL,
+            .help = "set or reset hostcache (on/off)"
         },
         { /* end of list */ }
     },
#9 lore raw

Re: [Qemu-devel] [V4 Patch 3/4 - Updated]Qemu: Command "block_set" for dynamic block params change

In reply to Supriya Kannery

Updated "block_set" command to accept multiple -drive parameters. Also, added code for re-opening of device file with original flags, incase opening file using changed hostcache setting fails.

----------------------------------------------------------------------
New command "block_set" added for dynamically changing any of the block device parameters. For now, dynamic setting of hostcache params using this command is implemented. Other block device parameters, can be integrated in similar lines.

---
 block.c         |   60 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 block.h         |    2 +
 blockdev.c      |   60 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 blockdev.h      |    1
 hmp-commands.hx |   14 +++++++++++++
 qemu-config.c   |   13 ++++++++++++
 qemu-option.c   |   25 +++++++++++++++++++++++
 qemu-option.h   |    2 +
 qmp-commands.hx |   28 ++++++++++++++++++++++++++
 9 files changed, 205 insertions(+)
block.c +60 -0
Index: qemu/block.c
===================================================================
@@ -651,6 +651,40 @@ unlink_and_fail:
     return ret;
 }
 
+int bdrv_reopen(BlockDriverState *bs, int bdrv_flags)
+{
+    BlockDriver *drv = bs->drv;
+    int ret = 0;
+
+    /* Quiesce IO for the given block device */
+    qemu_aio_flush();
+    if (bdrv_flush(bs)) {
+        qerror_report(QERR_DATA_SYNC_FAILED, bs->device_name);
+        return ret;
+    }
+    bdrv_close(bs);
+
+
+    ret = bdrv_open(bs, bs->filename, bdrv_flags, drv);
+    if (ret < 0) {
+        /* Reopen failed. Try to open with original flags */
+        error_report("Opening file with changed flags...");
+        qerror_report(QERR_REOPEN_FILE_FAILED, bs->filename);
+
+        ret = bdrv_open(bs, bs->filename, bs->open_flags, drv);
+        if (ret < 0) {
+            /*
+             * Reopen failed with orig and modified flags
+            */
+            error_report("Opening file with original flags...");
+            qerror_report(QERR_REOPEN_FILE_FAILED, bs->filename);
+            abort();
+        }
+    }
+
+    return ret;
+}
+
 void bdrv_close(BlockDriverState *bs)
 {
     if (bs->drv) {
@@ -691,6 +725,32 @@ void bdrv_close_all(void)
     }
 }
 
+int bdrv_change_hostcache(BlockDriverState *bs, bool enable_host_cache)
+{
+    int bdrv_flags = bs->open_flags;
+
+    /* set hostcache flags (without changing WCE/flush bits) */
+    if (enable_host_cache) {
+        bdrv_flags &= ~BDRV_O_NOCACHE;
+    } else {
+        bdrv_flags |= BDRV_O_NOCACHE;
+    }
+
+    /* If no change in flags, no need to reopen */
+    if (bdrv_flags == bs->open_flags) {
+        return 0;
+    }
+
+    if (bdrv_is_inserted(bs)) {
+        /* Reopen file with changed set of flags */
+        return bdrv_reopen(bs, bdrv_flags);
+    } else {
+        /* Save hostcache change for future use */
+        bs->open_flags = bdrv_flags;
+        return 0;
+    }
+}
+
 /* make a BlockDriverState anonymous by removing from bdrv_state list.
    Also, NULL terminate the device_name to prevent double remove */
 void bdrv_make_anon(BlockDriverState *bs)
block.h +0 -0
Index: qemu/block.h
===================================================================
@@ -71,6 +71,7 @@ void bdrv_delete(BlockDriverState *bs);
 int bdrv_file_open(BlockDriverState **pbs, const char *filename, int 

flags);
int bdrv_open(BlockDriverState *bs, const char *filename, int flags,

               BlockDriver *drv);

+int bdrv_reopen(BlockDriverState *bs, int bdrv_flags);
void bdrv_close(BlockDriverState *bs);
int bdrv_attach(BlockDriverState *bs, DeviceState *qdev); void bdrv_detach(BlockDriverState *bs, DeviceState *qdev);
@@ -96,6 +97,7 @@ void bdrv_commit_all(void);
int bdrv_change_backing_file(BlockDriverState *bs,

     const char *backing_file, const char *backing_fmt);

void bdrv_register(BlockDriver *bdrv);
+int bdrv_change_hostcache(BlockDriverState *bs, bool enable_host_cache);

typedef struct BdrvCheckResult {

blockdev.c +60 -0

63 lines not on this page · raw message

blockdev.h +0 -0
Index: qemu/blockdev.h
===================================================================
@@ -65,5 +65,6 @@ int do_change_block(Monitor *mon, const
 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
 int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject 

**ret_data);
int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data);
+int do_block_set(Monitor *mon, const QDict *qdict, QObject **ret_data);

#endif

hmp-commands.hx +14 -0
Index: qemu/hmp-commands.hx
===================================================================
@@ -70,6 +70,20 @@ but should be used with extreme caution.
 resizes image files, it can not resize block devices like LVM volumes.
 ETEXI
 
+    {
+        .name       = "block_set",
+        .args_type  = "device:B,device:O",
+        .params     = "device [prop=value][,...]",
+        .help       = "Change block device parameters [hostcache=on/off]",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_block_set,
+    },
+STEXI
+@item block_set @var{config}
+@findex block_set
+Change block device parameters (eg: hostcache=on/off) while guest is running.
+ETEXI
+
 
     {
         .name       = "eject",
qemu-config.c +13 -0
Index: qemu/qemu-config.c
===================================================================
@@ -506,6 +506,19 @@ QemuOptsList *qemu_find_opts(const char
     return find_list(vm_config_groups, group);
 }
 
+/* Validate given opts list with that of defined vm_group */
+int qemu_validate_opts(QemuOpts *opts, const char *group)
+{
+    QemuOptsList *vm_group;
+
+    vm_group  = qemu_find_opts(group);
+    if (vm_group == NULL) {
+        return -1;
+    }
+
+    return qemu_opts_validate(opts, &vm_group->desc[0]);
+}
+
 void qemu_add_opts(QemuOptsList *list)
 {
     int entries, i;
qemu-option.c +25 -0
Index: qemu/qemu-option.c
===================================================================
@@ -599,6 +599,31 @@ static void qemu_opt_del(QemuOpt *opt)
     qemu_free(opt);
 }
 
+/*
+ * Delete specified parameter with name "name" from opts
+ * Return
+ *     0 - Deletion Successful
+ *    -1 - Param doesn't exist in opts
+ *     1 - Deletion Successful and opts is empty.
+*/
+
+int qemu_opt_delete(QemuOpts *opts, const char *name)
+{
+    QemuOpt *opt = qemu_opt_find(opts, name);
+    if (opt == NULL) {
+        return -1;
+    }
+
+    qemu_opt_del(opt);
+
+    if ((&opts->head)->tqh_first == NULL) {
+        /* opt queue is empty */
+        return 1;
+    }
+
+    return 0;
+}
+
 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
 {
     QemuOpt *opt;
qemu-option.h +1 -0
Index: qemu/qemu-option.h
===================================================================
@@ -121,6 +121,7 @@ int qemu_opts_set(QemuOptsList *list, co
                   const char *name, const char *value);
 const char *qemu_opts_id(QemuOpts *opts);
 void qemu_opts_del(QemuOpts *opts);
+int qemu_opt_delete(QemuOpts *opts, const char *name);
 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc);
 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char 

*firstname);
QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, int
permit_abbrev);
@@ -131,5 +132,6 @@ typedef int (*qemu_opts_loopfunc)(QemuOp
int qemu_opts_print(QemuOpts *opts, void *dummy);
int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
void *opaque,

                       int abort_on_failure);

+int qemu_validate_opts(QemuOpts *opts, const char *id);

#endif

qmp-commands.hx +28 -0
Index: qemu/qmp-commands.hx
===================================================================
@@ -693,7 +693,35 @@ Example:
 
 EQMP
 
+
     {
+        .name       = "block_set",
+        .args_type  = "device:B,device:O",
+        .params     = "device [prop=value][,...]",
+        .help       = "Change block device parameters [hostcache=on/off]",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_block_set,
+    },
+
+SQMP
+block_set
+---------
+
+Change various block device parameters (eg: hostcache=on/off)
+
+Arguments:
+
+- "device": the device's ID, must be unique (json-string)
+- device parameters to be changed (eg: "hostcache": "off")
+
+Example:
+
+-> { "execute": "block_set", "arguments": { "device": "ide0-hd0", "hostcache": "off"} }
+<- { "return": {} }
+
+EQMP
+
+	{
         .name       = "balloon",
         .args_type  = "value:M",
         .params     = "target",
#10 lore raw

Re: [Qemu-devel] [V4 Patch 3/4 - Updated]Qemu: Command "block_set" for dynamic block params change

Quoted 349 lines · Supriya Kannery

Updated "block_set" command to accept multiple -drive parameters. Also, added code for re-opening of device file with original flags, incase opening file using changed hostcache setting fails.

----------------------------------------------------------------------
New command "block_set" added for dynamically changing any of the block device parameters. For now, dynamic setting of hostcache params using this command is implemented. Other block device parameters, can be integrated in similar lines.

---
block.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ block.h | 2 +
blockdev.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ blockdev.h | 1
hmp-commands.hx | 14 +++++++++++++

qemu-config.c | 13 ++++++++++++
qemu-option.c | 25 +++++++++++++++++++++++
qemu-option.h | 2 +
qmp-commands.hx | 28 ++++++++++++++++++++++++++
9 files changed, 205 insertions(+)

Index: qemu/block.c
===================================================================

block.c +60 -0
@@ -651,6 +651,40 @@ unlink_and_fail:
 return ret;
 }
 
+int bdrv_reopen(BlockDriverState *bs, int bdrv_flags)
+{
+ BlockDriver *drv = bs->drv;
+ int ret = 0;
+
+ /* Quiesce IO for the given block device */
+ qemu_aio_flush();
+ if (bdrv_flush(bs)) {
+ qerror_report(QERR_DATA_SYNC_FAILED, bs->device_name);
+ return ret;
+ }
+ bdrv_close(bs);
+
+
+ ret = bdrv_open(bs, bs->filename, bdrv_flags, drv);
+ if (ret < 0) {
+ /* Reopen failed. Try to open with original flags */
+ error_report("Opening file with changed flags...");
+ qerror_report(QERR_REOPEN_FILE_FAILED, bs->filename);
+
+ ret = bdrv_open(bs, bs->filename, bs->open_flags, drv);
+ if (ret < 0) {
+ /*
+ * Reopen failed with orig and modified flags
+ */
+ error_report("Opening file with original flags...");
+ qerror_report(QERR_REOPEN_FILE_FAILED, bs->filename);
+ abort();
+ }
+ }
+
+ return ret;
+}
+
 void bdrv_close(BlockDriverState *bs)
 {
 if (bs->drv) {
@@ -691,6 +725,32 @@ void bdrv_close_all(void)
 }
 }
 
+int bdrv_change_hostcache(BlockDriverState *bs, bool enable_host_cache)
+{
+ int bdrv_flags = bs->open_flags;
+
+ /* set hostcache flags (without changing WCE/flush bits) */
+ if (enable_host_cache) {
+ bdrv_flags &= ~BDRV_O_NOCACHE;
+ } else {
+ bdrv_flags |= BDRV_O_NOCACHE;
+ }
+
+ /* If no change in flags, no need to reopen */
+ if (bdrv_flags == bs->open_flags) {
+ return 0;
+ }
+
+ if (bdrv_is_inserted(bs)) {
+ /* Reopen file with changed set of flags */
+ return bdrv_reopen(bs, bdrv_flags);
+ } else {
+ /* Save hostcache change for future use */
+ bs->open_flags = bdrv_flags;
+ return 0;
+ }
+}
+
 /* make a BlockDriverState anonymous by removing from bdrv_state list.

Also, NULL terminate the device_name to prevent double remove */ void bdrv_make_anon(BlockDriverState *bs)
Index: qemu/block.h
===================================================================

block.h +0 -0
@@ -71,6 +71,7 @@ void bdrv_delete(BlockDriverState *bs);

int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags);
int bdrv_open(BlockDriverState *bs, const char *filename, int flags, BlockDriver *drv);
+int bdrv_reopen(BlockDriverState *bs, int bdrv_flags); void bdrv_close(BlockDriverState *bs);
int bdrv_attach(BlockDriverState *bs, DeviceState *qdev); void bdrv_detach(BlockDriverState *bs, DeviceState *qdev);

block.h +1 -0
@@ -96,6 +97,7 @@ void bdrv_commit_all(void);
 int bdrv_change_backing_file(BlockDriverState *bs,
 const char *backing_file, const char *backing_fmt);
 void bdrv_register(BlockDriver *bdrv);
+int bdrv_change_hostcache(BlockDriverState *bs, bool enable_host_cache);
 
 
 typedef struct BdrvCheckResult {

Index: qemu/blockdev.c
===================================================================

blockdev.c +60 -0
@@ -793,3 +793,63 @@ int do_block_resize(Monitor *mon, const
 
 return 0;
 }
+
+
+/*
+ * Handle changes to block device settings, like hostcache,
+ * while guest is running.
+*/
+int do_block_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+ BlockDriverState *bs = NULL;
+ QemuOpts *opts;
+ int enable;
+ const char *device, *driver;
+ int ret;
+
+ /* Validate device */
+ device = qdict_get_str(qdict, "device");
+ bs = bdrv_find(device);
+ if (!bs) {
+ qerror_report(QERR_DEVICE_NOT_FOUND, device);
+ return -1;
+ }
+
+ opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict);
+ if (opts == NULL) {
+ return -1;
+ }
+
+ /* If input not in "param=value" format, display error */
+ driver = qemu_opt_get(opts, "driver");
+ if (driver != NULL) {
+ error_report("Invalid parameter %s", driver);
+ return -1;
+ }
+
+ /* Before validating parameters, remove "device" option */
+ ret = qemu_opt_delete(opts, "device");
+ if (ret == 1) {
+ error_report("Specify parameter to be changed");
+ error_report("Usage: block_set device [prop=value][,...]");
+ return 0;
+ }
+
+ /* Validate parameters with "-drive" parameter list */
+ ret = qemu_validate_opts(opts, "drive");
+ if (ret == -1) {
+ return -1;
+ }
+
+ /* Check for 'hostcache' parameter */
+ enable = qemu_opt_get_bool(opts, "hostcache", -1);
+ if (enable != -1) {
+ return bdrv_change_hostcache(bs, enable);
+ } else {
+ error_report("Specify 'hostcache=on/off'");
+ }
+
+ return 0;
+
+}
+

Index: qemu/blockdev.h
===================================================================

blockdev.h +0 -0
@@ -65,5 +65,6 @@ int do_change_block(Monitor *mon, const
 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data);

int do_snapshot_blkdev(Monitor *mon, const QDict *qdict, QObject **ret_data);
int do_block_resize(Monitor *mon, const QDict *qdict, QObject **ret_data); +int do_block_set(Monitor *mon, const QDict *qdict, QObject **ret_data);

#endif
Index: qemu/hmp-commands.hx
===================================================================

hmp-commands.hx +0 -0
@@ -70,6 +70,20 @@ but should be used with extreme caution.

resizes image files, it can not resize block devices like LVM volumes. ETEXI

+ {
+ .name = "block_set",
+ .args_type = "device:B,device:O",
+ .params = "device [prop=value][,...]",
+ .help = "Change block device parameters [hostcache=on/off]",
+ .user_print = monitor_user_noop,
+ .mhandler.cmd_new = do_block_set,
+ },

hmp-commands.hx +4 -0
+STEXI
+@item block_set @var{config}
+@findex block_set
+Change block device parameters (eg: hostcache=on/off) while guest is

running.

hmp-commands.hx +2 -0
+ETEXI
+

{
.name = "eject",
Index: qemu/qemu-config.c
===================================================================

qemu-config.c +13 -0
@@ -506,6 +506,19 @@ QemuOptsList *qemu_find_opts(const char
 return find_list(vm_config_groups, group);
 }
 
+/* Validate given opts list with that of defined vm_group */
+int qemu_validate_opts(QemuOpts *opts, const char *group)
+{
+ QemuOptsList *vm_group;
+
+ vm_group = qemu_find_opts(group);
+ if (vm_group == NULL) {
+ return -1;
+ }
+
+ return qemu_opts_validate(opts, &vm_group->desc[0]);
+}
+
 void qemu_add_opts(QemuOptsList *list)
 {
 int entries, i;

Index: qemu/qemu-option.c
===================================================================

qemu-option.c +25 -0
@@ -599,6 +599,31 @@ static void qemu_opt_del(QemuOpt *opt)
 qemu_free(opt);
 }
 
+/*
+ * Delete specified parameter with name "name" from opts
+ * Return
+ * 0 - Deletion Successful
+ * -1 - Param doesn't exist in opts
+ * 1 - Deletion Successful and opts is empty.
+*/
+
+int qemu_opt_delete(QemuOpts *opts, const char *name)
+{
+ QemuOpt *opt = qemu_opt_find(opts, name);
+ if (opt == NULL) {
+ return -1;
+ }
+
+ qemu_opt_del(opt);
+
+ if ((&opts->head)->tqh_first == NULL) {
+ /* opt queue is empty */
+ return 1;
+ }
+
+ return 0;
+}
+
 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
 {
 QemuOpt *opt;

Index: qemu/qemu-option.h
===================================================================

qemu-option.h +1 -0
@@ -121,6 +121,7 @@ int qemu_opts_set(QemuOptsList *list, co
 const char *name, const char *value);
 const char *qemu_opts_id(QemuOpts *opts);
 void qemu_opts_del(QemuOpts *opts);
+int qemu_opt_delete(QemuOpts *opts, const char *name);
 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc);

int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname);
QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, int permit_abbrev);

qemu-option.h +0 -0
@@ -131,5 +132,6 @@ typedef int (*qemu_opts_loopfunc)(QemuOp
 int qemu_opts_print(QemuOpts *opts, void *dummy);

int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
int abort_on_failure);
+int qemu_validate_opts(QemuOpts *opts, const char *id);

#endif
Index: qemu/qmp-commands.hx
===================================================================

qmp-commands.hx +0 -0
@@ -693,7 +693,35 @@ Example:
 

EQMP

+
{
+ .name = "block_set",
+ .args_type = "device:B,device:O",
+ .params = "device [prop=value][,...]",
+ .help = "Change block device parameters [hostcache=on/off]",
+ .user_print = monitor_user_noop,
+ .mhandler.cmd_new = do_block_set,
+ },

qmp-commands.hx +15 -0
+
+SQMP
+block_set
+---------
+
+Change various block device parameters (eg: hostcache=on/off)
+
+Arguments:
+
+- "device": the device's ID, must be unique (json-string)
+- device parameters to be changed (eg: "hostcache": "off")
+
+Example:
+
+-> { "execute": "block_set", "arguments": { "device": "ide0-hd0",

"hostcache": "off"} }
+<- { "return": {} }

qmp-commands.hx +4 -0
+
+EQMP
+
+ {
Quoted 4 lines

.name = "balloon",
.args_type = "value:M",
.params = "target",

Please review..

#20 lore raw

Re: [Qemu-devel] [V4 Patch 4/4 - Updated]Qemu: Add commandline -drive option 'hostcache'

In reply to Supriya Kannery
Quoted 69 lines · Supriya Kannery

Updated patch to use qemu_opt_get_bool() instead of qemu_opt_get() to read 'hostcache'

-------------------------------------------------------------------
qemu command option 'hostcache' added to -drive for block devices. While starting a VM from qemu commandline, this option can be used for setting host cache usage for block data access. It is not allowed to specify both 'hostcache' and 'cache' options in the same commandline. User has to specify only one among these.

---
blockdev.c | 13 +++++++++++++
qemu-config.c | 4 ++++
qemu-options.hx | 2 +-

3 files changed, 18 insertions(+), 1 deletion(-)

Index: qemu/blockdev.c
===================================================================

blockdev.c +13 -0
@@ -238,6 +238,7 @@ DriveInfo *drive_init(QemuOpts *opts, in
 DriveInfo *dinfo;
 int snapshot = 0;
 int ret;
+ int hostcache = 0;
 
 translation = BIOS_ATA_TRANSLATION_AUTO;
 
@@ -324,7 +325,19 @@ DriveInfo *drive_init(QemuOpts *opts, in
 }
 }
 
+ if ((hostcache = qemu_opt_get_bool(opts, "hostcache", -1)) != -1) {
+ if (!hostcache) {
+ bdrv_flags |= BDRV_O_NOCACHE;
+ } else {
+ bdrv_flags &= ~BDRV_O_NOCACHE;
+ }
+ }
+
 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
+ if (hostcache != -1) {
+ error_report("'hostcache' and 'cache' cannot co-exist");
+ return NULL;
+ }
 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
 bdrv_flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
 } else if (!strcmp(buf, "writeback")) {

Index: qemu/qemu-options.hx
===================================================================

qemu-options.hx +0 -0
@@ -120,7 +120,7 @@ DEF("drive", HAS_ARG, QEMU_OPTION_drive,

" [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n" " [,cache=writethrough|writeback|none|unsafe][,format=f]\n" " [,serial=s][,addr=A][,id=name][,aio=threads|native]\n"
- " [,readonly=on|off]\n"
+ " [,readonly=on|off][,hostcache=on|off]\n"
" use 'file' as a drive image\n", QEMU_ARCH_ALL) STEXI
@item -drive @var{option}[,@var{option}[,@var{option}[,...]]]
Index: qemu/qemu-config.c
===================================================================

qemu-config.c static QemuOptsList qemu_drive_opts = { · line 87

Quoted 10 lines
qemu-config.c +4 -0
@@ -78,6 +78,10 @@ static QemuOptsList qemu_drive_opts = {
 },{
 .name = "readonly",
 .type = QEMU_OPT_BOOL,
+ },{
+ .name = "hostcache",
+ .type = QEMU_OPT_BOOL,
+ .help = "set or reset hostcache (on/off)"
 },
 { /* end of list */ }
 },

Please review..

#4 lore raw

Re: [Qemu-devel] [V4 Patch 1/4 -Updated]Qemu: Enhance "info block" to display host cache setting

In reply to Supriya Kannery
Quoted 80 lines · Supriya Kannery

Updated patch to display hostcache = 1/0 instead of true/false in monitor.

-------------------------------------------------------------------
Enhance "info block" to display hostcache setting for each block device.

Example:
(qemu) info block
ide0-hd0: type=hd removable=0 file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

Enhanced to display "hostcache" setting:
(qemu) info block
ide0-hd0: type=hd removable=0 hostcache=true file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0

---
block.c | 21 +++++++++++++++++---- qmp-commands.hx | 2 ++

2 files changed, 19 insertions(+), 4 deletions(-)

Index: qemu/block.c
===================================================================

block.c +8 -0
@@ -1694,6 +1694,14 @@ static void bdrv_print_dict(QObject *obj
 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
 }
 
+ if (qdict_haskey(bs_dict, "open_flags")) {
+ int open_flags = qdict_get_int(bs_dict, "open_flags");
+ if (open_flags & BDRV_O_NOCACHE)
+ monitor_printf(mon, " hostcache=0");
+ else
+ monitor_printf(mon, " hostcache=1");
+ }
+
 if (qdict_haskey(bs_dict, "inserted")) {
 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
 
@@ -1730,13 +1738,18 @@ void bdrv_info(Monitor *mon, QObject **r
 QObject *bs_obj;
 

bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
- "'removable': %i, 'locked': %i }",
- bs->device_name, bs->removable,
- bs->locked);
+ "'removable': %i, 'locked': %i, "
+ "'hostcache': %s }",
+ bs->device_name, bs->removable,
+ bs->locked,
+ (bs->open_flags & BDRV_O_NOCACHE) ?
+ "false" : "true");
+
+ QDict *bs_dict = qobject_to_qdict(bs_obj);
+ qdict_put(bs_dict, "open_flags", qint_from_int(bs->open_flags));

if (bs->drv) {
QObject *obj;
- QDict *bs_dict = qobject_to_qdict(bs_obj);

obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " "'encrypted': %i }",
Index: qemu/qmp-commands.hx
===================================================================

qmp-commands.hx +1 -4
@@ -1070,6 +1070,7 @@ Each json-object contain the following:
- Possible values: "unknown"
- "removable": true if the device is removable, false otherwise (json-bool)
- "locked": true if the device is locked, false otherwise (json-bool)
+- "hostcache": true if hostcache enabled, false otherwise (json-bool)
- "inserted": only present if the device is inserted, it is a json-object

containing the following:
- "file": device file name (json-string)

qmp-commands.hx Example: · line 1098

Quoted 7 lines
qmp-commands.hx +1 -0
@@ -1091,6 +1092,7 @@ Example:
 {
 "device":"ide0-hd0",
 "locked":false,
+ "hostcache":false,
 "removable":false,
 "inserted":{
 "ro":false,

Pls review..

#11 lore raw

Re: [Qemu-devel] [V4 Patch 3/4 - Updated]Qemu: Command "block_set" for dynamic block params change

In reply to Supriya Kannery
Quoted 17 lines · Supriya Kannery

Index: qemu/block.c
===================================================================

block.c +9 -0
@@ -651,6 +651,40 @@ unlink_and_fail:
     return ret;
 }
 
+int bdrv_reopen(BlockDriverState *bs, int bdrv_flags)
+{
+    BlockDriver *drv = bs->drv;
+    int ret = 0;
+
+    /* Quiesce IO for the given block device */
+    qemu_aio_flush();
+    if (bdrv_flush(bs)) {
+        qerror_report(QERR_DATA_SYNC_FAILED, bs->device_name);

block.c unlink_and_fail: · line 672

Quoted 10 lines
block.c +10 -0
+        return ret;
+    }
+    bdrv_close(bs);
+
+
+    ret = bdrv_open(bs, bs->filename, bdrv_flags, drv);
+    if (ret < 0) {
+        /* Reopen failed. Try to open with original flags */
+        error_report("Opening file with changed flags...");
+        qerror_report(QERR_REOPEN_FILE_FAILED, bs->filename);

If the next open fails too then there will be two qerror_report() calls. This causes a warning and the new qerror is dropped. Please consolidate the error reporting so there is only one error before returning from this function.

block.c unlink_and_fail: · line 674

Quoted 2 lines
block.c +2 -0
+
+        ret = bdrv_open(bs, bs->filename, bs->open_flags, drv);

bs->open_flags has been clobbered by the previous bdrv_open(). It would be best to take a copy of bs->open_flags before bdrv_close(bs) above.

Quoted 32 lines
block.c +25 -0
+        if (ret < 0) {
+            /*
+             * Reopen failed with orig and modified flags
+            */
+            error_report("Opening file with original flags...");
+            qerror_report(QERR_REOPEN_FILE_FAILED, bs->filename);
+            abort();
+        }
+    }
+
+    return ret;
+}
+
 void bdrv_close(BlockDriverState *bs)
 {
     if (bs->drv) {
@@ -691,6 +725,32 @@ void bdrv_close_all(void)
     }
 }
 
+int bdrv_change_hostcache(BlockDriverState *bs, bool enable_host_cache)
+{
+    int bdrv_flags = bs->open_flags;
+
+    /* set hostcache flags (without changing WCE/flush bits) */
+    if (enable_host_cache) {
+        bdrv_flags &= ~BDRV_O_NOCACHE;
+    } else {
+        bdrv_flags |= BDRV_O_NOCACHE;
+    }
+
+    /* If no change in flags, no need to reopen */

block.c void bdrv_close_all(void) · line 749

Quoted 10 lines
block.c +10 -0
+    if (bdrv_flags == bs->open_flags) {
+        return 0;
+    }
+
+    if (bdrv_is_inserted(bs)) {
+        /* Reopen file with changed set of flags */
+        return bdrv_reopen(bs, bdrv_flags);
+    } else {
+        /* Save hostcache change for future use */
+        bs->open_flags = bdrv_flags;

Can you explain the scenario where this works?

Looking at do_change_block() the flags will be clobbered so saving them away does not help.

Quoted 29 lines

Index: qemu/blockdev.c
===================================================================

blockdev.c +21 -0
@@ -793,3 +793,63 @@ int do_block_resize(Monitor *mon, const
 
     return 0;
 }
+
+
+/*
+ * Handle changes to block device settings, like hostcache,
+ * while guest is running.
+*/
+int do_block_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    BlockDriverState *bs = NULL;
+    QemuOpts *opts;
+    int enable;
+    const char *device, *driver;
+    int ret;
+
+    /* Validate device */
+    device = qdict_get_str(qdict, "device");
+    bs = bdrv_find(device);
+    if (!bs) {
+        qerror_report(QERR_DEVICE_NOT_FOUND, device);
+        return -1;
+    }

blockdev.c int do_block_resize(Monitor *mon, const · line 826

Quoted 10 lines
blockdev.c +10 -0
+
+    opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict);
+    if (opts == NULL) {
+        return -1;
+    }
+
+    /* If input not in "param=value" format, display error */
+    driver = qemu_opt_get(opts, "driver");
+    if (driver != NULL) {
+        error_report("Invalid parameter %s", driver);

error_report() only works for HMP. Please use qerror_report() so both HMP and QMP see the error. Same issue further down.

Stefan

#12 lore raw

Re: [Qemu-devel] [V4 Patch 3/4 - Updated]Qemu: Command "block_set" for dynamic block params change

Quoted 8 lines · Stefan Hajnoczi

block.c unlink_and_fail: · line 672

Quoted 5 lines · Supriya Kannery
block.c +5 -0
+    ret = bdrv_open(bs, bs->filename, bdrv_flags, drv);
+    if (ret<  0) {
+        /* Reopen failed. Try to open with original flags */
+        error_report("Opening file with changed flags...");
+        qerror_report(QERR_REOPEN_FILE_FAILED, bs->filename);
Quoted 5 lines

If the next open fails too then there will be two qerror_report() calls. This causes a warning and the new qerror is dropped. Please consolidate the error reporting so there is only one error before returning from this function.

ok

Quoted 8 lines

block.c void bdrv_close_all(void) · line 749

Quoted 6 lines
block.c +6 -0
+    if (bdrv_is_inserted(bs)) {
+        /* Reopen file with changed set of flags */
+        return bdrv_reopen(bs, bdrv_flags);
+    } else {
+        /* Save hostcache change for future use */
+        bs->open_flags = bdrv_flags;
Quoted 4 lines

Can you explain the scenario where this works?

Looking at do_change_block() the flags will be clobbered so saving them away does not help.

Intention here is to use the changed hostcache setting during device insertion and there after. To apply this to 'change' command (during insertion of a device), will need to make the following code changes in do_change_block.

+
+ bdrv_flags = bs->open_flags;

+    bdrv_flags |= bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
     bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
     if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
         qerror_report(QERR_OPEN_FILE_FAILED, filename);

Need to track bs->open_flags a bit more to see what other changes needed to ensure usage of changed hostcache setting until user initiates next hostcache change explicitly for that drive.

Please suggest... should we be saving the hostcache change for future use or just inhibit user from changing hostcache setting for an empty drive?

Quoted 8 lines

blockdev.c int do_block_resize(Monitor *mon, const · line 826

Quoted 4 lines
blockdev.c +4 -0
+    /* If input not in "param=value" format, display error */
+    driver = qemu_opt_get(opts, "driver");
+    if (driver != NULL) {
+        error_report("Invalid parameter %s", driver);

error_report() only works for HMP. Please use qerror_report() so both HMP and QMP see the error. Same issue further down.

ok..will change error_report to qerror_report.

Will make further code changes into patchset version 5

#13 lore raw

Re: [Qemu-devel] [V4 Patch 3/4 - Updated]Qemu: Command "block_set" for dynamic block params change

Quoted 28 lines · Supriya Kannery
Quoted 10 lines · Stefan Hajnoczi
Quoted 6 lines · Supriya Kannery

+    if (bdrv_is_inserted(bs)) {
+        /* Reopen file with changed set of flags */
+        return bdrv_reopen(bs, bdrv_flags);
+    } else {
+        /* Save hostcache change for future use */
+        bs->open_flags = bdrv_flags;

Quoted 4 lines

Can you explain the scenario where this works?

Looking at do_change_block() the flags will be clobbered so saving them away does not help.

Intention here is to use the changed hostcache setting during device insertion and there after. To apply this to 'change' command (during insertion of a device), will need to make the following code changes in do_change_block.

+
+    bdrv_flags = bs->open_flags;
+    bdrv_flags |= bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) { qerror_report(QERR_OPEN_FILE_FAILED, filename);

Quoted 7 lines

Need to track bs->open_flags a bit more to see what other changes needed to ensure usage of changed hostcache setting until user initiates next hostcache change explicitly for that drive.

Please suggest... should we be saving the hostcache change for future use or just inhibit user from changing hostcache setting for an empty drive?

The 'change' command does not support any cache= options today. It always opens the new image with cache=writethrough semantics.

This seems like a bug in 'change' to me. We should preserve cache= settings across change or at least provide a way to specify them as arguments.

Perhaps your existing code is fine. When 'change' is fixed or replaced then 'block_set' will work across 'change' too.

Kevin: Thoughts?

Stefan

#14 lore raw

Re: [Qemu-devel] [V4 Patch 3/4 - Updated]Qemu: Command "block_set" for dynamic block params change

Am 25.07.2011 14:50, schrieb Stefan Hajnoczi:

Quoted 40 lines · Supriya Kannery
Quoted 28 lines · Stefan Hajnoczi
Quoted 10 lines · Supriya Kannery

block.c void bdrv_close_all(void) · line 749

Quoted 6 lines
block.c +6 -0
+    if (bdrv_is_inserted(bs)) {
+        /* Reopen file with changed set of flags */
+        return bdrv_reopen(bs, bdrv_flags);
+    } else {
+        /* Save hostcache change for future use */
+        bs->open_flags = bdrv_flags;
Quoted 4 lines

Can you explain the scenario where this works?

Looking at do_change_block() the flags will be clobbered so saving them away does not help.

Intention here is to use the changed hostcache setting during device insertion and there after. To apply this to 'change' command (during insertion of a device), will need to make the following code changes in do_change_block.

(quoted diff) +3 -0
+
+    bdrv_flags = bs->open_flags;
+    bdrv_flags |= bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
    bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
    if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) {
        qerror_report(QERR_OPEN_FILE_FAILED, filename);
Quoted 7 lines

Need to track bs->open_flags a bit more to see what other changes needed to ensure usage of changed hostcache setting until user initiates next hostcache change explicitly for that drive.

Please suggest... should we be saving the hostcache change for future use or just inhibit user from changing hostcache setting for an empty drive?

The 'change' command does not support any cache= options today. It always opens the new image with cache=writethrough semantics.

Quoted 8 lines

This seems like a bug in 'change' to me. We should preserve cache= settings across change or at least provide a way to specify them as arguments.

Perhaps your existing code is fine. When 'change' is fixed or replaced then 'block_set' will work across 'change' too.

Kevin: Thoughts?

I'm not sure if I would say it's a bug. Probably preserving would be more useful in most cases, but using the default cache mode doesn't appear completely wrong either. If you like to change it, I'm not opposed to it.

Kevin

#15 lore raw

Re: [Qemu-devel] [V4 Patch 3/4 - Updated]Qemu: Command "block_set" for dynamic block params change

Quoted 43 lines · Kevin Wolf

Am 25.07.2011 14:50, schrieb Stefan Hajnoczi:

Quoted 42 lines · Supriya Kannery
Quoted 24 lines · Stefan Hajnoczi
Quoted 11 lines · Supriya Kannery

block.c void bdrv_close_all(void) · line 749

Quoted 6 lines
block.c +6 -0
+    if (bdrv_is_inserted(bs)) {
+        /* Reopen file with changed set of flags */
+        return bdrv_reopen(bs, bdrv_flags);
+    } else {
+        /* Save hostcache change for future use */
+        bs->open_flags = bdrv_flags;

Can you explain the scenario where this works?

Looking at do_change_block() the flags will be clobbered so saving them away does not help.

Intention here is to use the changed hostcache setting during device insertion and there after. To apply this to 'change' command (during insertion of a device), will need to make the following code changes in do_change_block.

(quoted diff) +3 -0
+
+    bdrv_flags = bs->open_flags;
+    bdrv_flags |= bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR;
     bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0;
     if (bdrv_open(bs, filename, bdrv_flags, drv)<  0) {
         qerror_report(QERR_OPEN_FILE_FAILED, filename);
Quoted 7 lines

Need to track bs->open_flags a bit more to see what other changes needed to ensure usage of changed hostcache setting until user initiates next hostcache change explicitly for that drive.

Please suggest... should we be saving the hostcache change for future use or just inhibit user from changing hostcache setting for an empty drive?

The 'change' command does not support any cache= options today. It always opens the new image with cache=writethrough semantics.

This seems like a bug in 'change' to me. We should preserve cache= settings across change or at least provide a way to specify them as arguments.

Perhaps your existing code is fine. When 'change' is fixed or replaced then 'block_set' will work across 'change' too.

Quoted 9 lines
Quoted 1 line

Kevin: Thoughts?

I'm not sure if I would say it's a bug. Probably preserving would be more useful in most cases, but using the default cache mode doesn't appear completely wrong either. If you like to change it, I'm not opposed to it.

Kevin

ok, so for now, will retain the code that saves hostcache setting for empty drives. When other commands (like 'change') are focused, can look at using this saved setting.

Will post V5 with other error_report related code changes.

thanks, Supriya