1 From 04b6468a295871877b55fe792c566839e2afe29c Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Tue, 13 Nov 2012 11:11:38 +0100
4 Subject: [PATCH 11/41] introduce new vma archive format
6 This is a very simple archive format, see docs/specs/vma_spec.txt
8 Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
12 vma-reader.c | 799 ++++++++++++++++++++++++++++++++++++++++++++++++++++
13 vma-writer.c | 876 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14 vma.c | 582 ++++++++++++++++++++++++++++++++++++++
15 vma.h | 146 ++++++++++
16 6 files changed, 2406 insertions(+), 1 deletion(-)
17 create mode 100644 vma-reader.c
18 create mode 100644 vma-writer.c
19 create mode 100644 vma.c
20 create mode 100644 vma.h
22 diff --git a/Makefile b/Makefile
23 index 930ac27..6cedc40 100644
26 @@ -166,7 +166,7 @@ ifneq ($(wildcard config-host.mak),)
27 include $(SRC_PATH)/tests/Makefile
30 -all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
31 +all: $(DOCS) $(TOOLS) vma$(EXESUF) $(HELPERS-y) recurse-all modules
33 config-host.h: config-host.h-timestamp
34 config-host.h-timestamp: config-host.mak
35 @@ -235,6 +235,7 @@ qemu-img.o: qemu-img-cmds.h
36 qemu-img$(EXESUF): qemu-img.o $(block-obj-y) $(crypto-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a
37 qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) $(crypto-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a
38 qemu-io$(EXESUF): qemu-io.o $(block-obj-y) $(crypto-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a
39 +vma$(EXESUF): vma.o vma-reader.o $(block-obj-y) $(crypto-obj-y) $(qom-obj-y) libqemuutil.a libqemustub.a
41 qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
43 diff --git a/Makefile.objs b/Makefile.objs
44 index 77be052..97d2bcc 100644
47 @@ -14,6 +14,7 @@ block-obj-$(CONFIG_POSIX) += aio-posix.o
48 block-obj-$(CONFIG_WIN32) += aio-win32.o
50 block-obj-y += qemu-io-cmds.o
51 +block-obj-y += vma-writer.o
55 diff --git a/vma-reader.c b/vma-reader.c
57 index 0000000..bc36cba
62 + * VMA: Virtual Machine Archive
64 + * Copyright (C) 2012 Proxmox Server Solutions
67 + * Dietmar Maurer (dietmar@proxmox.com)
69 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
70 + * See the COPYING file in the top-level directory.
79 +#include <sys/types.h>
80 +#include <sys/stat.h>
83 +#include <uuid/uuid.h>
85 +#include "qemu-common.h"
86 +#include "qemu/timer.h"
87 +#include "qemu/ratelimit.h"
89 +#include "block/block.h"
91 +#define BITS_PER_LONG (sizeof(unsigned long) * CHAR_BIT)
93 +static unsigned char zero_vma_block[VMA_BLOCK_SIZE];
95 +typedef struct VmaRestoreState {
96 + BlockDriverState *bs;
98 + unsigned long *bitmap;
104 + GChecksum *md5csum;
105 + GHashTable *blob_hash;
106 + unsigned char *head_data;
107 + VmaDeviceInfo devinfo[256];
108 + VmaRestoreState rstate[256];
110 + guint8 vmstate_stream;
111 + uint32_t vmstate_clusters;
112 + /* to show restore percentage if run with -v */
114 + int64_t cluster_count;
115 + int64_t clusters_read;
116 + int clusters_read_per;
120 +g_int32_hash(gconstpointer v)
122 + return *(const uint32_t *)v;
126 +g_int32_equal(gconstpointer v1, gconstpointer v2)
128 + return *((const uint32_t *)v1) == *((const uint32_t *)v2);
131 +static int vma_reader_get_bitmap(VmaRestoreState *rstate, int64_t cluster_num)
134 + assert(rstate->bitmap);
136 + unsigned long val, idx, bit;
138 + idx = cluster_num / BITS_PER_LONG;
140 + assert(rstate->bitmap_size > idx);
142 + bit = cluster_num % BITS_PER_LONG;
143 + val = rstate->bitmap[idx];
145 + return !!(val & (1UL << bit));
148 +static void vma_reader_set_bitmap(VmaRestoreState *rstate, int64_t cluster_num,
152 + assert(rstate->bitmap);
154 + unsigned long val, idx, bit;
156 + idx = cluster_num / BITS_PER_LONG;
158 + assert(rstate->bitmap_size > idx);
160 + bit = cluster_num % BITS_PER_LONG;
161 + val = rstate->bitmap[idx];
163 + if (!(val & (1UL << bit))) {
167 + if (val & (1UL << bit)) {
168 + val &= ~(1UL << bit);
171 + rstate->bitmap[idx] = val;
174 +typedef struct VmaBlob {
180 +static const VmaBlob *get_header_blob(VmaReader *vmar, uint32_t pos)
183 + assert(vmar->blob_hash);
185 + return g_hash_table_lookup(vmar->blob_hash, &pos);
188 +static const char *get_header_str(VmaReader *vmar, uint32_t pos)
190 + const VmaBlob *blob = get_header_blob(vmar, pos);
194 + const char *res = (char *)blob->data;
195 + if (res[blob->len-1] != '\0') {
202 +safe_read(int fd, unsigned char *buf, size_t count)
207 + n = read(fd, buf, count);
208 + } while (n < 0 && errno == EINTR);
214 +full_read(int fd, unsigned char *buf, size_t len)
222 + n = safe_read(fd, buf, len);
244 +void vma_reader_destroy(VmaReader *vmar)
248 + if (vmar->fd >= 0) {
252 + if (vmar->cdata_list) {
253 + g_list_free(vmar->cdata_list);
257 + for (i = 1; i < 256; i++) {
258 + if (vmar->rstate[i].bitmap) {
259 + g_free(vmar->rstate[i].bitmap);
263 + if (vmar->md5csum) {
264 + g_checksum_free(vmar->md5csum);
267 + if (vmar->blob_hash) {
268 + g_hash_table_destroy(vmar->blob_hash);
271 + if (vmar->head_data) {
272 + g_free(vmar->head_data);
279 +static int vma_reader_read_head(VmaReader *vmar, Error **errp)
283 + assert(*errp == NULL);
285 + unsigned char md5sum[16];
289 + vmar->head_data = g_malloc(sizeof(VmaHeader));
291 + if (full_read(vmar->fd, vmar->head_data, sizeof(VmaHeader)) !=
292 + sizeof(VmaHeader)) {
293 + error_setg(errp, "can't read vma header - %s",
294 + errno ? g_strerror(errno) : "got EOF");
298 + VmaHeader *h = (VmaHeader *)vmar->head_data;
300 + if (h->magic != VMA_MAGIC) {
301 + error_setg(errp, "not a vma file - wrong magic number");
305 + uint32_t header_size = GUINT32_FROM_BE(h->header_size);
306 + int need = header_size - sizeof(VmaHeader);
308 + error_setg(errp, "wrong vma header size %d", header_size);
312 + vmar->head_data = g_realloc(vmar->head_data, header_size);
313 + h = (VmaHeader *)vmar->head_data;
315 + if (full_read(vmar->fd, vmar->head_data + sizeof(VmaHeader), need) !=
317 + error_setg(errp, "can't read vma header data - %s",
318 + errno ? g_strerror(errno) : "got EOF");
322 + memcpy(md5sum, h->md5sum, 16);
323 + memset(h->md5sum, 0, 16);
325 + g_checksum_reset(vmar->md5csum);
326 + g_checksum_update(vmar->md5csum, vmar->head_data, header_size);
328 + g_checksum_get_digest(vmar->md5csum, (guint8 *)(h->md5sum), &csize);
330 + if (memcmp(md5sum, h->md5sum, 16) != 0) {
331 + error_setg(errp, "wrong vma header chechsum");
335 + /* we can modify header data after checksum verify */
336 + h->header_size = header_size;
338 + h->version = GUINT32_FROM_BE(h->version);
339 + if (h->version != 1) {
340 + error_setg(errp, "wrong vma version %d", h->version);
344 + h->ctime = GUINT64_FROM_BE(h->ctime);
345 + h->blob_buffer_offset = GUINT32_FROM_BE(h->blob_buffer_offset);
346 + h->blob_buffer_size = GUINT32_FROM_BE(h->blob_buffer_size);
348 + uint32_t bstart = h->blob_buffer_offset + 1;
349 + uint32_t bend = h->blob_buffer_offset + h->blob_buffer_size;
351 + if (bstart <= sizeof(VmaHeader)) {
352 + error_setg(errp, "wrong vma blob buffer offset %d",
353 + h->blob_buffer_offset);
357 + if (bend > header_size) {
358 + error_setg(errp, "wrong vma blob buffer size %d/%d",
359 + h->blob_buffer_offset, h->blob_buffer_size);
363 + while ((bstart + 2) <= bend) {
364 + uint32_t size = vmar->head_data[bstart] +
365 + (vmar->head_data[bstart+1] << 8);
366 + if ((bstart + size + 2) <= bend) {
367 + VmaBlob *blob = g_new0(VmaBlob, 1);
368 + blob->start = bstart - h->blob_buffer_offset;
370 + blob->data = vmar->head_data + bstart + 2;
371 + g_hash_table_insert(vmar->blob_hash, &blob->start, blob);
373 + bstart += size + 2;
378 + for (i = 1; i < 256; i++) {
379 + VmaDeviceInfoHeader *dih = &h->dev_info[i];
380 + uint32_t devname_ptr = GUINT32_FROM_BE(dih->devname_ptr);
381 + uint64_t size = GUINT64_FROM_BE(dih->size);
382 + const char *devname = get_header_str(vmar, devname_ptr);
384 + if (size && devname) {
386 + vmar->devinfo[i].size = size;
387 + vmar->devinfo[i].devname = devname;
389 + if (strcmp(devname, "vmstate") == 0) {
390 + vmar->vmstate_stream = i;
396 + error_setg(errp, "vma does not contain data");
400 + for (i = 0; i < VMA_MAX_CONFIGS; i++) {
401 + uint32_t name_ptr = GUINT32_FROM_BE(h->config_names[i]);
402 + uint32_t data_ptr = GUINT32_FROM_BE(h->config_data[i]);
404 + if (!(name_ptr && data_ptr)) {
407 + const char *name = get_header_str(vmar, name_ptr);
408 + const VmaBlob *blob = get_header_blob(vmar, data_ptr);
410 + if (!(name && blob)) {
411 + error_setg(errp, "vma contains invalid data pointers");
415 + VmaConfigData *cdata = g_new0(VmaConfigData, 1);
416 + cdata->name = name;
417 + cdata->data = blob->data;
418 + cdata->len = blob->len;
420 + vmar->cdata_list = g_list_append(vmar->cdata_list, cdata);
426 +VmaReader *vma_reader_create(const char *filename, Error **errp)
431 + VmaReader *vmar = g_new0(VmaReader, 1);
433 + if (strcmp(filename, "-") == 0) {
436 + vmar->fd = open(filename, O_RDONLY);
439 + if (vmar->fd < 0) {
440 + error_setg(errp, "can't open file %s - %s\n", filename,
441 + g_strerror(errno));
445 + vmar->md5csum = g_checksum_new(G_CHECKSUM_MD5);
446 + if (!vmar->md5csum) {
447 + error_setg(errp, "can't allocate cmsum\n");
451 + vmar->blob_hash = g_hash_table_new_full(g_int32_hash, g_int32_equal,
454 + if (vma_reader_read_head(vmar, errp) < 0) {
462 + vma_reader_destroy(vmar);
468 +VmaHeader *vma_reader_get_header(VmaReader *vmar)
471 + assert(vmar->head_data);
473 + return (VmaHeader *)(vmar->head_data);
476 +GList *vma_reader_get_config_data(VmaReader *vmar)
479 + assert(vmar->head_data);
481 + return vmar->cdata_list;
484 +VmaDeviceInfo *vma_reader_get_device_info(VmaReader *vmar, guint8 dev_id)
489 + if (vmar->devinfo[dev_id].size && vmar->devinfo[dev_id].devname) {
490 + return &vmar->devinfo[dev_id];
496 +int vma_reader_register_bs(VmaReader *vmar, guint8 dev_id, BlockDriverState *bs,
497 + bool write_zeroes, Error **errp)
500 + assert(bs != NULL);
502 + assert(vmar->rstate[dev_id].bs == NULL);
504 + int64_t size = bdrv_getlength(bs);
505 + if (size != vmar->devinfo[dev_id].size) {
506 + error_setg(errp, "vma_reader_register_bs for stream %s failed - "
507 + "unexpected size %zd != %zd", vmar->devinfo[dev_id].devname,
508 + size, vmar->devinfo[dev_id].size);
512 + vmar->rstate[dev_id].bs = bs;
513 + vmar->rstate[dev_id].write_zeroes = write_zeroes;
515 + int64_t bitmap_size = (size/BDRV_SECTOR_SIZE) +
516 + (VMA_CLUSTER_SIZE/BDRV_SECTOR_SIZE) * BITS_PER_LONG - 1;
517 + bitmap_size /= (VMA_CLUSTER_SIZE/BDRV_SECTOR_SIZE) * BITS_PER_LONG;
519 + vmar->rstate[dev_id].bitmap_size = bitmap_size;
520 + vmar->rstate[dev_id].bitmap = g_new0(unsigned long, bitmap_size);
522 + vmar->cluster_count += size/VMA_CLUSTER_SIZE;
527 +static ssize_t safe_write(int fd, void *buf, size_t count)
532 + n = write(fd, buf, count);
533 + } while (n < 0 && errno == EINTR);
538 +static size_t full_write(int fd, void *buf, size_t len)
546 + n = safe_write(fd, buf, len);
556 + /* incomplete write ? */
563 +static int restore_write_data(VmaReader *vmar, guint8 dev_id,
564 + BlockDriverState *bs, int vmstate_fd,
565 + unsigned char *buf, int64_t sector_num,
566 + int nb_sectors, Error **errp)
570 + if (dev_id == vmar->vmstate_stream) {
571 + if (vmstate_fd >= 0) {
572 + int len = nb_sectors * BDRV_SECTOR_SIZE;
573 + int res = full_write(vmstate_fd, buf, len);
575 + error_setg(errp, "write vmstate failed %d", res);
580 + int res = bdrv_write(bs, sector_num, buf, nb_sectors);
582 + error_setg(errp, "bdrv_write to %s failed (%d)",
583 + bdrv_get_device_name(bs), res);
589 +static int restore_extent(VmaReader *vmar, unsigned char *buf,
590 + int extent_size, int vmstate_fd,
591 + bool verbose, Error **errp)
596 + VmaExtentHeader *ehead = (VmaExtentHeader *)buf;
597 + int start = VMA_EXTENT_HEADER_SIZE;
600 + for (i = 0; i < VMA_BLOCKS_PER_EXTENT; i++) {
601 + uint64_t block_info = GUINT64_FROM_BE(ehead->blockinfo[i]);
602 + uint64_t cluster_num = block_info & 0xffffffff;
603 + uint8_t dev_id = (block_info >> 32) & 0xff;
604 + uint16_t mask = block_info >> (32+16);
605 + int64_t max_sector;
611 + VmaRestoreState *rstate = &vmar->rstate[dev_id];
612 + BlockDriverState *bs = NULL;
614 + if (dev_id != vmar->vmstate_stream) {
617 + error_setg(errp, "got wrong dev id %d", dev_id);
621 + if (vma_reader_get_bitmap(rstate, cluster_num)) {
622 + error_setg(errp, "found duplicated cluster %zd for stream %s",
623 + cluster_num, vmar->devinfo[dev_id].devname);
626 + vma_reader_set_bitmap(rstate, cluster_num, 1);
628 + max_sector = vmar->devinfo[dev_id].size/BDRV_SECTOR_SIZE;
630 + max_sector = G_MAXINT64;
631 + if (cluster_num != vmar->vmstate_clusters) {
632 + error_setg(errp, "found out of order vmstate data");
635 + vmar->vmstate_clusters++;
638 + vmar->clusters_read++;
641 + time_t duration = time(NULL) - vmar->start_time;
642 + int percent = (vmar->clusters_read*100)/vmar->cluster_count;
643 + if (percent != vmar->clusters_read_per) {
644 + printf("progress %d%% (read %zd bytes, duration %zd sec)\n",
645 + percent, vmar->clusters_read*VMA_CLUSTER_SIZE,
648 + vmar->clusters_read_per = percent;
652 + /* try to write whole clusters to speedup restore */
653 + if (mask == 0xffff) {
654 + if ((start + VMA_CLUSTER_SIZE) > extent_size) {
655 + error_setg(errp, "short vma extent - too many blocks");
658 + int64_t sector_num = (cluster_num * VMA_CLUSTER_SIZE) /
660 + int64_t end_sector = sector_num +
661 + VMA_CLUSTER_SIZE/BDRV_SECTOR_SIZE;
663 + if (end_sector > max_sector) {
664 + end_sector = max_sector;
667 + if (end_sector <= sector_num) {
668 + error_setg(errp, "got wrong block address - write bejond end");
672 + int nb_sectors = end_sector - sector_num;
673 + if (restore_write_data(vmar, dev_id, bs, vmstate_fd, buf + start,
674 + sector_num, nb_sectors, errp) < 0) {
678 + start += VMA_CLUSTER_SIZE;
683 + for (j = 0; j < 16; j++) {
684 + int64_t sector_num = (cluster_num*VMA_CLUSTER_SIZE +
685 + j*VMA_BLOCK_SIZE)/BDRV_SECTOR_SIZE;
687 + int64_t end_sector = sector_num +
688 + VMA_BLOCK_SIZE/BDRV_SECTOR_SIZE;
689 + if (end_sector > max_sector) {
690 + end_sector = max_sector;
694 + if ((start + VMA_BLOCK_SIZE) > extent_size) {
695 + error_setg(errp, "short vma extent - too many blocks");
699 + if (end_sector <= sector_num) {
700 + error_setg(errp, "got wrong block address - "
701 + "write bejond end");
705 + int nb_sectors = end_sector - sector_num;
706 + if (restore_write_data(vmar, dev_id, bs, vmstate_fd,
707 + buf + start, sector_num,
708 + nb_sectors, errp) < 0) {
712 + start += VMA_BLOCK_SIZE;
716 + if (rstate->write_zeroes && (end_sector > sector_num)) {
717 + /* Todo: use bdrv_co_write_zeroes (but that need to
718 + * be run inside coroutine?)
720 + int nb_sectors = end_sector - sector_num;
721 + if (restore_write_data(vmar, dev_id, bs, vmstate_fd,
722 + zero_vma_block, sector_num,
723 + nb_sectors, errp) < 0) {
734 + if (start != extent_size) {
735 + error_setg(errp, "vma extent error - missing blocks");
742 +int vma_reader_restore(VmaReader *vmar, int vmstate_fd, bool verbose,
746 + assert(vmar->head_data);
749 + unsigned char buf[VMA_MAX_EXTENT_SIZE];
751 + unsigned char md5sum[16];
752 + VmaHeader *h = (VmaHeader *)vmar->head_data;
754 + vmar->start_time = time(NULL);
757 + int bytes = full_read(vmar->fd, buf + buf_pos, sizeof(buf) - buf_pos);
759 + error_setg(errp, "read failed - %s", g_strerror(errno));
769 + if (buf_pos < VMA_EXTENT_HEADER_SIZE) {
770 + error_setg(errp, "read short extent (%d bytes)", buf_pos);
774 + VmaExtentHeader *ehead = (VmaExtentHeader *)buf;
776 + /* extract md5sum */
777 + memcpy(md5sum, ehead->md5sum, sizeof(ehead->md5sum));
778 + memset(ehead->md5sum, 0, sizeof(ehead->md5sum));
780 + g_checksum_reset(vmar->md5csum);
781 + g_checksum_update(vmar->md5csum, buf, VMA_EXTENT_HEADER_SIZE);
783 + g_checksum_get_digest(vmar->md5csum, ehead->md5sum, &csize);
785 + if (memcmp(md5sum, ehead->md5sum, 16) != 0) {
786 + error_setg(errp, "wrong vma extent header chechsum");
790 + if (memcmp(h->uuid, ehead->uuid, sizeof(ehead->uuid)) != 0) {
791 + error_setg(errp, "wrong vma extent uuid");
795 + if (ehead->magic != VMA_EXTENT_MAGIC || ehead->reserved1 != 0) {
796 + error_setg(errp, "wrong vma extent header magic");
800 + int block_count = GUINT16_FROM_BE(ehead->block_count);
801 + int extent_size = VMA_EXTENT_HEADER_SIZE + block_count*VMA_BLOCK_SIZE;
803 + if (buf_pos < extent_size) {
804 + error_setg(errp, "short vma extent (%d < %d)", buf_pos,
809 + if (restore_extent(vmar, buf, extent_size, vmstate_fd, verbose,
814 + if (buf_pos > extent_size) {
815 + memmove(buf, buf + extent_size, buf_pos - extent_size);
816 + buf_pos = buf_pos - extent_size;
825 + for (i = 1; i < 256; i++) {
826 + VmaRestoreState *rstate = &vmar->rstate[i];
831 + if (bdrv_flush(rstate->bs) < 0) {
832 + error_setg(errp, "vma bdrv_flush %s failed",
833 + vmar->devinfo[i].devname);
837 + if (vmar->devinfo[i].size &&
838 + (strcmp(vmar->devinfo[i].devname, "vmstate") != 0)) {
839 + assert(rstate->bitmap);
841 + int64_t cluster_num, end;
843 + end = (vmar->devinfo[i].size + VMA_CLUSTER_SIZE - 1) /
846 + for (cluster_num = 0; cluster_num < end; cluster_num++) {
847 + if (!vma_reader_get_bitmap(rstate, cluster_num)) {
848 + error_setg(errp, "detected missing cluster %zd "
849 + "for stream %s", cluster_num,
850 + vmar->devinfo[i].devname);
860 diff --git a/vma-writer.c b/vma-writer.c
862 index 0000000..8a3fa1c
867 + * VMA: Virtual Machine Archive
869 + * Copyright (C) 2012 Proxmox Server Solutions
872 + * Dietmar Maurer (dietmar@proxmox.com)
874 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
875 + * See the COPYING file in the top-level directory.
884 +#include <sys/types.h>
885 +#include <sys/stat.h>
888 +#include <uuid/uuid.h>
890 +#include "qemu-common.h"
892 +#include "block/block.h"
893 +#include "monitor/monitor.h"
894 +#include "qemu/main-loop.h"
898 +#define DPRINTF(fmt, ...)\
899 + do { if (DEBUG_VMA) { printf("vma: " fmt, ## __VA_ARGS__); } } while (0)
901 +#define WRITE_BUFFERS 5
903 +typedef struct VmaAIOCB VmaAIOCB;
905 + unsigned char buffer[VMA_MAX_EXTENT_SIZE];
917 + bool header_written;
920 + /* we always write extents */
921 + unsigned char outbuf[VMA_MAX_EXTENT_SIZE];
922 + int outbuf_pos; /* in bytes */
923 + int outbuf_count; /* in VMA_BLOCKS */
924 + uint64_t outbuf_block_info[VMA_BLOCKS_PER_EXTENT];
926 + VmaAIOCB *aiocbs[WRITE_BUFFERS];
929 + GChecksum *md5csum;
930 + CoMutex writer_lock;
931 + CoMutex flush_lock;
932 + Coroutine *co_writer;
934 + /* drive informations */
935 + VmaStreamInfo stream_info[256];
936 + guint stream_count;
938 + guint8 vmstate_stream;
939 + uint32_t vmstate_clusters;
941 + /* header blob table */
942 + char *header_blob_table;
943 + uint32_t header_blob_table_size;
944 + uint32_t header_blob_table_pos;
946 + /* store for config blobs */
947 + uint32_t config_names[VMA_MAX_CONFIGS]; /* offset into blob_buffer table */
948 + uint32_t config_data[VMA_MAX_CONFIGS]; /* offset into blob_buffer table */
949 + uint32_t config_count;
952 +void vma_writer_set_error(VmaWriter *vmaw, const char *fmt, ...)
956 + if (vmaw->status < 0) {
963 + g_vsnprintf(vmaw->errmsg, sizeof(vmaw->errmsg), fmt, ap);
966 + DPRINTF("vma_writer_set_error: %s\n", vmaw->errmsg);
969 +static uint32_t allocate_header_blob(VmaWriter *vmaw, const char *data,
976 + if (!vmaw->header_blob_table ||
977 + (vmaw->header_blob_table_size <
978 + (vmaw->header_blob_table_pos + len + 2))) {
979 + int newsize = vmaw->header_blob_table_size + ((len + 2 + 511)/512)*512;
981 + vmaw->header_blob_table = g_realloc(vmaw->header_blob_table, newsize);
982 + memset(vmaw->header_blob_table + vmaw->header_blob_table_size,
983 + 0, newsize - vmaw->header_blob_table_size);
984 + vmaw->header_blob_table_size = newsize;
987 + uint32_t cpos = vmaw->header_blob_table_pos;
988 + vmaw->header_blob_table[cpos] = len & 255;
989 + vmaw->header_blob_table[cpos+1] = (len >> 8) & 255;
990 + memcpy(vmaw->header_blob_table + cpos + 2, data, len);
991 + vmaw->header_blob_table_pos += len + 2;
995 +static uint32_t allocate_header_string(VmaWriter *vmaw, const char *str)
999 + size_t len = strlen(str) + 1;
1001 + return allocate_header_blob(vmaw, str, len);
1004 +int vma_writer_add_config(VmaWriter *vmaw, const char *name, gpointer data,
1008 + assert(!vmaw->header_written);
1009 + assert(vmaw->config_count < VMA_MAX_CONFIGS);
1014 + gchar *basename = g_path_get_basename(name);
1015 + uint32_t name_ptr = allocate_header_string(vmaw, basename);
1022 + uint32_t data_ptr = allocate_header_blob(vmaw, data, len);
1027 + vmaw->config_names[vmaw->config_count] = name_ptr;
1028 + vmaw->config_data[vmaw->config_count] = data_ptr;
1030 + vmaw->config_count++;
1035 +int vma_writer_register_stream(VmaWriter *vmaw, const char *devname,
1040 + assert(!vmaw->status);
1042 + if (vmaw->header_written) {
1043 + vma_writer_set_error(vmaw, "vma_writer_register_stream: header "
1044 + "already written");
1048 + guint n = vmaw->stream_count + 1;
1050 + /* we can have dev_ids form 1 to 255 (0 reserved)
1051 + * 255(-1) reseverd for safety
1054 + vma_writer_set_error(vmaw, "vma_writer_register_stream: "
1055 + "too many drives");
1060 + vma_writer_set_error(vmaw, "vma_writer_register_stream: "
1061 + "got strange size %zd", size);
1065 + DPRINTF("vma_writer_register_stream %s %zu %d\n", devname, size, n);
1067 + vmaw->stream_info[n].devname = g_strdup(devname);
1068 + vmaw->stream_info[n].size = size;
1070 + vmaw->stream_info[n].cluster_count = (size + VMA_CLUSTER_SIZE - 1) /
1073 + vmaw->stream_count = n;
1075 + if (strcmp(devname, "vmstate") == 0) {
1076 + vmaw->vmstate_stream = n;
1082 +static void vma_co_continue_write(void *opaque)
1084 + VmaWriter *vmaw = opaque;
1086 + DPRINTF("vma_co_continue_write\n");
1087 + qemu_coroutine_enter(vmaw->co_writer, NULL);
1090 +static ssize_t coroutine_fn
1091 +vma_co_write(VmaWriter *vmaw, const void *buf, size_t bytes)
1096 + /* atomic writes (we cannot interleave writes) */
1097 + qemu_co_mutex_lock(&vmaw->writer_lock);
1099 + DPRINTF("vma_co_write enter %zd\n", bytes);
1101 + assert(vmaw->co_writer == NULL);
1103 + vmaw->co_writer = qemu_coroutine_self();
1105 + aio_set_fd_handler(qemu_get_aio_context(), vmaw->fd, NULL, vma_co_continue_write, vmaw);
1107 + DPRINTF("vma_co_write wait until writable\n");
1108 + qemu_coroutine_yield();
1109 + DPRINTF("vma_co_write starting %zd\n", bytes);
1111 + while (done < bytes) {
1112 + ret = write(vmaw->fd, buf + done, bytes - done);
1115 + DPRINTF("vma_co_write written %zd %zd\n", done, ret);
1116 + } else if (ret < 0) {
1117 + if (errno == EAGAIN || errno == EWOULDBLOCK) {
1118 + DPRINTF("vma_co_write yield %zd\n", done);
1119 + qemu_coroutine_yield();
1120 + DPRINTF("vma_co_write restart %zd\n", done);
1122 + vma_writer_set_error(vmaw, "vma_co_write write error - %s",
1123 + g_strerror(errno));
1124 + done = -1; /* always return failure for partial writes */
1127 + } else if (ret == 0) {
1128 + /* should not happen - simply try again */
1132 + aio_set_fd_handler(qemu_get_aio_context(), vmaw->fd, NULL, NULL, NULL);
1134 + vmaw->co_writer = NULL;
1136 + qemu_co_mutex_unlock(&vmaw->writer_lock);
1138 + DPRINTF("vma_co_write leave %zd\n", done);
1142 +static void coroutine_fn vma_co_writer_task(void *opaque)
1144 + VmaAIOCB *cb = opaque;
1146 + DPRINTF("vma_co_writer_task start\n");
1148 + int64_t done = vma_co_write(cb->vmaw, cb->buffer, cb->bytes);
1149 + DPRINTF("vma_co_writer_task write done %zd\n", done);
1151 + if (done != cb->bytes) {
1152 + DPRINTF("vma_co_writer_task failed write %zd %zd", cb->bytes, done);
1153 + vma_writer_set_error(cb->vmaw, "vma_co_writer_task failed write %zd",
1159 + qemu_co_queue_next(&cb->vmaw->wqueue);
1161 + DPRINTF("vma_co_writer_task end\n");
1164 +static void coroutine_fn vma_queue_flush(VmaWriter *vmaw)
1166 + DPRINTF("vma_queue_flush enter\n");
1172 + VmaAIOCB *cb = NULL;
1173 + for (i = 0; i < WRITE_BUFFERS; i++) {
1174 + if (vmaw->aiocbs[i]->bytes) {
1175 + cb = vmaw->aiocbs[i];
1176 + DPRINTF("FOUND USED AIO BUFFER %d %zd\n", i,
1177 + vmaw->aiocbs[i]->bytes);
1184 + qemu_co_queue_wait(&vmaw->wqueue);
1187 + DPRINTF("vma_queue_flush leave\n");
1191 + * NOTE: pipe buffer size in only 4096 bytes on linux (see 'ulimit -a')
1192 + * So we need to create a coroutione to allow 'parallel' execution.
1194 +static ssize_t coroutine_fn
1195 +vma_queue_write(VmaWriter *vmaw, const void *buf, size_t bytes)
1197 + DPRINTF("vma_queue_write enter %zd\n", bytes);
1201 + assert(bytes <= VMA_MAX_EXTENT_SIZE);
1203 + VmaAIOCB *cb = NULL;
1206 + for (i = 0; i < WRITE_BUFFERS; i++) {
1207 + if (!vmaw->aiocbs[i]->bytes) {
1208 + cb = vmaw->aiocbs[i];
1213 + qemu_co_queue_wait(&vmaw->wqueue);
1217 + memcpy(cb->buffer, buf, bytes);
1218 + cb->bytes = bytes;
1221 + DPRINTF("vma_queue_write start %zd\n", bytes);
1222 + cb->co = qemu_coroutine_create(vma_co_writer_task);
1223 + qemu_coroutine_enter(cb->co, cb);
1225 + DPRINTF("vma_queue_write leave\n");
1230 +VmaWriter *vma_writer_create(const char *filename, uuid_t uuid, Error **errp)
1234 + assert(sizeof(VmaHeader) == (4096 + 8192));
1235 + assert(G_STRUCT_OFFSET(VmaHeader, config_names) == 2044);
1236 + assert(G_STRUCT_OFFSET(VmaHeader, config_data) == 3068);
1237 + assert(G_STRUCT_OFFSET(VmaHeader, dev_info) == 4096);
1238 + assert(sizeof(VmaExtentHeader) == 512);
1240 + VmaWriter *vmaw = g_new0(VmaWriter, 1);
1243 + vmaw->md5csum = g_checksum_new(G_CHECKSUM_MD5);
1244 + if (!vmaw->md5csum) {
1245 + error_setg(errp, "can't allocate cmsum\n");
1249 + if (strstart(filename, "exec:", &p)) {
1250 + vmaw->cmd = popen(p, "w");
1251 + if (vmaw->cmd == NULL) {
1252 + error_setg(errp, "can't popen command '%s' - %s\n", p,
1253 + g_strerror(errno));
1256 + vmaw->fd = fileno(vmaw->cmd);
1258 + /* try to use O_NONBLOCK and O_DIRECT */
1259 + fcntl(vmaw->fd, F_SETFL, fcntl(vmaw->fd, F_GETFL)|O_NONBLOCK);
1260 + fcntl(vmaw->fd, F_SETFL, fcntl(vmaw->fd, F_GETFL)|O_DIRECT);
1265 + const char *tmp_id_str;
1267 + if ((stat(filename, &st) == 0) && S_ISFIFO(st.st_mode)) {
1268 + oflags = O_NONBLOCK|O_DIRECT|O_WRONLY;
1269 + vmaw->fd = qemu_open(filename, oflags, 0644);
1270 + } else if (strstart(filename, "/dev/fdset/", &tmp_id_str)) {
1271 + oflags = O_NONBLOCK|O_DIRECT|O_WRONLY;
1272 + vmaw->fd = qemu_open(filename, oflags, 0644);
1273 + } else if (strstart(filename, "/dev/fdname/", &tmp_id_str)) {
1274 + vmaw->fd = monitor_get_fd(cur_mon, tmp_id_str, errp);
1275 + if (vmaw->fd < 0) {
1278 + /* try to use O_NONBLOCK and O_DIRECT */
1279 + fcntl(vmaw->fd, F_SETFL, fcntl(vmaw->fd, F_GETFL)|O_NONBLOCK);
1280 + fcntl(vmaw->fd, F_SETFL, fcntl(vmaw->fd, F_GETFL)|O_DIRECT);
1282 + oflags = O_NONBLOCK|O_DIRECT|O_WRONLY|O_CREAT|O_EXCL;
1283 + vmaw->fd = qemu_open(filename, oflags, 0644);
1286 + if (vmaw->fd < 0) {
1287 + error_setg(errp, "can't open file %s - %s\n", filename,
1288 + g_strerror(errno));
1293 + /* we use O_DIRECT, so we need to align IO buffers */
1295 + for (i = 0; i < WRITE_BUFFERS; i++) {
1296 + vmaw->aiocbs[i] = qemu_memalign(512, sizeof(VmaAIOCB));
1297 + memset(vmaw->aiocbs[i], 0, sizeof(VmaAIOCB));
1300 + vmaw->outbuf_count = 0;
1301 + vmaw->outbuf_pos = VMA_EXTENT_HEADER_SIZE;
1303 + vmaw->header_blob_table_pos = 1; /* start at pos 1 */
1305 + qemu_co_mutex_init(&vmaw->writer_lock);
1306 + qemu_co_mutex_init(&vmaw->flush_lock);
1307 + qemu_co_queue_init(&vmaw->wqueue);
1309 + uuid_copy(vmaw->uuid, uuid);
1316 + pclose(vmaw->cmd);
1317 + } else if (vmaw->fd >= 0) {
1321 + if (vmaw->md5csum) {
1322 + g_checksum_free(vmaw->md5csum);
1331 +static int coroutine_fn vma_write_header(VmaWriter *vmaw)
1334 + int header_clusters = 8;
1335 + char buf[65536*header_clusters];
1336 + VmaHeader *head = (VmaHeader *)buf;
1340 + DPRINTF("VMA WRITE HEADER\n");
1342 + if (vmaw->status < 0) {
1343 + return vmaw->status;
1346 + memset(buf, 0, sizeof(buf));
1348 + head->magic = VMA_MAGIC;
1349 + head->version = GUINT32_TO_BE(1); /* v1 */
1350 + memcpy(head->uuid, vmaw->uuid, 16);
1352 + time_t ctime = time(NULL);
1353 + head->ctime = GUINT64_TO_BE(ctime);
1355 + if (!vmaw->stream_count) {
1359 + for (i = 0; i < VMA_MAX_CONFIGS; i++) {
1360 + head->config_names[i] = GUINT32_TO_BE(vmaw->config_names[i]);
1361 + head->config_data[i] = GUINT32_TO_BE(vmaw->config_data[i]);
1364 + /* 32 bytes per device (12 used currently) = 8192 bytes max */
1365 + for (i = 1; i <= 254; i++) {
1366 + VmaStreamInfo *si = &vmaw->stream_info[i];
1368 + assert(si->devname);
1369 + uint32_t devname_ptr = allocate_header_string(vmaw, si->devname);
1370 + if (!devname_ptr) {
1373 + head->dev_info[i].devname_ptr = GUINT32_TO_BE(devname_ptr);
1374 + head->dev_info[i].size = GUINT64_TO_BE(si->size);
1378 + uint32_t header_size = sizeof(VmaHeader) + vmaw->header_blob_table_size;
1379 + head->header_size = GUINT32_TO_BE(header_size);
1381 + if (header_size > sizeof(buf)) {
1382 + return -1; /* just to be sure */
1385 + uint32_t blob_buffer_offset = sizeof(VmaHeader);
1386 + memcpy(buf + blob_buffer_offset, vmaw->header_blob_table,
1387 + vmaw->header_blob_table_size);
1388 + head->blob_buffer_offset = GUINT32_TO_BE(blob_buffer_offset);
1389 + head->blob_buffer_size = GUINT32_TO_BE(vmaw->header_blob_table_pos);
1391 + g_checksum_reset(vmaw->md5csum);
1392 + g_checksum_update(vmaw->md5csum, (const guchar *)buf, header_size);
1394 + g_checksum_get_digest(vmaw->md5csum, (guint8 *)(head->md5sum), &csize);
1396 + return vma_queue_write(vmaw, buf, header_size);
1399 +static int coroutine_fn vma_writer_flush(VmaWriter *vmaw)
1406 + if (vmaw->status < 0) {
1407 + return vmaw->status;
1410 + if (!vmaw->header_written) {
1411 + vmaw->header_written = true;
1412 + ret = vma_write_header(vmaw);
1414 + vma_writer_set_error(vmaw, "vma_writer_flush: write header failed");
1419 + DPRINTF("VMA WRITE FLUSH %d %d\n", vmaw->outbuf_count, vmaw->outbuf_pos);
1422 + VmaExtentHeader *ehead = (VmaExtentHeader *)vmaw->outbuf;
1424 + ehead->magic = VMA_EXTENT_MAGIC;
1425 + ehead->reserved1 = 0;
1427 + for (i = 0; i < VMA_BLOCKS_PER_EXTENT; i++) {
1428 + ehead->blockinfo[i] = GUINT64_TO_BE(vmaw->outbuf_block_info[i]);
1431 + guint16 block_count = (vmaw->outbuf_pos - VMA_EXTENT_HEADER_SIZE) /
1434 + ehead->block_count = GUINT16_TO_BE(block_count);
1436 + memcpy(ehead->uuid, vmaw->uuid, sizeof(ehead->uuid));
1437 + memset(ehead->md5sum, 0, sizeof(ehead->md5sum));
1439 + g_checksum_reset(vmaw->md5csum);
1440 + g_checksum_update(vmaw->md5csum, vmaw->outbuf, VMA_EXTENT_HEADER_SIZE);
1442 + g_checksum_get_digest(vmaw->md5csum, ehead->md5sum, &csize);
1444 + int bytes = vmaw->outbuf_pos;
1445 + ret = vma_queue_write(vmaw, vmaw->outbuf, bytes);
1446 + if (ret != bytes) {
1447 + vma_writer_set_error(vmaw, "vma_writer_flush: failed write");
1450 + vmaw->outbuf_count = 0;
1451 + vmaw->outbuf_pos = VMA_EXTENT_HEADER_SIZE;
1453 + for (i = 0; i < VMA_BLOCKS_PER_EXTENT; i++) {
1454 + vmaw->outbuf_block_info[i] = 0;
1457 + return vmaw->status;
1460 +static int vma_count_open_streams(VmaWriter *vmaw)
1462 + g_assert(vmaw != NULL);
1465 + int open_drives = 0;
1466 + for (i = 0; i <= 255; i++) {
1467 + if (vmaw->stream_info[i].size && !vmaw->stream_info[i].finished) {
1472 + return open_drives;
1476 + * all jobs should call this when there is no more data
1477 + * Returns: number of remaining stream (0 ==> finished)
1480 +vma_writer_close_stream(VmaWriter *vmaw, uint8_t dev_id)
1482 + g_assert(vmaw != NULL);
1484 + DPRINTF("vma_writer_set_status %d\n", dev_id);
1485 + if (!vmaw->stream_info[dev_id].size) {
1486 + vma_writer_set_error(vmaw, "vma_writer_close_stream: "
1487 + "no such stream %d", dev_id);
1490 + if (vmaw->stream_info[dev_id].finished) {
1491 + vma_writer_set_error(vmaw, "vma_writer_close_stream: "
1492 + "stream already closed %d", dev_id);
1496 + vmaw->stream_info[dev_id].finished = true;
1498 + int open_drives = vma_count_open_streams(vmaw);
1500 + if (open_drives <= 0) {
1501 + DPRINTF("vma_writer_set_status all drives completed\n");
1502 + qemu_co_mutex_lock(&vmaw->flush_lock);
1503 + int ret = vma_writer_flush(vmaw);
1504 + qemu_co_mutex_unlock(&vmaw->flush_lock);
1506 + vma_writer_set_error(vmaw, "vma_writer_close_stream: flush failed");
1510 + return open_drives;
1513 +int vma_writer_get_status(VmaWriter *vmaw, VmaStatus *status)
1517 + g_assert(vmaw != NULL);
1520 + status->status = vmaw->status;
1521 + g_strlcpy(status->errmsg, vmaw->errmsg, sizeof(status->errmsg));
1522 + for (i = 0; i <= 255; i++) {
1523 + status->stream_info[i] = vmaw->stream_info[i];
1526 + uuid_unparse_lower(vmaw->uuid, status->uuid_str);
1529 + status->closed = vmaw->closed;
1531 + return vmaw->status;
1534 +static int vma_writer_get_buffer(VmaWriter *vmaw)
1538 + qemu_co_mutex_lock(&vmaw->flush_lock);
1540 + /* wait until buffer is available */
1541 + while (vmaw->outbuf_count >= (VMA_BLOCKS_PER_EXTENT - 1)) {
1542 + ret = vma_writer_flush(vmaw);
1544 + vma_writer_set_error(vmaw, "vma_writer_get_buffer: flush failed");
1549 + qemu_co_mutex_unlock(&vmaw->flush_lock);
1555 +int64_t coroutine_fn
1556 +vma_writer_write(VmaWriter *vmaw, uint8_t dev_id, int64_t cluster_num,
1557 + unsigned char *buf, size_t *zero_bytes)
1559 + g_assert(vmaw != NULL);
1560 + g_assert(zero_bytes != NULL);
1564 + if (vmaw->status < 0) {
1565 + return vmaw->status;
1568 + if (!dev_id || !vmaw->stream_info[dev_id].size) {
1569 + vma_writer_set_error(vmaw, "vma_writer_write: "
1570 + "no such stream %d", dev_id);
1574 + if (vmaw->stream_info[dev_id].finished) {
1575 + vma_writer_set_error(vmaw, "vma_writer_write: "
1576 + "stream already closed %d", dev_id);
1581 + if (cluster_num >= (((uint64_t)1)<<32)) {
1582 + vma_writer_set_error(vmaw, "vma_writer_write: "
1583 + "cluster number out of range");
1587 + if (dev_id == vmaw->vmstate_stream) {
1588 + if (cluster_num != vmaw->vmstate_clusters) {
1589 + vma_writer_set_error(vmaw, "vma_writer_write: "
1590 + "non sequential vmstate write");
1592 + vmaw->vmstate_clusters++;
1593 + } else if (cluster_num >= vmaw->stream_info[dev_id].cluster_count) {
1594 + vma_writer_set_error(vmaw, "vma_writer_write: cluster number too big");
1598 + /* wait until buffer is available */
1599 + if (vma_writer_get_buffer(vmaw) < 0) {
1600 + vma_writer_set_error(vmaw, "vma_writer_write: "
1601 + "vma_writer_get_buffer failed");
1605 + DPRINTF("VMA WRITE %d %zd\n", dev_id, cluster_num);
1607 + uint16_t mask = 0;
1612 + for (i = 0; i < 16; i++) {
1613 + unsigned char *vmablock = buf + (i*VMA_BLOCK_SIZE);
1614 + if (!buffer_is_zero(vmablock, VMA_BLOCK_SIZE)) {
1616 + memcpy(vmaw->outbuf + vmaw->outbuf_pos, vmablock,
1618 + vmaw->outbuf_pos += VMA_BLOCK_SIZE;
1620 + DPRINTF("VMA WRITE %zd ZERO BLOCK %d\n", cluster_num, i);
1621 + vmaw->stream_info[dev_id].zero_bytes += VMA_BLOCK_SIZE;
1622 + *zero_bytes += VMA_BLOCK_SIZE;
1628 + DPRINTF("VMA WRITE %zd ZERO CLUSTER\n", cluster_num);
1629 + vmaw->stream_info[dev_id].zero_bytes += VMA_CLUSTER_SIZE;
1630 + *zero_bytes += VMA_CLUSTER_SIZE;
1633 + uint64_t block_info = ((uint64_t)mask) << (32+16);
1634 + block_info |= ((uint64_t)dev_id) << 32;
1635 + block_info |= (cluster_num & 0xffffffff);
1636 + vmaw->outbuf_block_info[vmaw->outbuf_count] = block_info;
1638 + DPRINTF("VMA WRITE MASK %zd %zx\n", cluster_num, block_info);
1640 + vmaw->outbuf_count++;
1642 + /** NOTE: We allways write whole clusters, but we correctly set
1643 + * transferred bytes. So transferred == size when when everything
1646 + size_t transferred = VMA_CLUSTER_SIZE;
1648 + if (dev_id != vmaw->vmstate_stream) {
1649 + uint64_t last = (cluster_num + 1) * VMA_CLUSTER_SIZE;
1650 + if (last > vmaw->stream_info[dev_id].size) {
1651 + uint64_t diff = last - vmaw->stream_info[dev_id].size;
1652 + if (diff >= VMA_CLUSTER_SIZE) {
1653 + vma_writer_set_error(vmaw, "vma_writer_write: "
1654 + "read after last cluster");
1657 + transferred -= diff;
1661 + vmaw->stream_info[dev_id].transferred += transferred;
1663 + return transferred;
1666 +int vma_writer_close(VmaWriter *vmaw, Error **errp)
1668 + g_assert(vmaw != NULL);
1672 + vma_queue_flush(vmaw);
1674 + /* this should not happen - just to be sure */
1675 + while (!qemu_co_queue_empty(&vmaw->wqueue)) {
1676 + DPRINTF("vma_writer_close wait\n");
1677 + co_aio_sleep_ns(qemu_get_aio_context(), QEMU_CLOCK_REALTIME, 1000000);
1681 + if (pclose(vmaw->cmd) < 0) {
1682 + vma_writer_set_error(vmaw, "vma_writer_close: "
1683 + "pclose failed - %s", g_strerror(errno));
1686 + if (close(vmaw->fd) < 0) {
1687 + vma_writer_set_error(vmaw, "vma_writer_close: "
1688 + "close failed - %s", g_strerror(errno));
1692 + for (i = 0; i <= 255; i++) {
1693 + VmaStreamInfo *si = &vmaw->stream_info[i];
1695 + if (!si->finished) {
1696 + vma_writer_set_error(vmaw, "vma_writer_close: "
1697 + "detected open stream '%s'", si->devname);
1698 + } else if ((si->transferred != si->size) &&
1699 + (i != vmaw->vmstate_stream)) {
1700 + vma_writer_set_error(vmaw, "vma_writer_close: "
1701 + "incomplete stream '%s' (%zd != %zd)",
1702 + si->devname, si->transferred, si->size);
1707 + for (i = 0; i <= 255; i++) {
1708 + vmaw->stream_info[i].finished = 1; /* mark as closed */
1713 + if (vmaw->status < 0 && *errp == NULL) {
1714 + error_setg(errp, "%s", vmaw->errmsg);
1717 + return vmaw->status;
1720 +void vma_writer_destroy(VmaWriter *vmaw)
1726 + for (i = 0; i <= 255; i++) {
1727 + if (vmaw->stream_info[i].devname) {
1728 + g_free(vmaw->stream_info[i].devname);
1732 + if (vmaw->md5csum) {
1733 + g_checksum_free(vmaw->md5csum);
1736 + for (i = 0; i < WRITE_BUFFERS; i++) {
1737 + free(vmaw->aiocbs[i]);
1742 diff --git a/vma.c b/vma.c
1743 new file mode 100644
1744 index 0000000..86c117b
1749 + * VMA: Virtual Machine Archive
1751 + * Copyright (C) 2012-2013 Proxmox Server Solutions
1754 + * Dietmar Maurer (dietmar@proxmox.com)
1756 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
1757 + * See the COPYING file in the top-level directory.
1763 +#include <unistd.h>
1765 +#include <string.h>
1766 +#include <sys/types.h>
1767 +#include <sys/stat.h>
1772 +#include "qemu-common.h"
1773 +#include "qemu/error-report.h"
1774 +#include "qemu/main-loop.h"
1776 +static void help(void)
1778 + const char *help_msg =
1779 + "usage: vma command [command options]\n"
1781 + "vma list <filename>\n"
1782 + "vma create <filename> [-c config] <archive> pathname ...\n"
1783 + "vma extract <filename> [-r <fifo>] <targetdir>\n"
1786 + printf("%s", help_msg);
1790 +static const char *extract_devname(const char *path, char **devname, int index)
1794 + const char *sep = strchr(path, '=');
1797 + *devname = g_strndup(path, sep - path);
1801 + *devname = g_strdup_printf("disk%d", index);
1810 +static void print_content(VmaReader *vmar)
1814 + VmaHeader *head = vma_reader_get_header(vmar);
1816 + GList *l = vma_reader_get_config_data(vmar);
1817 + while (l && l->data) {
1818 + VmaConfigData *cdata = (VmaConfigData *)l->data;
1819 + l = g_list_next(l);
1820 + printf("CFG: size: %d name: %s\n", cdata->len, cdata->name);
1824 + VmaDeviceInfo *di;
1825 + for (i = 1; i < 255; i++) {
1826 + di = vma_reader_get_device_info(vmar, i);
1828 + if (strcmp(di->devname, "vmstate") == 0) {
1829 + printf("VMSTATE: dev_id=%d memory: %zd\n", i, di->size);
1831 + printf("DEV: dev_id=%d size: %zd devname: %s\n",
1832 + i, di->size, di->devname);
1836 + /* ctime is the last entry we print */
1837 + printf("CTIME: %s", ctime(&head->ctime));
1841 +static int list_content(int argc, char **argv)
1844 + const char *filename;
1847 + c = getopt(argc, argv, "h");
1857 + g_assert_not_reached();
1861 + /* Get the filename */
1862 + if ((optind + 1) != argc) {
1865 + filename = argv[optind++];
1867 + Error *errp = NULL;
1868 + VmaReader *vmar = vma_reader_create(filename, &errp);
1871 + g_error("%s", error_get_pretty(errp));
1874 + print_content(vmar);
1876 + vma_reader_destroy(vmar);
1881 +typedef struct RestoreMap {
1887 +static int extract_content(int argc, char **argv)
1891 + const char *filename;
1892 + const char *dirname;
1893 + const char *readmap = NULL;
1896 + c = getopt(argc, argv, "hvr:");
1916 + /* Get the filename */
1917 + if ((optind + 2) != argc) {
1920 + filename = argv[optind++];
1921 + dirname = argv[optind++];
1923 + Error *errp = NULL;
1924 + VmaReader *vmar = vma_reader_create(filename, &errp);
1927 + g_error("%s", error_get_pretty(errp));
1930 + if (mkdir(dirname, 0777) < 0) {
1931 + g_error("unable to create target directory %s - %s",
1932 + dirname, g_strerror(errno));
1935 + GList *l = vma_reader_get_config_data(vmar);
1936 + while (l && l->data) {
1937 + VmaConfigData *cdata = (VmaConfigData *)l->data;
1938 + l = g_list_next(l);
1939 + char *cfgfn = g_strdup_printf("%s/%s", dirname, cdata->name);
1940 + GError *err = NULL;
1941 + if (!g_file_set_contents(cfgfn, (gchar *)cdata->data, cdata->len,
1943 + g_error("unable to write file: %s", err->message);
1947 + GHashTable *devmap = g_hash_table_new(g_str_hash, g_str_equal);
1950 + print_content(vmar);
1952 + FILE *map = fopen(readmap, "r");
1954 + g_error("unable to open fifo %s - %s", readmap, g_strerror(errno));
1959 + char *line = fgets(inbuf, sizeof(inbuf), map);
1960 + if (!line || line[0] == '\0' || !strcmp(line, "done\n")) {
1963 + int len = strlen(line);
1964 + if (line[len - 1] == '\n') {
1965 + line[len - 1] = '\0';
1973 + if (line[0] == '0' && line[1] == ':') {
1975 + write_zero = false;
1976 + } else if (line[0] == '1' && line[1] == ':') {
1978 + write_zero = true;
1980 + g_error("read map failed - parse error ('%s')", inbuf);
1983 + char *devname = NULL;
1984 + path = extract_devname(path, &devname, -1);
1986 + g_error("read map failed - no dev name specified ('%s')",
1990 + RestoreMap *map = g_new0(RestoreMap, 1);
1991 + map->devname = g_strdup(devname);
1992 + map->path = g_strdup(path);
1993 + map->write_zero = write_zero;
1995 + g_hash_table_insert(devmap, map->devname, map);
2001 + int vmstate_fd = -1;
2002 + guint8 vmstate_stream = 0;
2004 + for (i = 1; i < 255; i++) {
2005 + VmaDeviceInfo *di = vma_reader_get_device_info(vmar, i);
2006 + if (di && (strcmp(di->devname, "vmstate") == 0)) {
2007 + vmstate_stream = i;
2008 + char *statefn = g_strdup_printf("%s/vmstate.bin", dirname);
2009 + vmstate_fd = open(statefn, O_WRONLY|O_CREAT|O_EXCL, 0644);
2010 + if (vmstate_fd < 0) {
2011 + g_error("create vmstate file '%s' failed - %s", statefn,
2012 + g_strerror(errno));
2016 + char *devfn = NULL;
2017 + int flags = BDRV_O_RDWR|BDRV_O_CACHE_WB;
2018 + bool write_zero = true;
2022 + map = (RestoreMap *)g_hash_table_lookup(devmap, di->devname);
2023 + if (map == NULL) {
2024 + g_error("no device name mapping for %s", di->devname);
2026 + devfn = map->path;
2027 + write_zero = map->write_zero;
2029 + devfn = g_strdup_printf("%s/tmp-disk-%s.raw",
2030 + dirname, di->devname);
2031 + printf("DEVINFO %s %zd\n", devfn, di->size);
2033 + bdrv_img_create(devfn, "raw", NULL, NULL, NULL, di->size,
2036 + g_error("can't create file %s: %s", devfn,
2037 + error_get_pretty(errp));
2040 + /* Note: we created an empty file above, so there is no
2041 + * need to write zeroes (so we generate a sparse file)
2043 + write_zero = false;
2046 + BlockDriverState *bs = bdrv_new();
2047 + if (errp || bdrv_open(&bs, devfn, NULL, NULL, flags, NULL, &errp)) {
2048 + g_error("can't open file %s - %s", devfn,
2049 + error_get_pretty(errp));
2051 + if (vma_reader_register_bs(vmar, i, bs, write_zero, &errp) < 0) {
2052 + g_error("%s", error_get_pretty(errp));
2061 + if (vma_reader_restore(vmar, vmstate_fd, verbose, &errp) < 0) {
2062 + g_error("restore failed - %s", error_get_pretty(errp));
2066 + for (i = 1; i < 255; i++) {
2067 + VmaDeviceInfo *di = vma_reader_get_device_info(vmar, i);
2068 + if (di && (i != vmstate_stream)) {
2069 + char *tmpfn = g_strdup_printf("%s/tmp-disk-%s.raw",
2070 + dirname, di->devname);
2071 + char *fn = g_strdup_printf("%s/disk-%s.raw",
2072 + dirname, di->devname);
2073 + if (rename(tmpfn, fn) != 0) {
2074 + g_error("rename %s to %s failed - %s",
2075 + tmpfn, fn, g_strerror(errno));
2081 + vma_reader_destroy(vmar);
2088 +typedef struct BackupJob {
2089 + BlockDriverState *bs;
2095 +#define BACKUP_SECTORS_PER_CLUSTER (VMA_CLUSTER_SIZE / BDRV_SECTOR_SIZE)
2097 +static void coroutine_fn backup_run(void *opaque)
2099 + BackupJob *job = (BackupJob *)opaque;
2101 + QEMUIOVector qiov;
2103 + int64_t start, end;
2106 + unsigned char *buf = qemu_blockalign(job->bs, VMA_CLUSTER_SIZE);
2109 + end = DIV_ROUND_UP(job->len / BDRV_SECTOR_SIZE,
2110 + BACKUP_SECTORS_PER_CLUSTER);
2112 + for (; start < end; start++) {
2113 + iov.iov_base = buf;
2114 + iov.iov_len = VMA_CLUSTER_SIZE;
2115 + qemu_iovec_init_external(&qiov, &iov, 1);
2117 + ret = bdrv_co_readv(job->bs, start * BACKUP_SECTORS_PER_CLUSTER,
2118 + BACKUP_SECTORS_PER_CLUSTER, &qiov);
2120 + vma_writer_set_error(job->vmaw, "read error", -1);
2125 + if (vma_writer_write(job->vmaw, job->dev_id, start, buf, &zb) < 0) {
2126 + vma_writer_set_error(job->vmaw, "backup_dump_cb vma_writer_write failed", -1);
2133 + if (vma_writer_close_stream(job->vmaw, job->dev_id) <= 0) {
2134 + Error *err = NULL;
2135 + if (vma_writer_close(job->vmaw, &err) != 0) {
2136 + g_warning("vma_writer_close failed %s", error_get_pretty(err));
2141 +static int create_archive(int argc, char **argv)
2145 + const char *archivename;
2146 + GList *config_files = NULL;
2149 + c = getopt(argc, argv, "hvc:");
2159 + config_files = g_list_append(config_files, optarg);
2165 + g_assert_not_reached();
2170 + /* make sure we have archive name and at least one path */
2171 + if ((optind + 2) > argc) {
2175 + archivename = argv[optind++];
2178 + uuid_generate(uuid);
2180 + Error *local_err = NULL;
2181 + VmaWriter *vmaw = vma_writer_create(archivename, uuid, &local_err);
2183 + if (vmaw == NULL) {
2184 + g_error("%s", error_get_pretty(local_err));
2187 + GList *l = config_files;
2188 + while (l && l->data) {
2189 + char *name = l->data;
2190 + char *cdata = NULL;
2192 + GError *err = NULL;
2193 + if (!g_file_get_contents(name, &cdata, &clen, &err)) {
2194 + unlink(archivename);
2195 + g_error("Unable to read file: %s", err->message);
2198 + if (vma_writer_add_config(vmaw, name, cdata, clen) != 0) {
2199 + unlink(archivename);
2200 + g_error("Unable to append config data %s (len = %zd)",
2203 + l = g_list_next(l);
2207 + while (optind < argc) {
2208 + const char *path = argv[optind++];
2209 + char *devname = NULL;
2210 + path = extract_devname(path, &devname, ind++);
2212 + BlockDriver *drv = NULL;
2213 + Error *errp = NULL;
2214 + BlockDriverState *bs = bdrv_new();
2216 + res = bdrv_open(&bs, path, NULL, NULL, BDRV_O_CACHE_WB , drv, &errp);
2218 + unlink(archivename);
2219 + g_error("bdrv_open '%s' failed - %s", path, error_get_pretty(errp));
2221 + int64_t size = bdrv_getlength(bs);
2222 + int dev_id = vma_writer_register_stream(vmaw, devname, size);
2223 + if (dev_id <= 0) {
2224 + unlink(archivename);
2225 + g_error("vma_writer_register_stream '%s' failed", devname);
2228 + BackupJob *job = g_new0(BackupJob, 1);
2232 + job->dev_id = dev_id;
2234 + Coroutine *co = qemu_coroutine_create(backup_run);
2235 + qemu_coroutine_enter(co, job);
2238 + VmaStatus vmastat;
2240 + int last_percent = -1;
2243 + main_loop_wait(false);
2244 + vma_writer_get_status(vmaw, &vmastat);
2248 + uint64_t total = 0;
2249 + uint64_t transferred = 0;
2250 + uint64_t zero_bytes = 0;
2253 + for (i = 0; i < 256; i++) {
2254 + if (vmastat.stream_info[i].size) {
2255 + total += vmastat.stream_info[i].size;
2256 + transferred += vmastat.stream_info[i].transferred;
2257 + zero_bytes += vmastat.stream_info[i].zero_bytes;
2260 + percent = (transferred*100)/total;
2261 + if (percent != last_percent) {
2262 + fprintf(stderr, "progress %d%% %zd/%zd %zd\n", percent,
2263 + transferred, total, zero_bytes);
2266 + last_percent = percent;
2270 + if (vmastat.closed) {
2277 + vma_writer_get_status(vmaw, &vmastat);
2280 + for (i = 0; i < 256; i++) {
2281 + VmaStreamInfo *si = &vmastat.stream_info[i];
2283 + fprintf(stderr, "image %s: size=%zd zeros=%zd saved=%zd\n",
2284 + si->devname, si->size, si->zero_bytes,
2285 + si->size - si->zero_bytes);
2290 + if (vmastat.status < 0) {
2291 + unlink(archivename);
2292 + g_error("creating vma archive failed");
2298 +int main(int argc, char **argv)
2300 + const char *cmdname;
2301 + Error *main_loop_err = NULL;
2303 + error_set_progname(argv[0]);
2305 + if (qemu_init_main_loop(&main_loop_err)) {
2306 + g_error("%s", error_get_pretty(main_loop_err));
2315 + cmdname = argv[1];
2319 + if (!strcmp(cmdname, "list")) {
2320 + return list_content(argc, argv);
2321 + } else if (!strcmp(cmdname, "create")) {
2322 + return create_archive(argc, argv);
2323 + } else if (!strcmp(cmdname, "extract")) {
2324 + return extract_content(argc, argv);
2330 diff --git a/vma.h b/vma.h
2331 new file mode 100644
2332 index 0000000..6625eb9
2337 + * VMA: Virtual Machine Archive
2339 + * Copyright (C) Proxmox Server Solutions
2342 + * Dietmar Maurer (dietmar@proxmox.com)
2344 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
2345 + * See the COPYING file in the top-level directory.
2349 +#ifndef BACKUP_VMA_H
2350 +#define BACKUP_VMA_H
2352 +#include <uuid/uuid.h>
2353 +#include "qapi/error.h"
2354 +#include "block/block.h"
2356 +#define VMA_BLOCK_BITS 12
2357 +#define VMA_BLOCK_SIZE (1<<VMA_BLOCK_BITS)
2358 +#define VMA_CLUSTER_BITS (VMA_BLOCK_BITS+4)
2359 +#define VMA_CLUSTER_SIZE (1<<VMA_CLUSTER_BITS)
2361 +#if VMA_CLUSTER_SIZE != 65536
2362 +#error unexpected cluster size
2365 +#define VMA_EXTENT_HEADER_SIZE 512
2366 +#define VMA_BLOCKS_PER_EXTENT 59
2367 +#define VMA_MAX_CONFIGS 256
2369 +#define VMA_MAX_EXTENT_SIZE \
2370 + (VMA_EXTENT_HEADER_SIZE+VMA_CLUSTER_SIZE*VMA_BLOCKS_PER_EXTENT)
2371 +#if VMA_MAX_EXTENT_SIZE != 3867136
2372 +#error unexpected VMA_EXTENT_SIZE
2375 +/* File Format Definitions */
2377 +#define VMA_MAGIC (GUINT32_TO_BE(('V'<<24)|('M'<<16)|('A'<<8)|0x00))
2378 +#define VMA_EXTENT_MAGIC (GUINT32_TO_BE(('V'<<24)|('M'<<16)|('A'<<8)|'E'))
2380 +typedef struct VmaDeviceInfoHeader {
2381 + uint32_t devname_ptr; /* offset into blob_buffer table */
2382 + uint32_t reserved0;
2383 + uint64_t size; /* device size in bytes */
2384 + uint64_t reserved1;
2385 + uint64_t reserved2;
2386 +} VmaDeviceInfoHeader;
2388 +typedef struct VmaHeader {
2391 + unsigned char uuid[16];
2393 + unsigned char md5sum[16];
2395 + uint32_t blob_buffer_offset;
2396 + uint32_t blob_buffer_size;
2397 + uint32_t header_size;
2399 + unsigned char reserved[1984];
2401 + uint32_t config_names[VMA_MAX_CONFIGS]; /* offset into blob_buffer table */
2402 + uint32_t config_data[VMA_MAX_CONFIGS]; /* offset into blob_buffer table */
2404 + uint32_t reserved1;
2406 + VmaDeviceInfoHeader dev_info[256];
2409 +typedef struct VmaExtentHeader {
2411 + uint16_t reserved1;
2412 + uint16_t block_count;
2413 + unsigned char uuid[16];
2414 + unsigned char md5sum[16];
2415 + uint64_t blockinfo[VMA_BLOCKS_PER_EXTENT];
2418 +/* functions/definitions to read/write vma files */
2420 +typedef struct VmaReader VmaReader;
2422 +typedef struct VmaWriter VmaWriter;
2424 +typedef struct VmaConfigData {
2430 +typedef struct VmaStreamInfo {
2432 + uint64_t cluster_count;
2433 + uint64_t transferred;
2434 + uint64_t zero_bytes;
2439 +typedef struct VmaStatus {
2442 + char errmsg[8192];
2443 + char uuid_str[37];
2444 + VmaStreamInfo stream_info[256];
2447 +typedef struct VmaDeviceInfo {
2448 + uint64_t size; /* device size in bytes */
2449 + const char *devname;
2452 +VmaWriter *vma_writer_create(const char *filename, uuid_t uuid, Error **errp);
2453 +int vma_writer_close(VmaWriter *vmaw, Error **errp);
2454 +void vma_writer_destroy(VmaWriter *vmaw);
2455 +int vma_writer_add_config(VmaWriter *vmaw, const char *name, gpointer data,
2457 +int vma_writer_register_stream(VmaWriter *vmaw, const char *devname,
2460 +int64_t coroutine_fn vma_writer_write(VmaWriter *vmaw, uint8_t dev_id,
2461 + int64_t cluster_num, unsigned char *buf,
2462 + size_t *zero_bytes);
2464 +int coroutine_fn vma_writer_close_stream(VmaWriter *vmaw, uint8_t dev_id);
2466 +int vma_writer_get_status(VmaWriter *vmaw, VmaStatus *status);
2467 +void vma_writer_set_error(VmaWriter *vmaw, const char *fmt, ...);
2470 +VmaReader *vma_reader_create(const char *filename, Error **errp);
2471 +void vma_reader_destroy(VmaReader *vmar);
2472 +VmaHeader *vma_reader_get_header(VmaReader *vmar);
2473 +GList *vma_reader_get_config_data(VmaReader *vmar);
2474 +VmaDeviceInfo *vma_reader_get_device_info(VmaReader *vmar, guint8 dev_id);
2475 +int vma_reader_register_bs(VmaReader *vmar, guint8 dev_id,
2476 + BlockDriverState *bs, bool write_zeroes,
2478 +int vma_reader_restore(VmaReader *vmar, int vmstate_fd, bool verbose,
2481 +#endif /* BACKUP_VMA_H */