Skip to contentSkip to conversation

New block driver: blklogwrites

PATCH Superseded Ari Sundholm 2 participants 16 messages Jun 7, 2018 Block layer

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover New block driver: blklogwrites

This patch series adds a new block driver, blklogwrites, to QEMU. The driver is given two block devices: a raw device backed by an image or a host block device, and a log device, typically backed by a file, on which writes to the raw device are logged.

The logging format used is the same as in the dm-log-writes target of the Linux kernel device mapper. The log reflects the writes that have been performed on the guest block device and flushed. To be strict, the log may contain writes that have not been flushed yet, but they are technically outside the bounds of the log until the next flush updates the metadata in the log superblock. We believe these semantics to be useful even though they may not be completely identical to those of dm-log-writes.

This functionality can be used for crash consistency and fs consistency testing in filesystem drivers, including on non-Linux guests and older guests running Linux versions without support for dm-log-writes. This is simple and useful. Admittedly this and more advanced things could perhaps be done by extending the quorum driver, but this approach would require re-engineering the functionality and involve a more complicated setup, so we offer this simple solution which we have found useful internally.

In the first patch of this series, two block permission constants are moved from block.c to include/block/block.h to make them available outside of block.c. The next patch uses these constants.

The driver requires all requests to be aligned to the sector size. In the second patch of this series, which includes the bulk of the blklogwrites driver, this sector size is assumed to be equal to BDRV_SECTOR_SIZE for simplicity.

In patches 3-8, a mechanism to pass the block configuration of a block backend to a block driver is introduced, and this mechanism is integrated in various hw/block drivers.

In patch 9, blklogwrites is changed to use the block configuration of the relevant block backend to set its block limits.

Finally, in patch 10, blklogwrites is improved to use the logical sector size of the block backend for logging. This means that the sector size in the log metadata will be set to the logical sector size, and offsets and sizes of writes will be expressed as a multiple of that instead of BDRV_SECTOR_SIZE.

Aapo Vienamo (1):
block: Add blklogwrites

Ari Sundholm (9):
block: Move two block permission constants to the relevant enum
block: Add a mechanism for passing a block driver a block
configuration
hw/scsi/scsi-disk: Always apply block configuration to block driver hw/ide/qdev: Always apply block configuration to block driver hw/block/virtio-blk: Always apply block configuration to block driver hw/block/nvme: Always apply block configuration to block driver hw/block/fdc: Always apply block configuration to block driver block/blklogwrites: Use block limits from the backend block
configuration
block/blklogwrites: Use the block device logical sector size when
logging writes

 MAINTAINERS               |   6 +
 block.c                   |   6 -
 block/Makefile.objs       |   1 +
 block/blklogwrites.c      | 420 ++++++++++++++++++++++++++++++++++++++++++++++
 block/io.c                |  22 +++
 hw/block/block.c          |  12 +-
 hw/block/fdc.c            |   4 +
 hw/block/nvme.c           |   1 +
 hw/block/virtio-blk.c     |   1 +
 hw/ide/qdev.c             |   2 +
 hw/scsi/scsi-disk.c       |   1 +
 include/block/block.h     |  17 ++
 include/block/block_int.h |   9 +
 include/hw/block/block.h  |   1 +
 qapi/block-core.json      |  29 +++-
 15 files changed, 519 insertions(+), 13 deletions(-)
 create mode 100644 block/blklogwrites.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/10 block: Move two block permission constants to the relevant enum

This allows using the two constants outside of block.c, which will happen in a subsequent patch.

---
 block.c               | 6 ------
 include/block/block.h | 7 +++++++
 2 files changed, 7 insertions(+), 6 deletions(-)
block.c +0 -6
index 501b64c..c8cffe1 100644
@@ -1914,12 +1914,6 @@ int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
     return 0;
 }
 
-#define DEFAULT_PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \
-                                 | BLK_PERM_WRITE \
-                                 | BLK_PERM_WRITE_UNCHANGED \
-                                 | BLK_PERM_RESIZE)
-#define DEFAULT_PERM_UNCHANGED (BLK_PERM_ALL & ~DEFAULT_PERM_PASSTHROUGH)
-
 void bdrv_filter_default_perms(BlockDriverState *bs, BdrvChild *c,
                                const BdrvChildRole *role,
                                BlockReopenQueue *reopen_queue,
include/block/block.h +7 -0
index 3894edd..fb7d379 100644
@@ -225,6 +225,13 @@ enum {
     BLK_PERM_GRAPH_MOD          = 0x10,
 
     BLK_PERM_ALL                = 0x1f,
+
+    DEFAULT_PERM_PASSTHROUGH    = BLK_PERM_CONSISTENT_READ
+                                 | BLK_PERM_WRITE
+                                 | BLK_PERM_WRITE_UNCHANGED
+                                 | BLK_PERM_RESIZE,
+
+    DEFAULT_PERM_UNCHANGED      = BLK_PERM_ALL & ~DEFAULT_PERM_PASSTHROUGH,
 };
 
 char *bdrv_perm_names(uint64_t perm);
Signature · 1 line
2.7.4
#2 lore raw

PATCH v2 2/10 block: Add blklogwrites

In reply to Ari Sundholm

From: Aapo Vienamo <[email protected]>

Implements a block device write logging system, similar to Linux kernel device mapper dm-log-writes. The write operations that are performed on a block device are logged to a file or another block device. The write log format is identical to the dm-log-writes format. Currently, log markers are not supported.

This functionality can be used for crash consistency and fs consistency testing. By implementing it in qemu, tests utilizing write logs can be be used to test non-Linux drivers and older kernels.

The implementation is based on the blkverify and blkdebug block drivers.

---
 MAINTAINERS          |   6 +
 block/Makefile.objs  |   1 +
 block/blklogwrites.c | 383 +++++++++++++++++++++++++++++++++++++++++++++++++++
 qapi/block-core.json |  29 +++-
 4 files changed, 413 insertions(+), 6 deletions(-)
 create mode 100644 block/blklogwrites.c
MAINTAINERS +6 -0
index 41cd373..0a360d0 100644
@@ -2014,6 +2014,12 @@ S: Supported
 F: block/quorum.c
 L: [email protected]
 
+blklogwrites
+M: Ari Sundholm <[email protected]>
+L: [email protected]
+S: Supported
+F: block/blklogwrites.c
+
 blkverify
 M: Stefan Hajnoczi <[email protected]>
 L: [email protected]
block/Makefile.objs +1 -0
index 899bfb5..c8337bf 100644
@@ -5,6 +5,7 @@ block-obj-y += qed-check.o
 block-obj-y += vhdx.o vhdx-endian.o vhdx-log.o
 block-obj-y += quorum.o
 block-obj-y += parallels.o blkdebug.o blkverify.o blkreplay.o
+block-obj-y += blklogwrites.o
 block-obj-y += block-backend.o snapshot.o qapi.o
 block-obj-$(CONFIG_WIN32) += file-win32.o win32-aio.o
 block-obj-$(CONFIG_POSIX) += file-posix.o
block/blklogwrites.c new file +383 -0

383 lines not on this page · raw message

qapi/block-core.json +23 -6
index 4b1de47..c82ec40 100644
@@ -2429,12 +2429,12 @@
 # Since: 2.9
 ##
 { 'enum': 'BlockdevDriver',
-  'data': [ 'blkdebug', 'blkverify', 'bochs', 'cloop', 'copy-on-read',
-            'dmg', 'file', 'ftp', 'ftps', 'gluster', 'host_cdrom',
-            'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs',
-            'null-aio', 'null-co', 'nvme', 'parallels', 'qcow', 'qcow2', 'qed',
-            'quorum', 'raw', 'rbd', 'replication', 'sheepdog', 'ssh',
-            'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
+  'data': [ 'blkdebug', 'blklogwrites', 'blkverify', 'bochs', 'cloop',
+            'copy-on-read', 'dmg', 'file', 'ftp', 'ftps', 'gluster',
+            'host_cdrom', 'host_device', 'http', 'https', 'iscsi', 'luks',
+            'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels', 'qcow',
+            'qcow2', 'qed', 'quorum', 'raw', 'rbd', 'replication', 'sheepdog',
+            'ssh', 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
 
 ##
 # @BlockdevOptionsFile:
@@ -2949,6 +2949,21 @@
             '*set-state': ['BlkdebugSetStateOptions'] } }
 
 ##
+# @BlockdevOptionsBlklogwrites:
+#
+# Driver specific block device options for blklogwrites.
+#
+# @raw:     block device
+#
+# @log:     block device used to log writes on @raw
+#
+# Since: foo
+##
+{ 'struct': 'BlockdevOptionsBlklogwrites',
+  'data': { 'raw': 'BlockdevRef',
+            'log': 'BlockdevRef' } }
+
+##
 # @BlockdevOptionsBlkverify:
 #
 # Driver specific block device options for blkverify.
@@ -3443,6 +3458,7 @@
   'discriminator': 'driver',
   'data': {
       'blkdebug':   'BlockdevOptionsBlkdebug',
+      'blklogwrites': 'BlockdevOptionsBlklogwrites',
       'blkverify':  'BlockdevOptionsBlkverify',
       'bochs':      'BlockdevOptionsGenericFormat',
       'cloop':      'BlockdevOptionsGenericFormat',
@@ -3971,6 +3987,7 @@
   'discriminator': 'driver',
   'data': {
       'blkdebug':       'BlockdevCreateNotSupported',
+      'blklogwrites':   'BlockdevCreateNotSupported',
       'blkverify':      'BlockdevCreateNotSupported',
       'bochs':          'BlockdevCreateNotSupported',
       'cloop':          'BlockdevCreateNotSupported',
Signature · 1 line
2.7.4
#3 lore raw

Re: [Qemu-devel] [PATCH v2 02/10] block: Add blklogwrites

Quoted 20 lines · Ari Sundholm

From: Aapo Vienamo <[email protected]>

Implements a block device write logging system, similar to Linux kernel device mapper dm-log-writes. The write operations that are performed on a block device are logged to a file or another block device. The write log format is identical to the dm-log-writes format. Currently, log markers are not supported.

This functionality can be used for crash consistency and fs consistency testing. By implementing it in qemu, tests utilizing write logs can be be used to test non-Linux drivers and older kernels.

The implementation is based on the blkverify and blkdebug block drivers.

---

+++ b/qapi/block-core.json

qapi/block-core.json line 2431

Quoted 3 lines
qapi/block-core.json +0 -0
@@ -2429,12 +2429,12 @@
  # Since: 2.9
  ##
  { 'enum': 'BlockdevDriver',

Missing a comment:

# @blklogwrites: Since 3.0

Quoted 18 lines
qapi/block-core.json +6 -6
-  'data': [ 'blkdebug', 'blkverify', 'bochs', 'cloop', 'copy-on-read',
-            'dmg', 'file', 'ftp', 'ftps', 'gluster', 'host_cdrom',
-            'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs',
-            'null-aio', 'null-co', 'nvme', 'parallels', 'qcow', 'qcow2', 'qed',
-            'quorum', 'raw', 'rbd', 'replication', 'sheepdog', 'ssh',
-            'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
+  'data': [ 'blkdebug', 'blklogwrites', 'blkverify', 'bochs', 'cloop',
+            'copy-on-read', 'dmg', 'file', 'ftp', 'ftps', 'gluster',
+            'host_cdrom', 'host_device', 'http', 'https', 'iscsi', 'luks',
+            'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels', 'qcow',
+            'qcow2', 'qed', 'quorum', 'raw', 'rbd', 'replication', 'sheepdog',
+            'ssh', 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
 
  ##
  # @BlockdevOptionsFile:
@@ -2949,6 +2949,21 @@
              '*set-state': ['BlkdebugSetStateOptions'] } }
 

qapi/block-core.json line 2960

Quoted 10 lines
qapi/block-core.json +9 -0
  ##
+# @BlockdevOptionsBlklogwrites:
+#
+# Driver specific block device options for blklogwrites.
+#
+# @raw:     block device
+#
+# @log:     block device used to log writes on @raw
+#
+# Since: foo

s/foo/3.0/

Quoted 26 lines
qapi/block-core.json +8 -0
+##
+{ 'struct': 'BlockdevOptionsBlklogwrites',
+  'data': { 'raw': 'BlockdevRef',
+            'log': 'BlockdevRef' } }
+
+##
  # @BlockdevOptionsBlkverify:
  #
  # Driver specific block device options for blkverify.
@@ -3443,6 +3458,7 @@
    'discriminator': 'driver',
    'data': {
        'blkdebug':   'BlockdevOptionsBlkdebug',
+      'blklogwrites': 'BlockdevOptionsBlklogwrites',
        'blkverify':  'BlockdevOptionsBlkverify',
        'bochs':      'BlockdevOptionsGenericFormat',
        'cloop':      'BlockdevOptionsGenericFormat',
@@ -3971,6 +3987,7 @@
    'discriminator': 'driver',
    'data': {
        'blkdebug':       'BlockdevCreateNotSupported',
+      'blklogwrites':   'BlockdevCreateNotSupported',
        'blkverify':      'BlockdevCreateNotSupported',
        'bochs':          'BlockdevCreateNotSupported',
        'cloop':          'BlockdevCreateNotSupported',
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#4 lore raw

Re: [Qemu-devel] [PATCH v2 02/10] block: Add blklogwrites

Quoted 27 lines · Eric Blake
Quoted 20 lines · Ari Sundholm

From: Aapo Vienamo <[email protected]>

Implements a block device write logging system, similar to Linux kernel device mapper dm-log-writes. The write operations that are performed on a block device are logged to a file or another block device. The write log format is identical to the dm-log-writes format. Currently, log markers are not supported.

This functionality can be used for crash consistency and fs consistency testing. By implementing it in qemu, tests utilizing write logs can be be used to test non-Linux drivers and older kernels.

The implementation is based on the blkverify and blkdebug block drivers.

---

+++ b/qapi/block-core.json

qapi/block-core.json line 2431

Quoted 3 lines
qapi/block-core.json +0 -0
@@ -2429,12 +2429,12 @@
   # Since: 2.9
   ##
   { 'enum': 'BlockdevDriver',

Missing a comment:

Quoted 2 lines

# @blklogwrites: Since 3.0

Thanks, will add in v4.

Quoted 27 lines
Quoted 27 lines
(quoted diff) +3 -6
-  'data': [ 'blkdebug', 'blkverify', 'bochs', 'cloop', 'copy-on-read',
-            'dmg', 'file', 'ftp', 'ftps', 'gluster', 'host_cdrom',
-            'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 
 'nfs',
-            'null-aio', 'null-co', 'nvme', 'parallels', 'qcow', 
 'qcow2', 'qed',
-            'quorum', 'raw', 'rbd', 'replication', 'sheepdog', 'ssh',
-            'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' 
 ] }
+  'data': [ 'blkdebug', 'blklogwrites', 'blkverify', 'bochs', 'cloop',
+            'copy-on-read', 'dmg', 'file', 'ftp', 'ftps', 'gluster',
+            'host_cdrom', 'host_device', 'http', 'https', 'iscsi', 

'luks',
+            'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels', 'qcow',
+            'qcow2', 'qed', 'quorum', 'raw', 'rbd', 'replication', 'sheepdog',
+            'ssh', 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
  ##
  # @BlockdevOptionsFile:

qapi/block-core.json +3 -0
@@ -2949,6 +2949,21 @@
               '*set-state': ['BlkdebugSetStateOptions'] } }
   ##
+# @BlockdevOptionsBlklogwrites:
+#
+# Driver specific block device options for blklogwrites.
Quoted 9 lines

qapi/block-core.json line 2960

Quoted 6 lines
qapi/block-core.json +6 -0
+#
+# @raw:     block device
+#
+# @log:     block device used to log writes on @raw
+#
+# Since: foo

s/foo/3.0/

Oops, this was a placeholder that I ended up forgetting about. Will be fixed in v4.

Thanks,
Ari Sundholm
[email protected]

Quoted 27 lines
Quoted 26 lines
qapi/block-core.json +8 -0
+##
+{ 'struct': 'BlockdevOptionsBlklogwrites',
+  'data': { 'raw': 'BlockdevRef',
+            'log': 'BlockdevRef' } }
+
+##
   # @BlockdevOptionsBlkverify:
   #
   # Driver specific block device options for blkverify.
@@ -3443,6 +3458,7 @@
     'discriminator': 'driver',
     'data': {
         'blkdebug':   'BlockdevOptionsBlkdebug',
+      'blklogwrites': 'BlockdevOptionsBlklogwrites',
         'blkverify':  'BlockdevOptionsBlkverify',
         'bochs':      'BlockdevOptionsGenericFormat',
         'cloop':      'BlockdevOptionsGenericFormat',
@@ -3971,6 +3987,7 @@
     'discriminator': 'driver',
     'data': {
         'blkdebug':       'BlockdevCreateNotSupported',
+      'blklogwrites':   'BlockdevCreateNotSupported',
         'blkverify':      'BlockdevCreateNotSupported',
         'bochs':          'BlockdevCreateNotSupported',
         'cloop':          'BlockdevCreateNotSupported',
#5 lore raw

PATCH v2 3/10 block: Add a mechanism for passing a block driver a block configuration

In reply to Ari Sundholm

A block driver may need to know about the block configuration, most critically the sector sizes, of a block backend for alignment purposes or for some other reason. It doesn't seem like qemu has an existing mechanism for a block backend to convey the required information to the relevant block driver when it is being realized.

The need for this mechanism rises from the fact that a drive may not have a backend at the time it is created, as devices are created after drives during qemu startup. Therefore, a driver requiring information about the block configuration can get this information when a backend is created for it at the earliest. The most natural place for this to take place seems to be in the realization functions of the various block device drivers, such as scsi-hd. The interface proposed here allows the block driver to receive information about the block configuration and the associated backend through a new callback.

---
 block/io.c                | 22 ++++++++++++++++++++++
 hw/block/block.c          | 12 +++++++++++-
 include/block/block.h     | 10 ++++++++++
 include/block/block_int.h |  9 +++++++++
 include/hw/block/block.h  |  1 +
 5 files changed, 53 insertions(+), 1 deletion(-)
block/io.c +22 -0
index ca96b48..1f3211a 100644
@@ -2835,3 +2835,25 @@ void bdrv_unregister_buf(BlockDriverState *bs, void *host)
         bdrv_unregister_buf(child->bs, host);
     }
 }
+
+void bdrv_apply_blkconf(BlockDriverState *bs, BlockConf *conf)
+{
+    BlockDriver *drv = bs->drv;
+
+    if (!drv) {
+        return;
+    }
+
+    if (drv->bdrv_apply_blkconf) {
+        drv->bdrv_apply_blkconf(bs, conf);
+        return;
+    }
+
+    if (bs->file && bs->file->bs) {
+        bdrv_apply_blkconf(bs->file->bs, conf);
+    }
+
+    if (bs->drv->supports_backing && backing_bs(bs)) {
+        bdrv_apply_blkconf(backing_bs(bs), conf);
+    }
+}
hw/block/block.c +11 -1
index b91e2b6..8c87dbf 100644
@@ -38,7 +38,7 @@ void blkconf_blocksizes(BlockConf *conf)
     /* fill in detected values if they are not defined via qemu command line */
     if (!conf->physical_block_size) {
         if (!backend_ret) {
-           conf->physical_block_size = blocksizes.phys;
+            conf->physical_block_size = blocksizes.phys;
         } else {
             conf->physical_block_size = BDRV_SECTOR_SIZE;
         }
@@ -52,6 +52,16 @@ void blkconf_blocksizes(BlockConf *conf)
     }
 }
 
+void blkconf_apply_to_blkdrv(BlockConf *conf)
+{
+    BlockBackend *blk = conf->blk;
+    BlockDriverState *bs = blk_bs(blk);
+
+    if (bs) {
+        bdrv_apply_blkconf(bs, conf);
+    }
+}
+
 bool blkconf_apply_backend_options(BlockConf *conf, bool readonly,
                                    bool resizable, Error **errp)
 {
include/block/block.h +10 -0
index fb7d379..57a8b65 100644
@@ -10,6 +10,7 @@
 #include "block/dirty-bitmap.h"
 #include "block/blockjob.h"
 #include "qemu/hbitmap.h"
+#include "hw/block/block.h"
 
 /* block.c */
 typedef struct BlockDriver BlockDriver;
@@ -618,4 +619,13 @@ bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
  */
 void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size);
 void bdrv_unregister_buf(BlockDriverState *bs, void *host);
+
+/**
+ * bdrv_apply_blkconf:
+ *
+ * Recursively finds the highest-level block drivers among the files and
+ * backing files that accept a block configuration and applies the given block
+ * configuration to them.
+ */
+void bdrv_apply_blkconf(BlockDriverState *bs, BlockConf *conf);
 #endif
include/block/block_int.h +9 -0
index 6c0927b..1d99a0d 100644
@@ -433,6 +433,15 @@ struct BlockDriver {
     void (*bdrv_abort_perm_update)(BlockDriverState *bs);
 
     /**
+     * Called to inform the driver of the block configuration of the virtual
+     * block device.
+     *
+     * This function is called by a block device realization function if the
+     * device wants to inform the block driver of its block configuration.
+     */
+    void (*bdrv_apply_blkconf)(BlockDriverState *bs, BlockConf *conf);
+
+    /**
      * Returns in @nperm and @nshared the permissions that the driver for @bs
      * needs on its child @c, based on the cumulative permissions requested by
      * the parents in @parent_perm and @parent_shared.
include/hw/block/block.h +1 -0
index d4f4dff..2861995 100644
@@ -77,6 +77,7 @@ bool blkconf_geometry(BlockConf *conf, int *trans,
                       unsigned cyls_max, unsigned heads_max, unsigned secs_max,
                       Error **errp);
 void blkconf_blocksizes(BlockConf *conf);
+void blkconf_apply_to_blkdrv(BlockConf *conf);
 bool blkconf_apply_backend_options(BlockConf *conf, bool readonly,
                                    bool resizable, Error **errp);
 
Signature · 1 line
2.7.4
#6 lore raw

PATCH v2 4/10 hw/scsi/scsi-disk: Always apply block configuration to block driver

In reply to Ari Sundholm

This allows the block driver to use the block configuration of the new SCSI device. One use for this information is to set request limits using this information.

---
 hw/scsi/scsi-disk.c | 1 +
 1 file changed, 1 insertion(+)
hw/scsi/scsi-disk.c +1 -0
index ded23d3..d50517e 100644
@@ -2413,6 +2413,7 @@ static void scsi_realize(SCSIDevice *dev, Error **errp)
         blk_set_dev_ops(s->qdev.conf.blk, &scsi_disk_block_ops, s);
     }
     blk_set_guest_block_size(s->qdev.conf.blk, s->qdev.blocksize);
+    blkconf_apply_to_blkdrv(&s->qdev.conf);
 
     blk_iostatus_enable(s->qdev.conf.blk);
 }
Signature · 1 line
2.7.4
#7 lore raw

PATCH v2 6/6 block/blklogwrites: Use the block device logical sector size when logging writes

In reply to Ari Sundholm

The guest OS may perform writes which are aligned to the logical sector size instead of the physical one, so logging at this granularity records the writes performed on the block device most faithfully.

---
 block/blklogwrites.c | 47 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 15 deletions(-)
block/blklogwrites.c +32 -15
index 3e33964..280af8d 100644
@@ -47,6 +47,8 @@ struct log_write_entry {
 
 typedef struct {
     BdrvChild *log_file;
+    uint32_t sectorsize;
+    uint32_t sectorbits;
     uint64_t cur_log_sector;
     uint64_t nr_entries;
 } BDRVBlkLogWritesState;
@@ -67,6 +69,8 @@ static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
+    s->sectorsize = BDRV_SECTOR_SIZE; /* May be updated later */
+    s->sectorbits = BDRV_SECTOR_BITS;
     s->cur_log_sector = 1;
     s->nr_entries = 0;
 
@@ -168,11 +172,20 @@ static void blk_log_writes_refresh_limits(BlockDriverState *bs, Error **errp)
     }
 }
 
+static inline uint32_t blk_log_writes_log2(uint32_t value)
+{
+    assert(value > 0);
+    return 31 - clz32(value);
+}
+
 static void blk_log_writes_apply_blkconf(BlockDriverState *bs, BlockConf *conf)
 {
-    assert(bs && conf && conf->blk);
+    BDRVBlkLogWritesState *s = bs->opaque;
+    assert(bs && conf && conf->blk && s);
 
-    bs->bl.request_alignment = conf->logical_block_size;
+    s->sectorsize = conf->logical_block_size;
+    s->sectorbits = blk_log_writes_log2(s->sectorsize);
+    bs->bl.request_alignment = s->sectorsize;
     if (conf->discard_granularity != (uint32_t)-1) {
         bs->bl.pdiscard_alignment = conf->discard_granularity;
     }
@@ -222,20 +235,20 @@ static void coroutine_fn blk_log_writes_co_do_log(void *opaque)
 {
     BlkLogWritesLogReq *lr = opaque;
     BDRVBlkLogWritesState *s = lr->r->bs->opaque;
-    uint64_t cur_log_offset = s->cur_log_sector << BDRV_SECTOR_BITS;
+    uint64_t cur_log_offset = s->cur_log_sector << s->sectorbits;
 
     s->nr_entries++;
     s->cur_log_sector +=
-            ROUND_UP(lr->qiov->size, BDRV_SECTOR_SIZE) >> BDRV_SECTOR_BITS;
+            ROUND_UP(lr->qiov->size, s->sectorsize) >> s->sectorbits;
 
     lr->log_ret = bdrv_co_pwritev(s->log_file, cur_log_offset, lr->qiov->size,
                                   lr->qiov, 0);
 
     /* Logging for the "write zeroes" operation */
     if (lr->log_ret == 0 && lr->zero_size) {
-        cur_log_offset = s->cur_log_sector << BDRV_SECTOR_BITS;
+        cur_log_offset = s->cur_log_sector << s->sectorbits;
         s->cur_log_sector +=
-                ROUND_UP(lr->zero_size, BDRV_SECTOR_SIZE) >> BDRV_SECTOR_BITS;
+                ROUND_UP(lr->zero_size, s->sectorsize) >> s->sectorbits;
 
         lr->log_ret = bdrv_co_pwrite_zeroes(s->log_file, cur_log_offset,
                                             lr->zero_size, 0);
@@ -247,21 +260,22 @@ static void coroutine_fn blk_log_writes_co_do_log(void *opaque)
             .magic      = cpu_to_le64(WRITE_LOG_MAGIC),
             .version    = cpu_to_le64(WRITE_LOG_VERSION),
             .nr_entries = cpu_to_le64(s->nr_entries),
-            .sectorsize = cpu_to_le32(1 << BDRV_SECTOR_BITS),
+            .sectorsize = cpu_to_le32(s->sectorsize),
         };
-        static const char zeroes[BDRV_SECTOR_SIZE - sizeof(super)] = { '\0' };
+        void *zeroes = g_malloc0(s->sectorsize - sizeof(super));
         QEMUIOVector qiov;
 
         qemu_iovec_init(&qiov, 2);
         qemu_iovec_add(&qiov, &super, sizeof(super));
-        qemu_iovec_add(&qiov, (void *)zeroes, sizeof(zeroes));
+        qemu_iovec_add(&qiov, zeroes, s->sectorsize - sizeof(super));
 
         lr->log_ret =
-            bdrv_co_pwritev(s->log_file, 0, BDRV_SECTOR_SIZE, &qiov, 0);
+            bdrv_co_pwritev(s->log_file, 0, s->sectorsize, &qiov, 0);
         if (lr->log_ret == 0) {
             lr->log_ret = bdrv_co_flush(s->log_file->bs);
         }
         qemu_iovec_destroy(&qiov);
+        g_free(zeroes);
     }
 
     lr->r->done++;
@@ -286,6 +300,7 @@ blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 {
     QEMUIOVector log_qiov;
     size_t niov = qiov ? qiov->niov : 0;
+    BDRVBlkLogWritesState *s = bs->opaque;
     Coroutine *co_log, *co_file;
     BlkLogWritesReq r = {
         .co     = qemu_coroutine_self(),
@@ -304,22 +319,23 @@ blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
         .r              = &r,
         .qiov           = &log_qiov,
         .entry = {
-            .sector     = cpu_to_le64(offset >> BDRV_SECTOR_BITS),
-            .nr_sectors = cpu_to_le64(bytes >> BDRV_SECTOR_BITS),
+            .sector     = cpu_to_le64(offset >> s->sectorbits),
+            .nr_sectors = cpu_to_le64(bytes >> s->sectorbits),
             .flags      = cpu_to_le64(entry_flags),
             .data_len   = 0,
         },
         .zero_size = is_zero_write ? bytes : 0,
     };
-    static const char zeroes[BDRV_SECTOR_SIZE - sizeof(struct log_write_entry)]
-        = { '\0' };
+    void *zeroes = g_malloc0(s->sectorsize - sizeof(lr.entry));
 
+    assert((1 << s->sectorbits) == s->sectorsize);
+    assert(bs->bl.request_alignment == s->sectorsize);
     assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
     assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
 
     qemu_iovec_init(&log_qiov, niov + 2);
     qemu_iovec_add(&log_qiov, &lr.entry, sizeof(lr.entry));
-    qemu_iovec_add(&log_qiov, (void *)zeroes, sizeof(zeroes));
+    qemu_iovec_add(&log_qiov, zeroes, s->sectorsize - sizeof(lr.entry));
     for (size_t i = 0; i < niov; ++i) {
         qemu_iovec_add(&log_qiov, qiov->iov[i].iov_base, qiov->iov[i].iov_len);
     }
@@ -335,6 +351,7 @@ blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
     }
 
     qemu_iovec_destroy(&log_qiov);
+    g_free(zeroes);
 
     if (lr.log_ret < 0) {
         return lr.log_ret;
Signature · 1 line
2.7.4
#8 lore raw

Re: [Qemu-devel] [PATCH v2 6/6] block/blklogwrites: Use the block device logical sector size when logging writes

Oops, it seems this old patch found its way in this series somehow. My apologies.

And sorry for this top-post.

Best regards,
Ari Sundholm
[email protected]

Quoted 152 lines · Ari Sundholm

The guest OS may perform writes which are aligned to the logical sector size instead of the physical one, so logging at this granularity records the writes performed on the block device most faithfully.

---
  block/blklogwrites.c | 47 ++++++++++++++++++++++++++++++++---------------
  1 file changed, 32 insertions(+), 15 deletions(-)
block/blklogwrites.c +32 -15
index 3e33964..280af8d 100644
@@ -47,6 +47,8 @@ struct log_write_entry {
 
  typedef struct {
      BdrvChild *log_file;
+    uint32_t sectorsize;
+    uint32_t sectorbits;
      uint64_t cur_log_sector;
      uint64_t nr_entries;
  } BDRVBlkLogWritesState;
@@ -67,6 +69,8 @@ static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags,
          goto fail;
      }
 
+    s->sectorsize = BDRV_SECTOR_SIZE; /* May be updated later */
+    s->sectorbits = BDRV_SECTOR_BITS;
      s->cur_log_sector = 1;
      s->nr_entries = 0;
 
@@ -168,11 +172,20 @@ static void blk_log_writes_refresh_limits(BlockDriverState *bs, Error **errp)
      }
  }
 
+static inline uint32_t blk_log_writes_log2(uint32_t value)
+{
+    assert(value > 0);
+    return 31 - clz32(value);
+}
+
  static void blk_log_writes_apply_blkconf(BlockDriverState *bs, BlockConf *conf)
  {
-    assert(bs && conf && conf->blk);
+    BDRVBlkLogWritesState *s = bs->opaque;
+    assert(bs && conf && conf->blk && s);
 
-    bs->bl.request_alignment = conf->logical_block_size;
+    s->sectorsize = conf->logical_block_size;
+    s->sectorbits = blk_log_writes_log2(s->sectorsize);
+    bs->bl.request_alignment = s->sectorsize;
      if (conf->discard_granularity != (uint32_t)-1) {
          bs->bl.pdiscard_alignment = conf->discard_granularity;
      }
@@ -222,20 +235,20 @@ static void coroutine_fn blk_log_writes_co_do_log(void *opaque)
  {
      BlkLogWritesLogReq *lr = opaque;
      BDRVBlkLogWritesState *s = lr->r->bs->opaque;
-    uint64_t cur_log_offset = s->cur_log_sector << BDRV_SECTOR_BITS;
+    uint64_t cur_log_offset = s->cur_log_sector << s->sectorbits;
 
      s->nr_entries++;
      s->cur_log_sector +=
-            ROUND_UP(lr->qiov->size, BDRV_SECTOR_SIZE) >> BDRV_SECTOR_BITS;
+            ROUND_UP(lr->qiov->size, s->sectorsize) >> s->sectorbits;
 
      lr->log_ret = bdrv_co_pwritev(s->log_file, cur_log_offset, lr->qiov->size,
                                    lr->qiov, 0);
 
      /* Logging for the "write zeroes" operation */
      if (lr->log_ret == 0 && lr->zero_size) {
-        cur_log_offset = s->cur_log_sector << BDRV_SECTOR_BITS;
+        cur_log_offset = s->cur_log_sector << s->sectorbits;
          s->cur_log_sector +=
-                ROUND_UP(lr->zero_size, BDRV_SECTOR_SIZE) >> BDRV_SECTOR_BITS;
+                ROUND_UP(lr->zero_size, s->sectorsize) >> s->sectorbits;
 
          lr->log_ret = bdrv_co_pwrite_zeroes(s->log_file, cur_log_offset,
                                              lr->zero_size, 0);
@@ -247,21 +260,22 @@ static void coroutine_fn blk_log_writes_co_do_log(void *opaque)
              .magic      = cpu_to_le64(WRITE_LOG_MAGIC),
              .version    = cpu_to_le64(WRITE_LOG_VERSION),
              .nr_entries = cpu_to_le64(s->nr_entries),
-            .sectorsize = cpu_to_le32(1 << BDRV_SECTOR_BITS),
+            .sectorsize = cpu_to_le32(s->sectorsize),
          };
-        static const char zeroes[BDRV_SECTOR_SIZE - sizeof(super)] = { '\0' };
+        void *zeroes = g_malloc0(s->sectorsize - sizeof(super));
          QEMUIOVector qiov;
 
          qemu_iovec_init(&qiov, 2);
          qemu_iovec_add(&qiov, &super, sizeof(super));
-        qemu_iovec_add(&qiov, (void *)zeroes, sizeof(zeroes));
+        qemu_iovec_add(&qiov, zeroes, s->sectorsize - sizeof(super));
 
          lr->log_ret =
-            bdrv_co_pwritev(s->log_file, 0, BDRV_SECTOR_SIZE, &qiov, 0);
+            bdrv_co_pwritev(s->log_file, 0, s->sectorsize, &qiov, 0);
          if (lr->log_ret == 0) {
              lr->log_ret = bdrv_co_flush(s->log_file->bs);
          }
          qemu_iovec_destroy(&qiov);
+        g_free(zeroes);
      }
 
      lr->r->done++;
@@ -286,6 +300,7 @@ blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
  {
      QEMUIOVector log_qiov;
      size_t niov = qiov ? qiov->niov : 0;
+    BDRVBlkLogWritesState *s = bs->opaque;
      Coroutine *co_log, *co_file;
      BlkLogWritesReq r = {
          .co     = qemu_coroutine_self(),
@@ -304,22 +319,23 @@ blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
          .r              = &r,
          .qiov           = &log_qiov,
          .entry = {
-            .sector     = cpu_to_le64(offset >> BDRV_SECTOR_BITS),
-            .nr_sectors = cpu_to_le64(bytes >> BDRV_SECTOR_BITS),
+            .sector     = cpu_to_le64(offset >> s->sectorbits),
+            .nr_sectors = cpu_to_le64(bytes >> s->sectorbits),
              .flags      = cpu_to_le64(entry_flags),
              .data_len   = 0,
          },
          .zero_size = is_zero_write ? bytes : 0,
      };
-    static const char zeroes[BDRV_SECTOR_SIZE - sizeof(struct log_write_entry)]
-        = { '\0' };
+    void *zeroes = g_malloc0(s->sectorsize - sizeof(lr.entry));
 
+    assert((1 << s->sectorbits) == s->sectorsize);
+    assert(bs->bl.request_alignment == s->sectorsize);
      assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
      assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
 
      qemu_iovec_init(&log_qiov, niov + 2);
      qemu_iovec_add(&log_qiov, &lr.entry, sizeof(lr.entry));
-    qemu_iovec_add(&log_qiov, (void *)zeroes, sizeof(zeroes));
+    qemu_iovec_add(&log_qiov, zeroes, s->sectorsize - sizeof(lr.entry));
      for (size_t i = 0; i < niov; ++i) {
          qemu_iovec_add(&log_qiov, qiov->iov[i].iov_base, qiov->iov[i].iov_len);
      }
@@ -335,6 +351,7 @@ blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
      }
 
      qemu_iovec_destroy(&log_qiov);
+    g_free(zeroes);
 
      if (lr.log_ret < 0) {
          return lr.log_ret;
#9 lore raw

PATCH v2 5/10 hw/ide/qdev: Always apply block configuration to block driver

In reply to Ari Sundholm

This allows the block driver to use the block configuration of the new IDE device. One use for this information is to set request limits using this information.

---
 hw/ide/qdev.c | 2 ++
 1 file changed, 2 insertions(+)
hw/ide/qdev.c +2 -0
index f395d24..7da1d28 100644
@@ -214,6 +214,8 @@ static void ide_dev_initfn(IDEDevice *dev, IDEDriveKind kind, Error **errp)
         dev->serial = g_strdup(s->drive_serial_str);
     }
 
+    blkconf_apply_to_blkdrv(&dev->conf);
+
     add_boot_device_path(dev->conf.bootindex, &dev->qdev,
                          dev->unit ? "/disk@1" : "/disk@0");
 }
Signature · 1 line
2.7.4
#10 lore raw

PATCH v2 6/10 hw/block/virtio-blk: Always apply block configuration to block driver

In reply to Ari Sundholm

This allows the block driver to use the block configuration of the new VirtIO block device. One use for this information is to set request limits using this information.

---
 hw/block/virtio-blk.c | 1 +
 1 file changed, 1 insertion(+)
hw/block/virtio-blk.c +1 -0
index b1532e4..9103760 100644
@@ -976,6 +976,7 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp)
     s->change = qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
     blk_set_dev_ops(s->blk, &virtio_block_ops, s);
     blk_set_guest_block_size(s->blk, s->conf.conf.logical_block_size);
+    blkconf_apply_to_blkdrv(&s->conf.conf);
 
     blk_iostatus_enable(s->blk);
 }
Signature · 1 line
2.7.4
#11 lore raw

PATCH v2 7/10 hw/block/nvme: Always apply block configuration to block driver

In reply to Ari Sundholm

This allows the block driver to use the block configuration of the new NVMe device. One use for this information is to set request limits using this information.

---
 hw/block/nvme.c | 1 +
 1 file changed, 1 insertion(+)
hw/block/nvme.c +1 -0
index 85d2406..e1134d9 100644
@@ -1224,6 +1224,7 @@ static void nvme_realize(PCIDevice *pci_dev, Error **errp)
                                        false, errp)) {
         return;
     }
+    blkconf_apply_to_blkdrv(&n->conf);
 
     pci_conf = pci_dev->config;
     pci_conf[PCI_INTERRUPT_PIN] = 1;
Signature · 1 line
2.7.4
#12 lore raw

PATCH v2 8/10 hw/block/fdc: Always apply block configuration to block driver

In reply to Ari Sundholm

This allows the block driver to use the block configuration of the new floppy device. One use for this information is to set request limits using this information.

---
 hw/block/fdc.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/block/fdc.c +4 -0
index cd29e27..b11eeda 100644
@@ -482,6 +482,8 @@ static void fd_change_cb(void *opaque, bool load, Error **errp)
                                            errp)) {
             return;
         }
+
+        blkconf_apply_to_blkdrv(drive->conf);
     }
 
     drive->media_changed = 1;
@@ -594,6 +596,8 @@ static void floppy_drive_realize(DeviceState *qdev, Error **errp)
     pick_drive_type(drive);
     dev->type = drive->drive;
 
+    blkconf_apply_to_blkdrv(&dev->conf);
+
     fd_revalidate(drive);
 }
 
Signature · 1 line
2.7.4
#13 lore raw

PATCH v2 9/10 block/blklogwrites: Use block limits from the backend block configuration

In reply to Ari Sundholm

This is to ensure that writes are aligned properly for logging writes to the virtual block device. This is important because the dm-log-writes log format has a granularity of one sector for both the offset and the size of each write. By using the logical sector size for alignment, the log records the writes more faithfully for those devices that have a non-512 logical sector size.

Note that even with this patch, blklogwrites still uses BDRV_SECTOR_SIZE for logging. This will change in a subsequent patch which will introduce support for non-512 sector sizes.

---
 block/blklogwrites.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
block/blklogwrites.c +20 -0
index 0e60449..8b09e12 100644
@@ -168,6 +168,25 @@ static void blk_log_writes_refresh_limits(BlockDriverState *bs, Error **errp)
     }
 }
 
+static void blk_log_writes_apply_blkconf(BlockDriverState *bs, BlockConf *conf)
+{
+    assert(bs && conf && conf->blk);
+
+    bs->bl.request_alignment = conf->logical_block_size;
+    if (conf->discard_granularity != (uint32_t)-1) {
+        bs->bl.pdiscard_alignment = conf->discard_granularity;
+    }
+
+    if (bs->bl.pdiscard_alignment &&
+            bs->bl.pdiscard_alignment < bs->bl.request_alignment) {
+        bs->bl.pdiscard_alignment = bs->bl.request_alignment;
+    }
+    if (bs->bl.pwrite_zeroes_alignment &&
+            bs->bl.pwrite_zeroes_alignment < bs->bl.request_alignment) {
+        bs->bl.pwrite_zeroes_alignment = bs->bl.request_alignment;
+    }
+}
+
 static int coroutine_fn
 blk_log_writes_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
                          QEMUIOVector *qiov, int flags)
@@ -364,6 +383,7 @@ static BlockDriver bdrv_blk_log_writes = {
     .bdrv_refresh_filename  = blk_log_writes_refresh_filename,
     .bdrv_child_perm        = blk_log_writes_child_perm,
     .bdrv_refresh_limits    = blk_log_writes_refresh_limits,
+    .bdrv_apply_blkconf     = blk_log_writes_apply_blkconf,
 
     .bdrv_co_preadv         = blk_log_writes_co_preadv,
     .bdrv_co_pwritev        = blk_log_writes_co_pwritev,
Signature · 1 line
2.7.4
#14 lore raw

PATCH v2 10/10 block/blklogwrites: Use the block device logical sector size when logging writes

In reply to Ari Sundholm

The guest OS may perform writes which are aligned to the logical sector size instead of the physical one, so logging at this granularity records the writes performed on the block device most faithfully.

---
 block/blklogwrites.c | 47 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 15 deletions(-)
block/blklogwrites.c +32 -15
index 8b09e12..52166f2 100644
@@ -47,6 +47,8 @@ struct log_write_entry {
 
 typedef struct {
     BdrvChild *log_file;
+    uint32_t sectorsize;
+    uint32_t sectorbits;
     uint64_t cur_log_sector;
     uint64_t nr_entries;
 } BDRVBlkLogWritesState;
@@ -67,6 +69,8 @@ static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
+    s->sectorsize = BDRV_SECTOR_SIZE; /* May be updated later */
+    s->sectorbits = BDRV_SECTOR_BITS;
     s->cur_log_sector = 1;
     s->nr_entries = 0;
 
@@ -168,11 +172,20 @@ static void blk_log_writes_refresh_limits(BlockDriverState *bs, Error **errp)
     }
 }
 
+static inline uint32_t blk_log_writes_log2(uint32_t value)
+{
+    assert(value > 0);
+    return 31 - clz32(value);
+}
+
 static void blk_log_writes_apply_blkconf(BlockDriverState *bs, BlockConf *conf)
 {
-    assert(bs && conf && conf->blk);
+    BDRVBlkLogWritesState *s = bs->opaque;
+    assert(bs && conf && conf->blk && s);
 
-    bs->bl.request_alignment = conf->logical_block_size;
+    s->sectorsize = conf->logical_block_size;
+    s->sectorbits = blk_log_writes_log2(s->sectorsize);
+    bs->bl.request_alignment = s->sectorsize;
     if (conf->discard_granularity != (uint32_t)-1) {
         bs->bl.pdiscard_alignment = conf->discard_granularity;
     }
@@ -215,20 +228,20 @@ typedef struct {
 static void coroutine_fn blk_log_writes_co_do_log(BlkLogWritesLogReq *lr)
 {
     BDRVBlkLogWritesState *s = lr->bs->opaque;
-    uint64_t cur_log_offset = s->cur_log_sector << BDRV_SECTOR_BITS;
+    uint64_t cur_log_offset = s->cur_log_sector << s->sectorbits;
 
     s->nr_entries++;
     s->cur_log_sector +=
-            ROUND_UP(lr->qiov->size, BDRV_SECTOR_SIZE) >> BDRV_SECTOR_BITS;
+            ROUND_UP(lr->qiov->size, s->sectorsize) >> s->sectorbits;
 
     lr->log_ret = bdrv_co_pwritev(s->log_file, cur_log_offset, lr->qiov->size,
                                   lr->qiov, 0);
 
     /* Logging for the "write zeroes" operation */
     if (lr->log_ret == 0 && lr->zero_size) {
-        cur_log_offset = s->cur_log_sector << BDRV_SECTOR_BITS;
+        cur_log_offset = s->cur_log_sector << s->sectorbits;
         s->cur_log_sector +=
-                ROUND_UP(lr->zero_size, BDRV_SECTOR_SIZE) >> BDRV_SECTOR_BITS;
+                ROUND_UP(lr->zero_size, s->sectorsize) >> s->sectorbits;
 
         lr->log_ret = bdrv_co_pwrite_zeroes(s->log_file, cur_log_offset,
                                             lr->zero_size, 0);
@@ -240,21 +253,22 @@ static void coroutine_fn blk_log_writes_co_do_log(BlkLogWritesLogReq *lr)
             .magic      = cpu_to_le64(WRITE_LOG_MAGIC),
             .version    = cpu_to_le64(WRITE_LOG_VERSION),
             .nr_entries = cpu_to_le64(s->nr_entries),
-            .sectorsize = cpu_to_le32(1 << BDRV_SECTOR_BITS),
+            .sectorsize = cpu_to_le32(s->sectorsize),
         };
-        static const char zeroes[BDRV_SECTOR_SIZE - sizeof(super)] = { '\0' };
+        void *zeroes = g_malloc0(s->sectorsize - sizeof(super));
         QEMUIOVector qiov;
 
         qemu_iovec_init(&qiov, 2);
         qemu_iovec_add(&qiov, &super, sizeof(super));
-        qemu_iovec_add(&qiov, (void *)zeroes, sizeof(zeroes));
+        qemu_iovec_add(&qiov, zeroes, s->sectorsize - sizeof(super));
 
         lr->log_ret =
-            bdrv_co_pwritev(s->log_file, 0, BDRV_SECTOR_SIZE, &qiov, 0);
+            bdrv_co_pwritev(s->log_file, 0, s->sectorsize, &qiov, 0);
         if (lr->log_ret == 0) {
             lr->log_ret = bdrv_co_flush(s->log_file->bs);
         }
         qemu_iovec_destroy(&qiov);
+        g_free(zeroes);
     }
 }
 
@@ -271,6 +285,7 @@ blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 {
     QEMUIOVector log_qiov;
     size_t niov = qiov ? qiov->niov : 0;
+    BDRVBlkLogWritesState *s = bs->opaque;
     BlkLogWritesFileReq fr = {
         .bs     = bs,
         .offset = offset,
@@ -283,22 +298,23 @@ blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
         .bs             = bs,
         .qiov           = &log_qiov,
         .entry = {
-            .sector     = cpu_to_le64(offset >> BDRV_SECTOR_BITS),
-            .nr_sectors = cpu_to_le64(bytes >> BDRV_SECTOR_BITS),
+            .sector     = cpu_to_le64(offset >> s->sectorbits),
+            .nr_sectors = cpu_to_le64(bytes >> s->sectorbits),
             .flags      = cpu_to_le64(entry_flags),
             .data_len   = 0,
         },
         .zero_size = is_zero_write ? bytes : 0,
     };
-    static const char zeroes[BDRV_SECTOR_SIZE - sizeof(struct log_write_entry)]
-        = { '\0' };
+    void *zeroes = g_malloc0(s->sectorsize - sizeof(lr.entry));
 
+    assert((1 << s->sectorbits) == s->sectorsize);
+    assert(bs->bl.request_alignment == s->sectorsize);
     assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
     assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
 
     qemu_iovec_init(&log_qiov, niov + 2);
     qemu_iovec_add(&log_qiov, &lr.entry, sizeof(lr.entry));
-    qemu_iovec_add(&log_qiov, (void *)zeroes, sizeof(zeroes));
+    qemu_iovec_add(&log_qiov, zeroes, s->sectorsize - sizeof(lr.entry));
     for (size_t i = 0; i < niov; ++i) {
         qemu_iovec_add(&log_qiov, qiov->iov[i].iov_base, qiov->iov[i].iov_len);
     }
@@ -307,6 +323,7 @@ blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
     blk_log_writes_co_do_log(&lr);
 
     qemu_iovec_destroy(&log_qiov);
+    g_free(zeroes);
 
     if (lr.log_ret < 0) {
         return lr.log_ret;
Signature · 1 line
2.7.4
#15 lore raw

v2 cover New block driver: blklogwrites

In reply to Ari Sundholm

This patch series adds a new block driver, blklogwrites, to QEMU. The driver is given two block devices: a raw device backed by an image or a host block device, and a log device, typically backed by a file, on which writes to the raw device are logged.

The logging format used is the same as in the dm-log-writes target of the Linux kernel device mapper. The log reflects the writes that have been performed on the guest block device and flushed. To be strict, the log may contain writes that have not been flushed yet, but they are technically outside the bounds of the log until the next flush updates the metadata in the log superblock. We believe these semantics to be useful even though they may not be completely identical to those of dm-log-writes.

This functionality can be used for crash consistency and fs consistency testing in filesystem drivers, including on non-Linux guests and older guests running Linux versions without support for dm-log-writes. This is simple and useful. Admittedly this and more advanced things could perhaps be done by extending the quorum driver, but this approach would require re-engineering the functionality and involve a more complicated setup, so we offer this simple solution which we have found useful internally.

In the first patch of this series, two block permission constants are moved from block.c to include/block/block.h to make them available outside of block.c. The next patch uses these constants.

The driver requires all requests to be aligned to the sector size. In the second patch of this series, which includes the bulk of the blklogwrites driver, this sector size is assumed to be equal to BDRV_SECTOR_SIZE for simplicity.

In patches 3-8, a mechanism to pass the block configuration of a block backend to a block driver is introduced, and this mechanism is integrated in various hw/block drivers.

In patch 9, blklogwrites is changed to use the block configuration of the relevant block backend to set its block limits.

Finally, in patch 10, blklogwrites is improved to use the logical sector size of the block backend for logging. This means that the sector size in the log metadata will be set to the logical sector size, and offsets and sizes of writes will be expressed as a multiple of that instead of BDRV_SECTOR_SIZE.

Aapo Vienamo (1):
block: Add blklogwrites

Ari Sundholm (9):
block: Move two block permission constants to the relevant enum
block: Add a mechanism for passing a block driver a block
configuration
hw/scsi/scsi-disk: Always apply block configuration to block driver hw/ide/qdev: Always apply block configuration to block driver hw/block/virtio-blk: Always apply block configuration to block driver hw/block/nvme: Always apply block configuration to block driver hw/block/fdc: Always apply block configuration to block driver block/blklogwrites: Use block limits from the backend block
configuration
block/blklogwrites: Use the block device logical sector size when
logging writes

 MAINTAINERS               |   6 +
 block.c                   |   6 -
 block/Makefile.objs       |   1 +
 block/blklogwrites.c      | 420 
++++++++++++++++++++++++++++++++++++++++++++++
 block/io.c                |  22 +++
 hw/block/block.c          |  12 +-
 hw/block/fdc.c            |   4 +
 hw/block/nvme.c           |   1 +
 hw/block/virtio-blk.c     |   1 +
 hw/ide/qdev.c             |   2 +
 hw/scsi/scsi-disk.c       |   1 +
 include/block/block.h     |  17 ++
 include/block/block_int.h |   9 +
 include/hw/block/block.h  |   1 +
 qapi/block-core.json      |  29 +++-
 15 files changed, 519 insertions(+), 13 deletions(-)
 create mode 100644 block/blklogwrites.c
Signature · 1 line
2.7.4