]>
Commit | Line | Data |
---|---|---|
5444e298 EP |
1 | /* |
2 | * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> | |
3 | * | |
4 | * This program 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, or (at your option) | |
7 | * any later version. | |
8 | * | |
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. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; see the file COPYING. If not, write to | |
16 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 | */ | |
18 | ||
19 | /* | |
20 | * fsnotify inode mark locking/lifetime/and refcnting | |
21 | * | |
22 | * REFCNT: | |
9756b918 LS |
23 | * The group->recnt and mark->refcnt tell how many "things" in the kernel |
24 | * currently are referencing the objects. Both kind of objects typically will | |
25 | * live inside the kernel with a refcnt of 2, one for its creation and one for | |
26 | * the reference a group and a mark hold to each other. | |
27 | * If you are holding the appropriate locks, you can take a reference and the | |
28 | * object itself is guaranteed to survive until the reference is dropped. | |
5444e298 EP |
29 | * |
30 | * LOCKING: | |
9756b918 LS |
31 | * There are 3 locks involved with fsnotify inode marks and they MUST be taken |
32 | * in order as follows: | |
5444e298 | 33 | * |
9756b918 | 34 | * group->mark_mutex |
5444e298 | 35 | * mark->lock |
5444e298 EP |
36 | * inode->i_lock |
37 | * | |
9756b918 LS |
38 | * group->mark_mutex protects the marks_list anchored inside a given group and |
39 | * each mark is hooked via the g_list. It also protects the groups private | |
40 | * data (i.e group limits). | |
41 | ||
42 | * mark->lock protects the marks attributes like its masks and flags. | |
43 | * Furthermore it protects the access to a reference of the group that the mark | |
44 | * is assigned to as well as the access to a reference of the inode/vfsmount | |
45 | * that is being watched by the mark. | |
5444e298 EP |
46 | * |
47 | * inode->i_lock protects the i_fsnotify_marks list anchored inside a | |
48 | * given inode and each mark is hooked via the i_list. (and sorta the | |
49 | * free_i_list) | |
50 | * | |
51 | * | |
52 | * LIFETIME: | |
53 | * Inode marks survive between when they are added to an inode and when their | |
54 | * refcnt==0. | |
55 | * | |
56 | * The inode mark can be cleared for a number of different reasons including: | |
57 | * - The inode is unlinked for the last time. (fsnotify_inode_remove) | |
58 | * - The inode is being evicted from cache. (fsnotify_inode_delete) | |
59 | * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes) | |
60 | * - Something explicitly requests that it be removed. (fsnotify_destroy_mark) | |
61 | * - The fsnotify_group associated with the mark is going away and all such marks | |
62 | * need to be cleaned up. (fsnotify_clear_marks_by_group) | |
63 | * | |
64 | * Worst case we are given an inode and need to clean up all the marks on that | |
65 | * inode. We take i_lock and walk the i_fsnotify_marks safely. For each | |
66 | * mark on the list we take a reference (so the mark can't disappear under us). | |
67 | * We remove that mark form the inode's list of marks and we add this mark to a | |
9756b918 LS |
68 | * private list anchored on the stack using i_free_list; we walk i_free_list |
69 | * and before we destroy the mark we make sure that we dont race with a | |
70 | * concurrent destroy_group by getting a ref to the marks group and taking the | |
71 | * groups mutex. | |
72 | ||
5444e298 EP |
73 | * Very similarly for freeing by group, except we use free_g_list. |
74 | * | |
75 | * This has the very interesting property of being able to run concurrently with | |
76 | * any (or all) other directions. | |
77 | */ | |
78 | ||
79 | #include <linux/fs.h> | |
80 | #include <linux/init.h> | |
81 | #include <linux/kernel.h> | |
75c1be48 | 82 | #include <linux/kthread.h> |
5444e298 EP |
83 | #include <linux/module.h> |
84 | #include <linux/mutex.h> | |
85 | #include <linux/slab.h> | |
86 | #include <linux/spinlock.h> | |
75c1be48 | 87 | #include <linux/srcu.h> |
5444e298 | 88 | |
60063497 | 89 | #include <linux/atomic.h> |
5444e298 EP |
90 | |
91 | #include <linux/fsnotify_backend.h> | |
92 | #include "fsnotify.h" | |
93 | ||
0918f1c3 JL |
94 | #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */ |
95 | ||
75c1be48 | 96 | struct srcu_struct fsnotify_mark_srcu; |
13d34ac6 JL |
97 | static DEFINE_SPINLOCK(destroy_lock); |
98 | static LIST_HEAD(destroy_list); | |
0918f1c3 | 99 | |
35e48176 JK |
100 | static void fsnotify_mark_destroy_workfn(struct work_struct *work); |
101 | static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn); | |
75c1be48 | 102 | |
5444e298 EP |
103 | void fsnotify_get_mark(struct fsnotify_mark *mark) |
104 | { | |
105 | atomic_inc(&mark->refcnt); | |
106 | } | |
107 | ||
108 | void fsnotify_put_mark(struct fsnotify_mark *mark) | |
109 | { | |
23e964c2 LS |
110 | if (atomic_dec_and_test(&mark->refcnt)) { |
111 | if (mark->group) | |
112 | fsnotify_put_group(mark->group); | |
5444e298 | 113 | mark->free_mark(mark); |
23e964c2 | 114 | } |
5444e298 | 115 | } |
e14748e8 | 116 | EXPORT_SYMBOL_GPL(fsnotify_put_mark); |
5444e298 | 117 | |
0809ab69 JK |
118 | /* Calculate mask of events for a list of marks */ |
119 | u32 fsnotify_recalc_mask(struct hlist_head *head) | |
120 | { | |
121 | u32 new_mask = 0; | |
122 | struct fsnotify_mark *mark; | |
123 | ||
124 | hlist_for_each_entry(mark, head, obj_list) | |
125 | new_mask |= mark->mask; | |
126 | return new_mask; | |
127 | } | |
128 | ||
5444e298 | 129 | /* |
4712e722 JK |
130 | * Remove mark from inode / vfsmount list, group list, drop inode reference |
131 | * if we got one. | |
132 | * | |
133 | * Must be called with group->mark_mutex held. | |
5444e298 | 134 | */ |
4712e722 | 135 | void fsnotify_detach_mark(struct fsnotify_mark *mark) |
5444e298 | 136 | { |
0d48b7f0 | 137 | struct inode *inode = NULL; |
4712e722 | 138 | struct fsnotify_group *group = mark->group; |
5444e298 | 139 | |
d5a335b8 LS |
140 | BUG_ON(!mutex_is_locked(&group->mark_mutex)); |
141 | ||
104d06f0 | 142 | spin_lock(&mark->lock); |
5444e298 | 143 | |
700307a2 | 144 | /* something else already called this function on this mark */ |
4712e722 | 145 | if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { |
5444e298 | 146 | spin_unlock(&mark->lock); |
e2a29943 | 147 | return; |
5444e298 EP |
148 | } |
149 | ||
4712e722 | 150 | mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; |
700307a2 | 151 | |
0d48b7f0 | 152 | if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) { |
0809ab69 | 153 | inode = mark->inode; |
b31d397e | 154 | fsnotify_destroy_inode_mark(mark); |
0d48b7f0 EP |
155 | } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) |
156 | fsnotify_destroy_vfsmount_mark(mark); | |
5444e298 EP |
157 | else |
158 | BUG(); | |
4712e722 JK |
159 | /* |
160 | * Note that we didn't update flags telling whether inode cares about | |
161 | * what's happening with children. We update these flags from | |
162 | * __fsnotify_parent() lazily when next event happens on one of our | |
163 | * children. | |
164 | */ | |
5444e298 EP |
165 | |
166 | list_del_init(&mark->g_list); | |
d725e66c | 167 | |
5444e298 | 168 | spin_unlock(&mark->lock); |
d5a335b8 | 169 | |
6960b0d9 LS |
170 | if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) |
171 | iput(inode); | |
4712e722 JK |
172 | |
173 | atomic_dec(&group->num_marks); | |
174 | } | |
175 | ||
176 | /* | |
35e48176 JK |
177 | * Prepare mark for freeing and add it to the list of marks prepared for |
178 | * freeing. The actual freeing must happen after SRCU period ends and the | |
179 | * caller is responsible for this. | |
180 | * | |
181 | * The function returns true if the mark was added to the list of marks for | |
182 | * freeing. The function returns false if someone else has already called | |
183 | * __fsnotify_free_mark() for the mark. | |
4712e722 | 184 | */ |
35e48176 | 185 | static bool __fsnotify_free_mark(struct fsnotify_mark *mark) |
4712e722 JK |
186 | { |
187 | struct fsnotify_group *group = mark->group; | |
188 | ||
189 | spin_lock(&mark->lock); | |
190 | /* something else already called this function on this mark */ | |
191 | if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { | |
192 | spin_unlock(&mark->lock); | |
35e48176 | 193 | return false; |
4712e722 JK |
194 | } |
195 | mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; | |
196 | spin_unlock(&mark->lock); | |
5444e298 | 197 | |
d725e66c LT |
198 | /* |
199 | * Some groups like to know that marks are being freed. This is a | |
200 | * callback to the group function to let it know that this mark | |
201 | * is being freed. | |
202 | */ | |
203 | if (group->ops->freeing_mark) | |
204 | group->ops->freeing_mark(mark, group); | |
35e48176 JK |
205 | |
206 | spin_lock(&destroy_lock); | |
207 | list_add(&mark->g_list, &destroy_list); | |
208 | spin_unlock(&destroy_lock); | |
209 | ||
210 | return true; | |
211 | } | |
212 | ||
213 | /* | |
214 | * Free fsnotify mark. The freeing is actually happening from a workqueue which | |
215 | * first waits for srcu period end. Caller must have a reference to the mark | |
216 | * or be protected by fsnotify_mark_srcu. | |
217 | */ | |
218 | void fsnotify_free_mark(struct fsnotify_mark *mark) | |
219 | { | |
220 | if (__fsnotify_free_mark(mark)) { | |
221 | queue_delayed_work(system_unbound_wq, &reaper_work, | |
222 | FSNOTIFY_REAPER_DELAY); | |
223 | } | |
d5a335b8 LS |
224 | } |
225 | ||
226 | void fsnotify_destroy_mark(struct fsnotify_mark *mark, | |
227 | struct fsnotify_group *group) | |
228 | { | |
6960b0d9 | 229 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); |
4712e722 | 230 | fsnotify_detach_mark(mark); |
d5a335b8 | 231 | mutex_unlock(&group->mark_mutex); |
4712e722 | 232 | fsnotify_free_mark(mark); |
5444e298 | 233 | } |
e14748e8 | 234 | EXPORT_SYMBOL_GPL(fsnotify_destroy_mark); |
5444e298 | 235 | |
925d1132 | 236 | void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock) |
0809ab69 | 237 | { |
925d1132 | 238 | struct fsnotify_mark *mark; |
0809ab69 | 239 | |
925d1132 JK |
240 | while (1) { |
241 | /* | |
242 | * We have to be careful since we can race with e.g. | |
243 | * fsnotify_clear_marks_by_group() and once we drop 'lock', | |
244 | * mark can get removed from the obj_list and destroyed. But | |
245 | * we are holding mark reference so mark cannot be freed and | |
246 | * calling fsnotify_destroy_mark() more than once is fine. | |
247 | */ | |
248 | spin_lock(lock); | |
249 | if (hlist_empty(head)) { | |
250 | spin_unlock(lock); | |
251 | break; | |
252 | } | |
253 | mark = hlist_entry(head->first, struct fsnotify_mark, obj_list); | |
254 | /* | |
255 | * We don't update i_fsnotify_mask / mnt_fsnotify_mask here | |
256 | * since inode / mount is going away anyway. So just remove | |
257 | * mark from the list. | |
258 | */ | |
259 | hlist_del_init_rcu(&mark->obj_list); | |
260 | fsnotify_get_mark(mark); | |
261 | spin_unlock(lock); | |
262 | fsnotify_destroy_mark(mark, mark->group); | |
0809ab69 | 263 | fsnotify_put_mark(mark); |
0809ab69 JK |
264 | } |
265 | } | |
266 | ||
90b1e7a5 EP |
267 | void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) |
268 | { | |
269 | assert_spin_locked(&mark->lock); | |
270 | ||
271 | mark->mask = mask; | |
272 | ||
273 | if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) | |
274 | fsnotify_set_inode_mark_mask_locked(mark, mask); | |
275 | } | |
276 | ||
33af5e32 EP |
277 | void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) |
278 | { | |
279 | assert_spin_locked(&mark->lock); | |
280 | ||
281 | mark->ignored_mask = mask; | |
282 | } | |
90b1e7a5 | 283 | |
8edc6e16 JK |
284 | /* |
285 | * Sorting function for lists of fsnotify marks. | |
286 | * | |
287 | * Fanotify supports different notification classes (reflected as priority of | |
288 | * notification group). Events shall be passed to notification groups in | |
289 | * decreasing priority order. To achieve this marks in notification lists for | |
290 | * inodes and vfsmounts are sorted so that priorities of corresponding groups | |
291 | * are descending. | |
292 | * | |
293 | * Furthermore correct handling of the ignore mask requires processing inode | |
294 | * and vfsmount marks of each group together. Using the group address as | |
295 | * further sort criterion provides a unique sorting order and thus we can | |
296 | * merge inode and vfsmount lists of marks in linear time and find groups | |
297 | * present in both lists. | |
298 | * | |
299 | * A return value of 1 signifies that b has priority over a. | |
300 | * A return value of 0 signifies that the two marks have to be handled together. | |
301 | * A return value of -1 signifies that a has priority over b. | |
302 | */ | |
303 | int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b) | |
304 | { | |
305 | if (a == b) | |
306 | return 0; | |
307 | if (!a) | |
308 | return 1; | |
309 | if (!b) | |
310 | return -1; | |
311 | if (a->priority < b->priority) | |
312 | return 1; | |
313 | if (a->priority > b->priority) | |
314 | return -1; | |
315 | if (a < b) | |
316 | return 1; | |
317 | return -1; | |
318 | } | |
319 | ||
0809ab69 JK |
320 | /* Add mark into proper place in given list of marks */ |
321 | int fsnotify_add_mark_list(struct hlist_head *head, struct fsnotify_mark *mark, | |
322 | int allow_dups) | |
323 | { | |
324 | struct fsnotify_mark *lmark, *last = NULL; | |
325 | int cmp; | |
326 | ||
327 | /* is mark the first mark? */ | |
328 | if (hlist_empty(head)) { | |
329 | hlist_add_head_rcu(&mark->obj_list, head); | |
330 | return 0; | |
331 | } | |
332 | ||
333 | /* should mark be in the middle of the current list? */ | |
334 | hlist_for_each_entry(lmark, head, obj_list) { | |
335 | last = lmark; | |
336 | ||
337 | if ((lmark->group == mark->group) && !allow_dups) | |
338 | return -EEXIST; | |
339 | ||
340 | cmp = fsnotify_compare_groups(lmark->group, mark->group); | |
341 | if (cmp >= 0) { | |
342 | hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list); | |
343 | return 0; | |
344 | } | |
345 | } | |
346 | ||
347 | BUG_ON(last == NULL); | |
348 | /* mark should be the last entry. last is the current last entry */ | |
349 | hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); | |
350 | return 0; | |
351 | } | |
352 | ||
5444e298 EP |
353 | /* |
354 | * Attach an initialized mark to a given group and fs object. | |
355 | * These marks may be used for the fsnotify backend to determine which | |
356 | * event types should be delivered to which group. | |
357 | */ | |
d5a335b8 LS |
358 | int fsnotify_add_mark_locked(struct fsnotify_mark *mark, |
359 | struct fsnotify_group *group, struct inode *inode, | |
360 | struct vfsmount *mnt, int allow_dups) | |
5444e298 EP |
361 | { |
362 | int ret = 0; | |
363 | ||
5444e298 EP |
364 | BUG_ON(inode && mnt); |
365 | BUG_ON(!inode && !mnt); | |
d5a335b8 | 366 | BUG_ON(!mutex_is_locked(&group->mark_mutex)); |
5444e298 | 367 | |
5444e298 EP |
368 | /* |
369 | * LOCKING ORDER!!!! | |
986ab098 | 370 | * group->mark_mutex |
104d06f0 | 371 | * mark->lock |
5444e298 EP |
372 | * inode->i_lock |
373 | */ | |
104d06f0 | 374 | spin_lock(&mark->lock); |
4712e722 | 375 | mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; |
700307a2 | 376 | |
23e964c2 | 377 | fsnotify_get_group(group); |
5444e298 EP |
378 | mark->group = group; |
379 | list_add(&mark->g_list, &group->marks_list); | |
380 | atomic_inc(&group->num_marks); | |
381 | fsnotify_get_mark(mark); /* for i_list and g_list */ | |
382 | ||
383 | if (inode) { | |
384 | ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups); | |
385 | if (ret) | |
386 | goto err; | |
0d48b7f0 EP |
387 | } else if (mnt) { |
388 | ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups); | |
389 | if (ret) | |
390 | goto err; | |
5444e298 EP |
391 | } else { |
392 | BUG(); | |
393 | } | |
394 | ||
90b1e7a5 EP |
395 | /* this will pin the object if appropriate */ |
396 | fsnotify_set_mark_mask_locked(mark, mark->mask); | |
5444e298 EP |
397 | spin_unlock(&mark->lock); |
398 | ||
399 | if (inode) | |
400 | __fsnotify_update_child_dentry_flags(inode); | |
401 | ||
402 | return ret; | |
403 | err: | |
700307a2 | 404 | mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; |
5444e298 | 405 | list_del_init(&mark->g_list); |
23e964c2 | 406 | fsnotify_put_group(group); |
75c1be48 | 407 | mark->group = NULL; |
5444e298 | 408 | atomic_dec(&group->num_marks); |
5444e298 | 409 | |
5444e298 EP |
410 | spin_unlock(&mark->lock); |
411 | ||
13d34ac6 JL |
412 | spin_lock(&destroy_lock); |
413 | list_add(&mark->g_list, &destroy_list); | |
414 | spin_unlock(&destroy_lock); | |
0918f1c3 JL |
415 | queue_delayed_work(system_unbound_wq, &reaper_work, |
416 | FSNOTIFY_REAPER_DELAY); | |
13d34ac6 | 417 | |
5444e298 EP |
418 | return ret; |
419 | } | |
e14748e8 | 420 | EXPORT_SYMBOL_GPL(fsnotify_add_mark); |
5444e298 | 421 | |
d5a335b8 LS |
422 | int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group, |
423 | struct inode *inode, struct vfsmount *mnt, int allow_dups) | |
424 | { | |
425 | int ret; | |
426 | mutex_lock(&group->mark_mutex); | |
427 | ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups); | |
428 | mutex_unlock(&group->mark_mutex); | |
429 | return ret; | |
430 | } | |
431 | ||
0809ab69 JK |
432 | /* |
433 | * Given a list of marks, find the mark associated with given group. If found | |
434 | * take a reference to that mark and return it, else return NULL. | |
435 | */ | |
436 | struct fsnotify_mark *fsnotify_find_mark(struct hlist_head *head, | |
437 | struct fsnotify_group *group) | |
438 | { | |
439 | struct fsnotify_mark *mark; | |
440 | ||
441 | hlist_for_each_entry(mark, head, obj_list) { | |
442 | if (mark->group == group) { | |
443 | fsnotify_get_mark(mark); | |
444 | return mark; | |
445 | } | |
446 | } | |
447 | return NULL; | |
448 | } | |
449 | ||
5444e298 | 450 | /* |
d725e66c | 451 | * clear any marks in a group in which mark->flags & flags is true |
5444e298 | 452 | */ |
4d92604c EP |
453 | void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, |
454 | unsigned int flags) | |
5444e298 EP |
455 | { |
456 | struct fsnotify_mark *lmark, *mark; | |
8f2f3eb5 | 457 | LIST_HEAD(to_free); |
5444e298 | 458 | |
8f2f3eb5 JK |
459 | /* |
460 | * We have to be really careful here. Anytime we drop mark_mutex, e.g. | |
461 | * fsnotify_clear_marks_by_inode() can come and free marks. Even in our | |
462 | * to_free list so we have to use mark_mutex even when accessing that | |
463 | * list. And freeing mark requires us to drop mark_mutex. So we can | |
464 | * reliably free only the first mark in the list. That's why we first | |
465 | * move marks to free to to_free list in one go and then free marks in | |
466 | * to_free list one by one. | |
467 | */ | |
6960b0d9 | 468 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); |
5444e298 | 469 | list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { |
8f2f3eb5 JK |
470 | if (mark->flags & flags) |
471 | list_move(&mark->g_list, &to_free); | |
5444e298 | 472 | } |
986ab098 | 473 | mutex_unlock(&group->mark_mutex); |
8f2f3eb5 JK |
474 | |
475 | while (1) { | |
476 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); | |
477 | if (list_empty(&to_free)) { | |
478 | mutex_unlock(&group->mark_mutex); | |
479 | break; | |
480 | } | |
481 | mark = list_first_entry(&to_free, struct fsnotify_mark, g_list); | |
482 | fsnotify_get_mark(mark); | |
4712e722 | 483 | fsnotify_detach_mark(mark); |
8f2f3eb5 | 484 | mutex_unlock(&group->mark_mutex); |
4712e722 | 485 | fsnotify_free_mark(mark); |
8f2f3eb5 JK |
486 | fsnotify_put_mark(mark); |
487 | } | |
5444e298 EP |
488 | } |
489 | ||
4d92604c | 490 | /* |
35e48176 JK |
491 | * Given a group, prepare for freeing all the marks associated with that group. |
492 | * The marks are attached to the list of marks prepared for destruction, the | |
493 | * caller is responsible for freeing marks in that list after SRCU period has | |
494 | * ended. | |
4d92604c | 495 | */ |
35e48176 | 496 | void fsnotify_detach_group_marks(struct fsnotify_group *group) |
4d92604c | 497 | { |
35e48176 JK |
498 | struct fsnotify_mark *mark; |
499 | ||
500 | while (1) { | |
501 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); | |
502 | if (list_empty(&group->marks_list)) { | |
503 | mutex_unlock(&group->mark_mutex); | |
504 | break; | |
505 | } | |
506 | mark = list_first_entry(&group->marks_list, | |
507 | struct fsnotify_mark, g_list); | |
508 | fsnotify_get_mark(mark); | |
509 | fsnotify_detach_mark(mark); | |
510 | mutex_unlock(&group->mark_mutex); | |
511 | __fsnotify_free_mark(mark); | |
512 | fsnotify_put_mark(mark); | |
513 | } | |
4d92604c EP |
514 | } |
515 | ||
5444e298 EP |
516 | /* |
517 | * Nothing fancy, just initialize lists and locks and counters. | |
518 | */ | |
519 | void fsnotify_init_mark(struct fsnotify_mark *mark, | |
520 | void (*free_mark)(struct fsnotify_mark *mark)) | |
521 | { | |
ba643f04 | 522 | memset(mark, 0, sizeof(*mark)); |
5444e298 EP |
523 | spin_lock_init(&mark->lock); |
524 | atomic_set(&mark->refcnt, 1); | |
5444e298 EP |
525 | mark->free_mark = free_mark; |
526 | } | |
e14748e8 | 527 | EXPORT_SYMBOL_GPL(fsnotify_init_mark); |
13d34ac6 | 528 | |
35e48176 JK |
529 | /* |
530 | * Destroy all marks in destroy_list, waits for SRCU period to finish before | |
531 | * actually freeing marks. | |
532 | */ | |
533 | void fsnotify_mark_destroy_list(void) | |
13d34ac6 JL |
534 | { |
535 | struct fsnotify_mark *mark, *next; | |
536 | struct list_head private_destroy_list; | |
537 | ||
0918f1c3 JL |
538 | spin_lock(&destroy_lock); |
539 | /* exchange the list head */ | |
540 | list_replace_init(&destroy_list, &private_destroy_list); | |
541 | spin_unlock(&destroy_lock); | |
13d34ac6 | 542 | |
0918f1c3 | 543 | synchronize_srcu(&fsnotify_mark_srcu); |
13d34ac6 | 544 | |
0918f1c3 JL |
545 | list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) { |
546 | list_del_init(&mark->g_list); | |
547 | fsnotify_put_mark(mark); | |
13d34ac6 | 548 | } |
13d34ac6 | 549 | } |
35e48176 JK |
550 | |
551 | static void fsnotify_mark_destroy_workfn(struct work_struct *work) | |
552 | { | |
553 | fsnotify_mark_destroy_list(); | |
554 | } |