From: Kevin Wolf <kwolf@redhat.com>
To: anthony@codemonkey.ws
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 23/26] qcow2-refcount: Repair OFLAG_COPIED errors
Date: Fri, 30 Aug 2013 16:30:48 +0200 [thread overview]
Message-ID: <1377873051-18981-24-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1377873051-18981-1-git-send-email-kwolf@redhat.com>
From: Max Reitz <mreitz@redhat.com>
Since the OFLAG_COPIED checks are now executed after the refcounts have
been repaired (if repairing), it is safe to assume that they are correct
but the OFLAG_COPIED flag may be not. Therefore, if its value differs
from what it should be (considering the according refcount), that
discrepancy can be repaired by correctly setting (or clearing that flag.
Signed-off-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block/qcow2-cluster.c | 4 ++--
block/qcow2-refcount.c | 58 ++++++++++++++++++++++++++++++++++++++++++++------
block/qcow2.h | 1 +
3 files changed, 55 insertions(+), 8 deletions(-)
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 7c248aa..2d5aa92 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -145,7 +145,7 @@ static int l2_load(BlockDriverState *bs, uint64_t l2_offset,
* and we really don't want bdrv_pread to perform a read-modify-write)
*/
#define L1_ENTRIES_PER_SECTOR (512 / 8)
-static int write_l1_entry(BlockDriverState *bs, int l1_index)
+int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
{
BDRVQcowState *s = bs->opaque;
uint64_t buf[L1_ENTRIES_PER_SECTOR];
@@ -254,7 +254,7 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
/* update the L1 entry */
trace_qcow2_l2_allocate_write_l1(bs, l1_index);
s->l1_table[l1_index] = l2_offset | QCOW_OFLAG_COPIED;
- ret = write_l1_entry(bs, l1_index);
+ ret = qcow2_write_l1_entry(bs, l1_index);
if (ret < 0) {
goto fail;
}
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index aa4b98d..2276b6f 100644
--- a/block/qcow2-refcount.c
+++ b/block/qcow2-refcount.c
@@ -1225,7 +1225,8 @@ fail:
* been already detected and sufficiently signaled by the calling function
* (qcow2_check_refcounts) by the time this function is called).
*/
-static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res)
+static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
+ BdrvCheckMode fix)
{
BDRVQcowState *s = bs->opaque;
uint64_t *l2_table = qemu_blockalign(bs, s->cluster_size);
@@ -1236,6 +1237,7 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res)
for (i = 0; i < s->l1_size; i++) {
uint64_t l1_entry = s->l1_table[i];
uint64_t l2_offset = l1_entry & L1E_OFFSET_MASK;
+ bool l2_dirty = false;
if (!l2_offset) {
continue;
@@ -1247,10 +1249,24 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res)
continue;
}
if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) {
- fprintf(stderr, "ERROR OFLAG_COPIED L2 cluster: l1_index=%d "
+ fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
"l1_entry=%" PRIx64 " refcount=%d\n",
+ fix & BDRV_FIX_ERRORS ? "Repairing" :
+ "ERROR",
i, l1_entry, refcount);
- res->corruptions++;
+ if (fix & BDRV_FIX_ERRORS) {
+ s->l1_table[i] = refcount == 1
+ ? l1_entry | QCOW_OFLAG_COPIED
+ : l1_entry & ~QCOW_OFLAG_COPIED;
+ ret = qcow2_write_l1_entry(bs, i);
+ if (ret < 0) {
+ res->check_errors++;
+ goto fail;
+ }
+ res->corruptions_fixed++;
+ } else {
+ res->corruptions++;
+ }
}
ret = bdrv_pread(bs->file, l2_offset, l2_table,
@@ -1275,13 +1291,43 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res)
continue;
}
if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
- fprintf(stderr, "ERROR OFLAG_COPIED data cluster: "
+ fprintf(stderr, "%s OFLAG_COPIED data cluster: "
"l2_entry=%" PRIx64 " refcount=%d\n",
+ fix & BDRV_FIX_ERRORS ? "Repairing" :
+ "ERROR",
l2_entry, refcount);
- res->corruptions++;
+ if (fix & BDRV_FIX_ERRORS) {
+ l2_table[j] = cpu_to_be64(refcount == 1
+ ? l2_entry | QCOW_OFLAG_COPIED
+ : l2_entry & ~QCOW_OFLAG_COPIED);
+ l2_dirty = true;
+ res->corruptions_fixed++;
+ } else {
+ res->corruptions++;
+ }
}
}
}
+
+ if (l2_dirty) {
+ ret = qcow2_pre_write_overlap_check(bs,
+ QCOW2_OL_DEFAULT & ~QCOW2_OL_ACTIVE_L2, l2_offset,
+ s->cluster_size);
+ if (ret < 0) {
+ fprintf(stderr, "ERROR: Could not write L2 table; metadata "
+ "overlap check failed: %s\n", strerror(-ret));
+ res->check_errors++;
+ goto fail;
+ }
+
+ ret = bdrv_pwrite(bs->file, l2_offset, l2_table, s->cluster_size);
+ if (ret < 0) {
+ fprintf(stderr, "ERROR: Could not write L2 table: %s\n",
+ strerror(-ret));
+ res->check_errors++;
+ goto fail;
+ }
+ }
}
ret = 0;
@@ -1427,7 +1473,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
}
/* check OFLAG_COPIED */
- ret = check_oflag_copied(bs, res);
+ ret = check_oflag_copied(bs, res, fix);
if (ret < 0) {
goto fail;
}
diff --git a/block/qcow2.h b/block/qcow2.h
index d4448c6..1000239 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -432,6 +432,7 @@ int qcow2_pre_write_overlap_check(BlockDriverState *bs, int chk, int64_t offset,
/* qcow2-cluster.c functions */
int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size,
bool exact_size);
+int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index);
void qcow2_l2_cache_reset(BlockDriverState *bs);
int qcow2_decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
--
1.8.1.4
next prev parent reply other threads:[~2013-08-30 14:31 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-30 14:30 [Qemu-devel] [PULL 00/26] Block patches Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 01/26] qcow2: Change default for new images to compat=1.1 Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 02/26] block: Remove redundant assertion Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 03/26] qapi-types.py: Split off generate_struct_fields() Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 04/26] Revert "block: Disable driver-specific options for 1.6" Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 05/26] qemu-iotests: Update reference output for 051 Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 06/26] block/qcow2.h: Avoid "1LL << 63" (shifts into sign bit) Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 07/26] add skeleton for BSD licensed "raw" BlockDriver Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 08/26] raw_bsd: emit debug events in bdrv_co_readv() and bdrv_co_writev() Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 09/26] raw_bsd: add raw_create() Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 10/26] raw_bsd: introduce "special members" Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 11/26] raw_bsd: add raw_create_options Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 12/26] raw_bsd: register bdrv_raw Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 13/26] switch raw block driver from "raw.o" to "raw_bsd.o" Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 14/26] block: Remove old raw driver Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 15/26] gluster: Abort on AIO completion failure Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 16/26] option: Add assigned flag to QEMUOptionParameter Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 17/26] qcow2-refcount: Snapshot update for zero clusters Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 18/26] qemu-iotests: Snapshotting " Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 19/26] qcow2: Add corrupt bit Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 20/26] qcow2: Metadata overlap checks Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 21/26] qcow2: Employ metadata " Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 22/26] qcow2-refcount: Move OFLAG_COPIED checks Kevin Wolf
2013-08-30 14:30 ` Kevin Wolf [this message]
2013-08-30 14:30 ` [Qemu-devel] [PULL 24/26] qcow2-refcount: Repair shared refcount blocks Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 25/26] qcow2_check: Mark image consistent Kevin Wolf
2013-08-30 14:30 ` [Qemu-devel] [PULL 26/26] qemu-iotests: Overlapping cluster allocations Kevin Wolf
2013-08-30 17:14 ` [Qemu-devel] [PULL 00/26] Block patches Anthony Liguori
2013-09-02 8:24 ` Kevin Wolf
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=1377873051-18981-24-git-send-email-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=anthony@codemonkey.ws \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).