From nobody Sun Feb 8 18:29:35 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1599503122; cv=none; d=zohomail.com; s=zohoarc; b=Iz2b6w/nc0F9HrWcRIPo7twoyWhnQtzrVqBKvWUTwdILraQlbBfDJalVHpWSOZjxBH1R8tY2FvGT2nrsHZ4brMxQqAOx0FyubEjX+tlODBEmiT8hp6orUcliFXOgSYHSEoPAGfLCOV/7fKsYZBCPsqC8vwtaeO68+gg+JkGtCIE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599503122; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=u5i4Veo6BrqqglkWSuCsW5O4CBu1XuiSfgXbOY97GoY=; b=YfcbW6YZMkZZDkFZ1e87Tb3CecGzb74mG8MQS29ct5L4EXeJ/K4WQ7C0KenvO5OAIgveeOdXuy6ueNvW+Ndlp2CGg3g9EPkCowKIAkxnPT+OiDu2ebSjD2pyNpLCgVaUvxwWTYm9TFFHtx0lmMNmwOXYRjp9/5w6CfUSroJMdkg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1599503122791920.1642890963385; Mon, 7 Sep 2020 11:25:22 -0700 (PDT) Received: from localhost ([::1]:53568 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kFLpR-0005e1-Ad for importer@patchew.org; Mon, 07 Sep 2020 14:25:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42354) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kFLl2-00055G-ER for qemu-devel@nongnu.org; Mon, 07 Sep 2020 14:20:48 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:35526 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kFLku-0004Sz-9a for qemu-devel@nongnu.org; Mon, 07 Sep 2020 14:20:48 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-352-TaHdWhw3O-i6NCQ3iViWRQ-1; Mon, 07 Sep 2020 14:20:35 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9EEF910054FF; Mon, 7 Sep 2020 18:20:34 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-154.ams2.redhat.com [10.36.114.154]) by smtp.corp.redhat.com (Postfix) with ESMTP id 81B5760BF3; Mon, 7 Sep 2020 18:20:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1599502839; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=u5i4Veo6BrqqglkWSuCsW5O4CBu1XuiSfgXbOY97GoY=; b=i9zWUkztTCxb9JwXbih9S0cBkmUgcMg8LmlZUSfbXfY4jDyo9/7S0GOq0mOF1q368jYn7W 1jBOoc7uqkTnLX6e8CPhOeXTmKp3SUQNiYcj4JadVHcvQDDnuIvzUMWGV5DQLoPnvap3di AwBEk4laB6mSPqDkvjVONODVmCOcoDo= X-MC-Unique: TaHdWhw3O-i6NCQ3iViWRQ-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH 07/29] block/export: Remove magic from block-export-add Date: Mon, 7 Sep 2020 20:19:49 +0200 Message-Id: <20200907182011.521007-8-kwolf@redhat.com> In-Reply-To: <20200907182011.521007-1-kwolf@redhat.com> References: <20200907182011.521007-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=kwolf@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.120; envelope-from=kwolf@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/07 03:19:10 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" nbd-server-add tries to be convenient and adds two questionable features that we don't want to share in block-export-add, even for NBD exports: 1. When requesting a writable export of a read-only device, the export is silently downgraded to read-only. This should be an error in the context of block-export-add. 2. When using a BlockBackend name, unplugging the device from the guest will automatically stop the NBD server, too. This may sometimes be what you want, but it could also be very surprising. Let's keep things explicit with block-export-add. If the user wants to stop the export, they should tell us so. Move these things into the nbd-server-add QMP command handler so that they apply only there. Signed-off-by: Kevin Wolf Reviewed-by: Max Reitz --- include/block/export.h | 2 ++ include/block/nbd.h | 3 ++- block/export/export.c | 13 +++++++++--- blockdev-nbd.c | 47 +++++++++++++++++++++++++++++++++++------- nbd/server.c | 20 +++++++++++------- qemu-nbd.c | 3 +-- 6 files changed, 67 insertions(+), 21 deletions(-) diff --git a/include/block/export.h b/include/block/export.h index 42e3c055fc..e7af2c7687 100644 --- a/include/block/export.h +++ b/include/block/export.h @@ -30,4 +30,6 @@ struct BlockExport { const BlockExportDriver *drv; }; =20 +BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp); + #endif diff --git a/include/block/nbd.h b/include/block/nbd.h index 451f399b0a..f55f5b710b 100644 --- a/include/block/nbd.h +++ b/include/block/nbd.h @@ -335,7 +335,8 @@ NBDExport *nbd_export_new(BlockDriverState *bs, const char *name, const char *desc, const char *bitmap, bool readonly, bool shared, void (*close)(NBDExport *), bool writethrough, - BlockBackend *on_eject_blk, Error **errp); + Error **errp); +void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk); void nbd_export_close(NBDExport *exp); void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **e= rrp); void nbd_export_get(NBDExport *exp); diff --git a/block/export/export.c b/block/export/export.c index fd65541963..05bc5e3744 100644 --- a/block/export/export.c +++ b/block/export/export.c @@ -13,6 +13,8 @@ =20 #include "qemu/osdep.h" =20 +#include "block/block.h" +#include "sysemu/block-backend.h" #include "block/export.h" #include "block/nbd.h" #include "qapi/error.h" @@ -34,15 +36,20 @@ static const BlockExportDriver *blk_exp_find_driver(Blo= ckExportType type) return NULL; } =20 -void qmp_block_export_add(BlockExportOptions *export, Error **errp) +BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp) { const BlockExportDriver *drv; =20 drv =3D blk_exp_find_driver(export->type); if (!drv) { error_setg(errp, "No driver found for the requested export type"); - return; + return NULL; } =20 - drv->create(export, errp); + return drv->create(export, errp); +} + +void qmp_block_export_add(BlockExportOptions *export, Error **errp) +{ + blk_exp_add(export, errp); } diff --git a/blockdev-nbd.c b/blockdev-nbd.c index 96cb0100e9..7bcca105f9 100644 --- a/blockdev-nbd.c +++ b/blockdev-nbd.c @@ -152,7 +152,6 @@ BlockExport *nbd_export_create(BlockExportOptions *exp_= args, Error **errp) { BlockExportOptionsNbd *arg =3D &exp_args->u.nbd; BlockDriverState *bs =3D NULL; - BlockBackend *on_eject_blk; NBDExport *exp =3D NULL; AioContext *aio_context; =20 @@ -182,8 +181,6 @@ BlockExport *nbd_export_create(BlockExportOptions *exp_= args, Error **errp) return NULL; } =20 - on_eject_blk =3D blk_by_name(arg->device); - bs =3D bdrv_lookup_bs(arg->device, arg->device, errp); if (!bs) { return NULL; @@ -195,13 +192,14 @@ BlockExport *nbd_export_create(BlockExportOptions *ex= p_args, Error **errp) if (!arg->has_writable) { arg->writable =3D false; } - if (bdrv_is_read_only(bs)) { - arg->writable =3D false; + if (bdrv_is_read_only(bs) && arg->writable) { + error_setg(errp, "Cannot export read-only node as writable"); + goto out; } =20 exp =3D nbd_export_new(bs, arg->name, arg->description, arg->bitmap, !arg->writable, !arg->writable, - NULL, false, on_eject_blk, errp); + NULL, false, errp); if (!exp) { goto out; } @@ -219,11 +217,44 @@ BlockExport *nbd_export_create(BlockExportOptions *ex= p_args, Error **errp) =20 void qmp_nbd_server_add(BlockExportOptionsNbd *arg, Error **errp) { - BlockExportOptions export =3D { + BlockExport *export; + BlockDriverState *bs; + BlockBackend *on_eject_blk; + BlockExportOptions export_opts; + + bs =3D bdrv_lookup_bs(arg->device, arg->device, errp); + if (!bs) { + return; + } + + export_opts =3D (BlockExportOptions) { .type =3D BLOCK_EXPORT_TYPE_NBD, .u.nbd =3D *arg, }; - qmp_block_export_add(&export, errp); + + /* + * nbd-server-add doesn't complain when a read-only device should be + * exported as writable, but simply downgrades it. This is an error wi= th + * block-export-add. + */ + if (bdrv_is_read_only(bs)) { + export_opts.u.nbd.has_writable =3D true; + export_opts.u.nbd.writable =3D false; + } + + export =3D blk_exp_add(&export_opts, errp); + if (!export) { + return; + } + + /* + * nbd-server-add removes the export when the named BlockBackend used = for + * @device goes away. + */ + on_eject_blk =3D blk_by_name(arg->device); + if (on_eject_blk) { + nbd_export_set_on_eject_blk(export, on_eject_blk); + } } =20 void qmp_nbd_server_remove(const char *name, diff --git a/nbd/server.c b/nbd/server.c index 33aaca918c..23d9a53094 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1506,11 +1506,23 @@ static void nbd_eject_notifier(Notifier *n, void *d= ata) aio_context_release(aio_context); } =20 +void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk) +{ + NBDExport *nbd_exp =3D container_of(exp, NBDExport, common); + assert(exp->drv =3D=3D &blk_exp_nbd); + assert(nbd_exp->eject_notifier_blk =3D=3D NULL); + + blk_ref(blk); + nbd_exp->eject_notifier_blk =3D blk; + nbd_exp->eject_notifier.notify =3D nbd_eject_notifier; + blk_add_remove_bs_notifier(blk, &nbd_exp->eject_notifier); +} + NBDExport *nbd_export_new(BlockDriverState *bs, const char *name, const char *desc, const char *bitmap, bool readonly, bool shared, void (*close)(NBDExport *), bool writethrough, - BlockBackend *on_eject_blk, Error **errp) + Error **errp) { AioContext *ctx; BlockBackend *blk; @@ -1617,12 +1629,6 @@ NBDExport *nbd_export_new(BlockDriverState *bs, exp->ctx =3D ctx; blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, ex= p); =20 - if (on_eject_blk) { - blk_ref(on_eject_blk); - exp->eject_notifier_blk =3D on_eject_blk; - exp->eject_notifier.notify =3D nbd_eject_notifier; - blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier); - } QTAILQ_INSERT_TAIL(&exports, exp, next); nbd_export_get(exp); return exp; diff --git a/qemu-nbd.c b/qemu-nbd.c index 1752daa4c4..2bba6c0db3 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -1067,8 +1067,7 @@ int main(int argc, char **argv) =20 export =3D nbd_export_new(bs, export_name, export_description, bitmap, readonly, shared >= 1, - nbd_export_closed, writethrough, NULL, - &error_fatal); + nbd_export_closed, writethrough, &error_fatal); =20 if (device) { #if HAVE_NBD_DEVICE --=20 2.25.4