file.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. struct file;
  11. extern void fput(struct file *);
  12. extern void fput_many(struct file *, unsigned int);
  13. struct file_operations;
  14. struct vfsmount;
  15. struct dentry;
  16. struct inode;
  17. struct path;
  18. extern struct file *alloc_file_pseudo(struct inode *, struct vfsmount *,
  19. const char *, int flags, const struct file_operations *);
  20. extern struct file *alloc_file_clone(struct file *, int flags,
  21. const struct file_operations *);
  22. static inline void fput_light(struct file *file, int fput_needed)
  23. {
  24. if (fput_needed)
  25. fput(file);
  26. }
  27. struct fd {
  28. struct file *file;
  29. unsigned int flags;
  30. };
  31. #define FDPUT_FPUT 1
  32. #define FDPUT_POS_UNLOCK 2
  33. static inline void fdput(struct fd fd)
  34. {
  35. if (fd.flags & FDPUT_FPUT)
  36. fput(fd.file);
  37. }
  38. extern struct file *fget(unsigned int fd);
  39. extern struct file *fget_many(unsigned int fd, unsigned int refs);
  40. extern struct file *fget_raw(unsigned int fd);
  41. extern unsigned long __fdget(unsigned int fd);
  42. extern unsigned long __fdget_raw(unsigned int fd);
  43. extern unsigned long __fdget_pos(unsigned int fd);
  44. extern void __f_unlock_pos(struct file *);
  45. static inline struct fd __to_fd(unsigned long v)
  46. {
  47. return (struct fd){(struct file *)(v & ~3),v & 3};
  48. }
  49. static inline struct fd fdget(unsigned int fd)
  50. {
  51. return __to_fd(__fdget(fd));
  52. }
  53. static inline struct fd fdget_raw(unsigned int fd)
  54. {
  55. return __to_fd(__fdget_raw(fd));
  56. }
  57. static inline struct fd fdget_pos(int fd)
  58. {
  59. return __to_fd(__fdget_pos(fd));
  60. }
  61. static inline void fdput_pos(struct fd f)
  62. {
  63. if (f.flags & FDPUT_POS_UNLOCK)
  64. __f_unlock_pos(f.file);
  65. fdput(f);
  66. }
  67. extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
  68. extern int replace_fd(unsigned fd, struct file *file, unsigned flags);
  69. extern void set_close_on_exec(unsigned int fd, int flag);
  70. extern bool get_close_on_exec(unsigned int fd);
  71. extern int get_unused_fd_flags(unsigned flags);
  72. extern void put_unused_fd(unsigned int fd);
  73. extern void fd_install(unsigned int fd, struct file *file);
  74. extern void flush_delayed_fput(void);
  75. extern void __fput_sync(struct file *);
  76. #endif /* __LINUX_FILE_H */