From nobody Sun Nov 9 22:34:28 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 1552059992218725.9193133072703; Fri, 8 Mar 2019 07:46:32 -0800 (PST) Received: from localhost ([127.0.0.1]:45845 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h2Hhc-0003VC-A6 for importer@patchew.org; Fri, 08 Mar 2019 10:46:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:42654) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h2HZg-0005Zu-An 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 1h2HZf-00015S-BJ for qemu-devel@nongnu.org; Fri, 08 Mar 2019 10:38:16 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47022) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h2HZc-0000wu-RW; Fri, 08 Mar 2019 10:38:13 -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 209A9C4EC1; Fri, 8 Mar 2019 15:38:12 +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 AF7BA17BA4; Fri, 8 Mar 2019 15:38:10 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 8 Mar 2019 16:37:53 +0100 Message-Id: <20190308153757.25794-5-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:12 +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 4/8] file-posix: Factor out raw_reconfigure_getfd() 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" Signed-off-by: Kevin Wolf --- block/file-posix.c | 107 ++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 45 deletions(-) diff --git a/block/file-posix.c b/block/file-posix.c index ba6ab62a38..ae57ba1fc6 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -842,6 +842,62 @@ static int raw_handle_perm_lock(BlockDriverState *bs, return ret; } =20 +static int raw_reconfigure_getfd(BlockDriverState *bs, int flags, + int *open_flags, Error **errp) +{ + BDRVRawState *s =3D bs->opaque; + int fd =3D -1; + int ret; + int fcntl_flags =3D O_APPEND | O_NONBLOCK; +#ifdef O_NOATIME + fcntl_flags |=3D O_NOATIME; +#endif + + *open_flags =3D 0; + if (s->type =3D=3D FTYPE_CD) { + *open_flags |=3D O_NONBLOCK; + } + + raw_parse_flags(flags, open_flags); + +#ifdef O_ASYNC + /* Not all operating systems have O_ASYNC, and those that don't + * will not let us track the state into rs->open_flags (typically + * you achieve the same effect with an ioctl, for example I_SETSIG + * on Solaris). But we do not use O_ASYNC, so that's fine. + */ + assert((s->open_flags & O_ASYNC) =3D=3D 0); +#endif + + if ((*open_flags & ~fcntl_flags) =3D=3D (s->open_flags & ~fcntl_flags)= ) { + /* dup the original fd */ + fd =3D qemu_dup(s->fd); + if (fd >=3D 0) { + ret =3D fcntl_setfl(fd, *open_flags); + if (ret) { + qemu_close(fd); + fd =3D -1; + } + } + } + + /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */ + if (fd =3D=3D -1) { + const char *normalized_filename =3D bs->filename; + ret =3D raw_normalize_devicepath(&normalized_filename, errp); + if (ret >=3D 0) { + assert(!(*open_flags & O_CREAT)); + fd =3D qemu_open(normalized_filename, *open_flags); + if (fd =3D=3D -1) { + error_setg_errno(errp, errno, "Could not reopen file"); + return -1; + } + } + } + + return fd; +} + static int raw_reopen_prepare(BDRVReopenState *state, BlockReopenQueue *queue, Error **errp) { @@ -858,7 +914,6 @@ static int raw_reopen_prepare(BDRVReopenState *state, =20 state->opaque =3D g_new0(BDRVRawReopenState, 1); rs =3D state->opaque; - rs->fd =3D -1; =20 /* Handle options changes */ opts =3D qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); @@ -877,50 +932,12 @@ static int raw_reopen_prepare(BDRVReopenState *state, * bdrv_reopen_prepare() will detect changes and complain. */ qemu_opts_to_qdict(opts, state->options); =20 - if (s->type =3D=3D FTYPE_CD) { - rs->open_flags |=3D O_NONBLOCK; - } - - raw_parse_flags(state->flags, &rs->open_flags); - - int fcntl_flags =3D O_APPEND | O_NONBLOCK; -#ifdef O_NOATIME - fcntl_flags |=3D O_NOATIME; -#endif - -#ifdef O_ASYNC - /* Not all operating systems have O_ASYNC, and those that don't - * will not let us track the state into rs->open_flags (typically - * you achieve the same effect with an ioctl, for example I_SETSIG - * on Solaris). But we do not use O_ASYNC, so that's fine. - */ - assert((s->open_flags & O_ASYNC) =3D=3D 0); -#endif - - if ((rs->open_flags & ~fcntl_flags) =3D=3D (s->open_flags & ~fcntl_fla= gs)) { - /* dup the original fd */ - rs->fd =3D qemu_dup(s->fd); - if (rs->fd >=3D 0) { - ret =3D fcntl_setfl(rs->fd, rs->open_flags); - if (ret) { - qemu_close(rs->fd); - rs->fd =3D -1; - } - } - } - - /* If we cannot use fcntl, or fcntl failed, fall back to qemu_open() */ - if (rs->fd =3D=3D -1) { - const char *normalized_filename =3D state->bs->filename; - ret =3D raw_normalize_devicepath(&normalized_filename, errp); - if (ret >=3D 0) { - assert(!(rs->open_flags & O_CREAT)); - rs->fd =3D qemu_open(normalized_filename, rs->open_flags); - if (rs->fd =3D=3D -1) { - error_setg_errno(errp, errno, "Could not reopen file"); - ret =3D -1; - } - } + rs->fd =3D raw_reconfigure_getfd(state->bs, state->flags, &rs->open_fl= ags, + &local_err); + if (local_err) { + error_propagate(errp, local_err); + ret =3D -1; + goto out; } =20 /* Fail already reopen_prepare() if we can't get a working O_DIRECT --=20 2.20.1