stream: Allow users to request only format driver names in backing file format
[qemu/ar7.git] / block / monitor / block-hmp-cmds.c
blobd954bec6f1eb80ea08c8ac3c9e0e8d5b29f7afe5
1 /*
2 * Blockdev HMP commands
4 * Authors:
5 * Anthony Liguori <aliguori@us.ibm.com>
7 * Copyright (c) 2003-2008 Fabrice Bellard
9 * This work is licensed under the terms of the GNU GPL, version 2.
10 * See the COPYING file in the top-level directory.
11 * Contributions after 2012-01-13 are licensed under the terms of the
12 * GNU GPL, version 2 or (at your option) any later version.
14 * This file incorporates work covered by the following copyright and
15 * permission notice:
17 * Copyright (c) 2003-2008 Fabrice Bellard
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this software and associated documentation files (the "Software"), to deal
21 * in the Software without restriction, including without limitation the rights
22 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23 * copies of the Software, and to permit persons to whom the Software is
24 * furnished to do so, subject to the following conditions:
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35 * THE SOFTWARE.
38 #include "qemu/osdep.h"
39 #include "hw/boards.h"
40 #include "sysemu/block-backend.h"
41 #include "sysemu/blockdev.h"
42 #include "qapi/qapi-commands-block.h"
43 #include "qapi/qapi-commands-block-export.h"
44 #include "qapi/qmp/qdict.h"
45 #include "qapi/error.h"
46 #include "qapi/qmp/qerror.h"
47 #include "qemu/config-file.h"
48 #include "qemu/option.h"
49 #include "qemu/sockets.h"
50 #include "qemu/cutils.h"
51 #include "qemu/error-report.h"
52 #include "sysemu/sysemu.h"
53 #include "monitor/monitor.h"
54 #include "monitor/hmp.h"
55 #include "block/nbd.h"
56 #include "block/qapi.h"
57 #include "block/block_int.h"
58 #include "block/block-hmp-cmds.h"
59 #include "qemu-io.h"
61 static void hmp_drive_add_node(Monitor *mon, const char *optstr)
63 QemuOpts *opts;
64 QDict *qdict;
65 Error *local_err = NULL;
67 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
68 if (!opts) {
69 return;
72 qdict = qemu_opts_to_qdict(opts, NULL);
74 if (!qdict_get_try_str(qdict, "node-name")) {
75 qobject_unref(qdict);
76 error_report("'node-name' needs to be specified");
77 goto out;
80 BlockDriverState *bs = bds_tree_init(qdict, &local_err);
81 if (!bs) {
82 error_report_err(local_err);
83 goto out;
86 bdrv_set_monitor_owned(bs);
87 out:
88 qemu_opts_del(opts);
91 void hmp_drive_add(Monitor *mon, const QDict *qdict)
93 Error *err = NULL;
94 DriveInfo *dinfo;
95 QemuOpts *opts;
96 MachineClass *mc;
97 const char *optstr = qdict_get_str(qdict, "opts");
98 bool node = qdict_get_try_bool(qdict, "node", false);
100 if (node) {
101 hmp_drive_add_node(mon, optstr);
102 return;
105 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
106 if (!opts)
107 return;
109 mc = MACHINE_GET_CLASS(current_machine);
110 dinfo = drive_new(opts, mc->block_default_type, &err);
111 if (err) {
112 error_report_err(err);
113 qemu_opts_del(opts);
114 goto err;
117 if (!dinfo) {
118 return;
121 switch (dinfo->type) {
122 case IF_NONE:
123 monitor_printf(mon, "OK\n");
124 break;
125 default:
126 monitor_printf(mon, "Can't hot-add drive to type %d\n", dinfo->type);
127 goto err;
129 return;
131 err:
132 if (dinfo) {
133 BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
134 monitor_remove_blk(blk);
135 blk_unref(blk);
139 void hmp_drive_del(Monitor *mon, const QDict *qdict)
141 const char *id = qdict_get_str(qdict, "id");
142 BlockBackend *blk;
143 BlockDriverState *bs;
144 Error *local_err = NULL;
146 GLOBAL_STATE_CODE();
147 GRAPH_RDLOCK_GUARD_MAINLOOP();
149 bs = bdrv_find_node(id);
150 if (bs) {
151 qmp_blockdev_del(id, &local_err);
152 if (local_err) {
153 error_report_err(local_err);
155 return;
158 blk = blk_by_name(id);
159 if (!blk) {
160 error_report("Device '%s' not found", id);
161 return;
164 if (!blk_legacy_dinfo(blk)) {
165 error_report("Deleting device added with blockdev-add"
166 " is not supported");
167 return;
170 bs = blk_bs(blk);
171 if (bs) {
172 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) {
173 error_report_err(local_err);
174 return;
177 blk_remove_bs(blk);
180 /* Make the BlockBackend and the attached BlockDriverState anonymous */
181 monitor_remove_blk(blk);
184 * If this BlockBackend has a device attached to it, its refcount will be
185 * decremented when the device is removed; otherwise we have to do so here.
187 if (blk_get_attached_dev(blk)) {
188 /* Further I/O must not pause the guest */
189 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
190 BLOCKDEV_ON_ERROR_REPORT);
191 } else {
192 blk_unref(blk);
196 void hmp_commit(Monitor *mon, const QDict *qdict)
198 const char *device = qdict_get_str(qdict, "device");
199 BlockBackend *blk;
200 int ret;
202 GLOBAL_STATE_CODE();
203 GRAPH_RDLOCK_GUARD_MAINLOOP();
205 if (!strcmp(device, "all")) {
206 ret = blk_commit_all();
207 } else {
208 BlockDriverState *bs;
210 blk = blk_by_name(device);
211 if (!blk) {
212 error_report("Device '%s' not found", device);
213 return;
216 bs = bdrv_skip_implicit_filters(blk_bs(blk));
218 if (!blk_is_available(blk)) {
219 error_report("Device '%s' has no medium", device);
220 return;
223 ret = bdrv_commit(bs);
225 if (ret < 0) {
226 error_report("'commit' error for '%s': %s", device, strerror(-ret));
230 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
232 const char *filename = qdict_get_str(qdict, "target");
233 const char *format = qdict_get_try_str(qdict, "format");
234 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
235 bool full = qdict_get_try_bool(qdict, "full", false);
236 Error *err = NULL;
237 DriveMirror mirror = {
238 .device = (char *)qdict_get_str(qdict, "device"),
239 .target = (char *)filename,
240 .format = (char *)format,
241 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
242 .has_mode = true,
243 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
244 .unmap = true,
247 if (!filename) {
248 error_setg(&err, QERR_MISSING_PARAMETER, "target");
249 goto end;
251 qmp_drive_mirror(&mirror, &err);
252 end:
253 hmp_handle_error(mon, err);
256 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
258 const char *device = qdict_get_str(qdict, "device");
259 const char *filename = qdict_get_str(qdict, "target");
260 const char *format = qdict_get_try_str(qdict, "format");
261 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
262 bool full = qdict_get_try_bool(qdict, "full", false);
263 bool compress = qdict_get_try_bool(qdict, "compress", false);
264 Error *err = NULL;
265 DriveBackup backup = {
266 .device = (char *)device,
267 .target = (char *)filename,
268 .format = (char *)format,
269 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
270 .has_mode = true,
271 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
272 .has_compress = !!compress,
273 .compress = compress,
276 if (!filename) {
277 error_setg(&err, QERR_MISSING_PARAMETER, "target");
278 goto end;
281 qmp_drive_backup(&backup, &err);
282 end:
283 hmp_handle_error(mon, err);
286 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
288 Error *error = NULL;
289 const char *device = qdict_get_str(qdict, "device");
290 int64_t value = qdict_get_int(qdict, "speed");
292 qmp_block_job_set_speed(device, value, &error);
294 hmp_handle_error(mon, error);
297 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
299 Error *error = NULL;
300 const char *device = qdict_get_str(qdict, "device");
301 bool force = qdict_get_try_bool(qdict, "force", false);
303 qmp_block_job_cancel(device, true, force, &error);
305 hmp_handle_error(mon, error);
308 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
310 Error *error = NULL;
311 const char *device = qdict_get_str(qdict, "device");
313 qmp_block_job_pause(device, &error);
315 hmp_handle_error(mon, error);
318 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
320 Error *error = NULL;
321 const char *device = qdict_get_str(qdict, "device");
323 qmp_block_job_resume(device, &error);
325 hmp_handle_error(mon, error);
328 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
330 Error *error = NULL;
331 const char *device = qdict_get_str(qdict, "device");
333 qmp_block_job_complete(device, &error);
335 hmp_handle_error(mon, error);
338 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
340 const char *device = qdict_get_str(qdict, "device");
341 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
342 const char *format = qdict_get_try_str(qdict, "format");
343 bool reuse = qdict_get_try_bool(qdict, "reuse", false);
344 enum NewImageMode mode;
345 Error *err = NULL;
347 if (!filename) {
349 * In the future, if 'snapshot-file' is not specified, the snapshot
350 * will be taken internally. Today it's actually required.
352 error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
353 goto end;
356 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
357 qmp_blockdev_snapshot_sync(device, NULL, filename, NULL, format,
358 true, mode, &err);
359 end:
360 hmp_handle_error(mon, err);
363 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
365 const char *device = qdict_get_str(qdict, "device");
366 const char *name = qdict_get_str(qdict, "name");
367 Error *err = NULL;
369 qmp_blockdev_snapshot_internal_sync(device, name, &err);
370 hmp_handle_error(mon, err);
373 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
375 const char *device = qdict_get_str(qdict, "device");
376 const char *name = qdict_get_str(qdict, "name");
377 const char *id = qdict_get_try_str(qdict, "id");
378 Error *err = NULL;
380 qmp_blockdev_snapshot_delete_internal_sync(device, id, name, &err);
381 hmp_handle_error(mon, err);
384 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
386 const char *uri = qdict_get_str(qdict, "uri");
387 bool writable = qdict_get_try_bool(qdict, "writable", false);
388 bool all = qdict_get_try_bool(qdict, "all", false);
389 Error *local_err = NULL;
390 BlockInfoList *block_list, *info;
391 SocketAddress *addr;
392 NbdServerAddOptions export;
394 if (writable && !all) {
395 error_setg(&local_err, "-w only valid together with -a");
396 goto exit;
399 /* First check if the address is valid and start the server. */
400 addr = socket_parse(uri, &local_err);
401 if (local_err != NULL) {
402 goto exit;
405 nbd_server_start(addr, NULL, NULL, 0, &local_err);
406 qapi_free_SocketAddress(addr);
407 if (local_err != NULL) {
408 goto exit;
411 if (!all) {
412 return;
415 /* Then try adding all block devices. If one fails, close all and
416 * exit.
418 block_list = qmp_query_block(NULL);
420 for (info = block_list; info; info = info->next) {
421 if (!info->value->inserted) {
422 continue;
425 export = (NbdServerAddOptions) {
426 .device = info->value->device,
427 .has_writable = true,
428 .writable = writable,
431 qmp_nbd_server_add(&export, &local_err);
433 if (local_err != NULL) {
434 qmp_nbd_server_stop(NULL);
435 break;
439 qapi_free_BlockInfoList(block_list);
441 exit:
442 hmp_handle_error(mon, local_err);
445 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
447 const char *device = qdict_get_str(qdict, "device");
448 const char *name = qdict_get_try_str(qdict, "name");
449 bool writable = qdict_get_try_bool(qdict, "writable", false);
450 Error *local_err = NULL;
452 NbdServerAddOptions export = {
453 .device = (char *) device,
454 .name = (char *) name,
455 .has_writable = true,
456 .writable = writable,
459 qmp_nbd_server_add(&export, &local_err);
460 hmp_handle_error(mon, local_err);
463 void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict)
465 const char *name = qdict_get_str(qdict, "name");
466 bool force = qdict_get_try_bool(qdict, "force", false);
467 Error *err = NULL;
469 /* Rely on BLOCK_EXPORT_REMOVE_MODE_SAFE being the default */
470 qmp_nbd_server_remove(name, force, BLOCK_EXPORT_REMOVE_MODE_HARD, &err);
471 hmp_handle_error(mon, err);
474 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
476 Error *err = NULL;
478 qmp_nbd_server_stop(&err);
479 hmp_handle_error(mon, err);
482 void coroutine_fn hmp_block_resize(Monitor *mon, const QDict *qdict)
484 const char *device = qdict_get_str(qdict, "device");
485 int64_t size = qdict_get_int(qdict, "size");
486 Error *err = NULL;
488 qmp_block_resize(device, NULL, size, &err);
489 hmp_handle_error(mon, err);
492 void hmp_block_stream(Monitor *mon, const QDict *qdict)
494 Error *error = NULL;
495 const char *device = qdict_get_str(qdict, "device");
496 const char *base = qdict_get_try_str(qdict, "base");
497 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
499 qmp_block_stream(device, device, base, NULL, NULL, false, false, NULL,
500 qdict_haskey(qdict, "speed"), speed,
501 true, BLOCKDEV_ON_ERROR_REPORT, NULL,
502 false, false, false, false, &error);
504 hmp_handle_error(mon, error);
507 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
509 Error *err = NULL;
510 char *device = (char *) qdict_get_str(qdict, "device");
511 BlockIOThrottle throttle = {
512 .bps = qdict_get_int(qdict, "bps"),
513 .bps_rd = qdict_get_int(qdict, "bps_rd"),
514 .bps_wr = qdict_get_int(qdict, "bps_wr"),
515 .iops = qdict_get_int(qdict, "iops"),
516 .iops_rd = qdict_get_int(qdict, "iops_rd"),
517 .iops_wr = qdict_get_int(qdict, "iops_wr"),
521 * qmp_block_set_io_throttle has separate parameters for the
522 * (deprecated) block device name and the qdev ID but the HMP
523 * version has only one, so we must decide which one to pass.
525 if (blk_by_name(device)) {
526 throttle.device = device;
527 } else {
528 throttle.id = device;
531 qmp_block_set_io_throttle(&throttle, &err);
532 hmp_handle_error(mon, err);
535 void hmp_eject(Monitor *mon, const QDict *qdict)
537 bool force = qdict_get_try_bool(qdict, "force", false);
538 const char *device = qdict_get_str(qdict, "device");
539 Error *err = NULL;
541 qmp_eject(device, NULL, true, force, &err);
542 hmp_handle_error(mon, err);
545 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
547 BlockBackend *blk = NULL;
548 BlockDriverState *bs = NULL;
549 BlockBackend *local_blk = NULL;
550 bool qdev = qdict_get_try_bool(qdict, "qdev", false);
551 const char *device = qdict_get_str(qdict, "device");
552 const char *command = qdict_get_str(qdict, "command");
553 Error *err = NULL;
554 int ret;
556 if (qdev) {
557 blk = blk_by_qdev_id(device, &err);
558 if (!blk) {
559 goto fail;
561 } else {
562 blk = blk_by_name(device);
563 if (!blk) {
564 bs = bdrv_lookup_bs(NULL, device, &err);
565 if (!bs) {
566 goto fail;
571 if (bs) {
572 blk = local_blk = blk_new(bdrv_get_aio_context(bs), 0, BLK_PERM_ALL);
573 ret = blk_insert_bs(blk, bs, &err);
574 if (ret < 0) {
575 goto fail;
580 * Notably absent: Proper permission management. This is sad, but it seems
581 * almost impossible to achieve without changing the semantics and thereby
582 * limiting the use cases of the qemu-io HMP command.
584 * In an ideal world we would unconditionally create a new BlockBackend for
585 * qemuio_command(), but we have commands like 'reopen' and want them to
586 * take effect on the exact BlockBackend whose name the user passed instead
587 * of just on a temporary copy of it.
589 * Another problem is that deleting the temporary BlockBackend involves
590 * draining all requests on it first, but some qemu-iotests cases want to
591 * issue multiple aio_read/write requests and expect them to complete in
592 * the background while the monitor has already returned.
594 * This is also what prevents us from saving the original permissions and
595 * restoring them later: We can't revoke permissions until all requests
596 * have completed, and we don't know when that is nor can we really let
597 * anything else run before we have revoken them to avoid race conditions.
599 * What happens now is that command() in qemu-io-cmds.c can extend the
600 * permissions if necessary for the qemu-io command. And they simply stay
601 * extended, possibly resulting in a read-only guest device keeping write
602 * permissions. Ugly, but it appears to be the lesser evil.
604 qemuio_command(blk, command);
606 fail:
607 blk_unref(local_blk);
608 hmp_handle_error(mon, err);
611 static void print_block_info(Monitor *mon, BlockInfo *info,
612 BlockDeviceInfo *inserted, bool verbose)
614 ImageInfo *image_info;
616 assert(!info || !info->inserted || info->inserted == inserted);
618 if (info && *info->device) {
619 monitor_puts(mon, info->device);
620 if (inserted && inserted->node_name) {
621 monitor_printf(mon, " (%s)", inserted->node_name);
623 } else {
624 assert(info || inserted);
625 monitor_puts(mon,
626 inserted && inserted->node_name ? inserted->node_name
627 : info && info->qdev ? info->qdev
628 : "<anonymous>");
631 if (inserted) {
632 monitor_printf(mon, ": %s (%s%s%s)\n",
633 inserted->file,
634 inserted->drv,
635 inserted->ro ? ", read-only" : "",
636 inserted->encrypted ? ", encrypted" : "");
637 } else {
638 monitor_printf(mon, ": [not inserted]\n");
641 if (info) {
642 if (info->qdev) {
643 monitor_printf(mon, " Attached to: %s\n", info->qdev);
645 if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
646 monitor_printf(mon, " I/O status: %s\n",
647 BlockDeviceIoStatus_str(info->io_status));
650 if (info->removable) {
651 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
652 info->locked ? "" : "not ",
653 info->tray_open ? "open" : "closed");
658 if (!inserted) {
659 return;
662 monitor_printf(mon, " Cache mode: %s%s%s\n",
663 inserted->cache->writeback ? "writeback" : "writethrough",
664 inserted->cache->direct ? ", direct" : "",
665 inserted->cache->no_flush ? ", ignore flushes" : "");
667 if (inserted->backing_file) {
668 monitor_printf(mon,
669 " Backing file: %s "
670 "(chain depth: %" PRId64 ")\n",
671 inserted->backing_file,
672 inserted->backing_file_depth);
675 if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
676 monitor_printf(mon, " Detect zeroes: %s\n",
677 BlockdevDetectZeroesOptions_str(inserted->detect_zeroes));
680 if (inserted->bps || inserted->bps_rd || inserted->bps_wr ||
681 inserted->iops || inserted->iops_rd || inserted->iops_wr)
683 monitor_printf(mon, " I/O throttling: bps=%" PRId64
684 " bps_rd=%" PRId64 " bps_wr=%" PRId64
685 " bps_max=%" PRId64
686 " bps_rd_max=%" PRId64
687 " bps_wr_max=%" PRId64
688 " iops=%" PRId64 " iops_rd=%" PRId64
689 " iops_wr=%" PRId64
690 " iops_max=%" PRId64
691 " iops_rd_max=%" PRId64
692 " iops_wr_max=%" PRId64
693 " iops_size=%" PRId64
694 " group=%s\n",
695 inserted->bps,
696 inserted->bps_rd,
697 inserted->bps_wr,
698 inserted->bps_max,
699 inserted->bps_rd_max,
700 inserted->bps_wr_max,
701 inserted->iops,
702 inserted->iops_rd,
703 inserted->iops_wr,
704 inserted->iops_max,
705 inserted->iops_rd_max,
706 inserted->iops_wr_max,
707 inserted->iops_size,
708 inserted->group);
711 if (verbose) {
712 monitor_printf(mon, "\nImages:\n");
713 image_info = inserted->image;
714 while (1) {
715 bdrv_node_info_dump(qapi_ImageInfo_base(image_info), 0, false);
716 if (image_info->backing_image) {
717 image_info = image_info->backing_image;
718 } else {
719 break;
725 void hmp_info_block(Monitor *mon, const QDict *qdict)
727 BlockInfoList *block_list, *info;
728 BlockDeviceInfoList *blockdev_list, *blockdev;
729 const char *device = qdict_get_try_str(qdict, "device");
730 bool verbose = qdict_get_try_bool(qdict, "verbose", false);
731 bool nodes = qdict_get_try_bool(qdict, "nodes", false);
732 bool printed = false;
734 /* Print BlockBackend information */
735 if (!nodes) {
736 block_list = qmp_query_block(NULL);
737 } else {
738 block_list = NULL;
741 for (info = block_list; info; info = info->next) {
742 if (device && strcmp(device, info->value->device)) {
743 continue;
746 if (info != block_list) {
747 monitor_printf(mon, "\n");
750 print_block_info(mon, info->value, info->value->inserted,
751 verbose);
752 printed = true;
755 qapi_free_BlockInfoList(block_list);
757 if ((!device && !nodes) || printed) {
758 return;
761 /* Print node information */
762 blockdev_list = qmp_query_named_block_nodes(false, false, NULL);
763 for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
764 assert(blockdev->value->node_name);
765 if (device && strcmp(device, blockdev->value->node_name)) {
766 continue;
769 if (blockdev != blockdev_list) {
770 monitor_printf(mon, "\n");
773 print_block_info(mon, NULL, blockdev->value, verbose);
775 qapi_free_BlockDeviceInfoList(blockdev_list);
778 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
780 BlockStatsList *stats_list, *stats;
782 stats_list = qmp_query_blockstats(false, false, NULL);
784 for (stats = stats_list; stats; stats = stats->next) {
785 if (!stats->value->device) {
786 continue;
789 monitor_printf(mon, "%s:", stats->value->device);
790 monitor_printf(mon, " rd_bytes=%" PRId64
791 " wr_bytes=%" PRId64
792 " rd_operations=%" PRId64
793 " wr_operations=%" PRId64
794 " flush_operations=%" PRId64
795 " wr_total_time_ns=%" PRId64
796 " rd_total_time_ns=%" PRId64
797 " flush_total_time_ns=%" PRId64
798 " rd_merged=%" PRId64
799 " wr_merged=%" PRId64
800 " idle_time_ns=%" PRId64
801 "\n",
802 stats->value->stats->rd_bytes,
803 stats->value->stats->wr_bytes,
804 stats->value->stats->rd_operations,
805 stats->value->stats->wr_operations,
806 stats->value->stats->flush_operations,
807 stats->value->stats->wr_total_time_ns,
808 stats->value->stats->rd_total_time_ns,
809 stats->value->stats->flush_total_time_ns,
810 stats->value->stats->rd_merged,
811 stats->value->stats->wr_merged,
812 stats->value->stats->idle_time_ns);
815 qapi_free_BlockStatsList(stats_list);
818 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
820 BlockJobInfoList *list;
822 list = qmp_query_block_jobs(&error_abort);
824 if (!list) {
825 monitor_printf(mon, "No active jobs\n");
826 return;
829 while (list) {
830 if (list->value->type == JOB_TYPE_STREAM) {
831 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
832 " of %" PRId64 " bytes, speed limit %" PRId64
833 " bytes/s\n",
834 list->value->device,
835 list->value->offset,
836 list->value->len,
837 list->value->speed);
838 } else {
839 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
840 " of %" PRId64 " bytes, speed limit %" PRId64
841 " bytes/s\n",
842 JobType_str(list->value->type),
843 list->value->device,
844 list->value->offset,
845 list->value->len,
846 list->value->speed);
848 list = list->next;
851 qapi_free_BlockJobInfoList(list);
854 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
856 BlockDriverState *bs, *bs1;
857 BdrvNextIterator it1;
858 QEMUSnapshotInfo *sn_tab, *sn;
859 bool no_snapshot = true;
860 int nb_sns, i;
861 int total;
862 int *global_snapshots;
864 typedef struct SnapshotEntry {
865 QEMUSnapshotInfo sn;
866 QTAILQ_ENTRY(SnapshotEntry) next;
867 } SnapshotEntry;
869 typedef struct ImageEntry {
870 const char *imagename;
871 QTAILQ_ENTRY(ImageEntry) next;
872 QTAILQ_HEAD(, SnapshotEntry) snapshots;
873 } ImageEntry;
875 QTAILQ_HEAD(, ImageEntry) image_list =
876 QTAILQ_HEAD_INITIALIZER(image_list);
878 ImageEntry *image_entry, *next_ie;
879 SnapshotEntry *snapshot_entry;
880 Error *err = NULL;
882 GRAPH_RDLOCK_GUARD_MAINLOOP();
884 bs = bdrv_all_find_vmstate_bs(NULL, false, NULL, &err);
885 if (!bs) {
886 error_report_err(err);
887 return;
890 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
892 if (nb_sns < 0) {
893 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
894 return;
897 for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
898 int bs1_nb_sns = 0;
899 ImageEntry *ie;
900 SnapshotEntry *se;
902 if (bdrv_can_snapshot(bs1)) {
903 sn = NULL;
904 bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
905 if (bs1_nb_sns > 0) {
906 no_snapshot = false;
907 ie = g_new0(ImageEntry, 1);
908 ie->imagename = bdrv_get_device_name(bs1);
909 QTAILQ_INIT(&ie->snapshots);
910 QTAILQ_INSERT_TAIL(&image_list, ie, next);
911 for (i = 0; i < bs1_nb_sns; i++) {
912 se = g_new0(SnapshotEntry, 1);
913 se->sn = sn[i];
914 QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
917 g_free(sn);
921 if (no_snapshot) {
922 monitor_printf(mon, "There is no snapshot available.\n");
923 return;
926 global_snapshots = g_new0(int, nb_sns);
927 total = 0;
928 for (i = 0; i < nb_sns; i++) {
929 SnapshotEntry *next_sn;
930 if (bdrv_all_has_snapshot(sn_tab[i].name, false, NULL, NULL) == 1) {
931 global_snapshots[total] = i;
932 total++;
933 QTAILQ_FOREACH(image_entry, &image_list, next) {
934 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
935 next, next_sn) {
936 if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
937 QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
938 next);
939 g_free(snapshot_entry);
945 monitor_printf(mon, "List of snapshots present on all disks:\n");
947 if (total > 0) {
948 bdrv_snapshot_dump(NULL);
949 monitor_printf(mon, "\n");
950 for (i = 0; i < total; i++) {
951 sn = &sn_tab[global_snapshots[i]];
953 * The ID is not guaranteed to be the same on all images, so
954 * overwrite it.
956 pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
957 bdrv_snapshot_dump(sn);
958 monitor_printf(mon, "\n");
960 } else {
961 monitor_printf(mon, "None\n");
964 QTAILQ_FOREACH(image_entry, &image_list, next) {
965 if (QTAILQ_EMPTY(&image_entry->snapshots)) {
966 continue;
968 monitor_printf(mon,
969 "\nList of partial (non-loadable) snapshots on '%s':\n",
970 image_entry->imagename);
971 bdrv_snapshot_dump(NULL);
972 monitor_printf(mon, "\n");
973 QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
974 bdrv_snapshot_dump(&snapshot_entry->sn);
975 monitor_printf(mon, "\n");
979 QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
980 SnapshotEntry *next_sn;
981 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
982 next_sn) {
983 g_free(snapshot_entry);
985 g_free(image_entry);
987 g_free(sn_tab);
988 g_free(global_snapshots);
991 void hmp_change_medium(Monitor *mon, const char *device, const char *target,
992 const char *arg, const char *read_only, bool force,
993 Error **errp)
995 ERRP_GUARD();
996 BlockdevChangeReadOnlyMode read_only_mode = 0;
998 if (read_only) {
999 read_only_mode =
1000 qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
1001 read_only,
1002 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, errp);
1003 if (*errp) {
1004 return;
1008 qmp_blockdev_change_medium(device, NULL, target, arg, true, force,
1009 !!read_only, read_only_mode, errp);