]> Git Repo - qemu.git/blob - block/qapi.c
block: add snapshot info query function bdrv_query_snapshot_info_list()
[qemu.git] / block / qapi.c
1 /*
2  * Block layer qmp and info dump related functions
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #include "block/qapi.h"
26 #include "block/block_int.h"
27 #include "qmp-commands.h"
28
29 /*
30  * Returns 0 on success, with *p_list either set to describe snapshot
31  * information, or NULL because there are no snapshots.  Returns -errno on
32  * error, with *p_list untouched.
33  */
34 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
35                                   SnapshotInfoList **p_list,
36                                   Error **errp)
37 {
38     int i, sn_count;
39     QEMUSnapshotInfo *sn_tab = NULL;
40     SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
41     SnapshotInfo *info;
42
43     sn_count = bdrv_snapshot_list(bs, &sn_tab);
44     if (sn_count < 0) {
45         const char *dev = bdrv_get_device_name(bs);
46         switch (sn_count) {
47         case -ENOMEDIUM:
48             error_setg(errp, "Device '%s' is not inserted", dev);
49             break;
50         case -ENOTSUP:
51             error_setg(errp,
52                        "Device '%s' does not support internal snapshots",
53                        dev);
54             break;
55         default:
56             error_setg_errno(errp, -sn_count,
57                              "Can't list snapshots of device '%s'", dev);
58             break;
59         }
60         return sn_count;
61     }
62
63     for (i = 0; i < sn_count; i++) {
64         info = g_new0(SnapshotInfo, 1);
65         info->id            = g_strdup(sn_tab[i].id_str);
66         info->name          = g_strdup(sn_tab[i].name);
67         info->vm_state_size = sn_tab[i].vm_state_size;
68         info->date_sec      = sn_tab[i].date_sec;
69         info->date_nsec     = sn_tab[i].date_nsec;
70         info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
71         info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
72
73         info_list = g_new0(SnapshotInfoList, 1);
74         info_list->value = info;
75
76         /* XXX: waiting for the qapi to support qemu-queue.h types */
77         if (!cur_item) {
78             head = cur_item = info_list;
79         } else {
80             cur_item->next = info_list;
81             cur_item = info_list;
82         }
83
84     }
85
86     g_free(sn_tab);
87     *p_list = head;
88     return 0;
89 }
90
91 void bdrv_collect_image_info(BlockDriverState *bs,
92                              ImageInfo *info,
93                              const char *filename)
94 {
95     uint64_t total_sectors;
96     char backing_filename[1024];
97     char backing_filename2[1024];
98     BlockDriverInfo bdi;
99
100     bdrv_get_geometry(bs, &total_sectors);
101
102     info->filename        = g_strdup(filename);
103     info->format          = g_strdup(bdrv_get_format_name(bs));
104     info->virtual_size    = total_sectors * 512;
105     info->actual_size     = bdrv_get_allocated_file_size(bs);
106     info->has_actual_size = info->actual_size >= 0;
107     if (bdrv_is_encrypted(bs)) {
108         info->encrypted = true;
109         info->has_encrypted = true;
110     }
111     if (bdrv_get_info(bs, &bdi) >= 0) {
112         if (bdi.cluster_size != 0) {
113             info->cluster_size = bdi.cluster_size;
114             info->has_cluster_size = true;
115         }
116         info->dirty_flag = bdi.is_dirty;
117         info->has_dirty_flag = true;
118     }
119     bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
120     if (backing_filename[0] != '\0') {
121         info->backing_filename = g_strdup(backing_filename);
122         info->has_backing_filename = true;
123         bdrv_get_full_backing_filename(bs, backing_filename2,
124                                        sizeof(backing_filename2));
125
126         if (strcmp(backing_filename, backing_filename2) != 0) {
127             info->full_backing_filename =
128                         g_strdup(backing_filename2);
129             info->has_full_backing_filename = true;
130         }
131
132         if (bs->backing_format[0]) {
133             info->backing_filename_format = g_strdup(bs->backing_format);
134             info->has_backing_filename_format = true;
135         }
136     }
137 }
138
139 BlockInfo *bdrv_query_info(BlockDriverState *bs)
140 {
141     BlockInfo *info = g_malloc0(sizeof(*info));
142     info->device = g_strdup(bs->device_name);
143     info->type = g_strdup("unknown");
144     info->locked = bdrv_dev_is_medium_locked(bs);
145     info->removable = bdrv_dev_has_removable_media(bs);
146
147     if (bdrv_dev_has_removable_media(bs)) {
148         info->has_tray_open = true;
149         info->tray_open = bdrv_dev_is_tray_open(bs);
150     }
151
152     if (bdrv_iostatus_is_enabled(bs)) {
153         info->has_io_status = true;
154         info->io_status = bs->iostatus;
155     }
156
157     if (bs->dirty_bitmap) {
158         info->has_dirty = true;
159         info->dirty = g_malloc0(sizeof(*info->dirty));
160         info->dirty->count = bdrv_get_dirty_count(bs) * BDRV_SECTOR_SIZE;
161         info->dirty->granularity =
162          ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bs->dirty_bitmap));
163     }
164
165     if (bs->drv) {
166         info->has_inserted = true;
167         info->inserted = g_malloc0(sizeof(*info->inserted));
168         info->inserted->file = g_strdup(bs->filename);
169         info->inserted->ro = bs->read_only;
170         info->inserted->drv = g_strdup(bs->drv->format_name);
171         info->inserted->encrypted = bs->encrypted;
172         info->inserted->encryption_key_missing = bdrv_key_required(bs);
173
174         if (bs->backing_file[0]) {
175             info->inserted->has_backing_file = true;
176             info->inserted->backing_file = g_strdup(bs->backing_file);
177         }
178
179         info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
180
181         if (bs->io_limits_enabled) {
182             info->inserted->bps =
183                            bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
184             info->inserted->bps_rd =
185                            bs->io_limits.bps[BLOCK_IO_LIMIT_READ];
186             info->inserted->bps_wr =
187                            bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE];
188             info->inserted->iops =
189                            bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
190             info->inserted->iops_rd =
191                            bs->io_limits.iops[BLOCK_IO_LIMIT_READ];
192             info->inserted->iops_wr =
193                            bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
194         }
195     }
196     return info;
197 }
198
199 BlockStats *bdrv_query_stats(const BlockDriverState *bs)
200 {
201     BlockStats *s;
202
203     s = g_malloc0(sizeof(*s));
204
205     if (bs->device_name[0]) {
206         s->has_device = true;
207         s->device = g_strdup(bs->device_name);
208     }
209
210     s->stats = g_malloc0(sizeof(*s->stats));
211     s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
212     s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
213     s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
214     s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
215     s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
216     s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
217     s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
218     s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
219     s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
220
221     if (bs->file) {
222         s->has_parent = true;
223         s->parent = bdrv_query_stats(bs->file);
224     }
225
226     return s;
227 }
228
229 BlockInfoList *qmp_query_block(Error **errp)
230 {
231     BlockInfoList *head = NULL, **p_next = &head;
232     BlockDriverState *bs = NULL;
233
234      while ((bs = bdrv_next(bs))) {
235         BlockInfoList *info = g_malloc0(sizeof(*info));
236         info->value = bdrv_query_info(bs);
237
238         *p_next = info;
239         p_next = &info->next;
240     }
241
242     return head;
243 }
244
245 BlockStatsList *qmp_query_blockstats(Error **errp)
246 {
247     BlockStatsList *head = NULL, **p_next = &head;
248     BlockDriverState *bs = NULL;
249
250      while ((bs = bdrv_next(bs))) {
251         BlockStatsList *info = g_malloc0(sizeof(*info));
252         info->value = bdrv_query_stats(bs);
253
254         *p_next = info;
255         p_next = &info->next;
256     }
257
258     return head;
259 }
260
261 #define NB_SUFFIXES 4
262
263 static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
264 {
265     static const char suffixes[NB_SUFFIXES] = "KMGT";
266     int64_t base;
267     int i;
268
269     if (size <= 999) {
270         snprintf(buf, buf_size, "%" PRId64, size);
271     } else {
272         base = 1024;
273         for (i = 0; i < NB_SUFFIXES; i++) {
274             if (size < (10 * base)) {
275                 snprintf(buf, buf_size, "%0.1f%c",
276                          (double)size / base,
277                          suffixes[i]);
278                 break;
279             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
280                 snprintf(buf, buf_size, "%" PRId64 "%c",
281                          ((size + (base >> 1)) / base),
282                          suffixes[i]);
283                 break;
284             }
285             base = base * 1024;
286         }
287     }
288     return buf;
289 }
290
291 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
292                         QEMUSnapshotInfo *sn)
293 {
294     char buf1[128], date_buf[128], clock_buf[128];
295     struct tm tm;
296     time_t ti;
297     int64_t secs;
298
299     if (!sn) {
300         func_fprintf(f,
301                      "%-10s%-20s%7s%20s%15s",
302                      "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
303     } else {
304         ti = sn->date_sec;
305         localtime_r(&ti, &tm);
306         strftime(date_buf, sizeof(date_buf),
307                  "%Y-%m-%d %H:%M:%S", &tm);
308         secs = sn->vm_clock_nsec / 1000000000;
309         snprintf(clock_buf, sizeof(clock_buf),
310                  "%02d:%02d:%02d.%03d",
311                  (int)(secs / 3600),
312                  (int)((secs / 60) % 60),
313                  (int)(secs % 60),
314                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
315         func_fprintf(f,
316                      "%-10s%-20s%7s%20s%15s",
317                      sn->id_str, sn->name,
318                      get_human_readable_size(buf1, sizeof(buf1),
319                                              sn->vm_state_size),
320                      date_buf,
321                      clock_buf);
322     }
323 }
324
325 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
326                           ImageInfo *info)
327 {
328     char size_buf[128], dsize_buf[128];
329     if (!info->has_actual_size) {
330         snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
331     } else {
332         get_human_readable_size(dsize_buf, sizeof(dsize_buf),
333                                 info->actual_size);
334     }
335     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
336     func_fprintf(f,
337                  "image: %s\n"
338                  "file format: %s\n"
339                  "virtual size: %s (%" PRId64 " bytes)\n"
340                  "disk size: %s\n",
341                  info->filename, info->format, size_buf,
342                  info->virtual_size,
343                  dsize_buf);
344
345     if (info->has_encrypted && info->encrypted) {
346         func_fprintf(f, "encrypted: yes\n");
347     }
348
349     if (info->has_cluster_size) {
350         func_fprintf(f, "cluster_size: %" PRId64 "\n",
351                        info->cluster_size);
352     }
353
354     if (info->has_dirty_flag && info->dirty_flag) {
355         func_fprintf(f, "cleanly shut down: no\n");
356     }
357
358     if (info->has_backing_filename) {
359         func_fprintf(f, "backing file: %s", info->backing_filename);
360         if (info->has_full_backing_filename) {
361             func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
362         }
363         func_fprintf(f, "\n");
364         if (info->has_backing_filename_format) {
365             func_fprintf(f, "backing file format: %s\n",
366                          info->backing_filename_format);
367         }
368     }
369
370     if (info->has_snapshots) {
371         SnapshotInfoList *elem;
372
373         func_fprintf(f, "Snapshot list:\n");
374         bdrv_snapshot_dump(func_fprintf, f, NULL);
375         func_fprintf(f, "\n");
376
377         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
378          * we convert to the block layer's native QEMUSnapshotInfo for now.
379          */
380         for (elem = info->snapshots; elem; elem = elem->next) {
381             QEMUSnapshotInfo sn = {
382                 .vm_state_size = elem->value->vm_state_size,
383                 .date_sec = elem->value->date_sec,
384                 .date_nsec = elem->value->date_nsec,
385                 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
386                                  elem->value->vm_clock_nsec,
387             };
388
389             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
390             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
391             bdrv_snapshot_dump(func_fprintf, f, &sn);
392             func_fprintf(f, "\n");
393         }
394     }
395 }
This page took 0.046407 seconds and 4 git commands to generate.