get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

GET /api/patches/806830/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 806830,
    "url": "http://patchwork.ozlabs.org/api/patches/806830/?format=api",
    "web_url": "http://patchwork.ozlabs.org/project/qemu-devel/patch/1503965694-10794-25-git-send-email-mdroth@linux.vnet.ibm.com/",
    "project": {
        "id": 14,
        "url": "http://patchwork.ozlabs.org/api/projects/14/?format=api",
        "name": "QEMU Development",
        "link_name": "qemu-devel",
        "list_id": "qemu-devel.nongnu.org",
        "list_email": "qemu-devel@nongnu.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": "",
        "list_archive_url": "",
        "list_archive_url_format": "",
        "commit_url_format": ""
    },
    "msgid": "<1503965694-10794-25-git-send-email-mdroth@linux.vnet.ibm.com>",
    "list_archive_url": null,
    "date": "2017-08-29T00:13:59",
    "name": "[24/79] block: Add errp to b{lk, drv}_truncate()",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "8b7c7e8c11329525e46c3979441760fff2c23915",
    "submitter": {
        "id": 5549,
        "url": "http://patchwork.ozlabs.org/api/people/5549/?format=api",
        "name": "Michael Roth",
        "email": "mdroth@linux.vnet.ibm.com"
    },
    "delegate": null,
    "mbox": "http://patchwork.ozlabs.org/project/qemu-devel/patch/1503965694-10794-25-git-send-email-mdroth@linux.vnet.ibm.com/mbox/",
    "series": [
        {
            "id": 281,
            "url": "http://patchwork.ozlabs.org/api/series/281/?format=api",
            "web_url": "http://patchwork.ozlabs.org/project/qemu-devel/list/?series=281",
            "date": "2017-08-29T00:13:45",
            "name": "Patch Round-up for stable 2.9.1, freeze on 2017-09-04",
            "version": 1,
            "mbox": "http://patchwork.ozlabs.org/series/281/mbox/"
        }
    ],
    "comments": "http://patchwork.ozlabs.org/api/patches/806830/comments/",
    "check": "pending",
    "checks": "http://patchwork.ozlabs.org/api/patches/806830/checks/",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>",
        "X-Original-To": "incoming@patchwork.ozlabs.org",
        "Delivered-To": "patchwork-incoming@bilbo.ozlabs.org",
        "Authentication-Results": "ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)",
        "Received": [
            "from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xh8jv1Hwkz9sNr\n\tfor <incoming@patchwork.ozlabs.org>;\n\tTue, 29 Aug 2017 10:32:51 +1000 (AEST)",
            "from localhost ([::1]:41972 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dmUSX-0007hc-1u\n\tfor incoming@patchwork.ozlabs.org; Mon, 28 Aug 2017 20:32:49 -0400",
            "from eggs.gnu.org ([2001:4830:134:3::10]:47651)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <mdroth@linux.vnet.ibm.com>) id 1dmUCG-0000zo-6S\n\tfor qemu-devel@nongnu.org; Mon, 28 Aug 2017 20:16:04 -0400",
            "from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <mdroth@linux.vnet.ibm.com>) id 1dmUCC-0005Dj-PF\n\tfor qemu-devel@nongnu.org; Mon, 28 Aug 2017 20:16:00 -0400",
            "from mx0a-001b2d01.pphosted.com ([148.163.156.1]:37928)\n\tby eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <mdroth@linux.vnet.ibm.com>)\n\tid 1dmUCC-0005CX-Ak\n\tfor qemu-devel@nongnu.org; Mon, 28 Aug 2017 20:15:56 -0400",
            "from pps.filterd (m0098409.ppops.net [127.0.0.1])\n\tby mx0a-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id\n\tv7T0EA1N090193\n\tfor <qemu-devel@nongnu.org>; Mon, 28 Aug 2017 20:15:55 -0400",
            "from e38.co.us.ibm.com (e38.co.us.ibm.com [32.97.110.159])\n\tby mx0a-001b2d01.pphosted.com with ESMTP id 2cmvyehk6t-1\n\t(version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT)\n\tfor <qemu-devel@nongnu.org>; Mon, 28 Aug 2017 20:15:55 -0400",
            "from localhost\n\tby e38.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use\n\tOnly! Violators will be prosecuted\n\tfor <qemu-devel@nongnu.org> from <mdroth@linux.vnet.ibm.com>;\n\tMon, 28 Aug 2017 18:15:54 -0600",
            "from b03cxnp08028.gho.boulder.ibm.com (9.17.130.20)\n\tby e38.co.us.ibm.com (192.168.1.138) with IBM ESMTP SMTP Gateway:\n\tAuthorized Use Only! Violators will be prosecuted; \n\tMon, 28 Aug 2017 18:15:53 -0600",
            "from b03ledav001.gho.boulder.ibm.com\n\t(b03ledav001.gho.boulder.ibm.com [9.17.130.232])\n\tby b03cxnp08028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with\n\tESMTP id v7T0Fqfi32178208; Mon, 28 Aug 2017 17:15:52 -0700",
            "from b03ledav001.gho.boulder.ibm.com (unknown [127.0.0.1])\n\tby IMSVA (Postfix) with ESMTP id BA8246E040;\n\tMon, 28 Aug 2017 18:15:52 -0600 (MDT)",
            "from localhost (unknown [9.80.85.217])\n\tby b03ledav001.gho.boulder.ibm.com (Postfix) with ESMTP id 794B96E041;\n\tMon, 28 Aug 2017 18:15:52 -0600 (MDT)"
        ],
        "From": "Michael Roth <mdroth@linux.vnet.ibm.com>",
        "To": "qemu-devel@nongnu.org",
        "Date": "Mon, 28 Aug 2017 19:13:59 -0500",
        "X-Mailer": "git-send-email 2.7.4",
        "In-Reply-To": "<1503965694-10794-1-git-send-email-mdroth@linux.vnet.ibm.com>",
        "References": "<1503965694-10794-1-git-send-email-mdroth@linux.vnet.ibm.com>",
        "X-TM-AS-GCONF": "00",
        "x-cbid": "17082900-0028-0000-0000-00000846DB25",
        "X-IBM-SpamModules-Scores": "",
        "X-IBM-SpamModules-Versions": "BY=3.00007630; HX=3.00000241; KW=3.00000007;\n\tPH=3.00000004; SC=3.00000226; SDB=6.00909028; UDB=6.00455849;\n\tIPR=6.00689279; \n\tBA=6.00005557; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009;\n\tZB=6.00000000; \n\tZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00016909;\n\tXFM=3.00000015; UTC=2017-08-29 00:15:54",
        "X-IBM-AV-DETECTION": "SAVI=unused REMOTE=unused XFE=unused",
        "x-cbparentid": "17082900-0029-0000-0000-000037553118",
        "Message-Id": "<1503965694-10794-25-git-send-email-mdroth@linux.vnet.ibm.com>",
        "X-Proofpoint-Virus-Version": "vendor=fsecure engine=2.50.10432:, ,\n\tdefinitions=2017-08-28_13:, , signatures=0",
        "X-Proofpoint-Spam-Details": "rule=outbound_notspam policy=outbound score=0\n\tspamscore=0 suspectscore=3\n\tmalwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam\n\tadjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000\n\tdefinitions=main-1708290001",
        "X-detected-operating-system": "by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy]",
        "X-Received-From": "148.163.156.1",
        "Subject": "[Qemu-devel] [PATCH 24/79] block: Add errp to b{lk, drv}_truncate()",
        "X-BeenThere": "qemu-devel@nongnu.org",
        "X-Mailman-Version": "2.1.21",
        "Precedence": "list",
        "List-Id": "<qemu-devel.nongnu.org>",
        "List-Unsubscribe": "<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>",
        "List-Archive": "<http://lists.nongnu.org/archive/html/qemu-devel/>",
        "List-Post": "<mailto:qemu-devel@nongnu.org>",
        "List-Help": "<mailto:qemu-devel-request@nongnu.org?subject=help>",
        "List-Subscribe": "<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>",
        "Cc": "qemu-stable@nongnu.org, Max Reitz <mreitz@redhat.com>",
        "Errors-To": "qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org",
        "Sender": "\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"
    },
    "content": "From: Max Reitz <mreitz@redhat.com>\n\nFor one thing, this allows us to drop the error message generation from\nqemu-img.c and blockdev.c and instead have it unified in\nbdrv_truncate().\n\nSigned-off-by: Max Reitz <mreitz@redhat.com>\nMessage-id: 20170328205129.15138-3-mreitz@redhat.com\nReviewed-by: Stefan Hajnoczi <stefanha@redhat.com>\nSigned-off-by: Max Reitz <mreitz@redhat.com>\n(cherry picked from commit ed3d2ec98a33fbdeabc471b11ff807075f07e996)\n* prereq for 698bdfa\nSigned-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>\n---\n block.c                        | 16 ++++++++++++----\n block/blkdebug.c               |  2 +-\n block/block-backend.c          |  5 +++--\n block/commit.c                 |  5 +++--\n block/crypto.c                 |  2 +-\n block/mirror.c                 |  2 +-\n block/parallels.c              | 13 ++++++++-----\n block/qcow.c                   |  6 +++---\n block/qcow2-refcount.c         |  5 ++++-\n block/qcow2.c                  | 14 +++++++++-----\n block/qed.c                    |  2 +-\n block/raw-format.c             |  2 +-\n block/vdi.c                    |  4 ++--\n block/vhdx-log.c               |  2 +-\n block/vhdx.c                   |  6 +++---\n block/vmdk.c                   | 13 +++----------\n block/vpc.c                    | 13 +++++++------\n blockdev.c                     | 21 +--------------------\n include/block/block.h          |  2 +-\n include/sysemu/block-backend.h |  2 +-\n qemu-img.c                     | 17 ++++-------------\n qemu-io-cmds.c                 |  5 +++--\n 22 files changed, 73 insertions(+), 86 deletions(-)",
    "diff": "diff --git a/block.c b/block.c\nindex 513e6e5..21cb65e 100644\n--- a/block.c\n+++ b/block.c\n@@ -3262,7 +3262,7 @@ exit:\n /**\n  * Truncate file to 'offset' bytes (needed only for file protocols)\n  */\n-int bdrv_truncate(BdrvChild *child, int64_t offset)\n+int bdrv_truncate(BdrvChild *child, int64_t offset, Error **errp)\n {\n     BlockDriverState *bs = child->bs;\n     BlockDriver *drv = bs->drv;\n@@ -3274,12 +3274,18 @@ int bdrv_truncate(BdrvChild *child, int64_t offset)\n      *        cannot assert this permission in that case. */\n     // assert(child->perm & BLK_PERM_RESIZE);\n \n-    if (!drv)\n+    if (!drv) {\n+        error_setg(errp, \"No medium inserted\");\n         return -ENOMEDIUM;\n-    if (!drv->bdrv_truncate)\n+    }\n+    if (!drv->bdrv_truncate) {\n+        error_setg(errp, \"Image format driver does not support resize\");\n         return -ENOTSUP;\n-    if (bs->read_only)\n+    }\n+    if (bs->read_only) {\n+        error_setg(errp, \"Image is read-only\");\n         return -EACCES;\n+    }\n \n     ret = drv->bdrv_truncate(bs, offset);\n     if (ret == 0) {\n@@ -3287,6 +3293,8 @@ int bdrv_truncate(BdrvChild *child, int64_t offset)\n         bdrv_dirty_bitmap_truncate(bs);\n         bdrv_parent_cb_resize(bs);\n         ++bs->write_gen;\n+    } else {\n+        error_setg_errno(errp, -ret, \"Failed to resize image\");\n     }\n     return ret;\n }\ndiff --git a/block/blkdebug.c b/block/blkdebug.c\nindex ffc6f1d..46bd08a 100644\n--- a/block/blkdebug.c\n+++ b/block/blkdebug.c\n@@ -661,7 +661,7 @@ static int64_t blkdebug_getlength(BlockDriverState *bs)\n \n static int blkdebug_truncate(BlockDriverState *bs, int64_t offset)\n {\n-    return bdrv_truncate(bs->file, offset);\n+    return bdrv_truncate(bs->file, offset, NULL);\n }\n \n static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)\ndiff --git a/block/block-backend.c b/block/block-backend.c\nindex 7405024..1e70c4d 100644\n--- a/block/block-backend.c\n+++ b/block/block-backend.c\n@@ -1746,13 +1746,14 @@ int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,\n                    BDRV_REQ_WRITE_COMPRESSED);\n }\n \n-int blk_truncate(BlockBackend *blk, int64_t offset)\n+int blk_truncate(BlockBackend *blk, int64_t offset, Error **errp)\n {\n     if (!blk_is_available(blk)) {\n+        error_setg(errp, \"No medium inserted\");\n         return -ENOMEDIUM;\n     }\n \n-    return bdrv_truncate(blk->root, offset);\n+    return bdrv_truncate(blk->root, offset, errp);\n }\n \n static void blk_pdiscard_entry(void *opaque)\ndiff --git a/block/commit.c b/block/commit.c\nindex 91d2c34..76a0d98 100644\n--- a/block/commit.c\n+++ b/block/commit.c\n@@ -151,7 +151,7 @@ static void coroutine_fn commit_run(void *opaque)\n     }\n \n     if (base_len < s->common.len) {\n-        ret = blk_truncate(s->base, s->common.len);\n+        ret = blk_truncate(s->base, s->common.len, NULL);\n         if (ret) {\n             goto out;\n         }\n@@ -511,8 +511,9 @@ int bdrv_commit(BlockDriverState *bs)\n      * grow the backing file image if possible.  If not possible,\n      * we must return an error */\n     if (length > backing_length) {\n-        ret = blk_truncate(backing, length);\n+        ret = blk_truncate(backing, length, &local_err);\n         if (ret < 0) {\n+            error_report_err(local_err);\n             goto ro_cleanup;\n         }\n     }\ndiff --git a/block/crypto.c b/block/crypto.c\nindex 4a20388..52e4f2b 100644\n--- a/block/crypto.c\n+++ b/block/crypto.c\n@@ -389,7 +389,7 @@ static int block_crypto_truncate(BlockDriverState *bs, int64_t offset)\n \n     offset += payload_offset;\n \n-    return bdrv_truncate(bs->file, offset);\n+    return bdrv_truncate(bs->file, offset, NULL);\n }\n \n static void block_crypto_close(BlockDriverState *bs)\ndiff --git a/block/mirror.c b/block/mirror.c\nindex 164438f..2173a2f 100644\n--- a/block/mirror.c\n+++ b/block/mirror.c\n@@ -724,7 +724,7 @@ static void coroutine_fn mirror_run(void *opaque)\n         }\n \n         if (s->bdev_length > base_length) {\n-            ret = blk_truncate(s->target, s->bdev_length);\n+            ret = blk_truncate(s->target, s->bdev_length, NULL);\n             if (ret < 0) {\n                 goto immediate_exit;\n             }\ndiff --git a/block/parallels.c b/block/parallels.c\nindex 90acf79..8be46a7 100644\n--- a/block/parallels.c\n+++ b/block/parallels.c\n@@ -223,7 +223,8 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,\n                                      space << BDRV_SECTOR_BITS, 0);\n         } else {\n             ret = bdrv_truncate(bs->file,\n-                                (s->data_end + space) << BDRV_SECTOR_BITS);\n+                                (s->data_end + space) << BDRV_SECTOR_BITS,\n+                                NULL);\n         }\n         if (ret < 0) {\n             return ret;\n@@ -456,8 +457,10 @@ static int parallels_check(BlockDriverState *bs, BdrvCheckResult *res,\n                 size - res->image_end_offset);\n         res->leaks += count;\n         if (fix & BDRV_FIX_LEAKS) {\n-            ret = bdrv_truncate(bs->file, res->image_end_offset);\n+            Error *local_err = NULL;\n+            ret = bdrv_truncate(bs->file, res->image_end_offset, &local_err);\n             if (ret < 0) {\n+                error_report_err(local_err);\n                 res->check_errors++;\n                 return ret;\n             }\n@@ -504,7 +507,7 @@ static int parallels_create(const char *filename, QemuOpts *opts, Error **errp)\n \n     blk_set_allow_write_beyond_eof(file, true);\n \n-    ret = blk_truncate(file, 0);\n+    ret = blk_truncate(file, 0, errp);\n     if (ret < 0) {\n         goto exit;\n     }\n@@ -696,7 +699,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,\n     }\n \n     if (!(flags & BDRV_O_RESIZE) || !bdrv_has_zero_init(bs->file->bs) ||\n-            bdrv_truncate(bs->file, bdrv_getlength(bs->file->bs)) != 0) {\n+            bdrv_truncate(bs->file, bdrv_getlength(bs->file->bs), NULL) != 0) {\n         s->prealloc_mode = PRL_PREALLOC_MODE_FALLOCATE;\n     }\n \n@@ -739,7 +742,7 @@ static void parallels_close(BlockDriverState *bs)\n     }\n \n     if (bs->open_flags & BDRV_O_RDWR) {\n-        bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS);\n+        bdrv_truncate(bs->file, s->data_end << BDRV_SECTOR_BITS, NULL);\n     }\n \n     g_free(s->bat_dirty_bmap);\ndiff --git a/block/qcow.c b/block/qcow.c\nindex 9d6ac83..5d147b9 100644\n--- a/block/qcow.c\n+++ b/block/qcow.c\n@@ -473,7 +473,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,\n                 /* round to cluster size */\n                 cluster_offset = (cluster_offset + s->cluster_size - 1) &\n                     ~(s->cluster_size - 1);\n-                bdrv_truncate(bs->file, cluster_offset + s->cluster_size);\n+                bdrv_truncate(bs->file, cluster_offset + s->cluster_size, NULL);\n                 /* if encrypted, we must initialize the cluster\n                    content which won't be written */\n                 if (bs->encrypted &&\n@@ -833,7 +833,7 @@ static int qcow_create(const char *filename, QemuOpts *opts, Error **errp)\n \n     blk_set_allow_write_beyond_eof(qcow_blk, true);\n \n-    ret = blk_truncate(qcow_blk, 0);\n+    ret = blk_truncate(qcow_blk, 0, errp);\n     if (ret < 0) {\n         goto exit;\n     }\n@@ -916,7 +916,7 @@ static int qcow_make_empty(BlockDriverState *bs)\n     if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, s->l1_table,\n             l1_length) < 0)\n         return -1;\n-    ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length);\n+    ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, NULL);\n     if (ret < 0)\n         return ret;\n \ndiff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c\nindex 9e96f64..4efca7e 100644\n--- a/block/qcow2-refcount.c\n+++ b/block/qcow2-refcount.c\n@@ -1728,14 +1728,17 @@ static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,\n \n             if (fix & BDRV_FIX_ERRORS) {\n                 int64_t new_nb_clusters;\n+                Error *local_err = NULL;\n \n                 if (offset > INT64_MAX - s->cluster_size) {\n                     ret = -EINVAL;\n                     goto resize_fail;\n                 }\n \n-                ret = bdrv_truncate(bs->file, offset + s->cluster_size);\n+                ret = bdrv_truncate(bs->file, offset + s->cluster_size,\n+                                    &local_err);\n                 if (ret < 0) {\n+                    error_report_err(local_err);\n                     goto resize_fail;\n                 }\n                 size = bdrv_getlength(bs->file->bs);\ndiff --git a/block/qcow2.c b/block/qcow2.c\nindex 49c737f..0d7e9e4 100644\n--- a/block/qcow2.c\n+++ b/block/qcow2.c\n@@ -2294,9 +2294,9 @@ static int qcow2_create2(const char *filename, int64_t total_size,\n     }\n \n     /* Okay, now that we have a valid image, let's give it the right size */\n-    ret = blk_truncate(blk, total_size);\n+    ret = blk_truncate(blk, total_size, errp);\n     if (ret < 0) {\n-        error_setg_errno(errp, -ret, \"Could not resize image\");\n+        error_prepend(errp, \"Could not resize image: \");\n         goto out;\n     }\n \n@@ -2584,7 +2584,7 @@ qcow2_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset,\n         /* align end of file to a sector boundary to ease reading with\n            sector based I/Os */\n         cluster_offset = bdrv_getlength(bs->file->bs);\n-        return bdrv_truncate(bs->file, cluster_offset);\n+        return bdrv_truncate(bs->file, cluster_offset, NULL);\n     }\n \n     buf = qemu_blockalign(bs, s->cluster_size);\n@@ -2674,6 +2674,7 @@ fail:\n static int make_completely_empty(BlockDriverState *bs)\n {\n     BDRVQcow2State *s = bs->opaque;\n+    Error *local_err = NULL;\n     int ret, l1_clusters;\n     int64_t offset;\n     uint64_t *new_reftable = NULL;\n@@ -2798,8 +2799,10 @@ static int make_completely_empty(BlockDriverState *bs)\n         goto fail;\n     }\n \n-    ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size);\n+    ret = bdrv_truncate(bs->file, (3 + l1_clusters) * s->cluster_size,\n+                        &local_err);\n     if (ret < 0) {\n+        error_report_err(local_err);\n         goto fail;\n     }\n \n@@ -3273,9 +3276,10 @@ static int qcow2_amend_options(BlockDriverState *bs, QemuOpts *opts,\n             return ret;\n         }\n \n-        ret = blk_truncate(blk, new_size);\n+        ret = blk_truncate(blk, new_size, &local_err);\n         blk_unref(blk);\n         if (ret < 0) {\n+            error_report_err(local_err);\n             return ret;\n         }\n     }\ndiff --git a/block/qed.c b/block/qed.c\nindex 5ec7fd8..53199ac 100644\n--- a/block/qed.c\n+++ b/block/qed.c\n@@ -635,7 +635,7 @@ static int qed_create(const char *filename, uint32_t cluster_size,\n     blk_set_allow_write_beyond_eof(blk, true);\n \n     /* File must start empty and grow, check truncate is supported */\n-    ret = blk_truncate(blk, 0);\n+    ret = blk_truncate(blk, 0, errp);\n     if (ret < 0) {\n         goto out;\n     }\ndiff --git a/block/raw-format.c b/block/raw-format.c\nindex 86fbc65..a800733 100644\n--- a/block/raw-format.c\n+++ b/block/raw-format.c\n@@ -341,7 +341,7 @@ static int raw_truncate(BlockDriverState *bs, int64_t offset)\n \n     s->size = offset;\n     offset += s->offset;\n-    return bdrv_truncate(bs->file, offset);\n+    return bdrv_truncate(bs->file, offset, NULL);\n }\n \n static int raw_media_changed(BlockDriverState *bs)\ndiff --git a/block/vdi.c b/block/vdi.c\nindex 9b4f70e..d12d9cd 100644\n--- a/block/vdi.c\n+++ b/block/vdi.c\n@@ -832,9 +832,9 @@ static int vdi_create(const char *filename, QemuOpts *opts, Error **errp)\n     }\n \n     if (image_type == VDI_TYPE_STATIC) {\n-        ret = blk_truncate(blk, offset + blocks * block_size);\n+        ret = blk_truncate(blk, offset + blocks * block_size, errp);\n         if (ret < 0) {\n-            error_setg(errp, \"Failed to statically allocate %s\", filename);\n+            error_prepend(errp, \"Failed to statically allocate %s\", filename);\n             goto exit;\n         }\n     }\ndiff --git a/block/vhdx-log.c b/block/vhdx-log.c\nindex 67a91c0..3f4c2aa 100644\n--- a/block/vhdx-log.c\n+++ b/block/vhdx-log.c\n@@ -548,7 +548,7 @@ static int vhdx_log_flush(BlockDriverState *bs, BDRVVHDXState *s,\n             if (new_file_size % (1024*1024)) {\n                 /* round up to nearest 1MB boundary */\n                 new_file_size = ((new_file_size >> 20) + 1) << 20;\n-                bdrv_truncate(bs->file, new_file_size);\n+                bdrv_truncate(bs->file, new_file_size, NULL);\n             }\n         }\n         qemu_vfree(desc_entries);\ndiff --git a/block/vhdx.c b/block/vhdx.c\nindex d25bcd9..0a38f77 100644\n--- a/block/vhdx.c\n+++ b/block/vhdx.c\n@@ -1171,7 +1171,7 @@ static int vhdx_allocate_block(BlockDriverState *bs, BDRVVHDXState *s,\n     /* per the spec, the address for a block is in units of 1MB */\n     *new_offset = ROUND_UP(*new_offset, 1024 * 1024);\n \n-    return bdrv_truncate(bs->file, *new_offset + s->block_size);\n+    return bdrv_truncate(bs->file, *new_offset + s->block_size, NULL);\n }\n \n /*\n@@ -1607,14 +1607,14 @@ static int vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,\n     if (type == VHDX_TYPE_DYNAMIC) {\n         /* All zeroes, so we can just extend the file - the end of the BAT\n          * is the furthest thing we have written yet */\n-        ret = blk_truncate(blk, data_file_offset);\n+        ret = blk_truncate(blk, data_file_offset, errp);\n         if (ret < 0) {\n             error_setg_errno(errp, -ret,\n                             \"Failed to resize the underlying file\");\n             goto exit;\n         }\n     } else if (type == VHDX_TYPE_FIXED) {\n-        ret = blk_truncate(blk, data_file_offset + image_size);\n+        ret = blk_truncate(blk, data_file_offset + image_size, errp);\n         if (ret < 0) {\n             error_setg_errno(errp, -ret,\n                             \"Failed to resize the underlying file\");\ndiff --git a/block/vmdk.c b/block/vmdk.c\nindex a9bd22b..c61b9cc 100644\n--- a/block/vmdk.c\n+++ b/block/vmdk.c\n@@ -1714,10 +1714,7 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,\n     blk_set_allow_write_beyond_eof(blk, true);\n \n     if (flat) {\n-        ret = blk_truncate(blk, filesize);\n-        if (ret < 0) {\n-            error_setg_errno(errp, -ret, \"Could not truncate file\");\n-        }\n+        ret = blk_truncate(blk, filesize, errp);\n         goto exit;\n     }\n     magic = cpu_to_be32(VMDK4_MAGIC);\n@@ -1780,9 +1777,8 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,\n         goto exit;\n     }\n \n-    ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9);\n+    ret = blk_truncate(blk, le64_to_cpu(header.grain_offset) << 9, errp);\n     if (ret < 0) {\n-        error_setg_errno(errp, -ret, \"Could not truncate file\");\n         goto exit;\n     }\n \n@@ -2090,10 +2086,7 @@ static int vmdk_create(const char *filename, QemuOpts *opts, Error **errp)\n     /* bdrv_pwrite write padding zeros to align to sector, we don't need that\n      * for description file */\n     if (desc_offset == 0) {\n-        ret = blk_truncate(new_blk, desc_len);\n-        if (ret < 0) {\n-            error_setg_errno(errp, -ret, \"Could not truncate file\");\n-        }\n+        ret = blk_truncate(new_blk, desc_len, errp);\n     }\n exit:\n     if (new_blk) {\ndiff --git a/block/vpc.c b/block/vpc.c\nindex f591d4b..ecfee77 100644\n--- a/block/vpc.c\n+++ b/block/vpc.c\n@@ -851,20 +851,21 @@ static int create_dynamic_disk(BlockBackend *blk, uint8_t *buf,\n }\n \n static int create_fixed_disk(BlockBackend *blk, uint8_t *buf,\n-                             int64_t total_size)\n+                             int64_t total_size, Error **errp)\n {\n     int ret;\n \n     /* Add footer to total size */\n     total_size += HEADER_SIZE;\n \n-    ret = blk_truncate(blk, total_size);\n+    ret = blk_truncate(blk, total_size, errp);\n     if (ret < 0) {\n         return ret;\n     }\n \n     ret = blk_pwrite(blk, total_size - HEADER_SIZE, buf, HEADER_SIZE, 0);\n     if (ret < 0) {\n+        error_setg_errno(errp, -ret, \"Unable to write VHD header\");\n         return ret;\n     }\n \n@@ -996,11 +997,11 @@ static int vpc_create(const char *filename, QemuOpts *opts, Error **errp)\n \n     if (disk_type == VHD_DYNAMIC) {\n         ret = create_dynamic_disk(blk, buf, total_sectors);\n+        if (ret < 0) {\n+            error_setg(errp, \"Unable to create or write VHD header\");\n+        }\n     } else {\n-        ret = create_fixed_disk(blk, buf, total_size);\n-    }\n-    if (ret < 0) {\n-        error_setg(errp, \"Unable to create or write VHD header\");\n+        ret = create_fixed_disk(blk, buf, total_size, errp);\n     }\n \n out:\ndiff --git a/blockdev.c b/blockdev.c\nindex 841200e..78e1204 100644\n--- a/blockdev.c\n+++ b/blockdev.c\n@@ -2926,26 +2926,7 @@ void qmp_block_resize(bool has_device, const char *device,\n     /* complete all in-flight operations before resizing the device */\n     bdrv_drain_all();\n \n-    ret = blk_truncate(blk, size);\n-    switch (ret) {\n-    case 0:\n-        break;\n-    case -ENOMEDIUM:\n-        error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);\n-        break;\n-    case -ENOTSUP:\n-        error_setg(errp, QERR_UNSUPPORTED);\n-        break;\n-    case -EACCES:\n-        error_setg(errp, \"Device '%s' is read only\", device);\n-        break;\n-    case -EBUSY:\n-        error_setg(errp, QERR_DEVICE_IN_USE, device);\n-        break;\n-    default:\n-        error_setg_errno(errp, -ret, \"Could not resize\");\n-        break;\n-    }\n+    ret = blk_truncate(blk, size, errp);\n \n out:\n     blk_unref(blk);\ndiff --git a/include/block/block.h b/include/block/block.h\nindex 5ddc0cf..4bf4843 100644\n--- a/include/block/block.h\n+++ b/include/block/block.h\n@@ -294,7 +294,7 @@ BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,\n     const char *backing_file);\n int bdrv_get_backing_file_depth(BlockDriverState *bs);\n void bdrv_refresh_filename(BlockDriverState *bs);\n-int bdrv_truncate(BdrvChild *child, int64_t offset);\n+int bdrv_truncate(BdrvChild *child, int64_t offset, Error **errp);\n int64_t bdrv_nb_sectors(BlockDriverState *bs);\n int64_t bdrv_getlength(BlockDriverState *bs);\n int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);\ndiff --git a/include/sysemu/block-backend.h b/include/sysemu/block-backend.h\nindex 7462228..0ba4e27 100644\n--- a/include/sysemu/block-backend.h\n+++ b/include/sysemu/block-backend.h\n@@ -225,7 +225,7 @@ int coroutine_fn blk_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,\n                                       int count, BdrvRequestFlags flags);\n int blk_pwrite_compressed(BlockBackend *blk, int64_t offset, const void *buf,\n                           int count);\n-int blk_truncate(BlockBackend *blk, int64_t offset);\n+int blk_truncate(BlockBackend *blk, int64_t offset, Error **errp);\n int blk_pdiscard(BlockBackend *blk, int64_t offset, int count);\n int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,\n                      int64_t pos, int size);\ndiff --git a/qemu-img.c b/qemu-img.c\nindex 26ded22..e4a2686 100644\n--- a/qemu-img.c\n+++ b/qemu-img.c\n@@ -3500,20 +3500,11 @@ static int img_resize(int argc, char **argv)\n         goto out;\n     }\n \n-    ret = blk_truncate(blk, total_size);\n-    switch (ret) {\n-    case 0:\n+    ret = blk_truncate(blk, total_size, &err);\n+    if (!ret) {\n         qprintf(quiet, \"Image resized.\\n\");\n-        break;\n-    case -ENOTSUP:\n-        error_report(\"This image does not support resize\");\n-        break;\n-    case -EACCES:\n-        error_report(\"Image is read-only\");\n-        break;\n-    default:\n-        error_report(\"Error resizing image: %s\", strerror(-ret));\n-        break;\n+    } else {\n+        error_report_err(err);\n     }\n out:\n     blk_unref(blk);\ndiff --git a/qemu-io-cmds.c b/qemu-io-cmds.c\nindex 312fc6d..21af9e6 100644\n--- a/qemu-io-cmds.c\n+++ b/qemu-io-cmds.c\n@@ -1567,6 +1567,7 @@ static const cmdinfo_t flush_cmd = {\n \n static int truncate_f(BlockBackend *blk, int argc, char **argv)\n {\n+    Error *local_err = NULL;\n     int64_t offset;\n     int ret;\n \n@@ -1576,9 +1577,9 @@ static int truncate_f(BlockBackend *blk, int argc, char **argv)\n         return 0;\n     }\n \n-    ret = blk_truncate(blk, offset);\n+    ret = blk_truncate(blk, offset, &local_err);\n     if (ret < 0) {\n-        printf(\"truncate: %s\\n\", strerror(-ret));\n+        error_report_err(local_err);\n         return 0;\n     }\n \n",
    "prefixes": [
        "24/79"
    ]
}