From patchwork Sun Jul 9 21:07:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 107271 Delivered-To: patches@linaro.org Received: by 10.140.101.44 with SMTP id t41csp2641224qge; Sun, 9 Jul 2017 14:07:21 -0700 (PDT) X-Received: by 10.223.146.1 with SMTP id 1mr5970273wrj.152.1499634441688; Sun, 09 Jul 2017 14:07:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499634441; cv=none; d=google.com; s=arc-20160816; b=fdrQhb6xc15zB4fJ7yX1B5TfaFLDWdAB5GWpcXDI1cmnNFs72NhIxKI6VFM/G8zl3s KsHxBwFUTy/0xFLXa0V0JTQNd8XXc5QGZlxjUH9ncWbr8OYhL6cyLJaCqz2SsanKmzK9 TW+TraCNB8iYfJEIMgQ9EIjOZSLlSEzdFRg0hWGVNuLqmmBY+heMFNP1SIG+e8uEwUSk 3UQJTDb1P48bNBlGM2OA3tYdHifg5XnVLYLmGh5b5aN49q6RSa22dvYYuheKZMtoTTMu Z8kLIGd33BTX/87myVXUjPXRxNdP72pVw7lpskF5WWJJDtlPBELooyTuU3tryEcA9jHy QmUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:arc-authentication-results; bh=iEzW8Wp+8RcqynY9QZ07rk2gf/kRNCgXv9sUPcswplg=; b=xooQrePn2lI400msqO6rPdKZhmBTHgbBIG9nDaBCbLjuwHbYYMBBik/idQp9QSeDE1 gyNf+sWfHWVCmonCyQ9JnFc1R9R1vI2K4ECRvbaXoHSFRhOea8HmUuz9tjCORiWae1oB /XFwLPFErwowScUhYuOXcbahTWamBqUlo76/ZCYycdbIQ3vkxLgYGwcdpn8wBo7n1uc2 pvwwlUdXdVAJGXwD4qMSIf7kJhLXck1J7SWkb+oFKQgNFebG/uVdX3xColmJ1ahnFFwS N7XQJLOa+oNe8uyI/kx+h37p8FG346pYwqT82PA2zHCndNIJvGwpRfbZbpEc2CCtHJK3 QpZA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) smtp.mailfrom=pm215@archaic.org.uk; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by mx.google.com with ESMTPS id t18si4763966wmt.106.2017.07.09.14.07.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 09 Jul 2017 14:07:21 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) client-ip=2001:8b0:1d0::2; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) smtp.mailfrom=pm215@archaic.org.uk; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from mnementh.archaic.org.uk ([81.2.115.146]) by orth.archaic.org.uk with esmtp (Exim 4.84_2) (envelope-from ) id 1dUJQD-0000yM-VZ; Sun, 09 Jul 2017 22:07:17 +0100 Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1dUJQD-0000Gg-UJ; Sun, 09 Jul 2017 22:07:17 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, qemu-block@nongnu.org, Max Reitz , Kevin Wolf Subject: [PATCH] block/vpc.c: Handle write failures in get_image_offset() Date: Sun, 9 Jul 2017 22:07:17 +0100 Message-Id: <20170709210717.984-1-peter.maydell@linaro.org> X-Mailer: git-send-email 2.11.0 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 --- The new get_image_offset() API is pretty clunky, but I couldn't think of anything better -- we need to report one of 3 things: * offset number (0..INT64_MAX) * sector not allocated * arbitrary errno and they won't all fit into one return value. I opted for "minimal change compared to current code". --- block/vpc.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) -- 2.11.0 diff --git a/block/vpc.c b/block/vpc.c index 4240ba9d1c..b93211df36 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 offset, - bool write) + bool write, int *err) { BDRVVPCState *s = bs->opaque; uint64_t bitmap_offset, block_offset; uint32_t pagetable_index, offset_in_block; + assert(!(write && err == NULL)); + pagetable_index = offset / s->block_size; offset_in_block = offset % s->block_size; @@ -487,19 +493,25 @@ static inline int64_t get_image_offset(BlockDriverState *bs, uint64_t offset, correctness. */ if (write && (s->last_bitmap_offset != bitmap_offset)) { uint8_t bitmap[s->bitmap_size]; + int r; s->last_bitmap_offset = bitmap_offset; memset(bitmap, 0xff, s->bitmap_size); - bdrv_pwrite_sync(bs->file, bitmap_offset, bitmap, s->bitmap_size); + r = bdrv_pwrite_sync(bs->file, bitmap_offset, bitmap, s->bitmap_size); + if (r < 0) { + *err = r; + return -2; + } } return block_offset; } static inline int64_t get_sector_offset(BlockDriverState *bs, - int64_t sector_num, bool write) + int64_t sector_num, bool write, + int *err) { - return get_image_offset(bs, sector_num * BDRV_SECTOR_SIZE, write); + return get_image_offset(bs, sector_num * BDRV_SECTOR_SIZE, write, err); } /* @@ -567,7 +579,7 @@ static int64_t alloc_block(BlockDriverState* bs, int64_t offset) if (ret < 0) goto fail; - return get_image_offset(bs, offset, false); + return get_image_offset(bs, offset, false, NULL); fail: s->free_data_block_offset -= (s->block_size + s->bitmap_size); @@ -607,7 +619,7 @@ vpc_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, qemu_iovec_init(&local_qiov, qiov->niov); while (bytes > 0) { - image_offset = get_image_offset(bs, offset, false); + image_offset = get_image_offset(bs, offset, false, NULL); n_bytes = MIN(bytes, s->block_size - (offset % s->block_size)); if (image_offset == -1) { @@ -656,7 +668,11 @@ vpc_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, qemu_iovec_init(&local_qiov, qiov->niov); while (bytes > 0) { - image_offset = get_image_offset(bs, offset, true); + image_offset = get_image_offset(bs, offset, true, &ret); + if (image_offset == -2) { + /* Failed to write block bitmap: can't proceed with write */ + goto fail; + } n_bytes = MIN(bytes, s->block_size - (offset % s->block_size)); if (image_offset == -1) { @@ -705,7 +721,7 @@ static int64_t coroutine_fn vpc_co_get_block_status(BlockDriverState *bs, (sector_num << BDRV_SECTOR_BITS); } - offset = get_sector_offset(bs, sector_num, 0); + offset = get_sector_offset(bs, sector_num, false, NULL); start = offset; allocated = (offset != -1); *pnum = 0; @@ -728,7 +744,7 @@ static int64_t coroutine_fn vpc_co_get_block_status(BlockDriverState *bs, if (nb_sectors == 0) { break; } - offset = get_sector_offset(bs, sector_num, 0); + offset = get_sector_offset(bs, sector_num, false, NULL); } while (offset == -1); return 0;