file.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Wrapper functions for accessing the file_struct fd array.
  3. */
  4. #ifndef __LINUX_FILE_H
  5. #define __LINUX_FILE_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. extern void FASTCALL(__fput(struct file *));
  54. extern void FASTCALL(fput(struct file *));
  55. static inline void fput_light(struct file *file, int fput_needed)
  56. {
  57. if (unlikely(fput_needed))
  58. fput(file);
  59. }
  60. extern struct file * FASTCALL(fget(unsigned int fd));
  61. extern struct file * FASTCALL(fget_light(unsigned int fd, int *fput_needed));
  62. extern void FASTCALL(set_close_on_exec(unsigned int fd, int flag));
  63. extern void put_filp(struct file *);
  64. extern int get_unused_fd(void);
  65. extern void FASTCALL(put_unused_fd(unsigned int fd));
  66. struct kmem_cache;
  67. extern int expand_files(struct files_struct *, int nr);
  68. extern void free_fdtable_rcu(struct rcu_head *rcu);
  69. extern void __init files_defer_init(void);
  70. static inline void free_fdtable(struct fdtable *fdt)
  71. {
  72. call_rcu(&fdt->rcu, free_fdtable_rcu);
  73. }
  74. static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
  75. {
  76. struct file * file = NULL;
  77. struct fdtable *fdt = files_fdtable(files);
  78. if (fd < fdt->max_fds)
  79. file = rcu_dereference(fdt->fd[fd]);
  80. return file;
  81. }
  82. /*
  83. * Check whether the specified fd has an open file.
  84. */
  85. #define fcheck(fd) fcheck_files(current->files, fd)
  86. extern void FASTCALL(fd_install(unsigned int fd, struct file * file));
  87. struct task_struct;
  88. struct files_struct *get_files_struct(struct task_struct *);
  89. void FASTCALL(put_files_struct(struct files_struct *fs));
  90. void reset_files_struct(struct task_struct *, struct files_struct *);
  91. extern struct kmem_cache *files_cachep;
  92. #endif /* __LINUX_FILE_H */