Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * descriptor table internals; you almost certainly want file.h instead.
0004  */
0005 
0006 #ifndef __LINUX_FDTABLE_H
0007 #define __LINUX_FDTABLE_H
0008 
0009 #include <linux/posix_types.h>
0010 #include <linux/compiler.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/rcupdate.h>
0013 #include <linux/nospec.h>
0014 #include <linux/types.h>
0015 #include <linux/init.h>
0016 #include <linux/fs.h>
0017 
0018 #include <linux/atomic.h>
0019 
0020 /*
0021  * The default fd array needs to be at least BITS_PER_LONG,
0022  * as this is the granularity returned by copy_fdset().
0023  */
0024 #define NR_OPEN_DEFAULT BITS_PER_LONG
0025 #define NR_OPEN_MAX ~0U
0026 
0027 struct fdtable {
0028     unsigned int max_fds;
0029     struct file __rcu **fd;      /* current fd array */
0030     unsigned long *close_on_exec;
0031     unsigned long *open_fds;
0032     unsigned long *full_fds_bits;
0033     struct rcu_head rcu;
0034 };
0035 
0036 static inline bool close_on_exec(unsigned int fd, const struct fdtable *fdt)
0037 {
0038     return test_bit(fd, fdt->close_on_exec);
0039 }
0040 
0041 static inline bool fd_is_open(unsigned int fd, const struct fdtable *fdt)
0042 {
0043     return test_bit(fd, fdt->open_fds);
0044 }
0045 
0046 /*
0047  * Open file table structure
0048  */
0049 struct files_struct {
0050   /*
0051    * read mostly part
0052    */
0053     atomic_t count;
0054     bool resize_in_progress;
0055     wait_queue_head_t resize_wait;
0056 
0057     struct fdtable __rcu *fdt;
0058     struct fdtable fdtab;
0059   /*
0060    * written part on a separate cache line in SMP
0061    */
0062     spinlock_t file_lock ____cacheline_aligned_in_smp;
0063     unsigned int next_fd;
0064     unsigned long close_on_exec_init[1];
0065     unsigned long open_fds_init[1];
0066     unsigned long full_fds_bits_init[1];
0067     struct file __rcu * fd_array[NR_OPEN_DEFAULT];
0068 };
0069 
0070 struct file_operations;
0071 struct vfsmount;
0072 struct dentry;
0073 
0074 #define rcu_dereference_check_fdtable(files, fdtfd) \
0075     rcu_dereference_check((fdtfd), lockdep_is_held(&(files)->file_lock))
0076 
0077 #define files_fdtable(files) \
0078     rcu_dereference_check_fdtable((files), (files)->fdt)
0079 
0080 /*
0081  * The caller must ensure that fd table isn't shared or hold rcu or file lock
0082  */
0083 static inline struct file *files_lookup_fd_raw(struct files_struct *files, unsigned int fd)
0084 {
0085     struct fdtable *fdt = rcu_dereference_raw(files->fdt);
0086 
0087     if (fd < fdt->max_fds) {
0088         fd = array_index_nospec(fd, fdt->max_fds);
0089         return rcu_dereference_raw(fdt->fd[fd]);
0090     }
0091     return NULL;
0092 }
0093 
0094 static inline struct file *files_lookup_fd_locked(struct files_struct *files, unsigned int fd)
0095 {
0096     RCU_LOCKDEP_WARN(!lockdep_is_held(&files->file_lock),
0097                "suspicious rcu_dereference_check() usage");
0098     return files_lookup_fd_raw(files, fd);
0099 }
0100 
0101 static inline struct file *files_lookup_fd_rcu(struct files_struct *files, unsigned int fd)
0102 {
0103     RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
0104                "suspicious rcu_dereference_check() usage");
0105     return files_lookup_fd_raw(files, fd);
0106 }
0107 
0108 static inline struct file *lookup_fd_rcu(unsigned int fd)
0109 {
0110     return files_lookup_fd_rcu(current->files, fd);
0111 }
0112 
0113 struct file *task_lookup_fd_rcu(struct task_struct *task, unsigned int fd);
0114 struct file *task_lookup_next_fd_rcu(struct task_struct *task, unsigned int *fd);
0115 
0116 struct task_struct;
0117 
0118 void put_files_struct(struct files_struct *fs);
0119 int unshare_files(void);
0120 struct files_struct *dup_fd(struct files_struct *, unsigned, int *) __latent_entropy;
0121 void do_close_on_exec(struct files_struct *);
0122 int iterate_fd(struct files_struct *, unsigned,
0123         int (*)(const void *, struct file *, unsigned),
0124         const void *);
0125 
0126 extern int close_fd(unsigned int fd);
0127 extern int __close_range(unsigned int fd, unsigned int max_fd, unsigned int flags);
0128 extern struct file *close_fd_get_file(unsigned int fd);
0129 extern int unshare_fd(unsigned long unshare_flags, unsigned int max_fds,
0130               struct files_struct **new_fdp);
0131 
0132 extern struct kmem_cache *files_cachep;
0133 
0134 #endif /* __LINUX_FDTABLE_H */