From patchwork Sun Dec 30 20:09:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Shinkevich X-Patchwork-Id: 1019485 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=208.118.235.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=virtuozzo.com Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43SWr861B1z9s3l for ; Mon, 31 Dec 2018 07:14:32 +1100 (AEDT) Received: from localhost ([127.0.0.1]:46493 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gdhTi-0006P5-NR for incoming@patchwork.ozlabs.org; Sun, 30 Dec 2018 15:14:30 -0500 Received: from eggs.gnu.org ([208.118.235.92]:43333) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gdhOq-0001g1-Kk for qemu-devel@nongnu.org; Sun, 30 Dec 2018 15:09:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gdhOn-0008El-Ds for qemu-devel@nongnu.org; Sun, 30 Dec 2018 15:09:28 -0500 Received: from relay.sw.ru ([185.231.240.75]:46820) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gdhOn-0008C0-5a; Sun, 30 Dec 2018 15:09:25 -0500 Received: from [172.16.25.136] (helo=localhost.sw.ru) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1gdhOi-0000Oj-Ec; Sun, 30 Dec 2018 23:09:20 +0300 From: Andrey Shinkevich To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Sun, 30 Dec 2018 23:09:12 +0300 Message-Id: <1546200557-774583-2-git-send-email-andrey.shinkevich@virtuozzo.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1546200557-774583-1-git-send-email-andrey.shinkevich@virtuozzo.com> References: <1546200557-774583-1-git-send-email-andrey.shinkevich@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v5 1/6] Stream block job involves copy-on-read filter driver X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, jcody@redhat.com, armbru@redhat.com, dgilbert@redhat.com, andrey.shinkevich@virtuozzo.com, den@openvz.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" The copy-on-read filter is applied to block-stream operation. It is necessary for further block discard option. Signed-off-by: Andrey Shinkevich --- block/stream.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/block/stream.c b/block/stream.c index 7a49ac0..20e768e 100644 --- a/block/stream.c +++ b/block/stream.c @@ -16,6 +16,7 @@ #include "block/block_int.h" #include "block/blockjob_int.h" #include "qapi/error.h" +#include "qapi/qmp/qdict.h" #include "qapi/qmp/qerror.h" #include "qemu/ratelimit.h" #include "sysemu/block-backend.h" @@ -35,8 +36,14 @@ typedef struct StreamBlockJob { BlockdevOnError on_error; char *backing_file_str; bool bs_read_only; + BlockDriverState *cor_filter_bs; } StreamBlockJob; +static BlockDriverState *child_file_bs(BlockDriverState *bs) +{ + return bs->file ? bs->file->bs : NULL; +} + static int coroutine_fn stream_populate(BlockBackend *blk, int64_t offset, uint64_t bytes, void *buf) @@ -54,7 +61,7 @@ static int coroutine_fn stream_populate(BlockBackend *blk, return blk_co_preadv(blk, offset, qiov.size, &qiov, BDRV_REQ_COPY_ON_READ); } -static int stream_prepare(Job *job) +static int stream_change_backing_file(Job *job) { StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); BlockJob *bjob = &s->common; @@ -82,6 +89,43 @@ static int stream_prepare(Job *job) return ret; } +static void remove_filter(BlockDriverState *cor_filter_bs) +{ + BlockDriverState *bs = child_file_bs(cor_filter_bs); + + /* Hold a guest back from writing until we remove the filter */ + bdrv_drained_begin(bs); + bdrv_child_try_set_perm(cor_filter_bs->file, 0, BLK_PERM_ALL, + &error_abort); + bdrv_replace_node(cor_filter_bs, bs, &error_abort); + bdrv_drained_end(bs); + + bdrv_unref(cor_filter_bs); +} + +static void stream_exit(Job *job) +{ + StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); + if (s->cor_filter_bs == NULL) { + return; + } + /* Remove the filter driver from the graph */ + remove_filter(s->cor_filter_bs); + s->cor_filter_bs = NULL; +} + +static int stream_prepare(Job *job) +{ + stream_exit(job); + + return stream_change_backing_file(job); +} + +static void stream_abort(Job *job) +{ + stream_exit(job); +} + static void stream_clean(Job *job) { StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); @@ -102,7 +146,7 @@ static int coroutine_fn stream_run(Job *job, Error **errp) { StreamBlockJob *s = container_of(job, StreamBlockJob, common.job); BlockBackend *blk = s->common.blk; - BlockDriverState *bs = blk_bs(blk); + BlockDriverState *bs = child_file_bs(s->cor_filter_bs); BlockDriverState *base = s->base; int64_t len; int64_t offset = 0; @@ -206,6 +250,42 @@ out: return ret; } +static BlockDriverState *create_filter_node(BlockDriverState *bs, Error **errp) +{ + QDict *opts = qdict_new(); + + qdict_put_str(opts, "driver", "copy-on-read"); + qdict_put_str(opts, "file", bdrv_get_node_name(bs)); + + return bdrv_open(NULL, NULL, opts, BDRV_O_RDWR, errp); +} + +static BlockDriverState *insert_filter(BlockDriverState *bs, Error **errp) +{ + BlockDriverState *cor_filter_bs; + Error *local_err = NULL; + + cor_filter_bs = create_filter_node(bs, errp); + if (cor_filter_bs == NULL) { + error_prepend(errp, "Could not create filter node: "); + return NULL; + } + + bdrv_set_aio_context(cor_filter_bs, bdrv_get_aio_context(bs)); + + bdrv_drained_begin(bs); + bdrv_replace_node(bs, cor_filter_bs, &local_err); + bdrv_drained_end(bs); + + if (local_err) { + bdrv_unref(cor_filter_bs); + error_propagate(errp, local_err); + return NULL; + } + + return cor_filter_bs; +} + static const BlockJobDriver stream_job_driver = { .job_driver = { .instance_size = sizeof(StreamBlockJob), @@ -213,6 +293,7 @@ static const BlockJobDriver stream_job_driver = { .free = block_job_free, .run = stream_run, .prepare = stream_prepare, + .abort = stream_abort, .clean = stream_clean, .user_resume = block_job_user_resume, .drain = block_job_drain, @@ -259,6 +340,11 @@ void stream_start(const char *job_id, BlockDriverState *bs, &error_abort); } + s->cor_filter_bs = insert_filter(bs, errp); + if (s->cor_filter_bs == NULL) { + goto fail; + } + s->base = base; s->backing_file_str = g_strdup(backing_file_str); s->bs_read_only = bs_read_only;