[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 20/27] block/parallels: implement incorrect close de
From: |
Denis V. Lunev |
Subject: |
[Qemu-devel] [PATCH 20/27] block/parallels: implement incorrect close detection |
Date: |
Tue, 28 Apr 2015 10:46:53 +0300 |
The software driver must set inuse field in Parallels header to
0x746F6E59 when the image is opened in read-write mode. The presence of
this magic in the header on open forces image consistency check.
There is an unfortunate trick here. We can not check for inuse in
parallels_check as this will happen too late. It is possible to do
that for simple check, but during the fix this would always report
an error as the image was opened in BDRV_O_RDWR mode. Thus we save
the flag in BDRVParallelsState for this.
On the other hand, nothing should be done to clear inuse in
parallels_check. Generic close will do the job right.
Signed-off-by: Denis V. Lunev <address@hidden>
Reviewed-by: Roman Kagan <address@hidden>
Reviewed-by: Stefan Hajnoczi <address@hidden>
CC: Kevin Wolf <address@hidden>
---
block/parallels.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/block/parallels.c b/block/parallels.c
index 35f231a..76e3a4e 100644
--- a/block/parallels.c
+++ b/block/parallels.c
@@ -36,6 +36,7 @@
#define HEADER_MAGIC "WithoutFreeSpace"
#define HEADER_MAGIC2 "WithouFreSpacExt"
#define HEADER_VERSION 2
+#define HEADER_INUSE_MAGIC (0x746F6E59)
#define DEFAULT_CLUSTER_SIZE 1048576 /* 1 MiB */
@@ -63,6 +64,8 @@ typedef struct BDRVParallelsState {
ParallelsHeader *header;
uint32_t header_size;
+ bool header_unclean;
+
uint32_t *bat_bitmap;
unsigned int bat_size;
@@ -259,6 +262,17 @@ static int parallels_check(BlockDriverState *bs,
BdrvCheckResult *res,
return size;
}
+ if (s->header_unclean) {
+ fprintf(stderr, "%s image was not closed correctly\n",
+ fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
+ res->corruptions++;
+ if (fix & BDRV_FIX_ERRORS) {
+ /* parallels_close will do the job right */
+ res->corruptions_fixed++;
+ s->header_unclean = false;
+ }
+ }
+
res->bfi.total_clusters = s->bat_size;
res->bfi.compressed_clusters = 0; /* compression is not supported */
@@ -417,6 +431,17 @@ static int parallels_probe(const uint8_t *buf, int
buf_size,
return 0;
}
+static int parallels_update_header(BlockDriverState *bs)
+{
+ BDRVParallelsState *s = bs->opaque;
+ unsigned size = MAX(bdrv_opt_mem_align(bs->file), sizeof(ParallelsHeader));
+
+ if (size > s->header_size) {
+ size = s->header_size;
+ }
+ return bdrv_pwrite_sync(bs->file, 0, s->header, size);
+}
+
static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
Error **errp)
{
@@ -484,6 +509,25 @@ static int parallels_open(BlockDriverState *bs, QDict
*options, int flags,
s->has_truncate = bdrv_has_zero_init(bs->file) &&
bdrv_truncate(bs->file, bdrv_getlength(bs->file)) == 0;
+ if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
+ /* Image was not closed correctly. The check is mandatory */
+ s->header_unclean = true;
+ if ((flags & BDRV_O_RDWR) && !(flags & BDRV_O_CHECK)) {
+ error_setg(errp, "parallels: Image was not closed correctly; "
+ "cannot be opened read/write");
+ ret = -EACCES;
+ goto fail;
+ }
+ }
+
+ if (flags & BDRV_O_RDWR) {
+ s->header->inuse = cpu_to_le32(HEADER_INUSE_MAGIC);
+ ret = parallels_update_header(bs);
+ if (ret < 0) {
+ goto fail;
+ }
+ }
+
qemu_co_mutex_init(&s->lock);
return 0;
@@ -499,6 +543,12 @@ fail:
static void parallels_close(BlockDriverState *bs)
{
BDRVParallelsState *s = bs->opaque;
+
+ if (bs->open_flags & BDRV_O_RDWR) {
+ s->header->inuse = 0;
+ parallels_update_header(bs);
+ }
+
qemu_vfree(s->header);
}
--
1.9.1
- [Qemu-devel] [PATCH 11/27] block/parallels: support parallels image creation, (continued)
- [Qemu-devel] [PATCH 11/27] block/parallels: support parallels image creation, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 13/27] parallels: change copyright information in the image header, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 12/27] iotests, parallels: test for newly created parallels image via qemu-img, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 10/27] iotests, parallels: test for write into Parallels image, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 14/27] block/parallels: rename catalog_ names to bat_, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 15/27] block/parallels: create bat2sect helper, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 16/27] block/parallels: keep BAT bitmap data in little endian in memory, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 17/27] block/parallels: read parallels image header and BAT into single buffer, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 18/27] block/parallels: move parallels_open/probe to the very end of the file, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 19/27] block/parallels: implement parallels_check method of block driver, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 20/27] block/parallels: implement incorrect close detection,
Denis V. Lunev <=
- [Qemu-devel] [PATCH 22/27] block/parallels: improve image reading performance, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 21/27] iotests, parallels: check for incorrectly closed image in tests, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 23/27] block/parallels: create bat_entry_off helper, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 24/27] block/parallels: delay writing to BAT till bdrv_co_flush_to_os, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 25/27] block/parallels: add prealloc-mode and prealloc-size open paramemets, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 26/27] block/parallels: optimize linear image expansion, Denis V. Lunev, 2015/04/28
- [Qemu-devel] [PATCH 27/27] block/parallels: improve image writing performance further, Denis V. Lunev, 2015/04/28