file.h 3.1 KB

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