file.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /*
  12. * The default fd array needs to be at least BITS_PER_LONG,
  13. * as this is the granularity returned by copy_fdset().
  14. */
  15. #define NR_OPEN_DEFAULT BITS_PER_LONG
  16. struct fdtable {
  17. unsigned int max_fds;
  18. int max_fdset;
  19. int next_fd;
  20. struct file ** fd; /* current fd array */
  21. fd_set *close_on_exec;
  22. fd_set *open_fds;
  23. struct rcu_head rcu;
  24. struct files_struct *free_files;
  25. struct fdtable *next;
  26. };
  27. /*
  28. * Open file table structure
  29. */
  30. struct files_struct {
  31. atomic_t count;
  32. spinlock_t file_lock; /* Protects all the below members. Nests inside tsk->alloc_lock */
  33. struct fdtable *fdt;
  34. struct fdtable fdtab;
  35. fd_set close_on_exec_init;
  36. fd_set open_fds_init;
  37. struct file * fd_array[NR_OPEN_DEFAULT];
  38. };
  39. #define files_fdtable(files) (rcu_dereference((files)->fdt))
  40. extern void FASTCALL(__fput(struct file *));
  41. extern void FASTCALL(fput(struct file *));
  42. static inline void fput_light(struct file *file, int fput_needed)
  43. {
  44. if (unlikely(fput_needed))
  45. fput(file);
  46. }
  47. extern struct file * FASTCALL(fget(unsigned int fd));
  48. extern struct file * FASTCALL(fget_light(unsigned int fd, int *fput_needed));
  49. extern void FASTCALL(set_close_on_exec(unsigned int fd, int flag));
  50. extern void put_filp(struct file *);
  51. extern int get_unused_fd(void);
  52. extern void FASTCALL(put_unused_fd(unsigned int fd));
  53. struct kmem_cache_s;
  54. extern void filp_ctor(void * objp, struct kmem_cache_s *cachep, unsigned long cflags);
  55. extern void filp_dtor(void * objp, struct kmem_cache_s *cachep, unsigned long dflags);
  56. extern struct file ** alloc_fd_array(int);
  57. extern void free_fd_array(struct file **, int);
  58. extern fd_set *alloc_fdset(int);
  59. extern void free_fdset(fd_set *, int);
  60. extern int expand_files(struct files_struct *, int nr);
  61. extern void free_fdtable(struct fdtable *fdt);
  62. extern void __init files_defer_init(void);
  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. extern void FASTCALL(fd_install(unsigned int fd, struct file * file));
  76. struct task_struct;
  77. struct files_struct *get_files_struct(struct task_struct *);
  78. void FASTCALL(put_files_struct(struct files_struct *fs));
  79. #endif /* __LINUX_FILE_H */