]> Git Repo - qemu.git/blame - block/qcow.c
qemu-common: stop including qemu/bswap.h from qemu-common.h
[qemu.git] / block / qcow.c
CommitLineData
ea2384d3
FB
1/*
2 * Block driver for the QCOW format
5fafdf24 3 *
83f64091 4 * Copyright (c) 2004-2006 Fabrice Bellard
5fafdf24 5 *
ea2384d3
FB
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 */
80c71a24 24#include "qemu/osdep.h"
da34e65c 25#include "qapi/error.h"
faf07963 26#include "qemu-common.h"
e6ff69bf 27#include "qemu/error-report.h"
737e150e 28#include "block/block_int.h"
6af40160 29#include "sysemu/block-backend.h"
1de7afc9 30#include "qemu/module.h"
58369e22 31#include "qemu/bswap.h"
28d34b82 32#include <zlib.h>
cc7a8ea7 33#include "qapi/qmp/qerror.h"
f6fa64f6 34#include "crypto/cipher.h"
caf71f86 35#include "migration/migration.h"
ea2384d3
FB
36
37/**************************************************************/
38/* QEMU COW block driver with compression and encryption support */
39
40#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
41#define QCOW_VERSION 1
42
43#define QCOW_CRYPT_NONE 0
44#define QCOW_CRYPT_AES 1
45
46#define QCOW_OFLAG_COMPRESSED (1LL << 63)
47
48typedef struct QCowHeader {
49 uint32_t magic;
50 uint32_t version;
51 uint64_t backing_file_offset;
52 uint32_t backing_file_size;
53 uint32_t mtime;
54 uint64_t size; /* in bytes */
55 uint8_t cluster_bits;
56 uint8_t l2_bits;
ea54feff 57 uint16_t padding;
ea2384d3
FB
58 uint32_t crypt_method;
59 uint64_t l1_table_offset;
ea54feff 60} QEMU_PACKED QCowHeader;
ea2384d3
FB
61
62#define L2_CACHE_SIZE 16
63
64typedef struct BDRVQcowState {
ea2384d3
FB
65 int cluster_bits;
66 int cluster_size;
67 int cluster_sectors;
68 int l2_bits;
69 int l2_size;
46485de0 70 unsigned int l1_size;
ea2384d3
FB
71 uint64_t cluster_offset_mask;
72 uint64_t l1_table_offset;
73 uint64_t *l1_table;
74 uint64_t *l2_cache;
75 uint64_t l2_cache_offsets[L2_CACHE_SIZE];
76 uint32_t l2_cache_counts[L2_CACHE_SIZE];
77 uint8_t *cluster_cache;
78 uint8_t *cluster_data;
79 uint64_t cluster_cache_offset;
f6fa64f6 80 QCryptoCipher *cipher; /* NULL if no key yet */
ea2384d3 81 uint32_t crypt_method_header;
52b8eb60 82 CoMutex lock;
fd9f102c 83 Error *migration_blocker;
ea2384d3
FB
84} BDRVQcowState;
85
66f82cee 86static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
ea2384d3
FB
87
88static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
89{
90 const QCowHeader *cow_header = (const void *)buf;
3b46e624 91
712e7874
FB
92 if (buf_size >= sizeof(QCowHeader) &&
93 be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
5fafdf24 94 be32_to_cpu(cow_header->version) == QCOW_VERSION)
ea2384d3
FB
95 return 100;
96 else
97 return 0;
98}
99
015a1036
HR
100static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
101 Error **errp)
ea2384d3
FB
102{
103 BDRVQcowState *s = bs->opaque;
d66e5cee
KW
104 unsigned int len, i, shift;
105 int ret;
ea2384d3 106 QCowHeader header;
83f64091 107
9a4f4c31 108 ret = bdrv_pread(bs->file->bs, 0, &header, sizeof(header));
84b0ec02 109 if (ret < 0) {
ea2384d3 110 goto fail;
84b0ec02 111 }
ea2384d3
FB
112 be32_to_cpus(&header.magic);
113 be32_to_cpus(&header.version);
114 be64_to_cpus(&header.backing_file_offset);
115 be32_to_cpus(&header.backing_file_size);
116 be32_to_cpus(&header.mtime);
117 be64_to_cpus(&header.size);
118 be32_to_cpus(&header.crypt_method);
119 be64_to_cpus(&header.l1_table_offset);
3b46e624 120
84b0ec02 121 if (header.magic != QCOW_MAGIC) {
76abe407
PB
122 error_setg(errp, "Image not in qcow format");
123 ret = -EINVAL;
84b0ec02
LZH
124 goto fail;
125 }
126 if (header.version != QCOW_VERSION) {
a55448b3 127 error_setg(errp, "Unsupported qcow version %" PRIu32, header.version);
84b0ec02 128 ret = -ENOTSUP;
ea2384d3 129 goto fail;
84b0ec02
LZH
130 }
131
7159a45b
KW
132 if (header.size <= 1) {
133 error_setg(errp, "Image size is too small (must be at least 2 bytes)");
84b0ec02 134 ret = -EINVAL;
ea2384d3 135 goto fail;
84b0ec02 136 }
7159a45b
KW
137 if (header.cluster_bits < 9 || header.cluster_bits > 16) {
138 error_setg(errp, "Cluster size must be between 512 and 64k");
139 ret = -EINVAL;
140 goto fail;
141 }
142
42eb5817
KW
143 /* l2_bits specifies number of entries; storing a uint64_t in each entry,
144 * so bytes = num_entries << 3. */
145 if (header.l2_bits < 9 - 3 || header.l2_bits > 16 - 3) {
146 error_setg(errp, "L2 table size must be between 512 and 64k");
147 ret = -EINVAL;
148 goto fail;
149 }
150
84b0ec02 151 if (header.crypt_method > QCOW_CRYPT_AES) {
b6d5066d 152 error_setg(errp, "invalid encryption method in qcow header");
84b0ec02 153 ret = -EINVAL;
ea2384d3 154 goto fail;
84b0ec02 155 }
f6fa64f6
DB
156 if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128)) {
157 error_setg(errp, "AES cipher not available");
158 ret = -EINVAL;
159 goto fail;
160 }
ea2384d3 161 s->crypt_method_header = header.crypt_method;
84b0ec02 162 if (s->crypt_method_header) {
e6ff69bf
DB
163 if (bdrv_uses_whitelist() &&
164 s->crypt_method_header == QCOW_CRYPT_AES) {
165 error_report("qcow built-in AES encryption is deprecated");
166 error_printf("Support for it will be removed in a future release.\n"
167 "You can use 'qemu-img convert' to switch to an\n"
168 "unencrypted qcow image, or a LUKS raw image.\n");
169 }
170
ea2384d3 171 bs->encrypted = 1;
84b0ec02 172 }
ea2384d3
FB
173 s->cluster_bits = header.cluster_bits;
174 s->cluster_size = 1 << s->cluster_bits;
175 s->cluster_sectors = 1 << (s->cluster_bits - 9);
176 s->l2_bits = header.l2_bits;
177 s->l2_size = 1 << s->l2_bits;
178 bs->total_sectors = header.size / 512;
179 s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
180
181 /* read the level 1 table */
182 shift = s->cluster_bits + s->l2_bits;
46485de0
KW
183 if (header.size > UINT64_MAX - (1LL << shift)) {
184 error_setg(errp, "Image too large");
185 ret = -EINVAL;
186 goto fail;
187 } else {
188 uint64_t l1_size = (header.size + (1LL << shift) - 1) >> shift;
189 if (l1_size > INT_MAX / sizeof(uint64_t)) {
190 error_setg(errp, "Image too large");
191 ret = -EINVAL;
192 goto fail;
193 }
194 s->l1_size = l1_size;
195 }
ea2384d3
FB
196
197 s->l1_table_offset = header.l1_table_offset;
5839e53b 198 s->l1_table = g_try_new(uint64_t, s->l1_size);
0df93305
KW
199 if (s->l1_table == NULL) {
200 error_setg(errp, "Could not allocate memory for L1 table");
201 ret = -ENOMEM;
202 goto fail;
203 }
84b0ec02 204
9a4f4c31 205 ret = bdrv_pread(bs->file->bs, s->l1_table_offset, s->l1_table,
84b0ec02
LZH
206 s->l1_size * sizeof(uint64_t));
207 if (ret < 0) {
ea2384d3 208 goto fail;
84b0ec02
LZH
209 }
210
ea2384d3
FB
211 for(i = 0;i < s->l1_size; i++) {
212 be64_to_cpus(&s->l1_table[i]);
213 }
0df93305
KW
214
215 /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
216 s->l2_cache =
9a4f4c31 217 qemu_try_blockalign(bs->file->bs,
0df93305
KW
218 s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
219 if (s->l2_cache == NULL) {
220 error_setg(errp, "Could not allocate L2 table cache");
221 ret = -ENOMEM;
222 goto fail;
223 }
7267c094 224 s->cluster_cache = g_malloc(s->cluster_size);
7267c094 225 s->cluster_data = g_malloc(s->cluster_size);
ea2384d3 226 s->cluster_cache_offset = -1;
3b46e624 227
ea2384d3
FB
228 /* read the backing file name */
229 if (header.backing_file_offset != 0) {
230 len = header.backing_file_size;
e729fa6a 231 if (len > 1023 || len >= sizeof(bs->backing_file)) {
d66e5cee
KW
232 error_setg(errp, "Backing file name too long");
233 ret = -EINVAL;
234 goto fail;
84b0ec02 235 }
9a4f4c31 236 ret = bdrv_pread(bs->file->bs, header.backing_file_offset,
84b0ec02
LZH
237 bs->backing_file, len);
238 if (ret < 0) {
ea2384d3 239 goto fail;
84b0ec02 240 }
ea2384d3
FB
241 bs->backing_file[len] = '\0';
242 }
de33b1f3 243
fd9f102c 244 /* Disable migration when qcow images are used */
81e5f78a
AG
245 error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
246 "does not support live migration",
247 bdrv_get_device_or_node_name(bs));
fd9f102c
KW
248 migrate_add_blocker(s->migration_blocker);
249
de33b1f3 250 qemu_co_mutex_init(&s->lock);
ea2384d3
FB
251 return 0;
252
253 fail:
7267c094 254 g_free(s->l1_table);
0df93305 255 qemu_vfree(s->l2_cache);
7267c094
AL
256 g_free(s->cluster_cache);
257 g_free(s->cluster_data);
84b0ec02 258 return ret;
ea2384d3
FB
259}
260
d177692e
JC
261
262/* We have nothing to do for QCOW reopen, stubs just return
263 * success */
264static int qcow_reopen_prepare(BDRVReopenState *state,
265 BlockReopenQueue *queue, Error **errp)
266{
267 return 0;
268}
269
ea2384d3
FB
270static int qcow_set_key(BlockDriverState *bs, const char *key)
271{
272 BDRVQcowState *s = bs->opaque;
273 uint8_t keybuf[16];
274 int len, i;
f6fa64f6 275 Error *err;
3b46e624 276
ea2384d3
FB
277 memset(keybuf, 0, 16);
278 len = strlen(key);
279 if (len > 16)
280 len = 16;
281 /* XXX: we could compress the chars to 7 bits to increase
282 entropy */
283 for(i = 0;i < len;i++) {
284 keybuf[i] = key[i];
285 }
8336aafa 286 assert(bs->encrypted);
ea2384d3 287
f6fa64f6
DB
288 qcrypto_cipher_free(s->cipher);
289 s->cipher = qcrypto_cipher_new(
290 QCRYPTO_CIPHER_ALG_AES_128,
291 QCRYPTO_CIPHER_MODE_CBC,
292 keybuf, G_N_ELEMENTS(keybuf),
293 &err);
294
295 if (!s->cipher) {
296 /* XXX would be nice if errors in this method could
297 * be properly propagate to the caller. Would need
298 * the bdrv_set_key() API signature to be fixed. */
299 error_free(err);
ea2384d3 300 return -1;
f6fa64f6 301 }
ea2384d3
FB
302 return 0;
303}
304
305/* The crypt function is compatible with the linux cryptoloop
306 algorithm for < 4 GB images. NOTE: out_buf == in_buf is
307 supported */
f6fa64f6
DB
308static int encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
309 uint8_t *out_buf, const uint8_t *in_buf,
310 int nb_sectors, bool enc, Error **errp)
ea2384d3
FB
311{
312 union {
313 uint64_t ll[2];
314 uint8_t b[16];
315 } ivec;
316 int i;
f6fa64f6 317 int ret;
ea2384d3
FB
318
319 for(i = 0; i < nb_sectors; i++) {
320 ivec.ll[0] = cpu_to_le64(sector_num);
321 ivec.ll[1] = 0;
f6fa64f6
DB
322 if (qcrypto_cipher_setiv(s->cipher,
323 ivec.b, G_N_ELEMENTS(ivec.b),
324 errp) < 0) {
325 return -1;
326 }
327 if (enc) {
328 ret = qcrypto_cipher_encrypt(s->cipher,
329 in_buf,
330 out_buf,
331 512,
332 errp);
333 } else {
334 ret = qcrypto_cipher_decrypt(s->cipher,
335 in_buf,
336 out_buf,
337 512,
338 errp);
339 }
340 if (ret < 0) {
341 return -1;
342 }
ea2384d3
FB
343 sector_num++;
344 in_buf += 512;
345 out_buf += 512;
346 }
f6fa64f6 347 return 0;
ea2384d3
FB
348}
349
350/* 'allocate' is:
351 *
352 * 0 to not allocate.
353 *
354 * 1 to allocate a normal cluster (for sector indexes 'n_start' to
355 * 'n_end')
356 *
357 * 2 to allocate a compressed cluster of size
358 * 'compressed_size'. 'compressed_size' must be > 0 and <
5fafdf24 359 * cluster_size
ea2384d3
FB
360 *
361 * return 0 if not allocated.
362 */
363static uint64_t get_cluster_offset(BlockDriverState *bs,
364 uint64_t offset, int allocate,
365 int compressed_size,
366 int n_start, int n_end)
367{
368 BDRVQcowState *s = bs->opaque;
369 int min_index, i, j, l1_index, l2_index;
370 uint64_t l2_offset, *l2_table, cluster_offset, tmp;
371 uint32_t min_count;
372 int new_l2_table;
3b46e624 373
ea2384d3
FB
374 l1_index = offset >> (s->l2_bits + s->cluster_bits);
375 l2_offset = s->l1_table[l1_index];
376 new_l2_table = 0;
377 if (!l2_offset) {
378 if (!allocate)
379 return 0;
380 /* allocate a new l2 entry */
9a4f4c31 381 l2_offset = bdrv_getlength(bs->file->bs);
ea2384d3
FB
382 /* round to cluster size */
383 l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
384 /* update the L1 entry */
385 s->l1_table[l1_index] = l2_offset;
386 tmp = cpu_to_be64(l2_offset);
9a4f4c31 387 if (bdrv_pwrite_sync(bs->file->bs,
5e5557d9
KW
388 s->l1_table_offset + l1_index * sizeof(tmp),
389 &tmp, sizeof(tmp)) < 0)
ea2384d3
FB
390 return 0;
391 new_l2_table = 1;
392 }
393 for(i = 0; i < L2_CACHE_SIZE; i++) {
394 if (l2_offset == s->l2_cache_offsets[i]) {
395 /* increment the hit count */
396 if (++s->l2_cache_counts[i] == 0xffffffff) {
397 for(j = 0; j < L2_CACHE_SIZE; j++) {
398 s->l2_cache_counts[j] >>= 1;
399 }
400 }
401 l2_table = s->l2_cache + (i << s->l2_bits);
402 goto found;
403 }
404 }
405 /* not found: load a new entry in the least used one */
406 min_index = 0;
407 min_count = 0xffffffff;
408 for(i = 0; i < L2_CACHE_SIZE; i++) {
409 if (s->l2_cache_counts[i] < min_count) {
410 min_count = s->l2_cache_counts[i];
411 min_index = i;
412 }
413 }
414 l2_table = s->l2_cache + (min_index << s->l2_bits);
ea2384d3
FB
415 if (new_l2_table) {
416 memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
9a4f4c31 417 if (bdrv_pwrite_sync(bs->file->bs, l2_offset, l2_table,
5e5557d9 418 s->l2_size * sizeof(uint64_t)) < 0)
ea2384d3
FB
419 return 0;
420 } else {
9a4f4c31
KW
421 if (bdrv_pread(bs->file->bs, l2_offset, l2_table,
422 s->l2_size * sizeof(uint64_t)) !=
ea2384d3
FB
423 s->l2_size * sizeof(uint64_t))
424 return 0;
425 }
426 s->l2_cache_offsets[min_index] = l2_offset;
427 s->l2_cache_counts[min_index] = 1;
428 found:
429 l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
430 cluster_offset = be64_to_cpu(l2_table[l2_index]);
5fafdf24 431 if (!cluster_offset ||
ea2384d3
FB
432 ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
433 if (!allocate)
434 return 0;
435 /* allocate a new cluster */
436 if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
437 (n_end - n_start) < s->cluster_sectors) {
438 /* if the cluster is already compressed, we must
439 decompress it in the case it is not completely
440 overwritten */
66f82cee 441 if (decompress_cluster(bs, cluster_offset) < 0)
ea2384d3 442 return 0;
9a4f4c31 443 cluster_offset = bdrv_getlength(bs->file->bs);
5fafdf24 444 cluster_offset = (cluster_offset + s->cluster_size - 1) &
ea2384d3
FB
445 ~(s->cluster_size - 1);
446 /* write the cluster content */
9a4f4c31
KW
447 if (bdrv_pwrite(bs->file->bs, cluster_offset, s->cluster_cache,
448 s->cluster_size) !=
ea2384d3
FB
449 s->cluster_size)
450 return -1;
451 } else {
9a4f4c31 452 cluster_offset = bdrv_getlength(bs->file->bs);
7f48fa1f
AL
453 if (allocate == 1) {
454 /* round to cluster size */
455 cluster_offset = (cluster_offset + s->cluster_size - 1) &
456 ~(s->cluster_size - 1);
9a4f4c31 457 bdrv_truncate(bs->file->bs, cluster_offset + s->cluster_size);
7f48fa1f
AL
458 /* if encrypted, we must initialize the cluster
459 content which won't be written */
8336aafa 460 if (bs->encrypted &&
7f48fa1f
AL
461 (n_end - n_start) < s->cluster_sectors) {
462 uint64_t start_sect;
f6fa64f6 463 assert(s->cipher);
7f48fa1f
AL
464 start_sect = (offset & ~(s->cluster_size - 1)) >> 9;
465 memset(s->cluster_data + 512, 0x00, 512);
466 for(i = 0; i < s->cluster_sectors; i++) {
467 if (i < n_start || i >= n_end) {
f6fa64f6
DB
468 Error *err = NULL;
469 if (encrypt_sectors(s, start_sect + i,
470 s->cluster_data,
471 s->cluster_data + 512, 1,
472 true, &err) < 0) {
473 error_free(err);
474 errno = EIO;
475 return -1;
476 }
9a4f4c31
KW
477 if (bdrv_pwrite(bs->file->bs,
478 cluster_offset + i * 512,
7f48fa1f
AL
479 s->cluster_data, 512) != 512)
480 return -1;
481 }
ea2384d3
FB
482 }
483 }
7f48fa1f
AL
484 } else if (allocate == 2) {
485 cluster_offset |= QCOW_OFLAG_COMPRESSED |
486 (uint64_t)compressed_size << (63 - s->cluster_bits);
ea2384d3
FB
487 }
488 }
489 /* update L2 table */
490 tmp = cpu_to_be64(cluster_offset);
491 l2_table[l2_index] = tmp;
9a4f4c31 492 if (bdrv_pwrite_sync(bs->file->bs, l2_offset + l2_index * sizeof(tmp),
5e5557d9 493 &tmp, sizeof(tmp)) < 0)
ea2384d3
FB
494 return 0;
495 }
496 return cluster_offset;
497}
498
b6b8a333 499static int64_t coroutine_fn qcow_co_get_block_status(BlockDriverState *bs,
67a0fd2a 500 int64_t sector_num, int nb_sectors, int *pnum, BlockDriverState **file)
ea2384d3
FB
501{
502 BDRVQcowState *s = bs->opaque;
503 int index_in_cluster, n;
504 uint64_t cluster_offset;
505
f8a2e5e3 506 qemu_co_mutex_lock(&s->lock);
ea2384d3 507 cluster_offset = get_cluster_offset(bs, sector_num << 9, 0, 0, 0, 0);
f8a2e5e3 508 qemu_co_mutex_unlock(&s->lock);
ea2384d3
FB
509 index_in_cluster = sector_num & (s->cluster_sectors - 1);
510 n = s->cluster_sectors - index_in_cluster;
511 if (n > nb_sectors)
512 n = nb_sectors;
513 *pnum = n;
4bc74be9
PB
514 if (!cluster_offset) {
515 return 0;
516 }
f6fa64f6 517 if ((cluster_offset & QCOW_OFLAG_COMPRESSED) || s->cipher) {
4bc74be9
PB
518 return BDRV_BLOCK_DATA;
519 }
520 cluster_offset |= (index_in_cluster << BDRV_SECTOR_BITS);
3064bf6f 521 *file = bs->file->bs;
4bc74be9 522 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | cluster_offset;
ea2384d3
FB
523}
524
525static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
526 const uint8_t *buf, int buf_size)
527{
528 z_stream strm1, *strm = &strm1;
529 int ret, out_len;
530
531 memset(strm, 0, sizeof(*strm));
532
533 strm->next_in = (uint8_t *)buf;
534 strm->avail_in = buf_size;
535 strm->next_out = out_buf;
536 strm->avail_out = out_buf_size;
537
538 ret = inflateInit2(strm, -12);
539 if (ret != Z_OK)
540 return -1;
541 ret = inflate(strm, Z_FINISH);
542 out_len = strm->next_out - out_buf;
543 if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
544 out_len != out_buf_size) {
545 inflateEnd(strm);
546 return -1;
547 }
548 inflateEnd(strm);
549 return 0;
550}
3b46e624 551
66f82cee 552static int decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
ea2384d3 553{
66f82cee 554 BDRVQcowState *s = bs->opaque;
ea2384d3
FB
555 int ret, csize;
556 uint64_t coffset;
557
558 coffset = cluster_offset & s->cluster_offset_mask;
559 if (s->cluster_cache_offset != coffset) {
560 csize = cluster_offset >> (63 - s->cluster_bits);
561 csize &= (s->cluster_size - 1);
9a4f4c31 562 ret = bdrv_pread(bs->file->bs, coffset, s->cluster_data, csize);
5fafdf24 563 if (ret != csize)
ea2384d3
FB
564 return -1;
565 if (decompress_buffer(s->cluster_cache, s->cluster_size,
566 s->cluster_data, csize) < 0) {
567 return -1;
568 }
569 s->cluster_cache_offset = coffset;
570 }
571 return 0;
572}
573
a968168c 574static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,
27deebe8 575 int nb_sectors, QEMUIOVector *qiov)
83f64091 576{
83f64091 577 BDRVQcowState *s = bs->opaque;
83f64091 578 int index_in_cluster;
27deebe8 579 int ret = 0, n;
43ca85b5 580 uint64_t cluster_offset;
430bbaaa
FZ
581 struct iovec hd_iov;
582 QEMUIOVector hd_qiov;
27deebe8
FZ
583 uint8_t *buf;
584 void *orig_buf;
f6fa64f6 585 Error *err = NULL;
83f64091 586
27deebe8 587 if (qiov->niov > 1) {
0df93305
KW
588 buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
589 if (buf == NULL) {
590 return -ENOMEM;
591 }
27deebe8
FZ
592 } else {
593 orig_buf = NULL;
594 buf = (uint8_t *)qiov->iov->iov_base;
83f64091 595 }
3b46e624 596
27deebe8
FZ
597 qemu_co_mutex_lock(&s->lock);
598
599 while (nb_sectors != 0) {
600 /* prepare next request */
601 cluster_offset = get_cluster_offset(bs, sector_num << 9,
602 0, 0, 0, 0);
603 index_in_cluster = sector_num & (s->cluster_sectors - 1);
604 n = s->cluster_sectors - index_in_cluster;
605 if (n > nb_sectors) {
606 n = nb_sectors;
607 }
ce1a14dc 608
27deebe8 609 if (!cluster_offset) {
760e0063 610 if (bs->backing) {
27deebe8
FZ
611 /* read from the base image */
612 hd_iov.iov_base = (void *)buf;
613 hd_iov.iov_len = n * 512;
614 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
615 qemu_co_mutex_unlock(&s->lock);
760e0063 616 ret = bdrv_co_readv(bs->backing->bs, sector_num,
27deebe8
FZ
617 n, &hd_qiov);
618 qemu_co_mutex_lock(&s->lock);
619 if (ret < 0) {
620 goto fail;
621 }
622 } else {
623 /* Note: in this case, no need to wait */
624 memset(buf, 0, 512 * n);
625 }
626 } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
627 /* add AIO support for compressed blocks ? */
628 if (decompress_cluster(bs, cluster_offset) < 0) {
629 goto fail;
630 }
631 memcpy(buf,
632 s->cluster_cache + index_in_cluster * 512, 512 * n);
633 } else {
634 if ((cluster_offset & 511) != 0) {
635 goto fail;
636 }
637 hd_iov.iov_base = (void *)buf;
430bbaaa
FZ
638 hd_iov.iov_len = n * 512;
639 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
52b8eb60 640 qemu_co_mutex_unlock(&s->lock);
9a4f4c31 641 ret = bdrv_co_readv(bs->file->bs,
27deebe8 642 (cluster_offset >> 9) + index_in_cluster,
430bbaaa 643 n, &hd_qiov);
52b8eb60
KW
644 qemu_co_mutex_lock(&s->lock);
645 if (ret < 0) {
27deebe8
FZ
646 break;
647 }
8336aafa 648 if (bs->encrypted) {
f6fa64f6
DB
649 assert(s->cipher);
650 if (encrypt_sectors(s, sector_num, buf, buf,
651 n, false, &err) < 0) {
652 goto fail;
653 }
5614c188 654 }
5614c188 655 }
27deebe8 656 ret = 0;
f141eafe 657
27deebe8
FZ
658 nb_sectors -= n;
659 sector_num += n;
660 buf += n * 512;
43ca85b5
FZ
661 }
662
27deebe8 663done:
52b8eb60
KW
664 qemu_co_mutex_unlock(&s->lock);
665
27deebe8 666 if (qiov->niov > 1) {
03396148 667 qemu_iovec_from_buf(qiov, 0, orig_buf, qiov->size);
27deebe8 668 qemu_vfree(orig_buf);
b11a24de
KW
669 }
670
52b8eb60 671 return ret;
27deebe8
FZ
672
673fail:
f6fa64f6 674 error_free(err);
27deebe8
FZ
675 ret = -EIO;
676 goto done;
83f64091
FB
677}
678
a968168c 679static coroutine_fn int qcow_co_writev(BlockDriverState *bs, int64_t sector_num,
27deebe8 680 int nb_sectors, QEMUIOVector *qiov)
83f64091 681{
83f64091 682 BDRVQcowState *s = bs->opaque;
83f64091
FB
683 int index_in_cluster;
684 uint64_t cluster_offset;
685 const uint8_t *src_buf;
27deebe8 686 int ret = 0, n;
430bbaaa
FZ
687 uint8_t *cluster_data = NULL;
688 struct iovec hd_iov;
689 QEMUIOVector hd_qiov;
27deebe8
FZ
690 uint8_t *buf;
691 void *orig_buf;
ce1a14dc 692
27deebe8 693 s->cluster_cache_offset = -1; /* disable compressed cache */
3b46e624 694
27deebe8 695 if (qiov->niov > 1) {
0df93305
KW
696 buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
697 if (buf == NULL) {
698 return -ENOMEM;
699 }
d5e6b161 700 qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
83f64091 701 } else {
27deebe8
FZ
702 orig_buf = NULL;
703 buf = (uint8_t *)qiov->iov->iov_base;
83f64091 704 }
c87c0672 705
52b8eb60 706 qemu_co_mutex_lock(&s->lock);
43ca85b5 707
27deebe8 708 while (nb_sectors != 0) {
83f64091 709
27deebe8
FZ
710 index_in_cluster = sector_num & (s->cluster_sectors - 1);
711 n = s->cluster_sectors - index_in_cluster;
712 if (n > nb_sectors) {
713 n = nb_sectors;
714 }
715 cluster_offset = get_cluster_offset(bs, sector_num << 9, 1, 0,
716 index_in_cluster,
717 index_in_cluster + n);
718 if (!cluster_offset || (cluster_offset & 511) != 0) {
719 ret = -EIO;
720 break;
721 }
8336aafa 722 if (bs->encrypted) {
f6fa64f6
DB
723 Error *err = NULL;
724 assert(s->cipher);
27deebe8
FZ
725 if (!cluster_data) {
726 cluster_data = g_malloc0(s->cluster_size);
727 }
f6fa64f6
DB
728 if (encrypt_sectors(s, sector_num, cluster_data, buf,
729 n, true, &err) < 0) {
730 error_free(err);
731 ret = -EIO;
732 break;
733 }
27deebe8
FZ
734 src_buf = cluster_data;
735 } else {
736 src_buf = buf;
737 }
83f64091 738
27deebe8
FZ
739 hd_iov.iov_base = (void *)src_buf;
740 hd_iov.iov_len = n * 512;
741 qemu_iovec_init_external(&hd_qiov, &hd_iov, 1);
742 qemu_co_mutex_unlock(&s->lock);
9a4f4c31 743 ret = bdrv_co_writev(bs->file->bs,
27deebe8
FZ
744 (cluster_offset >> 9) + index_in_cluster,
745 n, &hd_qiov);
746 qemu_co_mutex_lock(&s->lock);
747 if (ret < 0) {
748 break;
749 }
750 ret = 0;
ad53089b 751
27deebe8
FZ
752 nb_sectors -= n;
753 sector_num += n;
754 buf += n * 512;
755 }
52b8eb60 756 qemu_co_mutex_unlock(&s->lock);
3b46e624 757
27deebe8
FZ
758 if (qiov->niov > 1) {
759 qemu_vfree(orig_buf);
b11a24de 760 }
add8d262 761 g_free(cluster_data);
b11a24de 762
52b8eb60 763 return ret;
83f64091
FB
764}
765
e2731add 766static void qcow_close(BlockDriverState *bs)
ea2384d3
FB
767{
768 BDRVQcowState *s = bs->opaque;
fd9f102c 769
f6fa64f6
DB
770 qcrypto_cipher_free(s->cipher);
771 s->cipher = NULL;
7267c094 772 g_free(s->l1_table);
0df93305 773 qemu_vfree(s->l2_cache);
7267c094
AL
774 g_free(s->cluster_cache);
775 g_free(s->cluster_data);
fd9f102c
KW
776
777 migrate_del_blocker(s->migration_blocker);
778 error_free(s->migration_blocker);
ea2384d3
FB
779}
780
16d12159 781static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
ea2384d3 782{
2b16c9ff 783 int header_size, backing_filename_len, l1_size, shift, i;
ea2384d3 784 QCowHeader header;
2b16c9ff 785 uint8_t *tmp;
0e7e1989 786 int64_t total_size = 0;
16d12159 787 char *backing_file = NULL;
0e7e1989 788 int flags = 0;
34b5d2c6 789 Error *local_err = NULL;
3e1a8134 790 int ret;
6af40160 791 BlockBackend *qcow_blk;
0e7e1989
KW
792
793 /* Read out options */
180e9526
HT
794 total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
795 BDRV_SECTOR_SIZE);
16d12159
CL
796 backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
797 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
798 flags |= BLOCK_FLAG_ENCRYPT;
0e7e1989 799 }
ea2384d3 800
c282e1fd 801 ret = bdrv_create_file(filename, opts, &local_err);
2b16c9ff 802 if (ret < 0) {
b6d5066d 803 error_propagate(errp, local_err);
16d12159 804 goto cleanup;
2b16c9ff
LZH
805 }
806
efaa7c4e 807 qcow_blk = blk_new_open(filename, NULL, NULL,
72e775c7 808 BDRV_O_RDWR | BDRV_O_PROTOCOL, &local_err);
6af40160 809 if (qcow_blk == NULL) {
b6d5066d 810 error_propagate(errp, local_err);
6af40160 811 ret = -EIO;
16d12159 812 goto cleanup;
2b16c9ff
LZH
813 }
814
6af40160
KW
815 blk_set_allow_write_beyond_eof(qcow_blk, true);
816
817 ret = blk_truncate(qcow_blk, 0);
2b16c9ff
LZH
818 if (ret < 0) {
819 goto exit;
820 }
821
ea2384d3
FB
822 memset(&header, 0, sizeof(header));
823 header.magic = cpu_to_be32(QCOW_MAGIC);
824 header.version = cpu_to_be32(QCOW_VERSION);
180e9526 825 header.size = cpu_to_be64(total_size);
ea2384d3
FB
826 header_size = sizeof(header);
827 backing_filename_len = 0;
828 if (backing_file) {
7852e5da
AJ
829 if (strcmp(backing_file, "fat:")) {
830 header.backing_file_offset = cpu_to_be64(header_size);
831 backing_filename_len = strlen(backing_file);
832 header.backing_file_size = cpu_to_be32(backing_filename_len);
833 header_size += backing_filename_len;
834 } else {
835 /* special backing file for vvfat */
836 backing_file = NULL;
837 }
ea2384d3 838 header.cluster_bits = 9; /* 512 byte cluster to avoid copying
dc6fb73d 839 unmodified sectors */
ea2384d3
FB
840 header.l2_bits = 12; /* 32 KB L2 tables */
841 } else {
842 header.cluster_bits = 12; /* 4 KB clusters */
843 header.l2_bits = 9; /* 4 KB L2 tables */
844 }
845 header_size = (header_size + 7) & ~7;
846 shift = header.cluster_bits + header.l2_bits;
180e9526 847 l1_size = (total_size + (1LL << shift) - 1) >> shift;
ea2384d3
FB
848
849 header.l1_table_offset = cpu_to_be64(header_size);
ec36ba14 850 if (flags & BLOCK_FLAG_ENCRYPT) {
ea2384d3
FB
851 header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
852 } else {
853 header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
854 }
3b46e624 855
ea2384d3 856 /* write all the data */
8341f00d 857 ret = blk_pwrite(qcow_blk, 0, &header, sizeof(header), 0);
3e1a8134 858 if (ret != sizeof(header)) {
3e1a8134
KS
859 goto exit;
860 }
861
ea2384d3 862 if (backing_file) {
6af40160 863 ret = blk_pwrite(qcow_blk, sizeof(header),
8341f00d 864 backing_file, backing_filename_len, 0);
3e1a8134 865 if (ret != backing_filename_len) {
3e1a8134
KS
866 goto exit;
867 }
ea2384d3 868 }
2b16c9ff
LZH
869
870 tmp = g_malloc0(BDRV_SECTOR_SIZE);
871 for (i = 0; i < ((sizeof(uint64_t)*l1_size + BDRV_SECTOR_SIZE - 1)/
872 BDRV_SECTOR_SIZE); i++) {
8341f00d
EB
873 ret = blk_pwrite(qcow_blk, header_size + BDRV_SECTOR_SIZE * i,
874 tmp, BDRV_SECTOR_SIZE, 0);
2b16c9ff
LZH
875 if (ret != BDRV_SECTOR_SIZE) {
876 g_free(tmp);
3e1a8134
KS
877 goto exit;
878 }
ea2384d3 879 }
3e1a8134 880
2b16c9ff 881 g_free(tmp);
3e1a8134
KS
882 ret = 0;
883exit:
6af40160 884 blk_unref(qcow_blk);
16d12159
CL
885cleanup:
886 g_free(backing_file);
3e1a8134 887 return ret;
ea2384d3
FB
888}
889
c47c33b0 890static int qcow_make_empty(BlockDriverState *bs)
95389c86
FB
891{
892 BDRVQcowState *s = bs->opaque;
893 uint32_t l1_length = s->l1_size * sizeof(uint64_t);
83f64091 894 int ret;
95389c86
FB
895
896 memset(s->l1_table, 0, l1_length);
9a4f4c31 897 if (bdrv_pwrite_sync(bs->file->bs, s->l1_table_offset, s->l1_table,
5e5557d9
KW
898 l1_length) < 0)
899 return -1;
9a4f4c31 900 ret = bdrv_truncate(bs->file->bs, s->l1_table_offset + l1_length);
83f64091
FB
901 if (ret < 0)
902 return ret;
95389c86
FB
903
904 memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
905 memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
906 memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
907
908 return 0;
909}
910
ea2384d3
FB
911/* XXX: put compressed sectors first, then all the cluster aligned
912 tables to avoid losing bytes in alignment */
5fafdf24 913static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
c47c33b0 914 const uint8_t *buf, int nb_sectors)
ea2384d3
FB
915{
916 BDRVQcowState *s = bs->opaque;
917 z_stream strm;
918 int ret, out_len;
919 uint8_t *out_buf;
920 uint64_t cluster_offset;
921
16b3c5cd
SH
922 if (nb_sectors != s->cluster_sectors) {
923 ret = -EINVAL;
924
925 /* Zero-pad last write if image size is not cluster aligned */
926 if (sector_num + nb_sectors == bs->total_sectors &&
927 nb_sectors < s->cluster_sectors) {
928 uint8_t *pad_buf = qemu_blockalign(bs, s->cluster_size);
929 memset(pad_buf, 0, s->cluster_size);
930 memcpy(pad_buf, buf, nb_sectors * BDRV_SECTOR_SIZE);
931 ret = qcow_write_compressed(bs, sector_num,
932 pad_buf, s->cluster_sectors);
933 qemu_vfree(pad_buf);
934 }
935 return ret;
936 }
ea2384d3 937
7267c094 938 out_buf = g_malloc(s->cluster_size + (s->cluster_size / 1000) + 128);
ea2384d3
FB
939
940 /* best compression, small window, no zlib header */
941 memset(&strm, 0, sizeof(strm));
942 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
5fafdf24 943 Z_DEFLATED, -12,
ea2384d3
FB
944 9, Z_DEFAULT_STRATEGY);
945 if (ret != 0) {
64ebe71a
KW
946 ret = -EINVAL;
947 goto fail;
ea2384d3
FB
948 }
949
950 strm.avail_in = s->cluster_size;
951 strm.next_in = (uint8_t *)buf;
952 strm.avail_out = s->cluster_size;
953 strm.next_out = out_buf;
954
955 ret = deflate(&strm, Z_FINISH);
956 if (ret != Z_STREAM_END && ret != Z_OK) {
ea2384d3 957 deflateEnd(&strm);
64ebe71a
KW
958 ret = -EINVAL;
959 goto fail;
ea2384d3
FB
960 }
961 out_len = strm.next_out - out_buf;
962
963 deflateEnd(&strm);
964
965 if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
966 /* could not compress: write normal cluster */
64ebe71a
KW
967 ret = bdrv_write(bs, sector_num, buf, s->cluster_sectors);
968 if (ret < 0) {
969 goto fail;
970 }
ea2384d3 971 } else {
5fafdf24 972 cluster_offset = get_cluster_offset(bs, sector_num << 9, 2,
ea2384d3 973 out_len, 0, 0);
64ebe71a
KW
974 if (cluster_offset == 0) {
975 ret = -EIO;
976 goto fail;
977 }
978
ea2384d3 979 cluster_offset &= s->cluster_offset_mask;
9a4f4c31 980 ret = bdrv_pwrite(bs->file->bs, cluster_offset, out_buf, out_len);
64ebe71a
KW
981 if (ret < 0) {
982 goto fail;
ea2384d3
FB
983 }
984 }
3b46e624 985
64ebe71a
KW
986 ret = 0;
987fail:
7267c094 988 g_free(out_buf);
64ebe71a 989 return ret;
ea2384d3
FB
990}
991
c47c33b0
FB
992static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
993{
994 BDRVQcowState *s = bs->opaque;
995 bdi->cluster_size = s->cluster_size;
996 return 0;
997}
998
16d12159
CL
999static QemuOptsList qcow_create_opts = {
1000 .name = "qcow-create-opts",
1001 .head = QTAILQ_HEAD_INITIALIZER(qcow_create_opts.head),
1002 .desc = {
1003 {
1004 .name = BLOCK_OPT_SIZE,
1005 .type = QEMU_OPT_SIZE,
1006 .help = "Virtual disk size"
1007 },
1008 {
1009 .name = BLOCK_OPT_BACKING_FILE,
1010 .type = QEMU_OPT_STRING,
1011 .help = "File name of a base image"
1012 },
1013 {
1014 .name = BLOCK_OPT_ENCRYPT,
1015 .type = QEMU_OPT_BOOL,
1016 .help = "Encrypt the image",
1017 .def_value_str = "off"
1018 },
1019 { /* end of list */ }
1020 }
0e7e1989
KW
1021};
1022
5efa9d5a 1023static BlockDriver bdrv_qcow = {
e60f469c
AJ
1024 .format_name = "qcow",
1025 .instance_size = sizeof(BDRVQcowState),
1026 .bdrv_probe = qcow_probe,
1027 .bdrv_open = qcow_open,
1028 .bdrv_close = qcow_close,
16d12159 1029 .bdrv_reopen_prepare = qcow_reopen_prepare,
c282e1fd 1030 .bdrv_create = qcow_create,
3ac21627 1031 .bdrv_has_zero_init = bdrv_has_zero_init_1,
8ee79e70 1032 .supports_backing = true,
c68b89ac
KW
1033
1034 .bdrv_co_readv = qcow_co_readv,
1035 .bdrv_co_writev = qcow_co_writev,
b6b8a333 1036 .bdrv_co_get_block_status = qcow_co_get_block_status,
c68b89ac
KW
1037
1038 .bdrv_set_key = qcow_set_key,
1039 .bdrv_make_empty = qcow_make_empty,
1040 .bdrv_write_compressed = qcow_write_compressed,
1041 .bdrv_get_info = qcow_get_info,
0e7e1989 1042
16d12159 1043 .create_opts = &qcow_create_opts,
ea2384d3 1044};
5efa9d5a
AL
1045
1046static void bdrv_qcow_init(void)
1047{
1048 bdrv_register(&bdrv_qcow);
1049}
1050
1051block_init(bdrv_qcow_init);
This page took 0.737707 seconds and 4 git commands to generate.