From nobody Mon Feb 9 05:43:37 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594151248; cv=none; d=zohomail.com; s=zohoarc; b=D1TFbBtpvy26406QFCNe4WjMBVV3UAdndOiELVzIJ+bV2hC5s+lQXSoEEldfVU7/mvJ5Py1KiYn53X8KQrrCQU4pI1JgDdFwsVTMBbtm/gVsG686PexD5/Yv0QzF2uTJrNvku7IsWC1UC0uwwCBQfeKuYUVZFmu7dMwQUXB0nmY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151248; h=Content-Type:Content-Transfer-Encoding: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=JCJ3YUIu8oXausLulrn4rWE0WTSP5IBg3eXKPYGnj70=; b=mU7IrTXO4/D2ULCAfmqOz+O+hbVnaF63ze5ZYFas6GCjdIxVxWUsuuuzWaJYi7MxJA/SG2vT0OPuD5X2D48I6qVx0Oih3h/KOMYj15q/q1T+7Gs6aY0SoxSkwwC1h9/ldhRT6WXn2Oes+B6cAN9EADm2aXudVQjotv6V0RVFgFg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1594151248908668.3319730527835; Tue, 7 Jul 2020 12:47:28 -0700 (PDT) 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-29-pHWg22foOcyt2EMGMJQzOA-1; Tue, 07 Jul 2020 15:47:25 -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 C95E080040C; Tue, 7 Jul 2020 19:47:19 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A7C3660CC0; Tue, 7 Jul 2020 19:47:19 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 7B037180954D; Tue, 7 Jul 2020 19:47:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 067JkrwF031569 for ; Tue, 7 Jul 2020 15:46:53 -0400 Received: by smtp.corp.redhat.com (Postfix) id 68B0A797F0; Tue, 7 Jul 2020 19:46:53 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id DB8A4797EF for ; Tue, 7 Jul 2020 19:46:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151247; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=JCJ3YUIu8oXausLulrn4rWE0WTSP5IBg3eXKPYGnj70=; b=jFkisKq+PYz0xpZK2BwqfAplg2OvGGjZVk/EMCAmZFrlBQwk3zqkNc7HafIRVpv/v2DJ4W DxHaSdjiVoZo/VNUiuip+NUoMj8OpF5EpEVcDcPKRzYsUer+EJTuZwHNcgkKCoPMOcknmK xT9Kymkseozo7q3FqEmaa3vsv3VO4LI= X-MC-Unique: pHWg22foOcyt2EMGMJQzOA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 15/17] virfdstream: Emulate skip for block devices Date: Tue, 7 Jul 2020 21:46:33 +0200 Message-Id: <0d947a3791f74787c93cf509311141d7a7b04e04.1594150891.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" This is similar to one of previous patches. When receiving stream (on virStorageVolUpload() and subsequent virStreamSparseSendAll()) we may receive a hole. If the volume we are saving the incoming data into is a regular file we just lseek() and ftruncate() to create the hole. But this won't work if the file is a block device. If that is the case we must write zeroes so that any subsequent reader reads nothing just zeroes (just like they would from a hole in a regular file). Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D1852528 Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- src/util/virfdstream.c | 59 +++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index 44b4855549..40825a8811 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -505,6 +505,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, static ssize_t virFDStreamThreadDoWrite(virFDStreamDataPtr fdst, bool sparse, + bool isBlock, const int fdin, const int fdout, const char *fdinname, @@ -512,7 +513,6 @@ virFDStreamThreadDoWrite(virFDStreamDataPtr fdst, { ssize_t got =3D 0; virFDStreamMsgPtr msg =3D fdst->msg; - off_t off; bool pop =3D false; =20 switch (msg->type) { @@ -540,19 +540,48 @@ virFDStreamThreadDoWrite(virFDStreamDataPtr fdst, } =20 got =3D msg->stream.hole.len; - off =3D lseek(fdout, got, SEEK_CUR); - if (off =3D=3D (off_t) -1) { - virReportSystemError(errno, - _("unable to seek in %s"), - fdoutname); - return -1; - } - - if (ftruncate(fdout, off) < 0) { - virReportSystemError(errno, - _("unable to truncate %s"), - fdoutname); - return -1; + if (isBlock) { + g_autofree char * buf =3D NULL; + const size_t buflen =3D 1 * 1024 * 1024; /* 1MiB */ + size_t toWrite =3D got; + + /* While for files it's enough to lseek() and ftruncate() to c= reate + * a hole which would emulate zeroes on read(), for block devi= ces + * we have to write zeroes to read() zeroes. And we have to wr= ite + * @got bytes of zeroes. Do that in smaller chunks though.*/ + + buf =3D g_new0(char, buflen); + + while (toWrite) { + size_t count =3D MIN(toWrite, buflen); + ssize_t r; + + if ((r =3D safewrite(fdout, buf, count)) < 0) { + virReportSystemError(errno, + _("Unable to write %s"), + fdoutname); + return -1; + } + + toWrite -=3D r; + } + } else { + off_t off; + + off =3D lseek(fdout, got, SEEK_CUR); + if (off =3D=3D (off_t) -1) { + virReportSystemError(errno, + _("unable to seek in %s"), + fdoutname); + return -1; + } + + if (ftruncate(fdout, off) < 0) { + virReportSystemError(errno, + _("unable to truncate %s"), + fdoutname); + return -1; + } } =20 pop =3D true; @@ -618,7 +647,7 @@ virFDStreamThread(void *opaque) length, total, &dataLen, buflen); else - got =3D virFDStreamThreadDoWrite(fdst, sparse, + got =3D virFDStreamThreadDoWrite(fdst, sparse, isBlock, fdin, fdout, fdinname, fdoutname); =20 --=20 2.26.2