Skip to contentSkip to conversation

VFS: refactor lookup_open and add vfs_lookup()

PATCH Superseded NeilBrown 3 participants 9 messages Jul 5 – Jul 7 VFS

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

NeilBrown

author
#0 lore raw

v2 cover VFS: refactor lookup_open and add vfs_lookup()

In this v2 I have:
- added reviewed-bys to 1/3 and 2/3
- added EXPORT_SYMBOL and WARN_ONCE to 3/3 as suggested
- changed vfs_lookup_open() to open non-regular files as O_PATH. This allows the caller to see what was found and to return an appropriate error code (different NFS versions have subtley different error code requirements). Because of this change I didn't include any Reviewed-by in 3/3

The series that prepares NFSd to use this is at
https://lore.kernel.org/linux-nfs/[email protected]/T/#t

If this can land in a stable branch, I can ask nfsd maintainer to merge that branch and connect the two series together to hopefully land in the next merge window.

Original series description:

My recent proposal for inverting the order between inode_lock() on a parent dir and d_alloc_parallel() missed that fact that atomic_open() has two callers, and would have resulted in easy deadlocks from nfsd when re-exporting and NFS filesystem.

I think the best way to fix this is to provide a richer interface for nfsd to use, which includes all the locking as well as inode_operations calls. This allows the nfsd behaviour to share more code with the system-call behaviour.

This series refactors code between lookup_open() and open_last_lookups(), and uses the new lookup_open() to provide vfs_lookup_open().

[PATCH v2 1/3] VFS: move mnt_want_write() and locking into [PATCH v2 2/3] VFS: move delegated_inode retry loop into [PATCH v2 3/3] VFS: add vfs_lookup_open() for nfsd

Thanks,
NeilBrown

author
#1 lore raw

PATCH v2 1/3 VFS: move mnt_want_write() and locking into lookup_open()

From: NeilBrown <[email protected]>

The mnt_want_write() call and the parent inode locking in open_last_lookups() are only needed for lookup_open(). So we can move them and all the got_write handling into lookup_open().

Note that we need to also check create_error when determining whether to unlock shared or not, as O_CREAT can be cleared, but create_error is only set of O_CREAT was set.

The fsnotify calls come too as they must be in the locked region.

Also use the existing dir_inode uniformly for dir->d_inode.

This is a step towards exporting an better "open/create" interface to nfsd.

---
 fs/namei.c | 77 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 41 insertions(+), 36 deletions(-)
fs/namei.c +41 -36
index 7ead3db55be2..55404fe0e269 100644
@@ -4398,7 +4398,7 @@ static struct dentry *atomic_open(const struct path *path, struct dentry *dentry
  */
 static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
 				  const struct open_flags *op,
-				  bool got_write, struct delegated_inode *delegated_inode)
+				  struct delegated_inode *delegated_inode)
 {
 	struct mnt_idmap *idmap;
 	struct dentry *dir = nd->path.dentry;
@@ -4407,9 +4407,25 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
 	struct dentry *dentry;
 	int error, create_error = 0;
 	umode_t mode = op->mode;
+	bool got_write = false;
 
-	if (unlikely(IS_DEADDIR(dir_inode)))
-		return ERR_PTR(-ENOENT);
+	if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
+		got_write = !mnt_want_write(nd->path.mnt);
+		/*
+		 * do _not_ fail yet - we might not need that or fail with
+		 * a different error; let lookup_open() decide; we'll be
+		 * dropping this one anyway.
+		 */
+	}
+	if (open_flag & O_CREAT)
+		inode_lock(dir_inode);
+	else
+		inode_lock_shared(dir_inode);
+
+	if (unlikely(IS_DEADDIR(dir_inode))) {
+		dentry = ERR_PTR(-ENOENT);
+		goto out;
+	}
 
 	file->f_mode &= ~FMODE_CREATED;
 	dentry = d_lookup(dir, &nd->last);
@@ -4417,7 +4433,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
 		if (!dentry) {
 			dentry = d_alloc_parallel(dir, &nd->last);
 			if (IS_ERR(dentry))
-				return dentry;
+				goto out;
 		}
 		if (d_in_lookup(dentry))
 			break;
@@ -4433,7 +4449,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
 	}
 	if (dentry->d_inode) {
 		/* Cached positive dentry: will open in f_op->open */
-		return dentry;
+		goto out;
 	}
 
 	if (open_flag & O_CREAT)
@@ -4454,7 +4470,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
 	if (open_flag & O_CREAT) {
 		if (open_flag & O_EXCL)
 			open_flag &= ~O_TRUNC;
-		mode = vfs_prepare_mode(idmap, dir->d_inode, mode, mode, mode);
+		mode = vfs_prepare_mode(idmap, dir_inode, mode, mode, mode);
 		if (likely(got_write))
 			create_error = may_o_create(idmap, &nd->path,
 						    dentry, mode);
@@ -4469,7 +4485,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
 		dentry = atomic_open(&nd->path, dentry, file, open_flag, mode);
 		if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
 			dentry = ERR_PTR(create_error);
-		return dentry;
+		goto out;
 	}
 
 	if (d_in_lookup(dentry)) {
@@ -4508,11 +4524,27 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
 		error = create_error;
 		goto out_dput;
 	}
+out:
+	if (!IS_ERR(dentry)) {
+		if (file->f_mode & FMODE_CREATED)
+			fsnotify_create(dir_inode, dentry);
+		if (file->f_mode & FMODE_OPENED)
+			fsnotify_open(file);
+	}
+	if ((open_flag & O_CREAT) || create_error)
+		inode_unlock(dir_inode);
+	else
+		inode_unlock_shared(dir_inode);
+
+	if (got_write)
+		mnt_drop_write(nd->path.mnt);
+
 	return dentry;
 
 out_dput:
 	dput(dentry);
-	return ERR_PTR(error);
+	dentry = ERR_PTR(error);
+	goto out;
 }
 
 static inline bool trailing_slashes(struct nameidata *nd)
@@ -4555,9 +4587,7 @@ static const char *open_last_lookups(struct nameidata *nd,
 		   struct file *file, const struct open_flags *op)
 {
 	struct delegated_inode delegated_inode = { };
-	struct dentry *dir = nd->path.dentry;
 	int open_flag = op->open_flag;
-	bool got_write = false;
 	struct dentry *dentry;
 	const char *res;
 
@@ -4587,32 +4617,7 @@ static const char *open_last_lookups(struct nameidata *nd,
 		}
 	}
 retry:
-	if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
-		got_write = !mnt_want_write(nd->path.mnt);
-		/*
-		 * do _not_ fail yet - we might not need that or fail with
-		 * a different error; let lookup_open() decide; we'll be
-		 * dropping this one anyway.
-		 */
-	}
-	if (open_flag & O_CREAT)
-		inode_lock(dir->d_inode);
-	else
-		inode_lock_shared(dir->d_inode);
-	dentry = lookup_open(nd, file, op, got_write, &delegated_inode);
-	if (!IS_ERR(dentry)) {
-		if (file->f_mode & FMODE_CREATED)
-			fsnotify_create(dir->d_inode, dentry);
-		if (file->f_mode & FMODE_OPENED)
-			fsnotify_open(file);
-	}
-	if (open_flag & O_CREAT)
-		inode_unlock(dir->d_inode);
-	else
-		inode_unlock_shared(dir->d_inode);
-
-	if (got_write)
-		mnt_drop_write(nd->path.mnt);
+	dentry = lookup_open(nd, file, op, &delegated_inode);
 
 	if (IS_ERR(dentry)) {
 		if (is_delegated(&delegated_inode)) {
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#2 lore raw

PATCH v2 2/3 VFS: move delegated_inode retry loop into lookup_open()

In reply to NeilBrown

From: NeilBrown <[email protected]>

By moving this retry into lookup_open() we no longer need to pass around the delegated_inode pointer.

Various variable assignments need to be moved out of the declaration block so that they can be repeated after the "goto retry".

---
 fs/namei.c | 43 ++++++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 19 deletions(-)
fs/namei.c +24 -19
index 55404fe0e269..cb33a01d16eb 100644
@@ -4397,17 +4397,23 @@ static struct dentry *atomic_open(const struct path *path, struct dentry *dentry
  * An error code is returned on failure.
  */
 static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
-				  const struct open_flags *op,
-				  struct delegated_inode *delegated_inode)
+				  const struct open_flags *op)
 {
+	struct delegated_inode delegated_inode = { };
 	struct mnt_idmap *idmap;
 	struct dentry *dir = nd->path.dentry;
 	struct inode *dir_inode = dir->d_inode;
-	int open_flag = op->open_flag;
+	int open_flag;
 	struct dentry *dentry;
-	int error, create_error = 0;
-	umode_t mode = op->mode;
-	bool got_write = false;
+	int error, create_error;
+	umode_t mode;
+	bool got_write;
+
+retry:
+	open_flag = op->open_flag;
+	got_write = false;
+	mode = op->mode;
+	create_error = 0;
 
 	if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
 		got_write = !mnt_want_write(nd->path.mnt);
@@ -4505,7 +4511,7 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
 	/* Negative dentry, just create the file */
 	if (!dentry->d_inode && (open_flag & O_CREAT)) {
 		/* but break the directory lease first! */
-		error = try_break_deleg(dir_inode, LEASE_BREAK_DIR_CREATE, delegated_inode);
+		error = try_break_deleg(dir_inode, LEASE_BREAK_DIR_CREATE, &delegated_inode);
 		if (error)
 			goto out_dput;
 
@@ -4539,6 +4545,15 @@ static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
 	if (got_write)
 		mnt_drop_write(nd->path.mnt);
 
+	if (is_delegated(&delegated_inode)) {
+		/* Must have come through out_dput: dentry is an ERR_PTR() */
+		error = break_deleg_wait(&delegated_inode);
+
+		if (!error)
+			goto retry;
+		dentry = ERR_PTR(error);
+	}
+
 	return dentry;
 
 out_dput:
@@ -4586,7 +4601,6 @@ static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag)
 static const char *open_last_lookups(struct nameidata *nd,
 		   struct file *file, const struct open_flags *op)
 {
-	struct delegated_inode delegated_inode = { };
 	int open_flag = op->open_flag;
 	struct dentry *dentry;
 	const char *res;
@@ -4616,19 +4630,10 @@ static const char *open_last_lookups(struct nameidata *nd,
 				return ERR_PTR(-ECHILD);
 		}
 	}
-retry:
-	dentry = lookup_open(nd, file, op, &delegated_inode);
-
-	if (IS_ERR(dentry)) {
-		if (is_delegated(&delegated_inode)) {
-			int error = break_deleg_wait(&delegated_inode);
 
-			if (!error)
-				goto retry;
-			return ERR_PTR(error);
-		}
+	dentry = lookup_open(nd, file, op);
+	if (IS_ERR(dentry))
 		return ERR_CAST(dentry);
-	}
 
 	if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
 		dput(nd->path.dentry);
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
author
#3 lore raw

PATCH v2 3/3 VFS: add vfs_lookup_open() for nfsd

In reply to NeilBrown

From: NeilBrown <[email protected]>

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.

---
 fs/namei.c            | 70 +++++++++++++++++++++++++++++++++++++++++++
 include/linux/namei.h |  3 ++
 2 files changed, 73 insertions(+)
fs/namei.c +70 -0
index cb33a01d16eb..60dddf7b97aa 100644
@@ -4562,6 +4562,76 @@ 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 in-kernel users, particularly
+ * nfsd.
+ * It uses ->atomic_open or ->lookup / ->create / ->open as appropriate.
+ *
+ * Returns: the opened struct file, or an error.
+ *     Caller should check that the file opened is regular,
+ *     if something else was found, an O_PATH file is provided.
+ *     Caller should validate FMODE_CREATED if O_EXCL was requested.
+ */
+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_OPENED)) {
+		nd.path.dentry = dentry;
+		if (!d_is_reg(dentry))
+			/*
+			 * allow caller to see dentry and choose
+			 * correct error code
+			 */
+			file->f_flags |= O_PATH;
+		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];
include/linux/namei.h +3 -0
index ebe6e29f7e93..86d657b24fc6 100644
@@ -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()
  *
Signature · 1 line
2.50.0.107.gf914562f5916.dirty
#4 lore raw

Re: [PATCH v2 3/3] VFS: add vfs_lookup_open() for nfsd

Quoted 73 lines

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.

fs/namei.c +51 -0
index 1ca738401afa..002ffae7083d 100644
@@ -4568,6 +4568,76 @@ 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 in-kernel users, particularly
+ * nfsd.
+ * It uses ->atomic_open or ->lookup / ->create / ->open as appropriate.
+ *
+ * Returns: the opened struct file, or an error.
+ *     Caller should check that the file opened is regular,
+ *     if something else was found, an O_PATH file is provided.
+ *     Caller should validate FMODE_CREATED if O_EXCL was requested.
+ */
+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)) {

fs/namei.c static struct dentry *lookup_open(struct nameidata *nd, struct file *file, · line 4631

Quoted 10 lines
fs/namei.c +10 -0
+		error = -ENOENT;
+	} else if (!(file->f_mode & FMODE_OPENED)) {
+		nd.path.dentry = dentry;
+		if (!d_is_reg(dentry))
+			/*
+			 * allow caller to see dentry and choose
+			 * correct error code
+			 */
+			file->f_flags |= O_PATH;
+		error = vfs_open(&nd.path, file);

Imho, that really throws a wrench into this function. I'd rather just fail with well-known error codes in that case:

/* directory */ -> EISDIR
/* device */ -> ENODEV

Also, why does the caller need to know details here? Because of the O_DIRECTORY work? But even so, EFTYPE would be enough afaict, I think?

Signature · 1 line
Christian Brauner <[email protected]>
#5 lore raw

Re: [PATCH v2 3/3] VFS: add vfs_lookup_open() for nfsd

Quoted 45 lines

Op 06-07-2026 15:33 CEST schreef Christian Brauner <[email protected]>:

Quoted 41 lines

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.

fs/namei.c +19 -0
index 1ca738401afa..002ffae7083d 100644
@@ -4568,6 +4568,76 @@ 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 in-kernel users, particularly
+ * nfsd.
+ * It uses ->atomic_open or ->lookup / ->create / ->open as appropriate.
+ *
+ * Returns: the opened struct file, or an error.
+ *     Caller should check that the file opened is regular,
+ *     if something else was found, an O_PATH file is provided.
+ *     Caller should validate FMODE_CREATED if O_EXCL was requested.
+ */
+struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
+			     int open_flag, umode_t mode)
Quoted 8 lines

fs/namei.c static struct dentry *lookup_open(struct nameidata *nd, struct file *file, · line 4591

Quoted 8 lines
fs/namei.c +8 -0
+{
+	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");

Typo here.

Quoted 38 lines
Quoted 24 lines
fs/namei.c +24 -0
+	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)) {

fs/namei.c static struct dentry *lookup_open(struct nameidata *nd, struct file *file, · line 4625

Quoted 10 lines
fs/namei.c +10 -0
+		error = -ENOENT;
+	} else if (!(file->f_mode & FMODE_OPENED)) {
+		nd.path.dentry = dentry;
+		if (!d_is_reg(dentry))
+			/*
+			 * allow caller to see dentry and choose
+			 * correct error code
+			 */
+			file->f_flags |= O_PATH;
+		error = vfs_open(&nd.path, file);

Imho, that really throws a wrench into this function. I'd rather just fail with well-known error codes in that case:

Quoted 6 lines

/* directory */ -> EISDIR
/* device */ -> ENODEV

Also, why does the caller need to know details here? Because of the O_DIRECTORY work? But even so, EFTYPE would be enough afaict, I think?

If that is the reason it would be easier to just ban O_DIRECTORY from the start here, if vfs_lookup_open() is meant to only ever open/create regular files.

Is that the case, Neil?

Now that I think about it, I think you also want to restrict the O_* flags. lookup_open() normally does not get passed __O_TMPFILE or O_PATH afaik, and it would probably a bad idea to start passing it these flags. Maybe also don't want to allow O_TRUNC.

Quoted 2 lines
Signature · 1 line
Christian Brauner <[email protected]>
author
#6 lore raw

Re: [PATCH v2 3/3] VFS: add vfs_lookup_open() for nfsd

Quoted 46 lines · Jori Koolstra
Quoted 46 lines

Op 06-07-2026 15:33 CEST schreef Christian Brauner <[email protected]>:

Quoted 43 lines

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.

fs/namei.c +21 -0
index 1ca738401afa..002ffae7083d 100644
@@ -4568,6 +4568,76 @@ 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 in-kernel users, particularly
+ * nfsd.
+ * It uses ->atomic_open or ->lookup / ->create / ->open as appropriate.
+ *
+ * Returns: the opened struct file, or an error.
+ *     Caller should check that the file opened is regular,
+ *     if something else was found, an O_PATH file is provided.
+ *     Caller should validate FMODE_CREATED if O_EXCL was requested.
+ */
+struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
+			     int open_flag, umode_t mode)
+{
+	struct file *file __free(fput) = NULL;
Quoted 8 lines
Quoted 6 lines

fs/namei.c static struct dentry *lookup_open(struct nameidata *nd, struct file *file, · line 4591

Quoted 6 lines
fs/namei.c +6 -0
+	struct nameidata nd = {};
+	struct open_flags op = {};
+	struct dentry *dentry;
+	int error = 0;
+
+	WARN_ONCE(mode & !S_IALLUGO, "mode must only have permission bits");

Typo here.

Thanks.

Quoted 46 lines
Quoted 39 lines
Quoted 25 lines
fs/namei.c +24 -0
+	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)) {

fs/namei.c static struct dentry *lookup_open(struct nameidata *nd, struct file *file, · line 4625

Quoted 10 lines
fs/namei.c +10 -0
+		error = -ENOENT;
+	} else if (!(file->f_mode & FMODE_OPENED)) {
+		nd.path.dentry = dentry;
+		if (!d_is_reg(dentry))
+			/*
+			 * allow caller to see dentry and choose
+			 * correct error code
+			 */
+			file->f_flags |= O_PATH;
+		error = vfs_open(&nd.path, file);

Imho, that really throws a wrench into this function. I'd rather just fail with well-known error codes in that case:

Quoted 6 lines

/* directory */ -> EISDIR
/* device */ -> ENODEV

Also, why does the caller need to know details here? Because of the O_DIRECTORY work? But even so, EFTYPE would be enough afaict, I think?

Quoted 5 lines

If that is the reason it would be easier to just ban O_DIRECTORY from the start here, if vfs_lookup_open() is meant to only ever open/create regular files.

Is that the case, Neil?

nfsd would never pass O_DIRECTORY to vfs_lookup_open() (unless the protocol were extended one day of course) so banning O_DIRECTORY would be best as accepting it could never be tested.

Quoted 5 lines

Now that I think about it, I think you also want to restrict the O_* flags. lookup_open() normally does not get passed __O_TMPFILE or O_PATH afaik, and it would probably a bad idea to start passing it these flags. Maybe also don't want to allow O_TRUNC.

Well nfsd is never going to send an illegal flag, but making it explicit in the code what is supported would be sensible.
O_TRUNC needs to be supported because ->atomic_open can handle that. An NFSv4 OPEN request can indicate that if the file wasn't created, it should be truncated.

Thanks,
NeilBrown

author
#7 lore raw

Re: [PATCH v2 3/3] VFS: add vfs_lookup_open() for nfsd

In reply to Christian Brauner
Quoted 87 lines · Christian Brauner
Quoted 73 lines

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.

fs/namei.c +51 -0
index 1ca738401afa..002ffae7083d 100644
@@ -4568,6 +4568,76 @@ 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 in-kernel users, particularly
+ * nfsd.
+ * It uses ->atomic_open or ->lookup / ->create / ->open as appropriate.
+ *
+ * Returns: the opened struct file, or an error.
+ *     Caller should check that the file opened is regular,
+ *     if something else was found, an O_PATH file is provided.
+ *     Caller should validate FMODE_CREATED if O_EXCL was requested.
+ */
+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)) {

fs/namei.c static struct dentry *lookup_open(struct nameidata *nd, struct file *file, · line 4631

Quoted 10 lines
fs/namei.c +10 -0
+		error = -ENOENT;
+	} else if (!(file->f_mode & FMODE_OPENED)) {
+		nd.path.dentry = dentry;
+		if (!d_is_reg(dentry))
+			/*
+			 * allow caller to see dentry and choose
+			 * correct error code
+			 */
+			file->f_flags |= O_PATH;
+		error = vfs_open(&nd.path, file);

Imho, that really throws a wrench into this function. I'd rather just fail with well-known error codes in that case:

Quoted 2 lines

/* directory */ -> EISDIR
/* device */ -> ENODEV

What if the dentry is a symlink? May ELOOP could be used?

Quoted 3 lines

Also, why does the caller need to know details here? Because of the O_DIRECTORY work? But even so, EFTYPE would be enough afaict, I think?

fs/nfsd/nfs4proc.c:

	if (S_ISREG(mode))
		return nfs_ok;
	if (S_ISDIR(mode))
		return nfserr_isdir;
	if (S_ISLNK(mode))
		return nfserr_symlink;

	/* RFC 7530 - 16.16.6 */
	if (minor_version == 0)
		return nfserr_symlink;
	else
		return nfserr_wrong_type;

The spec requires specific errors which are different between versions and S_ISLNK seemed problematic. I thought that keeping the VFS completely out of that issue seemed best: simply return the dentry found and let the caller handle it. But if you are OK with ELOOP for symlinks (and EFTYPE for sockets and named pipes), or can suggest an alternate error code, I'm happy to work with that.

Thanks,
NeilBrown

#8 lore raw

Re: [PATCH v2 3/3] VFS: add vfs_lookup_open() for nfsd

Quoted 88 lines · NeilBrown
Quoted 87 lines · Christian Brauner
Quoted 73 lines

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.

fs/namei.c +51 -0
index 1ca738401afa..002ffae7083d 100644
@@ -4568,6 +4568,76 @@ 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 in-kernel users, particularly
+ * nfsd.
+ * It uses ->atomic_open or ->lookup / ->create / ->open as appropriate.
+ *
+ * Returns: the opened struct file, or an error.
+ *     Caller should check that the file opened is regular,
+ *     if something else was found, an O_PATH file is provided.
+ *     Caller should validate FMODE_CREATED if O_EXCL was requested.
+ */
+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)) {

fs/namei.c static struct dentry *lookup_open(struct nameidata *nd, struct file *file, · line 4631

Quoted 10 lines
fs/namei.c +10 -0
+		error = -ENOENT;
+	} else if (!(file->f_mode & FMODE_OPENED)) {
+		nd.path.dentry = dentry;
+		if (!d_is_reg(dentry))
+			/*
+			 * allow caller to see dentry and choose
+			 * correct error code
+			 */
+			file->f_flags |= O_PATH;
+		error = vfs_open(&nd.path, file);

Imho, that really throws a wrench into this function. I'd rather just fail with well-known error codes in that case:

Quoted 4 lines
Quoted 2 lines

/* directory */ -> EISDIR
/* device */ -> ENODEV

What if the dentry is a symlink? May ELOOP could be used?

Sure.

Quoted 20 lines
Quoted 3 lines

Also, why does the caller need to know details here? Because of the O_DIRECTORY work? But even so, EFTYPE would be enough afaict, I think?

fs/nfsd/nfs4proc.c:

	if (S_ISREG(mode))
		return nfs_ok;
	if (S_ISDIR(mode))
		return nfserr_isdir;
	if (S_ISLNK(mode))
		return nfserr_symlink;

	/* RFC 7530 - 16.16.6 */
	if (minor_version == 0)
		return nfserr_symlink;
	else
		return nfserr_wrong_type;
Quoted 6 lines

The spec requires specific errors which are different between versions and S_ISLNK seemed problematic. I thought that keeping the VFS completely out of that issue seemed best: simply return the dentry found and let the caller handle it. But if you are OK with ELOOP for symlinks (and EFTYPE for sockets and named pipes), or can suggest an alternate error code, I'm happy to work with that.

I think ELOOP and EFTYPE is fine. I just don't really like that we open something that is of the wrong type - even with O_PATH. That seems ripe for accidental bugs imho.