Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / include / linux / fdtable.h
1 /*
2  * descriptor table internals; you almost certainly want file.h instead.
3  */
4
5 #ifndef __LINUX_FDTABLE_H
6 #define __LINUX_FDTABLE_H
7
8 #include <linux/posix_types.h>
9 #include <linux/compiler.h>
10 #include <linux/spinlock.h>
11 #include <linux/rcupdate.h>
12 #include <linux/nospec.h>
13 #include <linux/types.h>
14 #include <linux/init.h>
15 #include <linux/fs.h>
16
17 #include <linux/atomic.h>
18
19 /*
20  * The default fd array needs to be at least BITS_PER_LONG,
21  * as this is the granularity returned by copy_fdset().
22  */
23 #define NR_OPEN_DEFAULT BITS_PER_LONG
24
25 /*
26  * The embedded_fd_set is a small fd_set,
27  * suitable for most tasks (which open <= BITS_PER_LONG files)
28  */
29 struct embedded_fd_set {
30         unsigned long fds_bits[1];
31 };
32
33 struct fdtable {
34         unsigned int max_fds;
35         struct file __rcu **fd;      /* current fd array */
36         fd_set *close_on_exec;
37         fd_set *open_fds;
38         struct rcu_head rcu;
39         struct fdtable *next;
40 };
41
42 static inline void __set_close_on_exec(int fd, struct fdtable *fdt)
43 {
44         FD_SET(fd, fdt->close_on_exec);
45 }
46
47 static inline void __clear_close_on_exec(int fd, struct fdtable *fdt)
48 {
49         FD_CLR(fd, fdt->close_on_exec);
50 }
51
52 static inline bool close_on_exec(int fd, const struct fdtable *fdt)
53 {
54         return FD_ISSET(fd, fdt->close_on_exec);
55 }
56
57 static inline void __set_open_fd(int fd, struct fdtable *fdt)
58 {
59         FD_SET(fd, fdt->open_fds);
60 }
61
62 static inline void __clear_open_fd(int fd, struct fdtable *fdt)
63 {
64         FD_CLR(fd, fdt->open_fds);
65 }
66
67 static inline bool fd_is_open(int fd, const struct fdtable *fdt)
68 {
69         return FD_ISSET(fd, fdt->open_fds);
70 }
71
72 /*
73  * Open file table structure
74  */
75 struct files_struct {
76   /*
77    * read mostly part
78    */
79         atomic_t count;
80         struct fdtable __rcu *fdt;
81         struct fdtable fdtab;
82   /*
83    * written part on a separate cache line in SMP
84    */
85         spinlock_t file_lock ____cacheline_aligned_in_smp;
86         int next_fd;
87         struct embedded_fd_set close_on_exec_init;
88         struct embedded_fd_set open_fds_init;
89         struct file __rcu * fd_array[NR_OPEN_DEFAULT];
90 };
91
92 #define rcu_dereference_check_fdtable(files, fdtfd) \
93         (rcu_dereference_check((fdtfd), \
94                                lockdep_is_held(&(files)->file_lock) || \
95                                atomic_read(&(files)->count) == 1 || \
96                                rcu_my_thread_group_empty()))
97
98 #define files_fdtable(files) \
99                 (rcu_dereference_check_fdtable((files), (files)->fdt))
100
101 struct file_operations;
102 struct vfsmount;
103 struct dentry;
104
105 extern int expand_files(struct files_struct *, int nr);
106 extern void free_fdtable_rcu(struct rcu_head *rcu);
107 extern void __init files_defer_init(void);
108
109 static inline void free_fdtable(struct fdtable *fdt)
110 {
111         call_rcu(&fdt->rcu, free_fdtable_rcu);
112 }
113
114 static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
115 {
116         struct file * file = NULL;
117         struct fdtable *fdt = files_fdtable(files);
118
119         if (fd < fdt->max_fds) {
120                 fd = array_index_nospec(fd, fdt->max_fds);
121                 file = rcu_dereference_check_fdtable(files, fdt->fd[fd]);
122         }
123         return file;
124 }
125
126 /*
127  * Check whether the specified fd has an open file.
128  */
129 #define fcheck(fd)      fcheck_files(current->files, fd)
130
131 struct task_struct;
132
133 struct files_struct *get_files_struct(struct task_struct *);
134 void put_files_struct(struct files_struct *fs);
135 void reset_files_struct(struct files_struct *);
136 int unshare_files(struct files_struct **);
137 struct files_struct *dup_fd(struct files_struct *, int *);
138
139 extern struct kmem_cache *files_cachep;
140
141 #endif /* __LINUX_FDTABLE_H */