root / block / qcow2.c @ 45aba42f
History | View | Annotate | Download (41.5 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 |
#include "qemu-common.h" |
25 |
#include "block_int.h" |
26 |
#include "module.h" |
27 |
#include <zlib.h> |
28 |
#include "aes.h" |
29 |
#include "block/qcow2.h" |
30 |
|
31 |
/*
|
32 |
Differences with QCOW:
|
33 |
|
34 |
- Support for multiple incremental snapshots.
|
35 |
- Memory management by reference counts.
|
36 |
- Clusters which have a reference count of one have the bit
|
37 |
QCOW_OFLAG_COPIED to optimize write performance.
|
38 |
- Size of compressed clusters is stored in sectors to reduce bit usage
|
39 |
in the cluster offsets.
|
40 |
- Support for storing additional data (such as the VM state) in the
|
41 |
snapshots.
|
42 |
- If a backing store is used, the cluster size is not constrained
|
43 |
(could be backported to QCOW).
|
44 |
- L2 tables have always a size of one cluster.
|
45 |
*/
|
46 |
|
47 |
//#define DEBUG_ALLOC
|
48 |
//#define DEBUG_ALLOC2
|
49 |
//#define DEBUG_EXT
|
50 |
|
51 |
|
52 |
typedef struct { |
53 |
uint32_t magic; |
54 |
uint32_t len; |
55 |
} QCowExtension; |
56 |
#define QCOW_EXT_MAGIC_END 0 |
57 |
#define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA |
58 |
|
59 |
|
60 |
typedef struct __attribute__((packed)) QCowSnapshotHeader { |
61 |
/* header is 8 byte aligned */
|
62 |
uint64_t l1_table_offset; |
63 |
|
64 |
uint32_t l1_size; |
65 |
uint16_t id_str_size; |
66 |
uint16_t name_size; |
67 |
|
68 |
uint32_t date_sec; |
69 |
uint32_t date_nsec; |
70 |
|
71 |
uint64_t vm_clock_nsec; |
72 |
|
73 |
uint32_t vm_state_size; |
74 |
uint32_t extra_data_size; /* for extension */
|
75 |
/* extra data follows */
|
76 |
/* id_str follows */
|
77 |
/* name follows */
|
78 |
} QCowSnapshotHeader; |
79 |
|
80 |
|
81 |
static int qcow_read_snapshots(BlockDriverState *bs); |
82 |
static void qcow_free_snapshots(BlockDriverState *bs); |
83 |
|
84 |
static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) |
85 |
{ |
86 |
const QCowHeader *cow_header = (const void *)buf; |
87 |
|
88 |
if (buf_size >= sizeof(QCowHeader) && |
89 |
be32_to_cpu(cow_header->magic) == QCOW_MAGIC && |
90 |
be32_to_cpu(cow_header->version) == QCOW_VERSION) |
91 |
return 100; |
92 |
else
|
93 |
return 0; |
94 |
} |
95 |
|
96 |
|
97 |
/*
|
98 |
* read qcow2 extension and fill bs
|
99 |
* start reading from start_offset
|
100 |
* finish reading upon magic of value 0 or when end_offset reached
|
101 |
* unknown magic is skipped (future extension this version knows nothing about)
|
102 |
* return 0 upon success, non-0 otherwise
|
103 |
*/
|
104 |
static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset, |
105 |
uint64_t end_offset) |
106 |
{ |
107 |
BDRVQcowState *s = bs->opaque; |
108 |
QCowExtension ext; |
109 |
uint64_t offset; |
110 |
|
111 |
#ifdef DEBUG_EXT
|
112 |
printf("qcow_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
|
113 |
#endif
|
114 |
offset = start_offset; |
115 |
while (offset < end_offset) {
|
116 |
|
117 |
#ifdef DEBUG_EXT
|
118 |
/* Sanity check */
|
119 |
if (offset > s->cluster_size)
|
120 |
printf("qcow_handle_extension: suspicious offset %lu\n", offset);
|
121 |
|
122 |
printf("attemting to read extended header in offset %lu\n", offset);
|
123 |
#endif
|
124 |
|
125 |
if (bdrv_pread(s->hd, offset, &ext, sizeof(ext)) != sizeof(ext)) { |
126 |
fprintf(stderr, "qcow_handle_extension: ERROR: pread fail from offset %llu\n",
|
127 |
(unsigned long long)offset); |
128 |
return 1; |
129 |
} |
130 |
be32_to_cpus(&ext.magic); |
131 |
be32_to_cpus(&ext.len); |
132 |
offset += sizeof(ext);
|
133 |
#ifdef DEBUG_EXT
|
134 |
printf("ext.magic = 0x%x\n", ext.magic);
|
135 |
#endif
|
136 |
switch (ext.magic) {
|
137 |
case QCOW_EXT_MAGIC_END:
|
138 |
return 0; |
139 |
|
140 |
case QCOW_EXT_MAGIC_BACKING_FORMAT:
|
141 |
if (ext.len >= sizeof(bs->backing_format)) { |
142 |
fprintf(stderr, "ERROR: ext_backing_format: len=%u too large"
|
143 |
" (>=%zu)\n",
|
144 |
ext.len, sizeof(bs->backing_format));
|
145 |
return 2; |
146 |
} |
147 |
if (bdrv_pread(s->hd, offset , bs->backing_format,
|
148 |
ext.len) != ext.len) |
149 |
return 3; |
150 |
bs->backing_format[ext.len] = '\0';
|
151 |
#ifdef DEBUG_EXT
|
152 |
printf("Qcow2: Got format extension %s\n", bs->backing_format);
|
153 |
#endif
|
154 |
offset += ((ext.len + 7) & ~7); |
155 |
break;
|
156 |
|
157 |
default:
|
158 |
/* unknown magic -- just skip it */
|
159 |
offset += ((ext.len + 7) & ~7); |
160 |
break;
|
161 |
} |
162 |
} |
163 |
|
164 |
return 0; |
165 |
} |
166 |
|
167 |
|
168 |
static int qcow_open(BlockDriverState *bs, const char *filename, int flags) |
169 |
{ |
170 |
BDRVQcowState *s = bs->opaque; |
171 |
int len, i, shift, ret;
|
172 |
QCowHeader header; |
173 |
uint64_t ext_end; |
174 |
|
175 |
/* Performance is terrible right now with cache=writethrough due mainly
|
176 |
* to reference count updates. If the user does not explicitly specify
|
177 |
* a caching type, force to writeback caching.
|
178 |
*/
|
179 |
if ((flags & BDRV_O_CACHE_DEF)) {
|
180 |
flags |= BDRV_O_CACHE_WB; |
181 |
flags &= ~BDRV_O_CACHE_DEF; |
182 |
} |
183 |
ret = bdrv_file_open(&s->hd, filename, flags); |
184 |
if (ret < 0) |
185 |
return ret;
|
186 |
if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header)) |
187 |
goto fail;
|
188 |
be32_to_cpus(&header.magic); |
189 |
be32_to_cpus(&header.version); |
190 |
be64_to_cpus(&header.backing_file_offset); |
191 |
be32_to_cpus(&header.backing_file_size); |
192 |
be64_to_cpus(&header.size); |
193 |
be32_to_cpus(&header.cluster_bits); |
194 |
be32_to_cpus(&header.crypt_method); |
195 |
be64_to_cpus(&header.l1_table_offset); |
196 |
be32_to_cpus(&header.l1_size); |
197 |
be64_to_cpus(&header.refcount_table_offset); |
198 |
be32_to_cpus(&header.refcount_table_clusters); |
199 |
be64_to_cpus(&header.snapshots_offset); |
200 |
be32_to_cpus(&header.nb_snapshots); |
201 |
|
202 |
if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
|
203 |
goto fail;
|
204 |
if (header.size <= 1 || |
205 |
header.cluster_bits < MIN_CLUSTER_BITS || |
206 |
header.cluster_bits > MAX_CLUSTER_BITS) |
207 |
goto fail;
|
208 |
if (header.crypt_method > QCOW_CRYPT_AES)
|
209 |
goto fail;
|
210 |
s->crypt_method_header = header.crypt_method; |
211 |
if (s->crypt_method_header)
|
212 |
bs->encrypted = 1;
|
213 |
s->cluster_bits = header.cluster_bits; |
214 |
s->cluster_size = 1 << s->cluster_bits;
|
215 |
s->cluster_sectors = 1 << (s->cluster_bits - 9); |
216 |
s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */ |
217 |
s->l2_size = 1 << s->l2_bits;
|
218 |
bs->total_sectors = header.size / 512;
|
219 |
s->csize_shift = (62 - (s->cluster_bits - 8)); |
220 |
s->csize_mask = (1 << (s->cluster_bits - 8)) - 1; |
221 |
s->cluster_offset_mask = (1LL << s->csize_shift) - 1; |
222 |
s->refcount_table_offset = header.refcount_table_offset; |
223 |
s->refcount_table_size = |
224 |
header.refcount_table_clusters << (s->cluster_bits - 3);
|
225 |
|
226 |
s->snapshots_offset = header.snapshots_offset; |
227 |
s->nb_snapshots = header.nb_snapshots; |
228 |
|
229 |
/* read the level 1 table */
|
230 |
s->l1_size = header.l1_size; |
231 |
shift = s->cluster_bits + s->l2_bits; |
232 |
s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift; |
233 |
/* the L1 table must contain at least enough entries to put
|
234 |
header.size bytes */
|
235 |
if (s->l1_size < s->l1_vm_state_index)
|
236 |
goto fail;
|
237 |
s->l1_table_offset = header.l1_table_offset; |
238 |
s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
|
239 |
if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != |
240 |
s->l1_size * sizeof(uint64_t))
|
241 |
goto fail;
|
242 |
for(i = 0;i < s->l1_size; i++) { |
243 |
be64_to_cpus(&s->l1_table[i]); |
244 |
} |
245 |
/* alloc L2 cache */
|
246 |
s->l2_cache = qemu_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
|
247 |
s->cluster_cache = qemu_malloc(s->cluster_size); |
248 |
/* one more sector for decompressed data alignment */
|
249 |
s->cluster_data = qemu_malloc(QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size |
250 |
+ 512);
|
251 |
s->cluster_cache_offset = -1;
|
252 |
|
253 |
if (refcount_init(bs) < 0) |
254 |
goto fail;
|
255 |
|
256 |
/* read qcow2 extensions */
|
257 |
if (header.backing_file_offset)
|
258 |
ext_end = header.backing_file_offset; |
259 |
else
|
260 |
ext_end = s->cluster_size; |
261 |
if (qcow_read_extensions(bs, sizeof(header), ext_end)) |
262 |
goto fail;
|
263 |
|
264 |
/* read the backing file name */
|
265 |
if (header.backing_file_offset != 0) { |
266 |
len = header.backing_file_size; |
267 |
if (len > 1023) |
268 |
len = 1023;
|
269 |
if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
|
270 |
goto fail;
|
271 |
bs->backing_file[len] = '\0';
|
272 |
} |
273 |
if (qcow_read_snapshots(bs) < 0) |
274 |
goto fail;
|
275 |
|
276 |
#ifdef DEBUG_ALLOC
|
277 |
check_refcounts(bs); |
278 |
#endif
|
279 |
return 0; |
280 |
|
281 |
fail:
|
282 |
qcow_free_snapshots(bs); |
283 |
refcount_close(bs); |
284 |
qemu_free(s->l1_table); |
285 |
qemu_free(s->l2_cache); |
286 |
qemu_free(s->cluster_cache); |
287 |
qemu_free(s->cluster_data); |
288 |
bdrv_delete(s->hd); |
289 |
return -1; |
290 |
} |
291 |
|
292 |
static int qcow_set_key(BlockDriverState *bs, const char *key) |
293 |
{ |
294 |
BDRVQcowState *s = bs->opaque; |
295 |
uint8_t keybuf[16];
|
296 |
int len, i;
|
297 |
|
298 |
memset(keybuf, 0, 16); |
299 |
len = strlen(key); |
300 |
if (len > 16) |
301 |
len = 16;
|
302 |
/* XXX: we could compress the chars to 7 bits to increase
|
303 |
entropy */
|
304 |
for(i = 0;i < len;i++) { |
305 |
keybuf[i] = key[i]; |
306 |
} |
307 |
s->crypt_method = s->crypt_method_header; |
308 |
|
309 |
if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0) |
310 |
return -1; |
311 |
if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0) |
312 |
return -1; |
313 |
#if 0
|
314 |
/* test */
|
315 |
{
|
316 |
uint8_t in[16];
|
317 |
uint8_t out[16];
|
318 |
uint8_t tmp[16];
|
319 |
for(i=0;i<16;i++)
|
320 |
in[i] = i;
|
321 |
AES_encrypt(in, tmp, &s->aes_encrypt_key);
|
322 |
AES_decrypt(tmp, out, &s->aes_decrypt_key);
|
323 |
for(i = 0; i < 16; i++)
|
324 |
printf(" %02x", tmp[i]);
|
325 |
printf("\n");
|
326 |
for(i = 0; i < 16; i++)
|
327 |
printf(" %02x", out[i]);
|
328 |
printf("\n");
|
329 |
}
|
330 |
#endif
|
331 |
return 0; |
332 |
} |
333 |
|
334 |
static int64_t align_offset(int64_t offset, int n) |
335 |
{ |
336 |
offset = (offset + n - 1) & ~(n - 1); |
337 |
return offset;
|
338 |
} |
339 |
|
340 |
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
341 |
int nb_sectors, int *pnum) |
342 |
{ |
343 |
uint64_t cluster_offset; |
344 |
|
345 |
*pnum = nb_sectors; |
346 |
cluster_offset = get_cluster_offset(bs, sector_num << 9, pnum);
|
347 |
|
348 |
return (cluster_offset != 0); |
349 |
} |
350 |
|
351 |
/* handle reading after the end of the backing file */
|
352 |
int backing_read1(BlockDriverState *bs,
|
353 |
int64_t sector_num, uint8_t *buf, int nb_sectors)
|
354 |
{ |
355 |
int n1;
|
356 |
if ((sector_num + nb_sectors) <= bs->total_sectors)
|
357 |
return nb_sectors;
|
358 |
if (sector_num >= bs->total_sectors)
|
359 |
n1 = 0;
|
360 |
else
|
361 |
n1 = bs->total_sectors - sector_num; |
362 |
memset(buf + n1 * 512, 0, 512 * (nb_sectors - n1)); |
363 |
return n1;
|
364 |
} |
365 |
|
366 |
typedef struct QCowAIOCB { |
367 |
BlockDriverAIOCB common; |
368 |
int64_t sector_num; |
369 |
QEMUIOVector *qiov; |
370 |
uint8_t *buf; |
371 |
void *orig_buf;
|
372 |
int nb_sectors;
|
373 |
int n;
|
374 |
uint64_t cluster_offset; |
375 |
uint8_t *cluster_data; |
376 |
BlockDriverAIOCB *hd_aiocb; |
377 |
struct iovec hd_iov;
|
378 |
QEMUIOVector hd_qiov; |
379 |
QEMUBH *bh; |
380 |
QCowL2Meta l2meta; |
381 |
} QCowAIOCB; |
382 |
|
383 |
static void qcow_aio_cancel(BlockDriverAIOCB *blockacb) |
384 |
{ |
385 |
QCowAIOCB *acb = (QCowAIOCB *)blockacb; |
386 |
if (acb->hd_aiocb)
|
387 |
bdrv_aio_cancel(acb->hd_aiocb); |
388 |
qemu_aio_release(acb); |
389 |
} |
390 |
|
391 |
static AIOPool qcow_aio_pool = {
|
392 |
.aiocb_size = sizeof(QCowAIOCB),
|
393 |
.cancel = qcow_aio_cancel, |
394 |
}; |
395 |
|
396 |
static void qcow_aio_read_cb(void *opaque, int ret); |
397 |
static void qcow_aio_read_bh(void *opaque) |
398 |
{ |
399 |
QCowAIOCB *acb = opaque; |
400 |
qemu_bh_delete(acb->bh); |
401 |
acb->bh = NULL;
|
402 |
qcow_aio_read_cb(opaque, 0);
|
403 |
} |
404 |
|
405 |
static int qcow_schedule_bh(QEMUBHFunc *cb, QCowAIOCB *acb) |
406 |
{ |
407 |
if (acb->bh)
|
408 |
return -EIO;
|
409 |
|
410 |
acb->bh = qemu_bh_new(cb, acb); |
411 |
if (!acb->bh)
|
412 |
return -EIO;
|
413 |
|
414 |
qemu_bh_schedule(acb->bh); |
415 |
|
416 |
return 0; |
417 |
} |
418 |
|
419 |
static void qcow_aio_read_cb(void *opaque, int ret) |
420 |
{ |
421 |
QCowAIOCB *acb = opaque; |
422 |
BlockDriverState *bs = acb->common.bs; |
423 |
BDRVQcowState *s = bs->opaque; |
424 |
int index_in_cluster, n1;
|
425 |
|
426 |
acb->hd_aiocb = NULL;
|
427 |
if (ret < 0) |
428 |
goto done;
|
429 |
|
430 |
/* post process the read buffer */
|
431 |
if (!acb->cluster_offset) {
|
432 |
/* nothing to do */
|
433 |
} else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
434 |
/* nothing to do */
|
435 |
} else {
|
436 |
if (s->crypt_method) {
|
437 |
encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf, |
438 |
acb->n, 0,
|
439 |
&s->aes_decrypt_key); |
440 |
} |
441 |
} |
442 |
|
443 |
acb->nb_sectors -= acb->n; |
444 |
acb->sector_num += acb->n; |
445 |
acb->buf += acb->n * 512;
|
446 |
|
447 |
if (acb->nb_sectors == 0) { |
448 |
/* request completed */
|
449 |
ret = 0;
|
450 |
goto done;
|
451 |
} |
452 |
|
453 |
/* prepare next AIO request */
|
454 |
acb->n = acb->nb_sectors; |
455 |
acb->cluster_offset = get_cluster_offset(bs, acb->sector_num << 9, &acb->n);
|
456 |
index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
|
457 |
|
458 |
if (!acb->cluster_offset) {
|
459 |
if (bs->backing_hd) {
|
460 |
/* read from the base image */
|
461 |
n1 = backing_read1(bs->backing_hd, acb->sector_num, |
462 |
acb->buf, acb->n); |
463 |
if (n1 > 0) { |
464 |
acb->hd_iov.iov_base = (void *)acb->buf;
|
465 |
acb->hd_iov.iov_len = acb->n * 512;
|
466 |
qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
|
467 |
acb->hd_aiocb = bdrv_aio_readv(bs->backing_hd, acb->sector_num, |
468 |
&acb->hd_qiov, acb->n, |
469 |
qcow_aio_read_cb, acb); |
470 |
if (acb->hd_aiocb == NULL) |
471 |
goto done;
|
472 |
} else {
|
473 |
ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
474 |
if (ret < 0) |
475 |
goto done;
|
476 |
} |
477 |
} else {
|
478 |
/* Note: in this case, no need to wait */
|
479 |
memset(acb->buf, 0, 512 * acb->n); |
480 |
ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
481 |
if (ret < 0) |
482 |
goto done;
|
483 |
} |
484 |
} else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) { |
485 |
/* add AIO support for compressed blocks ? */
|
486 |
if (decompress_cluster(s, acb->cluster_offset) < 0) |
487 |
goto done;
|
488 |
memcpy(acb->buf, |
489 |
s->cluster_cache + index_in_cluster * 512, 512 * acb->n); |
490 |
ret = qcow_schedule_bh(qcow_aio_read_bh, acb); |
491 |
if (ret < 0) |
492 |
goto done;
|
493 |
} else {
|
494 |
if ((acb->cluster_offset & 511) != 0) { |
495 |
ret = -EIO; |
496 |
goto done;
|
497 |
} |
498 |
|
499 |
acb->hd_iov.iov_base = (void *)acb->buf;
|
500 |
acb->hd_iov.iov_len = acb->n * 512;
|
501 |
qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
|
502 |
acb->hd_aiocb = bdrv_aio_readv(s->hd, |
503 |
(acb->cluster_offset >> 9) + index_in_cluster,
|
504 |
&acb->hd_qiov, acb->n, qcow_aio_read_cb, acb); |
505 |
if (acb->hd_aiocb == NULL) |
506 |
goto done;
|
507 |
} |
508 |
|
509 |
return;
|
510 |
done:
|
511 |
if (acb->qiov->niov > 1) { |
512 |
qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size); |
513 |
qemu_vfree(acb->orig_buf); |
514 |
} |
515 |
acb->common.cb(acb->common.opaque, ret); |
516 |
qemu_aio_release(acb); |
517 |
} |
518 |
|
519 |
static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
|
520 |
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
521 |
BlockDriverCompletionFunc *cb, void *opaque, int is_write) |
522 |
{ |
523 |
QCowAIOCB *acb; |
524 |
|
525 |
acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque); |
526 |
if (!acb)
|
527 |
return NULL; |
528 |
acb->hd_aiocb = NULL;
|
529 |
acb->sector_num = sector_num; |
530 |
acb->qiov = qiov; |
531 |
if (qiov->niov > 1) { |
532 |
acb->buf = acb->orig_buf = qemu_blockalign(bs, qiov->size); |
533 |
if (is_write)
|
534 |
qemu_iovec_to_buffer(qiov, acb->buf); |
535 |
} else {
|
536 |
acb->buf = (uint8_t *)qiov->iov->iov_base; |
537 |
} |
538 |
acb->nb_sectors = nb_sectors; |
539 |
acb->n = 0;
|
540 |
acb->cluster_offset = 0;
|
541 |
acb->l2meta.nb_clusters = 0;
|
542 |
return acb;
|
543 |
} |
544 |
|
545 |
static BlockDriverAIOCB *qcow_aio_readv(BlockDriverState *bs,
|
546 |
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
547 |
BlockDriverCompletionFunc *cb, void *opaque)
|
548 |
{ |
549 |
QCowAIOCB *acb; |
550 |
|
551 |
acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
|
552 |
if (!acb)
|
553 |
return NULL; |
554 |
|
555 |
qcow_aio_read_cb(acb, 0);
|
556 |
return &acb->common;
|
557 |
} |
558 |
|
559 |
static void qcow_aio_write_cb(void *opaque, int ret) |
560 |
{ |
561 |
QCowAIOCB *acb = opaque; |
562 |
BlockDriverState *bs = acb->common.bs; |
563 |
BDRVQcowState *s = bs->opaque; |
564 |
int index_in_cluster;
|
565 |
const uint8_t *src_buf;
|
566 |
int n_end;
|
567 |
|
568 |
acb->hd_aiocb = NULL;
|
569 |
|
570 |
if (ret < 0) |
571 |
goto done;
|
572 |
|
573 |
if (alloc_cluster_link_l2(bs, acb->cluster_offset, &acb->l2meta) < 0) { |
574 |
free_any_clusters(bs, acb->cluster_offset, acb->l2meta.nb_clusters); |
575 |
goto done;
|
576 |
} |
577 |
|
578 |
acb->nb_sectors -= acb->n; |
579 |
acb->sector_num += acb->n; |
580 |
acb->buf += acb->n * 512;
|
581 |
|
582 |
if (acb->nb_sectors == 0) { |
583 |
/* request completed */
|
584 |
ret = 0;
|
585 |
goto done;
|
586 |
} |
587 |
|
588 |
index_in_cluster = acb->sector_num & (s->cluster_sectors - 1);
|
589 |
n_end = index_in_cluster + acb->nb_sectors; |
590 |
if (s->crypt_method &&
|
591 |
n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors) |
592 |
n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; |
593 |
|
594 |
acb->cluster_offset = alloc_cluster_offset(bs, acb->sector_num << 9,
|
595 |
index_in_cluster, |
596 |
n_end, &acb->n, &acb->l2meta); |
597 |
if (!acb->cluster_offset || (acb->cluster_offset & 511) != 0) { |
598 |
ret = -EIO; |
599 |
goto done;
|
600 |
} |
601 |
if (s->crypt_method) {
|
602 |
if (!acb->cluster_data) {
|
603 |
acb->cluster_data = qemu_mallocz(QCOW_MAX_CRYPT_CLUSTERS * |
604 |
s->cluster_size); |
605 |
} |
606 |
encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf, |
607 |
acb->n, 1, &s->aes_encrypt_key);
|
608 |
src_buf = acb->cluster_data; |
609 |
} else {
|
610 |
src_buf = acb->buf; |
611 |
} |
612 |
acb->hd_iov.iov_base = (void *)src_buf;
|
613 |
acb->hd_iov.iov_len = acb->n * 512;
|
614 |
qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
|
615 |
acb->hd_aiocb = bdrv_aio_writev(s->hd, |
616 |
(acb->cluster_offset >> 9) + index_in_cluster,
|
617 |
&acb->hd_qiov, acb->n, |
618 |
qcow_aio_write_cb, acb); |
619 |
if (acb->hd_aiocb == NULL) |
620 |
goto done;
|
621 |
|
622 |
return;
|
623 |
|
624 |
done:
|
625 |
if (acb->qiov->niov > 1) |
626 |
qemu_vfree(acb->orig_buf); |
627 |
acb->common.cb(acb->common.opaque, ret); |
628 |
qemu_aio_release(acb); |
629 |
} |
630 |
|
631 |
static BlockDriverAIOCB *qcow_aio_writev(BlockDriverState *bs,
|
632 |
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
633 |
BlockDriverCompletionFunc *cb, void *opaque)
|
634 |
{ |
635 |
BDRVQcowState *s = bs->opaque; |
636 |
QCowAIOCB *acb; |
637 |
|
638 |
s->cluster_cache_offset = -1; /* disable compressed cache */ |
639 |
|
640 |
acb = qcow_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
|
641 |
if (!acb)
|
642 |
return NULL; |
643 |
|
644 |
qcow_aio_write_cb(acb, 0);
|
645 |
return &acb->common;
|
646 |
} |
647 |
|
648 |
static void qcow_close(BlockDriverState *bs) |
649 |
{ |
650 |
BDRVQcowState *s = bs->opaque; |
651 |
qemu_free(s->l1_table); |
652 |
qemu_free(s->l2_cache); |
653 |
qemu_free(s->cluster_cache); |
654 |
qemu_free(s->cluster_data); |
655 |
refcount_close(bs); |
656 |
bdrv_delete(s->hd); |
657 |
} |
658 |
|
659 |
static int get_bits_from_size(size_t size) |
660 |
{ |
661 |
int res = 0; |
662 |
|
663 |
if (size == 0) { |
664 |
return -1; |
665 |
} |
666 |
|
667 |
while (size != 1) { |
668 |
/* Not a power of two */
|
669 |
if (size & 1) { |
670 |
return -1; |
671 |
} |
672 |
|
673 |
size >>= 1;
|
674 |
res++; |
675 |
} |
676 |
|
677 |
return res;
|
678 |
} |
679 |
|
680 |
static int qcow_create2(const char *filename, int64_t total_size, |
681 |
const char *backing_file, const char *backing_format, |
682 |
int flags, size_t cluster_size)
|
683 |
{ |
684 |
|
685 |
int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
|
686 |
int ref_clusters, backing_format_len = 0; |
687 |
QCowHeader header; |
688 |
uint64_t tmp, offset; |
689 |
QCowCreateState s1, *s = &s1; |
690 |
QCowExtension ext_bf = {0, 0}; |
691 |
|
692 |
|
693 |
memset(s, 0, sizeof(*s)); |
694 |
|
695 |
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
|
696 |
if (fd < 0) |
697 |
return -1; |
698 |
memset(&header, 0, sizeof(header)); |
699 |
header.magic = cpu_to_be32(QCOW_MAGIC); |
700 |
header.version = cpu_to_be32(QCOW_VERSION); |
701 |
header.size = cpu_to_be64(total_size * 512);
|
702 |
header_size = sizeof(header);
|
703 |
backing_filename_len = 0;
|
704 |
if (backing_file) {
|
705 |
if (backing_format) {
|
706 |
ext_bf.magic = QCOW_EXT_MAGIC_BACKING_FORMAT; |
707 |
backing_format_len = strlen(backing_format); |
708 |
ext_bf.len = (backing_format_len + 7) & ~7; |
709 |
header_size += ((sizeof(ext_bf) + ext_bf.len + 7) & ~7); |
710 |
} |
711 |
header.backing_file_offset = cpu_to_be64(header_size); |
712 |
backing_filename_len = strlen(backing_file); |
713 |
header.backing_file_size = cpu_to_be32(backing_filename_len); |
714 |
header_size += backing_filename_len; |
715 |
} |
716 |
|
717 |
/* Cluster size */
|
718 |
s->cluster_bits = get_bits_from_size(cluster_size); |
719 |
if (s->cluster_bits < MIN_CLUSTER_BITS ||
|
720 |
s->cluster_bits > MAX_CLUSTER_BITS) |
721 |
{ |
722 |
fprintf(stderr, "Cluster size must be a power of two between "
|
723 |
"%d and %dk\n",
|
724 |
1 << MIN_CLUSTER_BITS,
|
725 |
1 << (MAX_CLUSTER_BITS - 10)); |
726 |
return -EINVAL;
|
727 |
} |
728 |
s->cluster_size = 1 << s->cluster_bits;
|
729 |
|
730 |
header.cluster_bits = cpu_to_be32(s->cluster_bits); |
731 |
header_size = (header_size + 7) & ~7; |
732 |
if (flags & BLOCK_FLAG_ENCRYPT) {
|
733 |
header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES); |
734 |
} else {
|
735 |
header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE); |
736 |
} |
737 |
l2_bits = s->cluster_bits - 3;
|
738 |
shift = s->cluster_bits + l2_bits; |
739 |
l1_size = (((total_size * 512) + (1LL << shift) - 1) >> shift); |
740 |
offset = align_offset(header_size, s->cluster_size); |
741 |
s->l1_table_offset = offset; |
742 |
header.l1_table_offset = cpu_to_be64(s->l1_table_offset); |
743 |
header.l1_size = cpu_to_be32(l1_size); |
744 |
offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
|
745 |
|
746 |
s->refcount_table = qemu_mallocz(s->cluster_size); |
747 |
|
748 |
s->refcount_table_offset = offset; |
749 |
header.refcount_table_offset = cpu_to_be64(offset); |
750 |
header.refcount_table_clusters = cpu_to_be32(1);
|
751 |
offset += s->cluster_size; |
752 |
s->refcount_block_offset = offset; |
753 |
|
754 |
/* count how many refcount blocks needed */
|
755 |
tmp = offset >> s->cluster_bits; |
756 |
ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1;
|
757 |
for (i=0; i < ref_clusters; i++) { |
758 |
s->refcount_table[i] = cpu_to_be64(offset); |
759 |
offset += s->cluster_size; |
760 |
} |
761 |
|
762 |
s->refcount_block = qemu_mallocz(ref_clusters * s->cluster_size); |
763 |
|
764 |
/* update refcounts */
|
765 |
create_refcount_update(s, 0, header_size);
|
766 |
create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t));
|
767 |
create_refcount_update(s, s->refcount_table_offset, s->cluster_size); |
768 |
create_refcount_update(s, s->refcount_block_offset, ref_clusters * s->cluster_size); |
769 |
|
770 |
/* write all the data */
|
771 |
write(fd, &header, sizeof(header));
|
772 |
if (backing_file) {
|
773 |
if (backing_format_len) {
|
774 |
char zero[16]; |
775 |
int d = ext_bf.len - backing_format_len;
|
776 |
|
777 |
memset(zero, 0, sizeof(zero)); |
778 |
cpu_to_be32s(&ext_bf.magic); |
779 |
cpu_to_be32s(&ext_bf.len); |
780 |
write(fd, &ext_bf, sizeof(ext_bf));
|
781 |
write(fd, backing_format, backing_format_len); |
782 |
if (d>0) { |
783 |
write(fd, zero, d); |
784 |
} |
785 |
} |
786 |
write(fd, backing_file, backing_filename_len); |
787 |
} |
788 |
lseek(fd, s->l1_table_offset, SEEK_SET); |
789 |
tmp = 0;
|
790 |
for(i = 0;i < l1_size; i++) { |
791 |
write(fd, &tmp, sizeof(tmp));
|
792 |
} |
793 |
lseek(fd, s->refcount_table_offset, SEEK_SET); |
794 |
write(fd, s->refcount_table, s->cluster_size); |
795 |
|
796 |
lseek(fd, s->refcount_block_offset, SEEK_SET); |
797 |
write(fd, s->refcount_block, ref_clusters * s->cluster_size); |
798 |
|
799 |
qemu_free(s->refcount_table); |
800 |
qemu_free(s->refcount_block); |
801 |
close(fd); |
802 |
return 0; |
803 |
} |
804 |
|
805 |
static int qcow_create(const char *filename, QEMUOptionParameter *options) |
806 |
{ |
807 |
const char *backing_file = NULL; |
808 |
const char *backing_fmt = NULL; |
809 |
uint64_t sectors = 0;
|
810 |
int flags = 0; |
811 |
size_t cluster_size = 65536;
|
812 |
|
813 |
/* Read out options */
|
814 |
while (options && options->name) {
|
815 |
if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
816 |
sectors = options->value.n / 512;
|
817 |
} else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { |
818 |
backing_file = options->value.s; |
819 |
} else if (!strcmp(options->name, BLOCK_OPT_BACKING_FMT)) { |
820 |
backing_fmt = options->value.s; |
821 |
} else if (!strcmp(options->name, BLOCK_OPT_ENCRYPT)) { |
822 |
flags |= options->value.n ? BLOCK_FLAG_ENCRYPT : 0;
|
823 |
} else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { |
824 |
if (options->value.n) {
|
825 |
cluster_size = options->value.n; |
826 |
} |
827 |
} |
828 |
options++; |
829 |
} |
830 |
|
831 |
return qcow_create2(filename, sectors, backing_file, backing_fmt, flags,
|
832 |
cluster_size); |
833 |
} |
834 |
|
835 |
static int qcow_make_empty(BlockDriverState *bs) |
836 |
{ |
837 |
#if 0
|
838 |
/* XXX: not correct */
|
839 |
BDRVQcowState *s = bs->opaque;
|
840 |
uint32_t l1_length = s->l1_size * sizeof(uint64_t);
|
841 |
int ret;
|
842 |
|
843 |
memset(s->l1_table, 0, l1_length);
|
844 |
if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
|
845 |
return -1;
|
846 |
ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
|
847 |
if (ret < 0)
|
848 |
return ret;
|
849 |
|
850 |
l2_cache_reset(bs);
|
851 |
#endif
|
852 |
return 0; |
853 |
} |
854 |
|
855 |
/* XXX: put compressed sectors first, then all the cluster aligned
|
856 |
tables to avoid losing bytes in alignment */
|
857 |
static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, |
858 |
const uint8_t *buf, int nb_sectors) |
859 |
{ |
860 |
BDRVQcowState *s = bs->opaque; |
861 |
z_stream strm; |
862 |
int ret, out_len;
|
863 |
uint8_t *out_buf; |
864 |
uint64_t cluster_offset; |
865 |
|
866 |
if (nb_sectors == 0) { |
867 |
/* align end of file to a sector boundary to ease reading with
|
868 |
sector based I/Os */
|
869 |
cluster_offset = bdrv_getlength(s->hd); |
870 |
cluster_offset = (cluster_offset + 511) & ~511; |
871 |
bdrv_truncate(s->hd, cluster_offset); |
872 |
return 0; |
873 |
} |
874 |
|
875 |
if (nb_sectors != s->cluster_sectors)
|
876 |
return -EINVAL;
|
877 |
|
878 |
out_buf = qemu_malloc(s->cluster_size + (s->cluster_size / 1000) + 128); |
879 |
|
880 |
/* best compression, small window, no zlib header */
|
881 |
memset(&strm, 0, sizeof(strm)); |
882 |
ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, |
883 |
Z_DEFLATED, -12,
|
884 |
9, Z_DEFAULT_STRATEGY);
|
885 |
if (ret != 0) { |
886 |
qemu_free(out_buf); |
887 |
return -1; |
888 |
} |
889 |
|
890 |
strm.avail_in = s->cluster_size; |
891 |
strm.next_in = (uint8_t *)buf; |
892 |
strm.avail_out = s->cluster_size; |
893 |
strm.next_out = out_buf; |
894 |
|
895 |
ret = deflate(&strm, Z_FINISH); |
896 |
if (ret != Z_STREAM_END && ret != Z_OK) {
|
897 |
qemu_free(out_buf); |
898 |
deflateEnd(&strm); |
899 |
return -1; |
900 |
} |
901 |
out_len = strm.next_out - out_buf; |
902 |
|
903 |
deflateEnd(&strm); |
904 |
|
905 |
if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
|
906 |
/* could not compress: write normal cluster */
|
907 |
bdrv_write(bs, sector_num, buf, s->cluster_sectors); |
908 |
} else {
|
909 |
cluster_offset = alloc_compressed_cluster_offset(bs, sector_num << 9,
|
910 |
out_len); |
911 |
if (!cluster_offset)
|
912 |
return -1; |
913 |
cluster_offset &= s->cluster_offset_mask; |
914 |
if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
|
915 |
qemu_free(out_buf); |
916 |
return -1; |
917 |
} |
918 |
} |
919 |
|
920 |
qemu_free(out_buf); |
921 |
return 0; |
922 |
} |
923 |
|
924 |
static void qcow_flush(BlockDriverState *bs) |
925 |
{ |
926 |
BDRVQcowState *s = bs->opaque; |
927 |
bdrv_flush(s->hd); |
928 |
} |
929 |
|
930 |
static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
931 |
{ |
932 |
BDRVQcowState *s = bs->opaque; |
933 |
bdi->cluster_size = s->cluster_size; |
934 |
bdi->vm_state_offset = (int64_t)s->l1_vm_state_index << |
935 |
(s->cluster_bits + s->l2_bits); |
936 |
return 0; |
937 |
} |
938 |
|
939 |
/*********************************************************/
|
940 |
/* snapshot support */
|
941 |
|
942 |
|
943 |
static void qcow_free_snapshots(BlockDriverState *bs) |
944 |
{ |
945 |
BDRVQcowState *s = bs->opaque; |
946 |
int i;
|
947 |
|
948 |
for(i = 0; i < s->nb_snapshots; i++) { |
949 |
qemu_free(s->snapshots[i].name); |
950 |
qemu_free(s->snapshots[i].id_str); |
951 |
} |
952 |
qemu_free(s->snapshots); |
953 |
s->snapshots = NULL;
|
954 |
s->nb_snapshots = 0;
|
955 |
} |
956 |
|
957 |
static int qcow_read_snapshots(BlockDriverState *bs) |
958 |
{ |
959 |
BDRVQcowState *s = bs->opaque; |
960 |
QCowSnapshotHeader h; |
961 |
QCowSnapshot *sn; |
962 |
int i, id_str_size, name_size;
|
963 |
int64_t offset; |
964 |
uint32_t extra_data_size; |
965 |
|
966 |
if (!s->nb_snapshots) {
|
967 |
s->snapshots = NULL;
|
968 |
s->snapshots_size = 0;
|
969 |
return 0; |
970 |
} |
971 |
|
972 |
offset = s->snapshots_offset; |
973 |
s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
|
974 |
for(i = 0; i < s->nb_snapshots; i++) { |
975 |
offset = align_offset(offset, 8);
|
976 |
if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h)) |
977 |
goto fail;
|
978 |
offset += sizeof(h);
|
979 |
sn = s->snapshots + i; |
980 |
sn->l1_table_offset = be64_to_cpu(h.l1_table_offset); |
981 |
sn->l1_size = be32_to_cpu(h.l1_size); |
982 |
sn->vm_state_size = be32_to_cpu(h.vm_state_size); |
983 |
sn->date_sec = be32_to_cpu(h.date_sec); |
984 |
sn->date_nsec = be32_to_cpu(h.date_nsec); |
985 |
sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec); |
986 |
extra_data_size = be32_to_cpu(h.extra_data_size); |
987 |
|
988 |
id_str_size = be16_to_cpu(h.id_str_size); |
989 |
name_size = be16_to_cpu(h.name_size); |
990 |
|
991 |
offset += extra_data_size; |
992 |
|
993 |
sn->id_str = qemu_malloc(id_str_size + 1);
|
994 |
if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
|
995 |
goto fail;
|
996 |
offset += id_str_size; |
997 |
sn->id_str[id_str_size] = '\0';
|
998 |
|
999 |
sn->name = qemu_malloc(name_size + 1);
|
1000 |
if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size)
|
1001 |
goto fail;
|
1002 |
offset += name_size; |
1003 |
sn->name[name_size] = '\0';
|
1004 |
} |
1005 |
s->snapshots_size = offset - s->snapshots_offset; |
1006 |
return 0; |
1007 |
fail:
|
1008 |
qcow_free_snapshots(bs); |
1009 |
return -1; |
1010 |
} |
1011 |
|
1012 |
/* add at the end of the file a new list of snapshots */
|
1013 |
static int qcow_write_snapshots(BlockDriverState *bs) |
1014 |
{ |
1015 |
BDRVQcowState *s = bs->opaque; |
1016 |
QCowSnapshot *sn; |
1017 |
QCowSnapshotHeader h; |
1018 |
int i, name_size, id_str_size, snapshots_size;
|
1019 |
uint64_t data64; |
1020 |
uint32_t data32; |
1021 |
int64_t offset, snapshots_offset; |
1022 |
|
1023 |
/* compute the size of the snapshots */
|
1024 |
offset = 0;
|
1025 |
for(i = 0; i < s->nb_snapshots; i++) { |
1026 |
sn = s->snapshots + i; |
1027 |
offset = align_offset(offset, 8);
|
1028 |
offset += sizeof(h);
|
1029 |
offset += strlen(sn->id_str); |
1030 |
offset += strlen(sn->name); |
1031 |
} |
1032 |
snapshots_size = offset; |
1033 |
|
1034 |
snapshots_offset = alloc_clusters(bs, snapshots_size); |
1035 |
offset = snapshots_offset; |
1036 |
|
1037 |
for(i = 0; i < s->nb_snapshots; i++) { |
1038 |
sn = s->snapshots + i; |
1039 |
memset(&h, 0, sizeof(h)); |
1040 |
h.l1_table_offset = cpu_to_be64(sn->l1_table_offset); |
1041 |
h.l1_size = cpu_to_be32(sn->l1_size); |
1042 |
h.vm_state_size = cpu_to_be32(sn->vm_state_size); |
1043 |
h.date_sec = cpu_to_be32(sn->date_sec); |
1044 |
h.date_nsec = cpu_to_be32(sn->date_nsec); |
1045 |
h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec); |
1046 |
|
1047 |
id_str_size = strlen(sn->id_str); |
1048 |
name_size = strlen(sn->name); |
1049 |
h.id_str_size = cpu_to_be16(id_str_size); |
1050 |
h.name_size = cpu_to_be16(name_size); |
1051 |
offset = align_offset(offset, 8);
|
1052 |
if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h)) |
1053 |
goto fail;
|
1054 |
offset += sizeof(h);
|
1055 |
if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
|
1056 |
goto fail;
|
1057 |
offset += id_str_size; |
1058 |
if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size)
|
1059 |
goto fail;
|
1060 |
offset += name_size; |
1061 |
} |
1062 |
|
1063 |
/* update the various header fields */
|
1064 |
data64 = cpu_to_be64(snapshots_offset); |
1065 |
if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset),
|
1066 |
&data64, sizeof(data64)) != sizeof(data64)) |
1067 |
goto fail;
|
1068 |
data32 = cpu_to_be32(s->nb_snapshots); |
1069 |
if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots),
|
1070 |
&data32, sizeof(data32)) != sizeof(data32)) |
1071 |
goto fail;
|
1072 |
|
1073 |
/* free the old snapshot table */
|
1074 |
free_clusters(bs, s->snapshots_offset, s->snapshots_size); |
1075 |
s->snapshots_offset = snapshots_offset; |
1076 |
s->snapshots_size = snapshots_size; |
1077 |
return 0; |
1078 |
fail:
|
1079 |
return -1; |
1080 |
} |
1081 |
|
1082 |
static void find_new_snapshot_id(BlockDriverState *bs, |
1083 |
char *id_str, int id_str_size) |
1084 |
{ |
1085 |
BDRVQcowState *s = bs->opaque; |
1086 |
QCowSnapshot *sn; |
1087 |
int i, id, id_max = 0; |
1088 |
|
1089 |
for(i = 0; i < s->nb_snapshots; i++) { |
1090 |
sn = s->snapshots + i; |
1091 |
id = strtoul(sn->id_str, NULL, 10); |
1092 |
if (id > id_max)
|
1093 |
id_max = id; |
1094 |
} |
1095 |
snprintf(id_str, id_str_size, "%d", id_max + 1); |
1096 |
} |
1097 |
|
1098 |
static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str) |
1099 |
{ |
1100 |
BDRVQcowState *s = bs->opaque; |
1101 |
int i;
|
1102 |
|
1103 |
for(i = 0; i < s->nb_snapshots; i++) { |
1104 |
if (!strcmp(s->snapshots[i].id_str, id_str))
|
1105 |
return i;
|
1106 |
} |
1107 |
return -1; |
1108 |
} |
1109 |
|
1110 |
static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name) |
1111 |
{ |
1112 |
BDRVQcowState *s = bs->opaque; |
1113 |
int i, ret;
|
1114 |
|
1115 |
ret = find_snapshot_by_id(bs, name); |
1116 |
if (ret >= 0) |
1117 |
return ret;
|
1118 |
for(i = 0; i < s->nb_snapshots; i++) { |
1119 |
if (!strcmp(s->snapshots[i].name, name))
|
1120 |
return i;
|
1121 |
} |
1122 |
return -1; |
1123 |
} |
1124 |
|
1125 |
/* if no id is provided, a new one is constructed */
|
1126 |
static int qcow_snapshot_create(BlockDriverState *bs, |
1127 |
QEMUSnapshotInfo *sn_info) |
1128 |
{ |
1129 |
BDRVQcowState *s = bs->opaque; |
1130 |
QCowSnapshot *snapshots1, sn1, *sn = &sn1; |
1131 |
int i, ret;
|
1132 |
uint64_t *l1_table = NULL;
|
1133 |
|
1134 |
memset(sn, 0, sizeof(*sn)); |
1135 |
|
1136 |
if (sn_info->id_str[0] == '\0') { |
1137 |
/* compute a new id */
|
1138 |
find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
|
1139 |
} |
1140 |
|
1141 |
/* check that the ID is unique */
|
1142 |
if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) |
1143 |
return -ENOENT;
|
1144 |
|
1145 |
sn->id_str = qemu_strdup(sn_info->id_str); |
1146 |
if (!sn->id_str)
|
1147 |
goto fail;
|
1148 |
sn->name = qemu_strdup(sn_info->name); |
1149 |
if (!sn->name)
|
1150 |
goto fail;
|
1151 |
sn->vm_state_size = sn_info->vm_state_size; |
1152 |
sn->date_sec = sn_info->date_sec; |
1153 |
sn->date_nsec = sn_info->date_nsec; |
1154 |
sn->vm_clock_nsec = sn_info->vm_clock_nsec; |
1155 |
|
1156 |
ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
|
1157 |
if (ret < 0) |
1158 |
goto fail;
|
1159 |
|
1160 |
/* create the L1 table of the snapshot */
|
1161 |
sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
|
1162 |
sn->l1_size = s->l1_size; |
1163 |
|
1164 |
l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
|
1165 |
for(i = 0; i < s->l1_size; i++) { |
1166 |
l1_table[i] = cpu_to_be64(s->l1_table[i]); |
1167 |
} |
1168 |
if (bdrv_pwrite(s->hd, sn->l1_table_offset,
|
1169 |
l1_table, s->l1_size * sizeof(uint64_t)) !=
|
1170 |
(s->l1_size * sizeof(uint64_t)))
|
1171 |
goto fail;
|
1172 |
qemu_free(l1_table); |
1173 |
l1_table = NULL;
|
1174 |
|
1175 |
snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot)); |
1176 |
if (s->snapshots) {
|
1177 |
memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot));
|
1178 |
qemu_free(s->snapshots); |
1179 |
} |
1180 |
s->snapshots = snapshots1; |
1181 |
s->snapshots[s->nb_snapshots++] = *sn; |
1182 |
|
1183 |
if (qcow_write_snapshots(bs) < 0) |
1184 |
goto fail;
|
1185 |
#ifdef DEBUG_ALLOC
|
1186 |
check_refcounts(bs); |
1187 |
#endif
|
1188 |
return 0; |
1189 |
fail:
|
1190 |
qemu_free(sn->name); |
1191 |
qemu_free(l1_table); |
1192 |
return -1; |
1193 |
} |
1194 |
|
1195 |
/* copy the snapshot 'snapshot_name' into the current disk image */
|
1196 |
static int qcow_snapshot_goto(BlockDriverState *bs, |
1197 |
const char *snapshot_id) |
1198 |
{ |
1199 |
BDRVQcowState *s = bs->opaque; |
1200 |
QCowSnapshot *sn; |
1201 |
int i, snapshot_index, l1_size2;
|
1202 |
|
1203 |
snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id); |
1204 |
if (snapshot_index < 0) |
1205 |
return -ENOENT;
|
1206 |
sn = &s->snapshots[snapshot_index]; |
1207 |
|
1208 |
if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0) |
1209 |
goto fail;
|
1210 |
|
1211 |
if (grow_l1_table(bs, sn->l1_size) < 0) |
1212 |
goto fail;
|
1213 |
|
1214 |
s->l1_size = sn->l1_size; |
1215 |
l1_size2 = s->l1_size * sizeof(uint64_t);
|
1216 |
/* copy the snapshot l1 table to the current l1 table */
|
1217 |
if (bdrv_pread(s->hd, sn->l1_table_offset,
|
1218 |
s->l1_table, l1_size2) != l1_size2) |
1219 |
goto fail;
|
1220 |
if (bdrv_pwrite(s->hd, s->l1_table_offset,
|
1221 |
s->l1_table, l1_size2) != l1_size2) |
1222 |
goto fail;
|
1223 |
for(i = 0;i < s->l1_size; i++) { |
1224 |
be64_to_cpus(&s->l1_table[i]); |
1225 |
} |
1226 |
|
1227 |
if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0) |
1228 |
goto fail;
|
1229 |
|
1230 |
#ifdef DEBUG_ALLOC
|
1231 |
check_refcounts(bs); |
1232 |
#endif
|
1233 |
return 0; |
1234 |
fail:
|
1235 |
return -EIO;
|
1236 |
} |
1237 |
|
1238 |
static int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
1239 |
{ |
1240 |
BDRVQcowState *s = bs->opaque; |
1241 |
QCowSnapshot *sn; |
1242 |
int snapshot_index, ret;
|
1243 |
|
1244 |
snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id); |
1245 |
if (snapshot_index < 0) |
1246 |
return -ENOENT;
|
1247 |
sn = &s->snapshots[snapshot_index]; |
1248 |
|
1249 |
ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
|
1250 |
if (ret < 0) |
1251 |
return ret;
|
1252 |
/* must update the copied flag on the current cluster offsets */
|
1253 |
ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
|
1254 |
if (ret < 0) |
1255 |
return ret;
|
1256 |
free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
|
1257 |
|
1258 |
qemu_free(sn->id_str); |
1259 |
qemu_free(sn->name); |
1260 |
memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn)); |
1261 |
s->nb_snapshots--; |
1262 |
ret = qcow_write_snapshots(bs); |
1263 |
if (ret < 0) { |
1264 |
/* XXX: restore snapshot if error ? */
|
1265 |
return ret;
|
1266 |
} |
1267 |
#ifdef DEBUG_ALLOC
|
1268 |
check_refcounts(bs); |
1269 |
#endif
|
1270 |
return 0; |
1271 |
} |
1272 |
|
1273 |
static int qcow_snapshot_list(BlockDriverState *bs, |
1274 |
QEMUSnapshotInfo **psn_tab) |
1275 |
{ |
1276 |
BDRVQcowState *s = bs->opaque; |
1277 |
QEMUSnapshotInfo *sn_tab, *sn_info; |
1278 |
QCowSnapshot *sn; |
1279 |
int i;
|
1280 |
|
1281 |
if (!s->nb_snapshots) {
|
1282 |
*psn_tab = NULL;
|
1283 |
return s->nb_snapshots;
|
1284 |
} |
1285 |
|
1286 |
sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo));
|
1287 |
for(i = 0; i < s->nb_snapshots; i++) { |
1288 |
sn_info = sn_tab + i; |
1289 |
sn = s->snapshots + i; |
1290 |
pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
|
1291 |
sn->id_str); |
1292 |
pstrcpy(sn_info->name, sizeof(sn_info->name),
|
1293 |
sn->name); |
1294 |
sn_info->vm_state_size = sn->vm_state_size; |
1295 |
sn_info->date_sec = sn->date_sec; |
1296 |
sn_info->date_nsec = sn->date_nsec; |
1297 |
sn_info->vm_clock_nsec = sn->vm_clock_nsec; |
1298 |
} |
1299 |
*psn_tab = sn_tab; |
1300 |
return s->nb_snapshots;
|
1301 |
} |
1302 |
|
1303 |
static int qcow_check(BlockDriverState *bs) |
1304 |
{ |
1305 |
return check_refcounts(bs);
|
1306 |
} |
1307 |
|
1308 |
#if 0
|
1309 |
static void dump_refcounts(BlockDriverState *bs)
|
1310 |
{
|
1311 |
BDRVQcowState *s = bs->opaque;
|
1312 |
int64_t nb_clusters, k, k1, size;
|
1313 |
int refcount;
|
1314 |
|
1315 |
size = bdrv_getlength(s->hd);
|
1316 |
nb_clusters = size_to_clusters(s, size);
|
1317 |
for(k = 0; k < nb_clusters;) {
|
1318 |
k1 = k;
|
1319 |
refcount = get_refcount(bs, k);
|
1320 |
k++;
|
1321 |
while (k < nb_clusters && get_refcount(bs, k) == refcount)
|
1322 |
k++;
|
1323 |
printf("%lld: refcount=%d nb=%lld\n", k, refcount, k - k1);
|
1324 |
}
|
1325 |
}
|
1326 |
#endif
|
1327 |
|
1328 |
static int qcow_put_buffer(BlockDriverState *bs, const uint8_t *buf, |
1329 |
int64_t pos, int size)
|
1330 |
{ |
1331 |
int growable = bs->growable;
|
1332 |
|
1333 |
bs->growable = 1;
|
1334 |
bdrv_pwrite(bs, pos, buf, size); |
1335 |
bs->growable = growable; |
1336 |
|
1337 |
return size;
|
1338 |
} |
1339 |
|
1340 |
static int qcow_get_buffer(BlockDriverState *bs, uint8_t *buf, |
1341 |
int64_t pos, int size)
|
1342 |
{ |
1343 |
int growable = bs->growable;
|
1344 |
int ret;
|
1345 |
|
1346 |
bs->growable = 1;
|
1347 |
ret = bdrv_pread(bs, pos, buf, size); |
1348 |
bs->growable = growable; |
1349 |
|
1350 |
return ret;
|
1351 |
} |
1352 |
|
1353 |
static QEMUOptionParameter qcow_create_options[] = {
|
1354 |
{ |
1355 |
.name = BLOCK_OPT_SIZE, |
1356 |
.type = OPT_SIZE, |
1357 |
.help = "Virtual disk size"
|
1358 |
}, |
1359 |
{ |
1360 |
.name = BLOCK_OPT_BACKING_FILE, |
1361 |
.type = OPT_STRING, |
1362 |
.help = "File name of a base image"
|
1363 |
}, |
1364 |
{ |
1365 |
.name = BLOCK_OPT_BACKING_FMT, |
1366 |
.type = OPT_STRING, |
1367 |
.help = "Image format of the base image"
|
1368 |
}, |
1369 |
{ |
1370 |
.name = BLOCK_OPT_ENCRYPT, |
1371 |
.type = OPT_FLAG, |
1372 |
.help = "Encrypt the image"
|
1373 |
}, |
1374 |
{ |
1375 |
.name = BLOCK_OPT_CLUSTER_SIZE, |
1376 |
.type = OPT_SIZE, |
1377 |
.help = "qcow2 cluster size"
|
1378 |
}, |
1379 |
{ NULL }
|
1380 |
}; |
1381 |
|
1382 |
static BlockDriver bdrv_qcow2 = {
|
1383 |
.format_name = "qcow2",
|
1384 |
.instance_size = sizeof(BDRVQcowState),
|
1385 |
.bdrv_probe = qcow_probe, |
1386 |
.bdrv_open = qcow_open, |
1387 |
.bdrv_close = qcow_close, |
1388 |
.bdrv_create = qcow_create, |
1389 |
.bdrv_flush = qcow_flush, |
1390 |
.bdrv_is_allocated = qcow_is_allocated, |
1391 |
.bdrv_set_key = qcow_set_key, |
1392 |
.bdrv_make_empty = qcow_make_empty, |
1393 |
|
1394 |
.bdrv_aio_readv = qcow_aio_readv, |
1395 |
.bdrv_aio_writev = qcow_aio_writev, |
1396 |
.bdrv_write_compressed = qcow_write_compressed, |
1397 |
|
1398 |
.bdrv_snapshot_create = qcow_snapshot_create, |
1399 |
.bdrv_snapshot_goto = qcow_snapshot_goto, |
1400 |
.bdrv_snapshot_delete = qcow_snapshot_delete, |
1401 |
.bdrv_snapshot_list = qcow_snapshot_list, |
1402 |
.bdrv_get_info = qcow_get_info, |
1403 |
|
1404 |
.bdrv_put_buffer = qcow_put_buffer, |
1405 |
.bdrv_get_buffer = qcow_get_buffer, |
1406 |
|
1407 |
.create_options = qcow_create_options, |
1408 |
.bdrv_check = qcow_check, |
1409 |
}; |
1410 |
|
1411 |
static void bdrv_qcow2_init(void) |
1412 |
{ |
1413 |
bdrv_register(&bdrv_qcow2); |
1414 |
} |
1415 |
|
1416 |
block_init(bdrv_qcow2_init); |