586cba
From 5e385a0e49a520550a83299632be175857b63f19 Mon Sep 17 00:00:00 2001
586cba
From: Hanna Reitz <hreitz@redhat.com>
586cba
Date: Tue, 5 Apr 2022 15:46:52 +0200
586cba
Subject: [PATCH 06/16] qcow2: Add errp to rebuild_refcount_structure()
586cba
586cba
RH-Author: Hanna Reitz <hreitz@redhat.com>
586cba
RH-MergeRequest: 96: qcow2: Improve refcount structure rebuilding
586cba
RH-Commit: [3/4] 937b89a7eab6ec6b18618d59bc1526976ad03290 (hreitz/qemu-kvm-c-9-s)
586cba
RH-Bugzilla: 2072379
586cba
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
586cba
RH-Acked-by: Eric Blake <eblake@redhat.com>
586cba
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
586cba
586cba
Instead of fprint()-ing error messages in rebuild_refcount_structure()
586cba
and its rebuild_refcounts_write_refblocks() helper, pass them through an
586cba
Error object to qcow2_check_refcounts() (which will then print it).
586cba
586cba
Suggested-by: Eric Blake <eblake@redhat.com>
586cba
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
586cba
Message-Id: <20220405134652.19278-4-hreitz@redhat.com>
586cba
Reviewed-by: Eric Blake <eblake@redhat.com>
586cba
(cherry picked from commit 0423f75351ab83b844a31349218b0eadd830e07a)
586cba
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
586cba
---
586cba
 block/qcow2-refcount.c | 33 +++++++++++++++++++--------------
586cba
 1 file changed, 19 insertions(+), 14 deletions(-)
586cba
586cba
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
586cba
index c5669eaa51..ed0ecfaa89 100644
586cba
--- a/block/qcow2-refcount.c
586cba
+++ b/block/qcow2-refcount.c
586cba
@@ -2465,7 +2465,8 @@ static int64_t alloc_clusters_imrt(BlockDriverState *bs,
586cba
 static int rebuild_refcounts_write_refblocks(
586cba
         BlockDriverState *bs, void **refcount_table, int64_t *nb_clusters,
586cba
         int64_t first_cluster, int64_t end_cluster,
586cba
-        uint64_t **on_disk_reftable_ptr, uint32_t *on_disk_reftable_entries_ptr
586cba
+        uint64_t **on_disk_reftable_ptr, uint32_t *on_disk_reftable_entries_ptr,
586cba
+        Error **errp
586cba
     )
586cba
 {
586cba
     BDRVQcow2State *s = bs->opaque;
586cba
@@ -2516,8 +2517,8 @@ static int rebuild_refcounts_write_refblocks(
586cba
                                                   nb_clusters,
586cba
                                                   &first_free_cluster);
586cba
             if (refblock_offset < 0) {
586cba
-                fprintf(stderr, "ERROR allocating refblock: %s\n",
586cba
-                        strerror(-refblock_offset));
586cba
+                error_setg_errno(errp, -refblock_offset,
586cba
+                                 "ERROR allocating refblock");
586cba
                 return refblock_offset;
586cba
             }
586cba
 
586cba
@@ -2539,6 +2540,7 @@ static int rebuild_refcounts_write_refblocks(
586cba
                                   on_disk_reftable_entries *
586cba
                                   REFTABLE_ENTRY_SIZE);
586cba
                 if (!on_disk_reftable) {
586cba
+                    error_setg(errp, "ERROR allocating reftable memory");
586cba
                     return -ENOMEM;
586cba
                 }
586cba
 
586cba
@@ -2562,7 +2564,7 @@ static int rebuild_refcounts_write_refblocks(
586cba
         ret = qcow2_pre_write_overlap_check(bs, 0, refblock_offset,
586cba
                                             s->cluster_size, false);
586cba
         if (ret < 0) {
586cba
-            fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
586cba
+            error_setg_errno(errp, -ret, "ERROR writing refblock");
586cba
             return ret;
586cba
         }
586cba
 
586cba
@@ -2578,7 +2580,7 @@ static int rebuild_refcounts_write_refblocks(
586cba
         ret = bdrv_pwrite(bs->file, refblock_offset, on_disk_refblock,
586cba
                           s->cluster_size);
586cba
         if (ret < 0) {
586cba
-            fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
586cba
+            error_setg_errno(errp, -ret, "ERROR writing refblock");
586cba
             return ret;
586cba
         }
586cba
 
586cba
@@ -2601,7 +2603,8 @@ static int rebuild_refcounts_write_refblocks(
586cba
 static int rebuild_refcount_structure(BlockDriverState *bs,
586cba
                                       BdrvCheckResult *res,
586cba
                                       void **refcount_table,
586cba
-                                      int64_t *nb_clusters)
586cba
+                                      int64_t *nb_clusters,
586cba
+                                      Error **errp)
586cba
 {
586cba
     BDRVQcow2State *s = bs->opaque;
586cba
     int64_t reftable_offset = -1;
586cba
@@ -2652,7 +2655,7 @@ static int rebuild_refcount_structure(BlockDriverState *bs,
586cba
         rebuild_refcounts_write_refblocks(bs, refcount_table, nb_clusters,
586cba
                                           0, *nb_clusters,
586cba
                                           &on_disk_reftable,
586cba
-                                          &on_disk_reftable_entries);
586cba
+                                          &on_disk_reftable_entries, errp);
586cba
     if (reftable_size_changed < 0) {
586cba
         res->check_errors++;
586cba
         ret = reftable_size_changed;
586cba
@@ -2676,8 +2679,8 @@ static int rebuild_refcount_structure(BlockDriverState *bs,
586cba
                                               refcount_table, nb_clusters,
586cba
                                               &first_free_cluster);
586cba
         if (reftable_offset < 0) {
586cba
-            fprintf(stderr, "ERROR allocating reftable: %s\n",
586cba
-                    strerror(-reftable_offset));
586cba
+            error_setg_errno(errp, -reftable_offset,
586cba
+                             "ERROR allocating reftable");
586cba
             res->check_errors++;
586cba
             ret = reftable_offset;
586cba
             goto fail;
586cba
@@ -2695,7 +2698,7 @@ static int rebuild_refcount_structure(BlockDriverState *bs,
586cba
                                               reftable_start_cluster,
586cba
                                               reftable_end_cluster,
586cba
                                               &on_disk_reftable,
586cba
-                                              &on_disk_reftable_entries);
586cba
+                                              &on_disk_reftable_entries, errp);
586cba
         if (reftable_size_changed < 0) {
586cba
             res->check_errors++;
586cba
             ret = reftable_size_changed;
586cba
@@ -2725,7 +2728,7 @@ static int rebuild_refcount_structure(BlockDriverState *bs,
586cba
     ret = qcow2_pre_write_overlap_check(bs, 0, reftable_offset, reftable_length,
586cba
                                         false);
586cba
     if (ret < 0) {
586cba
-        fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
586cba
+        error_setg_errno(errp, -ret, "ERROR writing reftable");
586cba
         goto fail;
586cba
     }
586cba
 
586cba
@@ -2733,7 +2736,7 @@ static int rebuild_refcount_structure(BlockDriverState *bs,
586cba
     ret = bdrv_pwrite(bs->file, reftable_offset, on_disk_reftable,
586cba
                       reftable_length);
586cba
     if (ret < 0) {
586cba
-        fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
586cba
+        error_setg_errno(errp, -ret, "ERROR writing reftable");
586cba
         goto fail;
586cba
     }
586cba
 
586cba
@@ -2746,7 +2749,7 @@ static int rebuild_refcount_structure(BlockDriverState *bs,
586cba
                            &reftable_offset_and_clusters,
586cba
                            sizeof(reftable_offset_and_clusters));
586cba
     if (ret < 0) {
586cba
-        fprintf(stderr, "ERROR setting reftable: %s\n", strerror(-ret));
586cba
+        error_setg_errno(errp, -ret, "ERROR setting reftable");
586cba
         goto fail;
586cba
     }
586cba
 
586cba
@@ -2814,11 +2817,13 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
586cba
     if (rebuild && (fix & BDRV_FIX_ERRORS)) {
586cba
         BdrvCheckResult old_res = *res;
586cba
         int fresh_leaks = 0;
586cba
+        Error *local_err = NULL;
586cba
 
586cba
         fprintf(stderr, "Rebuilding refcount structure\n");
586cba
         ret = rebuild_refcount_structure(bs, res, &refcount_table,
586cba
-                                         &nb_clusters);
586cba
+                                         &nb_clusters, &local_err);
586cba
         if (ret < 0) {
586cba
+            error_report_err(local_err);
586cba
             goto fail;
586cba
         }
586cba
 
586cba
-- 
586cba
2.31.1
586cba