Skip to contentSkip to conversation

Block patches

GIT PULL In review Kevin Wolf 5 participants 50 messages Jun 15, 2012 – Dec 18, 2012 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

The following changes since commit 63bb682a18acc5d2f3219ea96c4d81c1a3b50748:

Merge branch 'master' of git://git.qemu.org/qemu (2012-06-15 15:53:34 +0400)

are available in the git repository at:

git://repo.or.cz/qemu/kevin.git for-anthony

Josh Durgin (1):
rbd: hook up cache options

Kevin Wolf (7):
qemu-img check -r for repairing images
qemu-img check: Print fixed clusters and recheck
qcow2: Support for fixing refcount inconsistencies
qcow2: Simplify calculation for COW area at the end
qcow2: Fix avail_sectors in cluster allocation code
qemu-iotests: Some backing file COW tests
qemu-iotests: COW with many AIO requests on the same cluster

MORITA Kazutaka (1):
sheepdog: add coroutine_fn markers to coroutine functions

Markus Armbruster (7):
Un-inline fdctrl_init_isa()
block: Simplify how drive_init() computes default ID
block: New bdrv_get_flags()
scsi-disk: Don't peek behind the BlockDriverState abstraction
block: Replace bdrv_get_format() by bdrv_get_format_name()
xen: Don't change -drive if=xen device name during machine init
xen: Don't peek behind the BlockDriverState abstraction

Max Filippov (1):
xtensa_lx60: add missing #include "blockdev.h"

Paolo Bonzini (13):
block: implement is_allocated for raw
stream: tweak usage of bdrv_co_is_allocated
stream: move is_allocated_above to block.c
stream: move rate limiting to a separate header file
qemu-iotests: fill streaming test image with data
qemu-iotests: start vms in qtest mode
block: flush in writethrough mode after writes
savevm: flush after saving vm state
block: copy enable_write_cache in bdrv_append
block: add bdrv_set_enable_write_cache
block: always open drivers in writeback mode
ide: support enable/disable write cache
qcow2: always operate caches in writeback mode

Pavel Dovgaluk (1):
Prevent disk data loss when closing qemu

Pavel Hrdina (2):
fdc: fix implied seek while there is no media in drive
fdc-test: introduced qtest read_without_media

Stefan Hajnoczi (4):
qemu-img: document qed format on qemu-img man page
qcow2: fix autoclear image header update
qemu-iotests: add qcow2.py set-feature-bit command
qemu-iotests: add 036 autoclear feature bit test

Zhi Yong Wu (2):
qcow2: remove a line of unnecessary code
qcow2: fix endianness conversion

 block.c                       |   90 ++++-
 block.h                       |   17 +-
 block/qcow2-cache.c           |   25 +-
 block/qcow2-cluster.c         |   16 +-
 block/qcow2-refcount.c        |   57 ++-
 block/qcow2.c                 |   29 +-
 block/qcow2.h                 |    8 +-
 block/qed-check.c             |    2 +
 block/qed.c                   |    5 +-
 block/raw-posix.c             |   98 +++++
 block/raw.c                   |    8 +
 block/rbd.c                   |   19 +
 block/sheepdog.c              |    9 +-
 block/stream.c                |  109 +-----
 block/vdi.c                   |    7 +-
 block_int.h                   |    3 +-
 blockdev.c                    |    7 +-
 hw/fdc.c                      |   24 ++
 hw/fdc.h                      |   24 +-
 hw/ide/core.c                 |   21 +-
 hw/ide/piix.c                 |    3 +-
 hw/isa.h                      |    2 -
 hw/pc_sysfw.c                 |    1 +
 hw/scsi-disk.c                |    3 +-
 hw/xen_devconfig.c            |   13 +-
 hw/xen_disk.c                 |    3 +-
 hw/xtensa_lx60.c              |    1 +
 include/qemu/ratelimit.h      |   48 +++
 os-win32.c                    |    8 +-
 qemu-common.h                 |    1 +
 qemu-img-cmds.hx              |    4 +-
 qemu-img.c                    |   40 ++-
 qemu-img.texi                 |   30 ++-
 savevm.c                      |    2 +-
 tests/fdc-test.c              |   66 +++
 tests/qemu-iotests/030        |   15 +-
 tests/qemu-iotests/036        |   68 +++
 tests/qemu-iotests/036.out    |   52 +++
 tests/qemu-iotests/037        |  119 ++++++
 tests/qemu-iotests/037.out    |  645 +++++++++++++++++++++++++++++
 tests/qemu-iotests/038        |  133 ++++++
 tests/qemu-iotests/038.out    |  909 +++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/group      |    3 +
 tests/qemu-iotests/iotests.py |    4 +-
 tests/qemu-iotests/qcow2.py   |   23 +
 45 files changed, 2531 insertions(+), 243 deletions(-)
 create mode 100644 include/qemu/ratelimit.h
 create mode 100755 tests/qemu-iotests/036
 create mode 100644 tests/qemu-iotests/036.out
 create mode 100755 tests/qemu-iotests/037
 create mode 100644 tests/qemu-iotests/037.out
 create mode 100755 tests/qemu-iotests/038
 create mode 100644 tests/qemu-iotests/038.out
author
#1 lore raw

PATCH 1/39 qcow2: remove a line of unnecessary code

From: Zhi Yong Wu <[email protected]>

Commit 3948d1d4 removed the pointer argument we filled in with l2_offset but forgot to remove the unnecessary l2_offset assignment.

---
 block/qcow2-cluster.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
block/qcow2-cluster.c +0 -1
index c173fcd..4eb5ba7 100644
@@ -540,7 +540,6 @@ static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
         if (l2_offset) {
             qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
         }
-        l2_offset = s->l1_table[l1_index] & L1E_OFFSET_MASK;
     }
 
     /* find the cluster offset for the given disk offset */
Signature · 1 line
1.7.6.5
author
#2 lore raw

PATCH 2/39 qcow2: fix endianness conversion

In reply to Kevin Wolf

From: Zhi Yong Wu <[email protected]>

---
 block/qcow2-refcount.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
block/qcow2-refcount.c +1 -1
index 812c93c..443c021 100644
@@ -367,7 +367,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
     }
 
     for(i = 0; i < table_size; i++) {
-        cpu_to_be64s(&new_table[i]);
+        be64_to_cpus(&new_table[i]);
     }
 
     /* Hook up the new refcount table in the qcow2 header */
Signature · 1 line
1.7.6.5
author
#3 lore raw

PATCH 3/39 block: implement is_allocated for raw

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

Either FIEMAP, or SEEK_DATA+SEEK_HOLE can be used to implement the is_allocated callback for raw files. On Linux ext4, btrfs and XFS all support it.

---
 block/raw-posix.c |   98 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 block/raw.c       |    8 ++++
 2 files changed, 106 insertions(+), 0 deletions(-)
block/raw-posix.c +98 -0
index 03fcfcc..bf7700a 100644
@@ -52,6 +52,10 @@
 #include <sys/param.h>
 #include <linux/cdrom.h>
 #include <linux/fd.h>
+#include <linux/fs.h>
+#endif
+#ifdef CONFIG_FIEMAP
+#include <linux/fiemap.h>
 #endif
 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
 #include <sys/disk.h>
@@ -583,6 +587,99 @@ static int raw_create(const char *filename, QEMUOptionParameter *options)
     return result;
 }
 
+/*
+ * Returns true iff the specified sector is present in the disk image. Drivers
+ * not implementing the functionality are assumed to not support backing files,
+ * hence all their sectors are reported as allocated.
+ *
+ * If 'sector_num' is beyond the end of the disk image the return value is 0
+ * and 'pnum' is set to 0.
+ *
+ * 'pnum' is set to the number of sectors (including and immediately following
+ * the specified sector) that are known to be in the same
+ * allocated/unallocated state.
+ *
+ * 'nb_sectors' is the max value 'pnum' should be set to.  If nb_sectors goes
+ * beyond the end of the disk image it will be clamped.
+ */
+static int coroutine_fn raw_co_is_allocated(BlockDriverState *bs,
+                                            int64_t sector_num,
+                                            int nb_sectors, int *pnum)
+{
+    BDRVRawState *s = bs->opaque;
+    off_t start, data, hole;
+    int ret;
+
+    ret = fd_open(bs);
+    if (ret < 0) {
+        return ret;
+    }
+
+    start = sector_num * BDRV_SECTOR_SIZE;
+#ifdef CONFIG_FIEMAP
+    struct {
+        struct fiemap fm;
+        struct fiemap_extent fe;
+    } f;
+    f.fm.fm_start = start;
+    f.fm.fm_length = (int64_t)nb_sectors * BDRV_SECTOR_SIZE;
+    f.fm.fm_flags = 0;
+    f.fm.fm_extent_count = 1;
+    f.fm.fm_reserved = 0;
+    if (ioctl(s->fd, FS_IOC_FIEMAP, &f) == -1) {
+        /* Assume everything is allocated.  */
+        *pnum = nb_sectors;
+        return 1;
+    }
+
+    if (f.fm.fm_mapped_extents == 0) {
+        /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
+         * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
+         */
+        off_t length = lseek(s->fd, 0, SEEK_END);
+        hole = f.fm.fm_start;
+        data = MIN(f.fm.fm_start + f.fm.fm_length, length);
+    } else {
+        data = f.fe.fe_logical;
+        hole = f.fe.fe_logical + f.fe.fe_length;
+    }
+#elif defined SEEK_HOLE && defined SEEK_DATA
+    hole = lseek(s->fd, start, SEEK_HOLE);
+    if (hole == -1) {
+        /* -ENXIO indicates that sector_num was past the end of the file.
+         * There is a virtual hole there.  */
+        assert(errno != -ENXIO);
+
+        /* Most likely EINVAL.  Assume everything is allocated.  */
+        *pnum = nb_sectors;
+        return 1;
+    }
+
+    if (hole > start) {
+        data = start;
+    } else {
+        /* On a hole.  We need another syscall to find its end.  */
+        data = lseek(s->fd, start, SEEK_DATA);
+        if (data == -1) {
+            data = lseek(s->fd, 0, SEEK_END);
+        }
+    }
+#else
+    *pnum = nb_sectors;
+    return 1;
+#endif
+
+    if (data <= start) {
+        /* On a data extent, compute sectors to the end of the extent.  */
+        *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
+        return 1;
+    } else {
+        /* On a hole, compute sectors to the beginning of the next extent.  */
+        *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
+        return 0;
+    }
+}
+
 #ifdef CONFIG_XFS
 static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors)
 {
@@ -634,6 +731,7 @@ static BlockDriver bdrv_file = {
     .bdrv_close = raw_close,
     .bdrv_create = raw_create,
     .bdrv_co_discard = raw_co_discard,
+    .bdrv_co_is_allocated = raw_co_is_allocated,
 
     .bdrv_aio_readv = raw_aio_readv,
     .bdrv_aio_writev = raw_aio_writev,
block/raw.c +8 -0
index 7086e31..09d9b48 100644
@@ -25,6 +25,13 @@ static void raw_close(BlockDriverState *bs)
 {
 }
 
+static int coroutine_fn raw_co_is_allocated(BlockDriverState *bs,
+                                            int64_t sector_num,
+                                            int nb_sectors, int *pnum)
+{
+    return bdrv_co_is_allocated(bs->file, sector_num, nb_sectors, pnum);
+}
+
 static int64_t raw_getlength(BlockDriverState *bs)
 {
     return bdrv_getlength(bs->file);
@@ -108,6 +115,7 @@ static BlockDriver bdrv_raw = {
 
     .bdrv_co_readv          = raw_co_readv,
     .bdrv_co_writev         = raw_co_writev,
+    .bdrv_co_is_allocated   = raw_co_is_allocated,
     .bdrv_co_discard        = raw_co_discard,
 
     .bdrv_probe         = raw_probe,
Signature · 1 line
1.7.6.5
#4 lore raw

Re: [Qemu-devel] [PATCH 03/39] block: implement is_allocated for raw

Quoted 42 lines · Kevin Wolf

From: Paolo Bonzini <[email protected]>

Either FIEMAP, or SEEK_DATA+SEEK_HOLE can be used to implement the is_allocated callback for raw files. On Linux ext4, btrfs and XFS all support it.

---
block/raw-posix.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++
block/raw.c | 8 ++++

2 files changed, 106 insertions(+), 0 deletions(-)
block/raw-posix.c +14 -0
index 03fcfcc..bf7700a 100644
@@ -52,6 +52,10 @@
 #include <sys/param.h>
 #include <linux/cdrom.h>
 #include <linux/fd.h>
+#include <linux/fs.h>
+#endif
+#ifdef CONFIG_FIEMAP
+#include <linux/fiemap.h>
 #endif
 #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
 #include <sys/disk.h>
@@ -583,6 +587,99 @@ static int raw_create(const char *filename, QEMUOptionParameter *options)
    return result;
 }
 
+/*
+ * Returns true iff the specified sector is present in the disk image. Drivers
+ * not implementing the functionality are assumed to not support backing files,
+ * hence all their sectors are reported as allocated.
+ *
+ * If 'sector_num' is beyond the end of the disk image the return value is 0
+ * and 'pnum' is set to 0.
+ *
+ * 'pnum' is set to the number of sectors (including and immediately following
+ * the specified sector) that are known to be in the same

block/raw-posix.c static int raw_create(const char *filename, QEMUOptionParameter *options) · line 609

Quoted 10 lines
block/raw-posix.c +10 -0
+ * allocated/unallocated state.
+ *
+ * 'nb_sectors' is the max value 'pnum' should be set to.  If nb_sectors goes
+ * beyond the end of the disk image it will be clamped.
+ */
+static int coroutine_fn raw_co_is_allocated(BlockDriverState *bs,
+                                            int64_t sector_num,
+                                            int nb_sectors, int *pnum)
+{
+    BDRVRawState *s = bs->opaque;

cc1: warnings being treated as errors block/raw-posix.c: In function ‘raw_co_is_allocated’:
block/raw-posix.c:609: error: unused variable ‘s’

Alex

author
#5 lore raw

PATCH 4/39 stream: tweak usage of bdrv_co_is_allocated

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

is_allocated_base has complex semantics that are not really usable outside streaming. Split the check in two parts, where the allocated state for the top bs is moved to the caller. The resulting function is more generally useful.

---
 block/stream.c |   51 +++++++++++++++++++++++++--------------------------
 1 files changed, 25 insertions(+), 26 deletions(-)
block/stream.c +25 -26
index 8e58322..4490a25 100644
@@ -101,45 +101,33 @@ static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
 /*
  * Given an image chain: [BASE] -> [INTER1] -> [INTER2] -> [TOP]
  *
- * Return true if the given sector is allocated in top.
- * Return false if the given sector is allocated in intermediate images.
- * Return true otherwise.
+ * Return true if the given sector is allocated in any image between
+ * BASE and TOP (inclusive).  BASE can be NULL to check if the given
+ * sector is allocated in any image of the chain.  Return false otherwise.
  *
  * 'pnum' is set to the number of sectors (including and immediately following
  *  the specified sector) that are known to be in the same
  *  allocated/unallocated state.
  *
  */
-static int coroutine_fn is_allocated_base(BlockDriverState *top,
-                                          BlockDriverState *base,
-                                          int64_t sector_num,
-                                          int nb_sectors, int *pnum)
+static int coroutine_fn is_allocated_above(BlockDriverState *top,
+                                           BlockDriverState *base,
+                                           int64_t sector_num,
+                                           int nb_sectors, int *pnum)
 {
     BlockDriverState *intermediate;
-    int ret, n;
-
-    ret = bdrv_co_is_allocated(top, sector_num, nb_sectors, &n);
-    if (ret) {
-        *pnum = n;
-        return ret;
-    }
-
-    /*
-     * Is the unallocated chunk [sector_num, n] also
-     * unallocated between base and top?
-     */
-    intermediate = top->backing_hd;
+    int ret, n = nb_sectors;
 
+    intermediate = top;
     while (intermediate != base) {
         int pnum_inter;
-
         ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors,
                                    &pnum_inter);
         if (ret < 0) {
             return ret;
         } else if (ret) {
             *pnum = pnum_inter;
-            return 0;
+            return 1;
         }
 
         /*
@@ -156,7 +144,7 @@ static int coroutine_fn is_allocated_base(BlockDriverState *top,
     }
 
     *pnum = n;
-    return 1;
+    return 0;
 }
 
 static void coroutine_fn stream_run(void *opaque)
@@ -189,6 +177,7 @@ static void coroutine_fn stream_run(void *opaque)
 
     for (sector_num = 0; sector_num < end; sector_num += n) {
         uint64_t delay_ns = 0;
+        bool copy;
 
 wait:
         /* Note that even when no rate limit is applied we need to yield
@@ -199,10 +188,20 @@ wait:
             break;
         }
 
-        ret = is_allocated_base(bs, base, sector_num,
-                                STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
+        ret = bdrv_co_is_allocated(bs, sector_num,
+                                   STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
+        if (ret == 1) {
+            /* Allocated in the top, no need to copy.  */
+            copy = false;
+        } else {
+            /* Copy if allocated in the intermediate images.  Limit to the
+             * known-unallocated area [sector_num, sector_num+n).  */
+            ret = is_allocated_above(bs->backing_hd, base, sector_num, n, &n);
+            copy = (ret == 1);
+        }
+
         trace_stream_one_iteration(s, sector_num, n, ret);
-        if (ret == 0) {
+        if (ret >= 0 && copy) {
             if (s->common.speed) {
                 delay_ns = ratelimit_calculate_delay(&s->limit, n);
                 if (delay_ns > 0) {
Signature · 1 line
1.7.6.5
author
#6 lore raw

PATCH 5/39 stream: move is_allocated_above to block.c

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

---
 block.c        |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 block.h        |    4 ++++
 block/stream.c |   53 ++---------------------------------------------------
 3 files changed, 55 insertions(+), 51 deletions(-)
block.c +49 -0
index 7547051..c07ff39 100644
@@ -2569,6 +2569,55 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
     return data.ret;
 }
 
+/*
+ * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
+ *
+ * Return true if the given sector is allocated in any image between
+ * BASE and TOP (inclusive).  BASE can be NULL to check if the given
+ * sector is allocated in any image of the chain.  Return false otherwise.
+ *
+ * 'pnum' is set to the number of sectors (including and immediately following
+ *  the specified sector) that are known to be in the same
+ *  allocated/unallocated state.
+ *
+ */
+int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top,
+                                            BlockDriverState *base,
+                                            int64_t sector_num,
+                                            int nb_sectors, int *pnum)
+{
+    BlockDriverState *intermediate;
+    int ret, n = nb_sectors;
+
+    intermediate = top;
+    while (intermediate && intermediate != base) {
+        int pnum_inter;
+        ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors,
+                                   &pnum_inter);
+        if (ret < 0) {
+            return ret;
+        } else if (ret) {
+            *pnum = pnum_inter;
+            return 1;
+        }
+
+        /*
+         * [sector_num, nb_sectors] is unallocated on top but intermediate
+         * might have
+         *
+         * [sector_num+x, nr_sectors] allocated.
+         */
+        if (n > pnum_inter) {
+            n = pnum_inter;
+        }
+
+        intermediate = intermediate->backing_hd;
+    }
+
+    *pnum = n;
+    return 0;
+}
+
 BlockInfoList *qmp_query_block(Error **errp)
 {
     BlockInfoList *head = NULL, *cur_item = NULL;
block.h +4 -0
index 7408acc..799cf48 100644
@@ -165,6 +165,10 @@ int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, int64_t sector_num,
     int nb_sectors);
 int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num,
     int nb_sectors, int *pnum);
+int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top,
+                                            BlockDriverState *base,
+                                            int64_t sector_num,
+                                            int nb_sectors, int *pnum);
 BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
     const char *backing_file);
 int bdrv_truncate(BlockDriverState *bs, int64_t offset);
block/stream.c +2 -51

67 lines not on this page · raw message

Signature · 1 line
1.7.6.5
author
#7 lore raw

PATCH 6/39 stream: move rate limiting to a separate header file

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

Make the code reusable.

---
 block/stream.c           |   31 +---------------------------
 include/qemu/ratelimit.h |   48 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 29 deletions(-)
 create mode 100644 include/qemu/ratelimit.h
block/stream.c +2 -29
index 811388a..37c4652 100644
@@ -13,6 +13,7 @@
 
 #include "trace.h"
 #include "block_int.h"
+#include "qemu/ratelimit.h"
 
 enum {
     /*
@@ -25,34 +26,6 @@ enum {
 
 #define SLICE_TIME 100000000ULL /* ns */
 
-typedef struct {
-    int64_t next_slice_time;
-    uint64_t slice_quota;
-    uint64_t dispatched;
-} RateLimit;
-
-static int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)
-{
-    int64_t now = qemu_get_clock_ns(rt_clock);
-
-    if (limit->next_slice_time < now) {
-        limit->next_slice_time = now + SLICE_TIME;
-        limit->dispatched = 0;
-    }
-    if (limit->dispatched == 0 || limit->dispatched + n <= limit->slice_quota) {
-        limit->dispatched += n;
-        return 0;
-    } else {
-        limit->dispatched = n;
-        return limit->next_slice_time - now;
-    }
-}
-
-static void ratelimit_set_speed(RateLimit *limit, uint64_t speed)
-{
-    limit->slice_quota = speed / (1000000000ULL / SLICE_TIME);
-}
-
 typedef struct StreamBlockJob {
     BlockJob common;
     RateLimit limit;
@@ -198,7 +171,7 @@ static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
         error_set(errp, QERR_INVALID_PARAMETER, "speed");
         return;
     }
-    ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE);
+    ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
 }
 
 static BlockJobType stream_job_type = {
include/qemu/ratelimit.h new file +48 -0

48 lines not on this page · raw message

Signature · 1 line
1.7.6.5
author
#8 lore raw

PATCH 7/39 xtensa_lx60: add missing #include "blockdev.h"

In reply to Kevin Wolf

From: Max Filippov <[email protected]>

This should fix the following build failure:

/home/buildbot/slave-public/block_mingw32/build/hw/xtensa_lx60.c: In function 'lx_init':
/home/buildbot/slave-public/block_mingw32/build/hw/xtensa_lx60.c:212: warning: implicit declaration of function 'drive_get' /home/buildbot/slave-public/block_mingw32/build/hw/xtensa_lx60.c:212: warning: nested extern declaration of 'drive_get' /home/buildbot/slave-public/block_mingw32/build/hw/xtensa_lx60.c:212: error: 'IF_PFLASH' undeclared (first use in this function) /home/buildbot/slave-public/block_mingw32/build/hw/xtensa_lx60.c:212: error: (Each undeclared identifier is reported only once /home/buildbot/slave-public/block_mingw32/build/hw/xtensa_lx60.c:212: error: for each function it appears in.) /home/buildbot/slave-public/block_mingw32/build/hw/xtensa_lx60.c:216: error: dereferencing pointer to incomplete type

---
 hw/xtensa_lx60.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
hw/xtensa_lx60.c +1 -0
index b153bfd..152eed9 100644
@@ -34,6 +34,7 @@
 #include "pc.h"
 #include "sysbus.h"
 #include "flash.h"
+#include "blockdev.h"
 #include "xtensa_bootparam.h"
 
 typedef struct LxBoardDesc {
Signature · 1 line
1.7.6.5
author
#9 lore raw

PATCH 8/39 Un-inline fdctrl_init_isa()

In reply to Kevin Wolf

From: Markus Armbruster <[email protected]>

---
 hw/fdc.c      |   20 ++++++++++++++++++++
 hw/fdc.h      |   24 ++----------------------
 hw/ide/piix.c |    3 ++-
 hw/isa.h      |    2 --
 hw/pc_sysfw.c |    1 +
 qemu-common.h |    1 +
 6 files changed, 26 insertions(+), 25 deletions(-)
hw/fdc.c +20 -0
index 30d34e3..bfa4e68 100644
@@ -1888,6 +1888,26 @@ static int fdctrl_connect_drives(FDCtrl *fdctrl)
     return 0;
 }
 
+ISADevice *fdctrl_init_isa(ISABus *bus, DriveInfo **fds)
+{
+    ISADevice *dev;
+
+    dev = isa_try_create(bus, "isa-fdc");
+    if (!dev) {
+        return NULL;
+    }
+
+    if (fds[0]) {
+        qdev_prop_set_drive_nofail(&dev->qdev, "driveA", fds[0]->bdrv);
+    }
+    if (fds[1]) {
+        qdev_prop_set_drive_nofail(&dev->qdev, "driveB", fds[1]->bdrv);
+    }
+    qdev_init_nofail(&dev->qdev);
+
+    return dev;
+}
+
 void fdctrl_init_sysbus(qemu_irq irq, int dma_chann,
                         target_phys_addr_t mmio_base, DriveInfo **fds)
 {
hw/fdc.h +2 -22
index 55a8d73..1b32b17 100644
@@ -1,32 +1,12 @@
 #ifndef HW_FDC_H
 #define HW_FDC_H
 
-#include "isa.h"
-#include "blockdev.h"
+#include "qemu-common.h"
 
 /* fdc.c */
 #define MAX_FD 2
 
-static inline ISADevice *fdctrl_init_isa(ISABus *bus, DriveInfo **fds)
-{
-    ISADevice *dev;
-
-    dev = isa_try_create(bus, "isa-fdc");
-    if (!dev) {
-        return NULL;
-    }
-
-    if (fds[0]) {
-        qdev_prop_set_drive_nofail(&dev->qdev, "driveA", fds[0]->bdrv);
-    }
-    if (fds[1]) {
-        qdev_prop_set_drive_nofail(&dev->qdev, "driveB", fds[1]->bdrv);
-    }
-    qdev_init_nofail(&dev->qdev);
-
-    return dev;
-}
-
+ISADevice *fdctrl_init_isa(ISABus *bus, DriveInfo **fds);
 void fdctrl_init_sysbus(qemu_irq irq, int dma_chann,
                         target_phys_addr_t mmio_base, DriveInfo **fds);
 void sun4m_fdctrl_init(qemu_irq irq, target_phys_addr_t io_base,
hw/ide/piix.c +2 -1
index bcaa400..f5a74c2 100644
@@ -22,11 +22,12 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+
 #include <hw/hw.h>
 #include <hw/pc.h>
 #include <hw/pci.h>
 #include <hw/isa.h>
-#include "block.h"
+#include "blockdev.h"
 #include "sysemu.h"
 #include "dma.h"
 
hw/isa.h +0 -2
index f7bc4b5..6c6fd7f 100644
@@ -9,8 +9,6 @@
 
 #define ISA_NUM_IRQS 16
 
-typedef struct ISADevice ISADevice;
-
 #define TYPE_ISA_DEVICE "isa-device"
 #define ISA_DEVICE(obj) \
      OBJECT_CHECK(ISADevice, (obj), TYPE_ISA_DEVICE)
hw/pc_sysfw.c +1 -0
index f0d7c21..b45f0ac 100644
@@ -23,6 +23,7 @@
  * THE SOFTWARE.
  */
 
+#include "blockdev.h"
 #include "sysbus.h"
 #include "hw.h"
 #include "pc.h"
qemu-common.h +1 -0
index 91e0562..8f87e41 100644
@@ -239,6 +239,7 @@ typedef struct VLANState VLANState;
 typedef struct VLANClientState VLANClientState;
 typedef struct i2c_bus i2c_bus;
 typedef struct ISABus ISABus;
+typedef struct ISADevice ISADevice;
 typedef struct SMBusDevice SMBusDevice;
 typedef struct PCIHostState PCIHostState;
 typedef struct PCIExpressHost PCIExpressHost;
Signature · 1 line
1.7.6.5
author
#10 lore raw

PATCH 9/39 qemu-img check -r for repairing images

In reply to Kevin Wolf

The QED block driver already provides the functionality to not only detect inconsistencies in images, but also fix them. However, this functionality cannot be manually invoked with qemu-img, but the check happens only automatically during bdrv_open().

This adds a -r switch to qemu-img check that allows manual invocation of an image repair.

---
 block.c          |    4 ++--
 block.h          |    7 ++++++-
 block/qcow2.c    |    7 ++++++-
 block/qed.c      |    5 +++--
 block/vdi.c      |    7 ++++++-
 block_int.h      |    3 ++-
 qemu-img-cmds.hx |    4 ++--
 qemu-img.c       |   25 ++++++++++++++++++++++---
 qemu-img.texi    |    7 ++++++-
 9 files changed, 55 insertions(+), 14 deletions(-)
block.c +2 -2
index c07ff39..355ac86 100644
@@ -1222,14 +1222,14 @@ bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
  * free of errors) or -errno when an internal error occurred. The results of the
  * check are stored in res.
  */
-int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
+int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
 {
     if (bs->drv->bdrv_check == NULL) {
         return -ENOTSUP;
     }
 
     memset(res, 0, sizeof(*res));
-    return bs->drv->bdrv_check(bs, res);
+    return bs->drv->bdrv_check(bs, res, fix);
 }
 
 #define COMMIT_BUF_SECTORS 2048
block.h +6 -1
index 799cf48..61b7e8e 100644
@@ -190,7 +190,12 @@ typedef struct BdrvCheckResult {
     BlockFragInfo bfi;
 } BdrvCheckResult;
 
-int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res);
+typedef enum {
+    BDRV_FIX_LEAKS    = 1,
+    BDRV_FIX_ERRORS   = 2,
+} BdrvCheckMode;
+
+int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix);
 
 /* async block I/O */
 typedef void BlockDriverDirtyHandler(BlockDriverState *bs, int64_t sector,
block/qcow2.c +6 -1
index c2e49cd..7797015 100644
@@ -1470,8 +1470,13 @@ static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
 }
 
 
-static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result)
+static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
+                       BdrvCheckMode fix)
 {
+    if (fix) {
+        return -ENOTSUP;
+    }
+
     return qcow2_check_refcounts(bs, result);
 }
 
block/qed.c +3 -2
index 30a31f9..ab59724 100644
@@ -1517,11 +1517,12 @@ static void bdrv_qed_invalidate_cache(BlockDriverState *bs)
     bdrv_qed_open(bs, bs->open_flags);
 }
 
-static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result)
+static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result,
+                          BdrvCheckMode fix)
 {
     BDRVQEDState *s = bs->opaque;
 
-    return qed_check(s, result, false);
+    return qed_check(s, result, !!fix);
 }
 
 static QEMUOptionParameter qed_create_options[] = {
block/vdi.c +6 -1
index 119d3c7..57325d6 100644
@@ -277,7 +277,8 @@ static void vdi_header_print(VdiHeader *header)
 }
 #endif
 
-static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res)
+static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res,
+                     BdrvCheckMode fix)
 {
     /* TODO: additional checks possible. */
     BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
@@ -286,6 +287,10 @@ static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res)
     uint32_t *bmap;
     logout("\n");
 
+    if (fix) {
+        return -ENOTSUP;
+    }
+
     bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t));
     memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
 
block_int.h +2 -1
index 3d4abc6..1fb5352 100644
@@ -241,7 +241,8 @@ struct BlockDriver {
      * Returns 0 for completed check, -errno for internal errors.
      * The check results are stored in result.
      */
-    int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result);
+    int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result,
+        BdrvCheckMode fix);
 
     void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
 
qemu-img-cmds.hx +2 -2
index 49dce7c..39419a0 100644
@@ -10,9 +10,9 @@ STEXI
 ETEXI
 
 DEF("check", img_check,
-    "check [-f fmt] filename")
+    "check [-f fmt] [-r [leaks | all]] filename")
 STEXI
-@item check [-f @var{fmt}] @var{filename}
+@item check [-f @var{fmt}] [-r [leaks | all]] @var{filename}
 ETEXI
 
 DEF("create", img_create,
qemu-img.c +22 -3
index c8a70ff..c45ff62 100644
@@ -85,6 +85,12 @@ static void help(void)
            "  '-S' indicates the consecutive number of bytes that must contain only zeros\n"
            "       for qemu-img to create a sparse image during conversion\n"
            "\n"
+           "Parameters to check subcommand:\n"
+           "  '-r' tries to repair any inconsistencies that are found during the check.\n"
+           "       '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
+           "       kinds of errors, with a higher risk of choosing the wrong fix or\n"
+           "       hiding corruption that has already occured.\n"
+           "\n"
            "Parameters to snapshot subcommand:\n"
            "  'snapshot' is the name of the snapshot to create, apply or delete\n"
            "  '-a' applies a snapshot (revert disk to saved state)\n"
@@ -372,10 +378,12 @@ static int img_check(int argc, char **argv)
     const char *filename, *fmt;
     BlockDriverState *bs;
     BdrvCheckResult result;
+    int fix = 0;
+    int flags = BDRV_O_FLAGS;
 
     fmt = NULL;
     for(;;) {
-        c = getopt(argc, argv, "f:h");
+        c = getopt(argc, argv, "f:hr:");
         if (c == -1) {
             break;
         }
@@ -387,6 +395,17 @@ static int img_check(int argc, char **argv)
         case 'f':
             fmt = optarg;
             break;
+        case 'r':
+            flags |= BDRV_O_RDWR;
+
+            if (!strcmp(optarg, "leaks")) {
+                fix = BDRV_FIX_LEAKS;
+            } else if (!strcmp(optarg, "all")) {
+                fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
+            } else {
+                help();
+            }
+            break;
         }
     }
     if (optind >= argc) {
@@ -394,11 +413,11 @@ static int img_check(int argc, char **argv)
     }
     filename = argv[optind++];
 
-    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS);
+    bs = bdrv_new_open(filename, fmt, flags);
     if (!bs) {
         return 1;
     }
-    ret = bdrv_check(bs, &result);
+    ret = bdrv_check(bs, &result, fix);
 
     if (ret == -ENOTSUP) {
         error_report("This image format does not support checks");
qemu-img.texi +6 -1
index 6fc3c28..5a7b2bb 100644
@@ -70,10 +70,15 @@ lists all snapshots in the given image
 Command description:
 
 @table @option
-@item check [-f @var{fmt}] @var{filename}
+@item check [-f @var{fmt}] [-r [leaks | all]] @var{filename}
 
 Perform a consistency check on the disk image @var{filename}.
 
+If @code{-r} is specified, qemu-img tries to repair any inconsistencies found
+during the check. @code{-r leaks} repairs only cluster leaks, whereas
+@code{-r all} fixes all kinds of errors, with a higher risk of choosing the
+wrong fix or hiding corruption that has already occured.
+
 Only the formats @code{qcow2}, @code{qed} and @code{vdi} support
 consistency checks.
 
Signature · 1 line
1.7.6.5
author
#11 lore raw

PATCH 10/39 qemu-img check: Print fixed clusters and recheck

In reply to Kevin Wolf

When any inconsistencies have been fixed, print the statistics and run another check to make sure everything is correct now.

---
 block.h           |    2 ++
 block/qed-check.c |    2 ++
 qemu-img.c        |   10 ++++++++++
 3 files changed, 14 insertions(+), 0 deletions(-)
block.h +2 -0
index 61b7e8e..f8200eb 100644
@@ -187,6 +187,8 @@ typedef struct BdrvCheckResult {
     int corruptions;
     int leaks;
     int check_errors;
+    int corruptions_fixed;
+    int leaks_fixed;
     BlockFragInfo bfi;
 } BdrvCheckResult;
 
block/qed-check.c +2 -0
index 94327ff..5edf607 100644
@@ -87,6 +87,7 @@ static unsigned int qed_check_l2_table(QEDCheck *check, QEDTable *table)
         if (!qed_check_cluster_offset(s, offset)) {
             if (check->fix) {
                 table->offsets[i] = 0;
+                check->result->corruptions_fixed++;
             } else {
                 check->result->corruptions++;
             }
@@ -127,6 +128,7 @@ static int qed_check_l1_table(QEDCheck *check, QEDTable *table)
             /* Clear invalid offset */
             if (check->fix) {
                 table->offsets[i] = 0;
+                check->result->corruptions_fixed++;
             } else {
                 check->result->corruptions++;
             }
qemu-img.c +10 -0
index c45ff62..9336c86 100644
@@ -425,6 +425,16 @@ static int img_check(int argc, char **argv)
         return 1;
     }
 
+    if (result.corruptions_fixed || result.leaks_fixed) {
+        printf("The following inconsistencies were found and repaired:\n\n"
+               "    %d leaked clusters\n"
+               "    %d corruptions\n\n"
+               "Double checking the fixed image now...\n",
+               result.leaks_fixed,
+               result.corruptions_fixed);
+        ret = bdrv_check(bs, &result, 0);
+    }
+
     if (!(result.corruptions || result.leaks || result.check_errors)) {
         printf("No errors were found on the image.\n");
     } else {
Signature · 1 line
1.7.6.5
author
#12 lore raw

PATCH 11/39 qcow2: Support for fixing refcount inconsistencies

In reply to Kevin Wolf
---
 block/qcow2-refcount.c |   43 ++++++++++++++++++++++++++++++++++---------
 block/qcow2.c          |    6 +-----
 block/qcow2.h          |    3 ++-
 3 files changed, 37 insertions(+), 15 deletions(-)
block/qcow2-refcount.c +34 -9
index 443c021..5d6ea72 100644
@@ -1122,11 +1122,12 @@ fail:
  * Returns 0 if no errors are found, the number of errors in case the image is
  * detected as corrupted, and -errno when an internal error occurred.
  */
-int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res)
+int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
+                          BdrvCheckMode fix)
 {
     BDRVQcowState *s = bs->opaque;
-    int64_t size;
-    int nb_clusters, refcount1, refcount2, i;
+    int64_t size, i;
+    int nb_clusters, refcount1, refcount2;
     QCowSnapshot *sn;
     uint16_t *refcount_table;
     int ret;
@@ -1170,14 +1171,15 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res)
 
         /* Refcount blocks are cluster aligned */
         if (offset & (s->cluster_size - 1)) {
-            fprintf(stderr, "ERROR refcount block %d is not "
+            fprintf(stderr, "ERROR refcount block %" PRId64 " is not "
                 "cluster aligned; refcount table entry corrupted\n", i);
             res->corruptions++;
             continue;
         }
 
         if (cluster >= nb_clusters) {
-            fprintf(stderr, "ERROR refcount block %d is outside image\n", i);
+            fprintf(stderr, "ERROR refcount block %" PRId64
+                    " is outside image\n", i);
             res->corruptions++;
             continue;
         }
@@ -1186,7 +1188,8 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res)
             inc_refcounts(bs, res, refcount_table, nb_clusters,
                 offset, s->cluster_size);
             if (refcount_table[cluster] != 1) {
-                fprintf(stderr, "ERROR refcount block %d refcount=%d\n",
+                fprintf(stderr, "ERROR refcount block %" PRId64
+                    " refcount=%d\n",
                     i, refcount_table[cluster]);
                 res->corruptions++;
             }
@@ -1197,7 +1200,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res)
     for(i = 0; i < nb_clusters; i++) {
         refcount1 = get_refcount(bs, i);
         if (refcount1 < 0) {
-            fprintf(stderr, "Can't get refcount for cluster %d: %s\n",
+            fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
                 i, strerror(-refcount1));
             res->check_errors++;
             continue;
@@ -1205,9 +1208,31 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res)
 
         refcount2 = refcount_table[i];
         if (refcount1 != refcount2) {
-            fprintf(stderr, "%s cluster %d refcount=%d reference=%d\n",
-                   refcount1 < refcount2 ? "ERROR" : "Leaked",
+
+            /* Check if we're allowed to fix the mismatch */
+            int *num_fixed = NULL;
+            if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) {
+                num_fixed = &res->leaks_fixed;
+            } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) {
+                num_fixed = &res->corruptions_fixed;
+            }
+
+            fprintf(stderr, "%s cluster %" PRId64 " refcount=%d reference=%d\n",
+                   num_fixed != NULL     ? "Repairing" :
+                   refcount1 < refcount2 ? "ERROR" :
+                                           "Leaked",
                    i, refcount1, refcount2);
+
+            if (num_fixed) {
+                ret = update_refcount(bs, i << s->cluster_bits, 1,
+                                      refcount2 - refcount1);
+                if (ret >= 0) {
+                    (*num_fixed)++;
+                    continue;
+                }
+            }
+
+            /* And if we couldn't, print an error */
             if (refcount1 < refcount2) {
                 res->corruptions++;
             } else {
block/qcow2.c +1 -5
index 7797015..d66de58 100644
@@ -1473,11 +1473,7 @@ static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
 static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
                        BdrvCheckMode fix)
 {
-    if (fix) {
-        return -ENOTSUP;
-    }
-
-    return qcow2_check_refcounts(bs, result);
+    return qcow2_check_refcounts(bs, result, fix);
 }
 
 #if 0
block/qcow2.h +2 -1
index 93567f6..c6e7237 100644
@@ -261,7 +261,8 @@ void qcow2_free_any_clusters(BlockDriverState *bs,
 int qcow2_update_snapshot_refcount(BlockDriverState *bs,
     int64_t l1_table_offset, int l1_size, int addend);
 
-int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res);
+int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
+                          BdrvCheckMode fix);
 
 /* qcow2-cluster.c functions */
 int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);
Signature · 1 line
1.7.6.5
author
#13 lore raw

PATCH 12/39 rbd: hook up cache options

In reply to Kevin Wolf

From: Josh Durgin <[email protected]>

Writeback caching was added in Ceph 0.46, and writethrough will be in 0.47. These are controlled by general config options, so there's no need to check for librbd version.

---
 block/rbd.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
block/rbd.c +19 -0
index 1280d66..eebc334 100644
@@ -476,6 +476,25 @@ static int qemu_rbd_open(BlockDriverState *bs, const char *filename, int flags)
         s->snap = g_strdup(snap_buf);
     }
 
+    /*
+     * Fallback to more conservative semantics if setting cache
+     * options fails. Ignore errors from setting rbd_cache because the
+     * only possible error is that the option does not exist, and
+     * librbd defaults to no caching. If write through caching cannot
+     * be set up, fall back to no caching.
+     */
+    if (flags & BDRV_O_NOCACHE) {
+        rados_conf_set(s->cluster, "rbd_cache", "false");
+    } else {
+        rados_conf_set(s->cluster, "rbd_cache", "true");
+        if (!(flags & BDRV_O_CACHE_WB)) {
+            r = rados_conf_set(s->cluster, "rbd_cache_max_dirty", "0");
+            if (r < 0) {
+                rados_conf_set(s->cluster, "rbd_cache", "false");
+            }
+        }
+    }
+
     if (strstr(conf, "conf=") == NULL) {
         /* try default location, but ignore failure */
         rados_conf_read_file(s->cluster, NULL);
Signature · 1 line
1.7.6.5
author
#14 lore raw

PATCH 13/39 sheepdog: add coroutine_fn markers to coroutine functions

In reply to Kevin Wolf

From: MORITA Kazutaka <[email protected]>

---
 block/sheepdog.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
block/sheepdog.c +5 -4
index f46ca8f..8877f45 100644
@@ -522,8 +522,8 @@ static int send_req(int sockfd, SheepdogReq *hdr, void *data,
     return ret;
 }
 
-static int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
-                       unsigned int *wlen)
+static coroutine_fn int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
+                                    unsigned int *wlen)
 {
     int ret;
 
@@ -540,6 +540,7 @@ static int send_co_req(int sockfd, SheepdogReq *hdr, void *data,
 
     return ret;
 }
+
 static int do_req(int sockfd, SheepdogReq *hdr, void *data,
                   unsigned int *wlen, unsigned int *rlen)
 {
@@ -576,8 +577,8 @@ out:
     return ret;
 }
 
-static int do_co_req(int sockfd, SheepdogReq *hdr, void *data,
-                     unsigned int *wlen, unsigned int *rlen)
+static coroutine_fn int do_co_req(int sockfd, SheepdogReq *hdr, void *data,
+                                  unsigned int *wlen, unsigned int *rlen)
 {
     int ret;
 
Signature · 1 line
1.7.6.5
author
#15 lore raw

PATCH 14/39 block: Simplify how drive_init() computes default ID

In reply to Kevin Wolf

From: Markus Armbruster <[email protected]>

---
 blockdev.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
blockdev.c +2 -5
index 622ecba..9e0a72a 100644
@@ -278,7 +278,6 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
 {
     const char *buf;
     const char *file = NULL;
-    char devname[128];
     const char *serial;
     const char *mediastr = "";
     BlockInterfaceType type;
@@ -318,7 +317,6 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
     serial = qemu_opt_get(opts, "serial");
 
     if ((buf = qemu_opt_get(opts, "if")) != NULL) {
-        pstrcpy(devname, sizeof(devname), buf);
         for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++)
             ;
         if (type == IF_COUNT) {
@@ -327,7 +325,6 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
 	}
     } else {
         type = default_to_scsi ? IF_SCSI : IF_IDE;
-        pstrcpy(devname, sizeof(devname), if_name[type]);
     }
 
     max_devs = if_max_devs[type];
@@ -523,10 +520,10 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
             mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
         if (max_devs)
             snprintf(dinfo->id, 32, "%s%i%s%i",
-                     devname, bus_id, mediastr, unit_id);
+                     if_name[type], bus_id, mediastr, unit_id);
         else
             snprintf(dinfo->id, 32, "%s%s%i",
-                     devname, mediastr, unit_id);
+                     if_name[type], mediastr, unit_id);
     }
     dinfo->bdrv = bdrv_new(dinfo->id);
     dinfo->devaddr = devaddr;
Signature · 1 line
1.7.6.5
author
#16 lore raw

PATCH 15/39 Prevent disk data loss when closing qemu

In reply to Kevin Wolf

From: Pavel Dovgaluk <[email protected]>

Prevent disk data loss when closing qemu console window under Windows 7.

v3. Comment for Sleep() parameter was updated.

---
 os-win32.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
os-win32.c +7 -1
index ad76370..13892ba 100644
@@ -57,7 +57,13 @@ int setenv(const char *name, const char *value, int overwrite)
 
 static BOOL WINAPI qemu_ctrl_handler(DWORD type)
 {
-    exit(STATUS_CONTROL_C_EXIT);
+    qemu_system_shutdown_request();
+    /* Windows 7 kills application when the function returns.
+       Sleep here to give QEMU a try for closing.
+       Sleep period is 10000ms because Windows kills the program
+       after 10 seconds anyway. */
+    Sleep(10000);
+
     return TRUE;
 }
 
Signature · 1 line
1.7.6.5
author
#17 lore raw

PATCH 16/39 block: New bdrv_get_flags()

In reply to Kevin Wolf

From: Markus Armbruster <[email protected]>

---
 block.c |    5 +++++
 block.h |    1 +
 2 files changed, 6 insertions(+), 0 deletions(-)
block.c +5 -0
index 355ac86..85ef6af 100644
@@ -2466,6 +2466,11 @@ const char *bdrv_get_device_name(BlockDriverState *bs)
     return bs->device_name;
 }
 
+int bdrv_get_flags(BlockDriverState *bs)
+{
+    return bs->open_flags;
+}
+
 void bdrv_flush_all(void)
 {
     BlockDriverState *bs;
block.h +1 -0
index f8200eb..574981d 100644
@@ -307,6 +307,7 @@ int bdrv_query_missing_keys(void);
 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
                          void *opaque);
 const char *bdrv_get_device_name(BlockDriverState *bs);
+int bdrv_get_flags(BlockDriverState *bs);
 int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
                           const uint8_t *buf, int nb_sectors);
 int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
Signature · 1 line
1.7.6.5
author
#18 lore raw

PATCH 17/39 scsi-disk: Don't peek behind the BlockDriverState abstraction

In reply to Kevin Wolf

From: Markus Armbruster <[email protected]>

Use the appropriate interface instead.

---
 hw/scsi-disk.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
hw/scsi-disk.c +1 -2
index 1691491..9197b08 100644
@@ -34,7 +34,6 @@ do { printf("scsi-disk: " fmt , ## __VA_ARGS__); } while (0)
 #include "scsi-defs.h"
 #include "sysemu.h"
 #include "blockdev.h"
-#include "block_int.h"
 #include "dma.h"
 
 #ifdef __linux
@@ -1889,7 +1888,7 @@ static SCSIRequest *scsi_block_new_request(SCSIDevice *d, uint32_t tag,
 	 * ones (such as WRITE SAME or EXTENDED COPY, etc.).  So, without
 	 * O_DIRECT everything must go through SG_IO.
          */
-        if (!(s->qdev.conf.bs->open_flags & BDRV_O_NOCACHE)) {
+        if (bdrv_get_flags(s->qdev.conf.bs) & BDRV_O_NOCACHE) {
             break;
         }
 
Signature · 1 line
1.7.6.5
author
#19 lore raw

PATCH 18/39 qemu-iotests: fill streaming test image with data

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

The TestStreamStop test case is racy; if the job completes before we can cancel it, it fails. If we remove the sleep the job will be canceled before it has even started, and the test succeeds but it is also not testing anything interesting.

But if the image is left sparse, then the job has really nothing to do. For qcow2 it will read one L2-table, for raw it will issue a bunch of ioctls. This also falls under "not testing anything interesting", and this may be happening right now (depending on the filesystem) since the file protocol got an is_allocated method.

Filling the test image with data ensures that the test covers the intended case. It also slows down the test, which will be particularly important after the next patch.

---
 tests/qemu-iotests/030 |   13 ++++++++++++-
 1 files changed, 12 insertions(+), 1 deletions(-)
tests/qemu-iotests/030 +12 -1
index eb7bf99..4ab7d62 100755
@@ -21,6 +21,7 @@
 import os
 import iotests
 from iotests import qemu_img, qemu_io
+import struct
 
 backing_img = os.path.join(iotests.test_dir, 'backing.img')
 mid_img = os.path.join(iotests.test_dir, 'mid.img')
@@ -48,11 +49,21 @@ class ImageStreamingTestCase(iotests.QMPTestCase):
 
         self.assert_no_active_streams()
 
+    def create_image(self, name, size):
+        file = open(name, 'w')
+        i = 0
+        while i < size:
+            sector = struct.pack('>l504xl', i / 512, i / 512)
+            file.write(sector)
+            i = i + 512
+        file.close()
+
+
 class TestSingleDrive(ImageStreamingTestCase):
     image_len = 1 * 1024 * 1024 # MB
 
     def setUp(self):
-        qemu_img('create', backing_img, str(TestSingleDrive.image_len))
+        self.create_image(backing_img, TestSingleDrive.image_len)
         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
         self.vm = iotests.VM().add_drive(test_img)
Signature · 1 line
1.7.6.5
author
#20 lore raw

PATCH 19/39 qemu-iotests: start vms in qtest mode

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

This way, they will not execute any VM code at all. However, right now the cancellation test is "relying" on being slowed down by TCG executing BIOS code. So, change the timeouts.

---
 tests/qemu-iotests/030        |    2 +-
 tests/qemu-iotests/iotests.py |    4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
tests/qemu-iotests/030 +1 -1
index 4ab7d62..cc671dd 100755
@@ -147,7 +147,7 @@ class TestStreamStop(ImageStreamingTestCase):
         result = self.vm.qmp('block-stream', device='drive0')
         self.assert_qmp(result, 'return', {})
 
-        time.sleep(1)
+        time.sleep(0.1)
         events = self.vm.get_qmp_events(wait=False)
         self.assertEqual(events, [], 'unexpected QMP event: %s' % events)
 
tests/qemu-iotests/iotests.py +3 -1
index e27b40e..e05b1d6 100644
@@ -54,7 +54,9 @@ class VM(object):
         self._qemu_log_path = os.path.join(test_dir, 'qemu-log.%d' % os.getpid())
         self._args = qemu_args + ['-chardev',
                      'socket,id=mon,path=' + self._monitor_path,
-                     '-mon', 'chardev=mon,mode=control', '-nographic']
+                     '-mon', 'chardev=mon,mode=control',
+                     '-qtest', 'stdio', '-machine', 'accel=qtest',
+                     '-display', 'none', '-vga', 'none']
         self._num_drives = 0
 
     def add_drive(self, path, opts=''):
Signature · 1 line
1.7.6.5
author
#21 lore raw

PATCH 20/39 block: flush in writethrough mode after writes

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

We want to make the formats handle their own flushes autonomously, while keeping for guests the ability to use a writethrough cache. Since formats will write metadata via bs->file, bdrv_co_do_writev is the only place where we need to add a flush.

---
 block.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
block.c +9 -2
index 85ef6af..7538112 100644
@@ -1758,8 +1758,8 @@ int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
         return ret;
     }
 
-    /* No flush needed for cache modes that use O_DSYNC */
-    if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) {
+    /* No flush needed for cache modes that already do it */
+    if (bs->enable_write_cache) {
         bdrv_flush(bs);
     }
 
@@ -1808,6 +1808,9 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs,
         ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num,
                                       cluster_nb_sectors);
     } else {
+        /* This does not change the data on the disk, it is not necessary
+         * to flush even in cache=writethrough mode.
+         */
         ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors,
                                   &bounce_qiov);
     }
@@ -1977,6 +1980,10 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
         ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
     }
 
+    if (ret == 0 && !bs->enable_write_cache) {
+        ret = bdrv_co_flush(bs);
+    }
+
     if (bs->dirty_bitmap) {
         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
     }
Signature · 1 line
1.7.6.5
author
#22 lore raw

PATCH 21/39 savevm: flush after saving vm state

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

Writing vm state uses bdrv_pwrite, so it will automatically get flushes in writethrough mode. But doing a flush at the end in writeback mode is probably a good idea anyway.

---
 savevm.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
savevm.c +1 -1
index 2d18bab..2b6833d 100644
@@ -400,7 +400,7 @@ static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
 
 static int bdrv_fclose(void *opaque)
 {
-    return 0;
+    return bdrv_flush(opaque);
 }
 
 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
Signature · 1 line
1.7.6.5
author
#23 lore raw

PATCH 22/39 block: copy enable_write_cache in bdrv_append

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

Because the guest will be able to flip enable_write_cache, the actual state may not match what is used to open the new snapshot.

---
 block.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
block.c +2 -0
index 7538112..9bff401 100644
@@ -1000,6 +1000,8 @@ void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
     tmp.buffer_alignment  = bs_top->buffer_alignment;
     tmp.copy_on_read      = bs_top->copy_on_read;
 
+    tmp.enable_write_cache = bs_top->enable_write_cache;
+
     /* i/o timing parameters */
     tmp.slice_time        = bs_top->slice_time;
     tmp.slice_start       = bs_top->slice_start;
Signature · 1 line
1.7.6.5
author
#24 lore raw

PATCH 23/39 block: add bdrv_set_enable_write_cache

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

---
 block.c |    5 +++++
 block.h |    1 +
 2 files changed, 6 insertions(+), 0 deletions(-)
block.c +5 -0
index 9bff401..e4396a6 100644
@@ -2380,6 +2380,11 @@ int bdrv_enable_write_cache(BlockDriverState *bs)
     return bs->enable_write_cache;
 }
 
+void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce)
+{
+    bs->enable_write_cache = wce;
+}
+
 int bdrv_is_encrypted(BlockDriverState *bs)
 {
     if (bs->backing_hd && bs->backing_hd->encrypted)
block.h +1 -0
index 574981d..43bfd99 100644
@@ -291,6 +291,7 @@ BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read);
 int bdrv_is_read_only(BlockDriverState *bs);
 int bdrv_is_sg(BlockDriverState *bs);
 int bdrv_enable_write_cache(BlockDriverState *bs);
+void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce);
 int bdrv_is_inserted(BlockDriverState *bs);
 int bdrv_media_changed(BlockDriverState *bs);
 void bdrv_lock_medium(BlockDriverState *bs, bool locked);
Signature · 1 line
1.7.6.5
author
#25 lore raw

PATCH 24/39 block: always open drivers in writeback mode

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

Formats are entirely in charge of flushes for metadata writes. For guest-initiated writes, a writethrough cache is faked in the block layer. So we can always open in writeback mode.

---
 block.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
block.c +2 -1
index e4396a6..48528fd 100644
@@ -649,12 +649,13 @@ static int bdrv_open_common(BlockDriverState *bs, const char *filename,
     bs->opaque = g_malloc0(drv->instance_size);
 
     bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
+    open_flags = flags | BDRV_O_CACHE_WB;
 
     /*
      * Clear flags that are internal to the block layer before opening the
      * image.
      */
-    open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
+    open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
 
     /*
      * Snapshots should be writable.
Signature · 1 line
1.7.6.5
author
#26 lore raw

PATCH 25/39 ide: support enable/disable write cache

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

Enabling or disabling the write cache is done with the SET FEATURES command. The command can be issued with sg_sat_set_features from sg3-utils.

---
 hw/ide/core.c |   21 ++++++++++++++++++---
 1 files changed, 18 insertions(+), 3 deletions(-)
hw/ide/core.c +18 -3
index 9785d5f..f28229a 100644
@@ -1047,6 +1047,7 @@ static bool ide_cmd_permitted(IDEState *s, uint32_t cmd)
 
 void ide_exec_cmd(IDEBus *bus, uint32_t val)
 {
+    uint16_t *identify_data;
     IDEState *s;
     int n;
     int lba48 = 0;
@@ -1231,10 +1232,21 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
             goto abort_cmd;
         /* XXX: valid for CDROM ? */
         switch(s->feature) {
-        case 0xcc: /* reverting to power-on defaults enable */
-        case 0x66: /* reverting to power-on defaults disable */
         case 0x02: /* write cache enable */
+            bdrv_set_enable_write_cache(s->bs, true);
+            identify_data = (uint16_t *)s->identify_data;
+            put_le16(identify_data + 85, (1 << 14) | (1 << 5) | 1);
+            s->status = READY_STAT | SEEK_STAT;
+            ide_set_irq(s->bus);
+            break;
         case 0x82: /* write cache disable */
+            bdrv_set_enable_write_cache(s->bs, false);
+            identify_data = (uint16_t *)s->identify_data;
+            put_le16(identify_data + 85, (1 << 14) | 1);
+            ide_flush_cache(s);
+            break;
+        case 0xcc: /* reverting to power-on defaults enable */
+        case 0x66: /* reverting to power-on defaults disable */
         case 0xaa: /* read look-ahead enable */
         case 0x55: /* read look-ahead disable */
         case 0x05: /* set advanced power management mode */
@@ -1250,7 +1262,7 @@ void ide_exec_cmd(IDEBus *bus, uint32_t val)
             break;
         case 0x03: { /* set transfer mode */
 		uint8_t val = s->nsector & 0x07;
-            uint16_t *identify_data = (uint16_t *)s->identify_data;
+		identify_data = (uint16_t *)s->identify_data;
 
 		switch (s->nsector >> 3) {
 		case 0x00: /* pio default */
@@ -2146,6 +2158,9 @@ static int ide_drive_post_load(void *opaque, int version_id)
             s->cdrom_changed = 1;
         }
     }
+    if (s->identify_set) {
+        bdrv_set_enable_write_cache(s->bs, !!(s->identify_data[85] & (1 << 5)));
+    }
     return 0;
 }
 
Signature · 1 line
1.7.6.5
author
#27 lore raw

PATCH 26/39 qcow2: always operate caches in writeback mode

In reply to Kevin Wolf

From: Paolo Bonzini <[email protected]>

Writethrough does not need special-casing anymore in the qcow2 caches. The block layer adds flushes after every guest-initiated data write, and these will also flush the qcow2 caches to the OS.

---
 block/qcow2-cache.c    |   25 ++-----------------------
 block/qcow2-refcount.c |   12 ------------
 block/qcow2.c          |    7 ++-----
 block/qcow2.h          |    5 +----
 4 files changed, 5 insertions(+), 44 deletions(-)
block/qcow2-cache.c +2 -23
index 710d4b1..2d4322a 100644
@@ -40,11 +40,9 @@ struct Qcow2Cache {
     struct Qcow2Cache*      depends;
     int                     size;
     bool                    depends_on_flush;
-    bool                    writethrough;
 };
 
-Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
-    bool writethrough)
+Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables)
 {
     BDRVQcowState *s = bs->opaque;
     Qcow2Cache *c;
@@ -53,7 +51,6 @@ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
     c = g_malloc0(sizeof(*c));
     c->size = num_tables;
     c->entries = g_malloc0(sizeof(*c->entries) * num_tables);
-    c->writethrough = writethrough;
 
     for (i = 0; i < c->size; i++) {
         c->entries[i].table = qemu_blockalign(bs, s->cluster_size);
@@ -307,12 +304,7 @@ found:
     *table = NULL;
 
     assert(c->entries[i].ref >= 0);
-
-    if (c->writethrough) {
-        return qcow2_cache_entry_flush(bs, c, i);
-    } else {
-        return 0;
-    }
+    return 0;
 }
 
 void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table)
@@ -329,16 +321,3 @@ void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table)
 found:
     c->entries[i].dirty = true;
 }
-
-bool qcow2_cache_set_writethrough(BlockDriverState *bs, Qcow2Cache *c,
-    bool enable)
-{
-    bool old = c->writethrough;
-
-    if (!old && enable) {
-        qcow2_cache_flush(bs, c);
-    }
-
-    c->writethrough = enable;
-    return old;
-}
block/qcow2-refcount.c +0 -12
index 5d6ea72..66f3915 100644
@@ -726,13 +726,6 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
     int64_t old_offset, old_l2_offset;
     int i, j, l1_modified = 0, nb_csectors, refcount;
     int ret;
-    bool old_l2_writethrough, old_refcount_writethrough;
-
-    /* Switch caches to writeback mode during update */
-    old_l2_writethrough =
-        qcow2_cache_set_writethrough(bs, s->l2_table_cache, false);
-    old_refcount_writethrough =
-        qcow2_cache_set_writethrough(bs, s->refcount_block_cache, false);
 
     l2_table = NULL;
     l1_table = NULL;
@@ -856,11 +849,6 @@ fail:
         qcow2_cache_put(bs, s->l2_table_cache, (void**) &l2_table);
     }
 
-    /* Enable writethrough cache mode again */
-    qcow2_cache_set_writethrough(bs, s->l2_table_cache, old_l2_writethrough);
-    qcow2_cache_set_writethrough(bs, s->refcount_block_cache,
-        old_refcount_writethrough);
-
     /* Update L1 only if it isn't deleted anyway (addend = -1) */
     if (addend >= 0 && l1_modified) {
         for(i = 0; i < l1_size; i++)
block/qcow2.c +2 -5
index d66de58..57fd43d 100644
@@ -220,7 +220,6 @@ static int qcow2_open(BlockDriverState *bs, int flags)
     int len, i, ret = 0;
     QCowHeader header;
     uint64_t ext_end;
-    bool writethrough;
 
     ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
     if (ret < 0) {
@@ -367,10 +366,8 @@ static int qcow2_open(BlockDriverState *bs, int flags)
     }
 
     /* alloc L2 table/refcount block cache */
-    writethrough = ((flags & BDRV_O_CACHE_WB) == 0);
-    s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE, writethrough);
-    s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE,
-        writethrough);
+    s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE);
+    s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE);
 
     s->cluster_cache = g_malloc(s->cluster_size);
     /* one more sector for decompressed data alignment */
block/qcow2.h +1 -4
index c6e7237..455b6d7 100644
@@ -297,11 +297,8 @@ void qcow2_free_snapshots(BlockDriverState *bs);
 int qcow2_read_snapshots(BlockDriverState *bs);
 
 /* qcow2-cache.c functions */
-Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables,
-    bool writethrough);
+Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
 int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
-bool qcow2_cache_set_writethrough(BlockDriverState *bs, Qcow2Cache *c,
-    bool enable);
 
 void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
 int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
Signature · 1 line
1.7.6.5
author
#28 lore raw

PATCH 27/39 qcow2: Simplify calculation for COW area at the end

In reply to Kevin Wolf

copy_sectors() always uses the sum (cluster_offset + n_start) or (start_sect + n_start), so if some value is added to both cluster_offset and start_sect, and subtracted from n_start, it's cancelled out anyway.

---
 block/qcow2-cluster.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
block/qcow2-cluster.c +2 -3
index 4eb5ba7..98fba71 100644
@@ -642,11 +642,10 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
     }
 
     if (m->nb_available & (s->cluster_sectors - 1)) {
-        uint64_t end = m->nb_available & ~(uint64_t)(s->cluster_sectors - 1);
         cow = true;
         qemu_co_mutex_unlock(&s->lock);
-        ret = copy_sectors(bs, start_sect + end, cluster_offset + (end << 9),
-                m->nb_available - end, s->cluster_sectors);
+        ret = copy_sectors(bs, start_sect, cluster_offset, m->nb_available,
+                           align_offset(m->nb_available, s->cluster_sectors));
         qemu_co_mutex_lock(&s->lock);
         if (ret < 0)
             goto err;
Signature · 1 line
1.7.6.5
author
#29 lore raw

PATCH 28/39 qcow2: Fix avail_sectors in cluster allocation code

In reply to Kevin Wolf

avail_sectors should really be the number of sectors from the start of the allocation, not from the start of the write request.

We're lucky enough that this mistake didn't cause any real bug. avail_sectors is only used in the intialiser of QCowL2Meta:

.nb_available = MIN(requested_sectors, avail_sectors),

m->nb_available in turn is only used for COW at the end of the allocation. A COW occurs only if the request wasn't cluster aligned, which in turn would imply that requested_sectors was less than avail_sectors (both in the original and in the fixed version). In this case avail_sectors is ignored and therefore the mistake doesn't cause any misbehaviour.

---
 block/qcow2-cluster.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
block/qcow2-cluster.c +9 -1
index 98fba71..d7e0e19 100644
@@ -947,8 +947,16 @@ again:
 
         /* save info needed for meta data update */
         if (nb_clusters > 0) {
+            /*
+             * requested_sectors: Number of sectors from the start of the first
+             * newly allocated cluster to the end of the (possibly shortened
+             * before) write request.
+             *
+             * avail_sectors: Number of sectors from the start of the first
+             * newly allocated to the end of the last newly allocated cluster.
+             */
             int requested_sectors = n_end - keep_clusters * s->cluster_sectors;
-            int avail_sectors = (keep_clusters + nb_clusters)
+            int avail_sectors = nb_clusters
                                 << (s->cluster_bits - BDRV_SECTOR_BITS);
 
             *m = (QCowL2Meta) {
Signature · 1 line
1.7.6.5
#30 lore raw

[Qemu-devel] [BUG] qemu-1.1.2 [FIXED-BY] qcow2: Fix avail_sectors in cluster allocation code

Hello Kevin, hello Michael, hello *,

we noticed a data corruption bug in qemu-1.1.2, which will be shipped by Debian and our own Debian based distibution.
The corruption mostly manifests while installing large Debian package files and seems to be reladed to memory preasure: As long as the file is still in the page cache, everything looks fine, but when the file is re-read from the virtual hard disk using a qcow2 file backed by another qcow2 file, the file is corrupted: dpkg complains that the .tar.gz file inside the Debian archive file is corrupted and the md5sum no longer matches.

I tracked this down using "git bisect" to your patch attached below, which fixed this bug, so everything is fine with qemu-kvm-1.2.0.
From my reading this seems to explain our problems, since during my own testing during development I never used backing chains and the problem only showed up when my collegues started using qemu-kvm-1.1.2 with their VMs using backing chains.

@Kevin: Do you thinks that's a valid explanation and your patch should fix that problem?
I'd like to get your expertise before filing a bug with Debian and asking Michael to include that patch with his next stable update for 1.1.

Thanks in advance.

Sincerely
Philipp

Signature · 4 lines
Philipp Hahn           Open Source Software Engineer      [email protected]
Univention GmbH        be open.                       fon: +49 421 22 232- 0
Mary-Somerville-Str.1  D-28359 Bremen                 fax: +49 421 22 232-99
                                                   http://www.univention.de/

avail_sectors should really be the number of sectors from the start of the allocation, not from the start of the write request.

We're lucky enough that this mistake didn't cause any real bug. avail_sectors is only used in the intialiser of QCowL2Meta:

.nb_available = MIN(requested_sectors, avail_sectors),

m->nb_available in turn is only used for COW at the end of the allocation. A COW occurs only if the request wasn't cluster aligned, which in turn would imply that requested_sectors was less than avail_sectors (both in the original and in the fixed version). In this case avail_sectors is ignored and therefore the mistake doesn't cause any misbehaviour.

---
 block/qcow2-cluster.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
block/qcow2-cluster.c +9 -1
index 98fba71..d7e0e19 100644
@@ -947,8 +947,16 @@ again:
 
         /* save info needed for meta data update */
         if (nb_clusters > 0) {
+            /*
+             * requested_sectors: Number of sectors from the start of the first
+             * newly allocated cluster to the end of the (possibly shortened
+             * before) write request.
+             *
+             * avail_sectors: Number of sectors from the start of the first
+             * newly allocated to the end of the last newly allocated cluster.
+             */
             int requested_sectors = n_end - keep_clusters * s->cluster_sectors;
-            int avail_sectors = (keep_clusters + nb_clusters)
+            int avail_sectors = nb_clusters
                                 << (s->cluster_bits - BDRV_SECTOR_BITS);
 
             *m = (QCowL2Meta) {
Signature · 1 line
1.7.6.5
author
#31 lore raw

Re: [Qemu-devel] [BUG] qemu-1.1.2 [FIXED-BY] qcow2: Fix avail_sectors in cluster allocation code

Hi Philipp,

Am 12.12.2012 14:25, schrieb Philipp Hahn:

Quoted 18 lines

Hello Kevin, hello Michael, hello *,

we noticed a data corruption bug in qemu-1.1.2, which will be shipped by Debian and our own Debian based distibution.
The corruption mostly manifests while installing large Debian package files and seems to be reladed to memory preasure: As long as the file is still in the page cache, everything looks fine, but when the file is re-read from the virtual hard disk using a qcow2 file backed by another qcow2 file, the file is corrupted: dpkg complains that the .tar.gz file inside the Debian archive file is corrupted and the md5sum no longer matches.

I tracked this down using "git bisect" to your patch attached below, which fixed this bug, so everything is fine with qemu-kvm-1.2.0.
From my reading this seems to explain our problems, since during my own testing during development I never used backing chains and the problem only showed up when my collegues started using qemu-kvm-1.1.2 with their VMs using backing chains.

Quoted 4 lines

@Kevin: Do you thinks that's a valid explanation and your patch should fix that problem?
I'd like to get your expertise before filing a bug with Debian and asking Michael to include that patch with his next stable update for 1.1.

As you can see in the commit message of that patch I was convinced that no bug did exist in practice and this was only dangerous with respect to future changes. Therefore my first question is if you're using an unmodified upstream qemu or if some backported patches are applied to it? If it's indeed unmodified, we should probably review the code once again to understand why it makes a difference.

In any case, this is the cluster allocation code. It's probably not related to rereading things from disk, but rather to the writeout of the page cache.

Kevin

#32 lore raw

Re: [Qemu-devel] [BUG] qemu-1.1.2 [FIXED-BY] qcow2: Fix avail_sectors in cluster allocation code

Hello Kevin,

Am Mittwoch 12 Dezember 2012 14:41:49 schrieb Kevin Wolf:

Quoted 6 lines

As you can see in the commit message of that patch I was convinced that no bug did exist in practice and this was only dangerous with respect to future changes. Therefore my first question is if you're using an unmodified upstream qemu or if some backported patches are applied to it? If it's indeed unmodified, we should probably review the code once again to understand why it makes a difference.

This were all unmodified versions directly from git between "qemu-kvm-1.1.0" and "qemu-kvm-1.2.0"

"git checkout b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c" works, "git checkout b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c~1" is broken. "git checkout qemu-kvm-1.1.2" is broken,
"git checkout qemu-kvm-1.1.2 ; git cherry-pick b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c" works

Quoted 3 lines

In any case, this is the cluster allocation code. It's probably not related to rereading things from disk, but rather to the writeout of the page cache.

Yes, the problem is probably write related. But as the write "doens't explode with some spectacular error", I only notice the error on the following read by comparing md5 sums.
I just re-checked it: After a reboot the md5sums are still invalid, so I guess the data is corrupted on writeout.

Sincerely
Philipp

Signature · 4 lines
Philipp Hahn           Open Source Software Engineer      [email protected]
Univention GmbH        be open.                       fon: +49 421 22 232- 0
Mary-Somerville-Str.1  D-28359 Bremen                 fax: +49 421 22 232-99
                                                   http://www.univention.de/
author
#33 lore raw

Re: [Qemu-devel] [BUG] qemu-1.1.2 [FIXED-BY] qcow2: Fix avail_sectors in cluster allocation code

Am 12.12.2012 15:09, schrieb Philipp Hahn:

Quoted 10 lines

Hello Kevin,

Am Mittwoch 12 Dezember 2012 14:41:49 schrieb Kevin Wolf:

Quoted 6 lines

As you can see in the commit message of that patch I was convinced that no bug did exist in practice and this was only dangerous with respect to future changes. Therefore my first question is if you're using an unmodified upstream qemu or if some backported patches are applied to it? If it's indeed unmodified, we should probably review the code once again to understand why it makes a difference.

Quoted 8 lines

This were all unmodified versions directly from git between "qemu-kvm-1.1.0" and "qemu-kvm-1.2.0"

"git checkout b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c" works, "git checkout b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c~1" is broken. "git checkout qemu-kvm-1.1.2" is broken,
"git checkout qemu-kvm-1.1.2 ; git cherry-pick b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c" works

Ok, thanks for clarifying. Then I must have missed some interesting case while doing the patch.

Ideally we would find a sequence of qemu-io commands to reliably reproduce this. First thing worth trying would be running the current qemu-iotests suite on the old versions. If we don't find it this way, I guess we need to catch it with code review. I'm not sure if I can get to it this week, and starting next week I'll be on vacation, so any help with finding a reproducer would be appreciated.

Quoted 9 lines
Quoted 3 lines

In any case, this is the cluster allocation code. It's probably not related to rereading things from disk, but rather to the writeout of the page cache.

Yes, the problem is probably write related. But as the write "doens't explode with some spectacular error", I only notice the error on the following read by comparing md5 sums.
I just re-checked it: After a reboot the md5sums are still invalid, so I guess the data is corrupted on writeout.

Yes, it's really the only thing that makes sense in connection with this patch.

Kevin

#34 lore raw

Re: [Qemu-devel] [BUG] qemu-1.1.2 [FIXED-BY] qcow2: Fix avail_sectors in cluster allocation code

Hello Kevin,

Am Mittwoch 12 Dezember 2012 17:54:58 schrieb Kevin Wolf:

Quoted 12 lines

Am 12.12.2012 15:09, schrieb Philipp Hahn:

Quoted 11 lines

Am Mittwoch 12 Dezember 2012 14:41:49 schrieb Kevin Wolf:

Quoted 6 lines

As you can see in the commit message of that patch I was convinced that no bug did exist in practice and this was only dangerous with respect to future changes. Therefore my first question is if you're using an unmodified upstream qemu or if some backported patches are applied to it? If it's indeed unmodified, we should probably review the code once again to understand why it makes a difference.

This were all unmodified versions directly from git between "qemu-kvm-1.1.0" and "qemu-kvm-1.2.0"

Quoted 8 lines
Quoted 5 lines

"git checkout b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c" works, "git checkout b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c~1" is broken. "git checkout qemu-kvm-1.1.2" is broken,
"git checkout qemu-kvm-1.1.2 ; git cherry-pick b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c" works

Ok, thanks for clarifying. Then I must have missed some interesting case while doing the patch.

I just re-run my "git bisect run ~/bisect.sh" case, but it again arrived at that patch. I just queued another run for tonight so make sure the test is reliable:

# bad: [4c3e02beed9878a5f760eeceb6cd42c475cf0127] Merge tag 'v1.2.0' # good: [bd11ac4feb54d32653e5d4eb7994bed18be0609c] fdc: fix implied seek while there is no media in drive
git bisect start 'qemu-kvm-1.2.0' 'qemu-kvm-1.1.0'
# good: [15ecf28f39e2b6fba359ed094770c8fa4ad8dc60] Merge tag 'v1.1.0' into next
git bisect good 15ecf28f39e2b6fba359ed094770c8fa4ad8dc60
# bad: [2fa5008ffd49e51540756adccf966a2fcde6e6c1] hd-geometry: Factor out guess_chs_for_size()
git bisect bad 2fa5008ffd49e51540756adccf966a2fcde6e6c1
# bad: [306a571a2d75e32cd2eae5486c2714b7b7792a63] hw/arm_gic: Add qdev property for GIC revision
git bisect bad 306a571a2d75e32cd2eae5486c2714b7b7792a63
# good: [5c6f4f178ba542358c012ca033985f73e61b8ae5] z2: Rename PXA2xxState variable
git bisect good 5c6f4f178ba542358c012ca033985f73e61b8ae5
# good: [833e40858cb9501c5e76b3aa345e4bb5be34385a] qcow2: remove a line of unnecessary code
git bisect good 833e40858cb9501c5e76b3aa345e4bb5be34385a
# bad: [0b0cb9d310edfe2b2d108f18be4f013a1e552cfd] Merge remote-tracking branch 'kwolf/for-anthony' into staging
git bisect bad 0b0cb9d310edfe2b2d108f18be4f013a1e552cfd
# bad: [0446919dcab51e7468f346c0a009a88632c5c5e0] qemu-iotests: COW with many AIO requests on the same cluster
git bisect bad 0446919dcab51e7468f346c0a009a88632c5c5e0
# good: [b75a02829dde98723dfe16fa098338cb267b28b9] Prevent disk data loss when closing qemu
git bisect good b75a02829dde98723dfe16fa098338cb267b28b9
# good: [c4a248a138028bee63a099410c79b428db0c4779] block: copy enable_write_cache in bdrv_append
git bisect good c4a248a138028bee63a099410c79b428db0c4779
# good: [6af4e9ead4ec9491259c9861b1b35f9abee24a66] qcow2: always operate caches in writeback mode
git bisect good 6af4e9ead4ec9491259c9861b1b35f9abee24a66
# bad: [b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c] qcow2: Fix avail_sectors in cluster allocation code
git bisect bad b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c
# good: [cdba7fee1daa8865bac2d69da288171fe7c21aae] qcow2: Simplify calculation for COW area at the end
git bisect good cdba7fee1daa8865bac2d69da288171fe7c21aae

Quoted 6 lines

Ideally we would find a sequence of qemu-io commands to reliably reproduce this. First thing worth trying would be running the current qemu-iotests suite on the old versions. If we don't find it this way, I guess we need to catch it with code review. I'm not sure if I can get to it this week, and starting next week I'll be on vacation, so any help with finding a reproducer would be appreciated.

I'll have a look at it tommorrow.

Thank you for your fast replies and have a nice vacation in case we don't head from each other this week again.

BYtE
Philipp

Signature · 4 lines
Philipp Hahn           Open Source Software Engineer      [email protected]
Univention GmbH        be open.                       fon: +49 421 22 232- 0
Mary-Somerville-Str.1  D-28359 Bremen                 fax: +49 421 22 232-99
                                                   http://www.univention.de/
#35 lore raw

Re: [Qemu-devel] [BUG] qemu-1.1.2 [FIXED-BY] qcow2: Fix avail_sectors in cluster allocation code

Hello Kevin,

Quoted 3 lines · On Wednesday 12 December 2012 18:29:48 Philipp Hahn wrote:

I just re-run my "git bisect run ~/bisect.sh" case, but it again arrived at that patch. I just queued another run for tonight so make sure the test is reliable:

The run from last night again arrived at the refecenced patch.

Quoted 6 lines
Quoted 6 lines

Ideally we would find a sequence of qemu-io commands to reliably reproduce this. First thing worth trying would be running the current qemu-iotests suite on the old versions. If we don't find it this way, I guess we need to catch it with code review. I'm not sure if I can get to it this week, and starting next week I'll be on vacation, so any help with finding a reproducer would be appreciated.

I took a closer look at what gets corrupted; I've attached my notes. Please notice that the partitions are not alignd properly.

If you would like to look at the full qcow2_alloc_clusters_offset trace, I can provide you with a link to the trace file.

BYtE
Philipp

Signature · 4 lines
Philipp Hahn           Open Source Software Engineer      [email protected]
Univention GmbH        be open.                       fon: +49 421 22 232- 0
Mary-Somerville-Str.1  D-28359 Bremen                 fax: +49 421 22 232-99
                                                   http://www.univention.de/

# FILE=/var/cache/apt/archives/liblqr-1-0_0.4.1-1.3.201104131631_amd64.deb

# debugfs /dev/vg_ucs/rootfs -R "stat $FILE"
debugfs 1.41.12 (17-May-2010)
Inode: 1385717   Type: regular    Mode:  0644   Flags: 0x0
Generation: 2854262870    Version: 0x00000000
User:     0   Group:     0   Size: 38884
File ACL: 0    Directory ACL: 0
Links: 1   Blockcount: 80
Fragment:  Address: 0    Number: 0    Size: 0
ctime: 0x50979ae9 -- Mon Nov  5 11:54:33 2012
atime: 0x50979b0d -- Mon Nov  5 11:55:09 2012
mtime: 0x4de1dbce -- Sun May 29 07:38:22 2011
Size of extra inode fields: 4
BLOCKS:
(0-9):5728401-5728410
TOTAL: 10

# BSIZE=4096
# BOFFSET=0

# dd bs=$BSIZE count=1 if=$FILE skip=$BOFFSET 2>/dev/null | md5sum
065b19ba6e9153dcc88003ea06076f9f  -

# BLOCK=5728401

# dd bs=$BSIZE count=1 if=/dev/vg_ucs/rootfs skip=$BLOCK 2>/dev/null | md5sum
065b19ba6e9153dcc88003ea06076f9f  -
# dmsetup table
vg_ucs-rootfs: 0 97910784 linear 254:3 384
# LV_SOFFSET=384
# dd bs=512c count=8 if=/dev/vda3 skip=$((BLOCK*8+LV_SOFFSET)) 2>/dev/null | md5sum
065b19ba6e9153dcc88003ea06076f9f  -
# fdisk -l -u /dev/vda
/dev/vda3         4739175   104856254    50058540   8e  Linux LVM
# PART_SOFFSET=4739175
# dd bs=512c count=8 if=/dev/vda skip=$((BLOCK*8+LV_SOFFSET+PART_SOFFSET)) 2>/dev/null | md5sum
065b19ba6e9153dcc88003ea06076f9f  -
# debugfs /dev/vg_ucs/rootfs -R "icheck $(seq 5728387 5728403 | tr '\n' ' ')"
debugfs 1.41.12 (17-May-2010)
Block   Inode number
5728387 1385715  
5728388 1385715  
5728389 1385715  
5728390 1385715  
5728391 1385715  
5728392 1385715  
5728393 1385715  
5728394 1385715  
5728395 1385715  
5728396 1385715  
5728397 1385716  
5728398 1385716  
5728399 1385716  
5728400 1385716  
5728401 1385717  
5728402 1385717  
5728403 1385717  
# debugfs /dev/vg_ucs/rootfs -R "ncheck 1385715 1385716 1385717"
debugfs 1.41.12 (17-May-2010)
Inode   Pathname 
1385715 /var/cache/apt/archives/libhtml-template-perl_2.9-2.7.201104290220_all.deb
1385717 /var/cache/apt/archives/liblqr-1-0_0.4.1-1.3.201104131631_amd64.deb
1385716 /var/cache/apt/archives/libio-socket-inet6-perl_2.65-1.1.3.201104291113_all.deb
# md5sum /var/cache/apt/archives/libhtml-template-perl_2.9-2.7.201104290220_all.deb /var/cache/apt/archives/liblqr-1-0_0.4.1-1.3.201104131631_amd64.deb /var/cache/apt/archives/libio-socket-inet6-perl_2.65-1.1.3.201104291113_all.deb
123f4c338bd875825d5d762e8bb48b2a  /var/cache/apt/archives/libhtml-template-perl_2.9-2.7.201104290220_all.deb
eadbf53d7313df2560f4741fbe982008  /var/cache/apt/archives/liblqr-1-0_0.4.1-1.3.201104131631_amd64.deb
d0c7bf2d62c125409e00e459ac94277a  /var/cache/apt/archives/libio-socket-inet6-perl_2.65-1.1.3.201104291113_all.deb
# apt-cache show libhtml-template-perl liblqr-1-0 libio-socket-inet6-perl | egrep 'MD5sum|Package'
Package: libhtml-template-perl
MD5sum: 123f4c338bd875825d5d762e8bb48b2a
Package: liblqr-1-0
MD5sum: 94ccb97b38bedef97072fdcc7bce1872
Package: libio-socket-inet6-perl
MD5sum: 8f04f2da2a7d2eefb9e77bf87a0b8972

# IMAGE=/var/lib/libvirt/images/stefan_UCS-3.0-2-13.3-Kolab-Slave.qcow2 # BLOCK=5728401 BSIZE=4096 BOFFSET=0 LV_SOFFSET=384 PART_SOFFSET=4739175 # OFFSET=$((BLOCK*BSIZE + LV_SOFFSET*512 + PART_SOFFSET*512))

# echo $((OFFSET >> 16 >> 13)) $(((OFFSET >> 16) & ((1 << 13) - 1))) $(((OFFSET >> 0) & ((1 << 16) - 1)))
48 1836 56832
# qemu-io -c "read -v $OFFSET $BSIZE" "$IMAGE" | sed -ne 's/^\([0-9a-f]\+:\) /\1/p' | xxd -r -seek -$OFFSET | md5sum
065b19ba6e9153dcc88003ea06076f9f  -
# qcow2.py -r -s "$IMAGE" --read $OFFSET $BSIZE | xxd -r | md5sum
l1=0x30 l2=0x72c c=0xde00
065b19ba6e9153dcc88003ea06076f9f  -
# qcow2.py -r -s "$IMAGE" | egrep "L1\[$((0x30))\]|L2\[$((0x72c))\]|cluster_bits| size|l1_size"
0000000000000000: + cluster_bits=16 (64 KiB)
0000000000000000: + size=50 GiB
0000000000000000: + l1_size=150
0000000014780000: + L1[48]=0x800000016cad0000
000000016cad0000: +  L2[1836]=0x8000000177a60000
# dd bs=1 count=$BSIZE if=$IMAGE skip=$((0x177a60000+0xde00)) 2>/dev/null | md5sum
065b19ba6e9153dcc88003ea06076f9f  -
# printf '0x%0x\n' $OFFSET
0x6072cde00
# ./scripts/simpletrace.py trace-events /trace-17705 | grep -n -A3 offset=0x6072c
2132:qcow2_alloc_clusters_offset    10.707 co=0x1938190 offset=0x6072c4e00 n_start=0x27 n_end=0x11f
2133-qcow2_alloc_clusters_offset    19.610 co=0x15c5030 offset=0x6072e4e00 n_start=0x27 n_end=0x417
2134-qcow2_alloc_clusters_offset 22891.351 co=0x1938950 offset=0x607250000 n_start=0x0  n_end=0x39f
2135-qcow2_alloc_clusters_offset    20.294 co=0x1961550 offset=0x607200000 n_start=0x0  n_end=0x1e7
3236:qcow2_alloc_clusters_offset 64432.668 co=0x1938950 offset=0x6072c0000 n_start=0x0  n_end=0x1f
2137-qcow2_alloc_clusters_offset  1557.462 co=0x1961340 offset=0x606fd0000 n_start=0x0  n_end=0x57
2138-qcow2_alloc_clusters_offset 17697.902 co=0x15c5030 offset=0x6072f0000 n_start=0x0  n_end=0x397
2139-qcow2_alloc_clusters_offset 97534.098 co=0x15c5030 offset=0x601625e00 n_start=0x2f n_end=0x14df
--
2827:qcow2_alloc_clusters_offset    16.653 co=0x195ca40 offset=0x6072c3e00 n_start=0x1f n_end=0x27
2827:qcow2_alloc_clusters_offset    16.653 co=0x195ca40 offset=0x6072c3e00 n_start=0x1f n_end=0x27
2828-qcow2_alloc_clusters_offset    31.017 co=0x195c5d0 offset=0x606c80000 n_start=0x0  n_end=0x7
2829-qcow2_alloc_clusters_offset   757.123 co=0x195c5d0 offset=0x6072e3e00 n_start=0x1f n_end=0x27
2830-qcow2_alloc_clusters_offset     9.656 co=0x195ca40 offset=0x607497e00 n_start=0x3f n_end=0x47
#36 lore raw

Re: [Qemu-devel] [BUG] qemu-1.1.2 [FIXED-BY] qcow2: Fix avail_sectors in cluster allocation code

In reply to Kevin Wolf

Hello Kevin, hello Michael,

Quoted 12 lines · On Wednesday 12 December 2012 17:54:58 Kevin Wolf wrote:

Am 12.12.2012 15:09, schrieb Philipp Hahn:

Quoted 11 lines

Am Mittwoch 12 Dezember 2012 14:41:49 schrieb Kevin Wolf:

Quoted 6 lines

As you can see in the commit message of that patch I was convinced that no bug did exist in practice and this was only dangerous with respect to future changes. Therefore my first question is if you're using an unmodified upstream qemu or if some backported patches are applied to it? If it's indeed unmodified, we should probably review the code once again to understand why it makes a difference.

This were all unmodified versions directly from git between "qemu-kvm-1.1.0" and "qemu-kvm-1.2.0"

Quoted 8 lines
Quoted 5 lines

"git checkout b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c" works, "git checkout b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c~1" is broken. "git checkout qemu-kvm-1.1.2" is broken,
"git checkout qemu-kvm-1.1.2 ; git cherry-pick b7ab0fea37c15ca9e249c42c46f5c48fd1a0943c" works

Ok, thanks for clarifying. Then I must have missed some interesting case while doing the patch.

I think I found your missing link:
After filling in "QCowL2Meta *m", that request ist queued:
QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
do prevent double allocating the same cluster for overlapping requests, which is checked in do_alloc_cluster_offset().

I guess that since the sector count was wrong, the overlap detection didn't work and the two concurrent write requests to the same cluster overwrote each other.

Quoted 2 lines

Ideally we would find a sequence of qemu-io commands to reliably reproduce this.

You're the block guru, so I leave that to you (or anybody else who knows more about the working of qemu-io.) ;-)

Sincerely
Philipp

Signature · 4 lines
Philipp Hahn           Open Source Software Engineer      [email protected]
Univention GmbH        be open.                       fon: +49 421 22 232- 0
Mary-Somerville-Str.1  D-28359 Bremen                 fax: +49 421 22 232-99
                                                   http://www.univention.de/
#37 lore raw

Re: [Qemu-devel] [BUG] qemu-1.1.2 [FIXED-BY] qcow2: Fix avail_sectors in cluster allocation code

Quoted 9 lines · Philipp Hahn

I think I found your missing link:
After filling in "QCowL2Meta *m", that request ist queued:
QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight);
do prevent double allocating the same cluster for overlapping requests, which is checked in do_alloc_cluster_offset().

I guess that since the sector count was wrong, the overlap detection didn't work and the two concurrent write requests to the same cluster overwrote each other.

Meh. And I already closed the debian bugreport... :)

But thank you Philipp for your excellent work on the matter!

/mjt

author
#38 lore raw

PATCH 29/39 qemu-iotests: Some backing file COW tests

In reply to Kevin Wolf

Looks like we're still missing these very basic tests for backing file handling.

---
 tests/qemu-iotests/037     |  119 ++++++++
 tests/qemu-iotests/037.out |  645 ++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/group   |    1 +
 3 files changed, 765 insertions(+), 0 deletions(-)
 create mode 100755 tests/qemu-iotests/037
 create mode 100644 tests/qemu-iotests/037.out
tests/qemu-iotests/037 new file +119 -0
new file mode 100755
index 0000000..c11460b
@@ -0,0 +1,119 @@
+#!/bin/bash
+#
+# Test COW from backing files
+#
+# Copyright (C) 2012 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# creator
[email protected]
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# failure is the default!
+
+_cleanup()
+{
+	_cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+
+_supported_fmt qcow qcow2 vmdk qed
+_supported_proto generic
+_supported_os Linux
+
+CLUSTER_SIZE=4k
+size=128M
+
+echo
+echo "== creating backing file for COW tests =="
+
+_make_test_img $size
+
+function backing_io()
+{
+    local offset=$1
+    local sectors=$2
+    local op=$3
+    local pattern=0
+    local cur_sec=0
+
+    for i in $(seq 0 $((sectors - 1))); do
+        cur_sec=$((offset / 512 + i))
+        pattern=$(( ( (cur_sec % 256) + (cur_sec / 256)) % 256 ))
+
+        echo "$op -P $pattern $((cur_sec * 512)) 512"
+    done
+}
+
+backing_io 0 256 write | $QEMU_IO $TEST_IMG | _filter_qemu_io
+
+mv $TEST_IMG $TEST_IMG.base
+
+_make_test_img -b $TEST_IMG.base 6G
+
+echo
+echo "== COW in a single cluster =="
+$QEMU_IO -c "write -P 0x77 0 2k" $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "write -P 0x88 6k 2k" $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "write -P 0x99 9k 2k" $TEST_IMG | _filter_qemu_io
+
+$QEMU_IO -c "read -P 0x77 0 2k" $TEST_IMG | _filter_qemu_io
+backing_io $((2 * 1024)) 8 read | $QEMU_IO $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "read -P 0x88 6k 2k" $TEST_IMG | _filter_qemu_io
+backing_io $((8 * 1024)) 2 read | $QEMU_IO $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "read -P 0x99 9k 2k" $TEST_IMG | _filter_qemu_io
+backing_io $((11 * 1024)) 2 read | $QEMU_IO $TEST_IMG | _filter_qemu_io
+
+echo
+echo "== COW in two-cluster allocations =="
+$QEMU_IO -c "write -P 0x77 16k 6k" $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "write -P 0x88 26k 6k" $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "write -P 0x99 33k 5k" $TEST_IMG | _filter_qemu_io
+
+$QEMU_IO -c "read -P 0x77 16k 6k" $TEST_IMG | _filter_qemu_io
+backing_io $((22 * 1024)) 8 read | $QEMU_IO $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "read -P 0x88 26k 6k" $TEST_IMG | _filter_qemu_io
+backing_io $((32 * 1024)) 2 read | $QEMU_IO $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "read -P 0x99 33k 5k" $TEST_IMG | _filter_qemu_io
+backing_io $((38 * 1024)) 4 read | $QEMU_IO $TEST_IMG | _filter_qemu_io
+
+echo
+echo "== COW in multi-cluster allocations =="
+$QEMU_IO -c "write -P 0x77 48k 15k" $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "write -P 0x88 66k 14k" $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "write -P 0x99 83k 15k" $TEST_IMG | _filter_qemu_io
+
+$QEMU_IO -c "read -P 0x77 48k 15k" $TEST_IMG | _filter_qemu_io
+backing_io $((63 * 1024)) 6 read | $QEMU_IO $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "read -P 0x88 66k 14k" $TEST_IMG | _filter_qemu_io
+backing_io $((80 * 1024)) 6 read | $QEMU_IO $TEST_IMG | _filter_qemu_io
+$QEMU_IO -c "read -P 0x99 83k 15k" $TEST_IMG | _filter_qemu_io
+backing_io $((98 * 1024)) 4 read | $QEMU_IO $TEST_IMG | _filter_qemu_io
+
+_check_test_img
+
+# success, all done
+echo "*** done"
+rm -f $seq.full
+status=0
tests/qemu-iotests/037.out new file +645 -0

645 lines not on this page · raw message

tests/qemu-iotests/group +1 -0
index 36ebf1a..595fdc2 100644
@@ -42,3 +42,4 @@
 033 rw auto
 034 rw auto backing
 035 rw auto quick
+037 rw auto backing
Signature · 1 line
1.7.6.5
author
#39 lore raw

PATCH 30/39 qemu-iotests: COW with many AIO requests on the same cluster

In reply to Kevin Wolf

This one is a bit more interesting. The COW operation isn't performed completely synchronously, and therefore dependencies must be handled correctly when multiple requests write to the same unallocated cluster.

---
 tests/qemu-iotests/038     |  133 +++++++
 tests/qemu-iotests/038.out |  909 ++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/group   |    1 +
 3 files changed, 1043 insertions(+), 0 deletions(-)
 create mode 100755 tests/qemu-iotests/038
 create mode 100644 tests/qemu-iotests/038.out
tests/qemu-iotests/038 new file +133 -0
new file mode 100755
index 0000000..36125ea
@@ -0,0 +1,133 @@
+#!/bin/bash
+#
+# Test COW from backing files with AIO
+#
+# Copyright (C) 2012 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# creator
[email protected]
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# failure is the default!
+
+_cleanup()
+{
+	_cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+
+_supported_fmt qcow2 qed
+_supported_proto generic
+_supported_os Linux
+
+CLUSTER_SIZE=2M
+size=128M
+
+echo
+echo "== creating backing file for COW tests =="
+
+_make_test_img $size
+
+function backing_io()
+{
+    local offset=$1
+    local sectors=$2
+    local op=$3
+    local pattern=0
+    local cur_sec=0
+
+    for i in $(seq 0 $((sectors - 1))); do
+        cur_sec=$((offset / 65536 + i))
+        pattern=$(( ( (cur_sec % 128) + (cur_sec / 128)) % 128 ))
+
+        echo "$op -P $pattern $((cur_sec * 64))k 64k"
+    done
+}
+
+backing_io 0 256 write | $QEMU_IO $TEST_IMG | _filter_qemu_io
+
+mv $TEST_IMG $TEST_IMG.base
+
+_make_test_img -b $TEST_IMG.base 6G
+
+echo
+echo "== Some concurrent requests touching the same cluster =="
+
+function overlay_io()
+{
+    # Start with a request touching two clusters
+    echo aio_write -P 0x80 2020k 80k
+
+    # Then add some requests all over the place
+    for i in $(seq 0 15; seq 17 31; seq 33 47); do
+        echo aio_write -P $((0x81 + i)) $((i * 128))k 64k
+    done
+
+    # Then backwards overwriting part of them
+    for i in $( (seq 0 15; seq 17 31; seq 33 47) | tac); do
+        echo aio_write -P $((0x81 + i)) $((i * 128 + 32))k 64k
+    done
+
+    # And finally crossing the next cluster boundary
+    echo aio_write -P 0x90 4080k 80k
+}
+
+overlay_io | $QEMU_IO $TEST_IMG | _filter_qemu_io |\
+	sed -e 's/bytes at offset [0-9]*/bytes at offset XXX/g'
+
+echo
+echo "== Verify image content =="
+
+function verify_io()
+{
+    echo read -P 31 2016k 4k
+    echo read -P 0x80 2020k 80k
+    echo read -P 32 2100k 12k
+    echo read -P 33 2112k 64k
+
+    echo read -P 63 4064k 16k
+    echo read -P 0x90 4080k 80k
+    echo read -P 65 4160k 64k
+
+    for i in $(seq 0 15; seq 17 31; seq 33 47); do
+        echo read -P $((0x81 + i)) $((i * 128))k 96k
+    done
+
+    for i in $(seq 0 14; seq 16 30; seq 32 47); do
+        local cur_sec=$(( i * 2 + 1 ))
+        local pattern=$(( ( (cur_sec % 128) + (cur_sec / 128)) % 128 ))
+
+        echo read -P $pattern $((i * 128 + 96))k 32k
+    done
+}
+
+verify_io | $QEMU_IO $TEST_IMG | _filter_qemu_io
+
+_check_test_img
+
+# success, all done
+echo "*** done"
+rm -f $seq.full
+status=0
tests/qemu-iotests/038.out new file +909 -0

909 lines not on this page · raw message

tests/qemu-iotests/group +1 -0
index 595fdc2..14230ce 100644
@@ -43,3 +43,4 @@
 034 rw auto backing
 035 rw auto quick
 037 rw auto backing
+038 rw auto backing
Signature · 1 line
1.7.6.5
author
#40 lore raw

PATCH 31/39 qemu-img: document qed format on qemu-img man page

In reply to Kevin Wolf

From: Stefan Hajnoczi <[email protected]>

The qemu-img.1 man page is missing the qed format from its list of supported formats. Document the image creation options for qed.

---
 qemu-img.texi |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)
qemu-img.texi +23 -0
index 5a7b2bb..77c6d0b 100644
@@ -237,6 +237,29 @@ to grow.
 
 @end table
 
+@item qed
+Image format with support for backing files and compact image files (when your
+filesystem or transport medium does not support holes).  Good performance due
+to less metadata than the more featureful qcow2 format, especially with
+cache=writethrough or cache=directsync.  Consider using qcow2 which will soon
+have a similar optimization and is most actively developed.
+
+Supported options:
+@table @code
+@item backing_file
+File name of a base image (see @option{create} subcommand).
+@item backing_fmt
+Image file format of backing file (optional).  Useful if the format cannot be
+autodetected because it has no header, like some vhd/vpc files.
+@item cluster_size
+Changes the cluster size (must be power-of-2 between 4K and 64K). Smaller
+cluster sizes can improve the image file size whereas larger cluster sizes
+generally provide better performance.
+@item table_size
+Changes the number of clusters per L1/L2 table (must be power-of-2 between 1
+and 16).  There is normally no need to change this value but this option can be
+used for performance benchmarking.
+@end table
 
 @item qcow
 Old QEMU image format. Left for compatibility.
Signature · 1 line
1.7.6.5
author
#41 lore raw

PATCH 32/39 block: Replace bdrv_get_format() by bdrv_get_format_name()

In reply to Kevin Wolf

From: Markus Armbruster <[email protected]>

So callers don't need to know anything about maximum name length. Returning a pointer is safe, because the name string lives as long as the block driver it names, and block drivers don't die.

Requested by Peter Maydell.

---
 block.c    |   11 ++++-------
 block.h    |    2 +-
 qemu-img.c |    5 ++---
 3 files changed, 7 insertions(+), 11 deletions(-)
block.c +4 -7
index 48528fd..0acdcac 100644
@@ -1035,7 +1035,8 @@ void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
      * swapping bs_new and bs_top contents. */
     tmp.backing_hd = bs_new;
     pstrcpy(tmp.backing_file, sizeof(tmp.backing_file), bs_top->filename);
-    bdrv_get_format(bs_top, tmp.backing_format, sizeof(tmp.backing_format));
+    pstrcpy(tmp.backing_format, sizeof(tmp.backing_format),
+            bs_top->drv ? bs_top->drv->format_name : "");
 
     /* swap contents of the fixed new bs and the current top */
     *bs_new = *bs_top;
@@ -2428,13 +2429,9 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
     return ret;
 }
 
-void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
+const char *bdrv_get_format_name(BlockDriverState *bs)
 {
-    if (!bs->drv) {
-        buf[0] = '\0';
-    } else {
-        pstrcpy(buf, buf_size, bs->drv->format_name);
-    }
+    return bs->drv ? bs->drv->format_name : NULL;
 }
 
 void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
block.h +1 -1
index 43bfd99..d135652 100644
@@ -296,7 +296,7 @@ int bdrv_is_inserted(BlockDriverState *bs);
 int bdrv_media_changed(BlockDriverState *bs);
 void bdrv_lock_medium(BlockDriverState *bs, bool locked);
 void bdrv_eject(BlockDriverState *bs, bool eject_flag);
-void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);
+const char *bdrv_get_format_name(BlockDriverState *bs);
 BlockDriverState *bdrv_find(const char *name);
 BlockDriverState *bdrv_next(BlockDriverState *bs);
 void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs),
qemu-img.c +2 -3
index 9336c86..80cfb9b 100644
@@ -1107,7 +1107,7 @@ static int img_info(int argc, char **argv)
     int c;
     const char *filename, *fmt;
     BlockDriverState *bs;
-    char fmt_name[128], size_buf[128], dsize_buf[128];
+    char size_buf[128], dsize_buf[128];
     uint64_t total_sectors;
     int64_t allocated_size;
     char backing_filename[1024];
@@ -1139,7 +1139,6 @@ static int img_info(int argc, char **argv)
     if (!bs) {
         return 1;
     }
-    bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
     bdrv_get_geometry(bs, &total_sectors);
     get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
     allocated_size = bdrv_get_allocated_file_size(bs);
@@ -1153,7 +1152,7 @@ static int img_info(int argc, char **argv)
            "file format: %s\n"
            "virtual size: %s (%" PRId64 " bytes)\n"
            "disk size: %s\n",
-           filename, fmt_name, size_buf,
+           filename, bdrv_get_format_name(bs), size_buf,
            (total_sectors * 512),
            dsize_buf);
     if (bdrv_is_encrypted(bs)) {
Signature · 1 line
1.7.6.5
author
#42 lore raw

PATCH 33/39 xen: Don't change -drive if=xen device name during machine init

In reply to Kevin Wolf

From: Markus Armbruster <[email protected]>

A "top" BlockDriverState has a non-empty device_name. If the user doesn't specify one with -drive parameter id, the system supplies a default name.

xen_config_dev_blk() changes this name, during machine initialization. Naughty. Don't do that.

---
 hw/xen_devconfig.c |    9 ++++-----
 1 files changed, 4 insertions(+), 5 deletions(-)
hw/xen_devconfig.c +4 -5
index 41accbb..7b7b0a2 100644
@@ -94,16 +94,15 @@ static int xen_config_dev_all(char *fe, char *be)
 
 int xen_config_dev_blk(DriveInfo *disk)
 {
-    char fe[256], be[256];
+    char fe[256], be[256], device_name[32];
     int vdev = 202 * 256 + 16 * disk->unit;
     int cdrom = disk->media_cd;
     const char *devtype = cdrom ? "cdrom" : "disk";
     const char *mode    = cdrom ? "r"     : "w";
 
-    snprintf(disk->bdrv->device_name, sizeof(disk->bdrv->device_name),
-	     "xvd%c", 'a' + disk->unit);
+    snprintf(device_name, sizeof(device_name), "xvd%c", 'a' + disk->unit);
     xen_be_printf(NULL, 1, "config disk %d [%s]: %s\n",
-                  disk->unit, disk->bdrv->device_name, disk->bdrv->filename);
+                  disk->unit, device_name, disk->bdrv->filename);
     xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe));
 
     /* frontend */
@@ -111,7 +110,7 @@ int xen_config_dev_blk(DriveInfo *disk)
     xenstore_write_str(fe, "device-type",     devtype);
 
     /* backend */
-    xenstore_write_str(be, "dev",             disk->bdrv->device_name);
+    xenstore_write_str(be, "dev",             device_name);
     xenstore_write_str(be, "type",            "file");
     xenstore_write_str(be, "params",          disk->bdrv->filename);
     xenstore_write_str(be, "mode",            mode);
Signature · 1 line
1.7.6.5
author
#43 lore raw

PATCH 34/39 xen: Don't peek behind the BlockDriverState abstraction

In reply to Kevin Wolf

From: Markus Armbruster <[email protected]>

First offender is xen_config_dev_blk()'s use of disk->bdrv->filename. Get the filename from disk->opts instead. Same result, except for
snapshots: there, we now get the filename specified by the user instead of the name of the temporary image created by bdrv_open(). Should be an improvement.

Second offender is blk_init()'s use of blkdev->bs->drv->format_name. Simply use the appropriate interface to get the format name.

---
 hw/xen_devconfig.c |    6 +++---
 hw/xen_disk.c      |    3 +--
 2 files changed, 4 insertions(+), 5 deletions(-)
hw/xen_devconfig.c +3 -3
index 7b7b0a2..0928613 100644
@@ -1,6 +1,5 @@
 #include "xen_backend.h"
 #include "blockdev.h"
-#include "block_int.h" /* XXX */
 
 /* ------------------------------------------------------------- */
 
@@ -99,10 +98,11 @@ int xen_config_dev_blk(DriveInfo *disk)
     int cdrom = disk->media_cd;
     const char *devtype = cdrom ? "cdrom" : "disk";
     const char *mode    = cdrom ? "r"     : "w";
+    const char *filename = qemu_opt_get(disk->opts, "file");
 
     snprintf(device_name, sizeof(device_name), "xvd%c", 'a' + disk->unit);
     xen_be_printf(NULL, 1, "config disk %d [%s]: %s\n",
-                  disk->unit, device_name, disk->bdrv->filename);
+                  disk->unit, device_name, filename);
     xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe));
 
     /* frontend */
@@ -112,7 +112,7 @@ int xen_config_dev_blk(DriveInfo *disk)
     /* backend */
     xenstore_write_str(be, "dev",             device_name);
     xenstore_write_str(be, "type",            "file");
-    xenstore_write_str(be, "params",          disk->bdrv->filename);
+    xenstore_write_str(be, "params",          filename);
     xenstore_write_str(be, "mode",            mode);
 
     /* common stuff */
hw/xen_disk.c +1 -2
index de7e8a4..fb68ed9 100644
@@ -40,7 +40,6 @@
 #include <xen/io/xenbus.h>
 
 #include "hw.h"
-#include "block_int.h"
 #include "qemu-char.h"
 #include "xen_blkif.h"
 #include "xen_backend.h"
@@ -650,7 +649,7 @@ static int blk_init(struct XenDevice *xendev)
     if (blkdev->file_size < 0) {
         xen_be_printf(&blkdev->xendev, 1, "bdrv_getlength: %d (%s) | drv %s\n",
                       (int)blkdev->file_size, strerror(-blkdev->file_size),
-                      blkdev->bs->drv ? blkdev->bs->drv->format_name : "-");
+                      bdrv_get_format_name(blkdev->bs) ?: "-");
         blkdev->file_size = 0;
     }
 
Signature · 1 line
1.7.6.5
author
#44 lore raw

PATCH 35/39 qcow2: fix autoclear image header update

In reply to Kevin Wolf

From: Stefan Hajnoczi <[email protected]>

The autoclear feature bits can be used for qcow2 file format features that are safe to "drop" by old programs that do not understand the feature. Upon opening the image file unknown autoclear feature bits are cleared and the image file header is rewritten, but this was happening too early in the code when critical header fields were not yet loaded.

Process autoclear feature bits after all necessary header information has been loaded.

---
 block/qcow2.c |   17 +++++++++--------
 1 files changed, 9 insertions(+), 8 deletions(-)
block/qcow2.c +9 -8
index 57fd43d..2c1cd0a 100644
@@ -297,14 +297,6 @@ static int qcow2_open(BlockDriverState *bs, int flags)
         goto fail;
     }
 
-    if (!bs->read_only && s->autoclear_features != 0) {
-        s->autoclear_features = 0;
-        ret = qcow2_update_header(bs);
-        if (ret < 0) {
-            goto fail;
-        }
-    }
-
     /* Check support for various header values */
     if (header.refcount_order != 4) {
         report_unsupported(bs, "%d bit reference counts",
@@ -408,6 +400,15 @@ static int qcow2_open(BlockDriverState *bs, int flags)
         goto fail;
     }
 
+    /* Clear unknown autoclear feature bits */
+    if (!bs->read_only && s->autoclear_features != 0) {
+        s->autoclear_features = 0;
+        ret = qcow2_update_header(bs);
+        if (ret < 0) {
+            goto fail;
+        }
+    }
+
     /* Initialise locks */
     qemu_co_mutex_init(&s->lock);
 
Signature · 1 line
1.7.6.5
author
#45 lore raw

PATCH 36/39 fdc: fix implied seek while there is no media in drive

In reply to Kevin Wolf

From: Pavel Hrdina <[email protected]>

The Windows uses 'READ' command at the start of an instalation without checking the 'dir' register. We have to abort the transfer with an abnormal termination if there is no media in the drive.

---
 hw/fdc.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
hw/fdc.c +4 -0
index bfa4e68..78b4e33 100644
@@ -159,6 +159,10 @@ static int fd_seek(FDrive *drv, uint8_t head, uint8_t track, uint8_t sect,
         drv->sect = sect;
     }
 
+    if (drv->bs == NULL || !bdrv_is_inserted(drv->bs)) {
+        ret = 2;
+    }
+
     return ret;
 }
 
Signature · 1 line
1.7.6.5
author
#46 lore raw

PATCH 37/39 fdc-test: introduced qtest read_without_media

In reply to Kevin Wolf

From: Pavel Hrdina <[email protected]>

If you try to read from a floppy drive without a media, you should get an abnormal termination error.

---
 tests/fdc-test.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 66 insertions(+), 0 deletions(-)
tests/fdc-test.c +66 -0
index 22d24ac..e730398 100644
@@ -49,6 +49,7 @@ enum {
 enum {
     CMD_SENSE_INT   = 0x08,
     CMD_SEEK        = 0x0f,
+    CMD_READ        = 0xe6,
 };
 
 enum {
@@ -99,6 +100,62 @@ static void ack_irq(void)
     g_assert(!get_irq(FLOPPY_IRQ));
 }
 
+static uint8_t send_read_command(void)
+{
+    uint8_t drive = 0;
+    uint8_t head = 0;
+    uint8_t cyl = 0;
+    uint8_t sect_addr = 1;
+    uint8_t sect_size = 2;
+    uint8_t eot = 1;
+    uint8_t gap = 0x1b;
+    uint8_t gpl = 0xff;
+
+    uint8_t msr = 0;
+    uint8_t st0;
+
+    uint8_t ret = 0;
+
+    floppy_send(CMD_READ);
+    floppy_send(head << 2 | drive);
+    g_assert(!get_irq(FLOPPY_IRQ));
+    floppy_send(cyl);
+    floppy_send(head);
+    floppy_send(sect_addr);
+    floppy_send(sect_size);
+    floppy_send(eot);
+    floppy_send(gap);
+    floppy_send(gpl);
+
+    uint8_t i = 0;
+    uint8_t n = 2;
+    for (; i < n; i++) {
+        msr = inb(FLOPPY_BASE + reg_msr);
+        if (msr == 0xd0) {
+            break;
+        }
+        sleep(1);
+    }
+
+    if (i >= n) {
+        return 1;
+    }
+
+    st0 = floppy_recv();
+    if (st0 != 0x40) {
+        ret = 1;
+    }
+
+    floppy_recv();
+    floppy_recv();
+    floppy_recv();
+    floppy_recv();
+    floppy_recv();
+    floppy_recv();
+
+    return ret;
+}
+
 static void send_step_pulse(void)
 {
     int drive = 0;
@@ -146,6 +203,14 @@ static void test_no_media_on_start(void)
     assert_bit_set(dir, DSKCHG);
 }
 
+static void test_read_without_media(void)
+{
+    uint8_t ret;
+
+    ret = send_read_command();
+    g_assert(ret == 0);
+}
+
 static void test_media_change(void)
 {
     uint8_t dir;
@@ -214,6 +279,7 @@ int main(int argc, char **argv)
     qtest_irq_intercept_in(global_qtest, "ioapic");
     qtest_add_func("/fdc/cmos", test_cmos);
     qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
+    qtest_add_func("/fdc/read_without_media", test_read_without_media);
     qtest_add_func("/fdc/media_change", test_media_change);
 
     ret = g_test_run();
Signature · 1 line
1.7.6.5
author
#47 lore raw

PATCH 38/39 qemu-iotests: add qcow2.py set-feature-bit command

In reply to Kevin Wolf

From: Stefan Hajnoczi <[email protected]>

This new command sets feature bits in the image file header:

qcow2.py set-feature-bit incompatible|compatible|autoclear <bit>

The bit number must be in the range [0, 64).

---
 tests/qemu-iotests/qcow2.py |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)
tests/qemu-iotests/qcow2.py +23 -0
index e27196a..97f3770 100755
@@ -181,10 +181,33 @@ def cmd_del_header_ext(fd, magic):
 
     h.update(fd)
 
+def cmd_set_feature_bit(fd, group, bit):
+    try:
+        bit = int(bit, 0)
+        if bit < 0 or bit >= 64:
+            raise ValueError
+    except:
+        print "'%s' is not a valid bit number in range [0, 64)" % bit
+        sys.exit(1)
+
+    h = QcowHeader(fd)
+    if group == 'incompatible':
+        h.incompatible_features |= 1 << bit
+    elif group == 'compatible':
+        h.compatible_features |= 1 << bit
+    elif group == 'autoclear':
+        h.autoclear_features |= 1 << bit
+    else:
+        print "'%s' is not a valid group, try 'incompatible', 'compatible', or 'autoclear'" % group
+        sys.exit(1)
+
+    h.update(fd)
+
 cmds = [
     [ 'dump-header',    cmd_dump_header,    0, 'Dump image header and header extensions' ],
     [ 'add-header-ext', cmd_add_header_ext, 2, 'Add a header extension' ],
     [ 'del-header-ext', cmd_del_header_ext, 1, 'Delete a header extension' ],
+    [ 'set-feature-bit', cmd_set_feature_bit, 2, 'Set a feature bit'],
 ]
 
 def main(filename, cmd, args):
Signature · 1 line
1.7.6.5
author
#48 lore raw

PATCH 39/39 qemu-iotests: add 036 autoclear feature bit test

In reply to Kevin Wolf

From: Stefan Hajnoczi <[email protected]>

This new test validates the autoclear feature bit behavior. When QEMU opens a qcow2v3 image file with an unknown autoclear feature bit the bit should be cleared in the image file header.

---
 tests/qemu-iotests/036     |   68 ++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/036.out |   52 +++++++++++++++++++++++++++++++++
 tests/qemu-iotests/group   |    1 +
 3 files changed, 121 insertions(+), 0 deletions(-)
 create mode 100755 tests/qemu-iotests/036
 create mode 100644 tests/qemu-iotests/036.out
tests/qemu-iotests/036 new file +68 -0
new file mode 100755
index 0000000..329533e
@@ -0,0 +1,68 @@
+#!/bin/bash
+#
+# Test that qcow2 unknown autoclear feature bits are cleared
+#
+# Copyright (C) 2011 Red Hat, Inc.
+# Copyright IBM, Corp. 2010
+#
+# Based on test 031.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# creator
[email protected]
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# failure is the default!
+
+_cleanup()
+{
+	_cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+. ./common.pattern
+
+# This tests qcow2-specific low-level functionality
+_supported_fmt qcow2
+_supported_proto generic
+_supported_os Linux
+
+# Only qcow2v3 and later supports feature bits
+IMGOPTS="compat=1.1"
+
+echo === Create image with unknown autoclear feature bit ===
+echo
+_make_test_img 64M
+./qcow2.py $TEST_IMG set-feature-bit autoclear 63
+./qcow2.py $TEST_IMG dump-header
+
+echo
+echo === Repair image ===
+echo
+$QEMU_IMG check -r all $TEST_IMG
+./qcow2.py $TEST_IMG dump-header
+
+# success, all done
+echo "*** done"
+rm -f $seq.full
+status=0
tests/qemu-iotests/036.out new file +52 -0

52 lines not on this page · raw message

tests/qemu-iotests/group +1 -0
index 14230ce..7a2c92b 100644
@@ -42,5 +42,6 @@
 033 rw auto
 034 rw auto backing
 035 rw auto quick
+036 rw auto quick
 037 rw auto backing
 038 rw auto backing
Signature · 1 line
1.7.6.5
#49 lore raw
In reply to Kevin Wolf
Quoted 7 lines · Kevin Wolf

The following changes since commit 63bb682a18acc5d2f3219ea96c4d81c1a3b50748:

   Merge branch 'master' of git://git.qemu.org/qemu (2012-06-15 15:53:34 +0400)

are available in the git repository at:

git://repo.or.cz/qemu/kevin.git for-anthony

Pulled. Thanks.

Regards,

Anthony Liguori

Quoted 115 lines

Josh Durgin (1):
rbd: hook up cache options

Kevin Wolf (7):
qemu-img check -r for repairing images
qemu-img check: Print fixed clusters and recheck
qcow2: Support for fixing refcount inconsistencies
qcow2: Simplify calculation for COW area at the end
qcow2: Fix avail_sectors in cluster allocation code
qemu-iotests: Some backing file COW tests
qemu-iotests: COW with many AIO requests on the same cluster

MORITA Kazutaka (1):
sheepdog: add coroutine_fn markers to coroutine functions

Markus Armbruster (7):
Un-inline fdctrl_init_isa()
block: Simplify how drive_init() computes default ID
block: New bdrv_get_flags()
scsi-disk: Don't peek behind the BlockDriverState abstraction
block: Replace bdrv_get_format() by bdrv_get_format_name()
xen: Don't change -drive if=xen device name during machine init
xen: Don't peek behind the BlockDriverState abstraction

Max Filippov (1):
xtensa_lx60: add missing #include "blockdev.h"

Paolo Bonzini (13):
block: implement is_allocated for raw
stream: tweak usage of bdrv_co_is_allocated
stream: move is_allocated_above to block.c
stream: move rate limiting to a separate header file
qemu-iotests: fill streaming test image with data
qemu-iotests: start vms in qtest mode
block: flush in writethrough mode after writes
savevm: flush after saving vm state
block: copy enable_write_cache in bdrv_append
block: add bdrv_set_enable_write_cache
block: always open drivers in writeback mode
ide: support enable/disable write cache
qcow2: always operate caches in writeback mode

Pavel Dovgaluk (1):
Prevent disk data loss when closing qemu

Pavel Hrdina (2):
fdc: fix implied seek while there is no media in drive
fdc-test: introduced qtest read_without_media

Stefan Hajnoczi (4):
qemu-img: document qed format on qemu-img man page
qcow2: fix autoclear image header update
qemu-iotests: add qcow2.py set-feature-bit command
qemu-iotests: add 036 autoclear feature bit test

Zhi Yong Wu (2):
qcow2: remove a line of unnecessary code
qcow2: fix endianness conversion

  block.c                       |   90 ++++-
  block.h                       |   17 +-
  block/qcow2-cache.c           |   25 +-
  block/qcow2-cluster.c         |   16 +-
  block/qcow2-refcount.c        |   57 ++-
  block/qcow2.c                 |   29 +-
  block/qcow2.h                 |    8 +-
  block/qed-check.c             |    2 +
  block/qed.c                   |    5 +-
  block/raw-posix.c             |   98 +++++
  block/raw.c                   |    8 +
  block/rbd.c                   |   19 +
  block/sheepdog.c              |    9 +-
  block/stream.c                |  109 +-----
  block/vdi.c                   |    7 +-
  block_int.h                   |    3 +-
  blockdev.c                    |    7 +-
  hw/fdc.c                      |   24 ++
  hw/fdc.h                      |   24 +-
  hw/ide/core.c                 |   21 +-
  hw/ide/piix.c                 |    3 +-
  hw/isa.h                      |    2 -
  hw/pc_sysfw.c                 |    1 +
  hw/scsi-disk.c                |    3 +-
  hw/xen_devconfig.c            |   13 +-
  hw/xen_disk.c                 |    3 +-
  hw/xtensa_lx60.c              |    1 +
  include/qemu/ratelimit.h      |   48 +++
  os-win32.c                    |    8 +-
  qemu-common.h                 |    1 +
  qemu-img-cmds.hx              |    4 +-
  qemu-img.c                    |   40 ++-
  qemu-img.texi                 |   30 ++-
  savevm.c                      |    2 +-
  tests/fdc-test.c              |   66 +++
  tests/qemu-iotests/030        |   15 +-
  tests/qemu-iotests/036        |   68 +++
  tests/qemu-iotests/036.out    |   52 +++
  tests/qemu-iotests/037        |  119 ++++++
  tests/qemu-iotests/037.out    |  645 +++++++++++++++++++++++++++++
  tests/qemu-iotests/038        |  133 ++++++
  tests/qemu-iotests/038.out    |  909 +++++++++++++++++++++++++++++++++++++++++
  tests/qemu-iotests/group      |    3 +
  tests/qemu-iotests/iotests.py |    4 +-
  tests/qemu-iotests/qcow2.py   |   23 +
  45 files changed, 2531 insertions(+), 243 deletions(-)

create mode 100644 include/qemu/ratelimit.h
create mode 100755 tests/qemu-iotests/036
create mode 100644 tests/qemu-iotests/036.out
create mode 100755 tests/qemu-iotests/037
create mode 100644 tests/qemu-iotests/037.out
create mode 100755 tests/qemu-iotests/038
create mode 100644 tests/qemu-iotests/038.out