fdtable.h 2.3 KB

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