Merge remote-tracking branch 'remotes/lalrae/tags/mips-20141015' into staging
[qemu/kevin.git] / block / qapi.c
blob9733ebd328d28ae7687a00552de090d254194d93
1 /*
2 * Block layer qmp and info dump related functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "block/qapi.h"
26 #include "block/block_int.h"
27 #include "qmp-commands.h"
28 #include "qapi-visit.h"
29 #include "qapi/qmp-output-visitor.h"
30 #include "qapi/qmp/types.h"
31 #ifdef __linux__
32 #include <linux/fs.h>
33 #include <sys/ioctl.h>
34 #ifndef FS_NOCOW_FL
35 #define FS_NOCOW_FL 0x00800000 /* Do not cow file */
36 #endif
37 #endif
39 BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs)
41 BlockDeviceInfo *info = g_malloc0(sizeof(*info));
43 info->file = g_strdup(bs->filename);
44 info->ro = bs->read_only;
45 info->drv = g_strdup(bs->drv->format_name);
46 info->encrypted = bs->encrypted;
47 info->encryption_key_missing = bdrv_key_required(bs);
49 if (bs->node_name[0]) {
50 info->has_node_name = true;
51 info->node_name = g_strdup(bs->node_name);
54 if (bs->backing_file[0]) {
55 info->has_backing_file = true;
56 info->backing_file = g_strdup(bs->backing_file);
59 info->backing_file_depth = bdrv_get_backing_file_depth(bs);
60 info->detect_zeroes = bs->detect_zeroes;
62 if (bs->io_limits_enabled) {
63 ThrottleConfig cfg;
64 throttle_get_config(&bs->throttle_state, &cfg);
65 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
66 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
67 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
69 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
70 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
71 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
73 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
74 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
75 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
76 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
77 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
78 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
80 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
81 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
82 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
83 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
84 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
85 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
87 info->has_iops_size = cfg.op_size;
88 info->iops_size = cfg.op_size;
91 return info;
95 * Returns 0 on success, with *p_list either set to describe snapshot
96 * information, or NULL because there are no snapshots. Returns -errno on
97 * error, with *p_list untouched.
99 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
100 SnapshotInfoList **p_list,
101 Error **errp)
103 int i, sn_count;
104 QEMUSnapshotInfo *sn_tab = NULL;
105 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
106 SnapshotInfo *info;
108 sn_count = bdrv_snapshot_list(bs, &sn_tab);
109 if (sn_count < 0) {
110 const char *dev = bdrv_get_device_name(bs);
111 switch (sn_count) {
112 case -ENOMEDIUM:
113 error_setg(errp, "Device '%s' is not inserted", dev);
114 break;
115 case -ENOTSUP:
116 error_setg(errp,
117 "Device '%s' does not support internal snapshots",
118 dev);
119 break;
120 default:
121 error_setg_errno(errp, -sn_count,
122 "Can't list snapshots of device '%s'", dev);
123 break;
125 return sn_count;
128 for (i = 0; i < sn_count; i++) {
129 info = g_new0(SnapshotInfo, 1);
130 info->id = g_strdup(sn_tab[i].id_str);
131 info->name = g_strdup(sn_tab[i].name);
132 info->vm_state_size = sn_tab[i].vm_state_size;
133 info->date_sec = sn_tab[i].date_sec;
134 info->date_nsec = sn_tab[i].date_nsec;
135 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
136 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
138 info_list = g_new0(SnapshotInfoList, 1);
139 info_list->value = info;
141 /* XXX: waiting for the qapi to support qemu-queue.h types */
142 if (!cur_item) {
143 head = cur_item = info_list;
144 } else {
145 cur_item->next = info_list;
146 cur_item = info_list;
151 g_free(sn_tab);
152 *p_list = head;
153 return 0;
157 * bdrv_query_image_info:
158 * @bs: block device to examine
159 * @p_info: location to store image information
160 * @errp: location to store error information
162 * Store "flat" image information in @p_info.
164 * "Flat" means it does *not* query backing image information,
165 * i.e. (*pinfo)->has_backing_image will be set to false and
166 * (*pinfo)->backing_image to NULL even when the image does in fact have
167 * a backing image.
169 * @p_info will be set only on success. On error, store error in @errp.
171 void bdrv_query_image_info(BlockDriverState *bs,
172 ImageInfo **p_info,
173 Error **errp)
175 int64_t size;
176 const char *backing_filename;
177 char backing_filename2[1024];
178 BlockDriverInfo bdi;
179 int ret;
180 Error *err = NULL;
181 ImageInfo *info;
182 #ifdef __linux__
183 int fd, attr;
184 #endif
186 size = bdrv_getlength(bs);
187 if (size < 0) {
188 error_setg_errno(errp, -size, "Can't get size of device '%s'",
189 bdrv_get_device_name(bs));
190 return;
193 info = g_new0(ImageInfo, 1);
194 info->filename = g_strdup(bs->filename);
195 info->format = g_strdup(bdrv_get_format_name(bs));
196 info->virtual_size = size;
197 info->actual_size = bdrv_get_allocated_file_size(bs);
198 info->has_actual_size = info->actual_size >= 0;
199 if (bdrv_is_encrypted(bs)) {
200 info->encrypted = true;
201 info->has_encrypted = true;
203 if (bdrv_get_info(bs, &bdi) >= 0) {
204 if (bdi.cluster_size != 0) {
205 info->cluster_size = bdi.cluster_size;
206 info->has_cluster_size = true;
208 info->dirty_flag = bdi.is_dirty;
209 info->has_dirty_flag = true;
211 info->format_specific = bdrv_get_specific_info(bs);
212 info->has_format_specific = info->format_specific != NULL;
214 #ifdef __linux__
215 /* get NOCOW info */
216 fd = qemu_open(bs->filename, O_RDONLY | O_NONBLOCK);
217 if (fd >= 0) {
218 if (ioctl(fd, FS_IOC_GETFLAGS, &attr) == 0 && (attr & FS_NOCOW_FL)) {
219 info->has_nocow = true;
220 info->nocow = true;
222 qemu_close(fd);
224 #endif
226 backing_filename = bs->backing_file;
227 if (backing_filename[0] != '\0') {
228 info->backing_filename = g_strdup(backing_filename);
229 info->has_backing_filename = true;
230 bdrv_get_full_backing_filename(bs, backing_filename2,
231 sizeof(backing_filename2));
233 if (strcmp(backing_filename, backing_filename2) != 0) {
234 info->full_backing_filename =
235 g_strdup(backing_filename2);
236 info->has_full_backing_filename = true;
239 if (bs->backing_format[0]) {
240 info->backing_filename_format = g_strdup(bs->backing_format);
241 info->has_backing_filename_format = true;
245 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
246 switch (ret) {
247 case 0:
248 if (info->snapshots) {
249 info->has_snapshots = true;
251 break;
252 /* recoverable error */
253 case -ENOMEDIUM:
254 case -ENOTSUP:
255 error_free(err);
256 break;
257 default:
258 error_propagate(errp, err);
259 qapi_free_ImageInfo(info);
260 return;
263 *p_info = info;
266 /* @p_info will be set only on success. */
267 void bdrv_query_info(BlockDriverState *bs,
268 BlockInfo **p_info,
269 Error **errp)
271 BlockInfo *info = g_malloc0(sizeof(*info));
272 BlockDriverState *bs0;
273 ImageInfo **p_image_info;
274 Error *local_err = NULL;
275 info->device = g_strdup(bs->device_name);
276 info->type = g_strdup("unknown");
277 info->locked = bdrv_dev_is_medium_locked(bs);
278 info->removable = bdrv_dev_has_removable_media(bs);
280 if (bdrv_dev_has_removable_media(bs)) {
281 info->has_tray_open = true;
282 info->tray_open = bdrv_dev_is_tray_open(bs);
285 if (bdrv_iostatus_is_enabled(bs)) {
286 info->has_io_status = true;
287 info->io_status = bs->iostatus;
290 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
291 info->has_dirty_bitmaps = true;
292 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
295 if (bs->drv) {
296 info->has_inserted = true;
297 info->inserted = bdrv_block_device_info(bs);
299 bs0 = bs;
300 p_image_info = &info->inserted->image;
301 while (1) {
302 bdrv_query_image_info(bs0, p_image_info, &local_err);
303 if (local_err) {
304 error_propagate(errp, local_err);
305 goto err;
307 if (bs0->drv && bs0->backing_hd) {
308 bs0 = bs0->backing_hd;
309 (*p_image_info)->has_backing_image = true;
310 p_image_info = &((*p_image_info)->backing_image);
311 } else {
312 break;
317 *p_info = info;
318 return;
320 err:
321 qapi_free_BlockInfo(info);
324 static BlockStats *bdrv_query_stats(const BlockDriverState *bs)
326 BlockStats *s;
328 s = g_malloc0(sizeof(*s));
330 if (bs->device_name[0]) {
331 s->has_device = true;
332 s->device = g_strdup(bs->device_name);
335 s->stats = g_malloc0(sizeof(*s->stats));
336 s->stats->rd_bytes = bs->stats.nr_bytes[BLOCK_ACCT_READ];
337 s->stats->wr_bytes = bs->stats.nr_bytes[BLOCK_ACCT_WRITE];
338 s->stats->rd_operations = bs->stats.nr_ops[BLOCK_ACCT_READ];
339 s->stats->wr_operations = bs->stats.nr_ops[BLOCK_ACCT_WRITE];
340 s->stats->wr_highest_offset =
341 bs->stats.wr_highest_sector * BDRV_SECTOR_SIZE;
342 s->stats->flush_operations = bs->stats.nr_ops[BLOCK_ACCT_FLUSH];
343 s->stats->wr_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_WRITE];
344 s->stats->rd_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_READ];
345 s->stats->flush_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_FLUSH];
347 if (bs->file) {
348 s->has_parent = true;
349 s->parent = bdrv_query_stats(bs->file);
352 if (bs->backing_hd) {
353 s->has_backing = true;
354 s->backing = bdrv_query_stats(bs->backing_hd);
357 return s;
360 BlockInfoList *qmp_query_block(Error **errp)
362 BlockInfoList *head = NULL, **p_next = &head;
363 BlockDriverState *bs = NULL;
364 Error *local_err = NULL;
366 while ((bs = bdrv_next(bs))) {
367 BlockInfoList *info = g_malloc0(sizeof(*info));
368 bdrv_query_info(bs, &info->value, &local_err);
369 if (local_err) {
370 error_propagate(errp, local_err);
371 goto err;
374 *p_next = info;
375 p_next = &info->next;
378 return head;
380 err:
381 qapi_free_BlockInfoList(head);
382 return NULL;
385 BlockStatsList *qmp_query_blockstats(Error **errp)
387 BlockStatsList *head = NULL, **p_next = &head;
388 BlockDriverState *bs = NULL;
390 while ((bs = bdrv_next(bs))) {
391 BlockStatsList *info = g_malloc0(sizeof(*info));
392 AioContext *ctx = bdrv_get_aio_context(bs);
394 aio_context_acquire(ctx);
395 info->value = bdrv_query_stats(bs);
396 aio_context_release(ctx);
398 *p_next = info;
399 p_next = &info->next;
402 return head;
405 #define NB_SUFFIXES 4
407 static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
409 static const char suffixes[NB_SUFFIXES] = "KMGT";
410 int64_t base;
411 int i;
413 if (size <= 999) {
414 snprintf(buf, buf_size, "%" PRId64, size);
415 } else {
416 base = 1024;
417 for (i = 0; i < NB_SUFFIXES; i++) {
418 if (size < (10 * base)) {
419 snprintf(buf, buf_size, "%0.1f%c",
420 (double)size / base,
421 suffixes[i]);
422 break;
423 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
424 snprintf(buf, buf_size, "%" PRId64 "%c",
425 ((size + (base >> 1)) / base),
426 suffixes[i]);
427 break;
429 base = base * 1024;
432 return buf;
435 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
436 QEMUSnapshotInfo *sn)
438 char buf1[128], date_buf[128], clock_buf[128];
439 struct tm tm;
440 time_t ti;
441 int64_t secs;
443 if (!sn) {
444 func_fprintf(f,
445 "%-10s%-20s%7s%20s%15s",
446 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
447 } else {
448 ti = sn->date_sec;
449 localtime_r(&ti, &tm);
450 strftime(date_buf, sizeof(date_buf),
451 "%Y-%m-%d %H:%M:%S", &tm);
452 secs = sn->vm_clock_nsec / 1000000000;
453 snprintf(clock_buf, sizeof(clock_buf),
454 "%02d:%02d:%02d.%03d",
455 (int)(secs / 3600),
456 (int)((secs / 60) % 60),
457 (int)(secs % 60),
458 (int)((sn->vm_clock_nsec / 1000000) % 1000));
459 func_fprintf(f,
460 "%-10s%-20s%7s%20s%15s",
461 sn->id_str, sn->name,
462 get_human_readable_size(buf1, sizeof(buf1),
463 sn->vm_state_size),
464 date_buf,
465 clock_buf);
469 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
470 QDict *dict);
471 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
472 QList *list);
474 static void dump_qobject(fprintf_function func_fprintf, void *f,
475 int comp_indent, QObject *obj)
477 switch (qobject_type(obj)) {
478 case QTYPE_QINT: {
479 QInt *value = qobject_to_qint(obj);
480 func_fprintf(f, "%" PRId64, qint_get_int(value));
481 break;
483 case QTYPE_QSTRING: {
484 QString *value = qobject_to_qstring(obj);
485 func_fprintf(f, "%s", qstring_get_str(value));
486 break;
488 case QTYPE_QDICT: {
489 QDict *value = qobject_to_qdict(obj);
490 dump_qdict(func_fprintf, f, comp_indent, value);
491 break;
493 case QTYPE_QLIST: {
494 QList *value = qobject_to_qlist(obj);
495 dump_qlist(func_fprintf, f, comp_indent, value);
496 break;
498 case QTYPE_QFLOAT: {
499 QFloat *value = qobject_to_qfloat(obj);
500 func_fprintf(f, "%g", qfloat_get_double(value));
501 break;
503 case QTYPE_QBOOL: {
504 QBool *value = qobject_to_qbool(obj);
505 func_fprintf(f, "%s", qbool_get_int(value) ? "true" : "false");
506 break;
508 case QTYPE_QERROR: {
509 QString *value = qerror_human((QError *)obj);
510 func_fprintf(f, "%s", qstring_get_str(value));
511 QDECREF(value);
512 break;
514 case QTYPE_NONE:
515 break;
516 case QTYPE_MAX:
517 default:
518 abort();
522 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
523 QList *list)
525 const QListEntry *entry;
526 int i = 0;
528 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
529 qtype_code type = qobject_type(entry->value);
530 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
531 const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
533 func_fprintf(f, format, indentation * 4, "", i);
534 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
535 if (!composite) {
536 func_fprintf(f, "\n");
541 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
542 QDict *dict)
544 const QDictEntry *entry;
546 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
547 qtype_code type = qobject_type(entry->value);
548 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
549 const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
550 char key[strlen(entry->key) + 1];
551 int i;
553 /* replace dashes with spaces in key (variable) names */
554 for (i = 0; entry->key[i]; i++) {
555 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
557 key[i] = 0;
559 func_fprintf(f, format, indentation * 4, "", key);
560 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
561 if (!composite) {
562 func_fprintf(f, "\n");
567 void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
568 ImageInfoSpecific *info_spec)
570 QmpOutputVisitor *ov = qmp_output_visitor_new();
571 QObject *obj, *data;
573 visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), &info_spec, NULL,
574 &error_abort);
575 obj = qmp_output_get_qobject(ov);
576 assert(qobject_type(obj) == QTYPE_QDICT);
577 data = qdict_get(qobject_to_qdict(obj), "data");
578 dump_qobject(func_fprintf, f, 1, data);
579 qmp_output_visitor_cleanup(ov);
582 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
583 ImageInfo *info)
585 char size_buf[128], dsize_buf[128];
586 if (!info->has_actual_size) {
587 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
588 } else {
589 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
590 info->actual_size);
592 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
593 func_fprintf(f,
594 "image: %s\n"
595 "file format: %s\n"
596 "virtual size: %s (%" PRId64 " bytes)\n"
597 "disk size: %s\n",
598 info->filename, info->format, size_buf,
599 info->virtual_size,
600 dsize_buf);
602 if (info->has_encrypted && info->encrypted) {
603 func_fprintf(f, "encrypted: yes\n");
606 if (info->has_cluster_size) {
607 func_fprintf(f, "cluster_size: %" PRId64 "\n",
608 info->cluster_size);
611 if (info->has_dirty_flag && info->dirty_flag) {
612 func_fprintf(f, "cleanly shut down: no\n");
615 if (info->has_backing_filename) {
616 func_fprintf(f, "backing file: %s", info->backing_filename);
617 if (info->has_full_backing_filename) {
618 func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
620 func_fprintf(f, "\n");
621 if (info->has_backing_filename_format) {
622 func_fprintf(f, "backing file format: %s\n",
623 info->backing_filename_format);
627 if (info->has_snapshots) {
628 SnapshotInfoList *elem;
630 func_fprintf(f, "Snapshot list:\n");
631 bdrv_snapshot_dump(func_fprintf, f, NULL);
632 func_fprintf(f, "\n");
634 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
635 * we convert to the block layer's native QEMUSnapshotInfo for now.
637 for (elem = info->snapshots; elem; elem = elem->next) {
638 QEMUSnapshotInfo sn = {
639 .vm_state_size = elem->value->vm_state_size,
640 .date_sec = elem->value->date_sec,
641 .date_nsec = elem->value->date_nsec,
642 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
643 elem->value->vm_clock_nsec,
646 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
647 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
648 bdrv_snapshot_dump(func_fprintf, f, &sn);
649 func_fprintf(f, "\n");
653 if (info->has_format_specific) {
654 func_fprintf(f, "Format specific information:\n");
655 bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
658 if (info->has_nocow && info->nocow) {
659 func_fprintf(f, "NOCOW flag: set\n");