fdtable.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /*
  20. * The embedded_fd_set is a small fd_set,
  21. * suitable for most tasks (which open <= BITS_PER_LONG files)
  22. */
  23. struct embedded_fd_set {
  24. unsigned long fds_bits[1];
  25. };
  26. struct fdtable {
  27. unsigned int max_fds;
  28. struct file __rcu **fd; /* current fd array */
  29. fd_set *close_on_exec;
  30. fd_set *open_fds;
  31. struct rcu_head rcu;
  32. struct fdtable *next;
  33. };
  34. static inline void __set_close_on_exec(int fd, struct fdtable *fdt)
  35. {
  36. FD_SET(fd, fdt->close_on_exec);
  37. }
  38. static inline void __clear_close_on_exec(int fd, struct fdtable *fdt)
  39. {
  40. FD_CLR(fd, fdt->close_on_exec);
  41. }
  42. static inline bool close_on_exec(int fd, const struct fdtable *fdt)
  43. {
  44. return FD_ISSET(fd, fdt->close_on_exec);
  45. }
  46. static inline void __set_open_fd(int fd, struct fdtable *fdt)
  47. {
  48. FD_SET(fd, fdt->open_fds);
  49. }
  50. static inline void __clear_open_fd(int fd, struct fdtable *fdt)
  51. {
  52. FD_CLR(fd, fdt->open_fds);
  53. }
  54. static inline bool fd_is_open(int fd, const struct fdtable *fdt)
  55. {
  56. return FD_ISSET(fd, fdt->open_fds);
  57. }
  58. /*
  59. * Open file table structure
  60. */
  61. struct files_struct {
  62. /*
  63. * read mostly part
  64. */
  65. atomic_t count;
  66. struct fdtable __rcu *fdt;
  67. struct fdtable fdtab;
  68. /*
  69. * written part on a separate cache line in SMP
  70. */
  71. spinlock_t file_lock ____cacheline_aligned_in_smp;
  72. int next_fd;
  73. struct embedded_fd_set close_on_exec_init;
  74. struct embedded_fd_set open_fds_init;
  75. struct file __rcu * fd_array[NR_OPEN_DEFAULT];
  76. };
  77. #define rcu_dereference_check_fdtable(files, fdtfd) \
  78. (rcu_dereference_check((fdtfd), \
  79. lockdep_is_held(&(files)->file_lock) || \
  80. atomic_read(&(files)->count) == 1 || \
  81. rcu_my_thread_group_empty()))
  82. #define files_fdtable(files) \
  83. (rcu_dereference_check_fdtable((files), (files)->fdt))
  84. struct file_operations;
  85. struct vfsmount;
  86. struct dentry;
  87. extern int expand_files(struct files_struct *, int nr);
  88. extern void free_fdtable_rcu(struct rcu_head *rcu);
  89. extern void __init files_defer_init(void);
  90. static inline void free_fdtable(struct fdtable *fdt)
  91. {
  92. call_rcu(&fdt->rcu, free_fdtable_rcu);
  93. }
  94. static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
  95. {
  96. struct file * file = NULL;
  97. struct fdtable *fdt = files_fdtable(files);
  98. if (fd < fdt->max_fds)
  99. file = rcu_dereference_check_fdtable(files, fdt->fd[fd]);
  100. return file;
  101. }
  102. /*
  103. * Check whether the specified fd has an open file.
  104. */
  105. #define fcheck(fd) fcheck_files(current->files, fd)
  106. struct task_struct;
  107. struct files_struct *get_files_struct(struct task_struct *);
  108. void put_files_struct(struct files_struct *fs);
  109. void reset_files_struct(struct files_struct *);
  110. int unshare_files(struct files_struct **);
  111. struct files_struct *dup_fd(struct files_struct *, int *);
  112. extern struct kmem_cache *files_cachep;
  113. #endif /* __LINUX_FDTABLE_H */