hfsnotify.c 6.8 KB
Newer Older
J. R. Okajima's avatar
J. R. Okajima committed
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/>.
J. R. Okajima's avatar
J. R. Okajima committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
 */

/*
 * fsnotify for the lower directories
 */

#include "aufs.h"

/* FS_IN_IGNORED is unnecessary */
static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
				 | FS_CREATE | FS_EVENT_ON_CHILD);
static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);

static void au_hfsn_free_mark(struct fsnotify_mark *mark)
{
	struct au_hnotify *hn = container_of(mark, struct au_hnotify,
					     hn_mark);
	/* AuDbg("here\n"); */
	au_cache_free_hnotify(hn);
	smp_mb__before_atomic(); /* for atomic64_dec */
	if (atomic64_dec_and_test(&au_hfsn_ifree))
		wake_up(&au_hfsn_wq);
}

static int au_hfsn_alloc(struct au_hinode *hinode)
{
	int err;
	struct au_hnotify *hn;
	struct super_block *sb;
	struct au_branch *br;
	struct fsnotify_mark *mark;
	aufs_bindex_t bindex;

	hn = hinode->hi_notify;
	sb = hn->hn_aufs_inode->i_sb;
	bindex = au_br_index(sb, hinode->hi_id);
	br = au_sbr(sb, bindex);
	AuDebugOn(!br->br_hfsn);

	mark = &hn->hn_mark;
	fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
	mark->mask = AuHfsnMask;
	/*
	 * by udba rename or rmdir, aufs assign a new inode to the known
	 * h_inode, so specify 1 to allow dups.
	 */
	lockdep_off();
	err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
	lockdep_on();

	return err;
}

static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
{
	struct fsnotify_mark *mark;
	unsigned long long ull;
	struct fsnotify_group *group;

	ull = atomic64_inc_return(&au_hfsn_ifree);
	BUG_ON(!ull);

	mark = &hn->hn_mark;
	spin_lock(&mark->lock);
	group = mark->group;
	fsnotify_get_group(group);
	spin_unlock(&mark->lock);
	lockdep_off();
	fsnotify_destroy_mark(mark, group);
	fsnotify_put_mark(mark);
	fsnotify_put_group(group);
	lockdep_on();

	/* free hn by myself */
	return 0;
}

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

static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
{
	struct fsnotify_mark *mark;

	mark = &hinode->hi_notify->hn_mark;
	spin_lock(&mark->lock);
	if (do_set) {
		AuDebugOn(mark->mask & AuHfsnMask);
		mark->mask |= AuHfsnMask;
	} else {
		AuDebugOn(!(mark->mask & AuHfsnMask));
		mark->mask &= ~AuHfsnMask;
	}
	spin_unlock(&mark->lock);
	/* fsnotify_recalc_inode_mask(hinode->hi_inode); */
}

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

/* #define AuDbgHnotify */
#ifdef AuDbgHnotify
static char *au_hfsn_name(u32 mask)
{
#ifdef CONFIG_AUFS_DEBUG
#define test_ret(flag)				\
	do {					\
		if (mask & flag)		\
			return #flag;		\
	} while (0)
	test_ret(FS_ACCESS);
	test_ret(FS_MODIFY);
	test_ret(FS_ATTRIB);
	test_ret(FS_CLOSE_WRITE);
	test_ret(FS_CLOSE_NOWRITE);
	test_ret(FS_OPEN);
	test_ret(FS_MOVED_FROM);
	test_ret(FS_MOVED_TO);
	test_ret(FS_CREATE);
	test_ret(FS_DELETE);
	test_ret(FS_DELETE_SELF);
	test_ret(FS_MOVE_SELF);
	test_ret(FS_UNMOUNT);
	test_ret(FS_Q_OVERFLOW);
	test_ret(FS_IN_IGNORED);
	test_ret(FS_ISDIR);
	test_ret(FS_IN_ONESHOT);
	test_ret(FS_EVENT_ON_CHILD);
	return "";
#undef test_ret
#else
	return "??";
#endif
}
#endif

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

static void au_hfsn_free_group(struct fsnotify_group *group)
{
	struct au_br_hfsnotify *hfsn = group->private;

	/* AuDbg("here\n"); */
	au_kfree_try_rcu(hfsn);
}

static int au_hfsn_handle_event(struct fsnotify_group *group,
				u32 mask, const void *data, int data_type,
164
				struct inode *dir,
165
				const struct qstr *file_name, u32 cookie,
J. R. Okajima's avatar
J. R. Okajima committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
				struct fsnotify_iter_info *iter_info)
{
	int err;
	struct au_hnotify *hnotify;
	struct inode *h_dir, *h_inode;
	struct fsnotify_mark *inode_mark;

	AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);

	err = 0;
	/* if FS_UNMOUNT happens, there must be another bug */
	AuDebugOn(mask & FS_UNMOUNT);
	if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
		goto out;

181
	h_dir = dir;
J. R. Okajima's avatar
J. R. Okajima committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
	h_inode = NULL;
#ifdef AuDbgHnotify
	au_debug_on();
	if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
	    || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
		AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
		      h_dir->i_ino, mask, au_hfsn_name(mask),
		      AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
		/* WARN_ON(1); */
	}
	au_debug_off();
#endif

	inode_mark = fsnotify_iter_inode_mark(iter_info);
	AuDebugOn(!inode_mark);
	hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
198
	err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
J. R. Okajima's avatar
J. R. Okajima committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

out:
	return err;
}

static struct fsnotify_ops au_hfsn_ops = {
	.handle_event		= au_hfsn_handle_event,
	.free_group_priv	= au_hfsn_free_group,
	.free_mark		= au_hfsn_free_mark
};

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

static void au_hfsn_fin_br(struct au_branch *br)
{
	struct au_br_hfsnotify *hfsn;

	hfsn = br->br_hfsn;
	if (hfsn) {
		lockdep_off();
		fsnotify_put_group(hfsn->hfsn_group);
		lockdep_on();
	}
}

static int au_hfsn_init_br(struct au_branch *br, int perm)
{
	int err;
	struct fsnotify_group *group;
	struct au_br_hfsnotify *hfsn;

	err = 0;
	br->br_hfsn = NULL;
	if (!au_br_hnotifyable(perm))
		goto out;

	err = -ENOMEM;
	hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
	if (unlikely(!hfsn))
		goto out;

	err = 0;
	group = fsnotify_alloc_group(&au_hfsn_ops);
	if (IS_ERR(group)) {
		err = PTR_ERR(group);
		pr_err("fsnotify_alloc_group() failed, %d\n", err);
		goto out_hfsn;
	}

	group->private = hfsn;
	hfsn->hfsn_group = group;
	br->br_hfsn = hfsn;
	goto out; /* success */

out_hfsn:
	au_kfree_try_rcu(hfsn);
out:
	return err;
}

static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
{
	int err;

	err = 0;
	if (!br->br_hfsn)
		err = au_hfsn_init_br(br, perm);

	return err;
}

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

static void au_hfsn_fin(void)
{
	AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
	wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
}

const struct au_hnotify_op au_hnotify_op = {
	.ctl		= au_hfsn_ctl,
	.alloc		= au_hfsn_alloc,
	.free		= au_hfsn_free,

	.fin		= au_hfsn_fin,

	.reset_br	= au_hfsn_reset_br,
	.fin_br		= au_hfsn_fin_br,
	.init_br	= au_hfsn_init_br
};