From nobody Mon Feb 9 09:28:45 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=1622825786; cv=none; d=zohomail.com; s=zohoarc; b=DA+DiOy6pvGiBI1wl23hr4iyU7QEVe7JB0OQA+yzczobfixyQ634nk+hUKm5PbQAdHTF2sAOsLfdgVrssB3itkfiRjFwki0EfSkh5Af9wTTcz8KF4OQ0dLFntmQe3e0N5zOA+ebRva5B4SeHYaV16K+LIt5QCc/FHFLwvhGpzGo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622825786; 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=gk7tAp+TLfJPXJUPjLXunxG51/JTCGoSqZU5H5wMKKc=; b=ejIbiJU80pQLqQlIVEvIjmUa61NtD+ispypJNnATJODqAfJFjZx46Se4X0WcJGKJdl9ptyvYNECy6ROEWI63G87uqeZoRDZ6nri3F2GFNa/pw1jgzeiMfwZMQ84SaiMAQRLbUiyBXmk2jxeLu1Kg2Yl+wbhqrTfwMJLQ9/7kzVw= 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 1622825786305303.7906011695741; Fri, 4 Jun 2021 09:56:26 -0700 (PDT) Received: from localhost ([::1]:51238 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lpD7R-0001l3-8O for importer@patchew.org; Fri, 04 Jun 2021 12:56:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52568) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lpCSp-0006Fh-QX for qemu-devel@nongnu.org; Fri, 04 Jun 2021 12:14:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:47407) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lpCSn-00044Q-Fh for qemu-devel@nongnu.org; Fri, 04 Jun 2021 12:14: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-410-6PY5j6rLNzSHHp6QQ4E3vQ-1; Fri, 04 Jun 2021 12:14:23 -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 A58FC801107 for ; Fri, 4 Jun 2021 16:14:22 +0000 (UTC) Received: from localhost (ovpn-114-199.ams2.redhat.com [10.36.114.199]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3AAE519C79; Fri, 4 Jun 2021 16:14:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622823264; 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=gk7tAp+TLfJPXJUPjLXunxG51/JTCGoSqZU5H5wMKKc=; b=OZqGRGDrsDdg+3v5TPhSUh2On18zRafJx+lza7OG8A1nFZinfhI3P3D4Sx63rYO5O9M2S0 Fi18XadRSsQ8qdra+fRrCf1Q8Wfd7RPx3Yx/7yMNHt4z0XWC697yY102KJOf0zD99ApP0n o3eZrfM1hSokYaUBCclhPRds6mtG4q0= X-MC-Unique: 6PY5j6rLNzSHHp6QQ4E3vQ-1 From: Max Reitz To: qemu-devel@nongnu.org, virtio-fs@redhat.com Subject: [PATCH 3/9] virtiofsd: Add lo_inode_fd() helper Date: Fri, 4 Jun 2021 18:13:31 +0200 Message-Id: <20210604161337.16048-4-mreitz@redhat.com> In-Reply-To: <20210604161337.16048-1-mreitz@redhat.com> References: <20210604161337.16048-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.373, 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" Once we let lo_inode.fd be optional, we will need its users to open the file handle stored in lo_inode instead. This function will do that. For now, it just returns lo_inode.fd, though. Signed-off-by: Max Reitz --- tools/virtiofsd/passthrough_ll.c | 138 ++++++++++++++++++++++++++----- 1 file changed, 117 insertions(+), 21 deletions(-) diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough= _ll.c index 436f771d2a..46c9dfe200 100644 --- a/tools/virtiofsd/passthrough_ll.c +++ b/tools/virtiofsd/passthrough_ll.c @@ -629,6 +629,16 @@ static struct lo_inode *lo_inode(fuse_req_t req, fuse_= ino_t ino) return elem->inode; } =20 +static int lo_inode_fd(const struct lo_inode *inode, TempFd *tfd) +{ + *tfd =3D (TempFd) { + .fd =3D inode->fd, + .owned =3D false, + }; + + return 0; +} + /* * TODO Remove this helper and force callers to hold an inode refcount unt= il * they are done with the fd. This will be done in a later patch to make @@ -790,11 +800,11 @@ static int lo_fi_fd(fuse_req_t req, struct fuse_file_= info *fi) static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, int valid, struct fuse_file_info *fi) { + g_auto(TempFd) inode_fd =3D TEMP_FD_INIT; int saverr; char procname[64]; struct lo_data *lo =3D lo_data(req); struct lo_inode *inode; - int ifd; int res; int fd =3D -1; =20 @@ -804,7 +814,11 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino,= struct stat *attr, return; } =20 - ifd =3D inode->fd; + res =3D lo_inode_fd(inode, &inode_fd); + if (res < 0) { + saverr =3D -res; + goto out_err; + } =20 /* If fi->fh is invalid we'll report EBADF later */ if (fi) { @@ -815,7 +829,7 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, = struct stat *attr, if (fi) { res =3D fchmod(fd, attr->st_mode); } else { - sprintf(procname, "%i", ifd); + sprintf(procname, "%i", inode_fd.fd); res =3D fchmodat(lo->proc_self_fd, procname, attr->st_mode, 0); } if (res =3D=3D -1) { @@ -827,12 +841,13 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino= , struct stat *attr, uid_t uid =3D (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t)= -1; gid_t gid =3D (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t)= -1; =20 - saverr =3D drop_security_capability(lo, ifd); + saverr =3D drop_security_capability(lo, inode_fd.fd); if (saverr) { goto out_err; } =20 - res =3D fchownat(ifd, "", uid, gid, AT_EMPTY_PATH | AT_SYMLINK_NOF= OLLOW); + res =3D fchownat(inode_fd.fd, "", uid, gid, + AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW); if (res =3D=3D -1) { saverr =3D errno; goto out_err; @@ -911,7 +926,7 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, = struct stat *attr, if (fi) { res =3D futimens(fd, tv); } else { - sprintf(procname, "%i", inode->fd); + sprintf(procname, "%i", inode_fd.fd); res =3D utimensat(lo->proc_self_fd, procname, tv, 0); } if (res =3D=3D -1) { @@ -1026,7 +1041,8 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t pa= rent, const char *name, struct fuse_entry_param *e, struct lo_inode **inodep) { - int newfd; + g_auto(TempFd) dir_fd =3D TEMP_FD_INIT; + int newfd =3D -1; int res; int saverr; uint64_t mnt_id; @@ -1056,7 +1072,13 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t p= arent, const char *name, name =3D "."; } =20 - newfd =3D openat(dir->fd, name, O_PATH | O_NOFOLLOW); + res =3D lo_inode_fd(dir, &dir_fd); + if (res < 0) { + saverr =3D -res; + goto out; + } + + newfd =3D openat(dir_fd.fd, name, O_PATH | O_NOFOLLOW); if (newfd =3D=3D -1) { goto out_err; } @@ -1123,6 +1145,7 @@ static int lo_do_lookup(fuse_req_t req, fuse_ino_t pa= rent, const char *name, =20 out_err: saverr =3D errno; +out: if (newfd !=3D -1) { close(newfd); } @@ -1228,6 +1251,7 @@ static void lo_mknod_symlink(fuse_req_t req, fuse_ino= _t parent, const char *name, mode_t mode, dev_t rdev, const char *link) { + g_auto(TempFd) dir_fd =3D TEMP_FD_INIT; int res; int saverr; struct lo_data *lo =3D lo_data(req); @@ -1251,12 +1275,18 @@ static void lo_mknod_symlink(fuse_req_t req, fuse_i= no_t parent, return; } =20 + res =3D lo_inode_fd(dir, &dir_fd); + if (res < 0) { + saverr =3D -res; + goto out; + } + saverr =3D lo_change_cred(req, &old); if (saverr) { goto out; } =20 - res =3D mknod_wrapper(dir->fd, name, link, mode, rdev); + res =3D mknod_wrapper(dir_fd.fd, name, link, mode, rdev); =20 saverr =3D errno; =20 @@ -1304,6 +1334,8 @@ static void lo_symlink(fuse_req_t req, const char *li= nk, fuse_ino_t parent, static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent, const char *name) { + g_auto(TempFd) inode_fd =3D TEMP_FD_INIT; + g_auto(TempFd) parent_fd =3D TEMP_FD_INIT; int res; struct lo_data *lo =3D lo_data(req); struct lo_inode *parent_inode; @@ -1329,18 +1361,31 @@ static void lo_link(fuse_req_t req, fuse_ino_t ino,= fuse_ino_t parent, goto out_err; } =20 + res =3D lo_inode_fd(inode, &inode_fd); + if (res < 0) { + errno =3D -res; + goto out_err; + } + + res =3D lo_inode_fd(parent_inode, &parent_fd); + if (res < 0) { + errno =3D -res; + goto out_err; + } + memset(&e, 0, sizeof(struct fuse_entry_param)); e.attr_timeout =3D lo->timeout; e.entry_timeout =3D lo->timeout; =20 - sprintf(procname, "%i", inode->fd); - res =3D linkat(lo->proc_self_fd, procname, parent_inode->fd, name, + sprintf(procname, "%i", inode_fd.fd); + res =3D linkat(lo->proc_self_fd, procname, parent_fd.fd, name, AT_SYMLINK_FOLLOW); if (res =3D=3D -1) { goto out_err; } =20 - res =3D fstatat(inode->fd, "", &e.attr, AT_EMPTY_PATH | AT_SYMLINK_NOF= OLLOW); + res =3D fstatat(inode_fd.fd, "", &e.attr, + AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW); if (res =3D=3D -1) { goto out_err; } @@ -1369,6 +1414,7 @@ out_err: static struct lo_inode *lookup_name(fuse_req_t req, fuse_ino_t parent, const char *name) { + g_auto(TempFd) dir_fd =3D TEMP_FD_INIT; int res; uint64_t mnt_id; struct stat attr; @@ -1379,7 +1425,12 @@ static struct lo_inode *lookup_name(fuse_req_t req, = fuse_ino_t parent, return NULL; } =20 - res =3D do_statx(lo, dir->fd, name, &attr, AT_SYMLINK_NOFOLLOW, &mnt_i= d); + res =3D lo_inode_fd(dir, &dir_fd); + if (res < 0) { + return NULL; + } + + res =3D do_statx(lo, dir_fd.fd, name, &attr, AT_SYMLINK_NOFOLLOW, &mnt= _id); lo_inode_put(lo, &dir); if (res =3D=3D -1) { return NULL; @@ -1421,6 +1472,8 @@ static void lo_rename(fuse_req_t req, fuse_ino_t pare= nt, const char *name, fuse_ino_t newparent, const char *newname, unsigned int flags) { + g_auto(TempFd) parent_fd =3D TEMP_FD_INIT; + g_auto(TempFd) newparent_fd =3D TEMP_FD_INIT; int res; struct lo_inode *parent_inode; struct lo_inode *newparent_inode; @@ -1453,12 +1506,24 @@ static void lo_rename(fuse_req_t req, fuse_ino_t pa= rent, const char *name, goto out; } =20 + res =3D lo_inode_fd(parent_inode, &parent_fd); + if (res < 0) { + fuse_reply_err(req, -res); + goto out; + } + + res =3D lo_inode_fd(newparent_inode, &newparent_fd); + if (res < 0) { + fuse_reply_err(req, -res); + goto out; + } + if (flags) { #ifndef SYS_renameat2 fuse_reply_err(req, EINVAL); #else - res =3D syscall(SYS_renameat2, parent_inode->fd, name, - newparent_inode->fd, newname, flags); + res =3D syscall(SYS_renameat2, parent_fd.fd, name, + newparent_fd.fd, newname, flags); if (res =3D=3D -1 && errno =3D=3D ENOSYS) { fuse_reply_err(req, EINVAL); } else { @@ -1468,7 +1533,7 @@ static void lo_rename(fuse_req_t req, fuse_ino_t pare= nt, const char *name, goto out; } =20 - res =3D renameat(parent_inode->fd, name, newparent_inode->fd, newname); + res =3D renameat(parent_fd.fd, name, newparent_fd.fd, newname); =20 fuse_reply_err(req, res =3D=3D -1 ? errno : 0); out: @@ -1953,6 +2018,7 @@ static int lo_do_open(struct lo_data *lo, struct lo_i= node *inode, static void lo_create(fuse_req_t req, fuse_ino_t parent, const char *name, mode_t mode, struct fuse_file_info *fi) { + g_auto(TempFd) parent_fd =3D TEMP_FD_INIT; int fd =3D -1; struct lo_data *lo =3D lo_data(req); struct lo_inode *parent_inode; @@ -1975,6 +2041,12 @@ static void lo_create(fuse_req_t req, fuse_ino_t par= ent, const char *name, return; } =20 + err =3D lo_inode_fd(parent_inode, &parent_fd); + if (err < 0) { + err =3D -err; + goto out; + } + err =3D lo_change_cred(req, &old); if (err) { goto out; @@ -1983,7 +2055,7 @@ static void lo_create(fuse_req_t req, fuse_ino_t pare= nt, const char *name, update_open_flags(lo->writeback, lo->allow_direct_io, fi); =20 /* Try to create a new file but don't open existing files */ - fd =3D openat(parent_inode->fd, name, fi->flags | O_CREAT | O_EXCL, mo= de); + fd =3D openat(parent_fd.fd, name, fi->flags | O_CREAT | O_EXCL, mode); err =3D fd =3D=3D -1 ? errno : 0; =20 lo_restore_cred(&old); @@ -2788,6 +2860,7 @@ static int xattr_map_server(const struct lo_data *lo,= const char *server_name, static void lo_getxattr(fuse_req_t req, fuse_ino_t ino, const char *in_nam= e, size_t size) { + g_auto(TempFd) inode_fd =3D TEMP_FD_INIT; struct lo_data *lo =3D lo_data(req); g_autofree char *value =3D NULL; char procname[64]; @@ -2850,7 +2923,12 @@ static void lo_getxattr(fuse_req_t req, fuse_ino_t i= no, const char *in_name, } ret =3D fgetxattr(fd, name, value, size); } else { - sprintf(procname, "%i", inode->fd); + ret =3D lo_inode_fd(inode, &inode_fd); + if (ret < 0) { + saverr =3D -ret; + goto out; + } + sprintf(procname, "%i", inode_fd.fd); /* fchdir should not fail here */ FCHDIR_NOFAIL(lo->proc_self_fd); ret =3D getxattr(procname, name, value, size); @@ -2887,6 +2965,7 @@ out: =20 static void lo_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) { + g_auto(TempFd) inode_fd =3D TEMP_FD_INIT; struct lo_data *lo =3D lo_data(req); g_autofree char *value =3D NULL; char procname[64]; @@ -2924,7 +3003,12 @@ static void lo_listxattr(fuse_req_t req, fuse_ino_t = ino, size_t size) } ret =3D flistxattr(fd, value, size); } else { - sprintf(procname, "%i", inode->fd); + ret =3D lo_inode_fd(inode, &inode_fd); + if (ret < 0) { + saverr =3D -ret; + goto out; + } + sprintf(procname, "%i", inode_fd.fd); /* fchdir should not fail here */ FCHDIR_NOFAIL(lo->proc_self_fd); ret =3D listxattr(procname, value, size); @@ -3013,6 +3097,7 @@ out: static void lo_setxattr(fuse_req_t req, fuse_ino_t ino, const char *in_nam= e, const char *value, size_t size, int flags) { + g_auto(TempFd) inode_fd =3D TEMP_FD_INIT; char procname[64]; const char *name; char *mapped_name; @@ -3058,7 +3143,12 @@ static void lo_setxattr(fuse_req_t req, fuse_ino_t i= no, const char *in_name, } ret =3D fsetxattr(fd, name, value, size, flags); } else { - sprintf(procname, "%i", inode->fd); + ret =3D lo_inode_fd(inode, &inode_fd); + if (ret < 0) { + saverr =3D -ret; + goto out; + } + sprintf(procname, "%i", inode_fd.fd); /* fchdir should not fail here */ FCHDIR_NOFAIL(lo->proc_self_fd); ret =3D setxattr(procname, name, value, size, flags); @@ -3079,6 +3169,7 @@ out: =20 static void lo_removexattr(fuse_req_t req, fuse_ino_t ino, const char *in_= name) { + g_auto(TempFd) inode_fd =3D TEMP_FD_INIT; char procname[64]; const char *name; char *mapped_name; @@ -3124,7 +3215,12 @@ static void lo_removexattr(fuse_req_t req, fuse_ino_= t ino, const char *in_name) } ret =3D fremovexattr(fd, name); } else { - sprintf(procname, "%i", inode->fd); + ret =3D lo_inode_fd(inode, &inode_fd); + if (ret < 0) { + saverr =3D -ret; + goto out; + } + sprintf(procname, "%i", inode_fd.fd); /* fchdir should not fail here */ FCHDIR_NOFAIL(lo->proc_self_fd); ret =3D removexattr(procname, name); --=20 2.31.1