[pve-devel] [PATCH 3/5] introduce new vma archive format
Dietmar Maurer
dietmar at proxmox.com
Mon Nov 19 12:29:30 CET 2012
Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
Makefile | 3 +-
docs/specs/vma_spec.txt | 24 ++
vma-reader.c | 708 +++++++++++++++++++++++++++++++++++++++++
vma-writer.c | 802 +++++++++++++++++++++++++++++++++++++++++++++++
vma.c | 512 ++++++++++++++++++++++++++++++
vma.h | 137 ++++++++
6 files changed, 2185 insertions(+), 1 deletions(-)
create mode 100644 docs/specs/vma_spec.txt
create mode 100644 vma-reader.c
create mode 100644 vma-writer.c
create mode 100644 vma.c
create mode 100644 vma.h
diff --git a/Makefile b/Makefile
index 81c660f..f69789f 100644
--- a/Makefile
+++ b/Makefile
@@ -100,7 +100,7 @@ defconfig:
-include config-all-devices.mak
-all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all
+all: $(DOCS) $(TOOLS) vma$(EXESUF) $(HELPERS-y) recurse-all
config-host.h: config-host.h-timestamp
config-host.h-timestamp: config-host.mak
@@ -186,6 +186,7 @@ tools-obj-$(CONFIG_POSIX) += compatfd.o
qemu-img$(EXESUF): qemu-img.o $(tools-obj-y) $(block-obj-y)
qemu-nbd$(EXESUF): qemu-nbd.o $(tools-obj-y) $(block-obj-y)
qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y)
+vma$(EXESUF): vma.o vma-writer.o vma-reader.o $(tools-obj-y) $(block-obj-y)
qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
diff --git a/docs/specs/vma_spec.txt b/docs/specs/vma_spec.txt
new file mode 100644
index 0000000..28eb292
--- /dev/null
+++ b/docs/specs/vma_spec.txt
@@ -0,0 +1,24 @@
+=Virtual Machine Archive format (VMA)=
+
+This format contains a header which includes the VM configuration as
+binary blobs, and a list of devices (dev_id, name).
+
+The actual VM image date is stored inside extends. En extend contains
+up to 64 clusters, and start with a 512 byte header containing
+additional information for those clusters.
+
+We use a cluster size of 65536, and use 8 bytes for each
+cluster in the header to store the following information:
+
+* 1 byte dev_id (to identity the drive)
+* 2 bytes zero indicator (mark zero regions (16x4069))
+* 4 bytes cluster number
+* 1 byte not used (reserved)
+
+We only store non-zero blocks (such block is 4096 bytes).
+
+Each archive is marked with an unique uuid. The archive header and all
+extend headers includes that uuid and a MD5 checksum (over header
+data).
+
+
diff --git a/vma-reader.c b/vma-reader.c
new file mode 100644
index 0000000..4e818bd
--- /dev/null
+++ b/vma-reader.c
@@ -0,0 +1,708 @@
+/*
+ * VMA: Virtual Machine Archive
+ *
+ * Copyright (C) Proxmox Server Solutions
+ *
+ * Authors:
+ * Dietmar Maurer (dietmar at proxmox.com)
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+/*
+ TODOs:
+
+*/
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <glib.h>
+#include <uuid/uuid.h>
+
+#include "qemu-common.h"
+#include "qemu_socket.h"
+#include "qemu-coroutine.h"
+#include "qemu-aio.h"
+#include "qemu/ratelimit.h"
+#include "vma.h"
+#include "block.h"
+
+/* TODO:
+
+ check if we restored all data (use bitmaps?)
+
+ */
+
+#define BITS_PER_LONG (sizeof(unsigned long) * 8)
+
+static unsigned char zero_vma_block[VMA_BLOCK_SIZE];
+
+typedef struct VmaRestoreState {
+ BlockDriverState *bs;
+ bool write_zeroes;
+ unsigned long *bitmap;
+ int bitmap_size;
+} VmaRestoreState;
+
+struct VmaReader {
+ int fd;
+ GChecksum *md5csum;
+ GHashTable *blob_hash;
+ unsigned char *head_data;
+ VmaDeviceInfo devinfo[256];
+ VmaRestoreState rstate[256];
+ GList *cdata_list;
+};
+
+static guint
+g_int32_hash (gconstpointer v)
+{
+ return *(const uint32_t *) v;
+}
+
+static gboolean
+g_int32_equal (gconstpointer v1,
+ gconstpointer v2)
+{
+ return *((const uint32_t*) v1) == *((const uint32_t*) v2);
+}
+
+static int vma_reader_get_bitmap(VmaRestoreState *rstate, int64_t cluster_num)
+{
+ assert(rstate);
+ assert(rstate->bitmap);
+
+ unsigned long val, idx, bit;
+
+ idx = cluster_num / BITS_PER_LONG;
+
+ assert(rstate->bitmap_size > idx);
+
+ bit = cluster_num % BITS_PER_LONG;
+ val = rstate->bitmap[idx];
+
+ return !!(val & (1UL << bit));
+}
+
+static void vma_reader_set_bitmap(VmaRestoreState *rstate,int64_t cluster_num,
+ int dirty)
+{
+ assert(rstate);
+ assert(rstate->bitmap);
+
+ unsigned long val, idx, bit;
+
+ idx = cluster_num / BITS_PER_LONG;
+
+ assert(rstate->bitmap_size > idx);
+
+ bit = cluster_num % BITS_PER_LONG;
+ val = rstate->bitmap[idx];
+ if (dirty) {
+ if (!(val & (1UL << bit))) {
+ val |= 1UL << bit;
+ }
+ } else {
+ if (val & (1UL << bit)) {
+ val &= ~(1UL << bit);
+ }
+ }
+ rstate->bitmap[idx] = val;
+}
+
+typedef struct VmaBlob {
+ uint32_t start;
+ uint32_t len;
+ unsigned char *data;
+} VmaBlob;
+
+static const VmaBlob *get_header_blob(VmaReader *vmar, uint32_t pos)
+{
+ assert(vmar);
+ assert(vmar->blob_hash);
+
+ return g_hash_table_lookup(vmar->blob_hash, &pos);
+}
+
+static const unsigned char *get_header_str(VmaReader *vmar, uint32_t pos)
+{
+ const VmaBlob *blob = get_header_blob(vmar, pos);
+ if (!blob || (blob->data[blob->len-1] != '\0')) {
+ return NULL;
+ }
+ return blob->data;
+}
+
+static ssize_t
+safe_read(int fd, unsigned char *buf, size_t count)
+{
+ ssize_t n;
+
+ do {
+ n = read(fd, buf, count);
+ } while (n < 0 && errno == EINTR);
+
+ return n;
+}
+
+static ssize_t
+full_read(int fd, unsigned char *buf, size_t len)
+{
+ ssize_t n;
+ size_t total;
+
+ total = 0;
+
+ while (len > 0) {
+ n = safe_read(fd, buf, len);
+
+ if (n == 0)
+ return total;
+
+ if (n <= 0)
+ break;
+
+ buf += n;
+ total += n;
+ len -= n;
+ }
+
+ if (len) {
+ return -1;
+ }
+
+ return total;
+}
+
+void vma_reader_destroy(VmaReader *vmar)
+{
+ assert(vmar);
+
+ if (vmar->fd >= 0) {
+ close(vmar->fd);
+ }
+
+ if (vmar->cdata_list) {
+ g_list_free(vmar->cdata_list);
+ }
+
+ int i;
+ for (i = 1; i < 256; i++) {
+ if (vmar->rstate[i].bitmap) {
+ g_free(vmar->rstate[i].bitmap);
+ }
+ }
+
+ if (vmar->md5csum)
+ g_checksum_free(vmar->md5csum);
+
+ if (vmar->blob_hash)
+ g_hash_table_destroy(vmar->blob_hash);
+
+ if (vmar->head_data)
+ g_free(vmar->head_data);
+
+ g_free(vmar);
+
+};
+
+static int vma_reader_read_head(VmaReader *vmar, Error **errp)
+{
+ assert(vmar);
+ assert(errp);
+ assert(*errp == NULL);
+
+ unsigned char md5sum[16];
+ int i;
+ int ret = 0;
+
+ vmar->head_data = g_malloc(sizeof(VmaHeader));
+
+ if (full_read(vmar->fd, vmar->head_data, sizeof(VmaHeader)) != sizeof(VmaHeader)) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "can't read vma header - %s", errno ? strerror(errno) : "got EOF");
+ return -1;
+ }
+
+ VmaHeader *h = (VmaHeader *)vmar->head_data;
+
+ if (h->magic != VMA_MAGIC) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "not a vma file - wrong magic number");
+ return -1;
+ }
+
+ uint32_t header_size = GUINT32_FROM_BE(h->header_size);
+ int need = header_size - sizeof(VmaHeader);
+ if (need <= 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "wrong vma header size %d", header_size);
+ return -1;
+ }
+
+ vmar->head_data = g_realloc(vmar->head_data, header_size);
+ h = (VmaHeader *)vmar->head_data;
+
+ if (full_read(vmar->fd, vmar->head_data + sizeof(VmaHeader), need) != need) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "can't read vma header data - %s", errno ? strerror(errno) : "got EOF");
+ return -1;
+ }
+
+ memcpy(md5sum, h->md5sum, 16);
+ memset(h->md5sum, 0, 16);
+
+ g_checksum_reset(vmar->md5csum);
+ g_checksum_update(vmar->md5csum, vmar->head_data, header_size);
+ gsize csize = 16;
+ g_checksum_get_digest(vmar->md5csum, (guint8 *)(h->md5sum), &csize);
+
+ if (memcmp(md5sum, h->md5sum, 16) != 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "wrong vma header chechsum");
+ return -1;
+ }
+
+ /* we can modify header data after checksum verify */
+ h->header_size = header_size;
+
+ h->version = GUINT32_FROM_BE(h->version);
+ if (h->version != 1) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "wrong vma version %d", h->version);
+ return -1;
+ }
+
+ h->ctime = GUINT64_FROM_BE(h->ctime);
+ h->blob_buffer_offset = GUINT32_FROM_BE(h->blob_buffer_offset);
+ h->blob_buffer_size = GUINT32_FROM_BE(h->blob_buffer_size);
+
+ uint32_t bstart = h->blob_buffer_offset + 1;
+ uint32_t bend = h->blob_buffer_offset + h->blob_buffer_size;
+
+ if (bstart <= sizeof(VmaHeader)) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "wrong vma blob buffer offset %d", h->blob_buffer_offset);
+ return -1;
+ }
+
+ if (bend > header_size) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "wrong vma blob buffer size %d/%d", h->blob_buffer_offset,
+ h->blob_buffer_size);
+ return -1;
+ }
+
+ while ((bstart + 2) <= bend) {
+ uint32_t size = vmar->head_data[bstart] + (vmar->head_data[bstart+1] << 8);
+ if ((bstart + size + 2 ) <= bend) {
+ VmaBlob *blob = g_new0(VmaBlob, 1);
+ blob->start = bstart - h->blob_buffer_offset;
+ blob->len = size;
+ blob->data = vmar->head_data + bstart + 2;
+ g_hash_table_insert(vmar->blob_hash, &blob->start, blob);
+ }
+ bstart += size + 2;
+ }
+
+
+ int count = 0;
+ for (i = 1; i < 256; i++) {
+ VmaDeviceInfoHeader *dih = &h->dev_info[i];
+ uint32_t devname_ptr = GUINT32_FROM_BE(dih->devname_ptr);
+ uint64_t size = GUINT64_FROM_BE(dih->size);
+ const unsigned char *devname = get_header_str(vmar, devname_ptr);
+
+ if (size && devname) {
+ count++;
+ //printf("FOUND DRIVE %s/%d %zd bytes\n", devname, i, size);
+ vmar->devinfo[i].size = size;
+ vmar->devinfo[i].devname = devname;
+ }
+ }
+
+ if (!count) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "vma does not contain data");
+ return -1;
+ }
+
+ for (i = 0; i < VMA_MAX_CONFIGS; i++) {
+ uint32_t name_ptr = GUINT32_FROM_BE(h->config_names[i]);
+ uint32_t data_ptr = GUINT32_FROM_BE(h->config_data[i]);
+
+ if (!(name_ptr && data_ptr)) {
+ continue;
+ }
+ const unsigned char *name = get_header_str(vmar, name_ptr);
+ const VmaBlob *blob = get_header_blob(vmar, data_ptr);
+
+ if (!(name && blob)) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "vma contains invalid data pointers");
+ return -1;
+ }
+
+ VmaConfigData *cdata = g_new0(VmaConfigData, 1);
+ cdata->name = name;
+ cdata->data = blob->data;
+ cdata->len = blob->len;
+
+ vmar->cdata_list = g_list_append(vmar->cdata_list, cdata);
+
+ //printf("FOUND CONFIG BLOB %s len = %d\n", name, blob->len);
+
+ }
+
+ return ret;
+};
+
+VmaReader *vma_reader_create(const char *filename, Error **errp)
+{
+ assert(filename);
+ assert(errp);
+
+ VmaReader *vmar = g_new0(VmaReader, 1);
+
+ vmar->fd = -1;
+
+ if ((vmar->fd = open(filename, O_RDONLY)) < 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "can't open file %s - %s\n", filename, strerror(errno));
+ goto err;
+ }
+
+ vmar->md5csum = g_checksum_new(G_CHECKSUM_MD5);
+ if (!vmar->md5csum) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "can't allocate cmsum\n");
+ goto err;
+ }
+
+ vmar->blob_hash = g_hash_table_new_full(g_int32_hash, g_int32_equal, NULL, g_free);
+
+ if (vma_reader_read_head(vmar, errp) < 0) {
+ goto err;
+ }
+
+ return vmar;
+
+err:
+ if (vmar) {
+ vma_reader_destroy(vmar);
+ }
+
+ return NULL;
+}
+
+VmaHeader *vma_reader_get_header(VmaReader *vmar)
+{
+ assert(vmar);
+ assert(vmar->head_data);
+
+ return (VmaHeader *)(vmar->head_data);
+}
+
+GList *vma_reader_get_config_data(VmaReader *vmar)
+{
+ assert(vmar);
+ assert(vmar->head_data);
+
+ return vmar->cdata_list;
+}
+
+VmaDeviceInfo *vma_reader_get_device_info(VmaReader *vmar, guint8 dev_id)
+{
+ assert(vmar);
+ assert(dev_id);
+
+ if (vmar->devinfo[dev_id].size && vmar->devinfo[dev_id].devname) {
+ return &vmar->devinfo[dev_id];
+ }
+
+ return NULL;
+}
+
+int vma_reader_register_bs(VmaReader *vmar, guint8 dev_id, BlockDriverState *bs,
+ bool write_zeroes, Error **errp)
+{
+ assert(vmar);
+ assert(bs != NULL);
+ assert(dev_id);
+ assert(vmar->rstate[dev_id].bs == NULL);
+
+ int64_t size = bdrv_getlength(bs);
+ if (size != vmar->devinfo[dev_id].size) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "vma_reader_register_bs for stream %s failed - unexpected size %zd != %zd",
+ vmar->devinfo[dev_id].devname, size, vmar->devinfo[dev_id].size);
+ return -1;
+ }
+
+ vmar->rstate[dev_id].bs = bs;
+ vmar->rstate[dev_id].write_zeroes = write_zeroes;
+
+ int64_t bitmap_size = (size/BDRV_SECTOR_SIZE) +
+ (VMA_CLUSTER_SIZE/BDRV_SECTOR_SIZE) * BITS_PER_LONG - 1;
+ bitmap_size /= (VMA_CLUSTER_SIZE/BDRV_SECTOR_SIZE) * BITS_PER_LONG;
+
+ vmar->rstate[dev_id].bitmap_size = bitmap_size;
+ vmar->rstate[dev_id].bitmap = g_new0(unsigned long, bitmap_size);
+
+ return 0;
+}
+
+static int restore_extend(VmaReader *vmar, unsigned char *buf, int extend_size, Error **errp)
+{
+ assert(vmar);
+ assert(buf);
+
+ VmaExtendHeader *ehead = (VmaExtendHeader *)buf;
+ int start = VMA_EXTEND_HEADER_SIZE;
+ int i;
+
+ for (i = 0; i < VMA_BLOCKS_PER_EXTEND; i++) {
+ uint64_t block_info = GUINT64_FROM_BE(ehead->blockinfo[i]);
+ uint32_t cluster_num = block_info & 0xffffffff;
+ uint8_t dev_id = (block_info >> 32) & 0xff;
+ uint16_t mask = block_info >> (32+16);
+
+ if (!dev_id) {
+ continue;
+ }
+
+ printf("BLOCK_INFO %d %u %x %zx\n", dev_id, cluster_num, mask, block_info);
+
+ VmaRestoreState *rstate = &vmar->rstate[dev_id];
+ BlockDriverState *bs = rstate->bs;
+ if (!bs) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "got wrong dev id %d", dev_id);
+ return -1;
+ }
+
+ if (vma_reader_get_bitmap(rstate, cluster_num)) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "found duplicated cluster %d for stream %s", cluster_num,
+ vmar->devinfo[dev_id].devname);
+ return -1;
+ }
+
+ vma_reader_set_bitmap(rstate, cluster_num, 1);
+
+ int64_t max_sector = vmar->devinfo[dev_id].size/BDRV_SECTOR_SIZE;
+
+ // try to write whole clusters to speedup restore
+ if (mask == 0xffff) {
+ if ((start + VMA_CLUSTER_SIZE) > extend_size) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "short vma extend - too many blocks");
+ return -1;
+ }
+ int64_t sector_num = (cluster_num * VMA_CLUSTER_SIZE) / BDRV_SECTOR_SIZE;
+ int64_t end_sector = sector_num + VMA_CLUSTER_SIZE/BDRV_SECTOR_SIZE;
+
+ if (end_sector > max_sector) {
+ end_sector = max_sector;
+ }
+
+ if (end_sector <= sector_num) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "got wrong block address - write bejond end");
+ return -1;
+ }
+
+ int res;
+ if ((res = bdrv_write(bs, sector_num, buf + start, end_sector - sector_num)) < 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "bdrv_write to %s failed (%d)", bdrv_get_device_name(bs), res);
+ return -1;
+ }
+ start += VMA_CLUSTER_SIZE;
+ } else {
+ int j;
+ int bit = 1;
+
+ for (j = 0; j < 16; j++) {
+ int64_t sector_num = (cluster_num * VMA_CLUSTER_SIZE + j * VMA_BLOCK_SIZE) /
+ BDRV_SECTOR_SIZE;
+
+ int64_t end_sector = sector_num + VMA_BLOCK_SIZE/BDRV_SECTOR_SIZE;
+ if (end_sector > max_sector) {
+ end_sector = max_sector;
+ }
+
+ int res;
+ if (mask & bit) {
+ if ((start + VMA_BLOCK_SIZE) > extend_size) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "short vma extend - too many blocks");
+ return -1;
+ }
+
+ if (end_sector <= sector_num) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "got wrong block address - write bejond end");
+ return -1;
+ } else {
+ if ((res = bdrv_write(bs, sector_num, buf + start,
+ end_sector - sector_num)) < 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "bdrv_write to %s failed (%d)", bdrv_get_device_name(bs), res);
+ return -1;
+ }
+ }
+
+ start += VMA_BLOCK_SIZE;
+
+ } else {
+
+ if (rstate->write_zeroes & (end_sector > sector_num)) {
+ /* Todo: use bdrv_co_write_zeroes (bu that need to be run
+ * inside coroutine?)
+ */
+ if ((res = bdrv_write(bs, sector_num, zero_vma_block,
+ end_sector - sector_num)) < 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "bdrv_write zeros to %s failed (%d)",
+ bdrv_get_device_name(bs), res);
+ return -1;
+ }
+ }
+ }
+
+ bit = bit << 1;
+ }
+ }
+ }
+
+ if (start != extend_size) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "vma extend error - missing blocks");
+ return -1;
+ }
+
+ return 0;
+}
+
+int vma_reader_restore(VmaReader *vmar, Error **errp)
+{
+ assert(vmar);
+ assert(vmar->head_data);
+
+ int ret = 0;
+ unsigned char buf[VMA_MAX_EXTEND_SIZE];
+ int buf_pos = 0;
+ unsigned char md5sum[16];
+ VmaHeader *h = (VmaHeader *)vmar->head_data;
+
+
+ while(1) {
+ int bytes = full_read(vmar->fd, buf + buf_pos, sizeof(buf) - buf_pos);
+ if (bytes < 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "read failed - %s", strerror(errno));
+ return -1;
+ }
+ printf("READ %d bytes\n", bytes);
+
+ buf_pos += bytes;
+
+ if (!buf_pos) {
+ break; /* EOF */
+ }
+
+ if (buf_pos < VMA_EXTEND_HEADER_SIZE) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "read short extend (%d bytes)", buf_pos);
+ return -1;
+ }
+
+ VmaExtendHeader *ehead = (VmaExtendHeader *)buf;
+
+ /* extract md5sum */
+ memcpy(md5sum, ehead->md5sum, sizeof(ehead->md5sum));
+ memset(ehead->md5sum, 0, sizeof(ehead->md5sum));
+
+ g_checksum_reset(vmar->md5csum);
+ g_checksum_update(vmar->md5csum, buf, VMA_EXTEND_HEADER_SIZE);
+ gsize csize = 16;
+ g_checksum_get_digest(vmar->md5csum, ehead->md5sum, &csize);
+
+ if (memcmp(md5sum, ehead->md5sum, 16) != 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "wrong vma extend header chechsum");
+ return -1;
+ }
+
+ if (memcmp(h->uuid, ehead->uuid, sizeof(ehead->uuid)) != 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "wrong vma extend uuid");
+ return -1;
+ }
+
+ if (ehead->magic != VMA_EXTEND_MAGIC || ehead->reserved1 != 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "wrong vma extend header magic");
+ return -1;
+ }
+
+ int block_count = GUINT16_FROM_BE(ehead->block_count);
+ int extend_size = VMA_EXTEND_HEADER_SIZE + block_count*VMA_BLOCK_SIZE;
+
+ if (buf_pos < extend_size) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "short vma extend (%d < %d)",
+ buf_pos, extend_size);
+ return -1;
+ }
+
+ printf("GOT VALID EXTEND %d\n", extend_size);
+
+ if (restore_extend(vmar, buf, extend_size, errp) < 0) {
+ return -1;
+ }
+
+ if (buf_pos > extend_size) {
+ memmove(buf, buf + extend_size, buf_pos - extend_size);
+ buf_pos = buf_pos - extend_size;
+ } else {
+ buf_pos = 0;
+ }
+ }
+
+ bdrv_drain_all();
+
+ int i;
+ for (i = 1; i < 256; i++) {
+ VmaRestoreState *rstate = &vmar->rstate[i];
+ if (!rstate->bs) {
+ continue;
+ }
+
+ if (bdrv_flush(rstate->bs) < 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "vma bdrv_flush %s failed", vmar->devinfo[i].devname);
+ return -1;
+ }
+
+ if (vmar->devinfo[i].size) {
+ assert(rstate->bitmap);
+
+ int64_t cluster_num, end;
+
+ end = (vmar->devinfo[i].size + VMA_CLUSTER_SIZE - 1) / VMA_CLUSTER_SIZE;
+
+ for (cluster_num = 0; cluster_num < end; cluster_num++) {
+ if (!vma_reader_get_bitmap(rstate, cluster_num)) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "detected missing cluster %zd for stream %s", cluster_num,
+ vmar->devinfo[i].devname);
+ return -1;
+ }
+ }
+ }
+ }
+
+ return ret;
+}
+
diff --git a/vma-writer.c b/vma-writer.c
new file mode 100644
index 0000000..49bdc37
--- /dev/null
+++ b/vma-writer.c
@@ -0,0 +1,802 @@
+/*
+ * VMA: Virtual Machine Archive
+ *
+ * Copyright (C) Proxmox Server Solutions
+ *
+ * Authors:
+ * Dietmar Maurer (dietmar at proxmox.com)
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+/*
+ TODOs:
+
+*/
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <glib.h>
+#include <uuid/uuid.h>
+
+#include "qemu-common.h"
+#include "qemu_socket.h"
+#include "qemu-coroutine.h"
+#include "qemu-aio.h"
+#include "qemu/ratelimit.h"
+#include "vma.h"
+#include "block.h"
+
+//#define DEBUG_VMA
+
+#ifdef DEBUG_VMA
+#define DPRINTF(fmt, ...) \
+ do { printf("vma: " fmt, ## __VA_ARGS__); } while (0)
+#else
+#define DPRINTF(fmt, ...) \
+ do { } while (0)
+#endif
+
+#define WRITE_BUFFERS 5
+
+typedef struct VmaAIOCB VmaAIOCB;
+struct VmaAIOCB {
+ VmaWriter *vmaw;
+ unsigned char buffer[VMA_MAX_EXTEND_SIZE];
+ size_t bytes;
+ Coroutine *co;
+};
+
+struct VmaWriter {
+ int fd;
+ FILE *cmd;
+ int status;
+ char errmsg[8192];
+ uuid_t uuid;
+ bool header_written;
+ bool closed;
+
+ /* we always write extends */
+ unsigned char outbuf[VMA_MAX_EXTEND_SIZE];
+ int outbuf_pos; // in bytes
+ int outbuf_count; // in VMA_BLOCKS
+ uint64_t outbuf_block_info[VMA_BLOCKS_PER_EXTEND];
+
+ VmaAIOCB aiocbs[WRITE_BUFFERS];
+ CoQueue wqueue;
+
+ GChecksum *md5csum;
+ CoMutex writer_lock;
+ CoMutex flush_lock;
+ Coroutine *co_writer;
+ RateLimit limit;
+ uint64_t delay_ns;
+
+ /* drive informations */
+ VmaStreamInfo stream_info[256];
+ guint stream_count;
+
+ /* header blob table */
+ char *header_blob_table;
+ uint32_t header_blob_table_size;
+ uint32_t header_blob_table_pos;
+
+ /* store for config blobs */
+ uint32_t config_names[VMA_MAX_CONFIGS]; /* offset into blob_buffer table */
+ uint32_t config_data[VMA_MAX_CONFIGS]; /* offset into blob_buffer table */
+ uint32_t config_count;
+
+ /* statistics */
+ uint64_t zero_block_count;
+ uint64_t total_bytes_written;
+};
+
+void vma_writer_set_error(VmaWriter *vmaw, const char *fmt, ...)
+{
+ va_list ap;
+
+ if (vmaw->status < 0) {
+ return;
+ }
+
+ vmaw->status = -1;
+
+ va_start(ap, fmt);
+ g_vsnprintf(vmaw->errmsg, sizeof(vmaw->errmsg), fmt, ap);
+ va_end(ap);
+
+ DPRINTF("vma_writer_set_error: %s\n",vmaw->errmsg);
+}
+
+static uint32_t allocate_header_blob(VmaWriter *vmaw, const char *data, size_t len)
+{
+ if (len > 65535) {
+ return 0;
+ }
+
+ if (!vmaw->header_blob_table ||
+ (vmaw->header_blob_table_size <
+ (vmaw->header_blob_table_pos + len + 2))) {
+ int newsize = vmaw->header_blob_table_size + ((len + 2 + 511)/512)*512;
+
+ vmaw->header_blob_table = g_realloc(vmaw->header_blob_table, newsize);
+ memset(vmaw->header_blob_table + vmaw->header_blob_table_size,
+ 0, newsize - vmaw->header_blob_table_size);
+ vmaw->header_blob_table_size = newsize;
+ }
+
+ uint32_t cpos = vmaw->header_blob_table_pos;
+ vmaw->header_blob_table[cpos] = len & 255;
+ vmaw->header_blob_table[cpos+1] = (len >> 8) & 255;
+ memcpy(vmaw->header_blob_table + cpos + 2, data, len);
+ vmaw->header_blob_table_pos += len + 2;
+ return cpos;
+}
+
+static uint32_t allocate_header_string(VmaWriter *vmaw, const char *str)
+{
+ assert(vmaw);
+
+ size_t len = strlen(str) + 1;
+
+ return allocate_header_blob(vmaw, str, len);
+}
+
+int vma_writer_add_config(VmaWriter *vmaw, const char *name, gpointer data,
+ gsize len)
+{
+ assert(vmaw);
+ assert(!vmaw->header_written);
+ assert(vmaw->config_count < VMA_MAX_CONFIGS);
+ assert(name);
+ assert(data);
+ assert(len);
+
+ uint32_t name_ptr = allocate_header_string(vmaw, name);
+ if (!name_ptr) {
+ return -1;
+ }
+
+ uint32_t data_ptr = allocate_header_blob(vmaw, data, len);
+ if (!data_ptr) {
+ return -1;
+ }
+
+ vmaw->config_names[vmaw->config_count] = name_ptr;
+ vmaw->config_data[vmaw->config_count] = data_ptr;
+
+ vmaw->config_count++;
+
+ return 0;
+}
+
+int vma_writer_register_stream(VmaWriter *vmaw, const char *devname, size_t size)
+{
+ assert(vmaw);
+ assert(!vmaw->status);
+
+ if (vmaw->header_written) {
+ vma_writer_set_error(vmaw, "vma_writer_register_drive: header already written");
+ return -1;
+ }
+
+ guint n = vmaw->stream_count + 1;
+
+ // we can have dev_ids form 1 to 255 (0 reserved)
+ // but 254 reserved for VM state
+ // 255(-1) reseverd for safety
+ if (n > 253) {
+ vma_writer_set_error(vmaw, "vma_writer_register_drive: too many drives");
+ return -1;
+ }
+
+ if (size <= 0) {
+ vma_writer_set_error(vmaw, "vma_writer_register_drive: got strange size %zd", size);
+ return -1;
+ }
+
+ DPRINTF("vma_writer_register_drive %s %zd %d", devname, size, n);
+
+ vmaw->stream_info[n].devname = g_strdup(devname);
+ vmaw->stream_info[n].size = size;
+
+ vmaw->stream_info[n].cluster_count = (size + VMA_CLUSTER_SIZE - 1) /
+ VMA_CLUSTER_SIZE;
+
+ vmaw->stream_count = n;
+
+ return n;
+}
+
+static void vma_co_continue_write(void *opaque)
+{
+ VmaWriter *vmaw = opaque;
+
+ qemu_aio_set_fd_handler(vmaw->fd, NULL, NULL, NULL, NULL);
+
+ DPRINTF("vma_co_continue_write\n");
+ qemu_coroutine_enter(vmaw->co_writer, NULL);
+}
+
+static ssize_t coroutine_fn
+vma_co_write(VmaWriter *vmaw, const void *buf, size_t bytes)
+{
+ size_t done = 0;
+ ssize_t ret;
+
+ /* atomic writes (we cannot interleave writes) */
+ qemu_co_mutex_lock(&vmaw->writer_lock);
+
+ DPRINTF("vma_co_write enter %zd\n", bytes);
+ while (done < bytes) {
+ ret = write(vmaw->fd, buf + done, bytes - done);
+ if (ret > 0) {
+ done += ret;
+ vmaw->total_bytes_written += ret;
+ DPRINTF("vma_co_write written %zd %zd\n", done, ret);
+ } else if (ret < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ DPRINTF("vma_co_write yield %zd\n", done);
+
+ vmaw->co_writer = qemu_coroutine_self();
+ qemu_aio_set_fd_handler(vmaw->fd, NULL, vma_co_continue_write,
+ NULL, vmaw);
+
+ qemu_coroutine_yield();
+ DPRINTF("vma_co_write restart %zd\n", done);
+ } else {
+ vma_writer_set_error(vmaw, "vma_co_write write error %zd - %s\n", ret, strerror(errno));
+ done = -1; /* always return failure for partial writes */
+ break;
+ }
+ } else if (ret == 0) {
+ /* should not happen - simply try again */
+ }
+ }
+
+ qemu_co_mutex_unlock(&vmaw->writer_lock);
+
+ DPRINTF("vma_co_write leave %zd\n", done);
+ return done;
+}
+
+static void coroutine_fn vma_co_writer_task(void *opaque)
+{
+ VmaAIOCB *cb = opaque;
+
+ DPRINTF("vma_co_writer_task start\n");
+
+ int64_t done = vma_co_write(cb->vmaw, cb->buffer, cb->bytes);
+ DPRINTF("vma_co_writer_task write done %zd\n", done);
+
+ if (done != cb->bytes) {
+ DPRINTF("vma_co_writer_task failed write %zd %zd", cb->bytes, done);
+ vma_writer_set_error(cb->vmaw, "vma_co_writer_task failed write %zd", done);
+ }
+
+ cb->bytes = 0;
+
+ qemu_co_queue_next(&cb->vmaw->wqueue);
+
+ DPRINTF("vma_co_writer_task end\n");
+}
+
+static void coroutine_fn vma_queue_flush(VmaWriter *vmaw)
+{
+ DPRINTF("vma_queue_flush enter\n");
+
+ assert(vmaw);
+
+ while (1) {
+ int i;
+ VmaAIOCB *cb = NULL;
+ for (i = 0; i < WRITE_BUFFERS; i++) {
+ if (vmaw->aiocbs[i].bytes) {
+ cb = &vmaw->aiocbs[i];
+ DPRINTF("FOUND USED AIO BUFFER %d %zd\n", i, vmaw->aiocbs[i].bytes);
+ break;
+ }
+ }
+ if (!cb)
+ break;
+
+ qemu_co_queue_wait(&vmaw->wqueue);
+ }
+
+ DPRINTF("vma_queue_flush leave\n");
+}
+
+/**
+ * NOTE: pipe buffer size in only 4096 bytes on linux (see 'ulimit -a')
+ * So we need to create a coroutione to allow 'parallel' execution.
+ */
+static ssize_t coroutine_fn
+vma_queue_write(VmaWriter *vmaw, const void *buf, size_t bytes)
+{
+ DPRINTF("vma_queue_write enter %zd\n", bytes);
+
+ assert(vmaw);
+ assert(buf);
+ assert(bytes <= VMA_MAX_EXTEND_SIZE);
+
+ VmaAIOCB *cb = NULL;
+ while (!cb) {
+ int i;
+ for (i = 0; i < WRITE_BUFFERS; i++) {
+ if (!vmaw->aiocbs[i].bytes) {
+ cb = &vmaw->aiocbs[i];
+ break;
+ }
+ }
+ if (!cb) {
+ qemu_co_queue_wait(&vmaw->wqueue);
+ }
+ }
+
+ memcpy(cb->buffer, buf, bytes);
+ cb->bytes = bytes;
+ cb->vmaw = vmaw;
+
+ DPRINTF("vma_queue_write start %zd\n", bytes);
+ cb->co = qemu_coroutine_create(vma_co_writer_task);
+ qemu_coroutine_enter(cb->co, cb);
+
+ DPRINTF("vma_queue_write leave\n");
+
+ return bytes;
+}
+
+VmaWriter *vma_writer_create(const char *filename, int64_t speed, Error **errp)
+{
+ const char *p;
+
+ assert(sizeof(VmaHeader) == (4096 + 8192));
+ assert(sizeof(VmaExtendHeader) == 512);
+
+ VmaWriter *vmaw = g_new0(VmaWriter, 1);
+ vmaw->fd = -1;
+
+ vmaw->md5csum = g_checksum_new(G_CHECKSUM_MD5);
+ if (!vmaw->md5csum) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "can't allocate cmsum\n");
+ goto err;
+ }
+
+ if (strstart(filename, "exec:", &p)) {
+ if ((vmaw->cmd = popen(p, "w")) == NULL) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "can't popen command '%s' - %s\n", p, strerror(errno));
+ goto err;
+ }
+ vmaw->fd = fileno(vmaw->cmd);
+ socket_set_nonblock(vmaw->fd);
+
+ } else if ((vmaw->fd = open(filename, O_NONBLOCK|O_WRONLY|O_CREAT|O_EXCL, 0644)) < 0) {
+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
+ "can't open file %s - %s\n", filename, strerror(errno));
+ goto err;
+ }
+
+ vmaw->outbuf_count = 0;
+ vmaw->outbuf_pos = VMA_EXTEND_HEADER_SIZE;
+
+ vmaw->header_blob_table_pos = 1; /* start at pos 1 */
+
+ qemu_co_mutex_init(&vmaw->writer_lock);
+ qemu_co_mutex_init(&vmaw->flush_lock);
+ qemu_co_queue_init(&vmaw->wqueue);
+
+ uuid_generate(vmaw->uuid);
+
+ if (speed <= 0) {
+ speed = 1024*1024*1024; // default 1GB/s
+ }
+
+ ratelimit_set_speed(&vmaw->limit, speed, 100000000ULL /* 0.1 sec */);
+
+ return vmaw;
+
+err:
+ if (vmaw) {
+ if (vmaw->cmd) {
+ pclose(vmaw->cmd);
+ } else if (vmaw->fd >= 0) {
+ close(vmaw->fd);
+ }
+
+ if (vmaw->md5csum)
+ g_checksum_free(vmaw->md5csum);
+
+ g_free(vmaw);
+ }
+
+ return NULL;
+}
+
+static int coroutine_fn vma_write_header(VmaWriter *vmaw)
+{
+ assert(vmaw);
+ int header_clusters = 8;
+ char buf[65536*header_clusters];
+ VmaHeader *head = (VmaHeader *)buf;
+
+ int i;
+
+ DPRINTF("VMA WRITE HEADER\n");
+
+ if (vmaw->status < 0) {
+ return vmaw->status;
+ }
+
+ memset(buf, 0, sizeof(buf));
+
+ head->magic = VMA_MAGIC;
+ head->version = GUINT32_TO_BE(1); /* v1 */
+ memcpy(head->uuid, vmaw->uuid, 16);
+
+ time_t ctime = time(NULL);
+ head->ctime = GUINT64_TO_BE(ctime);
+
+ if (!vmaw->stream_count) {
+ return -1;
+ }
+
+ for (i = 0; i < VMA_MAX_CONFIGS; i++) {
+ head->config_names[i] = GUINT32_TO_BE(vmaw->config_names[i]);
+ head->config_data[i] = GUINT32_TO_BE(vmaw->config_data[i]);
+ }
+
+ /* 32 bytes per device (12 used currently) = 8192 bytes max */
+ for (i = 1; i <= 254; i++) {
+ VmaStreamInfo *si = &vmaw->stream_info[i];
+ if (si->size) {
+ assert(si->devname);
+ uint32_t devname_ptr = allocate_header_string(vmaw, si->devname);
+ if (!devname_ptr) {
+ return -1;
+ }
+ head->dev_info[i].devname_ptr = GUINT32_TO_BE(devname_ptr);
+ head->dev_info[i].size = GUINT64_TO_BE(si->size);
+ }
+ }
+
+ uint32_t header_size = sizeof(VmaHeader) + vmaw->header_blob_table_size;
+ head->header_size = GUINT32_TO_BE(header_size);
+
+ if (header_size > sizeof(buf)) {
+ return -1; /* just to be sure */
+ }
+
+ uint32_t blob_buffer_offset = sizeof(VmaHeader);
+ memcpy(buf + blob_buffer_offset, vmaw->header_blob_table,
+ vmaw->header_blob_table_size);
+ head->blob_buffer_offset = GUINT32_TO_BE(blob_buffer_offset);
+ head->blob_buffer_size = GUINT32_TO_BE(vmaw->header_blob_table_pos);
+
+ g_checksum_reset(vmaw->md5csum);
+ g_checksum_update(vmaw->md5csum, (const guchar *)buf, header_size);
+ gsize csize = 16;
+ g_checksum_get_digest(vmaw->md5csum, (guint8 *)(head->md5sum), &csize);
+
+ return vma_queue_write(vmaw, buf, header_size);
+}
+
+static int coroutine_fn vma_writer_flush(VmaWriter *vmaw)
+{
+ assert(vmaw);
+
+ int ret;
+ int i;
+
+ if (vmaw->status < 0) {
+ return vmaw->status;
+ }
+
+ if (!vmaw->header_written) {
+ vmaw->header_written = true;
+ ret = vma_write_header(vmaw);
+ if (ret < 0) {
+ vma_writer_set_error(vmaw, "vma_writer_flush: write header failed");
+ return ret;
+ }
+ }
+
+ DPRINTF("VMA WRITE FLUSH %d %d %zd\n", vmaw->outbuf_count,
+ vmaw->outbuf_pos,
+ vmaw->zero_block_count);
+
+
+ VmaExtendHeader *ehead = (VmaExtendHeader *)vmaw->outbuf;
+
+ ehead->magic = VMA_EXTEND_MAGIC;
+ ehead->reserved1 = 0;
+
+ for (i = 0; i < VMA_BLOCKS_PER_EXTEND; i++) {
+ ehead->blockinfo[i] = GUINT64_TO_BE(vmaw->outbuf_block_info[i]);
+ }
+
+ guint16 block_count = (vmaw->outbuf_pos - VMA_EXTEND_HEADER_SIZE) /
+ VMA_BLOCK_SIZE;
+
+ ehead->block_count = GUINT16_TO_BE(block_count);
+
+ memcpy(ehead->uuid, vmaw->uuid, sizeof(ehead->uuid));
+ memset(ehead->md5sum, 0, sizeof(ehead->md5sum));
+
+ g_checksum_reset(vmaw->md5csum);
+ g_checksum_update(vmaw->md5csum, vmaw->outbuf, VMA_EXTEND_HEADER_SIZE);
+ gsize csize = 16;
+ g_checksum_get_digest(vmaw->md5csum, ehead->md5sum, &csize);
+
+ int bytes = vmaw->outbuf_pos;
+ ret = vma_queue_write(vmaw, vmaw->outbuf, bytes);
+ if (ret != bytes) {
+ vma_writer_set_error(vmaw, "vma_writer_flush: failed write");
+ }
+
+ vmaw->outbuf_count = 0;
+ vmaw->outbuf_pos = VMA_EXTEND_HEADER_SIZE;
+
+ for (i = 0; i < VMA_BLOCKS_PER_EXTEND; i++) {
+ vmaw->outbuf_block_info[i] = 0;
+ }
+
+ return vmaw->status;
+}
+
+static int vma_count_open_streams(VmaWriter *vmaw)
+{
+ g_assert(vmaw != NULL);
+
+ int i;
+ int open_drives = 0;
+ for (i = 0; i <= 255; i++) {
+ if (vmaw->stream_info[i].size && !vmaw->stream_info[i].finished) {
+ open_drives++;
+ }
+ }
+
+ return open_drives;
+}
+
+/**
+ * all jobs should call this when there is no more data
+ * Returns: number of remaining stream (0 ==> finished)
+ */
+int coroutine_fn
+vma_writer_close_stream(VmaWriter *vmaw, uint8_t dev_id)
+{
+ g_assert(vmaw != NULL);
+
+ DPRINTF("vma_writer_set_status %d\n", dev_id);
+ if (!vmaw->stream_info[dev_id].size) {
+ vma_writer_set_error(vmaw, "vma_writer_close_stream: no such stream %d", dev_id);
+ return -1;
+ }
+ if (vmaw->stream_info[dev_id].finished) {
+ vma_writer_set_error(vmaw, "vma_writer_close_stream: stream already closed %d", dev_id);
+ return -1;
+ }
+
+ vmaw->stream_info[dev_id].finished = true;
+
+ int open_drives = vma_count_open_streams(vmaw);
+
+ if (open_drives <= 0) {
+ DPRINTF("vma_writer_set_status all drives completed\n");
+ qemu_co_mutex_lock(&vmaw->flush_lock);
+ int ret = vma_writer_flush(vmaw);
+ qemu_co_mutex_unlock(&vmaw->flush_lock);
+ if (ret < 0) {
+ vma_writer_set_error(vmaw, "vma_writer_close_stream: flush failed");
+ }
+ }
+
+ return open_drives;
+}
+
+int vma_writer_get_status(VmaWriter *vmaw, VmaStatus *status)
+{
+ int i;
+
+ g_assert(vmaw != NULL);
+
+ if (status) {
+ status->status = vmaw->status;
+ g_strlcpy(status->errmsg, vmaw->errmsg, sizeof(status->errmsg));
+ for (i = 0; i <= 255; i++) {
+ status->stream_info[i] = vmaw->stream_info[i];
+ }
+
+ uuid_unparse_lower(vmaw->uuid, status->uuid_str);
+ }
+
+ status->closed = vmaw->closed;
+
+ return vmaw->status;
+}
+
+static int vma_writer_get_buffer(VmaWriter *vmaw)
+{
+
+ // wait until buffer is available
+ while (vmaw->outbuf_count >= (VMA_BLOCKS_PER_EXTEND - 1)) {
+ int res = 0;
+
+ qemu_co_mutex_lock(&vmaw->flush_lock);
+ res = vma_writer_flush(vmaw);
+ qemu_co_mutex_unlock(&vmaw->flush_lock);
+
+ if (res < 0) {
+ vma_writer_set_error(vmaw, "vma_writer_get_buffer: flush failed");
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+
+int64_t coroutine_fn
+vma_writer_write(VmaWriter *vmaw, uint8_t dev_id, int64_t cluster_num,
+ unsigned char *buf)
+{
+ g_assert(vmaw != NULL);
+
+ if (vmaw->status < 0) {
+ return vmaw->status;
+ }
+
+ if (!dev_id || !vmaw->stream_info[dev_id].size) {
+ vma_writer_set_error(vmaw, "vma_writer_write: no such stream %d", dev_id);
+ return -1;
+ }
+
+ if (vmaw->stream_info[dev_id].finished) {
+ vma_writer_set_error(vmaw, "vma_writer_write: stream already closed %d", dev_id);
+ return -1;
+ }
+
+
+ if (cluster_num >= (((uint64_t)1)<<32)) {
+ vma_writer_set_error(vmaw, "vma_writer_write: cluster number out of range");
+ return -1;
+ }
+
+ if (cluster_num >= vmaw->stream_info[dev_id].cluster_count) {
+ vma_writer_set_error(vmaw, "vma_writer_write: cluster number too big");
+ return -1;
+ }
+
+ // wait until buffer is available
+ if (vma_writer_get_buffer(vmaw) < 0) {
+ vma_writer_set_error(vmaw, "vma_writer_write: vma_writer_get_buffer failed");
+ return -1;
+ }
+
+ DPRINTF("VMA WRITE %zd\n", cluster_num);
+
+ int i;
+ int bit = 1;
+ uint16_t mask = 0;
+ for (i = 0; i < 16; i++) {
+ unsigned char *vmablock = buf + (i*VMA_BLOCK_SIZE);
+ if (buffer_is_zero(vmablock, VMA_BLOCK_SIZE)) {
+ DPRINTF("VMA WRITE %zd ZERO BLOCK %d\n", cluster_num, i);
+ vmaw->zero_block_count++;
+ vmaw->stream_info[dev_id].zero_bytes += VMA_BLOCK_SIZE;
+ } else {
+ mask |= bit;
+ memcpy(vmaw->outbuf + vmaw->outbuf_pos, vmablock, VMA_BLOCK_SIZE);
+ vmaw->outbuf_pos += VMA_BLOCK_SIZE;
+
+ /* fixme: is that the correct way to do rate control ? */
+ vmaw->delay_ns = ratelimit_calculate_delay(&vmaw->limit, VMA_BLOCK_SIZE);
+ if (vmaw->delay_ns) {
+ co_sleep_ns(rt_clock, vmaw->delay_ns);
+ }
+ }
+
+ bit = bit << 1;
+ }
+
+ uint64_t block_info = ((uint64_t)mask) << (32+16);
+ block_info |= ((uint64_t)dev_id) << 32;
+ block_info |= (cluster_num & 0xffffffff);
+ vmaw->outbuf_block_info[vmaw->outbuf_count] = block_info;
+
+ DPRINTF("VMA WRITE MASK %zd %zx\n", cluster_num, block_info);
+
+ vmaw->outbuf_count++;
+
+ vmaw->stream_info[dev_id].transferred += VMA_CLUSTER_SIZE;
+
+ /** NOTE: We allways write whole clusters, but we correctly set
+ * transferred bytes. So transferred == size when when everything
+ * went OK.
+ */
+ uint64_t last = (cluster_num + 1) * VMA_CLUSTER_SIZE;
+ if (last > vmaw->stream_info[dev_id].size) {
+ uint64_t diff = last - vmaw->stream_info[dev_id].size;
+ if (diff >= VMA_CLUSTER_SIZE) {
+ vma_writer_set_error(vmaw, "vma_writer_write: read after last cluster");
+ return -1;
+ }
+ vmaw->stream_info[dev_id].transferred -= diff;
+ }
+
+ return vmaw->total_bytes_written;
+}
+
+int vma_writer_close(VmaWriter *vmaw)
+{
+ g_assert(vmaw != NULL);
+
+ int i;
+
+ vma_queue_flush(vmaw);
+
+ /* this should not happen - just to be sure */
+ while (!qemu_co_queue_empty(&vmaw->wqueue)) {
+ DPRINTF("vma_writer_close wait\n");
+ co_sleep_ns(rt_clock, 1000000);
+ }
+
+ if (vmaw->cmd) {
+ if (pclose(vmaw->cmd) < 0) {
+ vma_writer_set_error(vmaw, "vma_writer_close: pclose failed - %s", strerror(errno));
+ }
+ } else {
+ if (close(vmaw->fd) < 0) {
+ vma_writer_set_error(vmaw, "vma_writer_close: close failed - %s", strerror(errno));
+ }
+ }
+
+ for (i = 0; i <= 255; i++) {
+ VmaStreamInfo *si = &vmaw->stream_info[i];
+ if (si->size) {
+ if (!si->finished) {
+ vma_writer_set_error(vmaw, "vma_writer_close: detected open stream '%s'", si->devname);
+ } else if (si->transferred != si->size) {
+ vma_writer_set_error(vmaw, "vma_writer_close: incomplete stream '%s' (%zd != %zd)",
+ si->devname, si->transferred, si->size);
+ }
+ }
+ }
+
+ for (i = 0; i <= 255; i++) {
+ vmaw->stream_info[i].finished = 1; /* mark as closed */
+ }
+
+ vmaw->closed = 1;
+
+ return vmaw->status;
+}
+
+void vma_writer_destroy(VmaWriter *vmaw)
+{
+ assert(vmaw);
+
+ int i;
+
+ for (i = 0; i <= 255; i++) {
+ if (vmaw->stream_info[i].devname) {
+ g_free(vmaw->stream_info[i].devname);
+ }
+ }
+
+ if (vmaw->md5csum)
+ g_checksum_free(vmaw->md5csum);
+
+ g_free(vmaw);
+}
+
diff --git a/vma.c b/vma.c
new file mode 100644
index 0000000..814d41a
--- /dev/null
+++ b/vma.c
@@ -0,0 +1,512 @@
+/*
+ * VMA: Virtual Machine Archive
+ *
+ * Copyright (C) Proxmox Server Solutions
+ *
+ * Authors:
+ * Dietmar Maurer (dietmar at proxmox.com)
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <glib.h>
+
+#include "qemu-common.h"
+#include "qemu-option.h"
+#include "qemu-error.h"
+#include "osdep.h"
+#include "sysemu.h"
+#include "block_int.h"
+#include <stdio.h>
+#include "vma.h"
+
+static void help(void)
+{
+ const char *help_msg =
+ "usage: vma command [command options]\n"
+ "\n"
+ "vma list <filename>\n"
+ "vma create <filename> [-c config] <archive> pathname ...\n"
+ "vma extract <filename> [-r] <targetdir>\n"
+ ;
+
+ printf("%s", help_msg);
+ exit(1);
+}
+
+static const char *extract_devname(const char *path, char **devname, int index)
+{
+ assert(path);
+
+ const char *sep = strchr(path, '=');
+
+ if (sep) {
+ *devname = g_strndup(path, sep - path);
+ path = sep + 1;
+ } else {
+ if (index >= 0) {
+ *devname = g_strdup_printf("disk%d", index);
+ } else {
+ *devname = NULL;
+ }
+ }
+
+ return path;
+}
+
+static void print_content(VmaReader *vmar)
+{
+ assert(vmar);
+
+ VmaHeader *head = vma_reader_get_header(vmar);
+
+ printf("CTIME: %s", ctime(&head->ctime));
+
+ GList *l = vma_reader_get_config_data(vmar);
+ while (l && l->data) {
+ VmaConfigData *cdata = (VmaConfigData *)l->data;
+ l = g_list_next(l);
+ printf("CFG: size: %d name: %s\n", cdata->len, cdata->name);
+ }
+
+ int i;
+ VmaDeviceInfo *di;
+ for (i = 1; i < 255; i++) {
+ if ((di = vma_reader_get_device_info(vmar, i))) {
+ printf("DEV: dev_id=%d size: %zd devname: %s\n", i, di->size, di->devname);
+ }
+ }
+}
+
+static int list_content(int argc, char **argv)
+{
+ int c, ret = 0;
+ const char *filename;
+
+ for(;;) {
+ c = getopt(argc, argv, "h");
+ if (c == -1) {
+ break;
+ }
+ switch(c) {
+ case '?':
+ case 'h':
+ help();
+ break;
+ default:
+ g_assert_not_reached();
+ }
+ }
+
+ /* Get the filename */
+ if ((optind + 1) != argc) {
+ help();
+ }
+ filename = argv[optind++];
+
+ Error *errp = NULL;
+ VmaReader *vmar = vma_reader_create(filename, &errp);
+
+ if (!vmar) {
+ g_error("%s", error_get_pretty(errp));
+ }
+
+ print_content(vmar);
+
+ vma_reader_destroy(vmar);
+
+ return ret;
+}
+
+typedef struct RestoreMap {
+ char *devname;
+ char *path;
+ bool write_zero;
+} RestoreMap;
+
+static int extract_content(int argc, char **argv)
+{
+ int c, ret = 0;
+ const char *filename;
+ const char *dirname;
+ int readmap = 0;
+
+ for(;;) {
+ c = getopt(argc, argv, "hr");
+ if (c == -1) {
+ break;
+ }
+ switch(c) {
+ case '?':
+ case 'h':
+ help();
+ break;
+ case 'r':
+ readmap = 1;
+ break;
+ default:
+ help();
+ }
+ }
+
+ /* Get the filename */
+ if ((optind + 2) != argc) {
+ help();
+ }
+ filename = argv[optind++];
+ dirname = argv[optind++];
+
+ Error *errp = NULL;
+ VmaReader *vmar = vma_reader_create(filename, &errp);
+
+ if (!vmar) {
+ g_error("%s", error_get_pretty(errp));
+ }
+
+ if (mkdir(dirname, 0777) < 0) {
+ g_error("unable to create target directory %s - %s", dirname, strerror(errno));
+ }
+
+ GList *l = vma_reader_get_config_data(vmar);
+ while (l && l->data) {
+ VmaConfigData *cdata = (VmaConfigData *)l->data;
+ l = g_list_next(l);
+ char *cfgfn = g_strdup_printf("%s/%s", dirname, cdata->name);
+ GError *err = NULL;
+ if (!g_file_set_contents(cfgfn, (gchar *)cdata->data, cdata->len, &err)) {
+ g_error("Unable to write file: %s", err->message);
+ }
+ }
+
+ GHashTable *devmap = g_hash_table_new(g_str_hash, g_str_equal);
+
+ if (readmap) {
+ print_content(vmar);
+
+ while(1) {
+ char inbuf[8192];
+ char *line = fgets(inbuf, sizeof(inbuf), stdin);
+ if (!line || line[0] == '\0' || !strcmp(line, "done\n")) {
+ break;
+ }
+ int len = strlen(line);
+ if (line[len - 1] == '\n') {
+ line[len - 1] = '\0';
+ if (len == 1) {
+ break;
+ }
+ }
+
+ const char *path;
+ bool write_zero;
+ if (line[0] == '0' && line[1] == ':') {
+ path = inbuf + 2;
+ write_zero = false;
+ } else if (line[0] == '1' && line[1] == ':') {
+ path = inbuf + 2;
+ write_zero = true;
+ } else {
+ g_error("read map failed - parse error ('%s')", inbuf);
+ }
+
+ char *devname = NULL;
+ path = extract_devname(path, &devname, -1);
+ if (!devname) {
+ g_error("read map failed - no dev name specified ('%s')",
+ inbuf);
+ }
+
+ printf("TEST %s %s\n", path, devname);
+
+ RestoreMap *map = g_new0(RestoreMap, 1);
+ map->devname = g_strdup(devname);
+ map->path = g_strdup(path);
+ map->write_zero = write_zero;
+
+ g_hash_table_insert(devmap, map->devname, map);
+
+ };
+ }
+
+ int i;
+ for (i = 1; i < 255; i++) {
+ VmaDeviceInfo *di = vma_reader_get_device_info(vmar, i);
+ if (di) {
+ char *devfn = NULL;
+ int flags = BDRV_O_RDWR|BDRV_O_CACHE_WB;
+ bool write_zero = true;
+
+ if (readmap) {
+ RestoreMap *map;
+ if (!(map = (RestoreMap *)g_hash_table_lookup(devmap, di->devname))) {
+ g_error("no device name mapping for %s", di->devname);
+ }
+ devfn = map->path;
+ write_zero = map->write_zero;
+ } else {
+ devfn = g_strdup_printf("%s/tmp-disk-%s.raw", dirname, di->devname);
+ printf("DEVINFO %s %zd\n", devfn, di->size);
+
+ if (bdrv_img_create(devfn, "raw", NULL, NULL, NULL, di->size, flags)) {
+ g_error("can't create file %s", devfn);
+ }
+ /* Note: if we created an empty file above, there is no need
+ * to write zeroes (so we generate a sparse file)
+ */
+ write_zero = false;
+ }
+
+ BlockDriverState *bs = NULL;
+ if (bdrv_file_open(&bs, devfn, flags)) {
+ g_error("can't open file %s", devfn);
+ }
+ if (vma_reader_register_bs(vmar, i, bs, write_zero, &errp) < 0) {
+ g_error("%s", error_get_pretty(errp));
+ }
+
+ if (!readmap) {
+ g_free(devfn);
+ }
+ }
+ }
+
+ if (vma_reader_restore(vmar, &errp) < 0) {
+ g_error("restore failed - %s", error_get_pretty(errp));
+ }
+
+ if (!readmap) {
+ for (i = 1; i < 255; i++) {
+ VmaDeviceInfo *di = vma_reader_get_device_info(vmar, i);
+ if (di) {
+ char *tmpfn = g_strdup_printf("%s/tmp-disk-%s.raw", dirname, di->devname);
+ char *fn = g_strdup_printf("%s/disk-%s.raw", dirname, di->devname);
+ if (rename(tmpfn, fn) != 0) {
+ g_error("rename %s to %s failed - %s", tmpfn, fn, strerror(errno));
+ }
+ }
+ }
+ }
+
+ vma_reader_destroy(vmar);
+
+ bdrv_close_all();
+
+ return ret;
+}
+
+typedef struct BackupCB {
+ VmaWriter *vmaw;
+ uint8_t dev_id;
+} BackupCB;
+
+static int backup_dump_cb(void *opaque, BlockDriverState *bs,
+ int64_t cluster_num, unsigned char *buf)
+{
+ BackupCB *bcb = opaque;
+
+ if (vma_writer_write(bcb->vmaw, bcb->dev_id, cluster_num, buf) < 0) {
+ g_warning("backup_dump_cb vma_writer_write failed");
+ return -1;
+ }
+
+ return 0;
+}
+
+static void backup_complete_cb(void *opaque, int ret)
+{
+ BackupCB *bcb = opaque;
+
+ if (ret < 0) {
+ vma_writer_set_error(bcb->vmaw, "backup_complete_cb %d", ret);
+ }
+
+ if (vma_writer_close_stream(bcb->vmaw, bcb->dev_id) <= 0) {
+ if (vma_writer_close(bcb->vmaw) != 0) {
+ g_warning("vma_writer_close failed");
+ }
+ }
+}
+
+static int create_archive(int argc, char **argv)
+{
+ int i, c, res;
+ int verbose = 0;
+ const char *archivename;
+ GList *config_files = NULL;
+
+ for(;;) {
+ c = getopt(argc, argv, "hvc:");
+ if (c == -1) {
+ break;
+ }
+ switch(c) {
+ case '?':
+ case 'h':
+ help();
+ break;
+ case 'c':
+ config_files = g_list_append(config_files, optarg);
+ break;
+ case 'v':
+ verbose = 1;
+ break;
+ default:
+ g_assert_not_reached();
+ }
+ }
+
+
+ /* make sure we have archive name and at least one path */
+ if ((optind + 2) > argc) {
+ help();
+ }
+
+ archivename = argv[optind++];
+
+ Error *local_err = NULL;
+ VmaWriter *vmaw = NULL;
+
+ if (!(vmaw = vma_writer_create(archivename, 0, &local_err))) {
+ g_error("%s", error_get_pretty(local_err));
+ }
+
+ GList *l = config_files;
+ while (l && l->data) {
+ char *name = l->data;
+ char *cdata = NULL;
+ gsize clen = 0;
+ GError *err = NULL;
+ if (!g_file_get_contents(name, &cdata, &clen, &err)) {
+ unlink(archivename);
+ g_error("Unable to read file: %s", err->message);
+ }
+
+ if (vma_writer_add_config(vmaw, name, cdata, clen) != 0) {
+ unlink(archivename);
+ g_error("Unable to append config data %s (len = %zd)",
+ name, clen);
+ }
+ l = g_list_next(l);
+ }
+
+ int ind = 0;
+ while (optind < argc) {
+ const char *path = argv[optind++];
+ char *devname = NULL;
+ path = extract_devname(path, &devname, ind++);
+
+ BlockDriver *drv = NULL;
+ BlockDriverState *bs = bdrv_new(devname);
+ if ((res = bdrv_open(bs, path, BDRV_O_CACHE_WB , drv)) < 0) {
+ unlink(archivename);
+ g_error("bdrv_open '%s' failed", path);
+ }
+ int64_t size = bdrv_getlength(bs);
+ int dev_id = vma_writer_register_stream(vmaw, devname, size);
+ if (dev_id <= 0) {
+ unlink(archivename);
+ g_error("vma_writer_register_stream '%s' failed", devname);
+ }
+
+ BackupCB *bcb = g_new0(BackupCB, 1);
+ bcb->vmaw = vmaw;
+ bcb->dev_id = dev_id;
+
+ bdrv_backup_init(bs, backup_dump_cb, backup_complete_cb, bcb);
+ }
+
+ VmaStatus vmastat;
+ int percent = 0;
+ int last_percent = -1;
+
+ while (1) {
+ main_loop_wait(false);
+ vma_writer_get_status(vmaw, &vmastat);
+
+ if (verbose) {
+
+ uint64_t total = 0;
+ uint64_t transferred = 0;
+ uint64_t zero_bytes = 0;
+
+ int i;
+ for (i = 0; i < 256; i++) {
+ if (vmastat.stream_info[i].size) {
+ total += vmastat.stream_info[i].size;
+ transferred += vmastat.stream_info[i].transferred;
+ zero_bytes += vmastat.stream_info[i].zero_bytes;
+ }
+ }
+ percent = (transferred*100)/total;
+ if (percent != last_percent) {
+ printf("progress %d%% %zd/%zd %zd\n", percent, transferred, total, zero_bytes);
+
+ last_percent = percent;
+ }
+ }
+
+ if (vmastat.closed) {
+ break;
+ }
+ }
+
+ bdrv_drain_all();
+
+ vma_writer_get_status(vmaw, &vmastat);
+
+ if (verbose) {
+ for (i = 0; i < 256; i++) {
+ VmaStreamInfo *si = &vmastat.stream_info[i];
+ if (si->size) {
+ printf("image %s: size=%zd zeros=%zd saved=%zd\n", si->devname,
+ si->size, si->zero_bytes, si->size - si->zero_bytes);
+ }
+ }
+ }
+
+ if (vmastat.status < 0) {
+ unlink(archivename);
+ g_error("creating vma archive failed");
+ }
+
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ const char *cmdname;
+
+ error_set_progname(argv[0]);
+
+ qemu_init_main_loop();
+
+ bdrv_init();
+
+ if (argc < 2)
+ help();
+
+ cmdname = argv[1];
+ argc--; argv++;
+
+
+ if (!strcmp(cmdname, "list")) {
+ return list_content(argc, argv);
+ } else if (!strcmp(cmdname, "create")) {
+ return create_archive(argc, argv);
+ } else if (!strcmp(cmdname, "extract")) {
+ return extract_content(argc, argv);
+ }
+
+ help();
+ return 0;
+}
diff --git a/vma.h b/vma.h
new file mode 100644
index 0000000..204757c
--- /dev/null
+++ b/vma.h
@@ -0,0 +1,137 @@
+/*
+ * VMA: Virtual Machine Archive
+ *
+ * Copyright (C) Proxmox Server Solutions
+ *
+ * Authors:
+ * Dietmar Maurer (dietmar at proxmox.com)
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef BACKUP_VMA_H
+#define BACKUP_VMA_H
+
+#include "error.h"
+
+#define VMA_BLOCK_BITS 12
+#define VMA_BLOCK_SIZE (1<<VMA_BLOCK_BITS)
+#define VMA_CLUSTER_BITS (VMA_BLOCK_BITS+4)
+#define VMA_CLUSTER_SIZE (1<<VMA_CLUSTER_BITS)
+
+#if VMA_CLUSTER_SIZE != 65536
+#error unexpected cluster size
+#endif
+
+#define VMA_EXTEND_HEADER_SIZE 512
+#define VMA_BLOCKS_PER_EXTEND 59
+#define VMA_MAX_CONFIGS 256
+
+#define VMA_MAX_EXTEND_SIZE (VMA_EXTEND_HEADER_SIZE+VMA_CLUSTER_SIZE*VMA_BLOCKS_PER_EXTEND)
+#if VMA_MAX_EXTEND_SIZE != 3867136
+#error unexpected VMA_EXTEND_SIZE
+#endif
+
+/* File Format Definitions */
+
+#define VMA_MAGIC (GUINT32_TO_BE(('V' << 24) | ('M' << 16) | ('A' << 8) | 0x00))
+#define VMA_EXTEND_MAGIC (GUINT32_TO_BE(('V' << 24) | ('M' << 16) | ('A' << 8) | 'E'))
+
+typedef struct VmaDeviceInfoHeader {
+ uint32_t devname_ptr; /* offset into blob_buffer table */
+ uint32_t reserved0;
+ uint64_t size; /* device size in bytes */
+ uint64_t reserved1;
+ uint64_t reserved2;
+} VmaDeviceInfoHeader;
+
+typedef struct VmaHeader {
+ uint32_t magic;
+ uint32_t version;
+ unsigned char uuid[16];
+ int64_t ctime;
+ unsigned char md5sum[16];
+
+ uint32_t blob_buffer_offset;
+ uint32_t blob_buffer_size;
+ uint32_t header_size;
+
+ unsigned char reserved[1984];
+
+ uint32_t config_names[VMA_MAX_CONFIGS]; /* offset into blob_buffer table */
+ uint32_t config_data[VMA_MAX_CONFIGS]; /* offset into blob_buffer table */
+
+ VmaDeviceInfoHeader dev_info[256];
+} VmaHeader;
+
+typedef struct VmaExtendHeader {
+ uint32_t magic;
+ uint16_t reserved1;
+ uint16_t block_count;
+ unsigned char uuid[16];
+ unsigned char md5sum[16];
+ uint64_t blockinfo[VMA_BLOCKS_PER_EXTEND];
+} VmaExtendHeader;
+
+/* functions/definitions to read/write vma files */
+
+typedef struct VmaReader VmaReader;
+
+typedef struct VmaWriter VmaWriter;
+
+typedef struct VmaConfigData {
+ const unsigned char *name;
+ const unsigned char *data;
+ uint32_t len;
+} VmaConfigData;
+
+typedef struct VmaStreamInfo {
+ uint64_t size;
+ uint64_t cluster_count;
+ uint64_t transferred;
+ uint64_t zero_bytes;
+ int finished;
+ char *devname;
+} VmaStreamInfo;
+
+typedef struct VmaStatus {
+ int status;
+ bool closed;
+ char errmsg[8192];
+ char uuid_str[37];
+ VmaStreamInfo stream_info[256];
+} VmaStatus;
+
+typedef struct VmaDeviceInfo {
+ uint64_t size; /* device size in bytes */
+ const unsigned char *devname;
+} VmaDeviceInfo;
+
+VmaWriter *vma_writer_create(const char *filename, int64_t speed, Error **errp);
+int vma_writer_close(VmaWriter *vmaw);
+void vma_writer_destroy(VmaWriter *vmaw);
+int vma_writer_add_config(VmaWriter *vmaw, const char *name, gpointer data,
+ size_t len);
+int vma_writer_register_stream(VmaWriter *vmaw, const char *devname,
+ size_t size);
+
+int64_t coroutine_fn vma_writer_write(VmaWriter *vmaw, uint8_t dev_id, int64_t cluster_num,
+ unsigned char *buf);
+int coroutine_fn vma_writer_close_stream(VmaWriter *vmaw, uint8_t dev_id);
+
+int vma_writer_get_status(VmaWriter *vmaw, VmaStatus *status);
+void vma_writer_set_error(VmaWriter *vmaw, const char *fmt, ...);
+
+
+VmaReader *vma_reader_create(const char *filename, Error **errp);
+void vma_reader_destroy(VmaReader *vmar);
+VmaHeader *vma_reader_get_header(VmaReader *vmar);
+GList *vma_reader_get_config_data(VmaReader *vmar);
+VmaDeviceInfo *vma_reader_get_device_info(VmaReader *vmar, guint8 dev_id);
+int vma_reader_register_bs(VmaReader *vmar, guint8 dev_id, BlockDriverState *bs,
+ bool write_zeroes, Error **errp);
+int vma_reader_restore(VmaReader *vmar, Error **errp);
+
+#endif /* BACKUP_VMA_H */
--
1.7.2.5
More information about the pve-devel
mailing list