From nobody Mon Feb 9 14:50:47 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1623254837; cv=none; d=zohomail.com; s=zohoarc; b=Jby0cT4Oe7db/xSabDV5MPOJ5ozJ+a23ofVU4isIswk94UmRNbuWten49ZjDucyXFJVezqxUpiblyyPn71ib76fnGtgYDFa9IblJtmSnTUfkP6kJiDSuLRrOaJqCWFblEEA6KGq2VV62/6kFf/3m4b0BQT3x5hPBAhQ4mC2uxjA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623254837; h=Content-Type:Content-Transfer-Encoding:Cc: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=zBNV20/rfrCAEeRG/biHZJ+6Oc8sUFzI+/cPDrPFG+k=; b=NlY4N0eSi50vPuM6W6rBFXKFZJ+kPKI8OlnsINEwSSgB9HdkVVfngpX0Th3i/grkbsnAwAi3OlbWR/OfRl1xgBNDTcJeacB7AD6OsiEiAbcrEGE0TTsSnyHoVN1hC2/na/zMXtOkmjTYNKHW29skLwBIxcR4myt1EvDwL8iWNGw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail 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 1623254837557406.8180220006983; Wed, 9 Jun 2021 09:07:17 -0700 (PDT) Received: from localhost ([::1]:45664 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lr0gh-0006wz-NN for importer@patchew.org; Wed, 09 Jun 2021 12:04:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57964) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lr0Z9-00042G-Nm for qemu-devel@nongnu.org; Wed, 09 Jun 2021 11:56:28 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:59286) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lr0Z7-0006xe-N2 for qemu-devel@nongnu.org; Wed, 09 Jun 2021 11:56:27 -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-472-FEGHfs4wPhGR3W5Rv_WnmA-1; Wed, 09 Jun 2021 11:56:23 -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 048F8100CA8C for ; Wed, 9 Jun 2021 15:56:23 +0000 (UTC) Received: from localhost (ovpn-114-102.ams2.redhat.com [10.36.114.102]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 94E0D100238C; Wed, 9 Jun 2021 15:56:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623254185; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zBNV20/rfrCAEeRG/biHZJ+6Oc8sUFzI+/cPDrPFG+k=; b=D93U9Axyn0QZKwSQSvjpgVTMLbIxHLLUCqChCZiQymPVV3Ljp8tM1KrCKr+twKBNp8awJ1 RfF6l/9qDSacr4PnWp74g3j7oDnF6+reGdBmu1MYbu2Ozk4ivUqcllYZa209jB84/dmq42 28yWVGT6Fw2q591SUqambyX4ZaisX4k= X-MC-Unique: FEGHfs4wPhGR3W5Rv_WnmA-1 From: Max Reitz To: qemu-devel@nongnu.org, virtio-fs@redhat.com Subject: [PATCH v2 2/9] virtiofsd: Use lo_inode_open() instead of openat() Date: Wed, 9 Jun 2021 17:55:44 +0200 Message-Id: <20210609155551.44437-3-mreitz@redhat.com> In-Reply-To: <20210609155551.44437-1-mreitz@redhat.com> References: <20210609155551.44437-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mreitz@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=170.10.133.124; envelope-from=mreitz@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.199, 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: , Cc: "Dr . David Alan Gilbert" , Stefan Hajnoczi , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The xattr functions want a non-O_PATH FD, so they reopen the lo_inode.fd with the flags they need through /proc/self/fd. Similarly, lo_opendir() needs an O_RDONLY FD. Instead of the /proc/self/fd trick, it just uses openat(fd, "."), because the FD is guaranteed to be a directory, so this works. All cases have one problem in common, though: In the future, when we may have a file handle in the lo_inode instead of an FD, querying an lo_inode FD may incur an open_by_handle_at() call. It does not make sense to then reopen that FD with custom flags, those should have been passed to open_by_handle_at() instead. Use lo_inode_open() instead of openat(). As part of the file handle change, lo_inode_open() will be made to invoke openat() only if lo_inode.fd is valid. Otherwise, it will invoke open_by_handle_at() with the right flags from the start. Consequently, after this patch, lo_inode_open() is the only place to invoke openat() to reopen an existing FD with different flags. Signed-off-by: Max Reitz Reviewed-by: Connor Kuehl --- tools/virtiofsd/passthrough_ll.c | 43 ++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough= _ll.c index a4674aba80..436f771d2a 100644 --- a/tools/virtiofsd/passthrough_ll.c +++ b/tools/virtiofsd/passthrough_ll.c @@ -1645,18 +1645,26 @@ static void lo_opendir(fuse_req_t req, fuse_ino_t i= no, { int error =3D ENOMEM; struct lo_data *lo =3D lo_data(req); - struct lo_dirp *d; + struct lo_inode *inode; + struct lo_dirp *d =3D NULL; int fd; ssize_t fh; =20 + inode =3D lo_inode(req, ino); + if (!inode) { + error =3D EBADF; + goto out_err; + } + d =3D calloc(1, sizeof(struct lo_dirp)); if (d =3D=3D NULL) { goto out_err; } =20 - fd =3D openat(lo_fd(req, ino), ".", O_RDONLY); - if (fd =3D=3D -1) { - goto out_errno; + fd =3D lo_inode_open(lo, inode, O_RDONLY); + if (fd < 0) { + error =3D -fd; + goto out_err; } =20 d->dp =3D fdopendir(fd); @@ -1685,6 +1693,7 @@ static void lo_opendir(fuse_req_t req, fuse_ino_t ino, out_errno: error =3D errno; out_err: + lo_inode_put(lo, &inode); if (d) { if (d->dp) { closedir(d->dp); @@ -2827,7 +2836,6 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t in= o, const char *in_name, } } =20 - sprintf(procname, "%i", inode->fd); /* * It is not safe to open() non-regular/non-dir files in file server * unless O_PATH is used, so use that method for regular files/dir @@ -2835,12 +2843,14 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t = ino, const char *in_name, * Otherwise, call fchdir() to avoid open(). */ if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) { - fd =3D openat(lo->proc_self_fd, procname, O_RDONLY); + fd =3D lo_inode_open(lo, inode, O_RDONLY); if (fd < 0) { - goto out_err; + saverr =3D -fd; + goto out; } ret =3D fgetxattr(fd, name, value, size); } else { + sprintf(procname, "%i", inode->fd); /* fchdir should not fail here */ FCHDIR_NOFAIL(lo->proc_self_fd); ret =3D getxattr(procname, name, value, size); @@ -2906,14 +2916,15 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t= ino, size_t size) } } =20 - sprintf(procname, "%i", inode->fd); if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) { - fd =3D openat(lo->proc_self_fd, procname, O_RDONLY); + fd =3D lo_inode_open(lo, inode, O_RDONLY); if (fd < 0) { - goto out_err; + saverr =3D -fd; + goto out; } ret =3D flistxattr(fd, value, size); } else { + sprintf(procname, "%i", inode->fd); /* fchdir should not fail here */ FCHDIR_NOFAIL(lo->proc_self_fd); ret =3D listxattr(procname, value, size); @@ -3039,15 +3050,15 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t = ino, const char *in_name, fuse_log(FUSE_LOG_DEBUG, "lo_setxattr(ino=3D%" PRIu64 ", name=3D%s value=3D%s size=3D%zd)\n", ino, name, value, siz= e); =20 - sprintf(procname, "%i", inode->fd); if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) { - fd =3D openat(lo->proc_self_fd, procname, O_RDONLY); + fd =3D lo_inode_open(lo, inode, O_RDONLY); if (fd < 0) { - saverr =3D errno; + saverr =3D -fd; goto out; } ret =3D fsetxattr(fd, name, value, size, flags); } else { + sprintf(procname, "%i", inode->fd); /* fchdir should not fail here */ FCHDIR_NOFAIL(lo->proc_self_fd); ret =3D setxattr(procname, name, value, size, flags); @@ -3105,15 +3116,15 @@ static void lo_removexattr(fuse_req_t req, fuse_ino= _t ino, const char *in_name) fuse_log(FUSE_LOG_DEBUG, "lo_removexattr(ino=3D%" PRIu64 ", name=3D%s)= \n", ino, name); =20 - sprintf(procname, "%i", inode->fd); if (S_ISREG(inode->filetype) || S_ISDIR(inode->filetype)) { - fd =3D openat(lo->proc_self_fd, procname, O_RDONLY); + fd =3D lo_inode_open(lo, inode, O_RDONLY); if (fd < 0) { - saverr =3D errno; + saverr =3D -fd; goto out; } ret =3D fremovexattr(fd, name); } else { + sprintf(procname, "%i", inode->fd); /* fchdir should not fail here */ FCHDIR_NOFAIL(lo->proc_self_fd); ret =3D removexattr(procname, name); --=20 2.31.1