1 From b2dde9133b9369c29bc2902ac4d674a5ee268cf2 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Wed, 9 Dec 2015 16:31:51 +0100
4 Subject: [PATCH 24/41] backup: vma: allow empty backups
7 vma-reader.c | 29 ++++++++++-----------
8 vma-writer.c | 30 ++++++++++++++--------
9 vma.c | 84 ++++++++++++++++++++++++++++++++++++++----------------------
11 4 files changed, 88 insertions(+), 56 deletions(-)
13 diff --git a/vma-reader.c b/vma-reader.c
14 index d9f43fe..5d0d3ea 100644
17 @@ -334,11 +334,6 @@ static int vma_reader_read_head(VmaReader *vmar, Error **errp)
22 - error_setg(errp, "vma does not contain data");
26 for (i = 0; i < VMA_MAX_CONFIGS; i++) {
27 uint32_t name_ptr = GUINT32_FROM_BE(h->config_names[i]);
28 uint32_t data_ptr = GUINT32_FROM_BE(h->config_data[i]);
29 @@ -830,16 +825,20 @@ static int vma_reader_restore_full(VmaReader *vmar, int vmstate_fd,
33 - printf("total bytes read %zd, sparse bytes %zd (%.3g%%)\n",
34 - vmar->clusters_read*VMA_CLUSTER_SIZE,
35 - vmar->zero_cluster_data,
36 - (double)(100.0*vmar->zero_cluster_data)/
37 - (vmar->clusters_read*VMA_CLUSTER_SIZE));
39 - int64_t datasize = vmar->clusters_read*VMA_CLUSTER_SIZE-vmar->zero_cluster_data;
40 - if (datasize) { // this does not make sense for empty files
41 - printf("space reduction due to 4K zero blocks %.3g%%\n",
42 - (double)(100.0*vmar->partial_zero_cluster_data) / datasize);
43 + if (vmar->clusters_read) {
44 + printf("total bytes read %zd, sparse bytes %zd (%.3g%%)\n",
45 + vmar->clusters_read*VMA_CLUSTER_SIZE,
46 + vmar->zero_cluster_data,
47 + (double)(100.0*vmar->zero_cluster_data)/
48 + (vmar->clusters_read*VMA_CLUSTER_SIZE));
50 + int64_t datasize = vmar->clusters_read*VMA_CLUSTER_SIZE-vmar->zero_cluster_data;
51 + if (datasize) { // this does not make sense for empty files
52 + printf("space reduction due to 4K zero blocks %.3g%%\n",
53 + (double)(100.0*vmar->partial_zero_cluster_data) / datasize);
56 + printf("vma archive contains no image data\n");
60 diff --git a/vma-writer.c b/vma-writer.c
61 index 3c77e98..5cc4564 100644
64 @@ -258,7 +258,7 @@ vma_queue_write(VmaWriter *vmaw, const void *buf, size_t bytes)
67 vmaw->co_writer = NULL;
70 return (done == bytes) ? bytes : -1;
73 @@ -382,10 +382,6 @@ static int coroutine_fn vma_write_header(VmaWriter *vmaw)
74 time_t ctime = time(NULL);
75 head->ctime = GUINT64_TO_BE(ctime);
77 - if (!vmaw->stream_count) {
81 for (i = 0; i < VMA_MAX_CONFIGS; i++) {
82 head->config_names[i] = GUINT32_TO_BE(vmaw->config_names[i]);
83 head->config_data[i] = GUINT32_TO_BE(vmaw->config_data[i]);
84 @@ -502,6 +498,23 @@ static int vma_count_open_streams(VmaWriter *vmaw)
90 + * You need to call this if the vma archive does not contain
94 +vma_writer_flush_output(VmaWriter *vmaw)
96 + qemu_co_mutex_lock(&vmaw->flush_lock);
97 + int ret = vma_writer_flush(vmaw);
98 + qemu_co_mutex_unlock(&vmaw->flush_lock);
100 + vma_writer_set_error(vmaw, "vma_writer_flush_header failed");
106 * all jobs should call this when there is no more data
107 * Returns: number of remaining stream (0 ==> finished)
108 @@ -529,12 +542,7 @@ vma_writer_close_stream(VmaWriter *vmaw, uint8_t dev_id)
110 if (open_drives <= 0) {
111 DPRINTF("vma_writer_set_status all drives completed\n");
112 - qemu_co_mutex_lock(&vmaw->flush_lock);
113 - int ret = vma_writer_flush(vmaw);
114 - qemu_co_mutex_unlock(&vmaw->flush_lock);
116 - vma_writer_set_error(vmaw, "vma_writer_close_stream: flush failed");
118 + vma_writer_flush_output(vmaw);
122 diff --git a/vma.c b/vma.c
123 index 6a33352..00f20b0 100644
126 @@ -33,7 +33,7 @@ static void help(void)
128 "vma list <filename>\n"
129 "vma config <filename> [-c config]\n"
130 - "vma create <filename> [-c config] <archive> pathname ...\n"
131 + "vma create <filename> [-c config] pathname ...\n"
132 "vma extract <filename> [-r <fifo>] <targetdir>\n"
133 "vma verify <filename> [-v]\n"
135 @@ -401,6 +401,18 @@ typedef struct BackupJob {
137 #define BACKUP_SECTORS_PER_CLUSTER (VMA_CLUSTER_SIZE / BDRV_SECTOR_SIZE)
139 +static void coroutine_fn backup_run_empty(void *opaque)
141 + VmaWriter *vmaw = (VmaWriter *)opaque;
143 + vma_writer_flush_output(vmaw);
146 + if (vma_writer_close(vmaw, &err) != 0) {
147 + g_warning("vma_writer_close failed %s", error_get_pretty(err));
151 static void coroutine_fn backup_run(void *opaque)
153 BackupJob *job = (BackupJob *)opaque;
154 @@ -474,8 +486,8 @@ static int create_archive(int argc, char **argv)
158 - /* make sure we have archive name and at least one path */
159 - if ((optind + 2) > argc) {
160 + /* make sure we an archive name */
161 + if ((optind + 1) > argc) {
165 @@ -510,11 +522,11 @@ static int create_archive(int argc, char **argv)
171 while (optind < argc) {
172 const char *path = argv[optind++];
173 char *devname = NULL;
174 - path = extract_devname(path, &devname, ind++);
175 + path = extract_devname(path, &devname, devcount++);
177 BlockDriver *drv = NULL;
179 @@ -546,37 +558,49 @@ static int create_archive(int argc, char **argv)
181 int last_percent = -1;
184 - main_loop_wait(false);
185 - vma_writer_get_status(vmaw, &vmastat);
189 - uint64_t total = 0;
190 - uint64_t transferred = 0;
191 - uint64_t zero_bytes = 0;
194 + main_loop_wait(false);
195 + vma_writer_get_status(vmaw, &vmastat);
199 + uint64_t total = 0;
200 + uint64_t transferred = 0;
201 + uint64_t zero_bytes = 0;
204 + for (i = 0; i < 256; i++) {
205 + if (vmastat.stream_info[i].size) {
206 + total += vmastat.stream_info[i].size;
207 + transferred += vmastat.stream_info[i].transferred;
208 + zero_bytes += vmastat.stream_info[i].zero_bytes;
211 + percent = (transferred*100)/total;
212 + if (percent != last_percent) {
213 + fprintf(stderr, "progress %d%% %zd/%zd %zd\n", percent,
214 + transferred, total, zero_bytes);
218 - for (i = 0; i < 256; i++) {
219 - if (vmastat.stream_info[i].size) {
220 - total += vmastat.stream_info[i].size;
221 - transferred += vmastat.stream_info[i].transferred;
222 - zero_bytes += vmastat.stream_info[i].zero_bytes;
223 + last_percent = percent;
226 - percent = (transferred*100)/total;
227 - if (percent != last_percent) {
228 - fprintf(stderr, "progress %d%% %zd/%zd %zd\n", percent,
229 - transferred, total, zero_bytes);
232 - last_percent = percent;
233 + if (vmastat.closed) {
238 - if (vmastat.closed) {
242 + Coroutine *co = qemu_coroutine_create(backup_run_empty);
243 + qemu_coroutine_enter(co, vmaw);
245 + main_loop_wait(false);
246 + vma_writer_get_status(vmaw, &vmastat);
247 + if (vmastat.closed) {
254 diff --git a/vma.h b/vma.h
255 index 98377e4..365ceb2 100644
258 @@ -128,6 +128,7 @@ int64_t coroutine_fn vma_writer_write(VmaWriter *vmaw, uint8_t dev_id,
261 int coroutine_fn vma_writer_close_stream(VmaWriter *vmaw, uint8_t dev_id);
262 +int coroutine_fn vma_writer_flush_output(VmaWriter *vmaw);
264 int vma_writer_get_status(VmaWriter *vmaw, VmaStatus *status);
265 void vma_writer_set_error(VmaWriter *vmaw, const char *fmt, ...);