block: don't convert file size to sector size
and avoid converting it back later. Signed-off-by: Hu Tao <hutao@cn.fujitsu.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Reviewed-by: Benoît Canet <benoit.canet@nodalink.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
c2eb918e32
commit
180e95265e
@ -494,8 +494,8 @@ static int qemu_gluster_create(const char *filename,
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
||||||
BDRV_SECTOR_SIZE);
|
BDRV_SECTOR_SIZE);
|
||||||
|
|
||||||
tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
|
tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
|
||||||
if (!tmp || !strcmp(tmp, "off")) {
|
if (!tmp || !strcmp(tmp, "off")) {
|
||||||
@ -516,9 +516,8 @@ static int qemu_gluster_create(const char *filename,
|
|||||||
if (!fd) {
|
if (!fd) {
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
} else {
|
} else {
|
||||||
if (!glfs_ftruncate(fd, total_size * BDRV_SECTOR_SIZE)) {
|
if (!glfs_ftruncate(fd, total_size)) {
|
||||||
if (prealloc && qemu_gluster_zerofill(fd, 0,
|
if (prealloc && qemu_gluster_zerofill(fd, 0, total_size)) {
|
||||||
total_size * BDRV_SECTOR_SIZE)) {
|
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -725,8 +725,8 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
BlockDriverState *qcow_bs;
|
BlockDriverState *qcow_bs;
|
||||||
|
|
||||||
/* Read out options */
|
/* Read out options */
|
||||||
total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
||||||
BDRV_SECTOR_SIZE);
|
BDRV_SECTOR_SIZE);
|
||||||
backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
|
backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
|
||||||
if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
|
if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
|
||||||
flags |= BLOCK_FLAG_ENCRYPT;
|
flags |= BLOCK_FLAG_ENCRYPT;
|
||||||
@ -754,7 +754,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
memset(&header, 0, sizeof(header));
|
memset(&header, 0, sizeof(header));
|
||||||
header.magic = cpu_to_be32(QCOW_MAGIC);
|
header.magic = cpu_to_be32(QCOW_MAGIC);
|
||||||
header.version = cpu_to_be32(QCOW_VERSION);
|
header.version = cpu_to_be32(QCOW_VERSION);
|
||||||
header.size = cpu_to_be64(total_size * 512);
|
header.size = cpu_to_be64(total_size);
|
||||||
header_size = sizeof(header);
|
header_size = sizeof(header);
|
||||||
backing_filename_len = 0;
|
backing_filename_len = 0;
|
||||||
if (backing_file) {
|
if (backing_file) {
|
||||||
@ -776,7 +776,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
}
|
}
|
||||||
header_size = (header_size + 7) & ~7;
|
header_size = (header_size + 7) & ~7;
|
||||||
shift = header.cluster_bits + header.l2_bits;
|
shift = header.cluster_bits + header.l2_bits;
|
||||||
l1_size = ((total_size * 512) + (1LL << shift) - 1) >> shift;
|
l1_size = (total_size + (1LL << shift) - 1) >> shift;
|
||||||
|
|
||||||
header.l1_table_offset = cpu_to_be64(header_size);
|
header.l1_table_offset = cpu_to_be64(header_size);
|
||||||
if (flags & BLOCK_FLAG_ENCRYPT) {
|
if (flags & BLOCK_FLAG_ENCRYPT) {
|
||||||
|
@ -1859,7 +1859,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Okay, now that we have a valid image, let's give it the right size */
|
/* Okay, now that we have a valid image, let's give it the right size */
|
||||||
ret = bdrv_truncate(bs, total_size * BDRV_SECTOR_SIZE);
|
ret = bdrv_truncate(bs, total_size);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg_errno(errp, -ret, "Could not resize image");
|
error_setg_errno(errp, -ret, "Could not resize image");
|
||||||
goto out;
|
goto out;
|
||||||
@ -1912,7 +1912,7 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
char *backing_file = NULL;
|
char *backing_file = NULL;
|
||||||
char *backing_fmt = NULL;
|
char *backing_fmt = NULL;
|
||||||
char *buf = NULL;
|
char *buf = NULL;
|
||||||
uint64_t sectors = 0;
|
uint64_t size = 0;
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
size_t cluster_size = DEFAULT_CLUSTER_SIZE;
|
size_t cluster_size = DEFAULT_CLUSTER_SIZE;
|
||||||
int prealloc = 0;
|
int prealloc = 0;
|
||||||
@ -1921,8 +1921,8 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
/* Read out options */
|
/* Read out options */
|
||||||
sectors = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
||||||
BDRV_SECTOR_SIZE);
|
BDRV_SECTOR_SIZE);
|
||||||
backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
|
backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE);
|
||||||
backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
|
backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
|
||||||
if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
|
if (qemu_opt_get_bool_del(opts, BLOCK_OPT_ENCRYPT, false)) {
|
||||||
@ -1972,7 +1972,7 @@ static int qcow2_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = qcow2_create2(filename, sectors, backing_file, backing_fmt, flags,
|
ret = qcow2_create2(filename, size, backing_file, backing_fmt, flags,
|
||||||
cluster_size, prealloc, opts, version, &local_err);
|
cluster_size, prealloc, opts, version, &local_err);
|
||||||
if (local_err) {
|
if (local_err) {
|
||||||
error_propagate(errp, local_err);
|
error_propagate(errp, local_err);
|
||||||
|
@ -1369,8 +1369,8 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
strstart(filename, "file:", &filename);
|
strstart(filename, "file:", &filename);
|
||||||
|
|
||||||
/* Read out options */
|
/* Read out options */
|
||||||
total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
||||||
BDRV_SECTOR_SIZE);
|
BDRV_SECTOR_SIZE);
|
||||||
nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);
|
nocow = qemu_opt_get_bool(opts, BLOCK_OPT_NOCOW, false);
|
||||||
|
|
||||||
fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
|
fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
|
||||||
@ -1394,7 +1394,7 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ftruncate(fd, total_size * BDRV_SECTOR_SIZE) != 0) {
|
if (ftruncate(fd, total_size) != 0) {
|
||||||
result = -errno;
|
result = -errno;
|
||||||
error_setg_errno(errp, -result, "Could not resize file");
|
error_setg_errno(errp, -result, "Could not resize file");
|
||||||
}
|
}
|
||||||
@ -1966,8 +1966,8 @@ static int hdev_create(const char *filename, QemuOpts *opts,
|
|||||||
(void)has_prefix;
|
(void)has_prefix;
|
||||||
|
|
||||||
/* Read out options */
|
/* Read out options */
|
||||||
total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
||||||
BDRV_SECTOR_SIZE);
|
BDRV_SECTOR_SIZE);
|
||||||
|
|
||||||
fd = qemu_open(filename, O_WRONLY | O_BINARY);
|
fd = qemu_open(filename, O_WRONLY | O_BINARY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
@ -1983,7 +1983,7 @@ static int hdev_create(const char *filename, QemuOpts *opts,
|
|||||||
error_setg(errp,
|
error_setg(errp,
|
||||||
"The given file is neither a block nor a character device");
|
"The given file is neither a block nor a character device");
|
||||||
ret = -ENODEV;
|
ret = -ENODEV;
|
||||||
} else if (lseek(fd, 0, SEEK_END) < total_size * BDRV_SECTOR_SIZE) {
|
} else if (lseek(fd, 0, SEEK_END) < total_size) {
|
||||||
error_setg(errp, "Device is too small");
|
error_setg(errp, "Device is too small");
|
||||||
ret = -ENOSPC;
|
ret = -ENOSPC;
|
||||||
}
|
}
|
||||||
|
@ -511,8 +511,8 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
strstart(filename, "file:", &filename);
|
strstart(filename, "file:", &filename);
|
||||||
|
|
||||||
/* Read out options */
|
/* Read out options */
|
||||||
total_size = DIV_ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
||||||
BDRV_SECTOR_SIZE);
|
BDRV_SECTOR_SIZE);
|
||||||
|
|
||||||
fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
|
fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
|
||||||
0644);
|
0644);
|
||||||
@ -521,7 +521,7 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
|
|||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
set_sparse(fd);
|
set_sparse(fd);
|
||||||
ftruncate(fd, total_size * 512);
|
ftruncate(fd, total_size);
|
||||||
qemu_close(fd);
|
qemu_close(fd);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user