From nobody Thu May 2 21:45:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; 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 205.139.110.61 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=1594151231; cv=none; d=zohomail.com; s=zohoarc; b=dS+cWaz75FUEEYJfgIgHs8Vq95Z9c8ML+NIjTo+Q5Y9HzH3huWT76mO9zBzamDGRabC5Y2rn+LLLieXqdUOhWKY+H2ea1s0Yjv2b6AOg/2rmR45/tdl5YQSd7szrNWJ56j9W/a9NTTMpvoKFT6XTHypKI3X7DYfFiBL9zqstnTo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151231; 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=oTB51Dv26vqoCgsjETYiwf5dUan5a2myfSEcrgGmBYQ=; b=WKc+r4xVTj8cjJWOBw1SEM2Vk5LIDVLP8nzAKFebQaLyNtEKTRwoKruagEHrf7F6nEJpG9BhmnFYjB9CfZdlFp0cpb3yh0oOpx5gsWPFSY959yYOUOu4g4/M5QN1FeEW203Ot7jM/mIg7NxiUdUK55R+HymIKnxIu4wpIUcKTio= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 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 [205.139.110.61]) by mx.zohomail.com with SMTPS id 1594151231378985.0809665502787; Tue, 7 Jul 2020 12:47:11 -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-322-dbAo15iFObeSIzNFSGFINQ-1; Tue, 07 Jul 2020 15:47:07 -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 965CD800408; Tue, 7 Jul 2020 19:47:02 +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 72A7860E1C; Tue, 7 Jul 2020 19:47:02 +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 403F21809554; Tue, 7 Jul 2020 19:47:02 +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 067JkfxK031414 for ; Tue, 7 Jul 2020 15:46:41 -0400 Received: by smtp.corp.redhat.com (Postfix) id 441E5797EF; Tue, 7 Jul 2020 19:46:41 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id B7CFF797F2 for ; Tue, 7 Jul 2020 19:46:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151230; 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=oTB51Dv26vqoCgsjETYiwf5dUan5a2myfSEcrgGmBYQ=; b=TJIOiFZ4r10/JwTPFlwHRp3HfyWsWJ3pfWcVv8sZGjOw6a4i+GjmtPDNpD5JGKWn0uuStg DUzSgm0wixs+vIEzpsVTzYw7SJiZQS4SLicobYis4Dp7zK5OZLLXd0lXIG/ZXcQlQtRmvi woElOjfWRU5T31v9+amznaqf+++7HkI= X-MC-Unique: dbAo15iFObeSIzNFSGFINQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 01/17] virfdstream: Use g_autofree in virFDStreamThreadDoRead() Date: Tue, 7 Jul 2020 21:46:19 +0200 Message-Id: <08e6cbf6f3ce02884b1cba1ed980e111f881aa61.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" The buffer that allocated in the virFDStreamThreadDoRead() can be automatically freed, or if saved into the message structure it can be stolen. Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- src/util/virfdstream.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index 1c32be47a9..e29c95690b 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -431,7 +431,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, virFDStreamMsgPtr msg =3D NULL; int inData =3D 0; long long sectionLen =3D 0; - char *buf =3D NULL; + g_autofree char *buf =3D NULL; ssize_t got; =20 if (sparse && *dataLen =3D=3D 0) { @@ -483,9 +483,8 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, } =20 msg->type =3D VIR_FDSTREAM_MSG_TYPE_DATA; - msg->stream.data.buf =3D buf; + msg->stream.data.buf =3D g_steal_pointer(&buf); msg->stream.data.len =3D got; - buf =3D NULL; if (sparse) *dataLen -=3D got; } @@ -496,7 +495,6 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, return got; =20 error: - VIR_FREE(buf); virFDStreamMsgFree(msg); return -1; } --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 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=1594151217; cv=none; d=zohomail.com; s=zohoarc; b=TrZVgLqMBlYTvcMtJVBsPmqvmu0Rr0Q6uTnmyTmLRfzVuBOSdr4jWNBsa5Hb6cXUUYtFXWaMaZpJM1n6VGeMOIr21tcGu09Kuc9YgRwQZPuL5xhSGK930h/88KRGZPEyVku2T3vNTZVMsKMa+2lNgP+gf0QZzyXTsXh7I1vQCBM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151217; 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=xgyUIRefyN17Yh/z7nev86Cq63kZFYfIvxJlh4ibVLQ=; b=JTt0zipA9yJeD4zP9KzizeRpNL2kuqETiRNosm06iG7vLV2HeBue34di3CWzKEn4OTxPABsKfE8Wp0JtrUtbdSVqrx4NalausECASl+HxhoUIO8W81lOaeStMQUei8uCCLgK7uuEvLrvlp2td/tEuhjJWJHZffCNlgs0lhn96Cg= 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 1594151217702513.7981703427848; Tue, 7 Jul 2020 12:46:57 -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-323-lEL5BToyODuwju37BQxqkw-1; Tue, 07 Jul 2020 15:46:54 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 129E8EC1A6; Tue, 7 Jul 2020 19:46:47 +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 E10BD5C1D0; Tue, 7 Jul 2020 19:46:45 +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 4E570180954D; Tue, 7 Jul 2020 19:46:44 +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 067JkgAc031421 for ; Tue, 7 Jul 2020 15:46:42 -0400 Received: by smtp.corp.redhat.com (Postfix) id 23B0E797F0; Tue, 7 Jul 2020 19:46:42 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 97887797EF for ; Tue, 7 Jul 2020 19:46:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151216; 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=xgyUIRefyN17Yh/z7nev86Cq63kZFYfIvxJlh4ibVLQ=; b=T7dta4hAe/BxEejlKa5qPffCptPBiDv39TR6pZTpuy3nRZIbqLs2cd0jesRAKg+nAbYgPn B6uaOup3yZD2tZ6C+Y5pAKXK6I8c8dmyRcmVD9KuiOEZpA66ZCmFeK72C5fOA+mgg9kTC4 olrK/capIULnOurhnzQrLXz84Qbc8Y8= X-MC-Unique: lEL5BToyODuwju37BQxqkw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 02/17] virFDStreamMsgQueuePush: Clear pointer to passed message Date: Tue, 7 Jul 2020 21:46:20 +0200 Message-Id: <521b0da7014c53ed98574882abaa415298502ca2.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.16 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" All callers of virFDStreamMsgQueuePush() have the same pattern: they explicitly set @msg passed to NULL to avoid freeing it later on. Well, the function can take address of the pointer and clear it for them. Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa =20 --- src/util/virfdstream.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index e29c95690b..6efe6c17ad 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -140,7 +140,7 @@ VIR_ONCE_GLOBAL_INIT(virFDStreamData); =20 static int virFDStreamMsgQueuePush(virFDStreamDataPtr fdst, - virFDStreamMsgPtr msg, + virFDStreamMsgPtr *msg, int fd, const char *fdname) { @@ -150,7 +150,7 @@ virFDStreamMsgQueuePush(virFDStreamDataPtr fdst, while (*tmp) tmp =3D &(*tmp)->next; =20 - *tmp =3D msg; + *tmp =3D g_steal_pointer(msg); virCondSignal(&fdst->threadCond); =20 if (safewrite(fd, &c, sizeof(c)) !=3D sizeof(c)) { @@ -489,8 +489,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, *dataLen -=3D got; } =20 - virFDStreamMsgQueuePush(fdst, msg, fdout, fdoutname); - msg =3D NULL; + virFDStreamMsgQueuePush(fdst, &msg, fdout, fdoutname); =20 return got; =20 @@ -814,8 +813,7 @@ static int virFDStreamWrite(virStreamPtr st, const char= *bytes, size_t nbytes) msg->stream.data.buf =3D buf; msg->stream.data.len =3D nbytes; =20 - virFDStreamMsgQueuePush(fdst, msg, fdst->fd, "pipe"); - msg =3D NULL; + virFDStreamMsgQueuePush(fdst, &msg, fdst->fd, "pipe"); ret =3D nbytes; } else { retry: @@ -1010,8 +1008,7 @@ virFDStreamSendHole(virStreamPtr st, =20 msg->type =3D VIR_FDSTREAM_MSG_TYPE_HOLE; msg->stream.hole.len =3D length; - virFDStreamMsgQueuePush(fdst, msg, fdst->fd, "pipe"); - msg =3D NULL; + virFDStreamMsgQueuePush(fdst, &msg, fdst->fd, "pipe"); } } else { off =3D lseek(fdst->fd, length, SEEK_CUR); --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.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 205.139.110.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=1594151235; cv=none; d=zohomail.com; s=zohoarc; b=VjSf2otZSo1Evlbe2cqnzWoh1D1s4BJ60cMFRWcHAKxoqn20swXUAuV3Z2vNfuTqEAGjcSvKFLTPfownzoLhZBKDu8YDkGrTR5xqhqPoeRXFO1Es9t7on2YDFfrGcR6Y5/7eAZ9sQv61/NICQI8iUdwfkLHzco1Y1QofGj5/6pk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151235; 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=mFuoMjmt3nEbtNeLgfXpFBKFGHjgPz96XukDr09r8H0=; b=FyFBgmaSgaWldKJcQ3PeVHPViGzjgxni5tIML5Zf4NFMhflb6hh69VydKSMe4pLlnDML593y0U7jgk+ElfQSA5TZta/q54rVqq+O89DgXwjUpYkAwF6sjxJvC3LeZRblh/aVD3mi7luRRVgoYYMKRlWkUCEVlHCEtRrSWsIz9rQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.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 [205.139.110.120]) by mx.zohomail.com with SMTPS id 1594151235331146.82585802363997; Tue, 7 Jul 2020 12:47:15 -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-240-qiIXH285OVyQ0DAzNCJpxw-1; Tue, 07 Jul 2020 15:47:11 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DD9AF8005B0; Tue, 7 Jul 2020 19:47:04 +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 BC1A619D7D; Tue, 7 Jul 2020 19:47:04 +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 893921800433; Tue, 7 Jul 2020 19:47:04 +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 067Jkh74031436 for ; Tue, 7 Jul 2020 15:46:43 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0361F797F0; Tue, 7 Jul 2020 19:46:43 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 77082797EF for ; Tue, 7 Jul 2020 19:46:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151234; 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=mFuoMjmt3nEbtNeLgfXpFBKFGHjgPz96XukDr09r8H0=; b=J2d78KGyCRRtdtonus3B+6PCJvwT/uCwt44G97HCyrZnb76Q6lteja5lmy8NdzDK4JDTUN rROsuKtpx07NwLAErkaC3X4ClO9cdLR6EJDou9M1OSmpgaXp3u6j1eA8CsMnPcnUD00RcS 9V8rt48ENz7Ot0sKAL8SCfgCiUyWjeg= X-MC-Unique: qiIXH285OVyQ0DAzNCJpxw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 03/17] virfdstream: Use autoptr for virFDStreamMsg Date: Tue, 7 Jul 2020 21:46:21 +0200 Message-Id: <48fe3808730de26d4301842499f9a784d4303577.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.84 on 10.5.11.23 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" A cleanup function can be declared for virFDStreamMsg type so that the structure doesn't have to be freed explicitly. Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- src/util/virfdstream.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index 6efe6c17ad..25661736ca 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -208,6 +208,8 @@ virFDStreamMsgFree(virFDStreamMsgPtr msg) VIR_FREE(msg); } =20 +G_DEFINE_AUTOPTR_CLEANUP_FUNC(virFDStreamMsg, virFDStreamMsgFree); + =20 static void virFDStreamMsgQueueFree(virFDStreamMsgPtr *queue) @@ -428,7 +430,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, size_t *dataLen, size_t buflen) { - virFDStreamMsgPtr msg =3D NULL; + g_autoptr(virFDStreamMsg) msg =3D NULL; int inData =3D 0; long long sectionLen =3D 0; g_autofree char *buf =3D NULL; @@ -494,7 +496,6 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, return got; =20 error: - virFDStreamMsgFree(msg); return -1; } =20 @@ -761,7 +762,7 @@ virFDStreamAbort(virStreamPtr st) static int virFDStreamWrite(virStreamPtr st, const char *bytes, size_t nby= tes) { virFDStreamDataPtr fdst =3D st->privateData; - virFDStreamMsgPtr msg =3D NULL; + g_autoptr(virFDStreamMsg) msg =3D NULL; int ret =3D -1; =20 if (nbytes > INT_MAX) { @@ -838,7 +839,6 @@ static int virFDStreamWrite(virStreamPtr st, const char= *bytes, size_t nbytes) =20 cleanup: virObjectUnlock(fdst); - virFDStreamMsgFree(msg); return ret; } =20 @@ -960,7 +960,7 @@ virFDStreamSendHole(virStreamPtr st, unsigned int flags) { virFDStreamDataPtr fdst =3D st->privateData; - virFDStreamMsgPtr msg =3D NULL; + g_autoptr(virFDStreamMsg) msg =3D NULL; off_t off; int ret =3D -1; =20 @@ -1028,7 +1028,6 @@ virFDStreamSendHole(virStreamPtr st, ret =3D 0; cleanup: virObjectUnlock(fdst); - virFDStreamMsgFree(msg); return ret; } =20 --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.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 205.139.110.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=1594151238; cv=none; d=zohomail.com; s=zohoarc; b=MzUW0EPAfcWrvatEMXZgw+4hd7UJYXZqlEX9w1Y5EnLN6ACHuzEps7HzXVZH/Dm3YTiX1fgcm1AZG/KhspJ3ztnwhfkPVWMV/TeSGInQmzuqGPUxjIUmDpzQrmwKkOW75hPYn5HCiNnsvg7FXp74KYihx6VWUisjnPGvVK0gOsg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151238; 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=vj7uwNpX8mZh+yzxGW0moBCDQoY37soTBGizt663q8Y=; b=kBc1+MZwEh1QbjLpVV7QnOC+NrwFQ0NtWYD0PNp5eOisNbwKDjPEC62gbWy1/bskGOmma4OWNLiDP0xE9Mpxn8ln6Fk3RyZmUFpyn1Z38XuXv7Ama+ZTOBqTP9sBVCzGsd6R2/P0z5w86O8MAtsNZ3DzMWlk0QRlGceDlkohCa8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.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 [205.139.110.120]) by mx.zohomail.com with SMTPS id 1594151238634276.9522597752908; Tue, 7 Jul 2020 12:47:18 -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-314-chrVH9CnOcSXpwzukUmRPQ-1; Tue, 07 Jul 2020 15:47:14 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3102A108BD13; Tue, 7 Jul 2020 19:47:07 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 123A45D9DC; Tue, 7 Jul 2020 19:47:07 +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 D653B8789F; Tue, 7 Jul 2020 19:47:06 +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 067JkhXf031442 for ; Tue, 7 Jul 2020 15:46:43 -0400 Received: by smtp.corp.redhat.com (Postfix) id D74C6797F0; Tue, 7 Jul 2020 19:46:43 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 56B97797EF for ; Tue, 7 Jul 2020 19:46:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151237; 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=vj7uwNpX8mZh+yzxGW0moBCDQoY37soTBGizt663q8Y=; b=JzrF2jK9KOu4Q2x/A/cQpXltb5D6QR9jKQ2cZ6HjAZk+hKS9M5yL6i0pm32BBvihFB5mCb EtH7bVpUcky/6KpPlr8VWVSMdAgjkgBsD1WXfV+FAaH2NAXcTQwbY8ds9k6+DDU8+QfMNL uWiAwSJBulU+JdtOqu9iHJCTEVqMacQ= X-MC-Unique: chrVH9CnOcSXpwzukUmRPQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 04/17] virfdstream: Use g_new0() instead of VIR_ALLOC() Date: Tue, 7 Jul 2020 21:46:22 +0200 Message-Id: <53566f6c87099391f3d5e8e762700d10ba307732.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.14 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 switch allow us to save a few lines of code. Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- src/util/virfdstream.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index 25661736ca..c85dee05c3 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -452,8 +452,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, buflen > length - total) buflen =3D length - total; =20 - if (VIR_ALLOC(msg) < 0) - goto error; + msg =3D g_new0(virFDStreamMsg, 1); =20 if (sparse && *dataLen =3D=3D 0) { msg->type =3D VIR_FDSTREAM_MSG_TYPE_HOLE; @@ -474,8 +473,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, buflen > *dataLen) buflen =3D *dataLen; =20 - if (VIR_ALLOC_N(buf, buflen) < 0) - goto error; + buf =3D g_new0(char, buflen); =20 if ((got =3D saferead(fdin, buf, buflen)) < 0) { virReportSystemError(errno, @@ -805,9 +803,8 @@ static int virFDStreamWrite(virStreamPtr st, const char= *bytes, size_t nbytes) goto cleanup; } =20 - if (VIR_ALLOC(msg) < 0 || - VIR_ALLOC_N(buf, nbytes) < 0) - goto cleanup; + msg =3D g_new0(virFDStreamMsg, 1); + buf =3D g_new0(char, nbytes); =20 memcpy(buf, bytes, nbytes); msg->type =3D VIR_FDSTREAM_MSG_TYPE_DATA; @@ -1003,8 +1000,7 @@ virFDStreamSendHole(virStreamPtr st, =20 virFDStreamMsgQueuePop(fdst, fdst->fd, "pipe"); } else { - if (VIR_ALLOC(msg) < 0) - goto cleanup; + msg =3D g_new0(virFDStreamMsg, 1); =20 msg->type =3D VIR_FDSTREAM_MSG_TYPE_HOLE; msg->stream.hole.len =3D length; @@ -1123,8 +1119,7 @@ static int virFDStreamOpenInternal(virStreamPtr st, =20 /* Create the thread after fdst and st were initialized. * The thread worker expects them to be that way. */ - if (VIR_ALLOC(fdst->thread) < 0) - goto error; + fdst->thread =3D g_new0(virThread, 1); =20 if (virCondInit(&fdst->threadCond) < 0) { virReportSystemError(errno, "%s", @@ -1277,8 +1272,7 @@ virFDStreamOpenFileInternal(virStreamPtr st, if (virPipe(pipefds) < 0) goto error; =20 - if (VIR_ALLOC(threadData) < 0) - goto error; + threadData =3D g_new0(virFDStreamThreadData, 1); =20 threadData->st =3D virObjectRef(st); threadData->length =3D length; --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; 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 205.139.110.61 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=1594151239; cv=none; d=zohomail.com; s=zohoarc; b=j6a954A/x7I1ZqR7UYJs3AcNWG3WDGruV25HoXtBw7ROAR3szNW11RiDcrP3m+0MLgEYkgEHuNcOuLU7Hz2//jCL+356muXRB62TBrJJQe26Jb0gdCMxM0kcW7WYKXAMnjhIYa/rUAF3dM5QNJ78LZYTP/3WJ6vMcOr1gD8RGZw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151239; 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=sqkWprPFk0ASMEDNskeK2fAPFN2BgF0lq+NB0oaM+k4=; b=VZMbv2RyQjjWoncCHgn3geTVXJu3sC76lXlSMfsSpodcDR/gVbsUtj+/vi+cA0JjIu+h3b9t+T3W8fTMSZbtsWaYDPt0viFQi9aXSUTs1aMTmXEBMEQMUJ2lkLLVkybkvIRhkQYSzOhs/RRyDF9CU4sCXFBuh/3pEneGjqzSyzc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 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 [205.139.110.61]) by mx.zohomail.com with SMTPS id 1594151239055808.2267838592182; Tue, 7 Jul 2020 12:47:19 -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-410-7yC1I2jvN6ee-N3wjKUnog-1; Tue, 07 Jul 2020 15:47:15 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CC6EB80572D; Tue, 7 Jul 2020 19:47:09 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 58251797F6; Tue, 7 Jul 2020 19:47:09 +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 1EF8B93F77; Tue, 7 Jul 2020 19:47:09 +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 067JkiQI031457 for ; Tue, 7 Jul 2020 15:46:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id B85C1797EF; Tue, 7 Jul 2020 19:46:44 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3679F797F2 for ; Tue, 7 Jul 2020 19:46:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151237; 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=sqkWprPFk0ASMEDNskeK2fAPFN2BgF0lq+NB0oaM+k4=; b=dBdcI4zK0mde8BWGq0w6xpcFcfSxCajiJr/sBdYMDthwn7Lesf7oI3PtCnlRR+rFyQRfgx j/7JMaZJGjsOOGDqjt5InfHMpuMET9LMD6CqLsFYDsuS93oq2Ek6sKEJMP9sN637mqmqEo QRMFLfnW/IBKE6PqOhV2z/8N5JQMnFE= X-MC-Unique: 7yC1I2jvN6ee-N3wjKUnog-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 05/17] virfdstream: Use VIR_AUTOCLOSE() Date: Tue, 7 Jul 2020 21:46:23 +0200 Message-Id: <8b570cd2cb917c1b6e0e880e8a43fb747e8ade29.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.13 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" Again, instead of closing FDs explicitly, we can automatically close them when they go out of their respective scopes. Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- src/util/virfdstream.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index c85dee05c3..bac1c95c0a 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -571,9 +571,9 @@ virFDStreamThread(void *opaque) virStreamPtr st =3D data->st; size_t length =3D data->length; bool sparse =3D data->sparse; - int fdin =3D data->fdin; + VIR_AUTOCLOSE fdin =3D data->fdin; char *fdinname =3D data->fdinname; - int fdout =3D data->fdout; + VIR_AUTOCLOSE fdout =3D data->fdout; char *fdoutname =3D data->fdoutname; virFDStreamDataPtr fdst =3D st->privateData; bool doRead =3D fdst->threadDoRead; @@ -633,8 +633,6 @@ virFDStreamThread(void *opaque) virObjectUnref(fdst); if (virFDStreamDataDisposed) st->privateData =3D NULL; - VIR_FORCE_CLOSE(fdin); - VIR_FORCE_CLOSE(fdout); virFDStreamThreadDataFree(data); return; =20 @@ -1160,9 +1158,10 @@ int virFDStreamConnectUNIX(virStreamPtr st, { struct sockaddr_un sa; virTimeBackOffVar timeout; + VIR_AUTOCLOSE fd =3D -1; int ret; =20 - int fd =3D socket(AF_UNIX, SOCK_STREAM, 0); + fd =3D socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) { virReportSystemError(errno, "%s", _("Unable to open UNIX socket")); goto error; @@ -1197,10 +1196,11 @@ int virFDStreamConnectUNIX(virStreamPtr st, =20 if (virFDStreamOpenInternal(st, fd, NULL, 0) < 0) goto error; + + fd =3D -1; return 0; =20 error: - VIR_FORCE_CLOSE(fd); return -1; } =20 --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; 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 205.139.110.61 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=1594151229; cv=none; d=zohomail.com; s=zohoarc; b=devA3SmYFqkCGVez5rH/X4PQLzp3Ig04vAPxYKU/OsfB5ub9uKRs5wCYOMfFhOrQdX28/O6IH5+ESa70duIX8ul1cNU69SwDX2KUj+NfVsul7OCcd9sN3y9RENSB3E7rpti9gN2BXIFvCJfdvODWIi2CLtogUsc/wy5Oiv5oj/o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151229; 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=pVTbH/IHELYnvuf8h5GS2vW6fvdOiwOQ68/w1aKxiUI=; b=N8nByYhkTpDgxtBQbcuq8C9ujpInqah8pAgTIsZdxavQ0Eyusld5bVde6ub2VMo7eSOPevIKf31s3znEMeOkZD3/Vw7TbiFNSx9/koDtavESEJhuv6Y1Dw34wwpLYY53rXLJDzqHMmsbqzgv5G9Kw0pONAbkPQc/2sjQm8YrPt4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 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 [205.139.110.61]) by mx.zohomail.com with SMTPS id 1594151229875392.94996479646284; Tue, 7 Jul 2020 12:47:09 -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-278-PLwrO_hXNZ2CTfbXC6EhEg-1; Tue, 07 Jul 2020 15:47:06 -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 EA203EC1A9; Tue, 7 Jul 2020 19:47:00 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C8B1210016DA; Tue, 7 Jul 2020 19:47:00 +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 6B4F172F7E; Tue, 7 Jul 2020 19:47:00 +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 067JkjpB031466 for ; Tue, 7 Jul 2020 15:46:45 -0400 Received: by smtp.corp.redhat.com (Postfix) id 97042797F0; Tue, 7 Jul 2020 19:46:45 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 167A6797EF for ; Tue, 7 Jul 2020 19:46:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151228; 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=pVTbH/IHELYnvuf8h5GS2vW6fvdOiwOQ68/w1aKxiUI=; b=EZVz8xm4fGGWIcvdrEthsT/Q9dkT+vLFsMsVjsCYQLoYNGCuWumg3LN2urlHMmF7CvCiq6 T+okeIL73t/uTEPY3UhVld6IYM0qcHbfCTpqpHkkW+hbVI66EGZFW4q9nYKcEydwd8PcFb fuT8Sdr9uxxlDAsdR0OLo3yGOvb/W9k= X-MC-Unique: PLwrO_hXNZ2CTfbXC6EhEg-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 06/17] virfdstream: Drop some needless labels Date: Tue, 7 Jul 2020 21:46:24 +0200 Message-Id: <6940e9304bd7e08c7ca1f2554a8d8812ba9325ab.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.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" After previous cleanups, some labels in some functions have nothing but 'return' statement in them. Drop the labels and replace 'goto'-s with respective return statements. Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- src/util/virfdstream.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index bac1c95c0a..50209a8bd4 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -438,7 +438,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, =20 if (sparse && *dataLen =3D=3D 0) { if (virFileInData(fdin, &inData, §ionLen) < 0) - goto error; + return -1; =20 if (length && sectionLen > length - total) @@ -466,7 +466,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, virReportSystemError(errno, _("unable to seek in %s"), fdinname); - goto error; + return -1; } } else { if (sparse && @@ -479,7 +479,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, virReportSystemError(errno, _("Unable to read %s"), fdinname); - goto error; + return -1; } =20 msg->type =3D VIR_FDSTREAM_MSG_TYPE_DATA; @@ -492,9 +492,6 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, virFDStreamMsgQueuePush(fdst, &msg, fdout, fdoutname); =20 return got; - - error: - return -1; } =20 =20 @@ -1164,22 +1161,22 @@ int virFDStreamConnectUNIX(virStreamPtr st, fd =3D socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) { virReportSystemError(errno, "%s", _("Unable to open UNIX socket")); - goto error; + return -1; } =20 memset(&sa, 0, sizeof(sa)); sa.sun_family =3D AF_UNIX; if (abstract) { if (virStrcpy(sa.sun_path+1, path, sizeof(sa.sun_path)-1) < 0) - goto error; + return -1; sa.sun_path[0] =3D '\0'; } else { if (virStrcpyStatic(sa.sun_path, path) < 0) - goto error; + return -1; } =20 if (virTimeBackOffStart(&timeout, 1, 3*1000 /* ms */) < 0) - goto error; + return -1; while (virTimeBackOffWait(&timeout)) { ret =3D connect(fd, (struct sockaddr *)&sa, sizeof(sa)); if (ret =3D=3D 0) @@ -1191,17 +1188,14 @@ int virFDStreamConnectUNIX(virStreamPtr st, continue; } =20 - goto error; + return -1; } =20 if (virFDStreamOpenInternal(st, fd, NULL, 0) < 0) - goto error; + return -1; =20 fd =3D -1; return 0; - - error: - return -1; } =20 =20 --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 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=1594151305; cv=none; d=zohomail.com; s=zohoarc; b=Zu4I979j9c1iwZv5FFKZujmvUJEUaL0TU9XGTAD3tdwbFZEONteJGpTD6NQweVAjivN+yPTf3GYa+xF6X7x6/FOdVGiNWk497uRuEAym0S1YwxYkE8/GoMECqveaMY6dZjBaa741CLB+04hSC0c2446NvbAHkbD2WDRQiBFR1hY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151305; 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=S6HorQNQM3PX4VmnOgK/Y0BNInrkfPwZYps7CJndr9E=; b=agbOwRl9Ldw3yEjCXO4yiaEmdKgVyKImF0C2JflQeFKaHhFKtE1O8ShzEgMJEF/gDy9pgkThw7d+o84TgLjY8TxZxjiCaTQ2cd2Cb7PrYgT/s0vSzmdbAhjFIF86uuHohAH3N5AftaBdiGlxxy1aO2zvfS/5wDksg5dHOIgLjlA= 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 1594151305066381.2904996520457; Tue, 7 Jul 2020 12:48:25 -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-417-8Gir9LqIOG6KfanSAsdRRQ-1; Tue, 07 Jul 2020 15:47:10 -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 F3EE618A8228; Tue, 7 Jul 2020 19:47:04 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D2D8B60E3E; Tue, 7 Jul 2020 19:47:04 +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 4573172F7B; Tue, 7 Jul 2020 19:47:04 +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 067Jkk3H031482 for ; Tue, 7 Jul 2020 15:46:46 -0400 Received: by smtp.corp.redhat.com (Postfix) id 76672797F0; Tue, 7 Jul 2020 19:46:46 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id EA0D5797EF for ; Tue, 7 Jul 2020 19:46:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151303; 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=S6HorQNQM3PX4VmnOgK/Y0BNInrkfPwZYps7CJndr9E=; b=M8ymcwpi9hvv2So+U9kHdIY2dV1gYUlL1zCBQeof7/cUa33ssQkibRPX0g4CCTFWO+vHrf e7KfRqyGFU90uKf/pnr9pcQY0PP45aw7ROn0SIZH059Vq1gdpxJoSHQDDKxyAqSfOe0HOH CZbC8r9C/JCr8VD7Cl2LE2eXDUjBP10= X-MC-Unique: 8Gir9LqIOG6KfanSAsdRRQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 07/17] libvirt-storage: Document volume upload/download stream format Date: Tue, 7 Jul 2020 21:46:25 +0200 Message-Id: <1f42dd145f3513cb90bddef4f75c790c68c3eb03.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" For libvirt, the volume is just a binary blob and it doesn't interpret data on volume upload/download. But as it turns out, this unspoken assumption is not clear to our users. Document it explicitly. Suggested in: https://bugzilla.redhat.com/show_bug.cgi?id=3D1851023#c17 Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- src/libvirt-storage.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libvirt-storage.c b/src/libvirt-storage.c index a45c8b98c1..8738f6dd14 100644 --- a/src/libvirt-storage.c +++ b/src/libvirt-storage.c @@ -1590,7 +1590,9 @@ virStorageVolCreateXMLFrom(virStoragePoolPtr pool, * * Download the content of the volume as a stream. If @length * is zero, then the remaining contents of the volume after - * @offset will be downloaded. + * @offset will be downloaded. Please note, that the data is + * not interpreted and therefore data received by stream + * client are in the very same format the volume is in. * * If VIR_STORAGE_VOL_DOWNLOAD_SPARSE_STREAM is set in @flags * effective transmission of holes is enabled. This assumes using @@ -1663,7 +1665,9 @@ virStorageVolDownload(virStorageVolPtr vol, * will fail if @offset + @length exceeds the size of the * volume. Otherwise, if @length is non-zero, an error * will be raised if an attempt is made to upload greater - * than @length bytes of data. + * than @length bytes of data. Please note, that the data is + * not interpreted and therefore data sent by stream client + * must be in the very same format the volume is in. * * If VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM is set in @flags * effective transmission of holes is enabled. This assumes using --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 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=1594151242; cv=none; d=zohomail.com; s=zohoarc; b=QPtZc/aHDqYtP5sKt66if722qEkpTkfiCUGeYjzN0zR3Sdbm/evZBH/UGN5DraNmAeNJ5eYuVFJevC9JfgziMp1I+43eAPcwM8H8BV3aCwwc0X9NOhZz61fHxVdqDW45z5LfIisqRsvA5IDz8h4HHGH06iXIo1+DkOCin1BI22A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151242; 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=Cu4MvNtZXUSlsnp+OmEumwyHrp/fUwarZxm2sJGc08o=; b=LZEcKpfPBpZaRmhz6Ak2s7iVYy5P8N9NBBUXSlGAngaMi9goS71402OWcWtzt8xhsjdkf/F70BSP0AOrtjQyP7dKmXzx1b/MLWW3cxiAwPgU0YTNQVyTTTptPTFaXVEOvLUzACbPDgb2wk6geR7ZFA3A3vHT80WiL4CTvIREaY0= 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 1594151242093776.880809620215; Tue, 7 Jul 2020 12:47:22 -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-301-JFey1oSrOm2Kr-277AhTrA-1; Tue, 07 Jul 2020 15:47:18 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 171CEEC1A4; Tue, 7 Jul 2020 19:47:13 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EC4AC5DA27; Tue, 7 Jul 2020 19:47:12 +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 BE7A893F70; Tue, 7 Jul 2020 19:47:12 +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 067JklSW031491 for ; Tue, 7 Jul 2020 15:46:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id 55E53797F0; Tue, 7 Jul 2020 19:46:47 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id C9BBA797EF for ; Tue, 7 Jul 2020 19:46:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151240; 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=Cu4MvNtZXUSlsnp+OmEumwyHrp/fUwarZxm2sJGc08o=; b=JmqrCmngykBVxKgmYa5nEskoMQHcOctEo6jwlGbyhT8wLfkwtNLEweeu4mcrlD+vuB1uk0 OIgs0zgsCO0qz2hsEJheLB+t9puPifIO+rMZIjwpM9pAGrCr5PPSHMIvrZX060fhnKvGv8 ZcrzLKYPAXAprDYq1euHiR+sFCA9M9w= X-MC-Unique: JFey1oSrOm2Kr-277AhTrA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 08/17] virstring: Introduce virStringIsNull() Date: Tue, 7 Jul 2020 21:46:26 +0200 Message-Id: <9d4126355f1203963c6e98b3884ef1a32b3b6e2a.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.14 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 function will be used to detect zero buffers (which are going to be interpreted as hole in virStream later). I shamelessly took inspiration from coreutils. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virstring.c | 38 ++++++++++++++++++++++++++++++++ src/util/virstring.h | 2 ++ tests/virstringtest.c | 47 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ae0e253ab9..1d80aeb833 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3197,6 +3197,7 @@ virStringHasChars; virStringHasControlChars; virStringHasSuffix; virStringIsEmpty; +virStringIsNull; virStringIsPrintable; virStringListAdd; virStringListAutoFree; diff --git a/src/util/virstring.c b/src/util/virstring.c index e9e792f3bf..c26bc770d4 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -1404,3 +1404,41 @@ int virStringParseYesNo(const char *str, bool *resul= t) =20 return 0; } + + +/** + * virStringIsNull: + * @buf: buffer to check + * @len: the length of the buffer + * + * For given buffer @buf and its size @len determine whether + * it contains only zero bytes (NUL) or not. + * + * Returns: true if buffer is full of zero bytes, + * false otherwise. + */ +bool virStringIsNull(const char *buf, size_t len) +{ + const char *p =3D buf; + + if (!len) + return true; + + /* Check up to 16 first bytes. */ + for (;;) { + if (*p) + return false; + + p++; + len--; + + if (!len) + return true; + + if ((len & 0xf) =3D=3D 0) + break; + } + + /* Now we know first 16 bytes are NUL, memcmp with self. */ + return memcmp(buf, p, len) =3D=3D 0; +} diff --git a/src/util/virstring.h b/src/util/virstring.h index 360c68395c..d0e54358ac 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -185,6 +185,8 @@ int virStringParsePort(const char *str, int virStringParseYesNo(const char *str, bool *result) G_GNUC_WARN_UNUSED_RESULT; +bool virStringIsNull(const char *buf, size_t len); + /** * VIR_AUTOSTRINGLIST: * diff --git a/tests/virstringtest.c b/tests/virstringtest.c index bee49e6cb6..5838a57574 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -649,6 +649,27 @@ static int testFilterChars(const void *args) return ret; } =20 +struct testIsNulData { + const char *buf; + size_t len; + bool nul; +}; + +static int +testIsNul(const void *args) +{ + const struct testIsNulData *data =3D args; + int rc; + + rc =3D virStringIsNull(data->buf, data->len); + if (rc !=3D data->nul) { + fprintf(stderr, "Returned %d, expected %d\n", rc, data->nul); + return -1; + } + + return 0; +} + static int mymain(void) { @@ -977,6 +998,32 @@ mymain(void) TEST_FILTER_CHARS(NULL, NULL, NULL); TEST_FILTER_CHARS("hello 123 hello", "helo", "hellohello"); =20 +#define TEST_IS_NUL(expect, ...) \ + do { \ + const char buf[] =3D {__VA_ARGS__ }; \ + struct testIsNulData isNulData =3D { \ + .buf =3D buf, \ + .len =3D G_N_ELEMENTS(buf), \ + .nul =3D expect \ + }; \ + if (virTestRun("isNul check", \ + testIsNul, &isNulData) < 0) \ + ret =3D -1; \ + } while (0) + + TEST_IS_NUL(true); + TEST_IS_NUL(true, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); + TEST_IS_NUL(false, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01); + TEST_IS_NUL(false, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01); + TEST_IS_NUL(false, + 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, + 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, + 0x00); + return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 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=1594151237; cv=none; d=zohomail.com; s=zohoarc; b=bGZmeym2a//B23pu/iTQy++Z6ZApFkqTaL34mtesnX5spWIyRkA2cFyecE+YzF+YPgCL37iqycVii50munJaD6NUtVZIFEU3Z1a2GhF5KXAmJLt7yOskxDEv+q5htOI2NjEuSgBXNVs5iqtweLplEn2I0H4OYLl5ntvcmCAy7s4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151237; 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=GJP+4yrEpdiBDkZUo9k3hGdF88hY+dXSA/iPni5FSJA=; b=Yd/OtxPp1IBXKtzGfrMLAPL81IxQMAV0UKdXmTQeW2JHlkDYHL3L3c6rFp+syHwjq7amW8ON03NxhaE3JUo2iZhDCiRm9UuEoOY3G+PgIe04b4hEg3nE7Fo5ndy94i6xADFBIloK9aNptENWJkYUns00izakUMg300+7OO3ENSM= 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 1594151237986914.3131670785499; Tue, 7 Jul 2020 12:47:17 -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-513-_O8Szm8yMreegxwps-1lLw-1; Tue, 07 Jul 2020 15:47:14 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 15797EC1B2; Tue, 7 Jul 2020 19:47:07 +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 E9D2A78469; Tue, 7 Jul 2020 19:47:06 +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 850C31806B0D; Tue, 7 Jul 2020 19:47:06 +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 067Jkmih031504 for ; Tue, 7 Jul 2020 15:46:48 -0400 Received: by smtp.corp.redhat.com (Postfix) id 351ED797F0; Tue, 7 Jul 2020 19:46:48 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id A913F797EF for ; Tue, 7 Jul 2020 19:46:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151236; 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=GJP+4yrEpdiBDkZUo9k3hGdF88hY+dXSA/iPni5FSJA=; b=fNDkCQlsJYgUdkXye81zcnjAFCBDhBcOKFO5v2COAIYujI6UztskTt5LzMdC8+SRAwHgCd jMxrk8qs4AEOkWKzJ+9d0Z9EYAfsVFGSzD87JKMcgQ//fd/OZ3JDw9QChiUsuDzp/HxxzK gRDeqYZh48j23xx8aeknnsPg3vsTZDc= X-MC-Unique: _O8Szm8yMreegxwps-1lLw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 09/17] virfile: Introduce virFileInDataDetectZeroes() Date: Tue, 7 Jul 2020 21:46:27 +0200 Message-Id: <22acfe6aa6e2499cbb3de0b94c02ed6698afd301.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.11 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" For given file descriptor determine if the current position it is in plus 1MiB (arbitrary chosen value) consists solely from zero bytes or not. This is a block device friendly version of virFileInData(). Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virfile.c | 67 ++++++++++++++++++++++++++++++++++++++++ src/util/virfile.h | 4 +++ 3 files changed, 72 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1d80aeb833..6b5a751788 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2070,6 +2070,7 @@ virFileGetMountSubtree; virFileGetXAttr; virFileGetXAttrQuiet; virFileInData; +virFileInDataDetectZeroes; virFileIsCDROM; virFileIsDir; virFileIsExecutable; diff --git a/src/util/virfile.c b/src/util/virfile.c index c034df5931..a35d9ccb7a 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -4064,6 +4064,73 @@ virFileInData(int fd G_GNUC_UNUSED, #endif /* !HAVE_DECL_SEEK_HOLE */ =20 =20 +#define DETECT_ZEORES_BLOCK_SIZE (1 * 1024 * 1024) + +/* + * virFileInDataDetectZeroes: + * @fd: file to check + * @inData: true if current position in the @fd is in data section + * @length: amount of bytes until the end of the current section + * + * This behaves exactly like virFileInData() except it doesn't use SEEK_DA= TA + * and SEEK_HOLE rather than a buffer into which it reads data from @fd and + * detects if the buffer is full of zeroes. Therefore, it is safe to use on + * special files (e.g. block devices). On the other hand it sees only + * DETECT_ZEORES_BLOCK_SIZE ahead. + * + * Returns: 0 on success, + * -1 otherwise. + */ +int +virFileInDataDetectZeroes(int fd, + int *inData, + long long *length) +{ + const size_t buflen =3D DETECT_ZEORES_BLOCK_SIZE; + g_autofree char *bytes =3D NULL; + off_t cur; + ssize_t r; + int ret =3D -1; + + /* Get current position */ + cur =3D lseek(fd, 0, SEEK_CUR); + if (cur =3D=3D (off_t) -1) { + virReportSystemError(errno, "%s", + _("Unable to get current position in file")); + goto cleanup; + } + + bytes =3D g_new0(char, buflen); + + if ((r =3D saferead(fd, bytes, buflen)) < 0) { + virReportSystemError(errno, "%s", + _("Unable to read from file")); + goto cleanup; + } + + *inData =3D !virStringIsNull(bytes, r); + *length =3D r; + ret =3D 0; + + cleanup: + /* At any rate, reposition back to where we started. */ + if (cur !=3D (off_t) -1) { + int theerrno =3D errno; + + if (lseek(fd, cur, SEEK_SET) =3D=3D (off_t) -1) { + virReportSystemError(errno, "%s", + _("unable to restore position in file")); + ret =3D -1; + if (theerrno =3D=3D 0) + theerrno =3D errno; + } + + errno =3D theerrno; + } + return ret; +} + + /** * virFileReadValueInt: * @value: pointer to int to be filled in with the value diff --git a/src/util/virfile.h b/src/util/virfile.h index 7a92364a5c..9a5eade609 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -352,6 +352,10 @@ int virFileInData(int fd, int *inData, long long *length); =20 +int virFileInDataDetectZeroes(int fd, + int *inData, + long long *length); + G_DEFINE_AUTOPTR_CLEANUP_FUNC(virFileWrapperFd, virFileWrapperFdFree); =20 int virFileGetXAttr(const char *path, --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 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-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=1594151232; cv=none; d=zohomail.com; s=zohoarc; b=exMAVVqgmg9Hviz8WkimovHmgAnyH07LA1SkWKTUVaruajcFbglk0DwR+591Ze/HxK1nmEF7jxtDzd9knKLRKPWHWz0722+LQebvjnkF+V58RHZU6rkSlf8ArY2NXCDrbfunzD5xWsLDOwWV1j5ZxS6Pn50G0smZxNIpAm3aqTk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151232; 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=/UW50IMzLK0fDKp9M0omUd4XLUKGp7krwUQWd6pVmJc=; b=oJwGgicHT5DSnbDTMP8tRq5T4VqBcs4+kQT162r6SmNYJtuQPyFZk6fpRc51Ia+dNAzrO0aBzicrO15XKdO3MfqOvnvmpyBVlg/zC6iUQhPDpxfDuZyaGhoN/fxHSTCL8jtGxuRfprpox5fkbbRzFJAtf1QfOv/38BuPYki/ZmU= 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-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 159415123243476.26196013579022; Tue, 7 Jul 2020 12:47:12 -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-3-R96dXebGNZq0shon5ZHZWQ-1; Tue, 07 Jul 2020 15:47:09 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9E3F28015FA; Tue, 7 Jul 2020 19:47:02 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 79AE719D7D; Tue, 7 Jul 2020 19:47:02 +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 4AD06833A3; Tue, 7 Jul 2020 19:47:02 +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 067Jknk9031512 for ; Tue, 7 Jul 2020 15:46:49 -0400 Received: by smtp.corp.redhat.com (Postfix) id 14F02797F0; Tue, 7 Jul 2020 19:46:49 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 88E4F797EF for ; Tue, 7 Jul 2020 19:46:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151231; 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=/UW50IMzLK0fDKp9M0omUd4XLUKGp7krwUQWd6pVmJc=; b=D7/wquKwWabFeElCL8b2PSJHZTBFYsqzcKcNZXoIx/FAWjL587tMIWgOMVIROdgiAcg60w v7VbHqULx6rso1Uq7uc1NgcvPjxIq+JXK4npRk0wGoaDQyi8uemK5bdRVdm65f6XPOCRWY uh79HcWMxRu7aS7jwxC7C7DQBgY2DGs= X-MC-Unique: R96dXebGNZq0shon5ZHZWQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 10/17] virsh: Pass virshStreamCallbackDataPtr to virshStreamSink() and virshStreamSkip() Date: Tue, 7 Jul 2020 21:46:28 +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.23 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" These callback will need to know more that the FD they are working on. Pass the structure that is passed to other stream callbacks (e.g. virshStreamSource() or virshStreamSourceSkip()) instead of inventing a new one. Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- tools/virsh-util.c | 10 +++++----- tools/virsh-volume.c | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/tools/virsh-util.c b/tools/virsh-util.c index 932d6d0849..89f15efd08 100644 --- a/tools/virsh-util.c +++ b/tools/virsh-util.c @@ -146,9 +146,9 @@ virshStreamSink(virStreamPtr st G_GNUC_UNUSED, size_t nbytes, void *opaque) { - int *fd =3D opaque; + virshStreamCallbackDataPtr cbData =3D opaque; =20 - return safewrite(*fd, bytes, nbytes); + return safewrite(cbData->fd, bytes, nbytes); } =20 =20 @@ -186,13 +186,13 @@ virshStreamSkip(virStreamPtr st G_GNUC_UNUSED, long long offset, void *opaque) { - int *fd =3D opaque; + virshStreamCallbackDataPtr cbData =3D opaque; off_t cur; =20 - if ((cur =3D lseek(*fd, offset, SEEK_CUR)) =3D=3D (off_t) -1) + if ((cur =3D lseek(cbData->fd, offset, SEEK_CUR)) =3D=3D (off_t) -1) return -1; =20 - if (ftruncate(*fd, cur) < 0) + if (ftruncate(cbData->fd, cur) < 0) return -1; =20 return 0; diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c index 72394915d8..5cbc2efb7a 100644 --- a/tools/virsh-volume.c +++ b/tools/virsh-volume.c @@ -793,6 +793,7 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd) unsigned long long offset =3D 0, length =3D 0; bool created =3D false; virshControlPtr priv =3D ctl->privData; + virshStreamCallbackData cbData; unsigned int flags =3D 0; =20 if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0) @@ -820,6 +821,9 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd) created =3D true; } =20 + cbData.ctl =3D ctl; + cbData.fd =3D fd; + if (!(st =3D virStreamNew(priv->conn, 0))) { vshError(ctl, _("cannot create a new stream")); goto cleanup; @@ -830,7 +834,7 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd) goto cleanup; } =20 - if (virStreamSparseRecvAll(st, virshStreamSink, virshStreamSkip, &fd) = < 0) { + if (virStreamSparseRecvAll(st, virshStreamSink, virshStreamSkip, &cbDa= ta) < 0) { vshError(ctl, _("cannot receive data from volume %s"), name); goto cleanup; } --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.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 205.139.110.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=1594151236; cv=none; d=zohomail.com; s=zohoarc; b=AYQKWfoDsa3kYBc2iQP9VE+Kem9HHlMq37J5EYEFgPupSv/ktEtxGIBBWJSM8jERfb8nxJ2JUg93Y+uMsGXnQCCWCvUIZK5Qc9H8XnLeArvEdcox9iQqJMm27bGXLSlSzbuwdeMttFe4aE2xFA6t0AViDNOnUrawk2GQLGRuZOk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151236; 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=C3g/PCvVDm0zZJyvtMAZa1Vg0ZtgVdgF8mhfLJiEgos=; b=QEbPiMpZ8yJiVWQBpypJKwvCih8DRO81CwVe0nXSrlymkRk8uvj81jrplmoRUz1zo/gbl24pCpbWyckdEIsSKXCCkvAJdlA71UG7u/vVpLqm/2oRM9YPESd8lZAJmvktdWgD3gUGv5FONU5mzdiTDOq3S1tK0K7kVCVbzcQiLEU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.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 [205.139.110.120]) by mx.zohomail.com with SMTPS id 1594151236321719.8906125140134; Tue, 7 Jul 2020 12:47:16 -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-344-9LmGNauiM_aFYyytJpl8NA-1; Tue, 07 Jul 2020 15:47:13 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D828580040F; Tue, 7 Jul 2020 19:47:04 +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 B85E771669; Tue, 7 Jul 2020 19:47:04 +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 839A71809554; Tue, 7 Jul 2020 19:47:04 +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 067JknxO031520 for ; Tue, 7 Jul 2020 15:46:49 -0400 Received: by smtp.corp.redhat.com (Postfix) id E8593797F0; Tue, 7 Jul 2020 19:46:49 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 68048797EF for ; Tue, 7 Jul 2020 19:46:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151235; 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=C3g/PCvVDm0zZJyvtMAZa1Vg0ZtgVdgF8mhfLJiEgos=; b=bIrkS7EYooDvt9eN9HkYdpBr0/gfWuiQgYa+d0ipoQVAtMVRlkolHyOkw7i7hCY6EYOk8p xCv457HwlbOKxetJgV15KEYCMFpBQ4xyIULfnROTh5+jkRN6Zh60YWrFCQhrjDEOlCu5R6 kOToU5WMaFuTLffcCg4BPqNqkwQUL6g= X-MC-Unique: 9LmGNauiM_aFYyytJpl8NA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 11/17] virsh: Track if vol-upload or vol-download work over a block device Date: Tue, 7 Jul 2020 21:46:29 +0200 Message-Id: <0d90dc73a936e4c82ae6561cfc10c71e6d98204e.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.11 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" We can't use virFileInData() with block devices, but we could use new virFileInDataDetectZeroes(). But to decide we need to know if the FD we are reading data from / writing data to is a block device. Store this information in _virshStreamCallbackData. Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- tools/virsh-util.h | 1 + tools/virsh-volume.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/tools/virsh-util.h b/tools/virsh-util.h index 72653d9735..9ef28cfe0a 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -72,6 +72,7 @@ typedef virshStreamCallbackData *virshStreamCallbackDataP= tr; struct _virshStreamCallbackData { vshControl *ctl; int fd; + bool isBlock; }; =20 int diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c index 5cbc2efb7a..20823e2d10 100644 --- a/tools/virsh-volume.c +++ b/tools/virsh-volume.c @@ -679,6 +679,7 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd) virshControlPtr priv =3D ctl->privData; unsigned int flags =3D 0; virshStreamCallbackData cbData; + struct stat sb; =20 if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0) return false; @@ -697,8 +698,14 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd) goto cleanup; } =20 + if (fstat(fd, &sb) < 0) { + vshError(ctl, _("unable to stat %s"), file); + goto cleanup; + } + cbData.ctl =3D ctl; cbData.fd =3D fd; + cbData.isBlock =3D !!S_ISBLK(sb.st_mode); =20 if (vshCommandOptBool(cmd, "sparse")) flags |=3D VIR_STORAGE_VOL_UPLOAD_SPARSE_STREAM; @@ -795,6 +802,7 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd) virshControlPtr priv =3D ctl->privData; virshStreamCallbackData cbData; unsigned int flags =3D 0; + struct stat sb; =20 if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0) return false; @@ -821,8 +829,14 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd) created =3D true; } =20 + if (fstat(fd, &sb) < 0) { + vshError(ctl, _("unable to stat %s"), file); + goto cleanup; + } + cbData.ctl =3D ctl; cbData.fd =3D fd; + cbData.isBlock =3D !!S_ISBLK(sb.st_mode); =20 if (!(st =3D virStreamNew(priv->conn, 0))) { vshError(ctl, _("cannot create a new stream")); --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; 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 205.139.110.61 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=1594151415; cv=none; d=zohomail.com; s=zohoarc; b=d10bGOvjfsJwsVt4GHQNXhUnx8qjluJfZ0ALChdifViPS11kTVJVpWIyIlEOjymMXQfdmUXNrCV+axMOejJVjfU5d9JdueNSCYWld84uSe1sOwdFfuecM4GwjTudVIdMIBeKRlaaWUz64XrRRx2GBK7A8UPT8t/JYzLnS8VwHWs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151415; 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=4iaslwF3zmWy+jtD7l+8UjwLEm5V9FlaqxEw3Ms2V0M=; b=G74Qu92vWUB1GHMOe7D8jPmPK25UBljlcvJXUgVVbBY3ChcCCAjOXfh34WK/kh1KoYc0TaJCoHwjQvLibh6oS3Tl9Noc0AMf/V9Qcl0SGRvU4BavCeE+68HEXf5oI3OopQpOO6MncHA/F5EAw+MFVWC/0l2TYBgkCm1XPobU3Gc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 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-2.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1594151415909964.1169247953866; Tue, 7 Jul 2020 12:50:15 -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-344-Ro3einxEPp2fTuTFNxROhA-1; Tue, 07 Jul 2020 15:47:21 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 53A2E100CCC7; Tue, 7 Jul 2020 19:47:15 +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 35269797F9; Tue, 7 Jul 2020 19:47:15 +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 04EDD180530E; Tue, 7 Jul 2020 19:47:15 +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 067JkoTY031535 for ; Tue, 7 Jul 2020 15:46:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id C88C3797F1; Tue, 7 Jul 2020 19:46:50 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 475DE797F0 for ; Tue, 7 Jul 2020 19:46:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151414; 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=4iaslwF3zmWy+jtD7l+8UjwLEm5V9FlaqxEw3Ms2V0M=; b=cFCkYTqUds2TZwlA0Wt8PqTlt/Gag4GEqeTUxe88ghiqZ38EdMuStqnJ17xV4x356cmc8O FoKIyIEBPdK0oM6YaP/gcNGRGum+2xK71NTLaDGj+7Cc3Yfy95wTZMo3ip9ShftwAkg20h ppz6v57kF5pi7HokrDKo+546SU5liDs= X-MC-Unique: Ro3einxEPp2fTuTFNxROhA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 12/17] virshStreamSkip: Emulate skip for block devices Date: Tue, 7 Jul 2020 21:46:30 +0200 Message-Id: <5ca67478806396ea0335f5adc296cab8141d15c1.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.13 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 callback is called when the server sends us STREAM_HOLE meaning there is no real data, only zeroes. For regular files we would just seek() beyond EOF and ftruncate() to create the hole. But for block devices this won't work. Not only we can't seek() beyond EOF, and ftruncate() will fail, this approach won't fill the device with zeroes. We have to do it manually. Partially resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D1852528 Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- tools/virsh-util.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tools/virsh-util.c b/tools/virsh-util.c index 89f15efd08..884261eb49 100644 --- a/tools/virsh-util.c +++ b/tools/virsh-util.c @@ -189,11 +189,33 @@ virshStreamSkip(virStreamPtr st G_GNUC_UNUSED, virshStreamCallbackDataPtr cbData =3D opaque; off_t cur; =20 - if ((cur =3D lseek(cbData->fd, offset, SEEK_CUR)) =3D=3D (off_t) -1) - return -1; + if (cbData->isBlock) { + g_autofree char * buf =3D NULL; + const size_t buflen =3D 1 * 1024 * 1024; /* 1MiB */ =20 - if (ftruncate(cbData->fd, cur) < 0) - return -1; + /* While for files it's enough to lseek() and ftruncate() to create + * a hole which would emulate zeroes on read(), for block devices + * we have to write zeroes to read() zeroes. And we have to write + * @got bytes of zeroes. Do that in smaller chunks though.*/ + + buf =3D g_new0(char, buflen); + + while (offset) { + size_t count =3D MIN(offset, buflen); + ssize_t r; + + if ((r =3D safewrite(cbData->fd, buf, count)) < 0) + return -1; + + offset -=3D r; + } + } else { + if ((cur =3D lseek(cbData->fd, offset, SEEK_CUR)) =3D=3D (off_t) -= 1) + return -1; + + if (ftruncate(cbData->fd, cur) < 0) + return -1; + } =20 return 0; } --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.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 205.139.110.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=1594151287; cv=none; d=zohomail.com; s=zohoarc; b=Xp+dsv+2+ZpEOibyFRd/0GZiy1PNR0DQ8F9kr5Wf+LyS3WotXnr0VfTIfOnkFRehlQQVRfdMQCKEPEAajaPobYwSgvP+D2dz3fLAR9NkSVGQBgtZEx18ecUNd/XkkAEt1wv3y+qSq5KdBcj8bYgE7frI2Eb98rlVA8nIXsqWgCo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151287; 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=4nVPpdd7RlJ6yyh/U8t5CFVvbZKVX89jUDZO5sXsDfA=; b=jS3IS1yCvZBOeleDYa0i4BEXBEWex10UF/iRA4arZPkD6U9Kna6PyuZJeTz1jG5NfjyEVtI6AB3zkZ5Us0LWAG+dSw2WZ+6n/uuWfrJkuOLoI6gKMyoTMnoatQFxP/JXehxImT690EhBEq+PnbxQBNpSzuZqbigFnNW1JTrID5o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.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 [205.139.110.120]) by mx.zohomail.com with SMTPS id 1594151287507858.0502618857378; Tue, 7 Jul 2020 12:48:07 -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-448-P64K_xGFOCmkvL8EsJnwEg-1; Tue, 07 Jul 2020 15:47:13 -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 3CBA78015FB; Tue, 7 Jul 2020 19:47:07 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 172F560CC0; Tue, 7 Jul 2020 19:47:07 +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 D9BA572F75; Tue, 7 Jul 2020 19:47:06 +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 067Jkp2O031547 for ; Tue, 7 Jul 2020 15:46:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id A9BC9797F1; Tue, 7 Jul 2020 19:46:51 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 27BB6797F0 for ; Tue, 7 Jul 2020 19:46:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151286; 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=4nVPpdd7RlJ6yyh/U8t5CFVvbZKVX89jUDZO5sXsDfA=; b=jGEF09OO/3OrXlUxfLR8Ndrh4cn+jZRP+LcKDNm9j+bM/t6YWZudSe8TtLzASv4jJFMFo3 Iu5AZcHWElxLZ0gGR+s/DYnnD2zBtkGEZ5FkJNm27SLzgW7jFBwpk9RQyOolAPGBoeDsdY wb7g3Q8GtLtUvtN3GKdYvdSlFTeqLOk= X-MC-Unique: P64K_xGFOCmkvL8EsJnwEg-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 13/17] virfdstream: Allow sparse stream vol-download Date: Tue, 7 Jul 2020 21:46:31 +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.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" When handling sparse stream, a thread is executed. This thread runs a read() or write() loop (depending what API is called; in this case it's virStorageVolDownload() and this the thread run read() loop). The read() is handled in virFDStreamThreadDoRead() which is then data/hole section aware, meaning it uses virFileInData() to detect data and hole sections and sends TYPE_DATA or TYPE_HOLE virStream messages accordingly. However, virFileInData() does not work with block devices. Simply because block devices don't have data and hole sections. But we can use new virFileInDataDetectZeroes() which is block device friendly for that. Partially resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D1852528 Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- src/util/virfdstream.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index 50209a8bd4..44b4855549 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -398,6 +398,7 @@ struct _virFDStreamThreadData { size_t length; bool doRead; bool sparse; + bool isBlock; int fdin; char *fdinname; int fdout; @@ -421,6 +422,7 @@ virFDStreamThreadDataFree(virFDStreamThreadDataPtr data) static ssize_t virFDStreamThreadDoRead(virFDStreamDataPtr fdst, bool sparse, + bool isBlock, const int fdin, const int fdout, const char *fdinname, @@ -437,8 +439,13 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, ssize_t got; =20 if (sparse && *dataLen =3D=3D 0) { - if (virFileInData(fdin, &inData, §ionLen) < 0) - return -1; + if (isBlock) { + if (virFileInDataDetectZeroes(fdin, &inData, §ionLen) < 0) + return -1; + } else { + if (virFileInData(fdin, &inData, §ionLen) < 0) + return -1; + } =20 if (length && sectionLen > length - total) @@ -568,6 +575,7 @@ virFDStreamThread(void *opaque) virStreamPtr st =3D data->st; size_t length =3D data->length; bool sparse =3D data->sparse; + bool isBlock =3D data->isBlock; VIR_AUTOCLOSE fdin =3D data->fdin; char *fdinname =3D data->fdinname; VIR_AUTOCLOSE fdout =3D data->fdout; @@ -604,7 +612,7 @@ virFDStreamThread(void *opaque) } =20 if (doRead) - got =3D virFDStreamThreadDoRead(fdst, sparse, + got =3D virFDStreamThreadDoRead(fdst, sparse, isBlock, fdin, fdout, fdinname, fdoutname, length, total, @@ -1271,6 +1279,7 @@ virFDStreamOpenFileInternal(virStreamPtr st, threadData->st =3D virObjectRef(st); threadData->length =3D length; threadData->sparse =3D sparse; + threadData->isBlock =3D !!S_ISBLK(sb.st_mode); =20 if ((oflags & O_ACCMODE) =3D=3D O_RDONLY) { threadData->fdin =3D fd; --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 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=1594151246; cv=none; d=zohomail.com; s=zohoarc; b=CosqznBEw4s6jkuk0YLv9gHaAIUngkOFFmCLxSHAhycgGzF7JzOPCqJ4DykW7moLPlo8Palhmxa7gSHeiyXvG6tUmo+dlUT8vA1NCuLVVB6sU5v0gKG2rqDjk4hiqA2SwLBoCXfu2BDPgikPvBUyHiw/bFl5U0B8eaEEKOVTSeE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151246; 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=Uee6XayEdsGgEj2ZTEA57z0Qt2dko4kss9iNldYC1j8=; b=VH0FjIZGt3K9Ui/jhHXn2ef8AveiYTyS9AkEcc7gjClV2siYExFJlQUM84//3aJpF1OQt+JMaRjsLybe8J54hoPRWKZ0l4HPnCloXrHL6TRjtAifZvU3fPM34C7iWnz+A1BNFkE213GSctxDMSPYr099eKxpLUPHFfgPw8IEgqE= 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 1594151246325587.241604105107; Tue, 7 Jul 2020 12:47:26 -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-417-Uy95LJO0May433QpY65IDQ-1; Tue, 07 Jul 2020 15:47:23 -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 93A4AEC1A2; Tue, 7 Jul 2020 19:47:17 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7326260CD0; Tue, 7 Jul 2020 19:47:17 +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 4555A93F7F; Tue, 7 Jul 2020 19:47:17 +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 067Jkq1o031556 for ; Tue, 7 Jul 2020 15:46:52 -0400 Received: by smtp.corp.redhat.com (Postfix) id 87F63797F0; Tue, 7 Jul 2020 19:46:52 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 074C4797F3 for ; Tue, 7 Jul 2020 19:46:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151245; 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=Uee6XayEdsGgEj2ZTEA57z0Qt2dko4kss9iNldYC1j8=; b=HvnOHFCi3If+tN3DrNghVDzLfGzpQO1e596bpjcmwfiDLlLc/Kh0FKDzNJbkavKCeSC1Qz WR+cTEqsNSO8zAvI6Wo++Hv+yhKmU9xyzUHnnIQs1cbh0pkS3d0cu7W0dna7wehADGgBoh 5Xnv8s+nE1e94DDzbBfufmExE2NfHik= X-MC-Unique: Uy95LJO0May433QpY65IDQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 14/17] virshStreamInData: Handle block devices Date: Tue, 7 Jul 2020 21:46:32 +0200 Message-Id: <2733dd941d5a7b831a456b36343e8ed70eae223e.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 very similar to previous commit. The virshStreamInData() callback is used by virStreamSparseSendAll() to detect whether the file the data is read from is in data or hole section. The SendAll() will then send corresponding type of virStream message to make server create a hole or write actual data. But the callback uses virFileInData() even for block devices, which results in an error. Switch to virFileInDataDetectZeroes() for block devices. Partially resolves: https://bugzilla.redhat.com/show_bug.cgi?id=3D1852528 Signed-off-by: Michal Privoznik --- tools/virsh-util.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/virsh-util.c b/tools/virsh-util.c index 884261eb49..867f69577f 100644 --- a/tools/virsh-util.c +++ b/tools/virsh-util.c @@ -230,12 +230,20 @@ virshStreamInData(virStreamPtr st G_GNUC_UNUSED, virshStreamCallbackDataPtr cbData =3D opaque; vshControl *ctl =3D cbData->ctl; int fd =3D cbData->fd; - int ret; =20 - if ((ret =3D virFileInData(fd, inData, offset)) < 0) - vshError(ctl, "%s", _("Unable to get current position in stream")); + if (cbData->isBlock) { + if (virFileInDataDetectZeroes(fd, inData, offset) < 0) { + vshError(ctl, "%s", _("Unable to get current position in strea= m")); + return -1; + } + } else { + if (virFileInData(fd, inData, offset) < 0) { + vshError(ctl, "%s", _("Unable to get current position in strea= m")); + return -1; + } + } =20 - return ret; + return 0; } =20 =20 --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 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 From nobody Thu May 2 21:45:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.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 205.139.110.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=1594151251; cv=none; d=zohomail.com; s=zohoarc; b=E3GkQj45qUI9hEFjFW1XcSWxuXKUPjlRSpZj94rhWm3EkT69+4LouulOYhXrbG6jmiBs/3nYPytbCq7kjFi8D3NfJnv4CzskhE8pv4zhvIDbQ04mvNqySBsbs0mIYxFoh8k9lkjm3GG2dM/bQ5FkO3G7NziKgxPFGjmAX9qFUW4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151251; 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=Ewi1vgUorpKA7KphdetQvZI5MtispL1Cthp0fGK0BJE=; b=YSPeasxSLU/VdNbsdCN+g5sleOu7CT+rKFmjPYVEitDqx3H7H4P0bkfujEdp5xdnGKTBbd8OU3Ca7Rdi4shZBkEeAXzxcfRM2ZvkRiRf3h9J9hicnSrL7cOBBwiALAryqoaygFwMLRCdN4rUhlHK+J7pCb0AIqId/Y9d7VDXduE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.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 [205.139.110.120]) by mx.zohomail.com with SMTPS id 1594151251967383.5825631522198; Tue, 7 Jul 2020 12:47:31 -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-154-VYQBxn9ePa-pkX61CBzYBQ-1; Tue, 07 Jul 2020 15:47:28 -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 271CE18A822F; Tue, 7 Jul 2020 19:47:22 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 028E560E1C; Tue, 7 Jul 2020 19:47:22 +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 C42A893F84; Tue, 7 Jul 2020 19:47:21 +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 067Jks6Y031577 for ; Tue, 7 Jul 2020 15:46:54 -0400 Received: by smtp.corp.redhat.com (Postfix) id 490E3797F0; Tue, 7 Jul 2020 19:46:54 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id BCC40797EF for ; Tue, 7 Jul 2020 19:46:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151250; 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=Ewi1vgUorpKA7KphdetQvZI5MtispL1Cthp0fGK0BJE=; b=DEbZvQlvQj+x5TTmKVxW7ABGKD1tIaAh0JPZxQYwSgBGNUvJU85gzacH9U5Fjv/zqG9/H7 Idu1jvk5U6bMZiKI1UWOarY5vbNfW16PFDobzPytlOSBQOUurMLXUxJJIv0/4btaL9MtLp IgdtzeOEdlTaJ0jUv2BXpm+euenLodM= X-MC-Unique: VYQBxn9ePa-pkX61CBzYBQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 16/17] stream: Don't read block device twice Date: Tue, 7 Jul 2020 21:46:34 +0200 Message-Id: <05ec2a612ad7f6a83b322fd81af491fae633b0db.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" The aim of virFileInDataDetectZeroes() is to mimic virFileInData() so that it can be used in sparse virStream with very little change to logic. This was implemented in previous commits. These two functions act alike - the passed FD is rewound back to the position it was in when either of the functions was called. For regular files (when virFileInData()) is used, this is not a problem - rewinding an FD is usually cheap and data is read() only from data section and it's read only once. But this is not the case for virFileInDataDetectZeroes(), which doesn't use cheap lseek() to learn data/hole sections, but uses read() and zero buffer detection. Worst case scenario, virFileInDataDetectZeroes() reads 1MiB of bytes, finds it is non-zero data, rewinds FD that 1MiB back only so that virFDStreamThreadDoRead() (in case of daemon) or virshStreamSource() (in case of client) can read the same data again. Possibly, this is not that big problem because kernel is likely to keep the data still in cache. But let's not rely on that and store the buffer for later use if we learned it contains actual data. Signed-off-by: Michal Privoznik --- src/util/virfdstream.c | 27 +++++++++++++--------- src/util/virfile.c | 51 ++++++++++++++---------------------------- src/util/virfile.h | 3 ++- tools/virsh-util.c | 40 +++++++++++++++++++++++++++++---- tools/virsh-util.h | 3 +++ tools/virsh-volume.c | 3 ++- 6 files changed, 76 insertions(+), 51 deletions(-) diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c index 40825a8811..9d617c71da 100644 --- a/src/util/virfdstream.c +++ b/src/util/virfdstream.c @@ -440,7 +440,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, =20 if (sparse && *dataLen =3D=3D 0) { if (isBlock) { - if (virFileInDataDetectZeroes(fdin, &inData, §ionLen) < 0) + if (virFileInDataDetectZeroes(fdin, &inData, §ionLen, &buf= ) < 0) return -1; } else { if (virFileInData(fdin, &inData, §ionLen) < 0) @@ -468,7 +468,7 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, =20 /* HACK: The message queue is one directional. So caller * cannot make us skip the hole. Do that for them instead. */ - if (sectionLen && + if (sectionLen && !isBlock && lseek(fdin, sectionLen, SEEK_CUR) =3D=3D (off_t) -1) { virReportSystemError(errno, _("unable to seek in %s"), @@ -476,17 +476,22 @@ virFDStreamThreadDoRead(virFDStreamDataPtr fdst, return -1; } } else { - if (sparse && - buflen > *dataLen) - buflen =3D *dataLen; + if (sparse && isBlock) { + /* Data already read by virFileInDataDetectZeroes() */ + got =3D sectionLen; + } else { + if (sparse && + buflen > *dataLen) + buflen =3D *dataLen; =20 - buf =3D g_new0(char, buflen); + buf =3D g_new0(char, buflen); =20 - if ((got =3D saferead(fdin, buf, buflen)) < 0) { - virReportSystemError(errno, - _("Unable to read %s"), - fdinname); - return -1; + if ((got =3D saferead(fdin, buf, buflen)) < 0) { + virReportSystemError(errno, + _("Unable to read %s"), + fdinname); + return -1; + } } =20 msg->type =3D VIR_FDSTREAM_MSG_TYPE_DATA; diff --git a/src/util/virfile.c b/src/util/virfile.c index a35d9ccb7a..e33a1c280c 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -4071,12 +4071,18 @@ virFileInData(int fd G_GNUC_UNUSED, * @fd: file to check * @inData: true if current position in the @fd is in data section * @length: amount of bytes until the end of the current section + * @buf: data read if in data section * - * This behaves exactly like virFileInData() except it doesn't use SEEK_DA= TA - * and SEEK_HOLE rather than a buffer into which it reads data from @fd and - * detects if the buffer is full of zeroes. Therefore, it is safe to use on - * special files (e.g. block devices). On the other hand it sees only - * DETECT_ZEORES_BLOCK_SIZE ahead. + * For given @fd it reads up to DETECT_ZEORES_BLOCK_SIZE bytes from it. + * If all bytes read are zero then @inData is set to 0, otherwise @inData + * is set to 1 and @buf is set to the read data. In either case, the + * number of bytes read is stored in @length. + * + * If @fd is at EOF, then this function sets @inData =3D 0 and @length =3D= 0. + * + * Note, in contrast to virFileInData(), this function is safe to use on + * special files (e.g. block devices), does not rewind @fd back to its + * starting position, and sees only DETECT_ZEORES_BLOCK_SIZE bytes ahead. * * Returns: 0 on success, * -1 otherwise. @@ -4084,50 +4090,27 @@ virFileInData(int fd G_GNUC_UNUSED, int virFileInDataDetectZeroes(int fd, int *inData, - long long *length) + long long *length, + char **buf) { const size_t buflen =3D DETECT_ZEORES_BLOCK_SIZE; g_autofree char *bytes =3D NULL; - off_t cur; ssize_t r; - int ret =3D -1; - - /* Get current position */ - cur =3D lseek(fd, 0, SEEK_CUR); - if (cur =3D=3D (off_t) -1) { - virReportSystemError(errno, "%s", - _("Unable to get current position in file")); - goto cleanup; - } =20 bytes =3D g_new0(char, buflen); =20 if ((r =3D saferead(fd, bytes, buflen)) < 0) { virReportSystemError(errno, "%s", _("Unable to read from file")); - goto cleanup; + return -1; } =20 *inData =3D !virStringIsNull(bytes, r); *length =3D r; - ret =3D 0; + if (*inData) + *buf =3D g_steal_pointer(&bytes); =20 - cleanup: - /* At any rate, reposition back to where we started. */ - if (cur !=3D (off_t) -1) { - int theerrno =3D errno; - - if (lseek(fd, cur, SEEK_SET) =3D=3D (off_t) -1) { - virReportSystemError(errno, "%s", - _("unable to restore position in file")); - ret =3D -1; - if (theerrno =3D=3D 0) - theerrno =3D errno; - } - - errno =3D theerrno; - } - return ret; + return 0; } =20 =20 diff --git a/src/util/virfile.h b/src/util/virfile.h index 9a5eade609..06ee0bd801 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -354,7 +354,8 @@ int virFileInData(int fd, =20 int virFileInDataDetectZeroes(int fd, int *inData, - long long *length); + long long *length, + char **buf); =20 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virFileWrapperFd, virFileWrapperFdFree); =20 diff --git a/tools/virsh-util.c b/tools/virsh-util.c index 867f69577f..dd2f4fb6a3 100644 --- a/tools/virsh-util.c +++ b/tools/virsh-util.c @@ -160,8 +160,25 @@ virshStreamSource(virStreamPtr st G_GNUC_UNUSED, { virshStreamCallbackDataPtr cbData =3D opaque; int fd =3D cbData->fd; + int r; =20 - return saferead(fd, bytes, nbytes); + if (cbData->isBlock) { + size_t bufavail =3D cbData->buflen - cbData->bufoff; + + r =3D MIN(nbytes, bufavail); + memcpy(bytes, cbData->buf + cbData->bufoff, r); + cbData->bufoff +=3D r; + + if (cbData->bufoff =3D=3D cbData->buflen) { + VIR_FREE(cbData->buf); + cbData->buflen =3D 0; + cbData->bufoff =3D 0; + } + } else { + r =3D saferead(fd, bytes, nbytes); + } + + return r; } =20 =20 @@ -172,9 +189,11 @@ virshStreamSourceSkip(virStreamPtr st G_GNUC_UNUSED, { virshStreamCallbackDataPtr cbData =3D opaque; int fd =3D cbData->fd; - off_t cur; =20 - if ((cur =3D lseek(fd, offset, SEEK_CUR)) =3D=3D (off_t) -1) + /* Don't seek if the @fd is a block device. It was left at + * the desired position by virshStreamInData(). */ + if (!cbData->isBlock && + (lseek(fd, offset, SEEK_CUR)) =3D=3D (off_t) -1) return -1; =20 return 0; @@ -232,10 +251,23 @@ virshStreamInData(virStreamPtr st G_GNUC_UNUSED, int fd =3D cbData->fd; =20 if (cbData->isBlock) { - if (virFileInDataDetectZeroes(fd, inData, offset) < 0) { + g_autofree char *buf =3D NULL; + + if (virFileInDataDetectZeroes(fd, inData, offset, &buf) < 0) { vshError(ctl, "%s", _("Unable to get current position in strea= m")); return -1; } + + if (*inData) { + cbData->buf =3D g_steal_pointer(&buf); + cbData->buflen =3D *offset; + cbData->bufoff =3D 0; + } + + /* Intentionally leaving @fd in a different position than on enter= ing + * because either we've found a hole and virshStreamSourceSkip() w= ould + * want to seek forward, or we've read some data and would need to + * imitate seek in virshStreamSource(). Skip seeking back and fort= h. */ } else { if (virFileInData(fd, inData, offset) < 0) { vshError(ctl, "%s", _("Unable to get current position in strea= m")); diff --git a/tools/virsh-util.h b/tools/virsh-util.h index 9ef28cfe0a..b2b5d6bcd2 100644 --- a/tools/virsh-util.h +++ b/tools/virsh-util.h @@ -73,6 +73,9 @@ struct _virshStreamCallbackData { vshControl *ctl; int fd; bool isBlock; + char *buf; + size_t buflen; + long long bufoff; }; =20 int diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c index 20823e2d10..9fd1674a9f 100644 --- a/tools/virsh-volume.c +++ b/tools/virsh-volume.c @@ -678,7 +678,7 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd) unsigned long long offset =3D 0, length =3D 0; virshControlPtr priv =3D ctl->privData; unsigned int flags =3D 0; - virshStreamCallbackData cbData; + virshStreamCallbackData cbData =3D { 0 }; struct stat sb; =20 if (vshCommandOptULongLong(ctl, cmd, "offset", &offset) < 0) @@ -752,6 +752,7 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd) virStorageVolFree(vol); if (st) virStreamFree(st); + VIR_FREE(cbData.buf); VIR_FORCE_CLOSE(fd); return ret; } --=20 2.26.2 From nobody Thu May 2 21:45:42 2024 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=1594151344; cv=none; d=zohomail.com; s=zohoarc; b=iNTy0v96t1ZTVe+AYcfZX0w/zhKcKhmYy1hzBCWiGhsvnph5yeAkNCc4A69WKbbivdIpiFRpJCxPC+oo4KnBZD72aAw4ozr2Bjgt12R8xzDHSnTMFLfjO2KrLW25Zr0zBX9kg9eAhtc66mbZwGJ50EILYSCqOhZ6hoqC3392EPQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594151344; 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=RzbNiblkWJQoTesrDlZvfgzsVCT9R56QEQHYV4jyFME=; b=goOMqj29ShpsO1QCHN5rTw3ut9C7LHoRv2/2XhDxwLJMj75wxVNygHNGZrtPF9h/qUaYYorqYW7zPM4wCkR9xvIyWDOJ4lTz/IEpJQgdscXtz1hywVexHM1B3dY6nfed5xVwZdEOHXx8e6Kh0BKPh1Y2TBcAuSS2WrraWe8HapM= 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-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1594151344141753.4422236393955; Tue, 7 Jul 2020 12:49:04 -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-506-ZL5q3CWwPX2MBjFs25s4EA-1; Tue, 07 Jul 2020 15:47:31 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 54C0E100CCD5; Tue, 7 Jul 2020 19:47:24 +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 382A47FEA6; Tue, 7 Jul 2020 19:47:24 +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 09C1F180CB27; Tue, 7 Jul 2020 19:47:24 +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 067Jkteh031593 for ; Tue, 7 Jul 2020 15:46:55 -0400 Received: by smtp.corp.redhat.com (Postfix) id 28E26797F0; Tue, 7 Jul 2020 19:46:55 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9C73D797EF for ; Tue, 7 Jul 2020 19:46:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594151342; 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=RzbNiblkWJQoTesrDlZvfgzsVCT9R56QEQHYV4jyFME=; b=Je5rKtZHHKs8YgtIhe7HMHa3fpPNlhIwGODuaxeMZZEw8iADGVWoEXGDnpjPNnpZjwNySj q9EAn18GFB3gY95hHnxjbNXXhdL1/TnBljSKIKcy9c8Ws6JXGU3o4RXtKY3n2TQvqVwkRD VFJUNHxNTZcCzCklNrpHcR7S7ifIidk= X-MC-Unique: ZL5q3CWwPX2MBjFs25s4EA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2 17/17] news: Document sparse streams for block devcies Date: Tue, 7 Jul 2020 21:46:35 +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.79 on 10.5.11.15 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" Signed-off-by: Michal Privoznik --- NEWS.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/NEWS.rst b/NEWS.rst index 232387ebdc..e225cbbcd3 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -15,6 +15,13 @@ v6.6.0 (unreleased) =20 * **Improvements** =20 + * Allow sparse streams for block devices + + Sparse streams (e.g. ``virsh vol-download --sparse`` or ``virsh vol-up= load + --sparse``) now handle if one of the stream ends is a block device. A = zero + block detection is performed so that zero block are not transferred and + thus bandwidth is saved. + * **Bug fixes** =20 =20 --=20 2.26.2