All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 05/21] block/vpc.c: Handle write failures in get_image_offset()
Date: Tue, 18 Jul 2017 16:17:50 +0200	[thread overview]
Message-ID: <1500387486-5469-6-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1500387486-5469-1-git-send-email-kwolf@redhat.com>

From: Peter Maydell <peter.maydell@linaro.org>

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 <peter.maydell@linaro.org>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 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 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,10 +493,15 @@ 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;
@@ -561,7 +572,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);
@@ -601,7 +612,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) {
@@ -650,7 +661,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) {
@@ -702,7 +717,7 @@ static int64_t coroutine_fn vpc_co_get_block_status(BlockDriverState *bs,
 
     qemu_co_mutex_lock(&s->lock);
 
-    offset = get_image_offset(bs, sector_num << BDRV_SECTOR_BITS, false);
+    offset = get_image_offset(bs, sector_num << BDRV_SECTOR_BITS, false, NULL);
     start = offset;
     allocated = (offset != -1);
     *pnum = 0;
@@ -727,7 +742,8 @@ static int64_t coroutine_fn vpc_co_get_block_status(BlockDriverState *bs,
         if (nb_sectors == 0) {
             break;
         }
-        offset = get_image_offset(bs, sector_num << BDRV_SECTOR_BITS, false);
+        offset = get_image_offset(bs, sector_num << BDRV_SECTOR_BITS, false,
+                                  NULL);
     } while (offset == -1);
 
     qemu_co_mutex_unlock(&s->lock);
-- 
1.8.3.1

  parent reply	other threads:[~2017-07-18 14:18 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-18 14:17 [Qemu-devel] [PULL 00/21] Block layer patches Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 01/21] commit: Add NULL check for overlay_bs Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 02/21] block: add clock_type field to ThrottleGroup Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 03/21] block: remove timer canceling in throttle_config() Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 04/21] block/vmdk: Report failures in vmdk_read_cid() Kevin Wolf
2017-07-18 14:17 ` Kevin Wolf [this message]
2017-07-18 14:17 ` [Qemu-devel] [PULL 06/21] block: Make blk_get_attached_dev_id() public Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 07/21] block/qapi: Add qdev device name to query-block Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 08/21] block: Make blk_all_next() public Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 09/21] block/qapi: Use blk_all_next() for query-block Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 10/21] block: List anonymous device BBs in query-block Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 11/21] ide: bdrv_attach_dev() for empty CD-ROM Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 12/21] scsi-disk: " Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 13/21] qemu-iotests: Test 'info block' Kevin Wolf
2017-07-18 14:17 ` [Qemu-devel] [PULL 14/21] qemu-iotests: Test unplug of -device without drive Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 15/21] vvfat: add constants for special values of name[0] Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 16/21] vvfat: add a constant for bootsector name Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 17/21] vvfat: correctly parse non-ASCII short and long file names Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 18/21] vvfat: initialize memory after allocating it Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 19/21] block/vvfat: Fix compiler warning with gcc 7 Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 20/21] blockdev: move BDRV_O_NO_BACKING option forward Kevin Wolf
2017-07-18 14:18 ` [Qemu-devel] [PULL 21/21] qemu-img: Check for backing image if specified during create Kevin Wolf
2017-07-18 18:57 ` [Qemu-devel] [PULL 00/21] Block layer patches no-reply
2017-07-19  6:11   ` Kevin Wolf
2017-07-18 21:23 ` no-reply
2017-07-19 11:28 ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1500387486-5469-6-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.