]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/aufs/file.h
perf xyarray: Fix wrong processing when closing evsel fd
[mirror_ubuntu-artful-kernel.git] / fs / aufs / file.h
1 /*
2 * Copyright (C) 2005-2017 Junjiro R. Okajima
3 *
4 * This program, aufs is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * file operations
20 */
21
22 #ifndef __AUFS_FILE_H__
23 #define __AUFS_FILE_H__
24
25 #ifdef __KERNEL__
26
27 #include <linux/file.h>
28 #include <linux/fs.h>
29 #include <linux/mm_types.h>
30 #include <linux/poll.h>
31 #include "rwsem.h"
32
33 struct au_branch;
34 struct au_hfile {
35 struct file *hf_file;
36 struct au_branch *hf_br;
37 };
38
39 struct au_vdir;
40 struct au_fidir {
41 aufs_bindex_t fd_bbot;
42 aufs_bindex_t fd_nent;
43 struct au_vdir *fd_vdir_cache;
44 struct au_hfile fd_hfile[];
45 };
46
47 static inline int au_fidir_sz(int nent)
48 {
49 AuDebugOn(nent < 0);
50 return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
51 }
52
53 struct au_finfo {
54 atomic_t fi_generation;
55
56 struct au_rwsem fi_rwsem;
57 aufs_bindex_t fi_btop;
58
59 /* do not union them */
60 struct { /* for non-dir */
61 struct au_hfile fi_htop;
62 atomic_t fi_mmapped;
63 };
64 struct au_fidir *fi_hdir; /* for dir only */
65
66 struct hlist_node fi_hlist;
67 struct file *fi_file; /* very ugly */
68 } ____cacheline_aligned_in_smp;
69
70 /* ---------------------------------------------------------------------- */
71
72 /* file.c */
73 extern const struct address_space_operations aufs_aop;
74 unsigned int au_file_roflags(unsigned int flags);
75 struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
76 struct file *file, int force_wr);
77 struct au_do_open_args {
78 int aopen;
79 int (*open)(struct file *file, int flags,
80 struct file *h_file);
81 struct au_fidir *fidir;
82 struct file *h_file;
83 };
84 int au_do_open(struct file *file, struct au_do_open_args *args);
85 int au_reopen_nondir(struct file *file);
86 struct au_pin;
87 int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
88 int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
89 int wlock, unsigned int fi_lsc);
90 int au_do_flush(struct file *file, fl_owner_t id,
91 int (*flush)(struct file *file, fl_owner_t id));
92
93 /* poll.c */
94 #ifdef CONFIG_AUFS_POLL
95 unsigned int aufs_poll(struct file *file, poll_table *wait);
96 #endif
97
98 #ifdef CONFIG_AUFS_BR_HFSPLUS
99 /* hfsplus.c */
100 struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
101 int force_wr);
102 void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
103 struct file *h_file);
104 #else
105 AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
106 aufs_bindex_t bindex, int force_wr)
107 AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
108 struct file *h_file);
109 #endif
110
111 /* f_op.c */
112 extern const struct file_operations aufs_file_fop;
113 int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
114 int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
115 struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
116
117 /* finfo.c */
118 void au_hfput(struct au_hfile *hf, int execed);
119 void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
120 struct file *h_file);
121
122 void au_update_figen(struct file *file);
123 struct au_fidir *au_fidir_alloc(struct super_block *sb);
124 int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
125
126 void au_fi_init_once(void *_fi);
127 void au_finfo_fin(struct file *file);
128 int au_finfo_init(struct file *file, struct au_fidir *fidir);
129
130 /* ioctl.c */
131 long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
132 #ifdef CONFIG_COMPAT
133 long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
134 unsigned long arg);
135 long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
136 unsigned long arg);
137 #endif
138
139 /* ---------------------------------------------------------------------- */
140
141 static inline struct au_finfo *au_fi(struct file *file)
142 {
143 return file->private_data;
144 }
145
146 /* ---------------------------------------------------------------------- */
147
148 /*
149 * fi_read_lock, fi_write_lock,
150 * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
151 */
152 AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
153
154 /* lock subclass for finfo */
155 enum {
156 AuLsc_FI_1,
157 AuLsc_FI_2
158 };
159
160 static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
161 {
162 au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
163 }
164
165 static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
166 {
167 au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
168 }
169
170 /*
171 * fi_read_lock_1, fi_write_lock_1,
172 * fi_read_lock_2, fi_write_lock_2
173 */
174 #define AuReadLockFunc(name) \
175 static inline void fi_read_lock_##name(struct file *f) \
176 { fi_read_lock_nested(f, AuLsc_FI_##name); }
177
178 #define AuWriteLockFunc(name) \
179 static inline void fi_write_lock_##name(struct file *f) \
180 { fi_write_lock_nested(f, AuLsc_FI_##name); }
181
182 #define AuRWLockFuncs(name) \
183 AuReadLockFunc(name) \
184 AuWriteLockFunc(name)
185
186 AuRWLockFuncs(1);
187 AuRWLockFuncs(2);
188
189 #undef AuReadLockFunc
190 #undef AuWriteLockFunc
191 #undef AuRWLockFuncs
192
193 #define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
194 #define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
195 #define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
196
197 /* ---------------------------------------------------------------------- */
198
199 /* todo: hard/soft set? */
200 static inline aufs_bindex_t au_fbtop(struct file *file)
201 {
202 FiMustAnyLock(file);
203 return au_fi(file)->fi_btop;
204 }
205
206 static inline aufs_bindex_t au_fbbot_dir(struct file *file)
207 {
208 FiMustAnyLock(file);
209 AuDebugOn(!au_fi(file)->fi_hdir);
210 return au_fi(file)->fi_hdir->fd_bbot;
211 }
212
213 static inline struct au_vdir *au_fvdir_cache(struct file *file)
214 {
215 FiMustAnyLock(file);
216 AuDebugOn(!au_fi(file)->fi_hdir);
217 return au_fi(file)->fi_hdir->fd_vdir_cache;
218 }
219
220 static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
221 {
222 FiMustWriteLock(file);
223 au_fi(file)->fi_btop = bindex;
224 }
225
226 static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
227 {
228 FiMustWriteLock(file);
229 AuDebugOn(!au_fi(file)->fi_hdir);
230 au_fi(file)->fi_hdir->fd_bbot = bindex;
231 }
232
233 static inline void au_set_fvdir_cache(struct file *file,
234 struct au_vdir *vdir_cache)
235 {
236 FiMustWriteLock(file);
237 AuDebugOn(!au_fi(file)->fi_hdir);
238 au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
239 }
240
241 static inline struct file *au_hf_top(struct file *file)
242 {
243 FiMustAnyLock(file);
244 AuDebugOn(au_fi(file)->fi_hdir);
245 return au_fi(file)->fi_htop.hf_file;
246 }
247
248 static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
249 {
250 FiMustAnyLock(file);
251 AuDebugOn(!au_fi(file)->fi_hdir);
252 return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
253 }
254
255 /* todo: memory barrier? */
256 static inline unsigned int au_figen(struct file *f)
257 {
258 return atomic_read(&au_fi(f)->fi_generation);
259 }
260
261 static inline void au_set_mmapped(struct file *f)
262 {
263 if (atomic_inc_return(&au_fi(f)->fi_mmapped))
264 return;
265 pr_warn("fi_mmapped wrapped around\n");
266 while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
267 ;
268 }
269
270 static inline void au_unset_mmapped(struct file *f)
271 {
272 atomic_dec(&au_fi(f)->fi_mmapped);
273 }
274
275 static inline int au_test_mmapped(struct file *f)
276 {
277 return atomic_read(&au_fi(f)->fi_mmapped);
278 }
279
280 /* customize vma->vm_file */
281
282 static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
283 struct file *file)
284 {
285 struct file *f;
286
287 f = vma->vm_file;
288 get_file(file);
289 vma->vm_file = file;
290 fput(f);
291 }
292
293 #ifdef CONFIG_MMU
294 #define AuDbgVmRegion(file, vma) do {} while (0)
295
296 static inline void au_vm_file_reset(struct vm_area_struct *vma,
297 struct file *file)
298 {
299 au_do_vm_file_reset(vma, file);
300 }
301 #else
302 #define AuDbgVmRegion(file, vma) \
303 AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
304
305 static inline void au_vm_file_reset(struct vm_area_struct *vma,
306 struct file *file)
307 {
308 struct file *f;
309
310 au_do_vm_file_reset(vma, file);
311 f = vma->vm_region->vm_file;
312 get_file(file);
313 vma->vm_region->vm_file = file;
314 fput(f);
315 }
316 #endif /* CONFIG_MMU */
317
318 /* handle vma->vm_prfile */
319 static inline void au_vm_prfile_set(struct vm_area_struct *vma,
320 struct file *file)
321 {
322 get_file(file);
323 vma->vm_prfile = file;
324 #ifndef CONFIG_MMU
325 get_file(file);
326 vma->vm_region->vm_prfile = file;
327 #endif
328 }
329
330 #endif /* __KERNEL__ */
331 #endif /* __AUFS_FILE_H__ */