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

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