mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Jonas Rebmann <jre@pengutronix.de>, Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 2/2] fs: allocate inodes for O_TMPFILE-created files
Date: Wed, 15 Oct 2025 10:34:16 +0200	[thread overview]
Message-ID: <20251015083512.4099259-2-a.fatoum@barebox.org> (raw)
In-Reply-To: <20251015083512.4099259-1-a.fatoum@barebox.org>

We support O_TMPFILE only for ramfs and the code enabling it was
so far hacked into the file system core.

With upcoming patches to move read/write operations from struct
fs_driver to struct file_operations, this will break:

  - struct inode::i_fop holds the file operations
  - we do not allocate inodes for temporary files
  - there is no way for the core to read/write the temporary files

Fix this by properly implementing O_TMPFILE similarly to how Linux does
it:

  - File systems implement an optional file_operations::tmpfile
  - Temporary files are allocated an inode, but no dentries,
    so nlink is zero

Reported-by: Jonas Rebmann <jre@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
Please place before "fs: move fs_driver operations to struct
file_operations" to avoid intermittent breakage and so "upcoming
patches" makes sense.
---
 fs/fs.c            | 104 ++++++++++++++++++++++++++++++++++-----------
 fs/ramfs.c         |  14 ++++++
 include/errno.h    |   6 +++
 include/linux/fs.h |  26 ++++++++++++
 4 files changed, 125 insertions(+), 25 deletions(-)

diff --git a/fs/fs.c b/fs/fs.c
index 76b1ec4c910a..529c328f26fc 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -2589,6 +2589,70 @@ static int do_dentry_open(struct file *f)
 	return 0;
 }
 
+/**
+ * finish_open - finish opening a file
+ * @file: file pointer
+ * @dentry: pointer to dentry
+ *
+ * If the open callback is set to NULL, then the standard f_op->open()
+ * filesystem callback is substituted.
+ *
+ * In Linux, this function accepts an open callback, but we don't yet
+ * need this in barebox. Thus the standard f_op()->open() callback
+ * will be used for non-O_PATH.
+ *
+ * Returns zero on success or -errno if the open failed.
+ */
+int finish_open(struct file *file, struct dentry *dentry)
+{
+	file->f_path.dentry = dentry;
+	return do_dentry_open(file);
+}
+
+/**
+ * tmpfile_create - create tmpfile
+ * @parentpath:	pointer to the path of the base directory
+ * @mode:	mode of the new tmpfile
+ * @flags:	flags used to open the file
+ *
+ * Create a temporary file.
+ */
+static struct file *tmpfile_create(const struct path *parentpath,
+				   umode_t mode, int flags)
+{
+	struct inode *dir = d_inode(parentpath->dentry);
+	struct fs_device *fsdev;
+	struct file *f;
+	int error;
+
+	fsdev = get_fsdevice_by_dentry(parentpath->dentry);
+	if (!fsdev)
+		return ERR_PTR(-ENOENT);
+
+	if (!dir->i_op->tmpfile)
+		return ERR_PTR(-EOPNOTSUPP);
+
+	f = get_file(fsdev);
+	if (!f)
+		return ERR_PTR(-EMFILE);
+
+	f->f_path.mnt = parentpath->mnt;
+	f->f_path.dentry = d_alloc_anon(&fsdev->sb);
+	f->f_flags = flags;
+
+	error = dir->i_op->tmpfile(dir, f, mode);
+
+	free(f->f_path.dentry);
+	f->f_path.dentry = NULL;
+
+	if (error) {
+		put_file(f);
+		return ERR_PTR(error);
+	}
+
+	return f;
+}
+
 int openat(int dirfd, const char *pathname, int flags)
 {
 	struct fs_device *fsdev;
@@ -2604,33 +2668,10 @@ int openat(int dirfd, const char *pathname, int flags)
 		if (error)
 			return errno_set(error);
 
-		fsdev = get_fsdevice_by_dentry(path.dentry);
+		f = tmpfile_create(&path, S_IFREG, flags);
 		path_put(&path);
 
-		if (!fsdev) {
-			errno = ENOENT;
-			return -errno;
-		}
-
-		if (fsdev->driver != ramfs_driver) {
-			errno = EOPNOTSUPP;
-			return -errno;
-		}
-
-		f = get_file(fsdev);
-		if (!f) {
-			errno = EMFILE;
-			return -errno;
-		}
-
-		f->path = NULL;
-		f->f_path.dentry = NULL;
-		f->f_inode = new_inode(&fsdev->sb);
-		f->f_inode->i_mode = S_IFREG;
-		f->f_flags = flags;
-		f->f_size = 0;
-
-		return file_to_fd(f);
+		return errno_setp(f) ?: file_to_fd(f);
 	}
 
 	if (flags & O_CREAT) {
@@ -2989,6 +3030,19 @@ static char *__dpath(struct dentry *dentry, struct dentry *root)
 	return res;
 }
 
+void d_tmpfile(struct file *file, struct inode *inode)
+{
+	struct dentry *dentry = file->f_path.dentry;
+
+	inode_dec_link_count(inode);
+
+	file->path = xasprintf(dentry->name, "#%llu",
+			       (unsigned long long)inode->i_ino);
+
+	d_instantiate(dentry, inode);
+}
+EXPORT_SYMBOL(d_tmpfile);
+
 /**
  * dpath - return path of a dentry
  * @dentry: The dentry to return the path from
diff --git a/fs/ramfs.c b/fs/ramfs.c
index d8c754492561..1caace0183a5 100644
--- a/fs/ramfs.c
+++ b/fs/ramfs.c
@@ -174,6 +174,19 @@ static const struct inode_operations ramfs_symlink_inode_operations =
 	.get_link = ramfs_get_link,
 };
 
+static int ramfs_tmpfile(struct inode *dir, struct file *file, umode_t mode)
+{
+	struct inode *inode;
+
+	inode = ramfs_get_inode(dir->i_sb, dir, mode);
+	if (!inode)
+		return -ENOSPC;
+
+	d_tmpfile(file, inode);
+
+	return finish_open_simple(file, 0);
+}
+
 static const struct inode_operations ramfs_dir_inode_operations =
 {
 	.lookup = simple_lookup,
@@ -182,6 +195,7 @@ static const struct inode_operations ramfs_dir_inode_operations =
 	.rmdir = simple_rmdir,
 	.unlink = simple_unlink,
 	.create = ramfs_create,
+	.tmpfile = ramfs_tmpfile,
 };
 
 static struct ramfs_chunk *ramfs_find_chunk(struct ramfs_inode *node,
diff --git a/include/errno.h b/include/errno.h
index fd358e1c1103..13968af0295b 100644
--- a/include/errno.h
+++ b/include/errno.h
@@ -27,4 +27,10 @@ static inline int errno_set(int err)
 	return err;
 }
 
+static inline int errno_setp(const void *errp)
+{
+	int error = IS_ERR(errp) ? PTR_ERR(errp) : 0;
+	return errno_set(error);
+}
+
 #endif /* __ERRNO_H */
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 1e12236bcd85..ed4332c79d49 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -443,6 +443,7 @@ struct inode_operations {
 	int (*rmdir) (struct inode *,struct dentry *);
 	int (*rename) (struct inode *, struct dentry *,
 		       struct inode *, struct dentry *, unsigned int);
+	int (*tmpfile)(struct inode *, struct file *, umode_t);
 };
 
 static inline ino_t parent_ino(struct dentry *dentry)
@@ -508,6 +509,8 @@ void drop_nlink(struct inode *inode);
 extern const struct file_operations simple_dir_operations;
 extern const struct inode_operations simple_symlink_inode_operations;
 
+extern void d_tmpfile(struct file *, struct inode *);
+
 int simple_empty(struct dentry *dentry);
 int simple_unlink(struct inode *dir, struct dentry *dentry);
 int simple_rmdir(struct inode *dir, struct dentry *dentry);
@@ -533,4 +536,27 @@ static inline void inode_init_owner(struct inode *inode,
 
 static inline void mark_inode_dirty(struct inode *inode) {}
 
+int finish_open(struct file *file, struct dentry *dentry);
+
+/* Helper for the simple case when original dentry is used */
+static inline int finish_open_simple(struct file *file, int error)
+{
+	if (error)
+		return error;
+
+	return finish_open(file, file->f_path.dentry);
+}
+
+static inline void inode_inc_link_count(struct inode *inode)
+{
+	inc_nlink(inode);
+	mark_inode_dirty(inode);
+}
+
+static inline void inode_dec_link_count(struct inode *inode)
+{
+	drop_nlink(inode);
+	mark_inode_dirty(inode);
+}
+
 #endif /* _LINUX_FS_H */
-- 
2.47.3




  reply	other threads:[~2025-10-15  8:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-15  8:34 [PATCH 1/2] fs: factor out final file open logic out of openat() Ahmad Fatoum
2025-10-15  8:34 ` Ahmad Fatoum [this message]
2025-10-20 10:58 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251015083512.4099259-2-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    --cc=jre@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox