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