fdtable.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <asm/atomic.h>
  13. /*
  14. * The default fd array needs to be at least BITS_PER_LONG,
  15. * as this is the granularity returned by copy_fdset().
  16. */
  17. #define NR_OPEN_DEFAULT BITS_PER_LONG
  18. /*
  19. * The embedded_fd_set is a small fd_set,
  20. * suitable for most tasks (which open <= BITS_PER_LONG files)
  21. */
  22. struct embedded_fd_set {
  23. unsigned long fds_bits[1];
  24. };
  25. struct fdtable {
  26. unsigned int max_fds;
  27. struct file ** fd; /* current fd array */
  28. fd_set *close_on_exec;
  29. fd_set *open_fds;
  30. struct rcu_head rcu;
  31. struct fdtable *next;
  32. };
  33. /*
  34. * Open file table structure
  35. */
  36. struct files_struct {
  37. /*
  38. * read mostly part
  39. */
  40. atomic_t count;
  41. struct fdtable *fdt;
  42. struct fdtable fdtab;
  43. /*
  44. * written part on a separate cache line in SMP
  45. */
  46. spinlock_t file_lock ____cacheline_aligned_in_smp;
  47. int next_fd;
  48. struct embedded_fd_set close_on_exec_init;
  49. struct embedded_fd_set open_fds_init;
  50. struct file * fd_array[NR_OPEN_DEFAULT];
  51. };
  52. #define rcu_dereference_check_fdtable(files, fdtfd) \
  53. (rcu_dereference_check((fdtfd), \
  54. rcu_read_lock_held() || \
  55. lockdep_is_held(&(files)->file_lock) || \
  56. atomic_read(&(files)->count) == 1))
  57. #define files_fdtable(files) \
  58. (rcu_dereference_check_fdtable((files), (files)->fdt))
  59. struct file_operations;
  60. struct vfsmount;
  61. struct dentry;
  62. extern int expand_files(struct files_struct *, int nr);
  63. extern void free_fdtable_rcu(struct rcu_head *rcu);
  64. extern void __init files_defer_init(void);
  65. static inline void free_fdtable(struct fdtable *fdt)
  66. {
  67. call_rcu(&fdt->rcu, free_fdtable_rcu);
  68. }
  69. static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
  70. {
  71. struct file * file = NULL;
  72. struct fdtable *fdt = files_fdtable(files);
  73. if (fd < fdt->max_fds)
  74. file = rcu_dereference_check_fdtable(files, fdt->fd[fd]);
  75. return file;
  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. extern struct kmem_cache *files_cachep;
  88. #endif /* __LINUX_FDTABLE_H */