2 * Threaded data processing for Qcow2: compression, encryption
4 * Copyright (c) 2004-2006 Fabrice Bellard
5 * Copyright (c) 2018 Virtuozzo International GmbH. All rights reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
33 #include <zstd_errors.h>
37 #include "block/thread-pool.h"
40 static int coroutine_fn
41 qcow2_co_process(BlockDriverState
*bs
, ThreadPoolFunc
*func
, void *arg
)
44 BDRVQcow2State
*s
= bs
->opaque
;
45 ThreadPool
*pool
= aio_get_thread_pool(bdrv_get_aio_context(bs
));
47 qemu_co_mutex_lock(&s
->lock
);
48 while (s
->nb_threads
>= QCOW2_MAX_THREADS
) {
49 qemu_co_queue_wait(&s
->thread_task_queue
, &s
->lock
);
52 qemu_co_mutex_unlock(&s
->lock
);
54 ret
= thread_pool_submit_co(pool
, func
, arg
);
56 qemu_co_mutex_lock(&s
->lock
);
58 qemu_co_queue_next(&s
->thread_task_queue
);
59 qemu_co_mutex_unlock(&s
->lock
);
69 typedef ssize_t (*Qcow2CompressFunc
)(void *dest
, size_t dest_size
,
70 const void *src
, size_t src_size
);
71 typedef struct Qcow2CompressData
{
78 Qcow2CompressFunc func
;
82 * qcow2_zlib_compress()
84 * Compress @src_size bytes of data using zlib compression method
86 * @dest - destination buffer, @dest_size bytes
87 * @src - source buffer, @src_size bytes
89 * Returns: compressed size on success
90 * -ENOMEM destination buffer is not enough to store compressed data
91 * -EIO on any other error
93 static ssize_t
qcow2_zlib_compress(void *dest
, size_t dest_size
,
94 const void *src
, size_t src_size
)
99 /* best compression, small window, no zlib header */
100 memset(&strm
, 0, sizeof(strm
));
101 ret
= deflateInit2(&strm
, Z_DEFAULT_COMPRESSION
, Z_DEFLATED
,
102 -12, 9, Z_DEFAULT_STRATEGY
);
108 * strm.next_in is not const in old zlib versions, such as those used on
109 * OpenBSD/NetBSD, so cast the const away
111 strm
.avail_in
= src_size
;
112 strm
.next_in
= (void *) src
;
113 strm
.avail_out
= dest_size
;
114 strm
.next_out
= dest
;
116 ret
= deflate(&strm
, Z_FINISH
);
117 if (ret
== Z_STREAM_END
) {
118 ret
= dest_size
- strm
.avail_out
;
120 ret
= (ret
== Z_OK
? -ENOMEM
: -EIO
);
129 * qcow2_zlib_decompress()
131 * Decompress some data (not more than @src_size bytes) to produce exactly
132 * @dest_size bytes using zlib compression method
134 * @dest - destination buffer, @dest_size bytes
135 * @src - source buffer, @src_size bytes
137 * Returns: 0 on success
140 static ssize_t
qcow2_zlib_decompress(void *dest
, size_t dest_size
,
141 const void *src
, size_t src_size
)
146 memset(&strm
, 0, sizeof(strm
));
147 strm
.avail_in
= src_size
;
148 strm
.next_in
= (void *) src
;
149 strm
.avail_out
= dest_size
;
150 strm
.next_out
= dest
;
152 ret
= inflateInit2(&strm
, -12);
157 ret
= inflate(&strm
, Z_FINISH
);
158 if ((ret
== Z_STREAM_END
|| ret
== Z_BUF_ERROR
) && strm
.avail_out
== 0) {
160 * We approve Z_BUF_ERROR because we need @dest buffer to be filled, but
161 * @src buffer may be processed partly (because in qcow2 we know size of
162 * compressed data with precision of one sector)
177 * qcow2_zstd_compress()
179 * Compress @src_size bytes of data using zstd compression method
181 * @dest - destination buffer, @dest_size bytes
182 * @src - source buffer, @src_size bytes
184 * Returns: compressed size on success
185 * -ENOMEM destination buffer is not enough to store compressed data
186 * -EIO on any other error
188 static ssize_t
qcow2_zstd_compress(void *dest
, size_t dest_size
,
189 const void *src
, size_t src_size
)
193 ZSTD_outBuffer output
= {
198 ZSTD_inBuffer input
= {
203 ZSTD_CCtx
*cctx
= ZSTD_createCCtx();
209 * Use the zstd streamed interface for symmetry with decompression,
210 * where streaming is essential since we don't record the exact
213 * ZSTD_compressStream2() tries to compress everything it could
214 * with a single call. Although, ZSTD docs says that:
215 * "You must continue calling ZSTD_compressStream2() with ZSTD_e_end
216 * until it returns 0, at which point you are free to start a new frame",
217 * in out tests we saw the only case when it returned with >0 -
218 * when the output buffer was too small. In that case,
219 * ZSTD_compressStream2() expects a bigger buffer on the next call.
220 * We can't provide a bigger buffer because we are limited with dest_size
221 * which we pass to the ZSTD_compressStream2() at once.
222 * So, we don't need any loops and just abort the compression when we
223 * don't get 0 result on the first call.
225 zstd_ret
= ZSTD_compressStream2(cctx
, &output
, &input
, ZSTD_e_end
);
228 if (zstd_ret
> output
.size
- output
.pos
) {
236 /* make sure that zstd didn't overflow the dest buffer */
237 assert(output
.pos
<= dest_size
);
245 * qcow2_zstd_decompress()
247 * Decompress some data (not more than @src_size bytes) to produce exactly
248 * @dest_size bytes using zstd compression method
250 * @dest - destination buffer, @dest_size bytes
251 * @src - source buffer, @src_size bytes
253 * Returns: 0 on success
256 static ssize_t
qcow2_zstd_decompress(void *dest
, size_t dest_size
,
257 const void *src
, size_t src_size
)
261 ZSTD_outBuffer output
= {
266 ZSTD_inBuffer input
= {
271 ZSTD_DCtx
*dctx
= ZSTD_createDCtx();
278 * The compressed stream from the input buffer may consist of more
279 * than one zstd frame. So we iterate until we get a fully
280 * uncompressed cluster.
281 * From zstd docs related to ZSTD_decompressStream:
282 * "return : 0 when a frame is completely decoded and fully flushed"
283 * We suppose that this means: each time ZSTD_decompressStream reads
284 * only ONE full frame and returns 0 if and only if that frame
285 * is completely decoded and flushed. Only after returning 0,
286 * ZSTD_decompressStream reads another ONE full frame.
288 while (output
.pos
< output
.size
) {
289 size_t last_in_pos
= input
.pos
;
290 size_t last_out_pos
= output
.pos
;
291 zstd_ret
= ZSTD_decompressStream(dctx
, &output
, &input
);
293 if (ZSTD_isError(zstd_ret
)) {
299 * The ZSTD manual is vague about what to do if it reads
300 * the buffer partially, and we don't want to get stuck
301 * in an infinite loop where ZSTD_decompressStream
302 * returns > 0 waiting for another input chunk. So, we add
303 * a check which ensures that the loop makes some progress
306 if (last_in_pos
>= input
.pos
&&
307 last_out_pos
>= output
.pos
) {
313 * Make sure that we have the frame fully flushed here
314 * if not, we somehow managed to get uncompressed cluster
315 * greater then the cluster size, possibly because of its
323 assert(ret
== 0 || ret
== -EIO
);
328 static int qcow2_compress_pool_func(void *opaque
)
330 Qcow2CompressData
*data
= opaque
;
332 data
->ret
= data
->func(data
->dest
, data
->dest_size
,
333 data
->src
, data
->src_size
);
338 static ssize_t coroutine_fn
339 qcow2_co_do_compress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
340 const void *src
, size_t src_size
, Qcow2CompressFunc func
)
342 Qcow2CompressData arg
= {
344 .dest_size
= dest_size
,
346 .src_size
= src_size
,
350 qcow2_co_process(bs
, qcow2_compress_pool_func
, &arg
);
356 * qcow2_co_compress()
358 * Compress @src_size bytes of data using the compression
359 * method defined by the image compression type
361 * @dest - destination buffer, @dest_size bytes
362 * @src - source buffer, @src_size bytes
364 * Returns: compressed size on success
365 * a negative error code on failure
368 qcow2_co_compress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
369 const void *src
, size_t src_size
)
371 BDRVQcow2State
*s
= bs
->opaque
;
372 Qcow2CompressFunc fn
;
374 switch (s
->compression_type
) {
375 case QCOW2_COMPRESSION_TYPE_ZLIB
:
376 fn
= qcow2_zlib_compress
;
380 case QCOW2_COMPRESSION_TYPE_ZSTD
:
381 fn
= qcow2_zstd_compress
;
388 return qcow2_co_do_compress(bs
, dest
, dest_size
, src
, src_size
, fn
);
392 * qcow2_co_decompress()
394 * Decompress some data (not more than @src_size bytes) to produce exactly
395 * @dest_size bytes using the compression method defined by the image
398 * @dest - destination buffer, @dest_size bytes
399 * @src - source buffer, @src_size bytes
401 * Returns: 0 on success
402 * a negative error code on failure
405 qcow2_co_decompress(BlockDriverState
*bs
, void *dest
, size_t dest_size
,
406 const void *src
, size_t src_size
)
408 BDRVQcow2State
*s
= bs
->opaque
;
409 Qcow2CompressFunc fn
;
411 switch (s
->compression_type
) {
412 case QCOW2_COMPRESSION_TYPE_ZLIB
:
413 fn
= qcow2_zlib_decompress
;
417 case QCOW2_COMPRESSION_TYPE_ZSTD
:
418 fn
= qcow2_zstd_decompress
;
425 return qcow2_co_do_compress(bs
, dest
, dest_size
, src
, src_size
, fn
);
434 * Qcow2EncDecFunc: common prototype of qcrypto_block_encrypt() and
435 * qcrypto_block_decrypt() functions.
437 typedef int (*Qcow2EncDecFunc
)(QCryptoBlock
*block
, uint64_t offset
,
438 uint8_t *buf
, size_t len
, Error
**errp
);
440 typedef struct Qcow2EncDecData
{
446 Qcow2EncDecFunc func
;
449 static int qcow2_encdec_pool_func(void *opaque
)
451 Qcow2EncDecData
*data
= opaque
;
453 return data
->func(data
->block
, data
->offset
, data
->buf
, data
->len
, NULL
);
456 static int coroutine_fn
457 qcow2_co_encdec(BlockDriverState
*bs
, uint64_t host_offset
,
458 uint64_t guest_offset
, void *buf
, size_t len
,
459 Qcow2EncDecFunc func
)
461 BDRVQcow2State
*s
= bs
->opaque
;
462 Qcow2EncDecData arg
= {
464 .offset
= s
->crypt_physical_offset
? host_offset
: guest_offset
,
469 uint64_t sector_size
;
473 sector_size
= qcrypto_block_get_sector_size(s
->crypto
);
474 assert(QEMU_IS_ALIGNED(guest_offset
, sector_size
));
475 assert(QEMU_IS_ALIGNED(host_offset
, sector_size
));
476 assert(QEMU_IS_ALIGNED(len
, sector_size
));
478 return len
== 0 ? 0 : qcow2_co_process(bs
, qcow2_encdec_pool_func
, &arg
);
484 * Encrypts one or more contiguous aligned sectors
486 * @host_offset - underlying storage offset of the first sector of the
487 * data to be encrypted
489 * @guest_offset - guest (virtual) offset of the first sector of the
490 * data to be encrypted
492 * @buf - buffer with the data to encrypt, that after encryption
493 * will be written to the underlying storage device at
496 * @len - length of the buffer (must be a multiple of the encryption
499 * Depending on the encryption method, @host_offset and/or @guest_offset
500 * may be used for generating the initialization vector for
503 * Note that while the whole range must be aligned on sectors, it
504 * does not have to be aligned on clusters and can also cross cluster
508 qcow2_co_encrypt(BlockDriverState
*bs
, uint64_t host_offset
,
509 uint64_t guest_offset
, void *buf
, size_t len
)
511 return qcow2_co_encdec(bs
, host_offset
, guest_offset
, buf
, len
,
512 qcrypto_block_encrypt
);
518 * Decrypts one or more contiguous aligned sectors
519 * Similar to qcow2_co_encrypt
522 qcow2_co_decrypt(BlockDriverState
*bs
, uint64_t host_offset
,
523 uint64_t guest_offset
, void *buf
, size_t len
)
525 return qcow2_co_encdec(bs
, host_offset
, guest_offset
, buf
, len
,
526 qcrypto_block_decrypt
);