1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Wrapper functions for accessing the file_struct fd array.
4 */
5
6 #ifndef __LINUX_FILE_H
7 #define __LINUX_FILE_H
8
9 #include <linux/compiler.h>
10 #include <linux/types.h>
11 #include <linux/posix_types.h>
12 #include <linux/errno.h>
13 #include <linux/cleanup.h>
14 #include <linux/err.h>
15 #include <linux/vfsdebug.h>
16
17 struct file;
18
19 extern void fput(struct file *);
20
21 struct file_operations;
22 struct task_struct;
23 struct vfsmount;
24 struct dentry;
25 struct inode;
26 struct path;
27 extern struct file *alloc_file_pseudo(struct inode *, struct vfsmount *,
28 const char *, int flags, const struct file_operations *);
29 extern struct file *alloc_file_pseudo_noaccount(struct inode *, struct vfsmount *,
30 const char *, int flags, const struct file_operations *);
31 extern struct file *alloc_file_clone(struct file *, int flags,
32 const struct file_operations *);
33
34 /* either a reference to struct file + flags
35 * (cloned vs. borrowed, pos locked), with
36 * flags stored in lower bits of value,
37 * or empty (represented by 0).
38 */
39 struct fd {
40 unsigned long word;
41 };
42 #define FDPUT_FPUT 1
43 #define FDPUT_POS_UNLOCK 2
44
45 #define fd_file(f) ((struct file *)((f).word & ~(FDPUT_FPUT|FDPUT_POS_UNLOCK)))
fd_empty(struct fd f)46 static inline bool fd_empty(struct fd f)
47 {
48 return unlikely(!f.word);
49 }
50
51 #define EMPTY_FD (struct fd){0}
BORROWED_FD(struct file * f)52 static inline struct fd BORROWED_FD(struct file *f)
53 {
54 return (struct fd){(unsigned long)f};
55 }
CLONED_FD(struct file * f)56 static inline struct fd CLONED_FD(struct file *f)
57 {
58 return (struct fd){(unsigned long)f | FDPUT_FPUT};
59 }
60
fdput(struct fd fd)61 static inline void fdput(struct fd fd)
62 {
63 if (unlikely(fd.word & FDPUT_FPUT))
64 fput(fd_file(fd));
65 }
66
67 extern struct file *fget(unsigned int fd);
68 extern struct file *fget_raw(unsigned int fd);
69 extern struct file *fget_task(struct task_struct *task, unsigned int fd);
70 extern struct file *fget_task_next(struct task_struct *task, unsigned int *fd);
71 extern void __f_unlock_pos(struct file *);
72
73 struct fd fdget(unsigned int fd);
74 struct fd fdget_raw(unsigned int fd);
75 struct fd fdget_pos(unsigned int fd);
76
fdput_pos(struct fd f)77 static inline void fdput_pos(struct fd f)
78 {
79 if (f.word & FDPUT_POS_UNLOCK)
80 __f_unlock_pos(fd_file(f));
81 fdput(f);
82 }
83
84 DEFINE_CLASS(fd, struct fd, fdput(_T), fdget(fd), int fd)
85 DEFINE_CLASS(fd_raw, struct fd, fdput(_T), fdget_raw(fd), int fd)
86 DEFINE_CLASS(fd_pos, struct fd, fdput_pos(_T), fdget_pos(fd), int fd)
87
88 extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
89 extern int replace_fd(unsigned fd, struct file *file, unsigned flags);
90 extern void set_close_on_exec(unsigned int fd, int flag);
91 extern bool get_close_on_exec(unsigned int fd);
92 extern int __get_unused_fd_flags(unsigned flags, unsigned long nofile);
93 extern int get_unused_fd_flags(unsigned flags);
94 extern void put_unused_fd(unsigned int fd);
95
96 DEFINE_CLASS(get_unused_fd, int, if (_T >= 0) put_unused_fd(_T),
97 get_unused_fd_flags(flags), unsigned flags)
98 DEFINE_FREE(fput, struct file *, if (!IS_ERR_OR_NULL(_T)) fput(_T))
99
100 /*
101 * take_fd() will take care to set @fd to -EBADF ensuring that
102 * CLASS(get_unused_fd) won't call put_unused_fd(). This makes it
103 * easier to rely on CLASS(get_unused_fd):
104 *
105 * struct file *f;
106 *
107 * CLASS(get_unused_fd, fd)(O_CLOEXEC);
108 * if (fd < 0)
109 * return fd;
110 *
111 * f = dentry_open(&path, O_RDONLY, current_cred());
112 * if (IS_ERR(f))
113 * return PTR_ERR(f);
114 *
115 * fd_install(fd, f);
116 * return take_fd(fd);
117 */
118 #define take_fd(fd) __get_and_null(fd, -EBADF)
119
120 extern void fd_install(unsigned int fd, struct file *file);
121
122 int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags);
123
124 int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags);
125
126 extern void flush_delayed_fput(void);
127 extern void __fput_sync(struct file *);
128
129 extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
130
131 /*
132 * fd_prepare: Combined fd + file allocation cleanup class.
133 * @fd: Allocated fd
134 * @file: Allocated struct file pointer
135 *
136 * Allocates an fd and a file together. On error paths, automatically cleans
137 * up whichever resource was successfully allocated. Allows flexible file
138 * allocation with different functions per usage.
139 *
140 * Do not declare directly, use FD_PREPARE().
141 */
142 struct fd_prepare {
143 int fd;
144 struct file *file;
145 };
146
147 /* Do not use directly. */
__fd_prepare_cleanup(const struct fd_prepare * fdf)148 static __always_inline void __fd_prepare_cleanup(const struct fd_prepare *fdf)
149 {
150 if (unlikely(fdf->fd >= 0)) {
151 put_unused_fd(fdf->fd);
152 fput(fdf->file);
153 }
154 }
155
156 /* Do not use directly. */
__fd_prepare(int fd,struct file * file)157 static __always_inline struct fd_prepare __fd_prepare(int fd, struct file *file)
158 {
159 if (fd >= 0 && IS_ERR_OR_NULL(file)) {
160 int err = file ? PTR_ERR(file) : -ENOMEM;
161
162 put_unused_fd(fd);
163 fd = err;
164 file = NULL;
165 }
166 return (struct fd_prepare){ .fd = fd, .file = file };
167 }
168
169 /*
170 * FD_PREPARE - Declare and initialize an fd_prepare instance.
171 *
172 * This allocates a new fd and only evaluates @_file_owned if the
173 * allocation succeeded. Cleanup happens when the variable goes out of
174 * scope and the guard releases whichever of the descriptor and the file
175 * was allocated. If fd_publish() was called the fd and file are
176 * published and cleanup becomes a nop.
177 *
178 * @_fdf: name of the const struct fd_prepare pointer to define
179 * @_fd_flags: flags for get_unused_fd_flags()
180 * @_file_owned: struct file to take ownership of (can be expression)
181 */
182 #define __FD_PREPARE(_guard, _fdf, _fd_flags, _file_owned) \
183 struct fd_prepare _guard __cleanup(__fd_prepare_cleanup) = ({ \
184 int __fd = get_unused_fd_flags(_fd_flags); \
185 __fd_prepare(__fd, __fd < 0 ? NULL : (_file_owned)); \
186 }); \
187 const struct fd_prepare *const _fdf = &_guard
188
189 #define FD_PREPARE(_fdf, _fd_flags, _file_owned) \
190 __FD_PREPARE(__UNIQUE_ID(fd_prepare), _fdf, _fd_flags, _file_owned)
191
192 /*
193 * fd_publish - Publish prepared fd and file to the fd table.
194 * @fdf: struct fd_prepare pointer defined by FD_PREPARE()
195 */
fd_publish(const struct fd_prepare * fdf)196 static __always_inline int fd_publish(const struct fd_prepare *fdf)
197 {
198 /* Callers only get a const view, the guard itself is writable. */
199 struct fd_prepare *guard = (struct fd_prepare *)fdf;
200
201 VFS_WARN_ON_ONCE(guard->fd < 0);
202 fd_install(guard->fd, guard->file);
203 return take_fd(guard->fd);
204 }
205
206 /* Do not use directly. */
207 #define __FD_ADD(_fdf, _fd_flags, _file_owned) \
208 ({ \
209 FD_PREPARE(_fdf, _fd_flags, _file_owned); \
210 _fdf->fd < 0 ? _fdf->fd : fd_publish(_fdf); \
211 })
212
213 /*
214 * FD_ADD - Allocate and install an fd and file in one step.
215 * @_fd_flags: flags for get_unused_fd_flags()
216 * @_file_owned: struct file to take ownership of
217 *
218 * Returns the allocated fd number, or negative error code on failure.
219 */
220 #define FD_ADD(_fd_flags, _file_owned) \
221 __FD_ADD(__UNIQUE_ID(fd_prepare), _fd_flags, _file_owned)
222
223 #endif /* __LINUX_FILE_H */
224