xref: /openbmc/linux/include/linux/file.h (revision 54da6a09)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Wrapper functions for accessing the file_struct fd array.
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #ifndef __LINUX_FILE_H
71da177e4SLinus Torvalds #define __LINUX_FILE_H
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/compiler.h>
100c9e63fdSEric Dumazet #include <linux/types.h>
119f3acc31SAl Viro #include <linux/posix_types.h>
12deefa7f3SKees Cook #include <linux/errno.h>
13*54da6a09SPeter Zijlstra #include <linux/cleanup.h>
141da177e4SLinus Torvalds 
159f3acc31SAl Viro struct file;
168b7d91ebSChristoph Lameter 
17b3c97528SHarvey Harrison extern void fput(struct file *);
181da177e4SLinus Torvalds 
19ce8d2cdfSDave Hansen struct file_operations;
205e876fb4SSargun Dhillon struct task_struct;
21ce8d2cdfSDave Hansen struct vfsmount;
22ce8d2cdfSDave Hansen struct dentry;
23d93aa9d8SAl Viro struct inode;
242c48b9c4SAl Viro struct path;
25d93aa9d8SAl Viro extern struct file *alloc_file_pseudo(struct inode *, struct vfsmount *,
26d93aa9d8SAl Viro 	const char *, int flags, const struct file_operations *);
27183266f2SAl Viro extern struct file *alloc_file_clone(struct file *, int flags,
28183266f2SAl Viro 	const struct file_operations *);
29ce8d2cdfSDave Hansen 
fput_light(struct file * file,int fput_needed)301da177e4SLinus Torvalds static inline void fput_light(struct file *file, int fput_needed)
311da177e4SLinus Torvalds {
32c2b3e74bSSteven Rostedt 	if (fput_needed)
331da177e4SLinus Torvalds 		fput(file);
341da177e4SLinus Torvalds }
351da177e4SLinus Torvalds 
36a5b470baSAl Viro struct fd {
37a5b470baSAl Viro 	struct file *file;
389c225f26SLinus Torvalds 	unsigned int flags;
39a5b470baSAl Viro };
409c225f26SLinus Torvalds #define FDPUT_FPUT       1
419c225f26SLinus Torvalds #define FDPUT_POS_UNLOCK 2
42a5b470baSAl Viro 
fdput(struct fd fd)43a5b470baSAl Viro static inline void fdput(struct fd fd)
44a5b470baSAl Viro {
459c225f26SLinus Torvalds 	if (fd.flags & FDPUT_FPUT)
46a5b470baSAl Viro 		fput(fd.file);
47a5b470baSAl Viro }
48a5b470baSAl Viro 
49b3c97528SHarvey Harrison extern struct file *fget(unsigned int fd);
50bd2a31d5SAl Viro extern struct file *fget_raw(unsigned int fd);
515e876fb4SSargun Dhillon extern struct file *fget_task(struct task_struct *task, unsigned int fd);
52bd2a31d5SAl Viro extern unsigned long __fdget(unsigned int fd);
53bd2a31d5SAl Viro extern unsigned long __fdget_raw(unsigned int fd);
54bd2a31d5SAl Viro extern unsigned long __fdget_pos(unsigned int fd);
5563b6df14SAl Viro extern void __f_unlock_pos(struct file *);
56bd2a31d5SAl Viro 
__to_fd(unsigned long v)57bd2a31d5SAl Viro static inline struct fd __to_fd(unsigned long v)
58bd2a31d5SAl Viro {
59bd2a31d5SAl Viro 	return (struct fd){(struct file *)(v & ~3),v & 3};
60bd2a31d5SAl Viro }
61a5b470baSAl Viro 
fdget(unsigned int fd)62a5b470baSAl Viro static inline struct fd fdget(unsigned int fd)
63a5b470baSAl Viro {
64bd2a31d5SAl Viro 	return __to_fd(__fdget(fd));
65a5b470baSAl Viro }
66a5b470baSAl Viro 
fdget_raw(unsigned int fd)67a5b470baSAl Viro static inline struct fd fdget_raw(unsigned int fd)
68a5b470baSAl Viro {
69bd2a31d5SAl Viro 	return __to_fd(__fdget_raw(fd));
70a5b470baSAl Viro }
71a5b470baSAl Viro 
fdget_pos(int fd)7263b6df14SAl Viro static inline struct fd fdget_pos(int fd)
7363b6df14SAl Viro {
7463b6df14SAl Viro 	return __to_fd(__fdget_pos(fd));
7563b6df14SAl Viro }
7663b6df14SAl Viro 
fdput_pos(struct fd f)7763b6df14SAl Viro static inline void fdput_pos(struct fd f)
7863b6df14SAl Viro {
7963b6df14SAl Viro 	if (f.flags & FDPUT_POS_UNLOCK)
8063b6df14SAl Viro 		__f_unlock_pos(f.file);
8163b6df14SAl Viro 	fdput(f);
8263b6df14SAl Viro }
8363b6df14SAl Viro 
84*54da6a09SPeter Zijlstra DEFINE_CLASS(fd, struct fd, fdput(_T), fdget(fd), int fd)
85*54da6a09SPeter Zijlstra 
86fe17f22dSAl Viro extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
878280d161SAl Viro extern int replace_fd(unsigned fd, struct file *file, unsigned flags);
88b3c97528SHarvey Harrison extern void set_close_on_exec(unsigned int fd, int flag);
89fe17f22dSAl Viro extern bool get_close_on_exec(unsigned int fd);
904022e7afSJens Axboe extern int __get_unused_fd_flags(unsigned flags, unsigned long nofile);
911a7bd226SAl Viro extern int get_unused_fd_flags(unsigned flags);
92b3c97528SHarvey Harrison extern void put_unused_fd(unsigned int fd);
931da177e4SLinus Torvalds 
94*54da6a09SPeter Zijlstra DEFINE_CLASS(get_unused_fd, int, if (_T >= 0) put_unused_fd(_T),
95*54da6a09SPeter Zijlstra 	     get_unused_fd_flags(flags), unsigned flags)
96*54da6a09SPeter Zijlstra 
97b3c97528SHarvey Harrison extern void fd_install(unsigned int fd, struct file *file);
981da177e4SLinus Torvalds 
9942eb0d54SChristoph Hellwig extern int __receive_fd(struct file *file, int __user *ufd,
10066590610SKees Cook 			unsigned int o_flags);
1019c930054SXie Yongji 
1029c930054SXie Yongji extern int receive_fd(struct file *file, unsigned int o_flags);
1039c930054SXie Yongji 
receive_fd_user(struct file * file,int __user * ufd,unsigned int o_flags)10466590610SKees Cook static inline int receive_fd_user(struct file *file, int __user *ufd,
10566590610SKees Cook 				  unsigned int o_flags)
10666590610SKees Cook {
107deefa7f3SKees Cook 	if (ufd == NULL)
108deefa7f3SKees Cook 		return -EFAULT;
10942eb0d54SChristoph Hellwig 	return __receive_fd(file, ufd, o_flags);
11066590610SKees Cook }
11142eb0d54SChristoph Hellwig int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags);
11266590610SKees Cook 
1134a9d4b02SAl Viro extern void flush_delayed_fput(void);
1144a9d4b02SAl Viro extern void __fput_sync(struct file *);
1154a9d4b02SAl Viro 
1162374c09bSChristoph Hellwig extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
1172374c09bSChristoph Hellwig 
1181da177e4SLinus Torvalds #endif /* __LINUX_FILE_H */
119