Received: from fout-b4-smtp.messagingengine.com (fout-b4-smtp.messagingengine.com [202.12.124.147]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 171AA86334 for ; Mon, 13 Jul 2026 06:08:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=202.12.124.147 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783922940; cv=none; b=IAGVnadODaTrGUhgQ5K80vlekSKC+vQCJLLusKR9ob0mL1gwbbNoQyie01gqDmXa9Fc6YGK3ka2YntLZJZVeqth9ddoET+jokZpZXWRsON5ulM6QT8/0Vi49LNqHtzIDSeOQua5HJEGiTbpT1gtr/si6k8osohjDkJGWF41CTTc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783922940; c=relaxed/simple; bh=J+rCs5ymYJcaQX1tMasTgb8f72PS+0Tz+OMg5FIdiOo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hYzYSwDwWPuwt3V/0OBbhJzFnJvIjmI3TRGppGk8Wi6D34QZAzSPynJj8oaWDV5/tpetdpjV+WPQUbzPh9tsOJq7ov74+68SNFOwAOhQCelk7t8T3ycx/avfj5Jxnxe53d6P1Kn5uibEzp99zHIk7PRizFhh1okfFJLomzgAg/8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ownmail.net; spf=pass smtp.mailfrom=ownmail.net; dkim=pass (2048-bit key) header.d=ownmail.net header.i=@ownmail.net header.b=V6j4Ak6M; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=C1u/dK5H; arc=none smtp.client-ip=202.12.124.147 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ownmail.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ownmail.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ownmail.net header.i=@ownmail.net header.b="V6j4Ak6M"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="C1u/dK5H" Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfout.stl.internal (Postfix) with ESMTP id 1AC011D00077; Mon, 13 Jul 2026 02:08:58 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Mon, 13 Jul 2026 02:08:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ownmail.net; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:subject:subject:to:to; s=fm2; t=1783922937; x=1784009337; bh=fu5NHV3BzTrJ2i43ee75MeMsL8QzEtroASsfgOGSLbk=; b= V6j4Ak6M9ggBvnHjOK1zDWmxMFl70TE/EHDX2yO1ghuNoF4SySSuP0j9u4A8BMfu xHV4mdRDMo+7VACsbrGargYa3xZz5yKqpMfLOE/GuDBWRQeXz4RcvHSSBPQHk/3I lmw6K+/lufSNYIOnynYDsm7Vjsduau+2zx9xT08zJr9ACed28iDWKpqdC+NBLW+1 4bor6CHCQIX1TZkyE8wwN+/3GWlQqD4YeTQ/NyIte4bSKF2WOUJl+fEtNhk1GJi5 JEjSH3wVOrfrxotg0cE5+b/4TIOK6RY9iG0XSLFcjyMwDfdg/vh4smStiBuSLPwU 4c1GgQK+tUUKYSVHSz3f/w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; t=1783922937; x=1784009337; bh=f u5NHV3BzTrJ2i43ee75MeMsL8QzE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mdqlHa3P+XafBG5DjY4LKWUQ X-ME-Proxy: Feedback-ID: i9d664b8f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 13 Jul 2026 02:08:56 -0400 (EDT) From: NeilBrown To: Alexander Viro , Christian Brauner Cc: Jan Kara , linux-fsdevel@vger.kernel.org, Jori Koolstra Subject: [PATCH v3 3/3] VFS: add vfs_lookup_open() for nfsd Date: Mon, 13 Jul 2026 16:05:07 +1000 Message-ID: <20260713060814.2849-4-neilb@ownmail.net> X-Mailer: git-send-email 2.50.0.107.gf914562f5916.dirty In-Reply-To: <20260713060814.2849-1-neilb@ownmail.net> References: <20260713060814.2849-1-neilb@ownmail.net> Reply-To: NeilBrown Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: NeilBrown vfs_lookup_open() is a limited version of lookup_open() which is exported for nfsd to use - to replace dentry_create(). It is limited in that no filename is given (thus no auditing) and no LOOKUP_ flags are passed. A few "intent" LOOKUP flags are deduced from the open flags. If a non-regular file is found, an O_PATH file is returned. This allows the caller to see what was found and to return an appropriate error code. However if ->atomic_open was used, it may return an error rather then provide a dentry. Signed-off-by: NeilBrown --- fs/namei.c | 89 +++++++++++++++++++++++++++++++++++++++++++ include/linux/namei.h | 3 ++ 2 files changed, 92 insertions(+) diff --git a/fs/namei.c b/fs/namei.c index cb33a01d16eb..2143f759443a 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -4562,6 +4562,95 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file, goto out; } +/** + * vfs_lookup_open - open and possibly create a regular file + * @parent: directory to contain file + * @last: final component of file name + * @open_flag: O_flags + * @mode: initial permissions for file + * + * Open a file after lookup and/or create. This provides similar + * functionality open_last_lookups() for non-VFS users, particularly + * nfsd. + * It uses ->atomic_open or ->lookup / ->create / ->open as appropriate. + * + * If the fs object found is not a regular file then an error is returned. + * In some cases, related errors are repurposed to so that the caller can + * determine the file of file found from the error. + * -EISDIR : a directory was found + * -ELOOP : a symlink was found + * -ENODEV : a block or character device special file was found + * -EFTYPE : any other non-regular file was found, such as FIFO or SOCK. + * + * Returns: the opened struct file, or an error. + */ +struct file *vfs_lookup_open(struct path *parent, struct qstr *last, + int open_flag, umode_t mode) +{ + struct file *file __free(fput) = NULL; + struct nameidata nd = {}; + struct open_flags op = {}; + struct dentry *dentry; + int error = 0; + + WARN_ONCE(mode & ~S_IALLUGO, "mode must only have permission bits"); + error = lookup_noperm_common(last, parent->dentry); + if (error) + return ERR_PTR(error); + + file = alloc_empty_file(open_flag, current_cred()); + if (IS_ERR(file)) + return file; + + nd.path = *parent; + nd.last = *last; + nd.flags = LOOKUP_OPEN; + if (open_flag & O_CREAT) { + nd.flags |= LOOKUP_CREATE; + if (open_flag & O_EXCL) + nd.flags |= LOOKUP_EXCL; + } + op.open_flag = open_flag; + op.mode = S_IFREG | mode; + dentry = lookup_open(&nd, file, &op); + + if (IS_ERR(dentry)) + return ERR_CAST(dentry); + + if (d_really_is_negative(dentry)) { + error = -ENOENT; + } else if (!(file->f_mode & FMODE_CREATED) && (open_flag & O_EXCL)) { + error = -EEXIST; + } else if ((dentry->d_inode->i_mode & S_IFMT) != S_IFREG) { + switch (dentry->d_inode->i_mode & S_IFMT) { + case S_IFDIR: + error = -EISDIR; + break; + case S_IFLNK: + error = -ELOOP; + break; + case S_IFBLK: + case S_IFCHR: + error = -ENODEV; + break; + case S_IFIFO: + case S_IFSOCK: + default: + error = -EFTYPE; + break; + } + } else if (!(file->f_mode & FMODE_OPENED)) { + nd.path.dentry = dentry; + error = vfs_open(&nd.path, file); + } + dput(dentry); + + if (error) + return ERR_PTR(error); + return no_free_ptr(file); +} +EXPORT_SYMBOL_FOR_MODULES(vfs_lookup_open, "nfsd"); + static inline bool trailing_slashes(struct nameidata *nd) { return (bool)nd->last.name[nd->last.len]; diff --git a/include/linux/namei.h b/include/linux/namei.h index ebe6e29f7e93..86d657b24fc6 100644 --- a/include/linux/namei.h +++ b/include/linux/namei.h @@ -97,6 +97,9 @@ struct dentry *start_creating_dentry(struct dentry *parent, struct dentry *start_removing_dentry(struct dentry *parent, struct dentry *child); +struct file *vfs_lookup_open(struct path *parent, struct qstr *last, + int open_flag, umode_t mode); + /* end_creating - finish action started with start_creating * @child: dentry returned by start_creating() or vfs_mkdir() * -- 2.50.0.107.gf914562f5916.dirty