2004-09-30 05:29:14 +08:00
|
|
|
/*
|
2004-12-12 19:24:44 +08:00
|
|
|
* QEMU Block driver for CLOOP images
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
2004-09-30 05:29:14 +08:00
|
|
|
* Copyright (c) 2004 Johannes E. Schindelin
|
2007-09-17 05:08:06 +08:00
|
|
|
*
|
2004-09-30 05:29:14 +08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2016-01-19 02:01:42 +08:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 16:01:28 +08:00
|
|
|
#include "qapi/error.h"
|
2017-11-08 01:21:41 +08:00
|
|
|
#include "qemu/error-report.h"
|
2012-12-18 01:19:44 +08:00
|
|
|
#include "block/block_int.h"
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/module.h"
|
2016-03-16 00:22:36 +08:00
|
|
|
#include "qemu/bswap.h"
|
2004-09-30 05:29:14 +08:00
|
|
|
#include <zlib.h>
|
|
|
|
|
2014-03-26 20:05:25 +08:00
|
|
|
/* Maximum compressed block size */
|
|
|
|
#define MAX_BLOCK_SIZE (64 * 1024 * 1024)
|
|
|
|
|
2004-09-30 05:29:14 +08:00
|
|
|
typedef struct BDRVCloopState {
|
2011-10-20 19:16:21 +08:00
|
|
|
CoMutex lock;
|
2004-09-30 05:29:14 +08:00
|
|
|
uint32_t block_size;
|
|
|
|
uint32_t n_blocks;
|
2011-11-02 16:36:20 +08:00
|
|
|
uint64_t *offsets;
|
2004-09-30 05:29:14 +08:00
|
|
|
uint32_t sectors_per_block;
|
|
|
|
uint32_t current_block;
|
2006-02-02 05:43:52 +08:00
|
|
|
uint8_t *compressed_block;
|
|
|
|
uint8_t *uncompressed_block;
|
2004-09-30 05:29:14 +08:00
|
|
|
z_stream zstream;
|
|
|
|
} BDRVCloopState;
|
|
|
|
|
|
|
|
static int cloop_probe(const uint8_t *buf, int buf_size, const char *filename)
|
|
|
|
{
|
2011-11-02 16:36:20 +08:00
|
|
|
const char *magic_version_2_0 = "#!/bin/sh\n"
|
|
|
|
"#V2.0 Format\n"
|
|
|
|
"modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n";
|
|
|
|
int length = strlen(magic_version_2_0);
|
|
|
|
if (length > buf_size) {
|
|
|
|
length = buf_size;
|
|
|
|
}
|
|
|
|
if (!memcmp(magic_version_2_0, buf, length)) {
|
|
|
|
return 2;
|
|
|
|
}
|
2004-09-30 05:29:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-05 20:22:29 +08:00
|
|
|
static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
2004-09-30 05:29:14 +08:00
|
|
|
{
|
|
|
|
BDRVCloopState *s = bs->opaque;
|
2011-11-02 16:36:20 +08:00
|
|
|
uint32_t offsets_size, max_compressed_block_size = 1, i;
|
2013-01-26 00:07:28 +08:00
|
|
|
int ret;
|
2004-09-30 05:29:14 +08:00
|
|
|
|
2018-10-12 17:27:41 +08:00
|
|
|
ret = bdrv_apply_auto_read_only(bs, NULL, errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-13 19:05:35 +08:00
|
|
|
bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
|
|
|
|
BDRV_CHILD_IMAGE, false, errp);
|
2016-12-17 01:52:37 +08:00
|
|
|
if (!bs->file) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2004-09-30 05:29:14 +08:00
|
|
|
/* read header */
|
2016-06-21 00:24:02 +08:00
|
|
|
ret = bdrv_pread(bs->file, 128, &s->block_size, 4);
|
2013-01-26 00:07:28 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2004-09-30 05:29:14 +08:00
|
|
|
}
|
2010-05-04 18:44:38 +08:00
|
|
|
s->block_size = be32_to_cpu(s->block_size);
|
2014-03-26 20:05:25 +08:00
|
|
|
if (s->block_size % 512) {
|
2014-04-23 16:05:20 +08:00
|
|
|
error_setg(errp, "block_size %" PRIu32 " must be a multiple of 512",
|
2014-03-26 20:05:25 +08:00
|
|
|
s->block_size);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (s->block_size == 0) {
|
|
|
|
error_setg(errp, "block_size cannot be zero");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cloop's create_compressed_fs.c warns about block sizes beyond 256 KB but
|
|
|
|
* we can accept more. Prevent ridiculous values like 4 GB - 1 since we
|
|
|
|
* need a buffer this big.
|
|
|
|
*/
|
|
|
|
if (s->block_size > MAX_BLOCK_SIZE) {
|
2014-04-23 16:05:20 +08:00
|
|
|
error_setg(errp, "block_size %" PRIu32 " must be %u MB or less",
|
2014-03-26 20:05:25 +08:00
|
|
|
s->block_size,
|
|
|
|
MAX_BLOCK_SIZE / (1024 * 1024));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-05-04 18:44:38 +08:00
|
|
|
|
2016-06-21 00:24:02 +08:00
|
|
|
ret = bdrv_pread(bs->file, 128 + 4, &s->n_blocks, 4);
|
2013-01-26 00:07:28 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2010-05-04 18:44:38 +08:00
|
|
|
}
|
|
|
|
s->n_blocks = be32_to_cpu(s->n_blocks);
|
2004-09-30 05:29:14 +08:00
|
|
|
|
|
|
|
/* read offsets */
|
2014-03-26 20:05:29 +08:00
|
|
|
if (s->n_blocks > (UINT32_MAX - 1) / sizeof(uint64_t)) {
|
2014-03-26 20:05:26 +08:00
|
|
|
/* Prevent integer overflow */
|
2014-04-23 16:05:20 +08:00
|
|
|
error_setg(errp, "n_blocks %" PRIu32 " must be %zu or less",
|
2014-03-26 20:05:26 +08:00
|
|
|
s->n_blocks,
|
2014-03-26 20:05:29 +08:00
|
|
|
(UINT32_MAX - 1) / sizeof(uint64_t));
|
2014-03-26 20:05:26 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-03-26 20:05:29 +08:00
|
|
|
offsets_size = (s->n_blocks + 1) * sizeof(uint64_t);
|
2014-03-26 20:05:27 +08:00
|
|
|
if (offsets_size > 512 * 1024 * 1024) {
|
|
|
|
/* Prevent ridiculous offsets_size which causes memory allocation to
|
|
|
|
* fail or overflows bdrv_pread() size. In practice the 512 MB
|
|
|
|
* offsets[] limit supports 16 TB images at 256 KB block size.
|
|
|
|
*/
|
|
|
|
error_setg(errp, "image requires too many offsets, "
|
|
|
|
"try increasing block size");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-05-20 19:22:38 +08:00
|
|
|
|
|
|
|
s->offsets = g_try_malloc(offsets_size);
|
|
|
|
if (s->offsets == NULL) {
|
|
|
|
error_setg(errp, "Could not allocate offsets table");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2013-01-26 00:07:28 +08:00
|
|
|
|
2016-06-21 00:24:02 +08:00
|
|
|
ret = bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size);
|
2013-01-26 00:07:28 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
2010-05-04 18:44:38 +08:00
|
|
|
}
|
2013-01-26 00:07:28 +08:00
|
|
|
|
2014-03-26 20:05:29 +08:00
|
|
|
for (i = 0; i < s->n_blocks + 1; i++) {
|
2014-03-26 20:05:28 +08:00
|
|
|
uint64_t size;
|
|
|
|
|
2011-11-02 16:36:20 +08:00
|
|
|
s->offsets[i] = be64_to_cpu(s->offsets[i]);
|
2014-03-26 20:05:28 +08:00
|
|
|
if (i == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->offsets[i] < s->offsets[i - 1]) {
|
|
|
|
error_setg(errp, "offsets not monotonically increasing at "
|
2014-04-23 16:05:20 +08:00
|
|
|
"index %" PRIu32 ", image file is corrupt", i);
|
2014-03-26 20:05:28 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = s->offsets[i] - s->offsets[i - 1];
|
|
|
|
|
|
|
|
/* Compressed blocks should be smaller than the uncompressed block size
|
|
|
|
* but maybe compression performed poorly so the compressed block is
|
|
|
|
* actually bigger. Clamp down on unrealistic values to prevent
|
|
|
|
* ridiculous s->compressed_block allocation.
|
|
|
|
*/
|
|
|
|
if (size > 2 * MAX_BLOCK_SIZE) {
|
2014-04-23 16:05:20 +08:00
|
|
|
error_setg(errp, "invalid compressed block size at index %" PRIu32
|
|
|
|
", image file is corrupt", i);
|
2014-03-26 20:05:28 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > max_compressed_block_size) {
|
|
|
|
max_compressed_block_size = size;
|
2011-11-02 16:36:20 +08:00
|
|
|
}
|
2004-09-30 05:29:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize zlib engine */
|
2014-05-20 19:22:38 +08:00
|
|
|
s->compressed_block = g_try_malloc(max_compressed_block_size + 1);
|
|
|
|
if (s->compressed_block == NULL) {
|
|
|
|
error_setg(errp, "Could not allocate compressed_block");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->uncompressed_block = g_try_malloc(s->block_size);
|
|
|
|
if (s->uncompressed_block == NULL) {
|
|
|
|
error_setg(errp, "Could not allocate uncompressed_block");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2011-11-02 16:36:20 +08:00
|
|
|
if (inflateInit(&s->zstream) != Z_OK) {
|
2013-01-26 00:07:28 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
2011-11-02 16:36:20 +08:00
|
|
|
}
|
|
|
|
s->current_block = s->n_blocks;
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2004-09-30 05:29:14 +08:00
|
|
|
s->sectors_per_block = s->block_size/512;
|
2011-11-02 16:36:20 +08:00
|
|
|
bs->total_sectors = s->n_blocks * s->sectors_per_block;
|
2011-10-20 19:16:21 +08:00
|
|
|
qemu_co_mutex_init(&s->lock);
|
2004-09-30 05:29:14 +08:00
|
|
|
return 0;
|
2010-05-04 18:44:38 +08:00
|
|
|
|
2013-01-26 00:07:28 +08:00
|
|
|
fail:
|
|
|
|
g_free(s->offsets);
|
|
|
|
g_free(s->compressed_block);
|
|
|
|
g_free(s->uncompressed_block);
|
|
|
|
return ret;
|
2004-09-30 05:29:14 +08:00
|
|
|
}
|
|
|
|
|
2016-06-24 06:37:17 +08:00
|
|
|
static void cloop_refresh_limits(BlockDriverState *bs, Error **errp)
|
|
|
|
{
|
2016-06-24 06:37:24 +08:00
|
|
|
bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
|
2016-06-24 06:37:17 +08:00
|
|
|
}
|
|
|
|
|
2010-05-04 18:44:52 +08:00
|
|
|
static inline int cloop_read_block(BlockDriverState *bs, int block_num)
|
2004-09-30 05:29:14 +08:00
|
|
|
{
|
2010-05-04 18:44:52 +08:00
|
|
|
BDRVCloopState *s = bs->opaque;
|
|
|
|
|
2011-11-02 16:36:20 +08:00
|
|
|
if (s->current_block != block_num) {
|
|
|
|
int ret;
|
|
|
|
uint32_t bytes = s->offsets[block_num + 1] - s->offsets[block_num];
|
2007-09-17 16:09:54 +08:00
|
|
|
|
2016-06-21 00:24:02 +08:00
|
|
|
ret = bdrv_pread(bs->file, s->offsets[block_num],
|
2015-06-16 20:19:22 +08:00
|
|
|
s->compressed_block, bytes);
|
2011-11-02 16:36:20 +08:00
|
|
|
if (ret != bytes) {
|
2004-09-30 05:29:14 +08:00
|
|
|
return -1;
|
2011-11-02 16:36:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
s->zstream.next_in = s->compressed_block;
|
|
|
|
s->zstream.avail_in = bytes;
|
|
|
|
s->zstream.next_out = s->uncompressed_block;
|
|
|
|
s->zstream.avail_out = s->block_size;
|
|
|
|
ret = inflateReset(&s->zstream);
|
|
|
|
if (ret != Z_OK) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = inflate(&s->zstream, Z_FINISH);
|
|
|
|
if (ret != Z_STREAM_END || s->zstream.total_out != s->block_size) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-09-17 05:08:06 +08:00
|
|
|
|
2011-11-02 16:36:20 +08:00
|
|
|
s->current_block = block_num;
|
2004-09-30 05:29:14 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-25 21:21:46 +08:00
|
|
|
static int coroutine_fn
|
|
|
|
cloop_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|
|
|
QEMUIOVector *qiov, int flags)
|
2004-09-30 05:29:14 +08:00
|
|
|
{
|
|
|
|
BDRVCloopState *s = bs->opaque;
|
2016-04-25 21:21:46 +08:00
|
|
|
uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
|
|
|
|
int nb_sectors = bytes >> BDRV_SECTOR_BITS;
|
|
|
|
int ret, i;
|
|
|
|
|
2019-08-28 02:59:12 +08:00
|
|
|
assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
|
|
|
|
assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
|
2016-04-25 21:21:46 +08:00
|
|
|
|
|
|
|
qemu_co_mutex_lock(&s->lock);
|
2004-09-30 05:29:14 +08:00
|
|
|
|
2011-11-02 16:36:20 +08:00
|
|
|
for (i = 0; i < nb_sectors; i++) {
|
2016-04-25 21:21:46 +08:00
|
|
|
void *data;
|
2011-11-02 16:36:20 +08:00
|
|
|
uint32_t sector_offset_in_block =
|
|
|
|
((sector_num + i) % s->sectors_per_block),
|
|
|
|
block_num = (sector_num + i) / s->sectors_per_block;
|
|
|
|
if (cloop_read_block(bs, block_num) != 0) {
|
2016-04-25 21:21:46 +08:00
|
|
|
ret = -EIO;
|
|
|
|
goto fail;
|
2011-11-02 16:36:20 +08:00
|
|
|
}
|
2016-04-25 21:21:46 +08:00
|
|
|
|
|
|
|
data = s->uncompressed_block + sector_offset_in_block * 512;
|
|
|
|
qemu_iovec_from_buf(qiov, i * 512, data, 512);
|
2004-09-30 05:29:14 +08:00
|
|
|
}
|
|
|
|
|
2016-04-25 21:21:46 +08:00
|
|
|
ret = 0;
|
|
|
|
fail:
|
2011-10-20 19:16:22 +08:00
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
2016-04-25 21:21:46 +08:00
|
|
|
|
2011-10-20 19:16:22 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-09-30 05:29:14 +08:00
|
|
|
static void cloop_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVCloopState *s = bs->opaque;
|
2014-03-26 20:05:29 +08:00
|
|
|
g_free(s->offsets);
|
2011-11-02 16:36:21 +08:00
|
|
|
g_free(s->compressed_block);
|
|
|
|
g_free(s->uncompressed_block);
|
2004-09-30 05:29:14 +08:00
|
|
|
inflateEnd(&s->zstream);
|
|
|
|
}
|
|
|
|
|
2009-05-10 06:03:42 +08:00
|
|
|
static BlockDriver bdrv_cloop = {
|
2011-11-02 16:36:20 +08:00
|
|
|
.format_name = "cloop",
|
|
|
|
.instance_size = sizeof(BDRVCloopState),
|
|
|
|
.bdrv_probe = cloop_probe,
|
|
|
|
.bdrv_open = cloop_open,
|
2020-05-13 19:05:39 +08:00
|
|
|
.bdrv_child_perm = bdrv_default_perms,
|
2016-06-24 06:37:17 +08:00
|
|
|
.bdrv_refresh_limits = cloop_refresh_limits,
|
2016-04-25 21:21:46 +08:00
|
|
|
.bdrv_co_preadv = cloop_co_preadv,
|
2011-11-02 16:36:20 +08:00
|
|
|
.bdrv_close = cloop_close,
|
2020-05-13 19:05:12 +08:00
|
|
|
.is_format = true,
|
2004-09-30 05:29:14 +08:00
|
|
|
};
|
2009-05-10 06:03:42 +08:00
|
|
|
|
|
|
|
static void bdrv_cloop_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_cloop);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_cloop_init);
|