2 * Copyright (C) 2005-2017 Junjiro R. Okajima
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.
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.
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/>.
22 #ifndef __AUFS_FILE_H__
23 #define __AUFS_FILE_H__
27 #include <linux/file.h>
29 #include <linux/mm_types.h>
30 #include <linux/poll.h>
36 struct au_branch
*hf_br
;
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
[];
47 static inline int au_fidir_sz(int nent
)
50 return sizeof(struct au_fidir
) + sizeof(struct au_hfile
) * nent
;
54 atomic_t fi_generation
;
56 struct au_rwsem fi_rwsem
;
57 aufs_bindex_t fi_btop
;
59 /* do not union them */
60 struct { /* for non-dir */
61 struct au_hfile fi_htop
;
64 struct au_fidir
*fi_hdir
; /* for dir only */
66 struct hlist_node fi_hlist
;
67 struct file
*fi_file
; /* very ugly */
68 } ____cacheline_aligned_in_smp
;
70 /* ---------------------------------------------------------------------- */
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
{
79 int (*open
)(struct file
*file
, int flags
,
81 struct au_fidir
*fidir
;
84 int au_do_open(struct file
*file
, struct au_do_open_args
*args
);
85 int au_reopen_nondir(struct file
*file
);
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
));
94 #ifdef CONFIG_AUFS_POLL
95 unsigned int aufs_poll(struct file
*file
, poll_table
*wait
);
98 #ifdef CONFIG_AUFS_BR_HFSPLUS
100 struct file
*au_h_open_pre(struct dentry
*dentry
, aufs_bindex_t bindex
,
102 void au_h_open_post(struct dentry
*dentry
, aufs_bindex_t bindex
,
103 struct file
*h_file
);
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
);
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
);
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
);
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
);
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
);
131 long aufs_ioctl_nondir(struct file
*file
, unsigned int cmd
, unsigned long arg
);
133 long aufs_compat_ioctl_dir(struct file
*file
, unsigned int cmd
,
135 long aufs_compat_ioctl_nondir(struct file
*file
, unsigned int cmd
,
139 /* ---------------------------------------------------------------------- */
141 static inline struct au_finfo
*au_fi(struct file
*file
)
143 return file
->private_data
;
146 /* ---------------------------------------------------------------------- */
149 * fi_read_lock, fi_write_lock,
150 * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
152 AuSimpleRwsemFuncs(fi
, struct file
*f
, &au_fi(f
)->fi_rwsem
);
154 /* lock subclass for finfo */
160 static inline void fi_read_lock_nested(struct file
*f
, unsigned int lsc
)
162 au_rw_read_lock_nested(&au_fi(f
)->fi_rwsem
, lsc
);
165 static inline void fi_write_lock_nested(struct file
*f
, unsigned int lsc
)
167 au_rw_write_lock_nested(&au_fi(f
)->fi_rwsem
, lsc
);
171 * fi_read_lock_1, fi_write_lock_1,
172 * fi_read_lock_2, fi_write_lock_2
174 #define AuReadLockFunc(name) \
175 static inline void fi_read_lock_##name(struct file *f) \
176 { fi_read_lock_nested(f, AuLsc_FI_##name); }
178 #define AuWriteLockFunc(name) \
179 static inline void fi_write_lock_##name(struct file *f) \
180 { fi_write_lock_nested(f, AuLsc_FI_##name); }
182 #define AuRWLockFuncs(name) \
183 AuReadLockFunc(name) \
184 AuWriteLockFunc(name)
189 #undef AuReadLockFunc
190 #undef AuWriteLockFunc
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)
197 /* ---------------------------------------------------------------------- */
199 /* todo: hard/soft set? */
200 static inline aufs_bindex_t
au_fbtop(struct file
*file
)
203 return au_fi(file
)->fi_btop
;
206 static inline aufs_bindex_t
au_fbbot_dir(struct file
*file
)
209 AuDebugOn(!au_fi(file
)->fi_hdir
);
210 return au_fi(file
)->fi_hdir
->fd_bbot
;
213 static inline struct au_vdir
*au_fvdir_cache(struct file
*file
)
216 AuDebugOn(!au_fi(file
)->fi_hdir
);
217 return au_fi(file
)->fi_hdir
->fd_vdir_cache
;
220 static inline void au_set_fbtop(struct file
*file
, aufs_bindex_t bindex
)
222 FiMustWriteLock(file
);
223 au_fi(file
)->fi_btop
= bindex
;
226 static inline void au_set_fbbot_dir(struct file
*file
, aufs_bindex_t bindex
)
228 FiMustWriteLock(file
);
229 AuDebugOn(!au_fi(file
)->fi_hdir
);
230 au_fi(file
)->fi_hdir
->fd_bbot
= bindex
;
233 static inline void au_set_fvdir_cache(struct file
*file
,
234 struct au_vdir
*vdir_cache
)
236 FiMustWriteLock(file
);
237 AuDebugOn(!au_fi(file
)->fi_hdir
);
238 au_fi(file
)->fi_hdir
->fd_vdir_cache
= vdir_cache
;
241 static inline struct file
*au_hf_top(struct file
*file
)
244 AuDebugOn(au_fi(file
)->fi_hdir
);
245 return au_fi(file
)->fi_htop
.hf_file
;
248 static inline struct file
*au_hf_dir(struct file
*file
, aufs_bindex_t bindex
)
251 AuDebugOn(!au_fi(file
)->fi_hdir
);
252 return au_fi(file
)->fi_hdir
->fd_hfile
[0 + bindex
].hf_file
;
255 /* todo: memory barrier? */
256 static inline unsigned int au_figen(struct file
*f
)
258 return atomic_read(&au_fi(f
)->fi_generation
);
261 static inline void au_set_mmapped(struct file
*f
)
263 if (atomic_inc_return(&au_fi(f
)->fi_mmapped
))
265 pr_warn("fi_mmapped wrapped around\n");
266 while (!atomic_inc_return(&au_fi(f
)->fi_mmapped
))
270 static inline void au_unset_mmapped(struct file
*f
)
272 atomic_dec(&au_fi(f
)->fi_mmapped
);
275 static inline int au_test_mmapped(struct file
*f
)
277 return atomic_read(&au_fi(f
)->fi_mmapped
);
280 /* customize vma->vm_file */
282 static inline void au_do_vm_file_reset(struct vm_area_struct
*vma
,
294 #define AuDbgVmRegion(file, vma) do {} while (0)
296 static inline void au_vm_file_reset(struct vm_area_struct
*vma
,
299 au_do_vm_file_reset(vma
, file
);
302 #define AuDbgVmRegion(file, vma) \
303 AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
305 static inline void au_vm_file_reset(struct vm_area_struct
*vma
,
310 au_do_vm_file_reset(vma
, file
);
311 f
= vma
->vm_region
->vm_file
;
313 vma
->vm_region
->vm_file
= file
;
316 #endif /* CONFIG_MMU */
318 /* handle vma->vm_prfile */
319 static inline void au_vm_prfile_set(struct vm_area_struct
*vma
,
323 vma
->vm_prfile
= file
;
326 vma
->vm_region
->vm_prfile
= file
;
330 #endif /* __KERNEL__ */
331 #endif /* __AUFS_FILE_H__ */