From nobody Tue Feb 10 02:00:45 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1546200814527211.40666127331428; Sun, 30 Dec 2018 12:13:34 -0800 (PST) Received: from localhost ([127.0.0.1]:46483 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gdhSn-0005QZ-0k for importer@patchew.org; Sun, 30 Dec 2018 15:13:33 -0500 Received: from eggs.gnu.org ([208.118.235.92]:43331) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gdhOq-0001fz-KN for qemu-devel@nongnu.org; Sun, 30 Dec 2018 15:09:30 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gdhOn-0008Ez-Jg for qemu-devel@nongnu.org; Sun, 30 Dec 2018 15:09:28 -0500 Received: from relay.sw.ru ([185.231.240.75]:46804) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gdhOn-0008Bz-BW; 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-Ho; 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:13 +0300 Message-Id: <1546200557-774583-3-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 2/6] Discard blocks while copy-on-read 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Discards the block duplicated in an intermediate backing file after the block have been copied into the active layer during QMP block-stream operation. It saves the disk space while merging external snapshots. Signed-off-by: Andrey Shinkevich --- block/stream.c | 207 +++++++++++++++++++++++++++++++++++++++++++++++++++++= ---- 1 file changed, 195 insertions(+), 12 deletions(-) diff --git a/block/stream.c b/block/stream.c index 20e768e..af2eebf 100644 --- a/block/stream.c +++ b/block/stream.c @@ -12,6 +12,7 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/cutils.h" #include "trace.h" #include "block/block_int.h" #include "block/blockjob_int.h" @@ -37,14 +38,68 @@ typedef struct StreamBlockJob { char *backing_file_str; bool bs_read_only; BlockDriverState *cor_filter_bs; + bool discard; + GSList *im_nodes; } StreamBlockJob; =20 +typedef struct IntermediateNode { + BlockBackend *blk; + bool bs_read_only; +} IntermediateNode; + static BlockDriverState *child_file_bs(BlockDriverState *bs) { return bs->file ? bs->file->bs : NULL; } =20 -static int coroutine_fn stream_populate(BlockBackend *blk, +static void restore_all_im_nodes(StreamBlockJob *s) +{ + GSList *l; + BlockDriverState *bs_active; + BlockDriverState *bs_im; + IntermediateNode *im_node; + QDict *opts; + BlockReopenQueue *queue =3D NULL; + Error *local_err =3D NULL; + + assert(s->cor_filter_bs); + bs_active =3D child_file_bs(s->cor_filter_bs); + assert(bs_active && backing_bs(bs_active)); + + bdrv_subtree_drained_begin(backing_bs(bs_active)); + + for (l =3D s->im_nodes; l; l =3D l->next) { + im_node =3D l->data; + if (im_node->blk) { + bs_im =3D blk_bs(im_node->blk); + + if (im_node->bs_read_only && bs_im && !bdrv_is_read_only(bs_im= )) { + opts =3D qdict_new(); + qdict_put_bool(opts, BDRV_OPT_READ_ONLY, true); + queue =3D bdrv_reopen_queue(queue, bs_im, opts); + } + /* Give up write permissions before making it read-only */ + blk_set_perm(im_node->blk, 0, BLK_PERM_ALL, &error_abort); + blk_unref(im_node->blk); + bdrv_unref(bs_im); + } + g_free(im_node); + } + g_slist_free(s->im_nodes); + s->im_nodes =3D NULL; + + if (queue) { + bdrv_reopen_multiple(bdrv_get_aio_context(bs_active), queue, + &local_err); + if (local_err !=3D NULL) { + error_report_err(local_err); + } + } + + bdrv_subtree_drained_end(backing_bs(bs_active)); +} + +static int coroutine_fn stream_populate(const StreamBlockJob *s, int64_t offset, uint64_t bytes, void *buf) { @@ -53,12 +108,28 @@ static int coroutine_fn stream_populate(BlockBackend *= blk, .iov_len =3D bytes, }; QEMUIOVector qiov; + GSList *l; + IntermediateNode *im_node; + int ret; =20 + assert(s); assert(bytes < SIZE_MAX); qemu_iovec_init_external(&qiov, &iov, 1); =20 /* Copy-on-read the unallocated clusters */ - return blk_co_preadv(blk, offset, qiov.size, &qiov, BDRV_REQ_COPY_ON_R= EAD); + ret =3D blk_co_preadv(s->common.blk, offset, qiov.size, &qiov, + BDRV_REQ_COPY_ON_READ); + + if (ret < 0 || !s->discard) { + return ret; + } + + for (l =3D s->im_nodes; l; l =3D l->next) { + im_node =3D l->data; + blk_co_pdiscard(im_node->blk, offset, bytes); + } + + return ret; } =20 static int stream_change_backing_file(Job *job) @@ -109,6 +180,8 @@ static void stream_exit(Job *job) if (s->cor_filter_bs =3D=3D NULL) { return; } + /* Reopen intermediate images back in read-only mode */ + restore_all_im_nodes(s); /* Remove the filter driver from the graph */ remove_filter(s->cor_filter_bs); s->cor_filter_bs =3D NULL; @@ -145,7 +218,6 @@ static void stream_clean(Job *job) static int coroutine_fn stream_run(Job *job, Error **errp) { StreamBlockJob *s =3D container_of(job, StreamBlockJob, common.job); - BlockBackend *blk =3D s->common.blk; BlockDriverState *bs =3D child_file_bs(s->cor_filter_bs); BlockDriverState *base =3D s->base; int64_t len; @@ -209,7 +281,7 @@ static int coroutine_fn stream_run(Job *job, Error **er= rp) } trace_stream_one_iteration(s, offset, n, ret); if (copy) { - ret =3D stream_populate(blk, offset, n, buf); + ret =3D stream_populate(s, offset, n, buf); } if (ret < 0) { BlockErrorAction action =3D @@ -250,22 +322,27 @@ out: return ret; } =20 -static BlockDriverState *create_filter_node(BlockDriverState *bs, Error **= errp) +static BlockDriverState *create_filter_node(BlockDriverState *bs, bool dis= card, + Error **errp) { QDict *opts =3D qdict_new(); =20 qdict_put_str(opts, "driver", "copy-on-read"); qdict_put_str(opts, "file", bdrv_get_node_name(bs)); + if (discard) { + qdict_put_bool(opts, "driver.discard", true); + } =20 return bdrv_open(NULL, NULL, opts, BDRV_O_RDWR, errp); } =20 -static BlockDriverState *insert_filter(BlockDriverState *bs, Error **errp) +static BlockDriverState *insert_filter(BlockDriverState *bs, bool discard, + Error **errp) { BlockDriverState *cor_filter_bs; Error *local_err =3D NULL; =20 - cor_filter_bs =3D create_filter_node(bs, errp); + cor_filter_bs =3D create_filter_node(bs, discard, errp); if (cor_filter_bs =3D=3D NULL) { error_prepend(errp, "Could not create filter node: "); return NULL; @@ -286,6 +363,92 @@ static BlockDriverState *insert_filter(BlockDriverStat= e *bs, Error **errp) return cor_filter_bs; } =20 +/* Makes intermediate block chain writable */ +static int init_intermediate_nodes(StreamBlockJob *s, + BlockDriverState *bs, + BlockDriverState *base, Error **errp) +{ + BlockDriverState *iter; + bool bs_read_only; + IntermediateNode *im_node; + BlockBackend *blk; + QDict *opts; + BlockReopenQueue *queue =3D NULL; + Error *local_err =3D NULL; + int ret; + + /* Sanity check */ + if (!backing_bs(bs)) { + error_setg(errp, "Top BDS does not have a backing file."); + return -EINVAL; + } + if (base && !bdrv_chain_contains(bs, base)) { + error_setg(errp, "The backing chain does not contain the base file= ."); + return -EINVAL; + } + + /* Reopen intermediate images in read-write mode */ + bdrv_subtree_drained_begin(backing_bs(bs)); + + for (iter =3D backing_bs(bs); iter && iter !=3D base; iter =3D backing= _bs(iter)) { + bs_read_only =3D bdrv_is_read_only(iter); + im_node =3D g_new0(IntermediateNode, 1); + im_node->blk =3D NULL; + im_node->bs_read_only =3D bs_read_only; + bdrv_ref(iter); + s->im_nodes =3D g_slist_prepend(s->im_nodes, im_node); + + if (bs_read_only) { + opts =3D qdict_new(); + qdict_put_bool(opts, BDRV_OPT_READ_ONLY, false); + queue =3D bdrv_reopen_queue(queue, iter, opts); + } + } + + if (queue) { + ret =3D bdrv_reopen_multiple(bdrv_get_aio_context(bs), queue, &loc= al_err); + if (local_err !=3D NULL) { + error_propagate(errp, local_err); + bdrv_subtree_drained_end(backing_bs(bs)); + restore_all_im_nodes(s); + return -1; + } + } + + bdrv_subtree_drained_end(backing_bs(bs)); + + s->im_nodes =3D g_slist_reverse(s->im_nodes); + GSList *l =3D s->im_nodes; + + for (iter =3D backing_bs(bs); iter && iter !=3D base; iter =3D backing= _bs(iter)) { + blk =3D blk_new(BLK_PERM_WRITE, BLK_PERM_CONSISTENT_READ | + BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED | + BLK_PERM_GRAPH_MOD); + if (!blk) { + error_setg(errp, + "Block Stream: failed to create new Block Backend."= ); + goto fail; + } + + ret =3D blk_insert_bs(blk, iter, errp); + if (ret < 0) { + goto fail; + } + + assert(l); + im_node =3D l->data; + im_node->blk =3D blk; + l =3D l->next; + } + + return 0; + +fail: + restore_all_im_nodes(s); + + return -1; +} + static const BlockJobDriver stream_job_driver =3D { .job_driver =3D { .instance_size =3D sizeof(StreamBlockJob), @@ -308,6 +471,9 @@ void stream_start(const char *job_id, BlockDriverState = *bs, StreamBlockJob *s; BlockDriverState *iter; bool bs_read_only; + const bool discard =3D false; + int node_shared_flags =3D BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UN= CHANGED; + int ret; =20 /* Make sure that the image is opened in read-write mode */ bs_read_only =3D bdrv_is_read_only(bs); @@ -330,21 +496,34 @@ void stream_start(const char *job_id, BlockDriverStat= e *bs, goto fail; } =20 - /* Block all intermediate nodes between bs and base, because they will + /* + * Block all intermediate nodes between bs and base, because they will * disappear from the chain after this operation. The streaming job re= ads - * every block only once, assuming that it doesn't change, so block wr= ites - * and resizes. */ + * every block only once, assuming that it doesn't change, so forbid w= rites + * and resizes. Allow writing in case of discard. + */ + if (discard) { + node_shared_flags |=3D BLK_PERM_WRITE; + } for (iter =3D backing_bs(bs); iter && iter !=3D base; iter =3D backing= _bs(iter)) { block_job_add_bdrv(&s->common, "intermediate node", iter, 0, - BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHA= NGED, + node_shared_flags, &error_abort); } =20 - s->cor_filter_bs =3D insert_filter(bs, errp); + s->cor_filter_bs =3D insert_filter(bs, discard, errp); if (s->cor_filter_bs =3D=3D NULL) { goto fail; } =20 + if (discard) { + ret =3D init_intermediate_nodes(s, bs, base, errp); + if (ret < 0) { + goto fail; + } + } + + s->discard =3D discard; s->base =3D base; s->backing_file_str =3D g_strdup(backing_file_str); s->bs_read_only =3D bs_read_only; @@ -355,6 +534,10 @@ void stream_start(const char *job_id, BlockDriverState= *bs, return; =20 fail: + if (s && s->cor_filter_bs) { + remove_filter(s->cor_filter_bs); + job_early_fail(&s->common.job); + } if (bs_read_only) { bdrv_reopen_set_read_only(bs, true, NULL); } --=20 1.8.3.1