From nobody Tue Apr 30 03:39:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1489000548138918.3504090214624; Wed, 8 Mar 2017 11:15:48 -0800 (PST) Received: from localhost ([::1]:58008 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1clh3n-000340-OH for importer@patchew.org; Wed, 08 Mar 2017 14:15:43 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48239) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1clh2w-00030H-SI for qemu-devel@nongnu.org; Wed, 08 Mar 2017 14:14:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1clh2v-0007pl-Ir for qemu-devel@nongnu.org; Wed, 08 Mar 2017 14:14:50 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34914) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1clh2s-0007ob-2N; Wed, 08 Mar 2017 14:14:46 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4594C61D20; Wed, 8 Mar 2017 19:14:45 +0000 (UTC) Received: from localhost (ovpn-204-172.brq.redhat.com [10.40.204.172]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v28JEew8008781 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 8 Mar 2017 14:14:42 -0500 From: Max Reitz To: qemu-block@nongnu.org Date: Wed, 8 Mar 2017 20:14:31 +0100 Message-Id: <20170308191434.2413-2-mreitz@redhat.com> In-Reply-To: <20170308191434.2413-1-mreitz@redhat.com> References: <20170308191434.2413-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 08 Mar 2017 19:14:46 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 for-2.10 1/4] block/vhdx: Make vhdx_create() always set errp X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch makes vhdx_create() always set errp in case of an error. It also adds errp parameters to vhdx_create_bat() and vhdx_create_new_region_table() so we can pass on the error object generated by blk_truncate() as of a future commit. Signed-off-by: Max Reitz Reviewed-by: Kevin Wolf --- block/vhdx.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/block/vhdx.c b/block/vhdx.c index 052a753159..d25bcd91de 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -1586,7 +1586,7 @@ exit: static int vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s, uint64_t image_size, VHDXImageType type, bool use_zero_blocks, uint64_t file_offset, - uint32_t length) + uint32_t length, Error **errp) { int ret =3D 0; uint64_t data_file_offset; @@ -1609,14 +1609,19 @@ static int vhdx_create_bat(BlockBackend *blk, BDRVV= HDXState *s, * is the furthest thing we have written yet */ ret =3D blk_truncate(blk, data_file_offset); if (ret < 0) { + error_setg_errno(errp, -ret, + "Failed to resize the underlying file"); goto exit; } } else if (type =3D=3D VHDX_TYPE_FIXED) { ret =3D blk_truncate(blk, data_file_offset + image_size); if (ret < 0) { + error_setg_errno(errp, -ret, + "Failed to resize the underlying file"); goto exit; } } else { + error_setg(errp, "Unsupported image type"); ret =3D -ENOTSUP; goto exit; } @@ -1627,6 +1632,7 @@ static int vhdx_create_bat(BlockBackend *blk, BDRVVHD= XState *s, /* for a fixed file, the default BAT entry is not zero */ s->bat =3D g_try_malloc0(length); if (length && s->bat =3D=3D NULL) { + error_setg(errp, "Failed to allocate memory for the BAT"); ret =3D -ENOMEM; goto exit; } @@ -1646,6 +1652,7 @@ static int vhdx_create_bat(BlockBackend *blk, BDRVVHD= XState *s, } ret =3D blk_pwrite(blk, file_offset, s->bat, length, 0); if (ret < 0) { + error_setg_errno(errp, -ret, "Failed to write the BAT"); goto exit; } } @@ -1671,7 +1678,8 @@ static int vhdx_create_new_region_table(BlockBackend = *blk, uint32_t log_size, bool use_zero_blocks, VHDXImageType type, - uint64_t *metadata_offset) + uint64_t *metadata_offset, + Error **errp) { int ret =3D 0; uint32_t offset =3D 0; @@ -1740,7 +1748,7 @@ static int vhdx_create_new_region_table(BlockBackend = *blk, /* The region table gives us the data we need to create the BAT, * so do that now */ ret =3D vhdx_create_bat(blk, s, image_size, type, use_zero_blocks, - bat_file_offset, bat_length); + bat_file_offset, bat_length, errp); if (ret < 0) { goto exit; } @@ -1749,12 +1757,14 @@ static int vhdx_create_new_region_table(BlockBacken= d *blk, ret =3D blk_pwrite(blk, VHDX_REGION_TABLE_OFFSET, buffer, VHDX_HEADER_BLOCK_SIZE, 0); if (ret < 0) { + error_setg_errno(errp, -ret, "Failed to write first region table"); goto exit; } =20 ret =3D blk_pwrite(blk, VHDX_REGION_TABLE2_OFFSET, buffer, VHDX_HEADER_BLOCK_SIZE, 0); if (ret < 0) { + error_setg_errno(errp, -ret, "Failed to write second region table"= ); goto exit; } =20 @@ -1825,6 +1835,7 @@ static int vhdx_create(const char *filename, QemuOpts= *opts, Error **errp) ret =3D -ENOTSUP; goto exit; } else { + error_setg(errp, "Invalid subformat '%s'", type); ret =3D -EINVAL; goto exit; } @@ -1879,12 +1890,14 @@ static int vhdx_create(const char *filename, QemuOp= ts *opts, Error **errp) ret =3D blk_pwrite(blk, VHDX_FILE_ID_OFFSET, &signature, sizeof(signat= ure), 0); if (ret < 0) { + error_setg_errno(errp, -ret, "Failed to write file signature"); goto delete_and_exit; } if (creator) { ret =3D blk_pwrite(blk, VHDX_FILE_ID_OFFSET + sizeof(signature), creator, creator_items * sizeof(gunichar2), 0); if (ret < 0) { + error_setg_errno(errp, -ret, "Failed to write creator field"); goto delete_and_exit; } } @@ -1893,13 +1906,14 @@ static int vhdx_create(const char *filename, QemuOp= ts *opts, Error **errp) /* Creates (B),(C) */ ret =3D vhdx_create_new_headers(blk, image_size, log_size); if (ret < 0) { + error_setg_errno(errp, -ret, "Failed to write image headers"); goto delete_and_exit; } =20 /* Creates (D),(E),(G) explicitly. (F) created as by-product */ ret =3D vhdx_create_new_region_table(blk, image_size, block_size, 512, log_size, use_zero_blocks, image_ty= pe, - &metadata_offset); + &metadata_offset, errp); if (ret < 0) { goto delete_and_exit; } @@ -1908,6 +1922,7 @@ static int vhdx_create(const char *filename, QemuOpts= *opts, Error **errp) ret =3D vhdx_create_new_metadata(blk, image_size, block_size, 512, metadata_offset, image_type); if (ret < 0) { + error_setg_errno(errp, -ret, "Failed to initialize metadata"); goto delete_and_exit; } =20 --=20 2.12.0 From nobody Tue Apr 30 03:39:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1489000559556517.267233028321; Wed, 8 Mar 2017 11:15:59 -0800 (PST) Received: from localhost ([::1]:58011 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1clh3y-0003A6-UW for importer@patchew.org; Wed, 08 Mar 2017 14:15:54 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48270) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1clh36-000378-8J for qemu-devel@nongnu.org; Wed, 08 Mar 2017 14:15:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1clh33-0007rs-9d for qemu-devel@nongnu.org; Wed, 08 Mar 2017 14:15:00 -0500 Received: from mx1.redhat.com ([209.132.183.28]:53106) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1clh2w-0007pu-SA; Wed, 08 Mar 2017 14:14:51 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 242E33D95C; Wed, 8 Mar 2017 19:14:50 +0000 (UTC) Received: from localhost (ovpn-204-172.brq.redhat.com [10.40.204.172]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v28JEiWc005432 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 8 Mar 2017 14:14:45 -0500 From: Max Reitz To: qemu-block@nongnu.org Date: Wed, 8 Mar 2017 20:14:32 +0100 Message-Id: <20170308191434.2413-3-mreitz@redhat.com> In-Reply-To: <20170308191434.2413-1-mreitz@redhat.com> References: <20170308191434.2413-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 08 Mar 2017 19:14:51 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 for-2.10 2/4] block: Add errp to b{lk, drv}_truncate() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" For one thing, this allows us to drop the error message generation from qemu-img.c and blockdev.c and instead have it unified in bdrv_truncate(). Signed-off-by: Max Reitz Reviewed-by: Kevin Wolf --- include/block/block.h | 2 +- include/sysemu/block-backend.h | 2 +- block.c | 16 ++++++++++++---- block/blkdebug.c | 2 +- block/block-backend.c | 5 +++-- block/commit.c | 5 +++-- block/crypto.c | 2 +- block/mirror.c | 2 +- block/parallels.c | 13 ++++++++----- block/qcow.c | 6 +++--- block/qcow2-refcount.c | 5 ++++- block/qcow2.c | 13 ++++++++----- block/qed.c | 2 +- block/raw-format.c | 2 +- block/vdi.c | 4 ++-- block/vhdx-log.c | 2 +- block/vhdx.c | 10 +++------- block/vmdk.c | 13 +++---------- block/vpc.c | 13 +++++++------ blockdev.c | 21 +-------------------- qemu-img.c | 17 ++++------------- qemu-io-cmds.c | 5 +++-- 22 files changed, 72 insertions(+), 90 deletions(-) diff --git a/include/block/block.h b/include/block/block.h index 5149260827..4c9ed0e43c 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -294,7 +294,7 @@ BlockDriverState *bdrv_find_backing_image(BlockDriverSt= ate *bs, const char *backing_file); int bdrv_get_backing_file_depth(BlockDriverState *bs); void bdrv_refresh_filename(BlockDriverState *bs); -int bdrv_truncate(BdrvChild *child, int64_t offset); +int bdrv_truncate(BdrvChild *child, int64_t offset, Error **errp); int64_t bdrv_nb_sectors(BlockDriverState *bs); int64_t bdrv_getlength(BlockDriverState *bs); int64_t bdrv_get_allocated_file_size(BlockDriverState *bs); diff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h index 096c17fce0..c142106006 100644 --- a/include/sysemu/block-backend.h +++ b/include/sysemu/block-backend.h @@ -217,7 +217,7 @@ int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk= , int64_t offset, int count, BdrvRequestFlags flags); int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *b= uf, int count); -int blk_truncate(BlockBackend *blk, int64_t offset); +int blk_truncate(BlockBackend *blk, int64_t offset, Error **errp); int blk_pdiscard(BlockBackend *blk, int64_t offset, int count); int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf, int64_t pos, int size); diff --git a/block.c b/block.c index b404ef251a..8e8ba60dc0 100644 --- a/block.c +++ b/block.c @@ -3207,7 +3207,7 @@ exit: /** * Truncate file to 'offset' bytes (needed only for file protocols) */ -int bdrv_truncate(BdrvChild *child, int64_t offset) +int bdrv_truncate(BdrvChild *child, int64_t offset, Error **errp) { BlockDriverState *bs =3D child->bs; BlockDriver *drv =3D bs->drv; @@ -3215,12 +3215,18 @@ int bdrv_truncate(BdrvChild *child, int64_t offset) =20 assert(child->perm & BLK_PERM_RESIZE); =20 - if (!drv) + if (!drv) { + error_setg(errp, "No medium inserted"); return -ENOMEDIUM; - if (!drv->bdrv_truncate) + } + if (!drv->bdrv_truncate) { + error_setg(errp, "Image format driver does not support resize"); return -ENOTSUP; - if (bs->read_only) + } + if (bs->read_only) { + error_setg(errp, "Image is read-only"); return -EACCES; + } =20 ret =3D drv->bdrv_truncate(bs, offset); if (ret =3D=3D 0) { @@ -3228,6 +3234,8 @@ int bdrv_truncate(BdrvChild *child, int64_t offset) bdrv_dirty_bitmap_truncate(bs); bdrv_parent_cb_resize(bs); ++bs->write_gen; + } else { + error_setg_errno(errp, -ret, "Failed to resize image"); } return ret; } diff --git a/block/blkdebug.c b/block/blkdebug.c index 67e8024e36..15a9966096 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -663,7 +663,7 @@ static int64_t blkdebug_getlength(BlockDriverState *bs) =20 static int blkdebug_truncate(BlockDriverState *bs, int64_t offset) { - return bdrv_truncate(bs->file, offset); + return bdrv_truncate(bs->file, offset, NULL); } =20 static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options) diff --git a/block/block-backend.c b/block/block-backend.c index 5742c09c2c..7de172e02c 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -1698,13 +1698,14 @@ int blk_pwrite_compressed(BlockBackend *blk, int64_= t offset, const void *buf, BDRV_REQ_WRITE_COMPRESSED); } =20 -int blk_truncate(BlockBackend *blk, int64_t offset) +int blk_truncate(BlockBackend *blk, int64_t offset, Error **errp) { if (!blk_is_available(blk)) { + error_setg(errp, "No medium inserted"); return -ENOMEDIUM; } =20 - return bdrv_truncate(blk->root, offset); + return bdrv_truncate(blk->root, offset, errp); } =20 static void blk_pdiscard_entry(void *opaque) diff --git a/block/commit.c b/block/commit.c index 9c4198837f..adec1c50e5 100644 --- a/block/commit.c +++ b/block/commit.c @@ -150,7 +150,7 @@ static void coroutine_fn commit_run(void *opaque) } =20 if (base_len < s->common.len) { - ret =3D blk_truncate(s->base, s->common.len); + ret =3D blk_truncate(s->base, s->common.len, NULL); if (ret) { goto out; } @@ -488,8 +488,9 @@ int bdrv_commit(BlockDriverState *bs) * grow the backing file image if possible. If not possible, * we must return an error */ if (length > backing_length) { - ret =3D blk_truncate(backing, length); + ret =3D blk_truncate(backing, length, &local_err); if (ret < 0) { + error_report_err(local_err); goto ro_cleanup; } } diff --git a/block/crypto.c b/block/crypto.c index 4a2038888d..52e4f2b20f 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -389,7 +389,7 @@ static int block_crypto_truncate(BlockDriverState *bs, = int64_t offset) =20 offset +=3D payload_offset; =20 - return bdrv_truncate(bs->file, offset); + return bdrv_truncate(bs->file, offset, NULL); } =20 static void block_crypto_close(BlockDriverState *bs) diff --git a/block/mirror.c b/block/mirror.c index a5d30ee575..599a2bf10a 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -721,7 +721,7 @@ static void coroutine_fn mirror_run(void *opaque) } =20 if (s->bdev_length > base_length) { - ret =3D blk_truncate(s->target, s->bdev_length); + ret =3D blk_truncate(s->target, s->bdev_length, NULL); if (ret < 0) { goto immediate_exit; } diff --git a/block/parallels.c b/block/parallels.c index 19935e29a9..618fc860e2 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -216,7 +216,8 @@ static int64_t allocate_clusters(BlockDriverState *bs, = int64_t sector_num, space << BDRV_SECTOR_BITS, 0); } else { ret =3D bdrv_truncate(bs->file, - (s->data_end + space) << BDRV_SECTOR_BITS); + (s->data_end + space) << BDRV_SECTOR_BITS, + NULL); } if (ret < 0) { return ret; @@ -449,8 +450,10 @@ static int parallels_check(BlockDriverState *bs, BdrvC= heckResult *res, size - res->image_end_offset); res->leaks +=3D count; if (fix & BDRV_FIX_LEAKS) { - ret =3D bdrv_truncate(bs->file, res->image_end_offset); + Error *local_err =3D NULL; + ret =3D bdrv_truncate(bs->file, res->image_end_offset, &local_= err); if (ret < 0) { + error_report_err(local_err); res->check_errors++; return ret; } @@ -497,7 +500,7 @@ static int parallels_create(const char *filename, QemuO= pts *opts, Error **errp) =20 blk_set_allow_write_beyond_eof(file, true); =20 - ret =3D blk_truncate(file, 0); + ret =3D blk_truncate(file, 0, errp); if (ret < 0) { goto exit; } @@ -688,7 +691,7 @@ static int parallels_open(BlockDriverState *bs, QDict *= options, int flags, goto fail_options; } if (!bdrv_has_zero_init(bs->file->bs) || - bdrv_truncate(bs->file, bdrv_getlength(bs->file->bs)) !=3D 0) { + bdrv_truncate(bs->file, bdrv_getlength(bs->file->bs), NULL) != =3D 0) { s->prealloc_mode =3D PRL_PREALLOC_MODE_FALLOCATE; } =20 @@ -731,7 +734,7 @@ static void parallels_close(BlockDriverState *bs) } =20 if (bs->open_flags & BDRV_O_RDWR) { - bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS); + bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, NULL); } =20 g_free(s->bat_dirty_bmap); diff --git a/block/qcow.c b/block/qcow.c index 9d6ac83959..5d147b962e 100644 --- a/block/qcow.c +++ b/block/qcow.c @@ -473,7 +473,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, /* round to cluster size */ cluster_offset =3D (cluster_offset + s->cluster_size - 1) & ~(s->cluster_size - 1); - bdrv_truncate(bs->file, cluster_offset + s->cluster_size); + bdrv_truncate(bs->file, cluster_offset + s->cluster_size, = NULL); /* if encrypted, we must initialize the cluster content which won't be written */ if (bs->encrypted && @@ -833,7 +833,7 @@ static int qcow_create(const char *filename, QemuOpts *= opts, Error **errp) =20 blk_set_allow_write_beyond_eof(qcow_blk, true); =20 - ret =3D blk_truncate(qcow_blk, 0); + ret =3D blk_truncate(qcow_blk, 0, errp); if (ret < 0) { goto exit; } @@ -916,7 +916,7 @@ static int qcow_make_empty(BlockDriverState *bs) if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table, l1_length) < 0) return -1; - ret =3D bdrv_truncate(bs->file, s->l1_table_offset + l1_length); + ret =3D bdrv_truncate(bs->file, s->l1_table_offset + l1_length, NULL); if (ret < 0) return ret; =20 diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 9e96f64c8b..4efca7ebdb 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1728,14 +1728,17 @@ static int check_refblocks(BlockDriverState *bs, Bd= rvCheckResult *res, =20 if (fix & BDRV_FIX_ERRORS) { int64_t new_nb_clusters; + Error *local_err =3D NULL; =20 if (offset > INT64_MAX - s->cluster_size) { ret =3D -EINVAL; goto resize_fail; } =20 - ret =3D bdrv_truncate(bs->file, offset + s->cluster_size); + ret =3D bdrv_truncate(bs->file, offset + s->cluster_size, + &local_err); if (ret < 0) { + error_report_err(local_err); goto resize_fail; } size =3D bdrv_getlength(bs->file->bs); diff --git a/block/qcow2.c b/block/qcow2.c index 6a92d2ef3f..43b8a986f0 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2294,9 +2294,8 @@ static int qcow2_create2(const char *filename, int64_= t total_size, } =20 /* Okay, now that we have a valid image, let's give it the right size = */ - ret =3D blk_truncate(blk, total_size); + ret =3D blk_truncate(blk, total_size, errp); if (ret < 0) { - error_setg_errno(errp, -ret, "Could not resize image"); goto out; } =20 @@ -2584,7 +2583,7 @@ qcow2_co_pwritev_compressed(BlockDriverState *bs, uin= t64_t offset, /* align end of file to a sector boundary to ease reading with sector based I/Os */ cluster_offset =3D bdrv_getlength(bs->file->bs); - return bdrv_truncate(bs->file, cluster_offset); + return bdrv_truncate(bs->file, cluster_offset, NULL); } =20 buf =3D qemu_blockalign(bs, s->cluster_size); @@ -2674,6 +2673,7 @@ fail: static int make_completely_empty(BlockDriverState *bs) { BDRVQcow2State *s =3D bs->opaque; + Error *local_err =3D NULL; int ret, l1_clusters; int64_t offset; uint64_t *new_reftable =3D NULL; @@ -2798,8 +2798,10 @@ static int make_completely_empty(BlockDriverState *b= s) goto fail; } =20 - ret =3D bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size); + ret =3D bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size, + &local_err); if (ret < 0) { + error_report_err(local_err); goto fail; } =20 @@ -3273,9 +3275,10 @@ static int qcow2_amend_options(BlockDriverState *bs,= QemuOpts *opts, return ret; } =20 - ret =3D blk_truncate(blk, new_size); + ret =3D blk_truncate(blk, new_size, &local_err); blk_unref(blk); if (ret < 0) { + error_report_err(local_err); return ret; } } diff --git a/block/qed.c b/block/qed.c index 5ec7fd83f2..53199ac8d7 100644 --- a/block/qed.c +++ b/block/qed.c @@ -635,7 +635,7 @@ static int qed_create(const char *filename, uint32_t cl= uster_size, blk_set_allow_write_beyond_eof(blk, true); =20 /* File must start empty and grow, check truncate is supported */ - ret =3D blk_truncate(blk, 0); + ret =3D blk_truncate(blk, 0, errp); if (ret < 0) { goto out; } diff --git a/block/raw-format.c b/block/raw-format.c index 86fbc657eb..a80073369a 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -341,7 +341,7 @@ static int raw_truncate(BlockDriverState *bs, int64_t o= ffset) =20 s->size =3D offset; offset +=3D s->offset; - return bdrv_truncate(bs->file, offset); + return bdrv_truncate(bs->file, offset, NULL); } =20 static int raw_media_changed(BlockDriverState *bs) diff --git a/block/vdi.c b/block/vdi.c index 9b4f70e977..d12d9cdc79 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -832,9 +832,9 @@ static int vdi_create(const char *filename, QemuOpts *o= pts, Error **errp) } =20 if (image_type =3D=3D VDI_TYPE_STATIC) { - ret =3D blk_truncate(blk, offset + blocks * block_size); + ret =3D blk_truncate(blk, offset + blocks * block_size, errp); if (ret < 0) { - error_setg(errp, "Failed to statically allocate %s", filename); + error_prepend(errp, "Failed to statically allocate %s", filena= me); goto exit; } } diff --git a/block/vhdx-log.c b/block/vhdx-log.c index 67a91c0de5..3f4c2aa095 100644 --- a/block/vhdx-log.c +++ b/block/vhdx-log.c @@ -548,7 +548,7 @@ static int vhdx_log_flush(BlockDriverState *bs, BDRVVHD= XState *s, if (new_file_size % (1024*1024)) { /* round up to nearest 1MB boundary */ new_file_size =3D ((new_file_size >> 20) + 1) << 20; - bdrv_truncate(bs->file, new_file_size); + bdrv_truncate(bs->file, new_file_size, NULL); } } qemu_vfree(desc_entries); diff --git a/block/vhdx.c b/block/vhdx.c index d25bcd91de..e8fe3fb5e9 100644 --- a/block/vhdx.c +++ b/block/vhdx.c @@ -1171,7 +1171,7 @@ static int vhdx_allocate_block(BlockDriverState *bs, = BDRVVHDXState *s, /* per the spec, the address for a block is in units of 1MB */ *new_offset =3D ROUND_UP(*new_offset, 1024 * 1024); =20 - return bdrv_truncate(bs->file, *new_offset + s->block_size); + return bdrv_truncate(bs->file, *new_offset + s->block_size, NULL); } =20 /* @@ -1607,17 +1607,13 @@ static int vhdx_create_bat(BlockBackend *blk, BDRVV= HDXState *s, if (type =3D=3D VHDX_TYPE_DYNAMIC) { /* All zeroes, so we can just extend the file - the end of the BAT * is the furthest thing we have written yet */ - ret =3D blk_truncate(blk, data_file_offset); + ret =3D blk_truncate(blk, data_file_offset, errp); if (ret < 0) { - error_setg_errno(errp, -ret, - "Failed to resize the underlying file"); goto exit; } } else if (type =3D=3D VHDX_TYPE_FIXED) { - ret =3D blk_truncate(blk, data_file_offset + image_size); + ret =3D blk_truncate(blk, data_file_offset + image_size, errp); if (ret < 0) { - error_setg_errno(errp, -ret, - "Failed to resize the underlying file"); goto exit; } } else { diff --git a/block/vmdk.c b/block/vmdk.c index a9bd22bf93..c61b9cc8e0 100644 --- a/block/vmdk.c +++ b/block/vmdk.c @@ -1714,10 +1714,7 @@ static int vmdk_create_extent(const char *filename, = int64_t filesize, blk_set_allow_write_beyond_eof(blk, true); =20 if (flat) { - ret =3D blk_truncate(blk, filesize); - if (ret < 0) { - error_setg_errno(errp, -ret, "Could not truncate file"); - } + ret =3D blk_truncate(blk, filesize, errp); goto exit; } magic =3D cpu_to_be32(VMDK4_MAGIC); @@ -1780,9 +1777,8 @@ static int vmdk_create_extent(const char *filename, i= nt64_t filesize, goto exit; } =20 - ret =3D blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9); + ret =3D blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9, errp); if (ret < 0) { - error_setg_errno(errp, -ret, "Could not truncate file"); goto exit; } =20 @@ -2090,10 +2086,7 @@ static int vmdk_create(const char *filename, QemuOpt= s *opts, Error **errp) /* bdrv_pwrite write padding zeros to align to sector, we don't need t= hat * for description file */ if (desc_offset =3D=3D 0) { - ret =3D blk_truncate(new_blk, desc_len); - if (ret < 0) { - error_setg_errno(errp, -ret, "Could not truncate file"); - } + ret =3D blk_truncate(new_blk, desc_len, errp); } exit: if (new_blk) { diff --git a/block/vpc.c b/block/vpc.c index f591d4be38..ecfee77149 100644 --- a/block/vpc.c +++ b/block/vpc.c @@ -851,20 +851,21 @@ static int create_dynamic_disk(BlockBackend *blk, uin= t8_t *buf, } =20 static int create_fixed_disk(BlockBackend *blk, uint8_t *buf, - int64_t total_size) + int64_t total_size, Error **errp) { int ret; =20 /* Add footer to total size */ total_size +=3D HEADER_SIZE; =20 - ret =3D blk_truncate(blk, total_size); + ret =3D blk_truncate(blk, total_size, errp); if (ret < 0) { return ret; } =20 ret =3D blk_pwrite(blk, total_size - HEADER_SIZE, buf, HEADER_SIZE, 0); if (ret < 0) { + error_setg_errno(errp, -ret, "Unable to write VHD header"); return ret; } =20 @@ -996,11 +997,11 @@ static int vpc_create(const char *filename, QemuOpts = *opts, Error **errp) =20 if (disk_type =3D=3D VHD_DYNAMIC) { ret =3D create_dynamic_disk(blk, buf, total_sectors); + if (ret < 0) { + error_setg(errp, "Unable to create or write VHD header"); + } } else { - ret =3D create_fixed_disk(blk, buf, total_size); - } - if (ret < 0) { - error_setg(errp, "Unable to create or write VHD header"); + ret =3D create_fixed_disk(blk, buf, total_size, errp); } =20 out: diff --git a/blockdev.c b/blockdev.c index f1f49bd3ca..de50d461d2 100644 --- a/blockdev.c +++ b/blockdev.c @@ -2928,26 +2928,7 @@ void qmp_block_resize(bool has_device, const char *d= evice, /* complete all in-flight operations before resizing the device */ bdrv_drain_all(); =20 - ret =3D blk_truncate(blk, size); - switch (ret) { - case 0: - break; - case -ENOMEDIUM: - error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); - break; - case -ENOTSUP: - error_setg(errp, QERR_UNSUPPORTED); - break; - case -EACCES: - error_setg(errp, "Device '%s' is read only", device); - break; - case -EBUSY: - error_setg(errp, QERR_DEVICE_IN_USE, device); - break; - default: - error_setg_errno(errp, -ret, "Could not resize"); - break; - } + ret =3D blk_truncate(blk, size, errp); =20 out: blk_unref(blk); diff --git a/qemu-img.c b/qemu-img.c index 98b836b030..0b0178949d 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -3440,20 +3440,11 @@ static int img_resize(int argc, char **argv) goto out; } =20 - ret =3D blk_truncate(blk, total_size); - switch (ret) { - case 0: + ret =3D blk_truncate(blk, total_size, &err); + if (!ret) { qprintf(quiet, "Image resized.\n"); - break; - case -ENOTSUP: - error_report("This image does not support resize"); - break; - case -EACCES: - error_report("Image is read-only"); - break; - default: - error_report("Error resizing image: %s", strerror(-ret)); - break; + } else { + error_report_err(err); } out: blk_unref(blk); diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index 2c48f9ce1a..05bb0b34ec 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -1560,6 +1560,7 @@ static const cmdinfo_t flush_cmd =3D { =20 static int truncate_f(BlockBackend *blk, int argc, char **argv) { + Error *local_err =3D NULL; int64_t offset; int ret; =20 @@ -1569,9 +1570,9 @@ static int truncate_f(BlockBackend *blk, int argc, ch= ar **argv) return 0; } =20 - ret =3D blk_truncate(blk, offset); + ret =3D blk_truncate(blk, offset, &local_err); if (ret < 0) { - printf("truncate: %s\n", strerror(-ret)); + error_report_err(local_err); return 0; } =20 --=20 2.12.0 From nobody Tue Apr 30 03:39:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1489000668193624.4551400051818; Wed, 8 Mar 2017 11:17:48 -0800 (PST) Received: from localhost ([::1]:58023 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1clh5l-0004hz-Se for importer@patchew.org; Wed, 08 Mar 2017 14:17:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48694) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1clh41-0003xC-KO for qemu-devel@nongnu.org; Wed, 08 Mar 2017 14:16:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1clh3z-0008Ju-O4 for qemu-devel@nongnu.org; Wed, 08 Mar 2017 14:15:57 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51480) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1clh3u-0008Do-0d; Wed, 08 Mar 2017 14:15:50 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5870AC057FAC; Wed, 8 Mar 2017 19:15:49 +0000 (UTC) Received: from localhost (ovpn-204-172.brq.redhat.com [10.40.204.172]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v28JFlKk009992 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 8 Mar 2017 14:15:48 -0500 From: Max Reitz To: qemu-block@nongnu.org Date: Wed, 8 Mar 2017 20:15:43 +0100 Message-Id: <20170308191544.3025-1-mreitz@redhat.com> In-Reply-To: <20170308191434.2413-1-mreitz@redhat.com> References: <20170308191434.2413-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 08 Mar 2017 19:15:49 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 for-2.10 3/4] block: Add errp to BD.bdrv_truncate() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add an Error parameter to the block drivers' bdrv_truncate() interface. If a block driver does not set this in case of an error, the generic bdrv_truncate() implementation will do so. Where it is obvious, this patch also makes some block drivers set this value. Signed-off-by: Max Reitz Reviewed-by: Kevin Wolf --- include/block/block_int.h | 2 +- block.c | 4 ++-- block/archipelago.c | 3 ++- block/blkdebug.c | 4 ++-- block/crypto.c | 5 +++-- block/file-posix.c | 2 +- block/file-win32.c | 6 +++--- block/gluster.c | 3 ++- block/iscsi.c | 4 ++-- block/nfs.c | 2 +- block/qcow2.c | 8 ++++---- block/qed.c | 2 +- block/raw-format.c | 4 ++-- block/rbd.c | 2 +- block/sheepdog.c | 14 ++++++-------- 15 files changed, 33 insertions(+), 32 deletions(-) diff --git a/include/block/block_int.h b/include/block/block_int.h index 6c699ac9c3..ee55d5e9cd 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -196,7 +196,7 @@ struct BlockDriver { int coroutine_fn (*bdrv_co_flush_to_os)(BlockDriverState *bs); =20 const char *protocol_name; - int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset); + int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset, Error **err= p); =20 int64_t (*bdrv_getlength)(BlockDriverState *bs); bool has_variable_length; diff --git a/block.c b/block.c index 8e8ba60dc0..01872b6f5a 100644 --- a/block.c +++ b/block.c @@ -3228,13 +3228,13 @@ int bdrv_truncate(BdrvChild *child, int64_t offset,= Error **errp) return -EACCES; } =20 - ret =3D drv->bdrv_truncate(bs, offset); + ret =3D drv->bdrv_truncate(bs, offset, errp); if (ret =3D=3D 0) { ret =3D refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); bdrv_dirty_bitmap_truncate(bs); bdrv_parent_cb_resize(bs); ++bs->write_gen; - } else { + } else if (errp && !*errp) { error_setg_errno(errp, -ret, "Failed to resize image"); } return ret; diff --git a/block/archipelago.c b/block/archipelago.c index 01c4ff2f7d..e65a3ff230 100644 --- a/block/archipelago.c +++ b/block/archipelago.c @@ -977,7 +977,8 @@ static int64_t qemu_archipelago_getlength(BlockDriverSt= ate *bs) return archipelago_volume_info(s); } =20 -static int qemu_archipelago_truncate(BlockDriverState *bs, int64_t offset) +static int qemu_archipelago_truncate(BlockDriverState *bs, int64_t offset, + Error **errp) { int ret, targetlen; struct xseg_request *req; diff --git a/block/blkdebug.c b/block/blkdebug.c index 15a9966096..c795ae9e72 100644 --- a/block/blkdebug.c +++ b/block/blkdebug.c @@ -661,9 +661,9 @@ static int64_t blkdebug_getlength(BlockDriverState *bs) return bdrv_getlength(bs->file->bs); } =20 -static int blkdebug_truncate(BlockDriverState *bs, int64_t offset) +static int blkdebug_truncate(BlockDriverState *bs, int64_t offset, Error *= *errp) { - return bdrv_truncate(bs->file, offset, NULL); + return bdrv_truncate(bs->file, offset, errp); } =20 static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options) diff --git a/block/crypto.c b/block/crypto.c index 52e4f2b20f..17b3140998 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -381,7 +381,8 @@ static int block_crypto_create_generic(QCryptoBlockForm= at format, return ret; } =20 -static int block_crypto_truncate(BlockDriverState *bs, int64_t offset) +static int block_crypto_truncate(BlockDriverState *bs, int64_t offset, + Error **errp) { BlockCrypto *crypto =3D bs->opaque; size_t payload_offset =3D @@ -389,7 +390,7 @@ static int block_crypto_truncate(BlockDriverState *bs, = int64_t offset) =20 offset +=3D payload_offset; =20 - return bdrv_truncate(bs->file, offset, NULL); + return bdrv_truncate(bs->file, offset, errp); } =20 static void block_crypto_close(BlockDriverState *bs) diff --git a/block/file-posix.c b/block/file-posix.c index 4de1abd023..9d2bea730d 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1337,7 +1337,7 @@ static void raw_close(BlockDriverState *bs) } } =20 -static int raw_truncate(BlockDriverState *bs, int64_t offset) +static int raw_truncate(BlockDriverState *bs, int64_t offset, Error **errp) { BDRVRawState *s =3D bs->opaque; struct stat st; diff --git a/block/file-win32.c b/block/file-win32.c index 800fabdd72..3f3925623f 100644 --- a/block/file-win32.c +++ b/block/file-win32.c @@ -461,7 +461,7 @@ static void raw_close(BlockDriverState *bs) } } =20 -static int raw_truncate(BlockDriverState *bs, int64_t offset) +static int raw_truncate(BlockDriverState *bs, int64_t offset, Error **errp) { BDRVRawState *s =3D bs->opaque; LONG low, high; @@ -476,11 +476,11 @@ static int raw_truncate(BlockDriverState *bs, int64_t= offset) */ dwPtrLow =3D SetFilePointer(s->hfile, low, &high, FILE_BEGIN); if (dwPtrLow =3D=3D INVALID_SET_FILE_POINTER && GetLastError() !=3D NO= _ERROR) { - fprintf(stderr, "SetFilePointer error: %lu\n", GetLastError()); + error_setg_win32(errp, GetLastError(), "SetFilePointer error"); return -EIO; } if (SetEndOfFile(s->hfile) =3D=3D 0) { - fprintf(stderr, "SetEndOfFile error: %lu\n", GetLastError()); + error_setg_win32(errp, GetLastError(), "SetEndOfFile error"); return -EIO; } return 0; diff --git a/block/gluster.c b/block/gluster.c index a577daef10..00b8240562 100644 --- a/block/gluster.c +++ b/block/gluster.c @@ -1084,7 +1084,8 @@ static coroutine_fn int qemu_gluster_co_rw(BlockDrive= rState *bs, return acb.ret; } =20 -static int qemu_gluster_truncate(BlockDriverState *bs, int64_t offset) +static int qemu_gluster_truncate(BlockDriverState *bs, int64_t offset, + Error **errp) { int ret; BDRVGlusterState *s =3D bs->opaque; diff --git a/block/iscsi.c b/block/iscsi.c index 75d890538e..ab559a6f71 100644 --- a/block/iscsi.c +++ b/block/iscsi.c @@ -2060,7 +2060,7 @@ static void iscsi_reopen_commit(BDRVReopenState *reop= en_state) } } =20 -static int iscsi_truncate(BlockDriverState *bs, int64_t offset) +static int iscsi_truncate(BlockDriverState *bs, int64_t offset, Error **er= rp) { IscsiLun *iscsilun =3D bs->opaque; Error *local_err =3D NULL; @@ -2071,7 +2071,7 @@ static int iscsi_truncate(BlockDriverState *bs, int64= _t offset) =20 iscsi_readcapacity_sync(iscsilun, &local_err); if (local_err !=3D NULL) { - error_free(local_err); + error_propagate(errp, local_err); return -EIO; } =20 diff --git a/block/nfs.c b/block/nfs.c index 3f43f6e26a..57d12efc51 100644 --- a/block/nfs.c +++ b/block/nfs.c @@ -757,7 +757,7 @@ static int64_t nfs_get_allocated_file_size(BlockDriverS= tate *bs) return (task.ret < 0 ? task.ret : st.st_blocks * 512); } =20 -static int nfs_file_truncate(BlockDriverState *bs, int64_t offset) +static int nfs_file_truncate(BlockDriverState *bs, int64_t offset, Error *= *errp) { NFSClient *client =3D bs->opaque; return nfs_ftruncate(client->context, client->fh, offset); diff --git a/block/qcow2.c b/block/qcow2.c index 43b8a986f0..17585fbb89 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2524,26 +2524,26 @@ static coroutine_fn int qcow2_co_pdiscard(BlockDriv= erState *bs, return ret; } =20 -static int qcow2_truncate(BlockDriverState *bs, int64_t offset) +static int qcow2_truncate(BlockDriverState *bs, int64_t offset, Error **er= rp) { BDRVQcow2State *s =3D bs->opaque; int64_t new_l1_size; int ret; =20 if (offset & 511) { - error_report("The new size must be a multiple of 512"); + error_setg(errp, "The new size must be a multiple of 512"); return -EINVAL; } =20 /* cannot proceed if image has snapshots */ if (s->nb_snapshots) { - error_report("Can't resize an image which has snapshots"); + error_setg(errp, "Can't resize an image which has snapshots"); return -ENOTSUP; } =20 /* shrinking is currently not supported */ if (offset < bs->total_sectors * 512) { - error_report("qcow2 doesn't support shrinking images yet"); + error_setg(errp, "qcow2 doesn't support shrinking images yet"); return -ENOTSUP; } =20 diff --git a/block/qed.c b/block/qed.c index 53199ac8d7..fa2aeee471 100644 --- a/block/qed.c +++ b/block/qed.c @@ -1518,7 +1518,7 @@ static int coroutine_fn bdrv_qed_co_pwrite_zeroes(Blo= ckDriverState *bs, return cb.ret; } =20 -static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset) +static int bdrv_qed_truncate(BlockDriverState *bs, int64_t offset, Error *= *errp) { BDRVQEDState *s =3D bs->opaque; uint64_t old_image_size; diff --git a/block/raw-format.c b/block/raw-format.c index a80073369a..9761bdaff8 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -327,7 +327,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Er= ror **errp) } } =20 -static int raw_truncate(BlockDriverState *bs, int64_t offset) +static int raw_truncate(BlockDriverState *bs, int64_t offset, Error **errp) { BDRVRawState *s =3D bs->opaque; =20 @@ -341,7 +341,7 @@ static int raw_truncate(BlockDriverState *bs, int64_t o= ffset) =20 s->size =3D offset; offset +=3D s->offset; - return bdrv_truncate(bs->file, offset, NULL); + return bdrv_truncate(bs->file, offset, errp); } =20 static int raw_media_changed(BlockDriverState *bs) diff --git a/block/rbd.c b/block/rbd.c index ee13f3d9d3..f7d4dd93fe 100644 --- a/block/rbd.c +++ b/block/rbd.c @@ -1028,7 +1028,7 @@ static int64_t qemu_rbd_getlength(BlockDriverState *b= s) return info.size; } =20 -static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset) +static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset, Error *= *errp) { BDRVRBDState *s =3D bs->opaque; int r; diff --git a/block/sheepdog.c b/block/sheepdog.c index 89e98edab6..27126ea474 100644 --- a/block/sheepdog.c +++ b/block/sheepdog.c @@ -2133,9 +2133,8 @@ static int64_t sd_getlength(BlockDriverState *bs) return s->inode.vdi_size; } =20 -static int sd_truncate(BlockDriverState *bs, int64_t offset) +static int sd_truncate(BlockDriverState *bs, int64_t offset, Error **errp) { - Error *local_err =3D NULL; BDRVSheepdogState *s =3D bs->opaque; int ret, fd; unsigned int datalen; @@ -2143,16 +2142,15 @@ static int sd_truncate(BlockDriverState *bs, int64_= t offset) =20 max_vdi_size =3D (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA= _OBJS; if (offset < s->inode.vdi_size) { - error_report("shrinking is not supported"); + error_setg(errp, "shrinking is not supported"); return -EINVAL; } else if (offset > max_vdi_size) { - error_report("too big image size"); + error_setg(errp, "too big image size"); return -EINVAL; } =20 - fd =3D connect_to_sdog(s, &local_err); + fd =3D connect_to_sdog(s, errp); if (fd < 0) { - error_report_err(local_err); return fd; } =20 @@ -2165,7 +2163,7 @@ static int sd_truncate(BlockDriverState *bs, int64_t = offset) close(fd); =20 if (ret < 0) { - error_report("failed to update an inode."); + error_setg_errno(errp, -ret, "failed to update an inode"); } =20 return ret; @@ -2430,7 +2428,7 @@ static coroutine_fn int sd_co_writev(BlockDriverState= *bs, int64_t sector_num, BDRVSheepdogState *s =3D bs->opaque; =20 if (offset > s->inode.vdi_size) { - ret =3D sd_truncate(bs, offset); + ret =3D sd_truncate(bs, offset, NULL); if (ret < 0) { return ret; } --=20 2.12.0 From nobody Tue Apr 30 03:39:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1489000706411201.51873762051025; Wed, 8 Mar 2017 11:18:26 -0800 (PST) Received: from localhost ([::1]:58027 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1clh6N-00054I-PY for importer@patchew.org; Wed, 08 Mar 2017 14:18:23 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48701) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1clh42-0003yN-Sz for qemu-devel@nongnu.org; Wed, 08 Mar 2017 14:16:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1clh41-0008KS-9c for qemu-devel@nongnu.org; Wed, 08 Mar 2017 14:15:58 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41386) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1clh3x-0008I2-2C; Wed, 08 Mar 2017 14:15:53 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4DCAEC04D2B4; Wed, 8 Mar 2017 19:15:53 +0000 (UTC) Received: from localhost (ovpn-204-172.brq.redhat.com [10.40.204.172]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v28JFn6E006626 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 8 Mar 2017 14:15:51 -0500 From: Max Reitz To: qemu-block@nongnu.org Date: Wed, 8 Mar 2017 20:15:44 +0100 Message-Id: <20170308191544.3025-2-mreitz@redhat.com> In-Reply-To: <20170308191434.2413-1-mreitz@redhat.com> References: <20170308191434.2413-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 08 Mar 2017 19:15:53 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 for-2.10 4/4] block: Add some bdrv_truncate() error messages X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add missing error messages for the drivers I am comfortable to do this in. Since one of these changes touches a mis-indented block in block/file-posix.c, this patch fixes that coding style issue along the way. Signed-off-by: Max Reitz Reviewed-by: Kevin Wolf --- block/file-posix.c | 17 ++++++++++++----- block/qcow2.c | 2 ++ block/qed.c | 4 +++- block/raw-format.c | 2 ++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 9d2bea730d..013aa07437 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1341,20 +1341,27 @@ static int raw_truncate(BlockDriverState *bs, int64= _t offset, Error **errp) { BDRVRawState *s =3D bs->opaque; struct stat st; + int ret; =20 if (fstat(s->fd, &st)) { - return -errno; + ret =3D -errno; + error_setg_errno(errp, -ret, "Failed to fstat() the file"); + return ret; } =20 if (S_ISREG(st.st_mode)) { if (ftruncate(s->fd, offset) < 0) { - return -errno; + ret =3D -errno; + error_setg_errno(errp, -ret, "Failed to resize the file"); + return ret; } } else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) { - if (offset > raw_getlength(bs)) { - return -EINVAL; - } + if (offset > raw_getlength(bs)) { + error_setg(errp, "Cannot grow device files"); + return -EINVAL; + } } else { + error_setg(errp, "Resizing this file is not supported"); return -ENOTSUP; } =20 diff --git a/block/qcow2.c b/block/qcow2.c index 17585fbb89..53b0bd61a7 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2550,6 +2550,7 @@ static int qcow2_truncate(BlockDriverState *bs, int64= _t offset, Error **errp) new_l1_size =3D size_to_l1(s, offset); ret =3D qcow2_grow_l1_table(bs, new_l1_size, true); if (ret < 0) { + error_setg(errp, "Failed to grow the L1 table"); return ret; } =20 @@ -2558,6 +2559,7 @@ static int qcow2_truncate(BlockDriverState *bs, int64= _t offset, Error **errp) ret =3D bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, size), &offset, sizeof(uint64_t)); if (ret < 0) { + error_setg(errp, "Failed to update the image size"); return ret; } =20 diff --git a/block/qed.c b/block/qed.c index fa2aeee471..eb346d645b 100644 --- a/block/qed.c +++ b/block/qed.c @@ -1526,11 +1526,12 @@ static int bdrv_qed_truncate(BlockDriverState *bs, = int64_t offset, Error **errp) =20 if (!qed_is_image_size_valid(offset, s->header.cluster_size, s->header.table_size)) { + error_setg(errp, "Invalid image size specified"); return -EINVAL; } =20 - /* Shrinking is currently not supported */ if ((uint64_t)offset < s->header.image_size) { + error_setg(errp, "Shrinking images is currently not supported"); return -ENOTSUP; } =20 @@ -1539,6 +1540,7 @@ static int bdrv_qed_truncate(BlockDriverState *bs, in= t64_t offset, Error **errp) ret =3D qed_write_header_sync(s); if (ret < 0) { s->header.image_size =3D old_image_size; + error_setg(errp, "Failed to update the image size"); } return ret; } diff --git a/block/raw-format.c b/block/raw-format.c index 9761bdaff8..36e65036f0 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -332,10 +332,12 @@ static int raw_truncate(BlockDriverState *bs, int64_t= offset, Error **errp) BDRVRawState *s =3D bs->opaque; =20 if (s->has_size) { + error_setg(errp, "Cannot resize fixed-size raw disks"); return -ENOTSUP; } =20 if (INT64_MAX - offset < s->offset) { + error_setg(errp, "Disk size too large for the chosen offset"); return -EINVAL; } =20 --=20 2.12.0