fdtable.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. };
  26. static inline bool close_on_exec(int fd, const struct fdtable *fdt)
  27. {
  28. return test_bit(fd, fdt->close_on_exec);
  29. }
  30. static inline bool fd_is_open(int fd, const struct fdtable *fdt)
  31. {
  32. return test_bit(fd, fdt->open_fds);
  33. }
  34. /*
  35. * Open file table structure
  36. */
  37. struct files_struct {
  38. /*
  39. * read mostly part
  40. */
  41. atomic_t count;
  42. struct fdtable __rcu *fdt;
  43. struct fdtable fdtab;
  44. /*
  45. * written part on a separate cache line in SMP
  46. */
  47. spinlock_t file_lock ____cacheline_aligned_in_smp;
  48. int next_fd;
  49. unsigned long close_on_exec_init[1];
  50. unsigned long open_fds_init[1];
  51. struct file __rcu * fd_array[NR_OPEN_DEFAULT];
  52. };
  53. struct file_operations;
  54. struct vfsmount;
  55. struct dentry;
  56. #define rcu_dereference_check_fdtable(files, fdtfd) \
  57. rcu_dereference_check((fdtfd), lockdep_is_held(&(files)->file_lock))
  58. #define files_fdtable(files) \
  59. rcu_dereference_check_fdtable((files), (files)->fdt)
  60. /*
  61. * The caller must ensure that fd table isn't shared or hold rcu or file lock
  62. */
  63. static inline struct file *__fcheck_files(struct files_struct *files, unsigned int fd)
  64. {
  65. struct fdtable *fdt = rcu_dereference_raw(files->fdt);
  66. if (fd < fdt->max_fds)
  67. return rcu_dereference_raw(fdt->fd[fd]);
  68. return NULL;
  69. }
  70. static inline struct file *fcheck_files(struct files_struct *files, unsigned int fd)
  71. {
  72. rcu_lockdep_assert(rcu_read_lock_held() ||
  73. lockdep_is_held(&files->file_lock),
  74. "suspicious rcu_dereference_check() usage");
  75. return __fcheck_files(files, fd);
  76. }
  77. /*
  78. * Check whether the specified fd has an open file.
  79. */
  80. #define fcheck(fd) fcheck_files(current->files, fd)
  81. struct task_struct;
  82. struct files_struct *get_files_struct(struct task_struct *);
  83. void put_files_struct(struct files_struct *fs);
  84. void reset_files_struct(struct files_struct *);
  85. int unshare_files(struct files_struct **);
  86. struct files_struct *dup_fd(struct files_struct *, int *);
  87. void do_close_on_exec(struct files_struct *);
  88. int iterate_fd(struct files_struct *, unsigned,
  89. int (*)(const void *, struct file *, unsigned),
  90. const void *);
  91. extern int __alloc_fd(struct files_struct *files,
  92. unsigned start, unsigned end, unsigned flags);
  93. extern void __fd_install(struct files_struct *files,
  94. unsigned int fd, struct file *file);
  95. extern int __close_fd(struct files_struct *files,
  96. unsigned int fd);
  97. extern struct kmem_cache *files_cachep;
  98. /*M: add fdleak debug log*/
  99. #define FD_OVER_CHECK
  100. #ifdef FD_OVER_CHECK
  101. extern void fd_show_open_files(pid_t pid, struct files_struct *files, struct fdtable *fdt);
  102. #endif
  103. #endif /* __LINUX_FDTABLE_H */