fdtable.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * descriptor table internals; you almost certainly want file.h instead.
  3. */
  4. #ifndef __LINUX_FDTABLE_H
  5. #define __LINUX_FDTABLE_H
  6. #include <linux/posix_types.h>
  7. #include <linux/compiler.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/rcupdate.h>
  10. #include <linux/types.h>
  11. #include <linux/init.h>
  12. #include <linux/fs.h>
  13. #include <linux/atomic.h>
  14. /*
  15. * The default fd array needs to be at least BITS_PER_LONG,
  16. * as this is the granularity returned by copy_fdset().
  17. */
  18. #define NR_OPEN_DEFAULT BITS_PER_LONG
  19. struct fdtable {
  20. unsigned int max_fds;
  21. struct file __rcu **fd; /* current fd array */
  22. unsigned long *close_on_exec;
  23. unsigned long *open_fds;
  24. struct rcu_head rcu;
  25. struct fdtable *next;
  26. };
  27. static inline bool close_on_exec(int fd, const struct fdtable *fdt)
  28. {
  29. return test_bit(fd, fdt->close_on_exec);
  30. }
  31. static inline bool fd_is_open(int fd, const struct fdtable *fdt)
  32. {
  33. return test_bit(fd, fdt->open_fds);
  34. }
  35. /*
  36. * Open file table structure
  37. */
  38. struct files_struct {
  39. /*
  40. * read mostly part
  41. */
  42. atomic_t count;
  43. struct fdtable __rcu *fdt;
  44. struct fdtable fdtab;
  45. /*
  46. * written part on a separate cache line in SMP
  47. */
  48. spinlock_t file_lock ____cacheline_aligned_in_smp;
  49. int next_fd;
  50. unsigned long close_on_exec_init[1];
  51. unsigned long open_fds_init[1];
  52. struct file __rcu * fd_array[NR_OPEN_DEFAULT];
  53. };
  54. #define rcu_dereference_check_fdtable(files, fdtfd) \
  55. (rcu_dereference_check((fdtfd), \
  56. lockdep_is_held(&(files)->file_lock) || \
  57. atomic_read(&(files)->count) == 1 || \
  58. rcu_my_thread_group_empty()))
  59. #define files_fdtable(files) \
  60. (rcu_dereference_check_fdtable((files), (files)->fdt))
  61. struct file_operations;
  62. struct vfsmount;
  63. struct dentry;
  64. extern void __init files_defer_init(void);
  65. static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
  66. {
  67. struct file * file = NULL;
  68. struct fdtable *fdt = files_fdtable(files);
  69. if (fd < fdt->max_fds)
  70. file = rcu_dereference_check_fdtable(files, fdt->fd[fd]);
  71. return file;
  72. }
  73. /*
  74. * Check whether the specified fd has an open file.
  75. */
  76. #define fcheck(fd) fcheck_files(current->files, fd)
  77. struct task_struct;
  78. struct files_struct *get_files_struct(struct task_struct *);
  79. void put_files_struct(struct files_struct *fs);
  80. void reset_files_struct(struct files_struct *);
  81. int unshare_files(struct files_struct **);
  82. struct files_struct *dup_fd(struct files_struct *, int *);
  83. void do_close_on_exec(struct files_struct *);
  84. extern int __alloc_fd(struct files_struct *files,
  85. unsigned start, unsigned end, unsigned flags);
  86. extern void __fd_install(struct files_struct *files,
  87. unsigned int fd, struct file *file);
  88. extern int __close_fd(struct files_struct *files,
  89. unsigned int fd);
  90. extern struct kmem_cache *files_cachep;
  91. #endif /* __LINUX_FDTABLE_H */