file.h 2.4 KB

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