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