file.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /*
  25. * More than this number of fds: we use a separately allocated fd_set
  26. */
  27. #define EMBEDDED_FD_SET_SIZE (BITS_PER_BYTE * sizeof(struct embedded_fd_set))
  28. struct fdtable {
  29. unsigned int max_fds;
  30. int max_fdset;
  31. struct file ** fd; /* current fd array */
  32. fd_set *close_on_exec;
  33. fd_set *open_fds;
  34. struct rcu_head rcu;
  35. struct files_struct *free_files;
  36. struct fdtable *next;
  37. };
  38. /*
  39. * Open file table structure
  40. */
  41. struct files_struct {
  42. /*
  43. * read mostly part
  44. */
  45. atomic_t count;
  46. struct fdtable *fdt;
  47. struct fdtable fdtab;
  48. /*
  49. * written part on a separate cache line in SMP
  50. */
  51. spinlock_t file_lock ____cacheline_aligned_in_smp;
  52. int next_fd;
  53. struct embedded_fd_set close_on_exec_init;
  54. struct embedded_fd_set open_fds_init;
  55. struct file * fd_array[NR_OPEN_DEFAULT];
  56. };
  57. #define files_fdtable(files) (rcu_dereference((files)->fdt))
  58. extern void FASTCALL(__fput(struct file *));
  59. extern void FASTCALL(fput(struct file *));
  60. static inline void fput_light(struct file *file, int fput_needed)
  61. {
  62. if (unlikely(fput_needed))
  63. fput(file);
  64. }
  65. extern struct file * FASTCALL(fget(unsigned int fd));
  66. extern struct file * FASTCALL(fget_light(unsigned int fd, int *fput_needed));
  67. extern void FASTCALL(set_close_on_exec(unsigned int fd, int flag));
  68. extern void put_filp(struct file *);
  69. extern int get_unused_fd(void);
  70. extern void FASTCALL(put_unused_fd(unsigned int fd));
  71. struct kmem_cache;
  72. extern struct file ** alloc_fd_array(int);
  73. extern void free_fd_array(struct file **, int);
  74. extern fd_set *alloc_fdset(int);
  75. extern void free_fdset(fd_set *, int);
  76. extern int expand_files(struct files_struct *, int nr);
  77. extern void free_fdtable(struct fdtable *fdt);
  78. extern void __init files_defer_init(void);
  79. static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
  80. {
  81. struct file * file = NULL;
  82. struct fdtable *fdt = files_fdtable(files);
  83. if (fd < fdt->max_fds)
  84. file = rcu_dereference(fdt->fd[fd]);
  85. return file;
  86. }
  87. /*
  88. * Check whether the specified fd has an open file.
  89. */
  90. #define fcheck(fd) fcheck_files(current->files, fd)
  91. extern void FASTCALL(fd_install(unsigned int fd, struct file * file));
  92. struct task_struct;
  93. struct files_struct *get_files_struct(struct task_struct *);
  94. void FASTCALL(put_files_struct(struct files_struct *fs));
  95. #endif /* __LINUX_FILE_H */