file.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. extern void fput_many(struct file *, unsigned int);
  14. struct file_operations;
  15. struct task_struct;
  16. struct vfsmount;
  17. struct dentry;
  18. struct inode;
  19. struct path;
  20. extern struct file *alloc_file_pseudo(struct inode *, struct vfsmount *,
  21. const char *, int flags, const struct file_operations *);
  22. extern struct file *alloc_file_clone(struct file *, int flags,
  23. const struct file_operations *);
  24. static inline void fput_light(struct file *file, int fput_needed)
  25. {
  26. if (fput_needed)
  27. fput(file);
  28. }
  29. struct fd {
  30. struct file *file;
  31. unsigned int flags;
  32. };
  33. #define FDPUT_FPUT 1
  34. #define FDPUT_POS_UNLOCK 2
  35. static inline void fdput(struct fd fd)
  36. {
  37. if (fd.flags & FDPUT_FPUT)
  38. fput(fd.file);
  39. }
  40. extern struct file *fget(unsigned int fd);
  41. extern struct file *fget_many(unsigned int fd, unsigned int refs);
  42. extern struct file *fget_raw(unsigned int fd);
  43. extern struct file *fget_task(struct task_struct *task, unsigned int fd);
  44. extern unsigned long __fdget(unsigned int fd);
  45. extern unsigned long __fdget_raw(unsigned int fd);
  46. extern unsigned long __fdget_pos(unsigned int fd);
  47. extern void __f_unlock_pos(struct file *);
  48. static inline struct fd __to_fd(unsigned long v)
  49. {
  50. return (struct fd){(struct file *)(v & ~3),v & 3};
  51. }
  52. static inline struct fd fdget(unsigned int fd)
  53. {
  54. return __to_fd(__fdget(fd));
  55. }
  56. static inline struct fd fdget_raw(unsigned int fd)
  57. {
  58. return __to_fd(__fdget_raw(fd));
  59. }
  60. static inline struct fd fdget_pos(int fd)
  61. {
  62. return __to_fd(__fdget_pos(fd));
  63. }
  64. static inline void fdput_pos(struct fd f)
  65. {
  66. if (f.flags & FDPUT_POS_UNLOCK)
  67. __f_unlock_pos(f.file);
  68. fdput(f);
  69. }
  70. extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
  71. extern int replace_fd(unsigned fd, struct file *file, unsigned flags);
  72. extern void set_close_on_exec(unsigned int fd, int flag);
  73. extern bool get_close_on_exec(unsigned int fd);
  74. extern int __get_unused_fd_flags(unsigned flags, unsigned long nofile);
  75. extern int get_unused_fd_flags(unsigned flags);
  76. extern void put_unused_fd(unsigned int fd);
  77. extern void fd_install(unsigned int fd, struct file *file);
  78. extern int __receive_fd(int fd, struct file *file, int __user *ufd,
  79. unsigned int o_flags);
  80. static inline int receive_fd_user(struct file *file, int __user *ufd,
  81. unsigned int o_flags)
  82. {
  83. if (ufd == NULL)
  84. return -EFAULT;
  85. return __receive_fd(-1, file, ufd, o_flags);
  86. }
  87. static inline int receive_fd(struct file *file, unsigned int o_flags)
  88. {
  89. return __receive_fd(-1, file, NULL, o_flags);
  90. }
  91. static inline int receive_fd_replace(int fd, struct file *file, unsigned int o_flags)
  92. {
  93. return __receive_fd(fd, file, NULL, o_flags);
  94. }
  95. extern void flush_delayed_fput(void);
  96. extern void __fput_sync(struct file *);
  97. extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
  98. #endif /* __LINUX_FILE_H */