From nobody Sun Feb 8 20:34:23 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1618415904; cv=none; d=zohomail.com; s=zohoarc; b=FSkWqEgmp2QStJB/DlK97oARhgnSbT6D+XBHEfDGpZ3MQR/TyPhJV/TlgC4Beu2iyWZQOLhqmpJf9ovXGMiYCUrbX0ww1MmDQs8HtEBDaobr5laiieW1S2wyrOEcXyTaXPtQKBml3HKZ1xjGfxYuiuynIykQHIUy7n881XENfmw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618415904; 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=vFtLfLC7j0WjNTwXgWNY54K5yrKdt9r7ZZL0hXU51BY=; b=fvQ+P0f0xFS2j0hzzbnfJnFqivHrBiSDhd4PFB5FdgXauag19XRp3LOM7+BWwIFp+p8xmRtlnfu+d2OeDoW0KbEbszM5YZcUtxxXIJMMLAgov2+2i5AQDrTTswXcBGnToUav/uK6R4PqY+hhL05EWcH05ZRT1HqodZanGDwZLCs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1618415904361694.0651150865286; Wed, 14 Apr 2021 08:58:24 -0700 (PDT) Received: from localhost ([::1]:38660 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWhuJ-0004Ra-4u for importer@patchew.org; Wed, 14 Apr 2021 11:58:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34170) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWho2-0007mx-2w for qemu-devel@nongnu.org; Wed, 14 Apr 2021 11:51:58 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:22259) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWhnw-0000Jf-Se for qemu-devel@nongnu.org; Wed, 14 Apr 2021 11:51:53 -0400 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-259-qyYzy-QAPOey7mDeteBuDA-1; Wed, 14 Apr 2021 11:51:45 -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 50273106BB44 for ; Wed, 14 Apr 2021 15:51:44 +0000 (UTC) Received: from dgilbert-t580.localhost (ovpn-115-158.ams2.redhat.com [10.36.115.158]) by smtp.corp.redhat.com (Postfix) with ESMTP id 471A413905; Wed, 14 Apr 2021 15:51:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618415507; h=from:from: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; bh=vFtLfLC7j0WjNTwXgWNY54K5yrKdt9r7ZZL0hXU51BY=; b=bVkWzACXqeNx0PCOKm1FkbN0p4pL176GAcuQbWQ0UsN+Oscj6yEPbTR37g0Q5i5hUr1RA4 3FWjDI2AyVvTd79AmWRrRZIF+CTxaFKBxM+CVsPJIp7YyX0U4O1I7cSyaiMyuUJJFrdGAk fcFduzB2qigBq0ILzwk+RwFXbNFSfQ4= X-MC-Unique: qyYzy-QAPOey7mDeteBuDA-1 From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, stefanha@redhat.com, vgoyal@redhat.com, virtio-fs@redhat.com Subject: [PATCH v2 02/25] virtiofsd: Don't assume header layout Date: Wed, 14 Apr 2021 16:51:14 +0100 Message-Id: <20210414155137.46522-3-dgilbert@redhat.com> In-Reply-To: <20210414155137.46522-1-dgilbert@redhat.com> References: <20210414155137.46522-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dgilbert@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=dgilbert@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" virtiofsd incorrectly assumed a fixed set of header layout in the virt queue; assuming that the fuse and write headers were conveniently separated from the data; the spec doesn't allow us to take that convenience, so fix it up to deal with it the hard way. Signed-off-by: Dr. David Alan Gilbert --- tools/virtiofsd/fuse_virtio.c | 94 +++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c index 3e13997406..6dd73c9b72 100644 --- a/tools/virtiofsd/fuse_virtio.c +++ b/tools/virtiofsd/fuse_virtio.c @@ -129,18 +129,55 @@ static void fv_panic(VuDev *dev, const char *err) * Copy from an iovec into a fuse_buf (memory only) * Caller must ensure there is space */ -static void copy_from_iov(struct fuse_buf *buf, size_t out_num, - const struct iovec *out_sg) +static size_t copy_from_iov(struct fuse_buf *buf, size_t out_num, + const struct iovec *out_sg, + size_t max) { void *dest =3D buf->mem; + size_t copied =3D 0; =20 - while (out_num) { + while (out_num && max) { size_t onelen =3D out_sg->iov_len; + onelen =3D MIN(onelen, max); memcpy(dest, out_sg->iov_base, onelen); dest +=3D onelen; + copied +=3D onelen; out_sg++; out_num--; + max -=3D onelen; } + + return copied; +} + +/* + * Skip 'skip' bytes in the iov; 'sg_1stindex' is set as + * the index for the 1st iovec to read data from, and + * 'sg_1stskip' is the number of bytes to skip in that entry. + * + * Returns True if there are at least 'skip' bytes in the iovec + * + */ +static bool skip_iov(const struct iovec *sg, size_t sg_size, + size_t skip, + size_t *sg_1stindex, size_t *sg_1stskip) +{ + size_t vec; + + for (vec =3D 0; vec < sg_size; vec++) { + if (sg[vec].iov_len > skip) { + *sg_1stskip =3D skip; + *sg_1stindex =3D vec; + + return true; + } + + skip -=3D sg[vec].iov_len; + } + + *sg_1stindex =3D vec; + *sg_1stskip =3D 0; + return skip =3D=3D 0; } =20 /* @@ -457,6 +494,7 @@ static void fv_queue_worker(gpointer data, gpointer use= r_data) bool allocated_bufv =3D false; struct fuse_bufvec bufv; struct fuse_bufvec *pbufv; + struct fuse_in_header inh; =20 assert(se->bufsize > sizeof(struct fuse_in_header)); =20 @@ -505,14 +543,15 @@ static void fv_queue_worker(gpointer data, gpointer u= ser_data) elem->index); assert(0); /* TODO */ } - /* Copy just the first element and look at it */ - copy_from_iov(&fbuf, 1, out_sg); + /* Copy just the fuse_in_header and look at it */ + copy_from_iov(&fbuf, out_num, out_sg, + sizeof(struct fuse_in_header)); + memcpy(&inh, fbuf.mem, sizeof(struct fuse_in_header)); =20 pbufv =3D NULL; /* Compiler thinks an unitialised path */ - if (out_num > 2 && - out_sg[0].iov_len =3D=3D sizeof(struct fuse_in_header) && - ((struct fuse_in_header *)fbuf.mem)->opcode =3D=3D FUSE_WRITE && - out_sg[1].iov_len =3D=3D sizeof(struct fuse_write_in)) { + if (inh.opcode =3D=3D FUSE_WRITE && + out_len >=3D (sizeof(struct fuse_in_header) + + sizeof(struct fuse_write_in))) { /* * For a write we don't actually need to copy the * data, we can just do it straight out of guest memory @@ -521,15 +560,15 @@ static void fv_queue_worker(gpointer data, gpointer u= ser_data) */ fuse_log(FUSE_LOG_DEBUG, "%s: Write special case\n", __func__); =20 - /* copy the fuse_write_in header afte rthe fuse_in_header */ - fbuf.mem +=3D out_sg->iov_len; - copy_from_iov(&fbuf, 1, out_sg + 1); - fbuf.mem -=3D out_sg->iov_len; - fbuf.size =3D out_sg[0].iov_len + out_sg[1].iov_len; + fbuf.size =3D copy_from_iov(&fbuf, out_num, out_sg, + sizeof(struct fuse_in_header) + + sizeof(struct fuse_write_in)); + /* That copy reread the in_header, make sure we use the original */ + memcpy(fbuf.mem, &inh, sizeof(struct fuse_in_header)); =20 /* Allocate the bufv, with space for the rest of the iov */ pbufv =3D malloc(sizeof(struct fuse_bufvec) + - sizeof(struct fuse_buf) * (out_num - 2)); + sizeof(struct fuse_buf) * out_num); if (!pbufv) { fuse_log(FUSE_LOG_ERR, "%s: pbufv malloc failed\n", __func__); @@ -540,24 +579,37 @@ static void fv_queue_worker(gpointer data, gpointer u= ser_data) pbufv->count =3D 1; pbufv->buf[0] =3D fbuf; =20 - size_t iovindex, pbufvindex; - iovindex =3D 2; /* 2 headers, separate iovs */ + size_t iovindex, pbufvindex, iov_bytes_skip; pbufvindex =3D 1; /* 2 headers, 1 fusebuf */ =20 + if (!skip_iov(out_sg, out_num, + sizeof(struct fuse_in_header) + + sizeof(struct fuse_write_in), + &iovindex, &iov_bytes_skip)) { + fuse_log(FUSE_LOG_ERR, "%s: skip failed\n", + __func__); + goto out; + } + for (; iovindex < out_num; iovindex++, pbufvindex++) { pbufv->count++; pbufv->buf[pbufvindex].pos =3D ~0; /* Dummy */ pbufv->buf[pbufvindex].flags =3D 0; pbufv->buf[pbufvindex].mem =3D out_sg[iovindex].iov_base; pbufv->buf[pbufvindex].size =3D out_sg[iovindex].iov_len; + + if (iov_bytes_skip) { + pbufv->buf[pbufvindex].mem +=3D iov_bytes_skip; + pbufv->buf[pbufvindex].size -=3D iov_bytes_skip; + iov_bytes_skip =3D 0; + } } } else { /* Normal (non fast write) path */ =20 - /* Copy the rest of the buffer */ - fbuf.mem +=3D out_sg->iov_len; - copy_from_iov(&fbuf, out_num - 1, out_sg + 1); - fbuf.mem -=3D out_sg->iov_len; + copy_from_iov(&fbuf, out_num, out_sg, se->bufsize); + /* That copy reread the in_header, make sure we use the original */ + memcpy(fbuf.mem, &inh, sizeof(struct fuse_in_header)); fbuf.size =3D out_len; =20 /* TODO! Endianness of header */ --=20 2.31.1