linux/include/linux/fdtable.h
<<
>>
Prefs
   1/*
   2 * descriptor table internals; you almost certainly want file.h instead.
   3 */
   4
   5#ifndef __LINUX_FDTABLE_H
   6#define __LINUX_FDTABLE_H
   7
   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>
  15#include <linux/fs.h>
  16
  17#include <linux/atomic.h>
  18
  19/*
  20 * The default fd array needs to be at least BITS_PER_LONG,
  21 * as this is the granularity returned by copy_fdset().
  22 */
  23#define NR_OPEN_DEFAULT BITS_PER_LONG
  24
  25struct fdtable {
  26        unsigned int max_fds;
  27        struct file __rcu **fd;      /* current fd array */
  28        unsigned long *close_on_exec;
  29        unsigned long *open_fds;
  30#ifdef __GENKSYMS__
  31        struct rcu_head rcu;
  32#else
  33        union {
  34                struct rcu_head rcu;
  35                unsigned long *full_fds_bits;
  36        };
  37#endif
  38};
  39
  40static inline bool close_on_exec(int fd, const struct fdtable *fdt)
  41{
  42        return test_bit(fd, fdt->close_on_exec);
  43}
  44
  45static inline bool fd_is_open(int fd, const struct fdtable *fdt)
  46{
  47        return test_bit(fd, fdt->open_fds);
  48}
  49
  50/*
  51 * Open file table structure
  52 */
  53struct files_struct {
  54  /*
  55   * read mostly part
  56   */
  57        atomic_t count;
  58        RH_KABI_FILL_HOLE(bool resize_in_progress)
  59        struct fdtable __rcu *fdt;
  60        struct fdtable fdtab;
  61  /*
  62   * written part on a separate cache line in SMP
  63   */
  64        spinlock_t file_lock ____cacheline_aligned_in_smp;
  65        int next_fd;
  66        unsigned long close_on_exec_init[1];
  67        unsigned long open_fds_init[1];
  68        struct file __rcu * fd_array[NR_OPEN_DEFAULT];
  69        RH_KABI_EXTEND(unsigned long full_fds_bits_init[1])
  70        RH_KABI_EXTEND(wait_queue_head_t resize_wait)
  71};
  72
  73#define rcu_dereference_check_fdtable(files, fdtfd) \
  74        (rcu_dereference_check((fdtfd), \
  75                               lockdep_is_held(&(files)->file_lock) || \
  76                               atomic_read(&(files)->count) == 1 || \
  77                               rcu_my_thread_group_empty()))
  78
  79#define files_fdtable(files) \
  80                (rcu_dereference_check_fdtable((files), (files)->fdt))
  81
  82struct file_operations;
  83struct vfsmount;
  84struct dentry;
  85
  86static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
  87{
  88        struct file * file = NULL;
  89        struct fdtable *fdt = files_fdtable(files);
  90
  91        if (fd < fdt->max_fds) {
  92                fd = array_index_nospec(fd, fdt->max_fds);
  93                file = rcu_dereference_check_fdtable(files, fdt->fd[fd]);
  94        }
  95        return file;
  96}
  97
  98/*
  99 * Check whether the specified fd has an open file.
 100 */
 101#define fcheck(fd)      fcheck_files(current->files, fd)
 102
 103struct task_struct;
 104
 105struct files_struct *get_files_struct(struct task_struct *);
 106void put_files_struct(struct files_struct *fs);
 107void reset_files_struct(struct files_struct *);
 108int unshare_files(struct files_struct **);
 109struct files_struct *dup_fd(struct files_struct *, int *);
 110void do_close_on_exec(struct files_struct *);
 111int iterate_fd(struct files_struct *, unsigned,
 112                int (*)(const void *, struct file *, unsigned),
 113                const void *);
 114
 115extern int __alloc_fd(struct files_struct *files,
 116                      unsigned start, unsigned end, unsigned flags);
 117extern void __fd_install(struct files_struct *files,
 118                      unsigned int fd, struct file *file);
 119extern int __close_fd(struct files_struct *files,
 120                      unsigned int fd);
 121
 122extern struct kmem_cache *files_cachep;
 123
 124#endif /* __LINUX_FDTABLE_H */
 125