From nobody Sun Nov 9 22:30:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1552059894353339.21627709489394; Fri, 8 Mar 2019 07:44:54 -0800 (PST) Received: from localhost ([127.0.0.1]:45799 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h2Hg3-0002CS-8p for importer@patchew.org; Fri, 08 Mar 2019 10:44:51 -0500 Received: from eggs.gnu.org ([209.51.188.92]:42652) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h2HZg-0005Ze-5A for qemu-devel@nongnu.org; Fri, 08 Mar 2019 10:38:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h2HZd-00011l-JC for qemu-devel@nongnu.org; Fri, 08 Mar 2019 10:38:16 -0500 Received: from mx1.redhat.com ([209.132.183.28]:46996) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h2HZb-0000qG-3h; Fri, 08 Mar 2019 10:38:11 -0500 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 mx1.redhat.com (Postfix) with ESMTPS id 6188BC4EAD; Fri, 8 Mar 2019 15:38:10 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-27.ams2.redhat.com [10.36.117.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id EF0875FCA4; Fri, 8 Mar 2019 15:38:08 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 8 Mar 2019 16:37:52 +0100 Message-Id: <20190308153757.25794-4-kwolf@redhat.com> In-Reply-To: <20190308153757.25794-1-kwolf@redhat.com> References: <20190308153757.25794-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 08 Mar 2019 15:38:10 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 3/8] block: Make permission changes in reopen less wrong 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, pkrempa@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" The way that reopen interacts with permission changes has one big problem: Both operations are recursive, and the permissions are changes for each node in the reopen queue. For a simple graph that consists just of parent and child, .bdrv_check_perm will be called twice for the child, once recursively when adjusting the permissions of parent, and once again when the child itself is reopened. Even worse, the first .bdrv_check_perm call happens before .bdrv_reopen_prepare was called for the child and the second one is called afterwards. Making sure that .bdrv_check_perm (and the other permission callbacks) are called only once is hard. We can cope with multiple calls right now, but as soon as file-posix gets a dynamic auto-read-only that may need to open a new file descriptor, we get the additional requirement that all of them are after the .bdrv_reopen_prepare call. So reorder things in bdrv_reopen_multiple() to first call .bdrv_reopen_prepare for all involved nodes and only then adjust permissions. Signed-off-by: Kevin Wolf --- block.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/block.c b/block.c index e18bd5eefd..9b9d25e843 100644 --- a/block.c +++ b/block.c @@ -1698,6 +1698,7 @@ static void bdrv_child_set_perm(BdrvChild *c, uint64_= t perm, uint64_t shared); =20 typedef struct BlockReopenQueueEntry { bool prepared; + bool perms_checked; BDRVReopenState state; QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; } BlockReopenQueueEntry; @@ -3166,6 +3167,16 @@ int bdrv_reopen_multiple(AioContext *ctx, BlockReope= nQueue *bs_queue, Error **er bs_entry->prepared =3D true; } =20 + QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { + BDRVReopenState *state =3D &bs_entry->state; + ret =3D bdrv_check_perm(state->bs, bs_queue, state->perm, + state->shared_perm, NULL, errp); + if (ret < 0) { + goto cleanup_perm; + } + bs_entry->perms_checked =3D true; + } + /* If we reach this point, we have success and just need to apply the * changes */ @@ -3174,7 +3185,20 @@ int bdrv_reopen_multiple(AioContext *ctx, BlockReope= nQueue *bs_queue, Error **er } =20 ret =3D 0; +cleanup_perm: + QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { + BDRVReopenState *state =3D &bs_entry->state; + + if (!bs_entry->perms_checked) { + continue; + } =20 + if (ret =3D=3D 0) { + bdrv_set_perm(state->bs, state->perm, state->shared_perm); + } else { + bdrv_abort_perm_update(state->bs); + } + } cleanup: QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { if (ret) { @@ -3428,12 +3452,6 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_stat= e, BlockReopenQueue *queue, } while ((entry =3D qdict_next(reopen_state->options, entry))); } =20 - ret =3D bdrv_check_perm(reopen_state->bs, queue, reopen_state->perm, - reopen_state->shared_perm, NULL, errp); - if (ret < 0) { - goto error; - } - ret =3D 0; =20 /* Restore the original reopen_state->options QDict */ @@ -3500,9 +3518,6 @@ void bdrv_reopen_commit(BDRVReopenState *reopen_state) =20 bdrv_refresh_limits(bs, NULL); =20 - bdrv_set_perm(reopen_state->bs, reopen_state->perm, - reopen_state->shared_perm); - new_can_write =3D !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE); if (!old_can_write && new_can_write && drv->bdrv_reopen_bitmaps_rw) { @@ -3534,8 +3549,6 @@ void bdrv_reopen_abort(BDRVReopenState *reopen_state) if (drv->bdrv_reopen_abort) { drv->bdrv_reopen_abort(reopen_state); } - - bdrv_abort_perm_update(reopen_state->bs); } =20 =20 --=20 2.20.1