From nobody Mon Feb 9 03:00:22 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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=1593772367; cv=none; d=zohomail.com; s=zohoarc; b=mNYEGrVz0r77zfwLk+1XEVzD/GvCpLOHNOCLB3I12ifjoSpKJCu1u5XYbhImXylLaSzpqX/FUbqzcTE+IIhEAx2jwFScdhhoikG8/M97qkVEpRNKB6PT/MrU0J3qGSs9xI5P3OTUb1wDwscOF7yK8fDzfuw9sQNyyw2fNwfznSg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1593772367; 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=dJdCRdXtdDJ4IsTXmj+KPwiEHAtIt7p83hqbdSxAioA=; b=Efl6Whi3CpInBsJZa1IgePY050BktgJc57ccm970rCktuzsR2XgO1S0mrHgZcTsNN09+gAU+Yd5yenI8xRROmpPQFunlCTvqHbCnf/TLsAZ3WRicTeGDa6eh9Y5b7INV10ITJvO0P5hA2UikXPYq1VZ+WWEytiZCDqrPUr426UQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1593772367189907.7936122585918; Fri, 3 Jul 2020 03:32:47 -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-387-tGcsmR8IMD2iwZG8l8DlTg-1; Fri, 03 Jul 2020 06:32:44 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 32CE5800C60; Fri, 3 Jul 2020 10:32:38 +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 11DBC1053B05; Fri, 3 Jul 2020 10:32:38 +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 D6CA61806B0C; Fri, 3 Jul 2020 10:32:37 +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 063AT9Jl017026 for ; Fri, 3 Jul 2020 06:29:09 -0400 Received: by smtp.corp.redhat.com (Postfix) id 8193B7BD56; Fri, 3 Jul 2020 10:29:09 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.194.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id 016B579242 for ; Fri, 3 Jul 2020 10:29:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1593772366; 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=dJdCRdXtdDJ4IsTXmj+KPwiEHAtIt7p83hqbdSxAioA=; b=cR75Wft0pyBUaghrqZFhuL7sslYjsB0zc6Sf3HhQvg4Pi1dGVys73Uky1/CGOFqBKYR9S2 SZ0DTlaedLEzUz5phWY+fzI4FQZL+wIWb+VQoYwzPm/XvwN+6YtOsXK9G+30fPeUDDA6RQ 9yczhXkA16opYKR3ES8ToQLZk33XOGs= X-MC-Unique: tGcsmR8IMD2iwZG8l8DlTg-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 9/9] virfdstream: Emulate skip for block devices Date: Fri, 3 Jul 2020 12:28:50 +0200 Message-Id: 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.84 on 10.5.11.22 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 --- 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 b5406fe690..0894be0341 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -512,6 +512,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, static ssize_t virFDStreamThreadDoWrite(virFDStreamDataPtr fdst, bool sparse, + bool isBlock, const int fdin, const int fdout, const char *fdinname, @@ -519,7 +520,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) { @@ -547,19 +547,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; @@ -625,7 +654,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