[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 12/62] block: Avoid bdrv_get_geometry() where errors
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PULL 12/62] block: Avoid bdrv_get_geometry() where errors should be detected |
Date: |
Fri, 8 Aug 2014 19:39:13 +0200 |
From: Markus Armbruster <address@hidden>
bdrv_get_geometry() hides errors. Use bdrv_nb_sectors() or
bdrv_getlength() instead where that's obviously inappropriate.
Signed-off-by: Markus Armbruster <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
Reviewed-by: Max Reitz <address@hidden>
Reviewed-by: Benoit Canet <address@hidden>
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
block.c | 11 ++++++++---
block/qapi.c | 14 +++++++++----
qemu-img.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++-------------
3 files changed, 68 insertions(+), 21 deletions(-)
diff --git a/block.c b/block.c
index ebb8aac..ea14981 100644
--- a/block.c
+++ b/block.c
@@ -5602,7 +5602,7 @@ void bdrv_img_create(const char *filename, const char
*fmt,
if (size == -1) {
if (backing_file) {
BlockDriverState *bs;
- uint64_t size;
+ int64_t size;
int back_flags;
/* backing files always opened read-only */
@@ -5620,8 +5620,13 @@ void bdrv_img_create(const char *filename, const char
*fmt,
local_err = NULL;
goto out;
}
- bdrv_get_geometry(bs, &size);
- size *= 512;
+ size = bdrv_getlength(bs);
+ if (size < 0) {
+ error_setg_errno(errp, -size, "Could not get size of '%s'",
+ backing_file);
+ bdrv_unref(bs);
+ goto out;
+ }
qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size);
diff --git a/block/qapi.c b/block/qapi.c
index f44f6b4..28ebb62 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -165,19 +165,25 @@ void bdrv_query_image_info(BlockDriverState *bs,
ImageInfo **p_info,
Error **errp)
{
- uint64_t total_sectors;
+ int64_t size;
const char *backing_filename;
char backing_filename2[1024];
BlockDriverInfo bdi;
int ret;
Error *err = NULL;
- ImageInfo *info = g_new0(ImageInfo, 1);
+ ImageInfo *info;
- bdrv_get_geometry(bs, &total_sectors);
+ size = bdrv_getlength(bs);
+ if (size < 0) {
+ error_setg_errno(errp, -size, "Can't get size of device '%s'",
+ bdrv_get_device_name(bs));
+ return;
+ }
+ info = g_new0(ImageInfo, 1);
info->filename = g_strdup(bs->filename);
info->format = g_strdup(bdrv_get_format_name(bs));
- info->virtual_size = total_sectors * 512;
+ info->virtual_size = size;
info->actual_size = bdrv_get_allocated_file_size(bs);
info->has_actual_size = info->actual_size >= 0;
if (bdrv_is_encrypted(bs)) {
diff --git a/qemu-img.c b/qemu-img.c
index b832997..9566fae 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -955,7 +955,6 @@ static int img_compare(int argc, char **argv)
int64_t sector_num = 0;
int64_t nb_sectors;
int c, pnum;
- uint64_t bs_sectors;
uint64_t progress_base;
for (;;) {
@@ -1017,10 +1016,20 @@ static int img_compare(int argc, char **argv)
buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
- bdrv_get_geometry(bs1, &bs_sectors);
- total_sectors1 = bs_sectors;
- bdrv_get_geometry(bs2, &bs_sectors);
- total_sectors2 = bs_sectors;
+ total_sectors1 = bdrv_nb_sectors(bs1);
+ if (total_sectors1 < 0) {
+ error_report("Can't get size of %s: %s",
+ filename1, strerror(-total_sectors1));
+ ret = 4;
+ goto out;
+ }
+ total_sectors2 = bdrv_nb_sectors(bs2);
+ if (total_sectors2 < 0) {
+ error_report("Can't get size of %s: %s",
+ filename2, strerror(-total_sectors2));
+ ret = 4;
+ goto out;
+ }
total_sectors = MIN(total_sectors1, total_sectors2);
progress_base = MAX(total_sectors1, total_sectors2);
@@ -1182,7 +1191,7 @@ static int img_convert(int argc, char **argv)
BlockDriver *drv, *proto_drv;
BlockDriverState **bs = NULL, *out_bs = NULL;
int64_t total_sectors, nb_sectors, sector_num, bs_offset;
- uint64_t *bs_sectors = NULL;
+ int64_t *bs_sectors = NULL;
uint8_t * buf = NULL;
size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
const uint8_t *buf1;
@@ -1324,7 +1333,7 @@ static int img_convert(int argc, char **argv)
qemu_progress_print(0, 100);
bs = g_new0(BlockDriverState *, bs_n);
- bs_sectors = g_new(uint64_t, bs_n);
+ bs_sectors = g_new(int64_t, bs_n);
total_sectors = 0;
for (bs_i = 0; bs_i < bs_n; bs_i++) {
@@ -1338,7 +1347,13 @@ static int img_convert(int argc, char **argv)
ret = -1;
goto out;
}
- bdrv_get_geometry(bs[bs_i], &bs_sectors[bs_i]);
+ bs_sectors[bs_i] = bdrv_nb_sectors(bs[bs_i]);
+ if (bs_sectors[bs_i] < 0) {
+ error_report("Could not get size of %s: %s",
+ argv[optind + bs_i], strerror(-bs_sectors[bs_i]));
+ ret = -1;
+ goto out;
+ }
total_sectors += bs_sectors[bs_i];
}
@@ -2413,9 +2428,9 @@ static int img_rebase(int argc, char **argv)
* the image is the same as the original one at any time.
*/
if (!unsafe) {
- uint64_t num_sectors;
- uint64_t old_backing_num_sectors;
- uint64_t new_backing_num_sectors = 0;
+ int64_t num_sectors;
+ int64_t old_backing_num_sectors;
+ int64_t new_backing_num_sectors = 0;
uint64_t sector;
int n;
uint8_t * buf_old;
@@ -2425,10 +2440,31 @@ static int img_rebase(int argc, char **argv)
buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
- bdrv_get_geometry(bs, &num_sectors);
- bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
+ num_sectors = bdrv_nb_sectors(bs);
+ if (num_sectors < 0) {
+ error_report("Could not get size of '%s': %s",
+ filename, strerror(-num_sectors));
+ ret = -1;
+ goto out;
+ }
+ old_backing_num_sectors = bdrv_nb_sectors(bs_old_backing);
+ if (old_backing_num_sectors < 0) {
+ char backing_name[1024];
+
+ bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
+ error_report("Could not get size of '%s': %s",
+ backing_name, strerror(-old_backing_num_sectors));
+ ret = -1;
+ goto out;
+ }
if (bs_new_backing) {
- bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
+ new_backing_num_sectors = bdrv_nb_sectors(bs_new_backing);
+ if (new_backing_num_sectors < 0) {
+ error_report("Could not get size of '%s': %s",
+ out_baseimg, strerror(-new_backing_num_sectors));
+ ret = -1;
+ goto out;
+ }
}
if (num_sectors != 0) {
--
1.8.3.1
- [Qemu-devel] [PULL 04/62] block: New bdrv_nb_sectors(), (continued)
- [Qemu-devel] [PULL 04/62] block: New bdrv_nb_sectors(), Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 02/62] block: Add AIO context notifiers, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 05/62] block: Use bdrv_nb_sectors() in bdrv_make_zero(), Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 06/62] block: Use bdrv_nb_sectors() in bdrv_aligned_preadv(), Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 03/62] nbd: Follow the BDS' AIO context, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 07/62] block: Use bdrv_nb_sectors() in bdrv_co_get_block_status(), Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 08/62] block: Use bdrv_nb_sectors() in img_convert(), Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 09/62] block: Use bdrv_nb_sectors() where sectors, not bytes are wanted, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 10/62] block: Drop superfluous aligning of bdrv_getlength()'s value, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 11/62] qemu-img: Make img_convert() get image size just once per image, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 12/62] block: Avoid bdrv_get_geometry() where errors should be detected,
Kevin Wolf <=
- [Qemu-devel] [PULL 14/62] configure: explicitly state version requirements to devel packages, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 15/62] xen_disk: fix possible null-ptr dereference, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 13/62] docs: Make the recommendation for the backing file name position a requirement, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 16/62] docs/multiple-iothreads.txt: add documentation on IOThread programming, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 18/62] qdev-monitor: include QOM properties in -device FOO, help output, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 17/62] qmp: hide "hotplugged" device property from device-list-properties, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 19/62] qemu-iotests: Add data pattern in version3 VMDK sample image in 059, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 21/62] qemu-img info: show nocow info, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 20/62] vmdk: Optimize cluster allocation, Kevin Wolf, 2014/08/08
- [Qemu-devel] [PULL 24/62] block/archipelago: Add support for creating images, Kevin Wolf, 2014/08/08