fdtable.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * descriptor table internals; you almost certainly want file.h instead.
  4. */
  5. #ifndef __LINUX_FDTABLE_H
  6. #define __LINUX_FDTABLE_H
  7. #include <linux/posix_types.h>
  8. #include <linux/compiler.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/rcupdate.h>
  11. #include <linux/nospec.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/fs.h>
  15. #include <linux/atomic.h>
  16. /*
  17. * The default fd array needs to be at least BITS_PER_LONG,
  18. * as this is the granularity returned by copy_fdset().
  19. */
  20. #define NR_OPEN_DEFAULT BITS_PER_LONG
  21. #define NR_OPEN_MAX ~0U
  22. struct fdtable {
  23. unsigned int max_fds;
  24. struct file __rcu **fd; /* current fd array */
  25. unsigned long *close_on_exec;
  26. unsigned long *open_fds;
  27. unsigned long *full_fds_bits;
  28. struct rcu_head rcu;
  29. };
  30. static inline bool close_on_exec(unsigned int fd, const struct fdtable *fdt)
  31. {
  32. return test_bit(fd, fdt->close_on_exec);
  33. }
  34. static inline bool fd_is_open(unsigned int fd, const struct fdtable *fdt)
  35. {
  36. return test_bit(fd, fdt->open_fds);
  37. }
  38. /*
  39. * Open file table structure
  40. */
  41. struct files_struct {
  42. /*
  43. * read mostly part
  44. */
  45. atomic_t count;
  46. bool resize_in_progress;
  47. wait_queue_head_t resize_wait;
  48. struct fdtable __rcu *fdt;
  49. struct fdtable fdtab;
  50. /*
  51. * written part on a separate cache line in SMP
  52. */
  53. spinlock_t file_lock ____cacheline_aligned_in_smp;
  54. unsigned int next_fd;
  55. unsigned long close_on_exec_init[1];
  56. unsigned long open_fds_init[1];
  57. unsigned long full_fds_bits_init[1];
  58. struct file __rcu * fd_array[NR_OPEN_DEFAULT];
  59. };
  60. struct file_operations;
  61. struct vfsmount;
  62. struct dentry;
  63. #define rcu_dereference_check_fdtable(files, fdtfd) \
  64. rcu_dereference_check((fdtfd), lockdep_is_held(&(files)->file_lock))
  65. #define files_fdtable(files) \
  66. rcu_dereference_check_fdtable((files), (files)->fdt)
  67. /*
  68. * The caller must ensure that fd table isn't shared or hold rcu or file lock
  69. */
  70. static inline struct file *files_lookup_fd_raw(struct files_struct *files, unsigned int fd)
  71. {
  72. struct fdtable *fdt = rcu_dereference_raw(files->fdt);
  73. if (fd < fdt->max_fds) {
  74. fd = array_index_nospec(fd, fdt->max_fds);
  75. return rcu_dereference_raw(fdt->fd[fd]);
  76. }
  77. return NULL;
  78. }
  79. static inline struct file *files_lookup_fd_locked(struct files_struct *files, unsigned int fd)
  80. {
  81. RCU_LOCKDEP_WARN(!lockdep_is_held(&files->file_lock),
  82. "suspicious rcu_dereference_check() usage");
  83. return files_lookup_fd_raw(files, fd);
  84. }
  85. static inline struct file *files_lookup_fd_rcu(struct files_struct *files, unsigned int fd)
  86. {
  87. RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
  88. "suspicious rcu_dereference_check() usage");
  89. return files_lookup_fd_raw(files, fd);
  90. }
  91. static inline struct file *lookup_fd_rcu(unsigned int fd)
  92. {
  93. return files_lookup_fd_rcu(current->files, fd);
  94. }
  95. struct file *task_lookup_fd_rcu(struct task_struct *task, unsigned int fd);
  96. struct file *task_lookup_next_fd_rcu(struct task_struct *task, unsigned int *fd);
  97. struct task_struct;
  98. void put_files_struct(struct files_struct *fs);
  99. int unshare_files(void);
  100. struct files_struct *dup_fd(struct files_struct *, unsigned, int *) __latent_entropy;
  101. void do_close_on_exec(struct files_struct *);
  102. int iterate_fd(struct files_struct *, unsigned,
  103. int (*)(const void *, struct file *, unsigned),
  104. const void *);
  105. extern int close_fd(unsigned int fd);
  106. extern int __close_range(unsigned int fd, unsigned int max_fd, unsigned int flags);
  107. extern struct file *close_fd_get_file(unsigned int fd);
  108. extern int unshare_fd(unsigned long unshare_flags, unsigned int max_fds,
  109. struct files_struct **new_fdp);
  110. extern struct kmem_cache *files_cachep;
  111. #endif /* __LINUX_FDTABLE_H */