xref: /openbmc/qemu/block/monitor/block-hmp-cmds.c (revision 29b62a10)
1 /*
2  * Blockdev HMP commands
3  *
4  *  Authors:
5  *  Anthony Liguori   <aliguori@us.ibm.com>
6  *
7  * Copyright (c) 2003-2008 Fabrice Bellard
8  *
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.
13  *
14  * This file incorporates work covered by the following copyright and
15  * permission notice:
16  *
17  * Copyright (c) 2003-2008 Fabrice Bellard
18  *
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:
25  *
26  * The above copyright notice and this permission notice shall be included in
27  * all copies or substantial portions of the Software.
28  *
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.
36  */
37 
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 "sysemu/sysemu.h"
52 #include "monitor/monitor.h"
53 #include "monitor/hmp.h"
54 #include "block/nbd.h"
55 #include "block/qapi.h"
56 #include "block/block_int.h"
57 #include "block/block-hmp-cmds.h"
58 #include "qemu-io.h"
59 
60 static void hmp_drive_add_node(Monitor *mon, const char *optstr)
61 {
62     QemuOpts *opts;
63     QDict *qdict;
64     Error *local_err = NULL;
65 
66     opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false);
67     if (!opts) {
68         return;
69     }
70 
71     qdict = qemu_opts_to_qdict(opts, NULL);
72 
73     if (!qdict_get_try_str(qdict, "node-name")) {
74         qobject_unref(qdict);
75         error_report("'node-name' needs to be specified");
76         goto out;
77     }
78 
79     BlockDriverState *bs = bds_tree_init(qdict, &local_err);
80     if (!bs) {
81         error_report_err(local_err);
82         goto out;
83     }
84 
85     bdrv_set_monitor_owned(bs);
86 out:
87     qemu_opts_del(opts);
88 }
89 
90 void hmp_drive_add(Monitor *mon, const QDict *qdict)
91 {
92     Error *err = NULL;
93     DriveInfo *dinfo;
94     QemuOpts *opts;
95     MachineClass *mc;
96     const char *optstr = qdict_get_str(qdict, "opts");
97     bool node = qdict_get_try_bool(qdict, "node", false);
98 
99     if (node) {
100         hmp_drive_add_node(mon, optstr);
101         return;
102     }
103 
104     opts = qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false);
105     if (!opts)
106         return;
107 
108     mc = MACHINE_GET_CLASS(current_machine);
109     dinfo = drive_new(opts, mc->block_default_type, &err);
110     if (err) {
111         error_report_err(err);
112         qemu_opts_del(opts);
113         goto err;
114     }
115 
116     if (!dinfo) {
117         return;
118     }
119 
120     switch (dinfo->type) {
121     case IF_NONE:
122         monitor_printf(mon, "OK\n");
123         break;
124     default:
125         monitor_printf(mon, "Can't hot-add drive to type %d\n", dinfo->type);
126         goto err;
127     }
128     return;
129 
130 err:
131     if (dinfo) {
132         BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
133         monitor_remove_blk(blk);
134         blk_unref(blk);
135     }
136 }
137 
138 void hmp_drive_del(Monitor *mon, const QDict *qdict)
139 {
140     const char *id = qdict_get_str(qdict, "id");
141     BlockBackend *blk;
142     BlockDriverState *bs;
143     AioContext *aio_context;
144     Error *local_err = NULL;
145 
146     bs = bdrv_find_node(id);
147     if (bs) {
148         qmp_blockdev_del(id, &local_err);
149         if (local_err) {
150             error_report_err(local_err);
151         }
152         return;
153     }
154 
155     blk = blk_by_name(id);
156     if (!blk) {
157         error_report("Device '%s' not found", id);
158         return;
159     }
160 
161     if (!blk_legacy_dinfo(blk)) {
162         error_report("Deleting device added with blockdev-add"
163                      " is not supported");
164         return;
165     }
166 
167     aio_context = blk_get_aio_context(blk);
168     aio_context_acquire(aio_context);
169 
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             aio_context_release(aio_context);
175             return;
176         }
177 
178         blk_remove_bs(blk);
179     }
180 
181     /* Make the BlockBackend and the attached BlockDriverState anonymous */
182     monitor_remove_blk(blk);
183 
184     /*
185      * If this BlockBackend has a device attached to it, its refcount will be
186      * decremented when the device is removed; otherwise we have to do so here.
187      */
188     if (blk_get_attached_dev(blk)) {
189         /* Further I/O must not pause the guest */
190         blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT,
191                          BLOCKDEV_ON_ERROR_REPORT);
192     } else {
193         blk_unref(blk);
194     }
195 
196     aio_context_release(aio_context);
197 }
198 
199 void hmp_commit(Monitor *mon, const QDict *qdict)
200 {
201     const char *device = qdict_get_str(qdict, "device");
202     BlockBackend *blk;
203     int ret;
204 
205     if (!strcmp(device, "all")) {
206         ret = blk_commit_all();
207     } else {
208         BlockDriverState *bs;
209         AioContext *aio_context;
210 
211         blk = blk_by_name(device);
212         if (!blk) {
213             error_report("Device '%s' not found", device);
214             return;
215         }
216         if (!blk_is_available(blk)) {
217             error_report("Device '%s' has no medium", device);
218             return;
219         }
220 
221         bs = bdrv_skip_implicit_filters(blk_bs(blk));
222         aio_context = bdrv_get_aio_context(bs);
223         aio_context_acquire(aio_context);
224 
225         ret = bdrv_commit(bs);
226 
227         aio_context_release(aio_context);
228     }
229     if (ret < 0) {
230         error_report("'commit' error for '%s': %s", device, strerror(-ret));
231     }
232 }
233 
234 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
235 {
236     const char *filename = qdict_get_str(qdict, "target");
237     const char *format = qdict_get_try_str(qdict, "format");
238     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
239     bool full = qdict_get_try_bool(qdict, "full", false);
240     Error *err = NULL;
241     DriveMirror mirror = {
242         .device = (char *)qdict_get_str(qdict, "device"),
243         .target = (char *)filename,
244         .format = (char *)format,
245         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
246         .has_mode = true,
247         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
248         .unmap = true,
249     };
250 
251     if (!filename) {
252         error_setg(&err, QERR_MISSING_PARAMETER, "target");
253         goto end;
254     }
255     qmp_drive_mirror(&mirror, &err);
256 end:
257     hmp_handle_error(mon, err);
258 }
259 
260 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
261 {
262     const char *device = qdict_get_str(qdict, "device");
263     const char *filename = qdict_get_str(qdict, "target");
264     const char *format = qdict_get_try_str(qdict, "format");
265     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
266     bool full = qdict_get_try_bool(qdict, "full", false);
267     bool compress = qdict_get_try_bool(qdict, "compress", false);
268     Error *err = NULL;
269     DriveBackup backup = {
270         .device = (char *)device,
271         .target = (char *)filename,
272         .format = (char *)format,
273         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
274         .has_mode = true,
275         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
276         .has_compress = !!compress,
277         .compress = compress,
278     };
279 
280     if (!filename) {
281         error_setg(&err, QERR_MISSING_PARAMETER, "target");
282         goto end;
283     }
284 
285     qmp_drive_backup(&backup, &err);
286 end:
287     hmp_handle_error(mon, err);
288 }
289 
290 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
291 {
292     Error *error = NULL;
293     const char *device = qdict_get_str(qdict, "device");
294     int64_t value = qdict_get_int(qdict, "speed");
295 
296     qmp_block_job_set_speed(device, value, &error);
297 
298     hmp_handle_error(mon, error);
299 }
300 
301 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
302 {
303     Error *error = NULL;
304     const char *device = qdict_get_str(qdict, "device");
305     bool force = qdict_get_try_bool(qdict, "force", false);
306 
307     qmp_block_job_cancel(device, true, force, &error);
308 
309     hmp_handle_error(mon, error);
310 }
311 
312 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
313 {
314     Error *error = NULL;
315     const char *device = qdict_get_str(qdict, "device");
316 
317     qmp_block_job_pause(device, &error);
318 
319     hmp_handle_error(mon, error);
320 }
321 
322 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
323 {
324     Error *error = NULL;
325     const char *device = qdict_get_str(qdict, "device");
326 
327     qmp_block_job_resume(device, &error);
328 
329     hmp_handle_error(mon, error);
330 }
331 
332 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
333 {
334     Error *error = NULL;
335     const char *device = qdict_get_str(qdict, "device");
336 
337     qmp_block_job_complete(device, &error);
338 
339     hmp_handle_error(mon, error);
340 }
341 
342 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
343 {
344     const char *device = qdict_get_str(qdict, "device");
345     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
346     const char *format = qdict_get_try_str(qdict, "format");
347     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
348     enum NewImageMode mode;
349     Error *err = NULL;
350 
351     if (!filename) {
352         /*
353          * In the future, if 'snapshot-file' is not specified, the snapshot
354          * will be taken internally. Today it's actually required.
355          */
356         error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
357         goto end;
358     }
359 
360     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
361     qmp_blockdev_snapshot_sync(device, NULL, filename, NULL, format,
362                                true, mode, &err);
363 end:
364     hmp_handle_error(mon, err);
365 }
366 
367 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
368 {
369     const char *device = qdict_get_str(qdict, "device");
370     const char *name = qdict_get_str(qdict, "name");
371     Error *err = NULL;
372 
373     qmp_blockdev_snapshot_internal_sync(device, name, &err);
374     hmp_handle_error(mon, err);
375 }
376 
377 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
378 {
379     const char *device = qdict_get_str(qdict, "device");
380     const char *name = qdict_get_str(qdict, "name");
381     const char *id = qdict_get_try_str(qdict, "id");
382     Error *err = NULL;
383 
384     qmp_blockdev_snapshot_delete_internal_sync(device, id, name, &err);
385     hmp_handle_error(mon, err);
386 }
387 
388 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
389 {
390     const char *uri = qdict_get_str(qdict, "uri");
391     bool writable = qdict_get_try_bool(qdict, "writable", false);
392     bool all = qdict_get_try_bool(qdict, "all", false);
393     Error *local_err = NULL;
394     BlockInfoList *block_list, *info;
395     SocketAddress *addr;
396     NbdServerAddOptions export;
397 
398     if (writable && !all) {
399         error_setg(&local_err, "-w only valid together with -a");
400         goto exit;
401     }
402 
403     /* First check if the address is valid and start the server.  */
404     addr = socket_parse(uri, &local_err);
405     if (local_err != NULL) {
406         goto exit;
407     }
408 
409     nbd_server_start(addr, NULL, NULL, 0, &local_err);
410     qapi_free_SocketAddress(addr);
411     if (local_err != NULL) {
412         goto exit;
413     }
414 
415     if (!all) {
416         return;
417     }
418 
419     /* Then try adding all block devices.  If one fails, close all and
420      * exit.
421      */
422     block_list = qmp_query_block(NULL);
423 
424     for (info = block_list; info; info = info->next) {
425         if (!info->value->inserted) {
426             continue;
427         }
428 
429         export = (NbdServerAddOptions) {
430             .device         = info->value->device,
431             .has_writable   = true,
432             .writable       = writable,
433         };
434 
435         qmp_nbd_server_add(&export, &local_err);
436 
437         if (local_err != NULL) {
438             qmp_nbd_server_stop(NULL);
439             break;
440         }
441     }
442 
443     qapi_free_BlockInfoList(block_list);
444 
445 exit:
446     hmp_handle_error(mon, local_err);
447 }
448 
449 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
450 {
451     const char *device = qdict_get_str(qdict, "device");
452     const char *name = qdict_get_try_str(qdict, "name");
453     bool writable = qdict_get_try_bool(qdict, "writable", false);
454     Error *local_err = NULL;
455 
456     NbdServerAddOptions export = {
457         .device         = (char *) device,
458         .name           = (char *) name,
459         .has_writable   = true,
460         .writable       = writable,
461     };
462 
463     qmp_nbd_server_add(&export, &local_err);
464     hmp_handle_error(mon, local_err);
465 }
466 
467 void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict)
468 {
469     const char *name = qdict_get_str(qdict, "name");
470     bool force = qdict_get_try_bool(qdict, "force", false);
471     Error *err = NULL;
472 
473     /* Rely on BLOCK_EXPORT_REMOVE_MODE_SAFE being the default */
474     qmp_nbd_server_remove(name, force, BLOCK_EXPORT_REMOVE_MODE_HARD, &err);
475     hmp_handle_error(mon, err);
476 }
477 
478 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
479 {
480     Error *err = NULL;
481 
482     qmp_nbd_server_stop(&err);
483     hmp_handle_error(mon, err);
484 }
485 
486 void coroutine_fn hmp_block_resize(Monitor *mon, const QDict *qdict)
487 {
488     const char *device = qdict_get_str(qdict, "device");
489     int64_t size = qdict_get_int(qdict, "size");
490     Error *err = NULL;
491 
492     qmp_block_resize(device, NULL, size, &err);
493     hmp_handle_error(mon, err);
494 }
495 
496 void hmp_block_stream(Monitor *mon, const QDict *qdict)
497 {
498     Error *error = NULL;
499     const char *device = qdict_get_str(qdict, "device");
500     const char *base = qdict_get_try_str(qdict, "base");
501     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
502 
503     qmp_block_stream(device, device, base, NULL, NULL, NULL,
504                      qdict_haskey(qdict, "speed"), speed,
505                      true, BLOCKDEV_ON_ERROR_REPORT, NULL,
506                      false, false, false, false, &error);
507 
508     hmp_handle_error(mon, error);
509 }
510 
511 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
512 {
513     Error *err = NULL;
514     char *device = (char *) qdict_get_str(qdict, "device");
515     BlockIOThrottle throttle = {
516         .bps = qdict_get_int(qdict, "bps"),
517         .bps_rd = qdict_get_int(qdict, "bps_rd"),
518         .bps_wr = qdict_get_int(qdict, "bps_wr"),
519         .iops = qdict_get_int(qdict, "iops"),
520         .iops_rd = qdict_get_int(qdict, "iops_rd"),
521         .iops_wr = qdict_get_int(qdict, "iops_wr"),
522     };
523 
524     /*
525      * qmp_block_set_io_throttle has separate parameters for the
526      * (deprecated) block device name and the qdev ID but the HMP
527      * version has only one, so we must decide which one to pass.
528      */
529     if (blk_by_name(device)) {
530         throttle.device = device;
531     } else {
532         throttle.id = device;
533     }
534 
535     qmp_block_set_io_throttle(&throttle, &err);
536     hmp_handle_error(mon, err);
537 }
538 
539 void hmp_eject(Monitor *mon, const QDict *qdict)
540 {
541     bool force = qdict_get_try_bool(qdict, "force", false);
542     const char *device = qdict_get_str(qdict, "device");
543     Error *err = NULL;
544 
545     qmp_eject(device, NULL, true, force, &err);
546     hmp_handle_error(mon, err);
547 }
548 
549 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
550 {
551     BlockBackend *blk = NULL;
552     BlockDriverState *bs = NULL;
553     BlockBackend *local_blk = NULL;
554     AioContext *ctx = NULL;
555     bool qdev = qdict_get_try_bool(qdict, "qdev", false);
556     const char *device = qdict_get_str(qdict, "device");
557     const char *command = qdict_get_str(qdict, "command");
558     Error *err = NULL;
559     int ret;
560 
561     if (qdev) {
562         blk = blk_by_qdev_id(device, &err);
563         if (!blk) {
564             goto fail;
565         }
566     } else {
567         blk = blk_by_name(device);
568         if (!blk) {
569             bs = bdrv_lookup_bs(NULL, device, &err);
570             if (!bs) {
571                 goto fail;
572             }
573         }
574     }
575 
576     ctx = blk ? blk_get_aio_context(blk) : bdrv_get_aio_context(bs);
577     aio_context_acquire(ctx);
578 
579     if (bs) {
580         blk = local_blk = blk_new(bdrv_get_aio_context(bs), 0, BLK_PERM_ALL);
581         ret = blk_insert_bs(blk, bs, &err);
582         if (ret < 0) {
583             goto fail;
584         }
585     }
586 
587     /*
588      * Notably absent: Proper permission management. This is sad, but it seems
589      * almost impossible to achieve without changing the semantics and thereby
590      * limiting the use cases of the qemu-io HMP command.
591      *
592      * In an ideal world we would unconditionally create a new BlockBackend for
593      * qemuio_command(), but we have commands like 'reopen' and want them to
594      * take effect on the exact BlockBackend whose name the user passed instead
595      * of just on a temporary copy of it.
596      *
597      * Another problem is that deleting the temporary BlockBackend involves
598      * draining all requests on it first, but some qemu-iotests cases want to
599      * issue multiple aio_read/write requests and expect them to complete in
600      * the background while the monitor has already returned.
601      *
602      * This is also what prevents us from saving the original permissions and
603      * restoring them later: We can't revoke permissions until all requests
604      * have completed, and we don't know when that is nor can we really let
605      * anything else run before we have revoken them to avoid race conditions.
606      *
607      * What happens now is that command() in qemu-io-cmds.c can extend the
608      * permissions if necessary for the qemu-io command. And they simply stay
609      * extended, possibly resulting in a read-only guest device keeping write
610      * permissions. Ugly, but it appears to be the lesser evil.
611      */
612     qemuio_command(blk, command);
613 
614 fail:
615     blk_unref(local_blk);
616 
617     if (ctx) {
618         aio_context_release(ctx);
619     }
620 
621     hmp_handle_error(mon, err);
622 }
623 
624 static void print_block_info(Monitor *mon, BlockInfo *info,
625                              BlockDeviceInfo *inserted, bool verbose)
626 {
627     ImageInfo *image_info;
628 
629     assert(!info || !info->inserted || info->inserted == inserted);
630 
631     if (info && *info->device) {
632         monitor_puts(mon, info->device);
633         if (inserted && inserted->node_name) {
634             monitor_printf(mon, " (%s)", inserted->node_name);
635         }
636     } else {
637         assert(info || inserted);
638         monitor_puts(mon,
639                      inserted && inserted->node_name ? inserted->node_name
640                      : info && info->qdev ? info->qdev
641                      : "<anonymous>");
642     }
643 
644     if (inserted) {
645         monitor_printf(mon, ": %s (%s%s%s)\n",
646                        inserted->file,
647                        inserted->drv,
648                        inserted->ro ? ", read-only" : "",
649                        inserted->encrypted ? ", encrypted" : "");
650     } else {
651         monitor_printf(mon, ": [not inserted]\n");
652     }
653 
654     if (info) {
655         if (info->qdev) {
656             monitor_printf(mon, "    Attached to:      %s\n", info->qdev);
657         }
658         if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
659             monitor_printf(mon, "    I/O status:       %s\n",
660                            BlockDeviceIoStatus_str(info->io_status));
661         }
662 
663         if (info->removable) {
664             monitor_printf(mon, "    Removable device: %slocked, tray %s\n",
665                            info->locked ? "" : "not ",
666                            info->tray_open ? "open" : "closed");
667         }
668     }
669 
670 
671     if (!inserted) {
672         return;
673     }
674 
675     monitor_printf(mon, "    Cache mode:       %s%s%s\n",
676                    inserted->cache->writeback ? "writeback" : "writethrough",
677                    inserted->cache->direct ? ", direct" : "",
678                    inserted->cache->no_flush ? ", ignore flushes" : "");
679 
680     if (inserted->backing_file) {
681         monitor_printf(mon,
682                        "    Backing file:     %s "
683                        "(chain depth: %" PRId64 ")\n",
684                        inserted->backing_file,
685                        inserted->backing_file_depth);
686     }
687 
688     if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
689         monitor_printf(mon, "    Detect zeroes:    %s\n",
690                 BlockdevDetectZeroesOptions_str(inserted->detect_zeroes));
691     }
692 
693     if (inserted->bps  || inserted->bps_rd  || inserted->bps_wr  ||
694         inserted->iops || inserted->iops_rd || inserted->iops_wr)
695     {
696         monitor_printf(mon, "    I/O throttling:   bps=%" PRId64
697                         " bps_rd=%" PRId64  " bps_wr=%" PRId64
698                         " bps_max=%" PRId64
699                         " bps_rd_max=%" PRId64
700                         " bps_wr_max=%" PRId64
701                         " iops=%" PRId64 " iops_rd=%" PRId64
702                         " iops_wr=%" PRId64
703                         " iops_max=%" PRId64
704                         " iops_rd_max=%" PRId64
705                         " iops_wr_max=%" PRId64
706                         " iops_size=%" PRId64
707                         " group=%s\n",
708                         inserted->bps,
709                         inserted->bps_rd,
710                         inserted->bps_wr,
711                         inserted->bps_max,
712                         inserted->bps_rd_max,
713                         inserted->bps_wr_max,
714                         inserted->iops,
715                         inserted->iops_rd,
716                         inserted->iops_wr,
717                         inserted->iops_max,
718                         inserted->iops_rd_max,
719                         inserted->iops_wr_max,
720                         inserted->iops_size,
721                         inserted->group);
722     }
723 
724     if (verbose) {
725         monitor_printf(mon, "\nImages:\n");
726         image_info = inserted->image;
727         while (1) {
728                 bdrv_image_info_dump(image_info);
729             if (image_info->backing_image) {
730                 image_info = image_info->backing_image;
731             } else {
732                 break;
733             }
734         }
735     }
736 }
737 
738 void hmp_info_block(Monitor *mon, const QDict *qdict)
739 {
740     BlockInfoList *block_list, *info;
741     BlockDeviceInfoList *blockdev_list, *blockdev;
742     const char *device = qdict_get_try_str(qdict, "device");
743     bool verbose = qdict_get_try_bool(qdict, "verbose", false);
744     bool nodes = qdict_get_try_bool(qdict, "nodes", false);
745     bool printed = false;
746 
747     /* Print BlockBackend information */
748     if (!nodes) {
749         block_list = qmp_query_block(NULL);
750     } else {
751         block_list = NULL;
752     }
753 
754     for (info = block_list; info; info = info->next) {
755         if (device && strcmp(device, info->value->device)) {
756             continue;
757         }
758 
759         if (info != block_list) {
760             monitor_printf(mon, "\n");
761         }
762 
763         print_block_info(mon, info->value, info->value->inserted,
764                          verbose);
765         printed = true;
766     }
767 
768     qapi_free_BlockInfoList(block_list);
769 
770     if ((!device && !nodes) || printed) {
771         return;
772     }
773 
774     /* Print node information */
775     blockdev_list = qmp_query_named_block_nodes(false, false, NULL);
776     for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
777         assert(blockdev->value->node_name);
778         if (device && strcmp(device, blockdev->value->node_name)) {
779             continue;
780         }
781 
782         if (blockdev != blockdev_list) {
783             monitor_printf(mon, "\n");
784         }
785 
786         print_block_info(mon, NULL, blockdev->value, verbose);
787     }
788     qapi_free_BlockDeviceInfoList(blockdev_list);
789 }
790 
791 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
792 {
793     BlockStatsList *stats_list, *stats;
794 
795     stats_list = qmp_query_blockstats(false, false, NULL);
796 
797     for (stats = stats_list; stats; stats = stats->next) {
798         if (!stats->value->device) {
799             continue;
800         }
801 
802         monitor_printf(mon, "%s:", stats->value->device);
803         monitor_printf(mon, " rd_bytes=%" PRId64
804                        " wr_bytes=%" PRId64
805                        " rd_operations=%" PRId64
806                        " wr_operations=%" PRId64
807                        " flush_operations=%" PRId64
808                        " wr_total_time_ns=%" PRId64
809                        " rd_total_time_ns=%" PRId64
810                        " flush_total_time_ns=%" PRId64
811                        " rd_merged=%" PRId64
812                        " wr_merged=%" PRId64
813                        " idle_time_ns=%" PRId64
814                        "\n",
815                        stats->value->stats->rd_bytes,
816                        stats->value->stats->wr_bytes,
817                        stats->value->stats->rd_operations,
818                        stats->value->stats->wr_operations,
819                        stats->value->stats->flush_operations,
820                        stats->value->stats->wr_total_time_ns,
821                        stats->value->stats->rd_total_time_ns,
822                        stats->value->stats->flush_total_time_ns,
823                        stats->value->stats->rd_merged,
824                        stats->value->stats->wr_merged,
825                        stats->value->stats->idle_time_ns);
826     }
827 
828     qapi_free_BlockStatsList(stats_list);
829 }
830 
831 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
832 {
833     BlockJobInfoList *list;
834 
835     list = qmp_query_block_jobs(&error_abort);
836 
837     if (!list) {
838         monitor_printf(mon, "No active jobs\n");
839         return;
840     }
841 
842     while (list) {
843         if (strcmp(list->value->type, "stream") == 0) {
844             monitor_printf(mon, "Streaming device %s: Completed %" PRId64
845                            " of %" PRId64 " bytes, speed limit %" PRId64
846                            " bytes/s\n",
847                            list->value->device,
848                            list->value->offset,
849                            list->value->len,
850                            list->value->speed);
851         } else {
852             monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
853                            " of %" PRId64 " bytes, speed limit %" PRId64
854                            " bytes/s\n",
855                            list->value->type,
856                            list->value->device,
857                            list->value->offset,
858                            list->value->len,
859                            list->value->speed);
860         }
861         list = list->next;
862     }
863 
864     qapi_free_BlockJobInfoList(list);
865 }
866 
867 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
868 {
869     BlockDriverState *bs, *bs1;
870     BdrvNextIterator it1;
871     QEMUSnapshotInfo *sn_tab, *sn;
872     bool no_snapshot = true;
873     int nb_sns, i;
874     int total;
875     int *global_snapshots;
876     AioContext *aio_context;
877 
878     typedef struct SnapshotEntry {
879         QEMUSnapshotInfo sn;
880         QTAILQ_ENTRY(SnapshotEntry) next;
881     } SnapshotEntry;
882 
883     typedef struct ImageEntry {
884         const char *imagename;
885         QTAILQ_ENTRY(ImageEntry) next;
886         QTAILQ_HEAD(, SnapshotEntry) snapshots;
887     } ImageEntry;
888 
889     QTAILQ_HEAD(, ImageEntry) image_list =
890         QTAILQ_HEAD_INITIALIZER(image_list);
891 
892     ImageEntry *image_entry, *next_ie;
893     SnapshotEntry *snapshot_entry;
894     Error *err = NULL;
895 
896     bs = bdrv_all_find_vmstate_bs(NULL, false, NULL, &err);
897     if (!bs) {
898         error_report_err(err);
899         return;
900     }
901     aio_context = bdrv_get_aio_context(bs);
902 
903     aio_context_acquire(aio_context);
904     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
905     aio_context_release(aio_context);
906 
907     if (nb_sns < 0) {
908         monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
909         return;
910     }
911 
912     for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
913         int bs1_nb_sns = 0;
914         ImageEntry *ie;
915         SnapshotEntry *se;
916         AioContext *ctx = bdrv_get_aio_context(bs1);
917 
918         aio_context_acquire(ctx);
919         if (bdrv_can_snapshot(bs1)) {
920             sn = NULL;
921             bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
922             if (bs1_nb_sns > 0) {
923                 no_snapshot = false;
924                 ie = g_new0(ImageEntry, 1);
925                 ie->imagename = bdrv_get_device_name(bs1);
926                 QTAILQ_INIT(&ie->snapshots);
927                 QTAILQ_INSERT_TAIL(&image_list, ie, next);
928                 for (i = 0; i < bs1_nb_sns; i++) {
929                     se = g_new0(SnapshotEntry, 1);
930                     se->sn = sn[i];
931                     QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
932                 }
933             }
934             g_free(sn);
935         }
936         aio_context_release(ctx);
937     }
938 
939     if (no_snapshot) {
940         monitor_printf(mon, "There is no snapshot available.\n");
941         return;
942     }
943 
944     global_snapshots = g_new0(int, nb_sns);
945     total = 0;
946     for (i = 0; i < nb_sns; i++) {
947         SnapshotEntry *next_sn;
948         if (bdrv_all_has_snapshot(sn_tab[i].name, false, NULL, NULL) == 1) {
949             global_snapshots[total] = i;
950             total++;
951             QTAILQ_FOREACH(image_entry, &image_list, next) {
952                 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
953                                     next, next_sn) {
954                     if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
955                         QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
956                                       next);
957                         g_free(snapshot_entry);
958                     }
959                 }
960             }
961         }
962     }
963     monitor_printf(mon, "List of snapshots present on all disks:\n");
964 
965     if (total > 0) {
966         bdrv_snapshot_dump(NULL);
967         monitor_printf(mon, "\n");
968         for (i = 0; i < total; i++) {
969             sn = &sn_tab[global_snapshots[i]];
970             /*
971              * The ID is not guaranteed to be the same on all images, so
972              * overwrite it.
973              */
974             pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
975             bdrv_snapshot_dump(sn);
976             monitor_printf(mon, "\n");
977         }
978     } else {
979         monitor_printf(mon, "None\n");
980     }
981 
982     QTAILQ_FOREACH(image_entry, &image_list, next) {
983         if (QTAILQ_EMPTY(&image_entry->snapshots)) {
984             continue;
985         }
986         monitor_printf(mon,
987                        "\nList of partial (non-loadable) snapshots on '%s':\n",
988                        image_entry->imagename);
989         bdrv_snapshot_dump(NULL);
990         monitor_printf(mon, "\n");
991         QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
992             bdrv_snapshot_dump(&snapshot_entry->sn);
993             monitor_printf(mon, "\n");
994         }
995     }
996 
997     QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
998         SnapshotEntry *next_sn;
999         QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
1000                             next_sn) {
1001             g_free(snapshot_entry);
1002         }
1003         g_free(image_entry);
1004     }
1005     g_free(sn_tab);
1006     g_free(global_snapshots);
1007 }
1008