From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([209.51.188.92]:59233) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h3O9O-00037v-Ji for qemu-devel@nongnu.org; Mon, 11 Mar 2019 12:51:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h3O8N-0007kS-H4 for qemu-devel@nongnu.org; Mon, 11 Mar 2019 12:50:40 -0400 From: Kevin Wolf Date: Mon, 11 Mar 2019 17:50:15 +0100 Message-Id: <20190311165017.32247-9-kwolf@redhat.com> In-Reply-To: <20190311165017.32247-1-kwolf@redhat.com> References: <20190311165017.32247-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH v2 08/10] file-posix: Lock new fd in raw_reopen_prepare() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-block@nongnu.org Cc: kwolf@redhat.com, mreitz@redhat.com, eblake@redhat.com, pkrempa@redhat.com, berto@igalia.com, qemu-devel@nongnu.org There is no reason why we can take locks on the new file descriptor only in raw_reopen_commit() where error handling isn't possible any more. Instead, we can already do this in raw_reopen_prepare(). Signed-off-by: Kevin Wolf --- block/file-posix.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index 6aaee1df16..932cc8e58c 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -906,7 +906,7 @@ static int raw_reopen_prepare(BDRVReopenState *state, BDRVRawState *s; BDRVRawReopenState *rs; QemuOpts *opts; - int ret =3D 0; + int ret; Error *local_err =3D NULL; =20 assert(state !=3D NULL); @@ -947,14 +947,27 @@ static int raw_reopen_prepare(BDRVReopenState *stat= e, if (rs->fd !=3D -1) { raw_probe_alignment(state->bs, rs->fd, &local_err); if (local_err) { - qemu_close(rs->fd); - rs->fd =3D -1; error_propagate(errp, local_err); ret =3D -EINVAL; + goto out_fd; + } + + /* Copy locks to the new fd */ + ret =3D raw_apply_lock_bytes(NULL, rs->fd, s->locked_perm, + s->locked_shared_perm, false, errp); + if (ret < 0) { + ret =3D -EINVAL; + goto out_fd; } } =20 s->reopen_state =3D state; + ret =3D 0; +out_fd: + if (ret < 0) { + qemu_close(rs->fd); + rs->fd =3D -1; + } out: qemu_opts_del(opts); return ret; @@ -964,18 +977,10 @@ static void raw_reopen_commit(BDRVReopenState *stat= e) { BDRVRawReopenState *rs =3D state->opaque; BDRVRawState *s =3D state->bs->opaque; - Error *local_err =3D NULL; =20 s->check_cache_dropped =3D rs->check_cache_dropped; s->open_flags =3D rs->open_flags; =20 - /* Copy locks to the new fd before closing the old one. */ - raw_apply_lock_bytes(NULL, rs->fd, s->locked_perm, - s->locked_shared_perm, false, &local_err); - if (local_err) { - /* shouldn't fail in a sane host, but report it just in case. */ - error_report_err(local_err); - } qemu_close(s->fd); s->fd =3D rs->fd; =20 --=20 2.20.1