2017-11-01 15:07:57 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
|
* Wrapper functions for accessing the file_struct fd array.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef __LINUX_FILE_H
|
|
|
|
|
#define __LINUX_FILE_H
|
|
|
|
|
|
|
|
|
|
#include <linux/compiler.h>
|
2006-03-23 03:00:12 -08:00
|
|
|
#include <linux/types.h>
|
2008-04-24 07:44:08 -04:00
|
|
|
#include <linux/posix_types.h>
|
2020-06-10 20:47:45 -07:00
|
|
|
#include <linux/errno.h>
|
2023-05-26 12:23:48 +02:00
|
|
|
#include <linux/cleanup.h>
|
2024-07-19 13:41:51 +02:00
|
|
|
#include <linux/err.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-04-24 07:44:08 -04:00
|
|
|
struct file;
|
2006-12-06 20:32:52 -08:00
|
|
|
|
2008-02-13 15:03:15 -08:00
|
|
|
extern void fput(struct file *);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-10-16 23:31:13 -07:00
|
|
|
struct file_operations;
|
2020-01-07 09:59:24 -08:00
|
|
|
struct task_struct;
|
2007-10-16 23:31:13 -07:00
|
|
|
struct vfsmount;
|
|
|
|
|
struct dentry;
|
2018-06-09 09:40:05 -04:00
|
|
|
struct inode;
|
2009-08-09 00:52:35 +04:00
|
|
|
struct path;
|
2018-06-09 09:40:05 -04:00
|
|
|
extern struct file *alloc_file_pseudo(struct inode *, struct vfsmount *,
|
|
|
|
|
const char *, int flags, const struct file_operations *);
|
2024-02-08 19:10:45 +01:00
|
|
|
extern struct file *alloc_file_pseudo_noaccount(struct inode *, struct vfsmount *,
|
|
|
|
|
const char *, int flags, const struct file_operations *);
|
2018-06-17 14:15:10 -04:00
|
|
|
extern struct file *alloc_file_clone(struct file *, int flags,
|
|
|
|
|
const struct file_operations *);
|
2007-10-16 23:31:13 -07:00
|
|
|
|
2024-05-31 15:45:12 -04:00
|
|
|
/* either a reference to struct file + flags
|
|
|
|
|
* (cloned vs. borrowed, pos locked), with
|
|
|
|
|
* flags stored in lower bits of value,
|
|
|
|
|
* or empty (represented by 0).
|
|
|
|
|
*/
|
2012-08-27 19:55:01 -04:00
|
|
|
struct fd {
|
2024-05-31 15:45:12 -04:00
|
|
|
unsigned long word;
|
2012-08-27 19:55:01 -04:00
|
|
|
};
|
2014-03-03 09:36:58 -08:00
|
|
|
#define FDPUT_FPUT 1
|
|
|
|
|
#define FDPUT_POS_UNLOCK 2
|
2012-08-27 19:55:01 -04:00
|
|
|
|
2024-05-31 15:45:12 -04:00
|
|
|
#define fd_file(f) ((struct file *)((f).word & ~(FDPUT_FPUT|FDPUT_POS_UNLOCK)))
|
|
|
|
|
static inline bool fd_empty(struct fd f)
|
|
|
|
|
{
|
|
|
|
|
return unlikely(!f.word);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define EMPTY_FD (struct fd){0}
|
2024-05-31 16:34:25 -04:00
|
|
|
static inline struct fd BORROWED_FD(struct file *f)
|
|
|
|
|
{
|
|
|
|
|
return (struct fd){(unsigned long)f};
|
|
|
|
|
}
|
|
|
|
|
static inline struct fd CLONED_FD(struct file *f)
|
|
|
|
|
{
|
|
|
|
|
return (struct fd){(unsigned long)f | FDPUT_FPUT};
|
|
|
|
|
}
|
2024-05-31 14:12:01 -04:00
|
|
|
|
2012-08-27 19:55:01 -04:00
|
|
|
static inline void fdput(struct fd fd)
|
|
|
|
|
{
|
2025-04-07 01:58:05 +02:00
|
|
|
if (unlikely(fd.word & FDPUT_FPUT))
|
2024-05-31 14:12:01 -04:00
|
|
|
fput(fd_file(fd));
|
2012-08-27 19:55:01 -04:00
|
|
|
}
|
|
|
|
|
|
2008-02-13 15:03:15 -08:00
|
|
|
extern struct file *fget(unsigned int fd);
|
2014-03-04 14:54:22 -05:00
|
|
|
extern struct file *fget_raw(unsigned int fd);
|
2020-01-07 09:59:24 -08:00
|
|
|
extern struct file *fget_task(struct task_struct *task, unsigned int fd);
|
2024-07-31 11:49:04 -04:00
|
|
|
extern struct file *fget_task_next(struct task_struct *task, unsigned int *fd);
|
2016-04-20 17:08:21 -04:00
|
|
|
extern void __f_unlock_pos(struct file *);
|
2014-03-04 14:54:22 -05:00
|
|
|
|
2024-05-31 16:34:25 -04:00
|
|
|
struct fd fdget(unsigned int fd);
|
|
|
|
|
struct fd fdget_raw(unsigned int fd);
|
|
|
|
|
struct fd fdget_pos(unsigned int fd);
|
2016-04-20 17:08:21 -04:00
|
|
|
|
|
|
|
|
static inline void fdput_pos(struct fd f)
|
|
|
|
|
{
|
2024-05-31 15:45:12 -04:00
|
|
|
if (f.word & FDPUT_POS_UNLOCK)
|
2024-05-31 14:12:01 -04:00
|
|
|
__f_unlock_pos(fd_file(f));
|
2016-04-20 17:08:21 -04:00
|
|
|
fdput(f);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 12:23:48 +02:00
|
|
|
DEFINE_CLASS(fd, struct fd, fdput(_T), fdget(fd), int fd)
|
2024-05-09 13:02:39 +02:00
|
|
|
DEFINE_CLASS(fd_raw, struct fd, fdput(_T), fdget_raw(fd), int fd)
|
2024-05-31 22:10:12 -04:00
|
|
|
DEFINE_CLASS(fd_pos, struct fd, fdput_pos(_T), fdget_pos(fd), int fd)
|
2023-05-26 12:23:48 +02:00
|
|
|
|
2012-08-21 11:48:11 -04:00
|
|
|
extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
|
2012-08-21 12:11:46 -04:00
|
|
|
extern int replace_fd(unsigned fd, struct file *file, unsigned flags);
|
2008-02-13 15:03:15 -08:00
|
|
|
extern void set_close_on_exec(unsigned int fd, int flag);
|
2012-08-21 11:48:11 -04:00
|
|
|
extern bool get_close_on_exec(unsigned int fd);
|
2020-03-19 19:23:18 -06:00
|
|
|
extern int __get_unused_fd_flags(unsigned flags, unsigned long nofile);
|
2012-08-12 17:18:05 -04:00
|
|
|
extern int get_unused_fd_flags(unsigned flags);
|
2008-02-13 15:03:15 -08:00
|
|
|
extern void put_unused_fd(unsigned int fd);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2023-05-26 12:23:48 +02:00
|
|
|
DEFINE_CLASS(get_unused_fd, int, if (_T >= 0) put_unused_fd(_T),
|
|
|
|
|
get_unused_fd_flags(flags), unsigned flags)
|
2024-07-19 13:41:51 +02:00
|
|
|
DEFINE_FREE(fput, struct file *, if (!IS_ERR_OR_NULL(_T)) fput(_T))
|
2023-05-26 12:23:48 +02:00
|
|
|
|
2024-06-27 16:11:39 +02:00
|
|
|
/*
|
|
|
|
|
* take_fd() will take care to set @fd to -EBADF ensuring that
|
|
|
|
|
* CLASS(get_unused_fd) won't call put_unused_fd(). This makes it
|
|
|
|
|
* easier to rely on CLASS(get_unused_fd):
|
|
|
|
|
*
|
|
|
|
|
* struct file *f;
|
|
|
|
|
*
|
|
|
|
|
* CLASS(get_unused_fd, fd)(O_CLOEXEC);
|
|
|
|
|
* if (fd < 0)
|
|
|
|
|
* return fd;
|
|
|
|
|
*
|
|
|
|
|
* f = dentry_open(&path, O_RDONLY, current_cred());
|
|
|
|
|
* if (IS_ERR(f))
|
2024-08-09 15:50:35 +02:00
|
|
|
* return PTR_ERR(f);
|
2024-06-27 16:11:39 +02:00
|
|
|
*
|
|
|
|
|
* fd_install(fd, f);
|
|
|
|
|
* return take_fd(fd);
|
|
|
|
|
*/
|
|
|
|
|
#define take_fd(fd) __get_and_null(fd, -EBADF)
|
|
|
|
|
|
2008-02-13 15:03:15 -08:00
|
|
|
extern void fd_install(unsigned int fd, struct file *file);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2023-11-30 13:49:11 +01:00
|
|
|
int receive_fd(struct file *file, int __user *ufd, unsigned int o_flags);
|
2021-08-31 18:36:24 +08:00
|
|
|
|
2021-03-25 09:22:09 +01:00
|
|
|
int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags);
|
2020-06-10 08:20:05 -07:00
|
|
|
|
2012-06-24 09:56:45 +04:00
|
|
|
extern void flush_delayed_fput(void);
|
|
|
|
|
extern void __fput_sync(struct file *);
|
|
|
|
|
|
2020-04-24 08:43:36 +02:00
|
|
|
extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
#endif /* __LINUX_FILE_H */
|