Qemu: Set host pagecache from cmdline and monitor
PATCH v6 1/4 Qemu: Enhance "info block" to display host cache setting
In reply to Supriya KanneryEnhance "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=1 file=../rhel6-32.qcow2 ro=0 drv=qcow2 encrypted=0
--- block.c | 20 ++++++++++++++++---- qmp-commands.hx | 2 ++ 2 files changed, 18 insertions(+), 4 deletions(-)
block.c +16 -4
@@ -1812,6 +1812,14 @@ static void bdrv_print_dict(QObject *objmonitor_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" ));
@@ -1848,13 +1856,17 @@ void bdrv_info(Monitor *mon, QObject **rQObject *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': %i }" , + bs->device_name, bs->removable, + bs->locked, + !(bs->open_flags &BDRV_O_NOCACHE )); + +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
@@ -1131,6 +1131,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 host pagecache 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)
@@ -1152,6 +1153,7 @@ Example: { "device":"ide0-hd0", "locked":false, + "hostcache":false, "removable":false, "inserted":{ "ro":false,
PATCH v6 2/4 Qemu: qerrors for file reopen and data sync
In reply to Supriya KanneryNew error classes defined for file reopen failure and data sync error
--- qerror.c | 8 ++++++++ qerror.h | 6 ++++++ 2 files changed, 14 insertions(+)
qerror.c +8 -0
@@ -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
@@ -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 } }"
@@ -142,6 +145,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 } }"
PATCH v6 3/4 Qemu: Command "block_set_hostcache" for dynamic hostcache change
In reply to Supriya KanneryNew command "block_set_hostcache" added for dynamically changing host pagecache setting of a block device.
--- block.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ block.h | 2 ++ blockdev.c | 26 ++++++++++++++++++++++++++ blockdev.h | 2 ++ hmp-commands.hx | 14 ++++++++++++++ qmp-commands.hx | 27 +++++++++++++++++++++++++++ 6 files changed, 125 insertions(+)
block.c +54 -0
@@ -676,6 +676,34 @@ unlink_and_fail:return ret; } +int bdrv_reopen (BlockDriverState *bs,int bdrv_flags) +{ +BlockDriver *drv = bs->drv; +int ret =0 , open_flags; + +/* 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; + } + open_flags = bs->open_flags; +bdrv_close (bs); + + ret =bdrv_open (bs, bs->filename, bdrv_flags, drv); +if (ret <0 ) { +/* Reopen failed. Try to open with original flags */ +qerror_report (QERR_REOPEN_FILE_FAILED , bs->filename); + ret =bdrv_open (bs, bs->filename, open_flags, drv); +if (ret <0 ) { +/* Reopen failed with orig and modified flags */ +abort (); + } + } + +return ret; +} +void bdrv_close (BlockDriverState *bs) {if (bs->drv) {
@@ -716,6 +744,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
@@ -72,6 +72,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);
@@ -100,6 +101,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
@@ -790,3 +790,29 @@ int do_block_resize(Monitor *mon, constreturn 0 ; } + + +/* +* Change host page cache setting while guest is running. +*/ +int do_block_set_hostcache (Monitor *mon,const QDict *qdict, +QObject **ret_data) +{ +BlockDriverState *bs =NULL ; +int enable; +const char *device; + +/* Validate device */ + device =qdict_get_str (qdict,"device" ); + bs =bdrv_find (device); +if (!bs) { +qerror_report (QERR_DEVICE_NOT_FOUND , device); +return -1 ; + } + +/* Read hostcache setting */ + enable =qdict_get_bool (qdict,"option" ); +return bdrv_change_hostcache (bs, enable); + +} +
blockdev.h +2 -0
@@ -65,5 +65,7 @@ int do_change_block(Monitor *mon, constint 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_hostcache (Monitor *mon,const QDict *qdict, +QObject **ret_data);#endif
hmp-commands.hx +14 -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_hostcache", + .args_type = "device:B,option:b", + .params = "device on|off", + .help = "Change setting of host pagecache", + .user_print = monitor_user_noop, + .mhandler.cmd_new = do_block_set_hostcache, + }, +STEXI +@item block_set_hostcache @var{device} @var{setting} +@findex block_set_hostcache +Change host pagecache setting of a block device while guest is running. +ETEXI + { .name = "eject",
qmp-commands.hx +27 -0
@@ -727,7 +727,34 @@ Example: EQMP + { + .name = "block_set_hostcache", + .args_type = "device:B,option:b", + .params = "device on|off", + .help = "Change setting of host pagecache (true|false)", + .user_print = monitor_user_noop, + .mhandler.cmd_new = do_block_set_hostcache, + }, + +SQMP +block_set_hostcache +------------------- + +Change host pagecache setting of a block device (on|off) + +Arguments: + +- "device": the device's ID, must be unique (json-string) +- "option": hostcache setting (json-bool) + +Example: +-> { "execute": "block_set_hostcache", "arguments": { "device": "ide0-hd0", "option": false } } +<- { "return": {} } + +EQMP + + { .name = "balloon", .args_type = "value:M", .params = "target",
PATCH v6 4/4 Qemu: Add commandline -drive option 'hostcache'
In reply to Supriya Kanneryqemu 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.
--- blockdev.c | 13 +++++++++++++ qemu-config.c | 4 ++++ qemu-options.hx | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-)
blockdev.c +13 -0
@@ -238,6 +238,7 @@ DriveInfo *drive_init(QemuOpts *opts, inDriveInfo *dinfo;int snapshot =0 ;int ret; +int hostcache =0 ; translation =BIOS_ATA_TRANSLATION_AUTO ; media =MEDIA_DISK ;
@@ -320,7 +321,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
@@ -135,7 +135,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
@@ -84,6 +84,10 @@ static QemuOptsList qemu_drive_opts = { .name = "readonly", .type = QEMU_OPT_BOOL, .help = "open drive file as read-only", + },{ + .name = "hostcache", + .type = QEMU_OPT_BOOL, + .help = "set or reset hostcache (on/off)" }, {/* end of list */ } },
v6 cover Qemu: Set host pagecache 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_hostcache'
using which host pagecache setting for a block device can be changed dynamically. This patchset also introduces 'hostcache', a new option for setting host cache from qemu command line.
Note: 'Hostcache and 'cache' options cannot be used simultaneously from commandline.
v6:
1. "block_set_hostcache" to replace "block_set" command
v5:
1. Defined qerror class for incorrect command syntax.
2. Changed error_report() calls to qerror_report()
v4:
Added 'hostcache' option to '-drive' commandline option.
v3:
1. Command "block_set" for changing various block params
2. Enhanced info-block to display hostcache setting
3. Added qmp interfaces for setting and querying hostcache
v2:
1. Support of dynamic cache change only for hostcache.
2. Monitor command "hostcache_get" added to display cache setting
3. Backed off the changes for display of cache setting in "info block"
v1:
Dynamic cache change through monitor
New block command added:
"block_set_hostcache"
-- Sets hostcache parameter for block device while guest is running.
Usage:
block_set_hostcache <device> <option>
<device> = block device
<option> = 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 qerrors for file reopen and data sync
3/4 Command "block_set_hostcache" for changing hostcache setting 4/4 'hostcache' option added to -drive in qemu commandline
qemu/block.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ qemu/block.h | 2 + qemu/blockdev.c | 39 ++++++++++++++++++++++++ qemu/blockdev.h | 2 + qemu/hmp-commands.hx | 14 +++++++++++++ qemu/qemu-config.c | 4 +++ qemu/qemu-options.hx | 2 - qemu/qerror.c | 8 +++++++ qemu/qerror.h | 6 +++++ qemu/qmp-commands.hx | 29 +++++++++++++++++++++++++ 10 files changed, 176 insertions(+), 5 deletions(-)
~