From nobody Wed Dec 17 05:32:24 2025 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.zohomail.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 1500387786919371.04078928663944; Tue, 18 Jul 2017 07:23:06 -0700 (PDT) Received: from localhost ([::1]:56804 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dXTOx-0004Q7-Ot for importer@patchew.org; Tue, 18 Jul 2017 10:23:03 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53057) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dXTKV-0000Rn-RG for qemu-devel@nongnu.org; Tue, 18 Jul 2017 10:18:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dXTKS-0007br-P4 for qemu-devel@nongnu.org; Tue, 18 Jul 2017 10:18:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40774) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dXTKP-0007YI-NZ; Tue, 18 Jul 2017 10:18:21 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6531F2F86C4; Tue, 18 Jul 2017 14:18:20 +0000 (UTC) Received: from noname.str.redhat.com (dhcp-192-175.str.redhat.com [10.33.192.175]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2EFE97EF7F; Tue, 18 Jul 2017 14:18:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6531F2F86C4 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=kwolf@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6531F2F86C4 From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 18 Jul 2017 16:17:50 +0200 Message-Id: <1500387486-5469-6-git-send-email-kwolf@redhat.com> In-Reply-To: <1500387486-5469-1-git-send-email-kwolf@redhat.com> References: <1500387486-5469-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Tue, 18 Jul 2017 14:18:20 +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] [PULL 05/21] block/vpc.c: Handle write failures in get_image_offset() 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: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org 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" From: Peter Maydell Coverity (CID 1355236) points out that get_image_offset() doesn't check that it actually succeeded in writing the updated block bitmap to the file. Check the error return from bdrv_pwrite_sync() and propagate an error response back up to the function which calls get_image_offset() for a write so that it can return the error to its caller. get_sector_offset() is only used for reads, but we move it to the same API for consistency. Signed-off-by: Peter Maydell Signed-off-by: Kevin Wolf --- block/vpc.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/block/vpc.c b/block/vpc.c index 8057d42..10e6519 100644 --- a/block/vpc.c +++ b/block/vpc.c @@ -460,17 +460,23 @@ static int vpc_reopen_prepare(BDRVReopenState *state, /* * Returns the absolute byte offset of the given sector in the image file. * If the sector is not allocated, -1 is returned instead. + * If an error occurred trying to write an updated block bitmap back to + * the file, -2 is returned, and the error value is written to *err. + * This can only happen for a write operation. * * The parameter write must be 1 if the offset will be used for a write * operation (the block bitmaps is updated then), 0 otherwise. + * If write is true then err must not be NULL. */ static inline int64_t get_image_offset(BlockDriverState *bs, uint64_t offs= et, - bool write) + bool write, int *err) { BDRVVPCState *s =3D bs->opaque; uint64_t bitmap_offset, block_offset; uint32_t pagetable_index, offset_in_block; =20 + assert(!(write && err =3D=3D NULL)); + pagetable_index =3D offset / s->block_size; offset_in_block =3D offset % s->block_size; =20 @@ -487,10 +493,15 @@ static inline int64_t get_image_offset(BlockDriverSta= te *bs, uint64_t offset, correctness. */ if (write && (s->last_bitmap_offset !=3D bitmap_offset)) { uint8_t bitmap[s->bitmap_size]; + int r; =20 s->last_bitmap_offset =3D bitmap_offset; memset(bitmap, 0xff, s->bitmap_size); - bdrv_pwrite_sync(bs->file, bitmap_offset, bitmap, s->bitmap_size); + r =3D bdrv_pwrite_sync(bs->file, bitmap_offset, bitmap, s->bitmap_= size); + if (r < 0) { + *err =3D r; + return -2; + } } =20 return block_offset; @@ -561,7 +572,7 @@ static int64_t alloc_block(BlockDriverState* bs, int64_= t offset) if (ret < 0) goto fail; =20 - return get_image_offset(bs, offset, false); + return get_image_offset(bs, offset, false, NULL); =20 fail: s->free_data_block_offset -=3D (s->block_size + s->bitmap_size); @@ -601,7 +612,7 @@ vpc_co_preadv(BlockDriverState *bs, uint64_t offset, ui= nt64_t bytes, qemu_iovec_init(&local_qiov, qiov->niov); =20 while (bytes > 0) { - image_offset =3D get_image_offset(bs, offset, false); + image_offset =3D get_image_offset(bs, offset, false, NULL); n_bytes =3D MIN(bytes, s->block_size - (offset % s->block_size)); =20 if (image_offset =3D=3D -1) { @@ -650,7 +661,11 @@ vpc_co_pwritev(BlockDriverState *bs, uint64_t offset, = uint64_t bytes, qemu_iovec_init(&local_qiov, qiov->niov); =20 while (bytes > 0) { - image_offset =3D get_image_offset(bs, offset, true); + image_offset =3D get_image_offset(bs, offset, true, &ret); + if (image_offset =3D=3D -2) { + /* Failed to write block bitmap: can't proceed with write */ + goto fail; + } n_bytes =3D MIN(bytes, s->block_size - (offset % s->block_size)); =20 if (image_offset =3D=3D -1) { @@ -702,7 +717,7 @@ static int64_t coroutine_fn vpc_co_get_block_status(Blo= ckDriverState *bs, =20 qemu_co_mutex_lock(&s->lock); =20 - offset =3D get_image_offset(bs, sector_num << BDRV_SECTOR_BITS, false); + offset =3D get_image_offset(bs, sector_num << BDRV_SECTOR_BITS, false,= NULL); start =3D offset; allocated =3D (offset !=3D -1); *pnum =3D 0; @@ -727,7 +742,8 @@ static int64_t coroutine_fn vpc_co_get_block_status(Blo= ckDriverState *bs, if (nb_sectors =3D=3D 0) { break; } - offset =3D get_image_offset(bs, sector_num << BDRV_SECTOR_BITS, fa= lse); + offset =3D get_image_offset(bs, sector_num << BDRV_SECTOR_BITS, fa= lse, + NULL); } while (offset =3D=3D -1); =20 qemu_co_mutex_unlock(&s->lock); --=20 1.8.3.1