file.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. struct file_operations;
  56. struct vfsmount;
  57. struct dentry;
  58. extern int init_file(struct file *, struct vfsmount *mnt,
  59. struct dentry *dentry, mode_t mode,
  60. const struct file_operations *fop);
  61. extern struct file *alloc_file(struct vfsmount *, struct dentry *dentry,
  62. mode_t mode, const struct file_operations *fop);
  63. static inline void fput_light(struct file *file, int fput_needed)
  64. {
  65. if (unlikely(fput_needed))
  66. fput(file);
  67. }
  68. extern struct file * FASTCALL(fget(unsigned int fd));
  69. extern struct file * FASTCALL(fget_light(unsigned int fd, int *fput_needed));
  70. extern void FASTCALL(set_close_on_exec(unsigned int fd, int flag));
  71. extern void put_filp(struct file *);
  72. extern int get_unused_fd(void);
  73. extern int get_unused_fd_flags(int flags);
  74. extern void FASTCALL(put_unused_fd(unsigned int fd));
  75. struct kmem_cache;
  76. extern int expand_files(struct files_struct *, int nr);
  77. extern void free_fdtable_rcu(struct rcu_head *rcu);
  78. extern void __init files_defer_init(void);
  79. static inline void free_fdtable(struct fdtable *fdt)
  80. {
  81. call_rcu(&fdt->rcu, free_fdtable_rcu);
  82. }
  83. static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
  84. {
  85. struct file * file = NULL;
  86. struct fdtable *fdt = files_fdtable(files);
  87. if (fd < fdt->max_fds)
  88. file = rcu_dereference(fdt->fd[fd]);
  89. return file;
  90. }
  91. /*
  92. * Check whether the specified fd has an open file.
  93. */
  94. #define fcheck(fd) fcheck_files(current->files, fd)
  95. extern void FASTCALL(fd_install(unsigned int fd, struct file * file));
  96. struct task_struct;
  97. struct files_struct *get_files_struct(struct task_struct *);
  98. void FASTCALL(put_files_struct(struct files_struct *fs));
  99. void reset_files_struct(struct task_struct *, struct files_struct *);
  100. extern struct kmem_cache *files_cachep;
  101. #endif /* __LINUX_FILE_H */