]> Git Repo - qemu.git/blame - block/qcow2-snapshot.c
qcow2: Return real error code in qcow2_read_snapshots
[qemu.git] / block / qcow2-snapshot.c
CommitLineData
c142442b
KW
1/*
2 * Block driver for the QCOW version 2 format
3 *
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#include "qemu-common.h"
26#include "block_int.h"
27#include "block/qcow2.h"
28
541dc0d4 29typedef struct QEMU_PACKED QCowSnapshotHeader {
c142442b
KW
30 /* header is 8 byte aligned */
31 uint64_t l1_table_offset;
32
33 uint32_t l1_size;
34 uint16_t id_str_size;
35 uint16_t name_size;
36
37 uint32_t date_sec;
38 uint32_t date_nsec;
39
40 uint64_t vm_clock_nsec;
41
42 uint32_t vm_state_size;
43 uint32_t extra_data_size; /* for extension */
44 /* extra data follows */
45 /* id_str follows */
46 /* name follows */
47} QCowSnapshotHeader;
48
ed6ccf0f 49void qcow2_free_snapshots(BlockDriverState *bs)
c142442b
KW
50{
51 BDRVQcowState *s = bs->opaque;
52 int i;
53
54 for(i = 0; i < s->nb_snapshots; i++) {
7267c094
AL
55 g_free(s->snapshots[i].name);
56 g_free(s->snapshots[i].id_str);
c142442b 57 }
7267c094 58 g_free(s->snapshots);
c142442b
KW
59 s->snapshots = NULL;
60 s->nb_snapshots = 0;
61}
62
ed6ccf0f 63int qcow2_read_snapshots(BlockDriverState *bs)
c142442b
KW
64{
65 BDRVQcowState *s = bs->opaque;
66 QCowSnapshotHeader h;
67 QCowSnapshot *sn;
68 int i, id_str_size, name_size;
69 int64_t offset;
70 uint32_t extra_data_size;
42deb29f 71 int ret;
c142442b
KW
72
73 if (!s->nb_snapshots) {
74 s->snapshots = NULL;
75 s->snapshots_size = 0;
76 return 0;
77 }
78
79 offset = s->snapshots_offset;
7267c094 80 s->snapshots = g_malloc0(s->nb_snapshots * sizeof(QCowSnapshot));
42deb29f 81
c142442b 82 for(i = 0; i < s->nb_snapshots; i++) {
42deb29f 83 /* Read statically sized part of the snapshot header */
c142442b 84 offset = align_offset(offset, 8);
42deb29f
KW
85 ret = bdrv_pread(bs->file, offset, &h, sizeof(h));
86 if (ret < 0) {
c142442b 87 goto fail;
42deb29f
KW
88 }
89
c142442b
KW
90 offset += sizeof(h);
91 sn = s->snapshots + i;
92 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
93 sn->l1_size = be32_to_cpu(h.l1_size);
94 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
95 sn->date_sec = be32_to_cpu(h.date_sec);
96 sn->date_nsec = be32_to_cpu(h.date_nsec);
97 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
98 extra_data_size = be32_to_cpu(h.extra_data_size);
99
100 id_str_size = be16_to_cpu(h.id_str_size);
101 name_size = be16_to_cpu(h.name_size);
102
42deb29f 103 /* Skip extra data */
c142442b
KW
104 offset += extra_data_size;
105
42deb29f 106 /* Read snapshot ID */
7267c094 107 sn->id_str = g_malloc(id_str_size + 1);
42deb29f
KW
108 ret = bdrv_pread(bs->file, offset, sn->id_str, id_str_size);
109 if (ret < 0) {
c142442b 110 goto fail;
42deb29f 111 }
c142442b
KW
112 offset += id_str_size;
113 sn->id_str[id_str_size] = '\0';
114
42deb29f 115 /* Read snapshot name */
7267c094 116 sn->name = g_malloc(name_size + 1);
42deb29f
KW
117 ret = bdrv_pread(bs->file, offset, sn->name, name_size);
118 if (ret < 0) {
c142442b 119 goto fail;
42deb29f 120 }
c142442b
KW
121 offset += name_size;
122 sn->name[name_size] = '\0';
123 }
42deb29f 124
c142442b
KW
125 s->snapshots_size = offset - s->snapshots_offset;
126 return 0;
42deb29f
KW
127
128fail:
ed6ccf0f 129 qcow2_free_snapshots(bs);
42deb29f 130 return ret;
c142442b
KW
131}
132
133/* add at the end of the file a new list of snapshots */
7c80ab3f 134static int qcow2_write_snapshots(BlockDriverState *bs)
c142442b
KW
135{
136 BDRVQcowState *s = bs->opaque;
137 QCowSnapshot *sn;
138 QCowSnapshotHeader h;
139 int i, name_size, id_str_size, snapshots_size;
140 uint64_t data64;
141 uint32_t data32;
142 int64_t offset, snapshots_offset;
143
144 /* compute the size of the snapshots */
145 offset = 0;
146 for(i = 0; i < s->nb_snapshots; i++) {
147 sn = s->snapshots + i;
148 offset = align_offset(offset, 8);
149 offset += sizeof(h);
150 offset += strlen(sn->id_str);
151 offset += strlen(sn->name);
152 }
153 snapshots_size = offset;
154
ed6ccf0f 155 snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
29216ed1 156 bdrv_flush(bs->file);
c142442b 157 offset = snapshots_offset;
5d757b56
KW
158 if (offset < 0) {
159 return offset;
160 }
c142442b
KW
161
162 for(i = 0; i < s->nb_snapshots; i++) {
163 sn = s->snapshots + i;
164 memset(&h, 0, sizeof(h));
165 h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
166 h.l1_size = cpu_to_be32(sn->l1_size);
167 h.vm_state_size = cpu_to_be32(sn->vm_state_size);
168 h.date_sec = cpu_to_be32(sn->date_sec);
169 h.date_nsec = cpu_to_be32(sn->date_nsec);
170 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
171
172 id_str_size = strlen(sn->id_str);
173 name_size = strlen(sn->name);
174 h.id_str_size = cpu_to_be16(id_str_size);
175 h.name_size = cpu_to_be16(name_size);
176 offset = align_offset(offset, 8);
8b3b7206 177 if (bdrv_pwrite_sync(bs->file, offset, &h, sizeof(h)) < 0)
c142442b
KW
178 goto fail;
179 offset += sizeof(h);
8b3b7206 180 if (bdrv_pwrite_sync(bs->file, offset, sn->id_str, id_str_size) < 0)
c142442b
KW
181 goto fail;
182 offset += id_str_size;
8b3b7206 183 if (bdrv_pwrite_sync(bs->file, offset, sn->name, name_size) < 0)
c142442b
KW
184 goto fail;
185 offset += name_size;
186 }
187
188 /* update the various header fields */
189 data64 = cpu_to_be64(snapshots_offset);
8b3b7206
KW
190 if (bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, snapshots_offset),
191 &data64, sizeof(data64)) < 0)
c142442b
KW
192 goto fail;
193 data32 = cpu_to_be32(s->nb_snapshots);
8b3b7206
KW
194 if (bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots),
195 &data32, sizeof(data32)) < 0)
c142442b
KW
196 goto fail;
197
198 /* free the old snapshot table */
ed6ccf0f 199 qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size);
c142442b
KW
200 s->snapshots_offset = snapshots_offset;
201 s->snapshots_size = snapshots_size;
202 return 0;
203 fail:
204 return -1;
205}
206
207static void find_new_snapshot_id(BlockDriverState *bs,
208 char *id_str, int id_str_size)
209{
210 BDRVQcowState *s = bs->opaque;
211 QCowSnapshot *sn;
212 int i, id, id_max = 0;
213
214 for(i = 0; i < s->nb_snapshots; i++) {
215 sn = s->snapshots + i;
216 id = strtoul(sn->id_str, NULL, 10);
217 if (id > id_max)
218 id_max = id;
219 }
220 snprintf(id_str, id_str_size, "%d", id_max + 1);
221}
222
223static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str)
224{
225 BDRVQcowState *s = bs->opaque;
226 int i;
227
228 for(i = 0; i < s->nb_snapshots; i++) {
229 if (!strcmp(s->snapshots[i].id_str, id_str))
230 return i;
231 }
232 return -1;
233}
234
235static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
236{
237 BDRVQcowState *s = bs->opaque;
238 int i, ret;
239
240 ret = find_snapshot_by_id(bs, name);
241 if (ret >= 0)
242 return ret;
243 for(i = 0; i < s->nb_snapshots; i++) {
244 if (!strcmp(s->snapshots[i].name, name))
245 return i;
246 }
247 return -1;
248}
249
250/* if no id is provided, a new one is constructed */
ed6ccf0f 251int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
c142442b
KW
252{
253 BDRVQcowState *s = bs->opaque;
254 QCowSnapshot *snapshots1, sn1, *sn = &sn1;
255 int i, ret;
256 uint64_t *l1_table = NULL;
5d757b56 257 int64_t l1_table_offset;
c142442b
KW
258
259 memset(sn, 0, sizeof(*sn));
260
261 if (sn_info->id_str[0] == '\0') {
262 /* compute a new id */
263 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
264 }
265
266 /* check that the ID is unique */
267 if (find_snapshot_by_id(bs, sn_info->id_str) >= 0)
268 return -ENOENT;
269
7267c094 270 sn->id_str = g_strdup(sn_info->id_str);
c142442b
KW
271 if (!sn->id_str)
272 goto fail;
7267c094 273 sn->name = g_strdup(sn_info->name);
c142442b
KW
274 if (!sn->name)
275 goto fail;
276 sn->vm_state_size = sn_info->vm_state_size;
277 sn->date_sec = sn_info->date_sec;
278 sn->date_nsec = sn_info->date_nsec;
279 sn->vm_clock_nsec = sn_info->vm_clock_nsec;
280
ed6ccf0f 281 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
c142442b
KW
282 if (ret < 0)
283 goto fail;
284
285 /* create the L1 table of the snapshot */
5d757b56
KW
286 l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
287 if (l1_table_offset < 0) {
288 goto fail;
289 }
29216ed1 290 bdrv_flush(bs->file);
5d757b56
KW
291
292 sn->l1_table_offset = l1_table_offset;
c142442b
KW
293 sn->l1_size = s->l1_size;
294
702ef63f 295 if (s->l1_size != 0) {
7267c094 296 l1_table = g_malloc(s->l1_size * sizeof(uint64_t));
702ef63f
KW
297 } else {
298 l1_table = NULL;
299 }
300
c142442b
KW
301 for(i = 0; i < s->l1_size; i++) {
302 l1_table[i] = cpu_to_be64(s->l1_table[i]);
303 }
8b3b7206
KW
304 if (bdrv_pwrite_sync(bs->file, sn->l1_table_offset,
305 l1_table, s->l1_size * sizeof(uint64_t)) < 0)
c142442b 306 goto fail;
7267c094 307 g_free(l1_table);
c142442b
KW
308 l1_table = NULL;
309
7267c094 310 snapshots1 = g_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
c142442b
KW
311 if (s->snapshots) {
312 memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
7267c094 313 g_free(s->snapshots);
c142442b
KW
314 }
315 s->snapshots = snapshots1;
316 s->snapshots[s->nb_snapshots++] = *sn;
317
7c80ab3f 318 if (qcow2_write_snapshots(bs) < 0)
c142442b
KW
319 goto fail;
320#ifdef DEBUG_ALLOC
6cbc3031
PH
321 {
322 BdrvCheckResult result = {0};
323 qcow2_check_refcounts(bs, &result);
324 }
c142442b
KW
325#endif
326 return 0;
327 fail:
7267c094
AL
328 g_free(sn->name);
329 g_free(l1_table);
c142442b
KW
330 return -1;
331}
332
333/* copy the snapshot 'snapshot_name' into the current disk image */
ed6ccf0f 334int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
c142442b
KW
335{
336 BDRVQcowState *s = bs->opaque;
337 QCowSnapshot *sn;
35d7ace7
KW
338 int i, snapshot_index;
339 int cur_l1_bytes, sn_l1_bytes;
c142442b
KW
340
341 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
342 if (snapshot_index < 0)
343 return -ENOENT;
344 sn = &s->snapshots[snapshot_index];
345
ed6ccf0f 346 if (qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
c142442b
KW
347 goto fail;
348
72893756 349 if (qcow2_grow_l1_table(bs, sn->l1_size, true) < 0)
c142442b
KW
350 goto fail;
351
35d7ace7
KW
352 cur_l1_bytes = s->l1_size * sizeof(uint64_t);
353 sn_l1_bytes = sn->l1_size * sizeof(uint64_t);
354
355 if (cur_l1_bytes > sn_l1_bytes) {
356 memset(s->l1_table + sn->l1_size, 0, cur_l1_bytes - sn_l1_bytes);
357 }
358
c142442b 359 /* copy the snapshot l1 table to the current l1 table */
66f82cee 360 if (bdrv_pread(bs->file, sn->l1_table_offset,
35d7ace7 361 s->l1_table, sn_l1_bytes) < 0)
c142442b 362 goto fail;
8b3b7206 363 if (bdrv_pwrite_sync(bs->file, s->l1_table_offset,
35d7ace7 364 s->l1_table, cur_l1_bytes) < 0)
c142442b
KW
365 goto fail;
366 for(i = 0;i < s->l1_size; i++) {
367 be64_to_cpus(&s->l1_table[i]);
368 }
369
ed6ccf0f 370 if (qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
c142442b
KW
371 goto fail;
372
373#ifdef DEBUG_ALLOC
6cbc3031
PH
374 {
375 BdrvCheckResult result = {0};
376 qcow2_check_refcounts(bs, &result);
377 }
c142442b
KW
378#endif
379 return 0;
380 fail:
381 return -EIO;
382}
383
ed6ccf0f 384int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
c142442b
KW
385{
386 BDRVQcowState *s = bs->opaque;
387 QCowSnapshot *sn;
388 int snapshot_index, ret;
389
390 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
391 if (snapshot_index < 0)
392 return -ENOENT;
393 sn = &s->snapshots[snapshot_index];
394
ed6ccf0f 395 ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
c142442b
KW
396 if (ret < 0)
397 return ret;
398 /* must update the copied flag on the current cluster offsets */
ed6ccf0f 399 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
c142442b
KW
400 if (ret < 0)
401 return ret;
ed6ccf0f 402 qcow2_free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
c142442b 403
7267c094
AL
404 g_free(sn->id_str);
405 g_free(sn->name);
c142442b
KW
406 memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn));
407 s->nb_snapshots--;
7c80ab3f 408 ret = qcow2_write_snapshots(bs);
c142442b
KW
409 if (ret < 0) {
410 /* XXX: restore snapshot if error ? */
411 return ret;
412 }
413#ifdef DEBUG_ALLOC
6cbc3031
PH
414 {
415 BdrvCheckResult result = {0};
416 qcow2_check_refcounts(bs, &result);
417 }
c142442b
KW
418#endif
419 return 0;
420}
421
ed6ccf0f 422int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
c142442b
KW
423{
424 BDRVQcowState *s = bs->opaque;
425 QEMUSnapshotInfo *sn_tab, *sn_info;
426 QCowSnapshot *sn;
427 int i;
428
429 if (!s->nb_snapshots) {
430 *psn_tab = NULL;
431 return s->nb_snapshots;
432 }
433
7267c094 434 sn_tab = g_malloc0(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
c142442b
KW
435 for(i = 0; i < s->nb_snapshots; i++) {
436 sn_info = sn_tab + i;
437 sn = s->snapshots + i;
438 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
439 sn->id_str);
440 pstrcpy(sn_info->name, sizeof(sn_info->name),
441 sn->name);
442 sn_info->vm_state_size = sn->vm_state_size;
443 sn_info->date_sec = sn->date_sec;
444 sn_info->date_nsec = sn->date_nsec;
445 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
446 }
447 *psn_tab = sn_tab;
448 return s->nb_snapshots;
449}
450
51ef6727 451int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name)
452{
453 int i, snapshot_index, l1_size2;
454 BDRVQcowState *s = bs->opaque;
455 QCowSnapshot *sn;
456
457 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_name);
458 if (snapshot_index < 0) {
459 return -ENOENT;
460 }
461
462 sn = &s->snapshots[snapshot_index];
463 s->l1_size = sn->l1_size;
464 l1_size2 = s->l1_size * sizeof(uint64_t);
465 if (s->l1_table != NULL) {
7267c094 466 g_free(s->l1_table);
51ef6727 467 }
468
469 s->l1_table_offset = sn->l1_table_offset;
7267c094 470 s->l1_table = g_malloc0(align_offset(l1_size2, 512));
51ef6727 471
472 if (bdrv_pread(bs->file, sn->l1_table_offset,
473 s->l1_table, l1_size2) != l1_size2) {
474 return -1;
475 }
476
477 for(i = 0;i < s->l1_size; i++) {
478 be64_to_cpus(&s->l1_table[i]);
479 }
480 return 0;
481}
This page took 0.299821 seconds and 4 git commands to generate.