linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>, Jan Kara <jack@suse.cz>,
	 linux-fsdevel@vger.kernel.org,
	Christian Brauner <brauner@kernel.org>
Subject: [PATCH DRAFT RFC UNTESTED 01/18] file: add FD_PREPARE()
Date: Tue, 18 Nov 2025 16:48:41 +0100	[thread overview]
Message-ID: <20251118-work-fd-prepare-v1-1-c20504d97375@kernel.org> (raw)
In-Reply-To: <20251118-work-fd-prepare-v1-0-c20504d97375@kernel.org>

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 include/linux/cleanup.h |  22 ++++++++
 include/linux/file.h    | 137 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 159 insertions(+)

diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 2573585b7f06..b35b9a1da026 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -255,12 +255,20 @@ const volatile void * __must_check_fn(const volatile void *val)
  *	@exit is an expression using '_T' -- similar to FREE above.
  *	@init is an expression in @init_args resulting in @type
  *
+ * DEFINE_CLASS_TYPE(name, type, exit):
+ *	Like DEFINE_CLASS but without a constructor. Use with CLASS_INIT()
+ *	for classes that need custom initialization expressions per usage.
+ *
  * EXTEND_CLASS(name, ext, init, init_args...):
  *	extends class @name to @name@ext with the new constructor
  *
  * CLASS(name, var)(args...):
  *	declare the variable @var as an instance of the named class
  *
+ * CLASS_INIT(name, var, init_expr):
+ *	declare the variable @var as an instance of the named class with
+ *	custom initialization expression. Use with DEFINE_CLASS_TYPE().
+ *
  * Ex.
  *
  * DEFINE_CLASS(fdget, struct fd, fdput(_T), fdget(fd), int fd)
@@ -270,6 +278,12 @@ const volatile void * __must_check_fn(const volatile void *val)
  *		return -EBADF;
  *
  *	// use 'f' without concern
+ *
+ * DEFINE_CLASS_TYPE(fd_file, struct { int fd; struct file *file; }, ...)
+ *
+ *	CLASS_INIT(fd_file, ff, custom_init_expression);
+ *	if (ff.fd < 0)
+ *		return ff.fd;
  */
 
 #define DEFINE_CLASS(_name, _type, _exit, _init, _init_args...)		\
@@ -279,6 +293,11 @@ static inline void class_##_name##_destructor(_type *p)			\
 static inline _type class_##_name##_constructor(_init_args)		\
 { _type t = _init; return t; }
 
+#define DEFINE_CLASS_TYPE(_name, _type, _exit)				\
+typedef _type class_##_name##_t;					\
+static inline void class_##_name##_destructor(_type *p)		\
+{ _type _T = *p; _exit; }
+
 #define EXTEND_CLASS(_name, ext, _init, _init_args...)			\
 typedef class_##_name##_t class_##_name##ext##_t;			\
 static inline void class_##_name##ext##_destructor(class_##_name##_t *p)\
@@ -290,6 +309,9 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
 	class_##_name##_t var __cleanup(class_##_name##_destructor) =	\
 		class_##_name##_constructor
 
+#define CLASS_INIT(_name, _var, _init_expr)				\
+	class_##_name##_t _var __cleanup(class_##_name##_destructor) = (_init_expr)
+
 #define scoped_class(_name, var, args)                          \
 	for (CLASS(_name, var)(args);                           \
 	     __guard_ptr(_name)(&var) || !__is_cond_ptr(_name); \
diff --git a/include/linux/file.h b/include/linux/file.h
index af1768d934a0..331b07f9a6fd 100644
--- a/include/linux/file.h
+++ b/include/linux/file.h
@@ -12,6 +12,7 @@
 #include <linux/errno.h>
 #include <linux/cleanup.h>
 #include <linux/err.h>
+#include <linux/vfsdebug.h>
 
 struct file;
 
@@ -127,4 +128,140 @@ extern void __fput_sync(struct file *);
 
 extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
 
+/*
+ * fd_prepare class: Combined fd + file allocation with automatic cleanup.
+ *
+ * Allocates an fd and a file together. On error paths, automatically cleans
+ * up whichever resource was successfully allocated. Allows flexible file
+ * allocation with different functions per usage.
+ */
+
+struct fd_prepare {
+	int fd;
+	struct file *file;
+};
+
+/*
+ * fd_prepare_fd() - Get fd from fd_prepare structure
+ * @fd_prepare: struct fd_prepare to extract fd from
+ *
+ * Returns the file descriptor from an fd_prepare structure.
+ *
+ * Return: The file descriptor
+ */
+static inline int fd_prepare_fd(struct fd_prepare fdp)
+{
+	return fdp.fd;
+}
+
+/*
+ * fd_prepare_file() - Get file from fd_prepare structure
+ * @fd_prepare: struct fd_prepare to extract file from
+ *
+ * Returns the file pointer from an fd_prepare structure.
+ *
+ * Return: The file pointer
+ */
+static inline struct file *fd_prepare_file(struct fd_prepare fdp)
+{
+	return fdp.file;
+}
+
+/*
+ * fd_prepare_failed() - Check if fd_prepare allocation failed
+ * @fd_prepare: struct fd_prepare to check
+ *
+ * Checks whether either the fd allocation or file allocation failed.
+ *
+ * Return: true if either allocation failed, false otherwise
+ */
+static inline bool fd_prepare_failed(struct fd_prepare fdp)
+{
+	VFS_WARN_ON_ONCE(fdp.fd < 0 && IS_ERR(fdp.file));
+	return fdp.fd < 0 || IS_ERR(fdp.file);
+}
+
+/*
+ * fd_prepare_error() - Get error from failed fd_prepare
+ * @fd_prepare: struct fd_prepare to extract error from
+ *
+ * Returns the error code from the first allocation that failed.
+ * Should only be called after fd_prepare_failed() returns true.
+ *
+ * Return: Negative error code
+ */
+static inline int fd_prepare_error(struct fd_prepare fdp)
+{
+	if (fdp.fd < 0) {
+		VFS_WARN_ON_ONCE(fdp.file);
+		return fdp.fd;
+	}
+	if (!fdp.file)
+		return -ENOMEM;
+	return PTR_ERR(fdp.file);
+}
+
+static inline void __fd_prepare_put(struct fd_prepare fdp)
+{
+	if (fdp.fd >= 0)
+		put_unused_fd(fdp.fd);
+	if (!IS_ERR_OR_NULL(fdp.file))
+		fput(fdp.file);
+}
+
+DEFINE_CLASS_TYPE(fd_prepare, struct fd_prepare, __fd_prepare_put(_T))
+
+/*
+ * __FD_PREPARE_INIT(fd_flags, file_init_expr):
+ *     Helper to initialize fd_prepare class.
+ *     @fd_flags: flags for get_unused_fd_flags()
+ *     @file_init_expr: expression that returns struct file *
+ *
+ * Returns a struct fd_prepare with fd and file set.
+ * If fd allocation fails, file will be NULL.
+ * If fd succeeds but file_init_expr fails, fd will be cleaned up.
+ */
+#define __FD_PREPARE_INIT(_fd_flags, _file_init)                \
+	({                                                    \
+		struct fd_prepare _fd_prepare = {             \
+			.fd = get_unused_fd_flags(_fd_flags), \
+		};                                            \
+		if (_fd_prepare.fd >= 0)                      \
+			_fd_prepare.file = (_file_init);      \
+		_fd_prepare;                                  \
+	})
+
+/*
+ * FD_PREPARE(var, fd_flags, file_init_expr):
+ *     Convenience wrapper for CLASS_INIT(fd_prepare, ...).
+ *
+ * Ex.
+ * FD_PREPARE(ff, O_RDWR | O_CLOEXEC,
+ *                  anon_inode_getfile("[eventpoll]", &eventpoll_fops, ep, O_RDWR));
+ * if (fd_prepare_failed(ff))
+ *     return fd_prepare_error(ff);
+ *
+ * ep->file = fd_prepare_file(ff);
+ * return fd_publish(ff);
+ *
+ * Or with different file init function:
+ *
+ * FD_PREPARE(ff, flags,
+ *                  anon_inode_getfile_fmode("[eventfd]", &eventfd_fops, ctx, flags, FMODE_NOWAIT));
+ * if (fd_prepare_failed(ff))
+ *     return fd_prepare_error(ff);
+ *
+ * return fd_publish(ff);
+ */
+#define FD_PREPARE(_var, _fd_flags, _file_init) \
+	CLASS_INIT(fd_prepare, _var, __FD_PREPARE_INIT(_fd_flags, _file_init))
+
+#define fd_publish(_fd_prepare)                          \
+	({                                               \
+		struct fd_prepare *__p = &(_fd_prepare); \
+		fd_install(__p->fd, __p->file);          \
+		retain_and_null_ptr(__p->file);          \
+		take_fd(__p->fd);                        \
+	})
+
 #endif /* __LINUX_FILE_H */

-- 
2.47.3


  reply	other threads:[~2025-11-18 15:49 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-18 15:48 [PATCH DRAFT RFC UNTESTED 00/18] file: FD_PREPARE() Christian Brauner
2025-11-18 15:48 ` Christian Brauner [this message]
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 02/18] fs: anon_inodes Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 03/18] fs: eventfd Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 04/18] fs: fhandle Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 05/18] fs: open_tree() Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 06/18] fs: namespace Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 07/18] fs: fanotify Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 08/18] fs: nsfs Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 09/18] fs: autofs Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 10/18] fs: eventpoll Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 11/18] fs: open_tree_attr() Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 12/18] fs: nsfs2 Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 13/18] fs: open Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 14/18] fs: signalfd64 Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 15/18] fs: timerfd Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 16/18] fs: userfaultfd Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 17/18] fs: xfs Christian Brauner
2025-11-18 15:48 ` [PATCH DRAFT RFC UNTESTED 18/18] drivers: drm_lease Christian Brauner

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=20251118-work-fd-prepare-v1-1-c20504d97375@kernel.org \
    --to=brauner@kernel.org \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).