Re: [PATCH 4/4] file: make struct fd_prepare const and kill its err field

From: David Laight

Date: Thu Sep 17 2026 - 08:47:23 EST


On Thu, 17 Sep 2026 11:09:45 +0200
Christian Brauner <brauner@xxxxxxxxxx> wrote:

> FD_PREPARE() releases the fd as soon as the file expression fails. The
> fd either holds the descriptor or the error. The separate field is
> redundant. Make fd and file plain members and struct fd_prepare simpler.
>
> Callers of FD_PREPARE() get a const pointer to the guard. Simplify all
> users as they can now easily access the trivial struct.
>
...
> @@ -191,35 +175,39 @@ static __always_inline struct fd_prepare __fd_prepare(int fd, struct file *file)
> * was allocated. If fd_publish() was called the fd and file are
> * published and cleanup becomes a nop.
> *
> - * @_fdf: name of struct fd_prepare variable to define
> + * @_fdf: name of the const struct fd_prepare pointer to define
> * @_fd_flags: flags for get_unused_fd_flags()
> * @_file_owned: struct file to take ownership of (can be expression)
> */
> -#define FD_PREPARE(_fdf, _fd_flags, _file_owned) \
> - struct fd_prepare _fdf __cleanup(__fd_prepare_cleanup) = ({ \
> +#define __FD_PREPARE(_guard, _fdf, _fd_flags, _file_owned) \
> + struct fd_prepare _guard __cleanup(__fd_prepare_cleanup) = ({ \
> int __fd = get_unused_fd_flags(_fd_flags); \
> __fd_prepare(__fd, __fd < 0 ? NULL : (_file_owned)); \
> - })
> + }); \
> + const struct fd_prepare *const _fdf = &_guard

Since the struct is only written by its initialiser, can't it just
be 'const' ?

David