root / block / qcow2-snapshot.c @ 73f5e313
History | View | Annotate | Download (17 kB)
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 |
|
29 |
typedef struct QEMU_PACKED QCowSnapshotHeader { |
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 |
|
49 |
void qcow2_free_snapshots(BlockDriverState *bs)
|
50 |
{ |
51 |
BDRVQcowState *s = bs->opaque; |
52 |
int i;
|
53 |
|
54 |
for(i = 0; i < s->nb_snapshots; i++) { |
55 |
g_free(s->snapshots[i].name); |
56 |
g_free(s->snapshots[i].id_str); |
57 |
} |
58 |
g_free(s->snapshots); |
59 |
s->snapshots = NULL;
|
60 |
s->nb_snapshots = 0;
|
61 |
} |
62 |
|
63 |
int qcow2_read_snapshots(BlockDriverState *bs)
|
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; |
71 |
int ret;
|
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; |
80 |
s->snapshots = g_malloc0(s->nb_snapshots * sizeof(QCowSnapshot));
|
81 |
|
82 |
for(i = 0; i < s->nb_snapshots; i++) { |
83 |
/* Read statically sized part of the snapshot header */
|
84 |
offset = align_offset(offset, 8);
|
85 |
ret = bdrv_pread(bs->file, offset, &h, sizeof(h));
|
86 |
if (ret < 0) { |
87 |
goto fail;
|
88 |
} |
89 |
|
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 |
|
103 |
/* Skip extra data */
|
104 |
offset += extra_data_size; |
105 |
|
106 |
/* Read snapshot ID */
|
107 |
sn->id_str = g_malloc(id_str_size + 1);
|
108 |
ret = bdrv_pread(bs->file, offset, sn->id_str, id_str_size); |
109 |
if (ret < 0) { |
110 |
goto fail;
|
111 |
} |
112 |
offset += id_str_size; |
113 |
sn->id_str[id_str_size] = '\0';
|
114 |
|
115 |
/* Read snapshot name */
|
116 |
sn->name = g_malloc(name_size + 1);
|
117 |
ret = bdrv_pread(bs->file, offset, sn->name, name_size); |
118 |
if (ret < 0) { |
119 |
goto fail;
|
120 |
} |
121 |
offset += name_size; |
122 |
sn->name[name_size] = '\0';
|
123 |
} |
124 |
|
125 |
s->snapshots_size = offset - s->snapshots_offset; |
126 |
return 0; |
127 |
|
128 |
fail:
|
129 |
qcow2_free_snapshots(bs); |
130 |
return ret;
|
131 |
} |
132 |
|
133 |
/* add at the end of the file a new list of snapshots */
|
134 |
static int qcow2_write_snapshots(BlockDriverState *bs) |
135 |
{ |
136 |
BDRVQcowState *s = bs->opaque; |
137 |
QCowSnapshot *sn; |
138 |
QCowSnapshotHeader h; |
139 |
int i, name_size, id_str_size, snapshots_size;
|
140 |
struct {
|
141 |
uint32_t nb_snapshots; |
142 |
uint64_t snapshots_offset; |
143 |
} QEMU_PACKED header_data; |
144 |
int64_t offset, snapshots_offset; |
145 |
int ret;
|
146 |
|
147 |
/* compute the size of the snapshots */
|
148 |
offset = 0;
|
149 |
for(i = 0; i < s->nb_snapshots; i++) { |
150 |
sn = s->snapshots + i; |
151 |
offset = align_offset(offset, 8);
|
152 |
offset += sizeof(h);
|
153 |
offset += strlen(sn->id_str); |
154 |
offset += strlen(sn->name); |
155 |
} |
156 |
snapshots_size = offset; |
157 |
|
158 |
/* Allocate space for the new snapshot list */
|
159 |
snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size); |
160 |
bdrv_flush(bs->file); |
161 |
offset = snapshots_offset; |
162 |
if (offset < 0) { |
163 |
return offset;
|
164 |
} |
165 |
|
166 |
/* Write all snapshots to the new list */
|
167 |
for(i = 0; i < s->nb_snapshots; i++) { |
168 |
sn = s->snapshots + i; |
169 |
memset(&h, 0, sizeof(h)); |
170 |
h.l1_table_offset = cpu_to_be64(sn->l1_table_offset); |
171 |
h.l1_size = cpu_to_be32(sn->l1_size); |
172 |
h.vm_state_size = cpu_to_be32(sn->vm_state_size); |
173 |
h.date_sec = cpu_to_be32(sn->date_sec); |
174 |
h.date_nsec = cpu_to_be32(sn->date_nsec); |
175 |
h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec); |
176 |
|
177 |
id_str_size = strlen(sn->id_str); |
178 |
name_size = strlen(sn->name); |
179 |
h.id_str_size = cpu_to_be16(id_str_size); |
180 |
h.name_size = cpu_to_be16(name_size); |
181 |
offset = align_offset(offset, 8);
|
182 |
|
183 |
ret = bdrv_pwrite(bs->file, offset, &h, sizeof(h));
|
184 |
if (ret < 0) { |
185 |
goto fail;
|
186 |
} |
187 |
offset += sizeof(h);
|
188 |
|
189 |
ret = bdrv_pwrite(bs->file, offset, sn->id_str, id_str_size); |
190 |
if (ret < 0) { |
191 |
goto fail;
|
192 |
} |
193 |
offset += id_str_size; |
194 |
|
195 |
ret = bdrv_pwrite(bs->file, offset, sn->name, name_size); |
196 |
if (ret < 0) { |
197 |
goto fail;
|
198 |
} |
199 |
offset += name_size; |
200 |
} |
201 |
|
202 |
/*
|
203 |
* Update the header to point to the new snapshot table. This requires the
|
204 |
* new table and its refcounts to be stable on disk.
|
205 |
*/
|
206 |
ret = bdrv_flush(bs); |
207 |
if (ret < 0) { |
208 |
goto fail;
|
209 |
} |
210 |
|
211 |
QEMU_BUILD_BUG_ON(offsetof(QCowHeader, snapshots_offset) != |
212 |
offsetof(QCowHeader, nb_snapshots) + sizeof(header_data.nb_snapshots));
|
213 |
|
214 |
header_data.nb_snapshots = cpu_to_be32(s->nb_snapshots); |
215 |
header_data.snapshots_offset = cpu_to_be64(snapshots_offset); |
216 |
|
217 |
ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots), |
218 |
&header_data, sizeof(header_data));
|
219 |
if (ret < 0) { |
220 |
goto fail;
|
221 |
} |
222 |
|
223 |
/* free the old snapshot table */
|
224 |
qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size); |
225 |
s->snapshots_offset = snapshots_offset; |
226 |
s->snapshots_size = snapshots_size; |
227 |
return 0; |
228 |
|
229 |
fail:
|
230 |
return ret;
|
231 |
} |
232 |
|
233 |
static void find_new_snapshot_id(BlockDriverState *bs, |
234 |
char *id_str, int id_str_size) |
235 |
{ |
236 |
BDRVQcowState *s = bs->opaque; |
237 |
QCowSnapshot *sn; |
238 |
int i, id, id_max = 0; |
239 |
|
240 |
for(i = 0; i < s->nb_snapshots; i++) { |
241 |
sn = s->snapshots + i; |
242 |
id = strtoul(sn->id_str, NULL, 10); |
243 |
if (id > id_max)
|
244 |
id_max = id; |
245 |
} |
246 |
snprintf(id_str, id_str_size, "%d", id_max + 1); |
247 |
} |
248 |
|
249 |
static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str) |
250 |
{ |
251 |
BDRVQcowState *s = bs->opaque; |
252 |
int i;
|
253 |
|
254 |
for(i = 0; i < s->nb_snapshots; i++) { |
255 |
if (!strcmp(s->snapshots[i].id_str, id_str))
|
256 |
return i;
|
257 |
} |
258 |
return -1; |
259 |
} |
260 |
|
261 |
static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name) |
262 |
{ |
263 |
BDRVQcowState *s = bs->opaque; |
264 |
int i, ret;
|
265 |
|
266 |
ret = find_snapshot_by_id(bs, name); |
267 |
if (ret >= 0) |
268 |
return ret;
|
269 |
for(i = 0; i < s->nb_snapshots; i++) { |
270 |
if (!strcmp(s->snapshots[i].name, name))
|
271 |
return i;
|
272 |
} |
273 |
return -1; |
274 |
} |
275 |
|
276 |
/* if no id is provided, a new one is constructed */
|
277 |
int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
|
278 |
{ |
279 |
BDRVQcowState *s = bs->opaque; |
280 |
QCowSnapshot *new_snapshot_list = NULL;
|
281 |
QCowSnapshot *old_snapshot_list = NULL;
|
282 |
QCowSnapshot sn1, *sn = &sn1; |
283 |
int i, ret;
|
284 |
uint64_t *l1_table = NULL;
|
285 |
int64_t l1_table_offset; |
286 |
|
287 |
memset(sn, 0, sizeof(*sn)); |
288 |
|
289 |
/* Generate an ID if it wasn't passed */
|
290 |
if (sn_info->id_str[0] == '\0') { |
291 |
find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
|
292 |
} |
293 |
|
294 |
/* Check that the ID is unique */
|
295 |
if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) { |
296 |
return -ENOENT;
|
297 |
} |
298 |
|
299 |
/* Populate sn with passed data */
|
300 |
sn->id_str = g_strdup(sn_info->id_str); |
301 |
sn->name = g_strdup(sn_info->name); |
302 |
|
303 |
sn->vm_state_size = sn_info->vm_state_size; |
304 |
sn->date_sec = sn_info->date_sec; |
305 |
sn->date_nsec = sn_info->date_nsec; |
306 |
sn->vm_clock_nsec = sn_info->vm_clock_nsec; |
307 |
|
308 |
/* Allocate the L1 table of the snapshot and copy the current one there. */
|
309 |
l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
|
310 |
if (l1_table_offset < 0) { |
311 |
ret = l1_table_offset; |
312 |
goto fail;
|
313 |
} |
314 |
|
315 |
sn->l1_table_offset = l1_table_offset; |
316 |
sn->l1_size = s->l1_size; |
317 |
|
318 |
l1_table = g_malloc(s->l1_size * sizeof(uint64_t));
|
319 |
for(i = 0; i < s->l1_size; i++) { |
320 |
l1_table[i] = cpu_to_be64(s->l1_table[i]); |
321 |
} |
322 |
|
323 |
ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table, |
324 |
s->l1_size * sizeof(uint64_t));
|
325 |
if (ret < 0) { |
326 |
goto fail;
|
327 |
} |
328 |
|
329 |
g_free(l1_table); |
330 |
l1_table = NULL;
|
331 |
|
332 |
/*
|
333 |
* Increase the refcounts of all clusters and make sure everything is
|
334 |
* stable on disk before updating the snapshot table to contain a pointer
|
335 |
* to the new L1 table.
|
336 |
*/
|
337 |
ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
|
338 |
if (ret < 0) { |
339 |
goto fail;
|
340 |
} |
341 |
|
342 |
ret = bdrv_flush(bs); |
343 |
if (ret < 0) { |
344 |
goto fail;
|
345 |
} |
346 |
|
347 |
/* Append the new snapshot to the snapshot list */
|
348 |
new_snapshot_list = g_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot)); |
349 |
if (s->snapshots) {
|
350 |
memcpy(new_snapshot_list, s->snapshots, |
351 |
s->nb_snapshots * sizeof(QCowSnapshot));
|
352 |
old_snapshot_list = s->snapshots; |
353 |
} |
354 |
s->snapshots = new_snapshot_list; |
355 |
s->snapshots[s->nb_snapshots++] = *sn; |
356 |
|
357 |
ret = qcow2_write_snapshots(bs); |
358 |
if (ret < 0) { |
359 |
g_free(s->snapshots); |
360 |
s->snapshots = old_snapshot_list; |
361 |
goto fail;
|
362 |
} |
363 |
|
364 |
g_free(old_snapshot_list); |
365 |
|
366 |
#ifdef DEBUG_ALLOC
|
367 |
{ |
368 |
BdrvCheckResult result = {0};
|
369 |
qcow2_check_refcounts(bs, &result); |
370 |
} |
371 |
#endif
|
372 |
return 0; |
373 |
|
374 |
fail:
|
375 |
g_free(sn->id_str); |
376 |
g_free(sn->name); |
377 |
g_free(l1_table); |
378 |
|
379 |
return ret;
|
380 |
} |
381 |
|
382 |
/* copy the snapshot 'snapshot_name' into the current disk image */
|
383 |
int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) |
384 |
{ |
385 |
BDRVQcowState *s = bs->opaque; |
386 |
QCowSnapshot *sn; |
387 |
int i, snapshot_index;
|
388 |
int cur_l1_bytes, sn_l1_bytes;
|
389 |
int ret;
|
390 |
uint64_t *sn_l1_table = NULL;
|
391 |
|
392 |
/* Search the snapshot */
|
393 |
snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id); |
394 |
if (snapshot_index < 0) { |
395 |
return -ENOENT;
|
396 |
} |
397 |
sn = &s->snapshots[snapshot_index]; |
398 |
|
399 |
/*
|
400 |
* Make sure that the current L1 table is big enough to contain the whole
|
401 |
* L1 table of the snapshot. If the snapshot L1 table is smaller, the
|
402 |
* current one must be padded with zeros.
|
403 |
*/
|
404 |
ret = qcow2_grow_l1_table(bs, sn->l1_size, true);
|
405 |
if (ret < 0) { |
406 |
goto fail;
|
407 |
} |
408 |
|
409 |
cur_l1_bytes = s->l1_size * sizeof(uint64_t);
|
410 |
sn_l1_bytes = sn->l1_size * sizeof(uint64_t);
|
411 |
|
412 |
/*
|
413 |
* Copy the snapshot L1 table to the current L1 table.
|
414 |
*
|
415 |
* Before overwriting the old current L1 table on disk, make sure to
|
416 |
* increase all refcounts for the clusters referenced by the new one.
|
417 |
* Decrease the refcount referenced by the old one only when the L1
|
418 |
* table is overwritten.
|
419 |
*/
|
420 |
sn_l1_table = g_malloc0(cur_l1_bytes); |
421 |
|
422 |
ret = bdrv_pread(bs->file, sn->l1_table_offset, sn_l1_table, sn_l1_bytes); |
423 |
if (ret < 0) { |
424 |
goto fail;
|
425 |
} |
426 |
|
427 |
ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset, |
428 |
sn->l1_size, 1);
|
429 |
if (ret < 0) { |
430 |
goto fail;
|
431 |
} |
432 |
|
433 |
ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset, sn_l1_table, |
434 |
cur_l1_bytes); |
435 |
if (ret < 0) { |
436 |
goto fail;
|
437 |
} |
438 |
|
439 |
/*
|
440 |
* Decrease refcount of clusters of current L1 table.
|
441 |
*
|
442 |
* At this point, the in-memory s->l1_table points to the old L1 table,
|
443 |
* whereas on disk we already have the new one.
|
444 |
*
|
445 |
* qcow2_update_snapshot_refcount special cases the current L1 table to use
|
446 |
* the in-memory data instead of really using the offset to load a new one,
|
447 |
* which is why this works.
|
448 |
*/
|
449 |
ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, |
450 |
s->l1_size, -1);
|
451 |
|
452 |
/*
|
453 |
* Now update the in-memory L1 table to be in sync with the on-disk one. We
|
454 |
* need to do this even if updating refcounts failed.
|
455 |
*/
|
456 |
for(i = 0;i < s->l1_size; i++) { |
457 |
s->l1_table[i] = be64_to_cpu(sn_l1_table[i]); |
458 |
} |
459 |
|
460 |
if (ret < 0) { |
461 |
goto fail;
|
462 |
} |
463 |
|
464 |
g_free(sn_l1_table); |
465 |
sn_l1_table = NULL;
|
466 |
|
467 |
/*
|
468 |
* Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
|
469 |
* when we decreased the refcount of the old snapshot.
|
470 |
*/
|
471 |
ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
|
472 |
if (ret < 0) { |
473 |
goto fail;
|
474 |
} |
475 |
|
476 |
#ifdef DEBUG_ALLOC
|
477 |
{ |
478 |
BdrvCheckResult result = {0};
|
479 |
qcow2_check_refcounts(bs, &result); |
480 |
} |
481 |
#endif
|
482 |
return 0; |
483 |
|
484 |
fail:
|
485 |
g_free(sn_l1_table); |
486 |
return ret;
|
487 |
} |
488 |
|
489 |
int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
490 |
{ |
491 |
BDRVQcowState *s = bs->opaque; |
492 |
QCowSnapshot sn; |
493 |
int snapshot_index, ret;
|
494 |
|
495 |
/* Search the snapshot */
|
496 |
snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id); |
497 |
if (snapshot_index < 0) { |
498 |
return -ENOENT;
|
499 |
} |
500 |
sn = s->snapshots[snapshot_index]; |
501 |
|
502 |
/* Remove it from the snapshot list */
|
503 |
memmove(s->snapshots + snapshot_index, |
504 |
s->snapshots + snapshot_index + 1,
|
505 |
(s->nb_snapshots - snapshot_index - 1) * sizeof(sn)); |
506 |
s->nb_snapshots--; |
507 |
ret = qcow2_write_snapshots(bs); |
508 |
if (ret < 0) { |
509 |
return ret;
|
510 |
} |
511 |
|
512 |
/*
|
513 |
* The snapshot is now unused, clean up. If we fail after this point, we
|
514 |
* won't recover but just leak clusters.
|
515 |
*/
|
516 |
g_free(sn.id_str); |
517 |
g_free(sn.name); |
518 |
|
519 |
/*
|
520 |
* Now decrease the refcounts of clusters referenced by the snapshot and
|
521 |
* free the L1 table.
|
522 |
*/
|
523 |
ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset, |
524 |
sn.l1_size, -1);
|
525 |
if (ret < 0) { |
526 |
return ret;
|
527 |
} |
528 |
qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64_t));
|
529 |
|
530 |
/* must update the copied flag on the current cluster offsets */
|
531 |
ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
|
532 |
if (ret < 0) { |
533 |
return ret;
|
534 |
} |
535 |
|
536 |
#ifdef DEBUG_ALLOC
|
537 |
{ |
538 |
BdrvCheckResult result = {0};
|
539 |
qcow2_check_refcounts(bs, &result); |
540 |
} |
541 |
#endif
|
542 |
return 0; |
543 |
} |
544 |
|
545 |
int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
|
546 |
{ |
547 |
BDRVQcowState *s = bs->opaque; |
548 |
QEMUSnapshotInfo *sn_tab, *sn_info; |
549 |
QCowSnapshot *sn; |
550 |
int i;
|
551 |
|
552 |
if (!s->nb_snapshots) {
|
553 |
*psn_tab = NULL;
|
554 |
return s->nb_snapshots;
|
555 |
} |
556 |
|
557 |
sn_tab = g_malloc0(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
|
558 |
for(i = 0; i < s->nb_snapshots; i++) { |
559 |
sn_info = sn_tab + i; |
560 |
sn = s->snapshots + i; |
561 |
pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
|
562 |
sn->id_str); |
563 |
pstrcpy(sn_info->name, sizeof(sn_info->name),
|
564 |
sn->name); |
565 |
sn_info->vm_state_size = sn->vm_state_size; |
566 |
sn_info->date_sec = sn->date_sec; |
567 |
sn_info->date_nsec = sn->date_nsec; |
568 |
sn_info->vm_clock_nsec = sn->vm_clock_nsec; |
569 |
} |
570 |
*psn_tab = sn_tab; |
571 |
return s->nb_snapshots;
|
572 |
} |
573 |
|
574 |
int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name) |
575 |
{ |
576 |
int i, snapshot_index;
|
577 |
BDRVQcowState *s = bs->opaque; |
578 |
QCowSnapshot *sn; |
579 |
uint64_t *new_l1_table; |
580 |
int new_l1_bytes;
|
581 |
int ret;
|
582 |
|
583 |
assert(bs->read_only); |
584 |
|
585 |
/* Search the snapshot */
|
586 |
snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_name); |
587 |
if (snapshot_index < 0) { |
588 |
return -ENOENT;
|
589 |
} |
590 |
sn = &s->snapshots[snapshot_index]; |
591 |
|
592 |
/* Allocate and read in the snapshot's L1 table */
|
593 |
new_l1_bytes = s->l1_size * sizeof(uint64_t);
|
594 |
new_l1_table = g_malloc0(align_offset(new_l1_bytes, 512));
|
595 |
|
596 |
ret = bdrv_pread(bs->file, sn->l1_table_offset, new_l1_table, new_l1_bytes); |
597 |
if (ret < 0) { |
598 |
g_free(new_l1_table); |
599 |
return ret;
|
600 |
} |
601 |
|
602 |
/* Switch the L1 table */
|
603 |
g_free(s->l1_table); |
604 |
|
605 |
s->l1_size = sn->l1_size; |
606 |
s->l1_table_offset = sn->l1_table_offset; |
607 |
s->l1_table = new_l1_table; |
608 |
|
609 |
for(i = 0;i < s->l1_size; i++) { |
610 |
be64_to_cpus(&s->l1_table[i]); |
611 |
} |
612 |
|
613 |
return 0; |
614 |
} |