From nobody Mon Feb 9 11:30:08 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=suse.de Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1654593609452320.94469553279373; Tue, 7 Jun 2022 02:20:09 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-219-JeUnh-RRNYGfnWEvkNlRMw-1; Tue, 07 Jun 2022 05:20:04 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5A3363C8ACF9; Tue, 7 Jun 2022 09:19:49 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 712352166B26; Tue, 7 Jun 2022 09:19:48 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 4F27F19452D8; Tue, 7 Jun 2022 09:19:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 698B519452D8 for ; Tue, 7 Jun 2022 09:19:46 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 398C62026985; Tue, 7 Jun 2022 09:19:46 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast09.extmail.prod.ext.rdu2.redhat.com [10.11.55.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 351BC2026D64 for ; Tue, 7 Jun 2022 09:19:46 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 19FE529DD984 for ; Tue, 7 Jun 2022 09:19:46 +0000 (UTC) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-518-kt8Hq6f_M2e1zHagmzGscQ-1; Tue, 07 Jun 2022 05:19:44 -0400 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id CD3281F941; Tue, 7 Jun 2022 09:19:42 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 9443613638; Tue, 7 Jun 2022 09:19:42 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id QBOtIi4Yn2IoLwAAMHmgww (envelope-from ); Tue, 07 Jun 2022 09:19:42 +0000 X-MC-Unique: JeUnh-RRNYGfnWEvkNlRMw-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: kt8Hq6f_M2e1zHagmzGscQ-1 From: Claudio Fontana To: =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= Subject: [libvirt RFCv11 05/33] virfile: virFileDiskCopy: prepare for O_DIRECT files without wrapper Date: Tue, 7 Jun 2022 11:19:08 +0200 Message-Id: <20220607091936.7948-6-cfontana@suse.de> In-Reply-To: <20220607091936.7948-1-cfontana@suse.de> References: <20220607091936.7948-1-cfontana@suse.de> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: libvir-list@redhat.com, Claudio Fontana , "Dr . David Alan Gilbert" , Anthony Iliopoulos Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1654593609871100008 Content-Type: text/plain; charset="utf-8"; x-default="true" we will allow to use already open fds that are not empty for both read and write, as long as they are properly aligned. Adapt the truncation to take into account the initial offset. Signed-off-by: Claudio Fontana --- src/util/virfile.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 770649108f..201d7f4e64 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -4811,14 +4811,24 @@ runIOCopy(const struct runIOParams p) =20 /* handle last write size align in direct case */ if (got < buflen && p.isDirect && p.isWrite) { - if (virFileDirectWrite(p.fdout, buf, got) < 0) { + ssize_t nwritten =3D virFileDirectWrite(p.fdout, buf, got); + if (nwritten < 0) { virReportSystemError(errno, _("Unable to write %s"), p.fdo= utname); return -3; } - - if (!p.isBlockDev && ftruncate(p.fdout, total) < 0) { - virReportSystemError(errno, _("Unable to truncate %s"), p.= fdoutname); - return -4; + if (!p.isBlockDev) { + off_t off =3D lseek(p.fdout, (off_t)0, SEEK_CUR); + if (off < 0) { + virReportSystemError(errno, "%s", _("Failed to lseek t= o get current file offset")); + return -6; + } + if (nwritten > got) { + off -=3D nwritten - got; + } + if (ftruncate(p.fdout, off) < 0) { + virReportSystemError(errno, _("Unable to truncate %s")= , p.fdoutname); + return -4; + } } =20 break; @@ -4898,23 +4908,15 @@ virFileDiskCopy(int disk_fd, const char *disk_path,= int remote_fd, const char *r (oflags & O_ACCMODE)); goto cleanup; } - /* To make the implementation simpler, we give up on any - * attempt to use O_DIRECT in a non-trivial manner. */ if (!p.isBlockDev && p.isDirect) { - off_t off; - if (p.isWrite) { - /* - * note: for write we do not only check that disk_fd is seekab= le, - * we also want to know that the file is empty, so we need SEE= K_END. - */ - if ((off =3D lseek(disk_fd, 0, SEEK_END)) !=3D 0) { - virReportSystemError(off < 0 ? errno : EINVAL, "%s", - _("O_DIRECT write needs empty seekabl= e file")); - goto cleanup; - } - } else if ((off =3D lseek(disk_fd, 0, SEEK_CUR)) !=3D 0) { - virReportSystemError(off < 0 ? errno : EINVAL, "%s", - _("O_DIRECT read needs entire seekable fi= le")); + off_t off =3D lseek(disk_fd, 0, SEEK_CUR); + if (off < 0) { + virReportSystemError(errno, "%s", _("O_DIRECT needs a seekable= file")); + goto cleanup; + } + if (virFileDirectAlign(off) !=3D off) { + /* we could write some zeroes, but maybe it is safer to just f= ail */ + virReportSystemError(EINVAL, "%s", _("O_DIRECT attempted with = unaligned file pointer")); goto cleanup; } } --=20 2.26.2