file.h 9.01 KB
Newer Older
1
2
/* SPDX-License-Identifier: GPL-2.0 */
/*
3
 * Copyright (C) 2005-2020 Junjiro R. Okajima
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * This program, aufs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19
20
21
22
23
24
25
26
27
28
29
 */

/*
 * file operations
 */

#ifndef __AUFS_FILE_H__
#define __AUFS_FILE_H__

#ifdef __KERNEL__

#include <linux/file.h>
#include <linux/fs.h>
J. R. Okajima's avatar
J. R. Okajima committed
30
#include <linux/mm_types.h>
31
#include <linux/poll.h>
32
33
34
35
36
37
38
39
#include "rwsem.h"

struct au_branch;
struct au_hfile {
	struct file		*hf_file;
	struct au_branch	*hf_br;
};

J. R. Okajima's avatar
J. R. Okajima committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
struct au_vdir;
struct au_fidir {
	aufs_bindex_t		fd_bbot;
	aufs_bindex_t		fd_nent;
	struct au_vdir		*fd_vdir_cache;
	struct au_hfile		fd_hfile[];
};

static inline int au_fidir_sz(int nent)
{
	AuDebugOn(nent < 0);
	return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
}

54
55
56
57
58
59
struct au_finfo {
	atomic_t		fi_generation;

	struct au_rwsem		fi_rwsem;
	aufs_bindex_t		fi_btop;

J. R. Okajima's avatar
J. R. Okajima committed
60
	/* do not union them */
J. R. Okajima's avatar
J. R. Okajima committed
61
62
63
64
	struct {				/* for non-dir */
		struct au_hfile			fi_htop;
		atomic_t			fi_mmapped;
	};
J. R. Okajima's avatar
J. R. Okajima committed
65
	struct au_fidir		*fi_hdir;	/* for dir only */
66
67
68

	struct hlist_bl_node	fi_hlist;
	struct file		*fi_file;	/* very ugly */
69
70
71
72
73
74
	struct rcu_head		rcu;
} ____cacheline_aligned_in_smp;

/* ---------------------------------------------------------------------- */

/* file.c */
75
extern const struct address_space_operations aufs_aop;
76
77
unsigned int au_file_roflags(unsigned int flags);
struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
78
		       struct file *file, int force_wr);
J. R. Okajima's avatar
J. R. Okajima committed
79
struct au_do_open_args {
80
81
82
	int		aopen;
	int		(*open)(struct file *file, int flags,
				struct file *h_file);
J. R. Okajima's avatar
J. R. Okajima committed
83
	struct au_fidir	*fidir;
84
	struct file	*h_file;
J. R. Okajima's avatar
J. R. Okajima committed
85
86
};
int au_do_open(struct file *file, struct au_do_open_args *args);
87
88
89
int au_reopen_nondir(struct file *file);
struct au_pin;
int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
90
91
int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
			  int wlock, unsigned int fi_lsc);
J. R. Okajima's avatar
J. R. Okajima committed
92
93
int au_do_flush(struct file *file, fl_owner_t id,
		int (*flush)(struct file *file, fl_owner_t id));
94

95
96
97
98
99
/* poll.c */
#ifdef CONFIG_AUFS_POLL
__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
#endif

J. R. Okajima's avatar
J. R. Okajima committed
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifdef CONFIG_AUFS_BR_HFSPLUS
/* hfsplus.c */
struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
			   int force_wr);
void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
		    struct file *h_file);
#else
AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
       aufs_bindex_t bindex, int force_wr)
AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
	   struct file *h_file);
#endif

J. R. Okajima's avatar
J. R. Okajima committed
113
114
/* f_op.c */
extern const struct file_operations aufs_file_fop;
115
int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
J. R. Okajima's avatar
J. R. Okajima committed
116
int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
J. R. Okajima's avatar
J. R. Okajima committed
117
struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
J. R. Okajima's avatar
J. R. Okajima committed
118

119
120
121
122
123
124
/* finfo.c */
void au_hfput(struct au_hfile *hf, int execed);
void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
		   struct file *h_file);

void au_update_figen(struct file *file);
J. R. Okajima's avatar
J. R. Okajima committed
125
126
struct au_fidir *au_fidir_alloc(struct super_block *sb);
int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
127
128
129

void au_fi_init_once(void *_fi);
void au_finfo_fin(struct file *file);
J. R. Okajima's avatar
J. R. Okajima committed
130
int au_finfo_init(struct file *file, struct au_fidir *fidir);
131

J. R. Okajima's avatar
J. R. Okajima committed
132
133
134
135
136
137
138
139
140
/* ioctl.c */
long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
#ifdef CONFIG_COMPAT
long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
			   unsigned long arg);
long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
			      unsigned long arg);
#endif

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* ---------------------------------------------------------------------- */

static inline struct au_finfo *au_fi(struct file *file)
{
	return file->private_data;
}

/* ---------------------------------------------------------------------- */

#define fi_read_lock(f)	au_rw_read_lock(&au_fi(f)->fi_rwsem)
#define fi_write_lock(f)	au_rw_write_lock(&au_fi(f)->fi_rwsem)
#define fi_read_trylock(f)	au_rw_read_trylock(&au_fi(f)->fi_rwsem)
#define fi_write_trylock(f)	au_rw_write_trylock(&au_fi(f)->fi_rwsem)
/*
#define fi_read_trylock_nested(f) \
	au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
#define fi_write_trylock_nested(f) \
	au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
*/

#define fi_read_unlock(f)	au_rw_read_unlock(&au_fi(f)->fi_rwsem)
#define fi_write_unlock(f)	au_rw_write_unlock(&au_fi(f)->fi_rwsem)
#define fi_downgrade_lock(f)	au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* lock subclass for finfo */
enum {
	AuLsc_FI_1,
	AuLsc_FI_2
};

static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
{
	au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
}

static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
{
	au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
}

/*
 * fi_read_lock_1, fi_write_lock_1,
 * fi_read_lock_2, fi_write_lock_2
 */
#define AuReadLockFunc(name) \
static inline void fi_read_lock_##name(struct file *f) \
{ fi_read_lock_nested(f, AuLsc_FI_##name); }

#define AuWriteLockFunc(name) \
static inline void fi_write_lock_##name(struct file *f) \
{ fi_write_lock_nested(f, AuLsc_FI_##name); }

#define AuRWLockFuncs(name) \
	AuReadLockFunc(name) \
	AuWriteLockFunc(name)

AuRWLockFuncs(1);
AuRWLockFuncs(2);

#undef AuReadLockFunc
#undef AuWriteLockFunc
#undef AuRWLockFuncs

204
205
206
207
208
209
210
211
212
213
214
215
216
#define FiMustNoWaiters(f)	AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
#define FiMustAnyLock(f)	AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
#define FiMustWriteLock(f)	AuRwMustWriteLock(&au_fi(f)->fi_rwsem)

/* ---------------------------------------------------------------------- */

/* todo: hard/soft set? */
static inline aufs_bindex_t au_fbtop(struct file *file)
{
	FiMustAnyLock(file);
	return au_fi(file)->fi_btop;
}

J. R. Okajima's avatar
J. R. Okajima committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
static inline aufs_bindex_t au_fbbot_dir(struct file *file)
{
	FiMustAnyLock(file);
	AuDebugOn(!au_fi(file)->fi_hdir);
	return au_fi(file)->fi_hdir->fd_bbot;
}

static inline struct au_vdir *au_fvdir_cache(struct file *file)
{
	FiMustAnyLock(file);
	AuDebugOn(!au_fi(file)->fi_hdir);
	return au_fi(file)->fi_hdir->fd_vdir_cache;
}

231
232
233
234
235
236
static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
{
	FiMustWriteLock(file);
	au_fi(file)->fi_btop = bindex;
}

J. R. Okajima's avatar
J. R. Okajima committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
{
	FiMustWriteLock(file);
	AuDebugOn(!au_fi(file)->fi_hdir);
	au_fi(file)->fi_hdir->fd_bbot = bindex;
}

static inline void au_set_fvdir_cache(struct file *file,
				      struct au_vdir *vdir_cache)
{
	FiMustWriteLock(file);
	AuDebugOn(!au_fi(file)->fi_hdir);
	au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
}

252
253
254
static inline struct file *au_hf_top(struct file *file)
{
	FiMustAnyLock(file);
J. R. Okajima's avatar
J. R. Okajima committed
255
	AuDebugOn(au_fi(file)->fi_hdir);
256
257
258
	return au_fi(file)->fi_htop.hf_file;
}

J. R. Okajima's avatar
J. R. Okajima committed
259
260
261
262
263
264
265
static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
{
	FiMustAnyLock(file);
	AuDebugOn(!au_fi(file)->fi_hdir);
	return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
}

266
267
268
269
270
271
/* todo: memory barrier? */
static inline unsigned int au_figen(struct file *f)
{
	return atomic_read(&au_fi(f)->fi_generation);
}

J. R. Okajima's avatar
J. R. Okajima committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
static inline void au_set_mmapped(struct file *f)
{
	if (atomic_inc_return(&au_fi(f)->fi_mmapped))
		return;
	pr_warn("fi_mmapped wrapped around\n");
	while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
		;
}

static inline void au_unset_mmapped(struct file *f)
{
	atomic_dec(&au_fi(f)->fi_mmapped);
}

static inline int au_test_mmapped(struct file *f)
{
	return atomic_read(&au_fi(f)->fi_mmapped);
}

/* customize vma->vm_file */

static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
				       struct file *file)
{
	struct file *f;

	f = vma->vm_file;
	get_file(file);
	vma->vm_file = file;
	fput(f);
}

#ifdef CONFIG_MMU
#define AuDbgVmRegion(file, vma) do {} while (0)

static inline void au_vm_file_reset(struct vm_area_struct *vma,
				    struct file *file)
{
	au_do_vm_file_reset(vma, file);
}
#else
#define AuDbgVmRegion(file, vma) \
	AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))

static inline void au_vm_file_reset(struct vm_area_struct *vma,
				    struct file *file)
{
	struct file *f;

	au_do_vm_file_reset(vma, file);
	f = vma->vm_region->vm_file;
	get_file(file);
	vma->vm_region->vm_file = file;
	fput(f);
}
#endif /* CONFIG_MMU */

/* handle vma->vm_prfile */
static inline void au_vm_prfile_set(struct vm_area_struct *vma,
				    struct file *file)
{
	get_file(file);
	vma->vm_prfile = file;
#ifndef CONFIG_MMU
	get_file(file);
	vma->vm_region->vm_prfile = file;
#endif
}

341
342
#endif /* __KERNEL__ */
#endif /* __AUFS_FILE_H__ */