1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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#include <linux/fs.h>
86#include <linux/init.h>
87#include <linux/kernel.h>
88#include <linux/kthread.h>
89#include <linux/module.h>
90#include <linux/mutex.h>
91#include <linux/slab.h>
92#include <linux/spinlock.h>
93#include <linux/srcu.h>
94
95#include <linux/atomic.h>
96
97#include <linux/fsnotify_backend.h>
98#include "fsnotify.h"
99
100struct srcu_struct fsnotify_mark_srcu;
101static DEFINE_SPINLOCK(destroy_lock);
102static LIST_HEAD(destroy_list);
103static DECLARE_WAIT_QUEUE_HEAD(destroy_waitq);
104
105void fsnotify_get_mark(struct fsnotify_mark *mark)
106{
107 atomic_inc(&mark->refcnt);
108}
109
110void fsnotify_put_mark(struct fsnotify_mark *mark)
111{
112 if (atomic_dec_and_test(&mark->refcnt))
113 mark->free_mark(mark);
114}
115
116
117
118
119
120
121void fsnotify_destroy_mark(struct fsnotify_mark *mark)
122{
123 struct fsnotify_group *group;
124 struct inode *inode = NULL;
125
126 spin_lock(&mark->lock);
127
128 group = mark->group;
129
130
131 if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
132 spin_unlock(&mark->lock);
133 return;
134 }
135
136 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
137
138 spin_lock(&group->mark_lock);
139
140 if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
141 inode = mark->i.inode;
142 fsnotify_destroy_inode_mark(mark);
143 } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT)
144 fsnotify_destroy_vfsmount_mark(mark);
145 else
146 BUG();
147
148 list_del_init(&mark->g_list);
149
150 spin_unlock(&group->mark_lock);
151 spin_unlock(&mark->lock);
152
153 spin_lock(&destroy_lock);
154 list_add(&mark->destroy_list, &destroy_list);
155 spin_unlock(&destroy_lock);
156 wake_up(&destroy_waitq);
157
158
159
160
161
162
163 if (group->ops->freeing_mark)
164 group->ops->freeing_mark(mark, group);
165
166
167
168
169
170
171
172
173
174
175
176
177
178 if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
179 iput(inode);
180
181
182
183
184
185
186
187
188
189
190
191 if (unlikely(atomic_dec_and_test(&group->num_marks)))
192 fsnotify_final_destroy_group(group);
193}
194
195void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
196{
197 assert_spin_locked(&mark->lock);
198
199 mark->mask = mask;
200
201 if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
202 fsnotify_set_inode_mark_mask_locked(mark, mask);
203}
204
205void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask)
206{
207 assert_spin_locked(&mark->lock);
208
209 mark->ignored_mask = mask;
210}
211
212
213
214
215
216
217int fsnotify_add_mark(struct fsnotify_mark *mark,
218 struct fsnotify_group *group, struct inode *inode,
219 struct vfsmount *mnt, int allow_dups)
220{
221 int ret = 0;
222
223 BUG_ON(inode && mnt);
224 BUG_ON(!inode && !mnt);
225
226
227
228
229
230
231
232 spin_lock(&mark->lock);
233 spin_lock(&group->mark_lock);
234
235 mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE;
236
237 mark->group = group;
238 list_add(&mark->g_list, &group->marks_list);
239 atomic_inc(&group->num_marks);
240 fsnotify_get_mark(mark);
241
242 if (inode) {
243 ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups);
244 if (ret)
245 goto err;
246 } else if (mnt) {
247 ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups);
248 if (ret)
249 goto err;
250 } else {
251 BUG();
252 }
253
254 spin_unlock(&group->mark_lock);
255
256
257 fsnotify_set_mark_mask_locked(mark, mark->mask);
258
259 spin_unlock(&mark->lock);
260
261 if (inode)
262 __fsnotify_update_child_dentry_flags(inode);
263
264 return ret;
265err:
266 mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
267 list_del_init(&mark->g_list);
268 mark->group = NULL;
269 atomic_dec(&group->num_marks);
270
271 spin_unlock(&group->mark_lock);
272 spin_unlock(&mark->lock);
273
274 spin_lock(&destroy_lock);
275 list_add(&mark->destroy_list, &destroy_list);
276 spin_unlock(&destroy_lock);
277 wake_up(&destroy_waitq);
278
279 return ret;
280}
281
282
283
284
285void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
286 unsigned int flags)
287{
288 struct fsnotify_mark *lmark, *mark;
289 LIST_HEAD(free_list);
290
291 spin_lock(&group->mark_lock);
292 list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
293 if (mark->flags & flags) {
294 list_add(&mark->free_g_list, &free_list);
295 list_del_init(&mark->g_list);
296 fsnotify_get_mark(mark);
297 }
298 }
299 spin_unlock(&group->mark_lock);
300
301 list_for_each_entry_safe(mark, lmark, &free_list, free_g_list) {
302 fsnotify_destroy_mark(mark);
303 fsnotify_put_mark(mark);
304 }
305}
306
307
308
309
310void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
311{
312 fsnotify_clear_marks_by_group_flags(group, (unsigned int)-1);
313}
314
315void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old)
316{
317 assert_spin_locked(&old->lock);
318 new->i.inode = old->i.inode;
319 new->m.mnt = old->m.mnt;
320 new->group = old->group;
321 new->mask = old->mask;
322 new->free_mark = old->free_mark;
323}
324
325
326
327
328void fsnotify_init_mark(struct fsnotify_mark *mark,
329 void (*free_mark)(struct fsnotify_mark *mark))
330{
331 memset(mark, 0, sizeof(*mark));
332 spin_lock_init(&mark->lock);
333 atomic_set(&mark->refcnt, 1);
334 mark->free_mark = free_mark;
335}
336
337static int fsnotify_mark_destroy(void *ignored)
338{
339 struct fsnotify_mark *mark, *next;
340 LIST_HEAD(private_destroy_list);
341
342 for (;;) {
343 spin_lock(&destroy_lock);
344
345 list_replace_init(&destroy_list, &private_destroy_list);
346 spin_unlock(&destroy_lock);
347
348 synchronize_srcu(&fsnotify_mark_srcu);
349
350 list_for_each_entry_safe(mark, next, &private_destroy_list, destroy_list) {
351 list_del_init(&mark->destroy_list);
352 fsnotify_put_mark(mark);
353 }
354
355 wait_event_interruptible(destroy_waitq, !list_empty(&destroy_list));
356 }
357
358 return 0;
359}
360
361static int __init fsnotify_mark_init(void)
362{
363 struct task_struct *thread;
364
365 thread = kthread_run(fsnotify_mark_destroy, NULL,
366 "fsnotify_mark");
367 if (IS_ERR(thread))
368 panic("unable to start fsnotify mark destruction thread.");
369
370 return 0;
371}
372device_initcall(fsnotify_mark_init);
373