file.h 4.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (C) 2005-2019 Junjiro R. Okajima
 */

/*
 * file operations
 */

#ifndef __AUFS_FILE_H__
#define __AUFS_FILE_H__

#ifdef __KERNEL__

#include <linux/file.h>
#include <linux/fs.h>
#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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
}

39
40
41
42
43
44
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
45
46
47
	/* do not union them */
	struct au_hfile		fi_htop;	/* for non-dir */
	struct au_fidir		*fi_hdir;	/* for dir only */
48
49
50
51
52
53
	struct rcu_head		rcu;
} ____cacheline_aligned_in_smp;

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

/* file.c */
54
extern const struct address_space_operations aufs_aop;
55
56
57
58
59
60
61
62
63
64
unsigned int au_file_roflags(unsigned int flags);
struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
		       struct file *file);

/* 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
65
66
struct au_fidir *au_fidir_alloc(struct super_block *sb);
int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
67
68
69

void au_fi_init_once(void *_fi);
void au_finfo_fin(struct file *file);
J. R. Okajima's avatar
J. R. Okajima committed
70
int au_finfo_init(struct file *file, struct au_fidir *fidir);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

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

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)

#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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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;
}

123
124
125
126
127
128
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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;
}

144
145
146
static inline struct file *au_hf_top(struct file *file)
{
	FiMustAnyLock(file);
J. R. Okajima's avatar
J. R. Okajima committed
147
	AuDebugOn(au_fi(file)->fi_hdir);
148
149
150
	return au_fi(file)->fi_htop.hf_file;
}

J. R. Okajima's avatar
J. R. Okajima committed
151
152
153
154
155
156
157
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;
}

158
159
160
161
162
163
164
165
/* todo: memory barrier? */
static inline unsigned int au_figen(struct file *f)
{
	return atomic_read(&au_fi(f)->fi_generation);
}

#endif /* __KERNEL__ */
#endif /* __AUFS_FILE_H__ */