[PATCH master] fs: fix file creation by not pointing file dentry at parent dir

Ahmad Fatoum a.fatoum at pengutronix.de
Tue Jun 24 08:21:48 PDT 2025


A last-minute regression fix to allow unmounting file systems in some
situations unfortunately introduced a regression of its own:
Creating new files with O_CREAT associated a file descriptor's struct
file with the dentry of the parent directory instead of the dentry
of the newly created file.

Opening already existing files worked fine though, leading to e.g.
problems when saving a fresh environment to a directory:

  barebox at TI AM335x BeagleBone black:/ rm /mnt/mmc0.0/barebox.env
  barebox at TI AM335x BeagleBone black:/ saveenv
  saving environment to /mnt/mmc0.0/barebox.env
  could not open /mnt/mmc0.0/barebox.env: Invalid argument
  saveenv: Invalid argument
  barebox at TI AM335x BeagleBone black:/ saveenv
  saving environment to /mnt/mmc0.0/barebox.env

Opening existing files did not regress, because filename_lookup stores
the dentry of the actual file, unlike filename_create, which stores the
dentry of the parent directory.

Fixes: a3ef153fdf26 ("fs: unreference path on file close")
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
I noticed this with saveenv to FAT, because FAT doesn't like opening /,
but I could imagine other subtle errors creeping up elsewhere.
I think a v2025.06.1 may be in order..
---
 fs/fs.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/fs/fs.c b/fs/fs.c
index 8689e14e68ef..4a500bbdd9da 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -2604,6 +2604,12 @@ int openat(int dirfd, const char *pathname, int flags)
 			error = create(path.dentry, dentry);
 			if (error)
 				goto out1;
+			/* repoint path.dentry from parent to newly created entry.
+			 * path.mnt already points at the correct vfsmount, even
+			 * for a dirfd of the root directory, so that's fine.
+			 */
+			dput(path.dentry);
+			path.dentry = dentry;
 		} else {
 			dput(dentry);
 			error = -ENOENT;
-- 
2.39.5




More information about the barebox mailing list