xref: /linux-6.15/include/linux/fdtable.h (revision f60d374d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * descriptor table internals; you almost certainly want file.h instead.
4  */
5 
6 #ifndef __LINUX_FDTABLE_H
7 #define __LINUX_FDTABLE_H
8 
9 #include <linux/posix_types.h>
10 #include <linux/compiler.h>
11 #include <linux/spinlock.h>
12 #include <linux/rcupdate.h>
13 #include <linux/nospec.h>
14 #include <linux/types.h>
15 #include <linux/init.h>
16 #include <linux/fs.h>
17 
18 #include <linux/atomic.h>
19 
20 /*
21  * The default fd array needs to be at least BITS_PER_LONG,
22  * as this is the granularity returned by copy_fdset().
23  */
24 #define NR_OPEN_DEFAULT BITS_PER_LONG
25 #define NR_OPEN_MAX ~0U
26 
27 struct fdtable {
28 	unsigned int max_fds;
29 	struct file __rcu **fd;      /* current fd array */
30 	unsigned long *close_on_exec;
31 	unsigned long *open_fds;
32 	unsigned long *full_fds_bits;
33 	struct rcu_head rcu;
34 };
35 
36 static inline bool fd_is_open(unsigned int fd, const struct fdtable *fdt)
37 {
38 	return test_bit(fd, fdt->open_fds);
39 }
40 
41 /*
42  * Open file table structure
43  */
44 struct files_struct {
45   /*
46    * read mostly part
47    */
48 	atomic_t count;
49 	bool resize_in_progress;
50 	wait_queue_head_t resize_wait;
51 
52 	struct fdtable __rcu *fdt;
53 	struct fdtable fdtab;
54   /*
55    * written part on a separate cache line in SMP
56    */
57 	spinlock_t file_lock ____cacheline_aligned_in_smp;
58 	unsigned int next_fd;
59 	unsigned long close_on_exec_init[1];
60 	unsigned long open_fds_init[1];
61 	unsigned long full_fds_bits_init[1];
62 	struct file __rcu * fd_array[NR_OPEN_DEFAULT];
63 };
64 
65 struct file_operations;
66 struct vfsmount;
67 struct dentry;
68 
69 #define rcu_dereference_check_fdtable(files, fdtfd) \
70 	rcu_dereference_check((fdtfd), lockdep_is_held(&(files)->file_lock))
71 
72 #define files_fdtable(files) \
73 	rcu_dereference_check_fdtable((files), (files)->fdt)
74 
75 /*
76  * The caller must ensure that fd table isn't shared or hold rcu or file lock
77  */
78 static inline struct file *files_lookup_fd_raw(struct files_struct *files, unsigned int fd)
79 {
80 	struct fdtable *fdt = rcu_dereference_raw(files->fdt);
81 	unsigned long mask = array_index_mask_nospec(fd, fdt->max_fds);
82 	struct file *needs_masking;
83 
84 	/*
85 	 * 'mask' is zero for an out-of-bounds fd, all ones for ok.
86 	 * 'fd&mask' is 'fd' for ok, or 0 for out of bounds.
87 	 *
88 	 * Accessing fdt->fd[0] is ok, but needs masking of the result.
89 	 */
90 	needs_masking = rcu_dereference_raw(fdt->fd[fd&mask]);
91 	return (struct file *)(mask & (unsigned long)needs_masking);
92 }
93 
94 static inline struct file *files_lookup_fd_locked(struct files_struct *files, unsigned int fd)
95 {
96 	RCU_LOCKDEP_WARN(!lockdep_is_held(&files->file_lock),
97 			   "suspicious rcu_dereference_check() usage");
98 	return files_lookup_fd_raw(files, fd);
99 }
100 
101 struct file *lookup_fdget_rcu(unsigned int fd);
102 struct file *task_lookup_fdget_rcu(struct task_struct *task, unsigned int fd);
103 struct file *task_lookup_next_fdget_rcu(struct task_struct *task, unsigned int *fd);
104 
105 static inline bool close_on_exec(unsigned int fd, const struct files_struct *files)
106 {
107 	return test_bit(fd, files_fdtable(files)->close_on_exec);
108 }
109 
110 struct task_struct;
111 
112 void put_files_struct(struct files_struct *fs);
113 int unshare_files(void);
114 struct files_struct *dup_fd(struct files_struct *, unsigned, int *) __latent_entropy;
115 void do_close_on_exec(struct files_struct *);
116 int iterate_fd(struct files_struct *, unsigned,
117 		int (*)(const void *, struct file *, unsigned),
118 		const void *);
119 
120 extern int close_fd(unsigned int fd);
121 extern int __close_range(unsigned int fd, unsigned int max_fd, unsigned int flags);
122 extern struct file *file_close_fd(unsigned int fd);
123 extern int unshare_fd(unsigned long unshare_flags, unsigned int max_fds,
124 		      struct files_struct **new_fdp);
125 
126 extern struct kmem_cache *files_cachep;
127 
128 #endif /* __LINUX_FDTABLE_H */
129