file.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Wrapper functions for accessing the file_struct fd array.
  4. */
  5. #ifndef __LINUX_FILE_H
  6. #define __LINUX_FILE_H
  7. #include <linux/compiler.h>
  8. #include <linux/types.h>
  9. #include <linux/posix_types.h>
  10. #include <linux/errno.h>
  11. struct file;
  12. extern void fput(struct file *);
  13. struct file_operations;
  14. struct task_struct;
  15. struct vfsmount;
  16. struct dentry;
  17. struct inode;
  18. struct path;
  19. extern struct file *alloc_file_pseudo(struct inode *, struct vfsmount *,
  20. const char *, int flags, const struct file_operations *);
  21. extern struct file *alloc_file_clone(struct file *, int flags,
  22. const struct file_operations *);
  23. static inline void fput_light(struct file *file, int fput_needed)
  24. {
  25. if (fput_needed)
  26. fput(file);
  27. }
  28. struct fd {
  29. struct file *file;
  30. unsigned int flags;
  31. };
  32. #define FDPUT_FPUT 1
  33. #define FDPUT_POS_UNLOCK 2
  34. static inline void fdput(struct fd fd)
  35. {
  36. if (fd.flags & FDPUT_FPUT)
  37. fput(fd.file);
  38. }
  39. extern struct file *fget(unsigned int fd);
  40. extern struct file *fget_raw(unsigned int fd);
  41. extern struct file *fget_task(struct task_struct *task, unsigned int fd);
  42. extern unsigned long __fdget(unsigned int fd);
  43. extern unsigned long __fdget_raw(unsigned int fd);
  44. extern unsigned long __fdget_pos(unsigned int fd);
  45. extern void __f_unlock_pos(struct file *);
  46. static inline struct fd __to_fd(unsigned long v)
  47. {
  48. return (struct fd){(struct file *)(v & ~3),v & 3};
  49. }
  50. static inline struct fd fdget(unsigned int fd)
  51. {
  52. return __to_fd(__fdget(fd));
  53. }
  54. static inline struct fd fdget_raw(unsigned int fd)
  55. {
  56. return __to_fd(__fdget_raw(fd));
  57. }
  58. static inline struct fd fdget_pos(int fd)
  59. {
  60. return __to_fd(__fdget_pos(fd));
  61. }
  62. static inline void fdput_pos(struct fd f)
  63. {
  64. if (f.flags & FDPUT_POS_UNLOCK)
  65. __f_unlock_pos(f.file);
  66. fdput(f);
  67. }
  68. extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
  69. extern int replace_fd(unsigned fd, struct file *file, unsigned flags);
  70. extern void set_close_on_exec(unsigned int fd, int flag);
  71. extern bool get_close_on_exec(unsigned int fd);
  72. extern int __get_unused_fd_flags(unsigned flags, unsigned long nofile);
  73. extern int get_unused_fd_flags(unsigned flags);
  74. extern void put_unused_fd(unsigned int fd);
  75. extern void fd_install(unsigned int fd, struct file *file);
  76. extern int __receive_fd(struct file *file, int __user *ufd,
  77. unsigned int o_flags);
  78. extern int receive_fd(struct file *file, unsigned int o_flags);
  79. static inline int receive_fd_user(struct file *file, int __user *ufd,
  80. unsigned int o_flags)
  81. {
  82. if (ufd == NULL)
  83. return -EFAULT;
  84. return __receive_fd(file, ufd, o_flags);
  85. }
  86. int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags);
  87. extern void flush_delayed_fput(void);
  88. extern void __fput_sync(struct file *);
  89. extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
  90. #endif /* __LINUX_FILE_H */