2 * descriptor table internals; you almost certainly want file.h instead.
5 #ifndef __LINUX_FDTABLE_H
6 #define __LINUX_FDTABLE_H
8 #include <linux/posix_types.h>
9 #include <linux/compiler.h>
10 #include <linux/spinlock.h>
11 #include <linux/rcupdate.h>
12 #include <linux/nospec.h>
13 #include <linux/types.h>
14 #include <linux/init.h>
17 #include <linux/atomic.h>
20 * The default fd array needs to be at least BITS_PER_LONG,
21 * as this is the granularity returned by copy_fdset().
23 #define NR_OPEN_DEFAULT BITS_PER_LONG
26 * The embedded_fd_set is a small fd_set,
27 * suitable for most tasks (which open <= BITS_PER_LONG files)
29 struct embedded_fd_set {
30 unsigned long fds_bits[1];
35 struct file __rcu **fd; /* current fd array */
36 fd_set *close_on_exec;
42 static inline void __set_close_on_exec(int fd, struct fdtable *fdt)
44 FD_SET(fd, fdt->close_on_exec);
47 static inline void __clear_close_on_exec(int fd, struct fdtable *fdt)
49 FD_CLR(fd, fdt->close_on_exec);
52 static inline bool close_on_exec(int fd, const struct fdtable *fdt)
54 return FD_ISSET(fd, fdt->close_on_exec);
57 static inline void __set_open_fd(int fd, struct fdtable *fdt)
59 FD_SET(fd, fdt->open_fds);
62 static inline void __clear_open_fd(int fd, struct fdtable *fdt)
64 FD_CLR(fd, fdt->open_fds);
67 static inline bool fd_is_open(int fd, const struct fdtable *fdt)
69 return FD_ISSET(fd, fdt->open_fds);
73 * Open file table structure
80 struct fdtable __rcu *fdt;
83 * written part on a separate cache line in SMP
85 spinlock_t file_lock ____cacheline_aligned_in_smp;
87 struct embedded_fd_set close_on_exec_init;
88 struct embedded_fd_set open_fds_init;
89 struct file __rcu * fd_array[NR_OPEN_DEFAULT];
92 #define rcu_dereference_check_fdtable(files, fdtfd) \
93 (rcu_dereference_check((fdtfd), \
94 lockdep_is_held(&(files)->file_lock) || \
95 atomic_read(&(files)->count) == 1 || \
96 rcu_my_thread_group_empty()))
98 #define files_fdtable(files) \
99 (rcu_dereference_check_fdtable((files), (files)->fdt))
101 struct file_operations;
105 extern int expand_files(struct files_struct *, int nr);
106 extern void free_fdtable_rcu(struct rcu_head *rcu);
107 extern void __init files_defer_init(void);
109 static inline void free_fdtable(struct fdtable *fdt)
111 call_rcu(&fdt->rcu, free_fdtable_rcu);
114 static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
116 struct file * file = NULL;
117 struct fdtable *fdt = files_fdtable(files);
119 if (fd < fdt->max_fds) {
120 fd = array_index_nospec(fd, fdt->max_fds);
121 file = rcu_dereference_check_fdtable(files, fdt->fd[fd]);
127 * Check whether the specified fd has an open file.
129 #define fcheck(fd) fcheck_files(current->files, fd)
133 struct files_struct *get_files_struct(struct task_struct *);
134 void put_files_struct(struct files_struct *fs);
135 void reset_files_struct(struct files_struct *);
136 int unshare_files(struct files_struct **);
137 struct files_struct *dup_fd(struct files_struct *, int *);
139 extern struct kmem_cache *files_cachep;
141 #endif /* __LINUX_FDTABLE_H */