From nobody Sat Feb 7 09:36:38 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.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=1571917385; cv=none; d=zoho.com; s=zohoarc; b=euRUaHCMnmu6oPpe8H8XjYhLZToVw5BH7jLyvzQLZWCPiHarlsio9mYsGgo5hrIBkYx47Rb+yJjjdbVu/6AF03tv1Y0VLBbgoPEatVFxfLl5XEuOxe0o7ouyk6Bvdv8OUgvTn1GMx4PJp+tR/ZvfyiNk62hoetaY+8Q/uKiZnnw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571917385; 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=HhF+ye4KeQJrro/A75A8XEgBNVYbuOMQSCicQJWTnRA=; b=gJDiSpIIhvUyiro7jjxKxVRWULeCBxnmtve+D9gAbzp3uZjgfy7G5wJlEtTDRXGWrJWLtrVLFYV1Kq1lcpJr/ce85z0+aFE+bQQ0OF5JnEp/2q6DERxl8nK3K0VKRlibVYPL7uKp1pJLc7bu8quseZ2SOTjHwSAzdUvF7YWycc8= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.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 1571917385080420.7003801769612; Thu, 24 Oct 2019 04:43:05 -0700 (PDT) Received: from localhost ([::1]:40216 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNbWB-0008UK-2P for importer@patchew.org; Thu, 24 Oct 2019 07:43:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39233) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNbHN-0001vs-Nn for qemu-devel@nongnu.org; Thu, 24 Oct 2019 07:27:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNbHM-0008I3-4X for qemu-devel@nongnu.org; Thu, 24 Oct 2019 07:27:45 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:33380 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNbHL-0008Hu-Uw for qemu-devel@nongnu.org; Thu, 24 Oct 2019 07:27:44 -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-176-eF96tUi_O5iqZrrP75fdRw-1; Thu, 24 Oct 2019 07:27:40 -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 D25791800E00; Thu, 24 Oct 2019 11:27:38 +0000 (UTC) Received: from dgilbert-t580.localhost (ovpn-117-248.ams2.redhat.com [10.36.117.248]) by smtp.corp.redhat.com (Postfix) with ESMTP id 59D144524; Thu, 24 Oct 2019 11:27:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571916463; 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=HhF+ye4KeQJrro/A75A8XEgBNVYbuOMQSCicQJWTnRA=; b=MWkcTMQhnirqU2ST6NtsjyFPSY4bs/bwvG+/XxjvhIp5G7d5CpMtisdZlzyTggUzKf+l38 tASdvt7QZMspDbBlJuJkAKv/2tqeD5o3oR60gB84M/FxTULF1QwRjp8xanpNxuhbrgRwPL M+Zfr5GKMiW4dmP8L7xBe1LvSmF4n+Y= From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, renzhen@linux.alibaba.com, eguan@linux.alibaba.com, ganesh.mahalingam@intel.com, m.mizuma@jp.fujitsu.com, mszeredi@redhat.com, misono.tomohiro@jp.fujitsu.com, tao.peng@linux.alibaba.com, piaojun@huawei.com, stefanha@redhat.com, vgoyal@redhat.com, mst@redhat.com, berrange@redhat.com Subject: [PATCH 04/25] virtiofsd: passthrough_ll: add dirp_map to hide lo_dirp pointers Date: Thu, 24 Oct 2019 12:26:57 +0100 Message-Id: <20191024112718.34657-5-dgilbert@redhat.com> In-Reply-To: <20191024112718.34657-1-dgilbert@redhat.com> References: <20191024112718.34657-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: eF96tUi_O5iqZrrP75fdRw-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 205.139.110.61 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: Stefan Hajnoczi Do not expose lo_dirp pointers to clients. Signed-off-by: Stefan Hajnoczi --- contrib/virtiofsd/passthrough_ll.c | 100 +++++++++++++++++++++-------- 1 file changed, 75 insertions(+), 25 deletions(-) diff --git a/contrib/virtiofsd/passthrough_ll.c b/contrib/virtiofsd/passthr= ough_ll.c index f718c951f7..9f82166079 100644 --- a/contrib/virtiofsd/passthrough_ll.c +++ b/contrib/virtiofsd/passthrough_ll.c @@ -56,22 +56,10 @@ =20 #define HAVE_POSIX_FALLOCATE 1 =20 -/* We are re-using pointers to our `struct lo_dirp` - elements as inodes. This means that we must be able to - store uintptr_t values in a fuse_ino_t variable. The following - incantation checks this condition at compile time. */ -#if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ =3D=3D 4 && __GNUC_MINO= R__ >=3D 6) && !defined __cplusplus -_Static_assert(sizeof(fuse_ino_t) >=3D sizeof(uintptr_t), - "fuse_ino_t too small to hold uintptr_t values!"); -#else -struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct \ - { unsigned _uintptr_to_must_hold_fuse_ino_t: - ((sizeof(fuse_ino_t) >=3D sizeof(uintptr_t)) ? 1 : -1); }; -#endif - struct lo_map_elem { union { struct lo_inode *inode; + struct lo_dirp *dirp; ssize_t freelist; }; bool in_use; @@ -118,6 +106,7 @@ struct lo_data { int timeout_set; struct lo_inode root; /* protected by lo->mutex */ struct lo_map ino_map; /* protected by lo->mutex */ + struct lo_map dirp_map; /* protected by lo->mutex */ }; =20 static const struct fuse_opt lo_opts[] =3D { @@ -252,6 +241,19 @@ static void lo_map_remove(struct lo_map *map, size_t k= ey) map->freelist =3D key; } =20 +/* Assumes lo->mutex is held */ +static ssize_t lo_add_dirp_mapping(fuse_req_t req, struct lo_dirp *dirp) +{ + struct lo_map_elem *elem; + + elem =3D lo_map_alloc_elem(&lo_data(req)->dirp_map); + if (!elem) + return -1; + + elem->dirp =3D dirp; + return elem - lo_data(req)->dirp_map.elems; +} + /* Assumes lo->mutex is held */ static ssize_t lo_add_inode_mapping(fuse_req_t req, struct lo_inode *inode) { @@ -820,16 +822,28 @@ struct lo_dirp { off_t offset; }; =20 -static struct lo_dirp *lo_dirp(struct fuse_file_info *fi) +static struct lo_dirp *lo_dirp(fuse_req_t req, struct fuse_file_info *fi) { - return (struct lo_dirp *) (uintptr_t) fi->fh; + struct lo_data *lo =3D lo_data(req); + struct lo_map_elem *elem; + + pthread_mutex_lock(&lo->mutex); + elem =3D lo_map_get(&lo->dirp_map, fi->fh); + pthread_mutex_unlock(&lo->mutex); + if (!elem) + return NULL; + + return elem->dirp; } =20 static void lo_opendir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_in= fo *fi) { int error =3D ENOMEM; struct lo_data *lo =3D lo_data(req); - struct lo_dirp *d =3D calloc(1, sizeof(struct lo_dirp)); + struct lo_dirp *d; + ssize_t fh; + + d =3D calloc(1, sizeof(struct lo_dirp)); if (d =3D=3D NULL) goto out_err; =20 @@ -844,7 +858,13 @@ static void lo_opendir(fuse_req_t req, fuse_ino_t ino,= struct fuse_file_info *fi d->offset =3D 0; d->entry =3D NULL; =20 - fi->fh =3D (uintptr_t) d; + pthread_mutex_lock(&lo->mutex); + fh =3D lo_add_dirp_mapping(req, d); + pthread_mutex_unlock(&lo->mutex); + if (fh =3D=3D -1) + goto out_err; + + fi->fh =3D fh; if (lo->cache =3D=3D CACHE_ALWAYS) fi->keep_cache =3D 1; fuse_reply_open(req, fi); @@ -854,6 +874,8 @@ out_errno: error =3D errno; out_err: if (d) { + if (d->dp) + closedir(d->dp); if (d->fd !=3D -1) close(d->fd); free(d); @@ -870,19 +892,21 @@ static int is_dot_or_dotdot(const char *name) static void lo_do_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t offset, struct fuse_file_info *fi, int plus) { - struct lo_dirp *d =3D lo_dirp(fi); - char *buf; + struct lo_dirp *d; + char *buf =3D NULL; char *p; size_t rem =3D size; - int err; + int err =3D ENOMEM; =20 (void) ino; =20 + d =3D lo_dirp(req, fi); + if (!d) + goto error; + buf =3D calloc(1, size); - if (!buf) { - err =3D ENOMEM; + if (!buf) goto error; - } p =3D buf; =20 if (offset !=3D d->offset) { @@ -974,8 +998,21 @@ static void lo_readdirplus(fuse_req_t req, fuse_ino_t = ino, size_t size, =20 static void lo_releasedir(fuse_req_t req, fuse_ino_t ino, struct fuse_file= _info *fi) { - struct lo_dirp *d =3D lo_dirp(fi); + struct lo_data *lo =3D lo_data(req); + struct lo_dirp *d; + (void) ino; + + d =3D lo_dirp(req, fi); + if (!d) { + fuse_reply_err(req, EBADF); + return; + } + + pthread_mutex_lock(&lo->mutex); + lo_map_remove(&lo->dirp_map, fi->fh); + pthread_mutex_unlock(&lo->mutex); + closedir(d->dp); free(d); fuse_reply_err(req, 0); @@ -1023,8 +1060,18 @@ static void lo_fsyncdir(fuse_req_t req, fuse_ino_t i= no, int datasync, struct fuse_file_info *fi) { int res; - int fd =3D dirfd(lo_dirp(fi)->dp); + struct lo_dirp *d; + int fd; + (void) ino; + + d =3D lo_dirp(req, fi); + if (!d) { + fuse_reply_err(req, EBADF); + return; + } + + fd =3D dirfd(d->dp); if (datasync) res =3D fdatasync(fd); else @@ -1504,6 +1551,8 @@ int main(int argc, char *argv[]) root_elem =3D lo_map_reserve(&lo.ino_map, lo.root.fuse_ino); root_elem->inode =3D &lo.root; =20 + lo_map_init(&lo.dirp_map); + if (fuse_parse_cmdline(&args, &opts) !=3D 0) return 1; if (opts.show_help) { @@ -1597,6 +1646,7 @@ err_out2: err_out1: fuse_opt_free_args(&args); =20 + lo_map_destroy(&lo.dirp_map); lo_map_destroy(&lo.ino_map); =20 if (lo.root.fd >=3D 0) --=20 2.23.0