Blame SOURCES/kvm-block-Add-COR-filter-driver.patch

383d26
From b6f5671191f1ce4d7ee2fe5d794c93b316cc0a1a Mon Sep 17 00:00:00 2001
383d26
From: Max Reitz <mreitz@redhat.com>
383d26
Date: Mon, 18 Jun 2018 16:12:04 +0200
383d26
Subject: [PATCH 28/54] block: Add COR filter driver
383d26
383d26
RH-Author: Max Reitz <mreitz@redhat.com>
383d26
Message-id: <20180618161212.14444-3-mreitz@redhat.com>
383d26
Patchwork-id: 80762
383d26
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 02/10] block: Add COR filter driver
383d26
Bugzilla: 1518738
383d26
RH-Acked-by: John Snow <jsnow@redhat.com>
383d26
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
383d26
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
383d26
This adds a simple copy-on-read filter driver.  It relies on the already
383d26
existing COR functionality in the central block layer code, which may be
383d26
moved here once we no longer need it there.
383d26
383d26
Signed-off-by: Max Reitz <mreitz@redhat.com>
383d26
Message-id: 20180421132929.21610-2-mreitz@redhat.com
383d26
Reviewed-by: Alberto Garcia <berto@igalia.com>
383d26
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
383d26
Signed-off-by: Max Reitz <mreitz@redhat.com>
383d26
(cherry picked from commit 6c6f24fd84895d03baa898bbc4324dd4ccc97071)
383d26
Signed-off-by: Max Reitz <mreitz@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 block/Makefile.objs  |   2 +-
383d26
 block/copy-on-read.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++
383d26
 qapi/block-core.json |   5 +-
383d26
 3 files changed, 176 insertions(+), 2 deletions(-)
383d26
 create mode 100644 block/copy-on-read.c
383d26
383d26
diff --git a/block/Makefile.objs b/block/Makefile.objs
383d26
index c0693fc..be2cda1 100644
383d26
--- a/block/Makefile.objs
383d26
+++ b/block/Makefile.objs
383d26
@@ -26,7 +26,7 @@ block-obj-y += accounting.o dirty-bitmap.o
383d26
 block-obj-y += write-threshold.o
383d26
 block-obj-y += backup.o
383d26
 block-obj-$(CONFIG_REPLICATION) += replication.o
383d26
-block-obj-y += throttle.o
383d26
+block-obj-y += throttle.o copy-on-read.o
383d26
 
383d26
 block-obj-y += crypto.o
383d26
 
383d26
diff --git a/block/copy-on-read.c b/block/copy-on-read.c
383d26
new file mode 100644
383d26
index 0000000..823ec75
383d26
--- /dev/null
383d26
+++ b/block/copy-on-read.c
383d26
@@ -0,0 +1,171 @@
383d26
+/*
383d26
+ * Copy-on-read filter block driver
383d26
+ *
383d26
+ * Copyright (c) 2018 Red Hat, Inc.
383d26
+ *
383d26
+ * Author:
383d26
+ *   Max Reitz <mreitz@redhat.com>
383d26
+ *
383d26
+ * This program is free software; you can redistribute it and/or
383d26
+ * modify it under the terms of the GNU General Public License as
383d26
+ * published by the Free Software Foundation; either version 2 or
383d26
+ * (at your option) version 3 of the License.
383d26
+ *
383d26
+ * This program is distributed in the hope that it will be useful,
383d26
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
383d26
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
383d26
+ * GNU General Public License for more details.
383d26
+ *
383d26
+ * You should have received a copy of the GNU General Public License
383d26
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
383d26
+ */
383d26
+
383d26
+#include "qemu/osdep.h"
383d26
+#include "block/block_int.h"
383d26
+
383d26
+
383d26
+static int cor_open(BlockDriverState *bs, QDict *options, int flags,
383d26
+                    Error **errp)
383d26
+{
383d26
+    bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, false,
383d26
+                               errp);
383d26
+    if (!bs->file) {
383d26
+        return -EINVAL;
383d26
+    }
383d26
+
383d26
+    bs->supported_write_flags = BDRV_REQ_FUA &
383d26
+                                    bs->file->bs->supported_write_flags;
383d26
+
383d26
+    bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
383d26
+                                    bs->file->bs->supported_zero_flags;
383d26
+
383d26
+    return 0;
383d26
+}
383d26
+
383d26
+
383d26
+static void cor_close(BlockDriverState *bs)
383d26
+{
383d26
+}
383d26
+
383d26
+
383d26
+#define PERM_PASSTHROUGH (BLK_PERM_CONSISTENT_READ \
383d26
+                          | BLK_PERM_WRITE \
383d26
+                          | BLK_PERM_RESIZE)
383d26
+#define PERM_UNCHANGED (BLK_PERM_ALL & ~PERM_PASSTHROUGH)
383d26
+
383d26
+static void cor_child_perm(BlockDriverState *bs, BdrvChild *c,
383d26
+                           const BdrvChildRole *role,
383d26
+                           BlockReopenQueue *reopen_queue,
383d26
+                           uint64_t perm, uint64_t shared,
383d26
+                           uint64_t *nperm, uint64_t *nshared)
383d26
+{
383d26
+    if (c == NULL) {
383d26
+        *nperm = (perm & PERM_PASSTHROUGH) | BLK_PERM_WRITE_UNCHANGED;
383d26
+        *nshared = (shared & PERM_PASSTHROUGH) | PERM_UNCHANGED;
383d26
+        return;
383d26
+    }
383d26
+
383d26
+    *nperm = (perm & PERM_PASSTHROUGH) |
383d26
+             (c->perm & PERM_UNCHANGED);
383d26
+    *nshared = (shared & PERM_PASSTHROUGH) |
383d26
+               (c->shared_perm & PERM_UNCHANGED);
383d26
+}
383d26
+
383d26
+
383d26
+static int64_t cor_getlength(BlockDriverState *bs)
383d26
+{
383d26
+    return bdrv_getlength(bs->file->bs);
383d26
+}
383d26
+
383d26
+
383d26
+static int cor_truncate(BlockDriverState *bs, int64_t offset,
383d26
+                        PreallocMode prealloc, Error **errp)
383d26
+{
383d26
+    return bdrv_truncate(bs->file, offset, prealloc, errp);
383d26
+}
383d26
+
383d26
+
383d26
+static int coroutine_fn cor_co_preadv(BlockDriverState *bs,
383d26
+                                      uint64_t offset, uint64_t bytes,
383d26
+                                      QEMUIOVector *qiov, int flags)
383d26
+{
383d26
+    return bdrv_co_preadv(bs->file, offset, bytes, qiov,
383d26
+                          flags | BDRV_REQ_COPY_ON_READ);
383d26
+}
383d26
+
383d26
+
383d26
+static int coroutine_fn cor_co_pwritev(BlockDriverState *bs,
383d26
+                                       uint64_t offset, uint64_t bytes,
383d26
+                                       QEMUIOVector *qiov, int flags)
383d26
+{
383d26
+
383d26
+    return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
383d26
+}
383d26
+
383d26
+
383d26
+static int coroutine_fn cor_co_pwrite_zeroes(BlockDriverState *bs,
383d26
+                                             int64_t offset, int bytes,
383d26
+                                             BdrvRequestFlags flags)
383d26
+{
383d26
+    return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
383d26
+}
383d26
+
383d26
+
383d26
+static int coroutine_fn cor_co_pdiscard(BlockDriverState *bs,
383d26
+                                        int64_t offset, int bytes)
383d26
+{
383d26
+    return bdrv_co_pdiscard(bs->file->bs, offset, bytes);
383d26
+}
383d26
+
383d26
+
383d26
+static void cor_eject(BlockDriverState *bs, bool eject_flag)
383d26
+{
383d26
+    bdrv_eject(bs->file->bs, eject_flag);
383d26
+}
383d26
+
383d26
+
383d26
+static void cor_lock_medium(BlockDriverState *bs, bool locked)
383d26
+{
383d26
+    bdrv_lock_medium(bs->file->bs, locked);
383d26
+}
383d26
+
383d26
+
383d26
+static bool cor_recurse_is_first_non_filter(BlockDriverState *bs,
383d26
+                                            BlockDriverState *candidate)
383d26
+{
383d26
+    return bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
383d26
+}
383d26
+
383d26
+
383d26
+BlockDriver bdrv_copy_on_read = {
383d26
+    .format_name                        = "copy-on-read",
383d26
+
383d26
+    .bdrv_open                          = cor_open,
383d26
+    .bdrv_close                         = cor_close,
383d26
+    .bdrv_child_perm                    = cor_child_perm,
383d26
+
383d26
+    .bdrv_getlength                     = cor_getlength,
383d26
+    .bdrv_truncate                      = cor_truncate,
383d26
+
383d26
+    .bdrv_co_preadv                     = cor_co_preadv,
383d26
+    .bdrv_co_pwritev                    = cor_co_pwritev,
383d26
+    .bdrv_co_pwrite_zeroes              = cor_co_pwrite_zeroes,
383d26
+    .bdrv_co_pdiscard                   = cor_co_pdiscard,
383d26
+
383d26
+    .bdrv_eject                         = cor_eject,
383d26
+    .bdrv_lock_medium                   = cor_lock_medium,
383d26
+
383d26
+    .bdrv_co_block_status               = bdrv_co_block_status_from_file,
383d26
+
383d26
+    .bdrv_recurse_is_first_non_filter   = cor_recurse_is_first_non_filter,
383d26
+
383d26
+    .has_variable_length                = true,
383d26
+    .is_filter                          = true,
383d26
+};
383d26
+
383d26
+static void bdrv_copy_on_read_init(void)
383d26
+{
383d26
+    bdrv_register(&bdrv_copy_on_read);
383d26
+}
383d26
+
383d26
+block_init(bdrv_copy_on_read_init);
383d26
diff --git a/qapi/block-core.json b/qapi/block-core.json
383d26
index 46469be..5aac0c7 100644
383d26
--- a/qapi/block-core.json
383d26
+++ b/qapi/block-core.json
383d26
@@ -2506,11 +2506,12 @@
383d26
 # @vxhs: Since 2.10
383d26
 # @throttle: Since 2.11
383d26
 # @nvme: Since 2.12
383d26
+# @copy-on-read: Since 2.13
383d26
 #
383d26
 # Since: 2.9
383d26
 ##
383d26
 { 'enum': 'BlockdevDriver',
383d26
-  'data': [ 'blkdebug', 'blkverify', 'bochs', 'cloop',
383d26
+  'data': [ 'blkdebug', 'blkverify', 'bochs', 'cloop', 'copy-on-read',
383d26
             'dmg', 'file', 'ftp', 'ftps', 'gluster', 'host_cdrom',
383d26
             'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs',
383d26
             'null-aio', 'null-co', 'nvme', 'parallels', 'qcow', 'qcow2', 'qed',
383d26
@@ -3541,6 +3542,7 @@
383d26
       'blkverify':  'BlockdevOptionsBlkverify',
383d26
       'bochs':      'BlockdevOptionsGenericFormat',
383d26
       'cloop':      'BlockdevOptionsGenericFormat',
383d26
+      'copy-on-read':'BlockdevOptionsGenericFormat',
383d26
       'dmg':        'BlockdevOptionsGenericFormat',
383d26
       'file':       'BlockdevOptionsFile',
383d26
       'ftp':        'BlockdevOptionsCurlFtp',
383d26
@@ -4068,6 +4070,7 @@
383d26
       'blkverify':      'BlockdevCreateNotSupported',
383d26
       'bochs':          'BlockdevCreateNotSupported',
383d26
       'cloop':          'BlockdevCreateNotSupported',
383d26
+      'copy-on-read':   'BlockdevCreateNotSupported',
383d26
       'dmg':            'BlockdevCreateNotSupported',
383d26
       'file':           'BlockdevCreateOptionsFile',
383d26
       'ftp':            'BlockdevCreateNotSupported',
383d26
-- 
383d26
1.8.3.1
383d26