Signed-off-by: Benoit Canet <benoit.ca...@gmail.com> --- block/qed.c | 55 ++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 34 insertions(+), 21 deletions(-)
diff --git a/block/qed.c b/block/qed.c index a041d31..25558fc 100644 --- a/block/qed.c +++ b/block/qed.c @@ -367,6 +367,37 @@ static void qed_cancel_need_check_timer(BDRVQEDState *s) qemu_del_timer(s->need_check_timer); } +static int check_image_if_needed(BlockDriverState *bs) +{ + int ret; + BDRVQEDState *s = bs->opaque; + + if (s->header.features & QED_F_NEED_CHECK) { + /* Read-only images cannot be fixed. There is no risk of corruption + * since write operations are not possible. Therefore, allow + * potentially inconsistent images to be opened read-only. This can + * aid data recovery from an otherwise inconsistent image. + */ + if (!bdrv_is_read_only(bs->file)) { + BdrvCheckResult result = {0}; + + ret = qed_check(s, &result, true); + if (ret) { + return ret; + } + if (!result.corruptions && !result.check_errors) { + /* Ensure fixes reach storage before clearing check bit */ + bdrv_flush(s->bs); + + s->header.features &= ~QED_F_NEED_CHECK; + qed_write_header_sync(s); + } + } + } + + return 0; +} + static int bdrv_qed_open(BlockDriverState *bs, int flags) { BDRVQEDState *s = bs->opaque; @@ -471,27 +502,9 @@ static int bdrv_qed_open(BlockDriverState *bs, int flags) } /* If image was not closed cleanly, check consistency */ - if (s->header.features & QED_F_NEED_CHECK) { - /* Read-only images cannot be fixed. There is no risk of corruption - * since write operations are not possible. Therefore, allow - * potentially inconsistent images to be opened read-only. This can - * aid data recovery from an otherwise inconsistent image. - */ - if (!bdrv_is_read_only(bs->file)) { - BdrvCheckResult result = {0}; - - ret = qed_check(s, &result, true); - if (ret) { - goto out; - } - if (!result.corruptions && !result.check_errors) { - /* Ensure fixes reach storage before clearing check bit */ - bdrv_flush(s->bs); - - s->header.features &= ~QED_F_NEED_CHECK; - qed_write_header_sync(s); - } - } + ret = check_image_if_needed(bs); + if (ret) { + goto out; } s->need_check_timer = qemu_new_timer_ns(vm_clock, -- 1.7.7.6