2 * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
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)
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.
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.
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mount.h>
24 #include <linux/mutex.h>
25 #include <linux/spinlock.h>
27 #include <asm/atomic.h>
29 #include <linux/fsnotify_backend.h>
32 void fsnotify_clear_marks_by_mount(struct vfsmount
*mnt
)
34 struct fsnotify_mark
*mark
, *lmark
;
35 struct hlist_node
*pos
, *n
;
38 spin_lock(&mnt
->mnt_root
->d_lock
);
39 hlist_for_each_entry_safe(mark
, pos
, n
, &mnt
->mnt_fsnotify_marks
, m
.m_list
) {
40 list_add(&mark
->m
.free_m_list
, &free_list
);
41 hlist_del_init_rcu(&mark
->m
.m_list
);
42 fsnotify_get_mark(mark
);
44 spin_unlock(&mnt
->mnt_root
->d_lock
);
46 list_for_each_entry_safe(mark
, lmark
, &free_list
, m
.free_m_list
) {
47 fsnotify_destroy_mark(mark
);
48 fsnotify_put_mark(mark
);
52 void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group
*group
)
54 fsnotify_clear_marks_by_group_flags(group
, FSNOTIFY_MARK_FLAG_VFSMOUNT
);
58 * Recalculate the mask of events relevant to a given vfsmount locked.
60 static void fsnotify_recalc_vfsmount_mask_locked(struct vfsmount
*mnt
)
62 struct fsnotify_mark
*mark
;
63 struct hlist_node
*pos
;
66 assert_spin_locked(&mnt
->mnt_root
->d_lock
);
68 hlist_for_each_entry(mark
, pos
, &mnt
->mnt_fsnotify_marks
, m
.m_list
)
69 new_mask
|= mark
->mask
;
70 mnt
->mnt_fsnotify_mask
= new_mask
;
74 * Recalculate the mnt->mnt_fsnotify_mask, or the mask of all FS_* event types
75 * any notifier is interested in hearing for this mount point
77 void fsnotify_recalc_vfsmount_mask(struct vfsmount
*mnt
)
79 spin_lock(&mnt
->mnt_root
->d_lock
);
80 fsnotify_recalc_vfsmount_mask_locked(mnt
);
81 spin_unlock(&mnt
->mnt_root
->d_lock
);
84 void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark
*mark
)
86 struct vfsmount
*mnt
= mark
->m
.mnt
;
88 assert_spin_locked(&mark
->lock
);
89 assert_spin_locked(&mark
->group
->mark_lock
);
91 spin_lock(&mnt
->mnt_root
->d_lock
);
93 hlist_del_init_rcu(&mark
->m
.m_list
);
96 fsnotify_recalc_vfsmount_mask_locked(mnt
);
98 spin_unlock(&mnt
->mnt_root
->d_lock
);
101 static struct fsnotify_mark
*fsnotify_find_vfsmount_mark_locked(struct fsnotify_group
*group
,
102 struct vfsmount
*mnt
)
104 struct fsnotify_mark
*mark
;
105 struct hlist_node
*pos
;
107 assert_spin_locked(&mnt
->mnt_root
->d_lock
);
109 hlist_for_each_entry(mark
, pos
, &mnt
->mnt_fsnotify_marks
, m
.m_list
) {
110 if (mark
->group
== group
) {
111 fsnotify_get_mark(mark
);
119 * given a group and vfsmount, find the mark associated with that combination.
120 * if found take a reference to that mark and return it, else return NULL
122 struct fsnotify_mark
*fsnotify_find_vfsmount_mark(struct fsnotify_group
*group
,
123 struct vfsmount
*mnt
)
125 struct fsnotify_mark
*mark
;
127 spin_lock(&mnt
->mnt_root
->d_lock
);
128 mark
= fsnotify_find_vfsmount_mark_locked(group
, mnt
);
129 spin_unlock(&mnt
->mnt_root
->d_lock
);
135 * Attach an initialized mark to a given group and vfsmount.
136 * These marks may be used for the fsnotify backend to determine which
137 * event types should be delivered to which groups.
139 int fsnotify_add_vfsmount_mark(struct fsnotify_mark
*mark
,
140 struct fsnotify_group
*group
, struct vfsmount
*mnt
,
143 struct fsnotify_mark
*lmark
;
144 struct hlist_node
*node
, *last
= NULL
;
147 mark
->flags
|= FSNOTIFY_MARK_FLAG_VFSMOUNT
;
149 assert_spin_locked(&mark
->lock
);
150 assert_spin_locked(&group
->mark_lock
);
152 spin_lock(&mnt
->mnt_root
->d_lock
);
156 /* is mark the first mark? */
157 if (hlist_empty(&mnt
->mnt_fsnotify_marks
)) {
158 hlist_add_head_rcu(&mark
->m
.m_list
, &mnt
->mnt_fsnotify_marks
);
162 /* should mark be in the middle of the current list? */
163 hlist_for_each_entry(lmark
, node
, &mnt
->mnt_fsnotify_marks
, m
.m_list
) {
166 if ((lmark
->group
== group
) && !allow_dups
) {
171 if (mark
->group
->priority
< lmark
->group
->priority
)
174 if ((mark
->group
->priority
== lmark
->group
->priority
) &&
175 (mark
->group
< lmark
->group
))
178 hlist_add_before_rcu(&mark
->m
.m_list
, &lmark
->m
.m_list
);
182 BUG_ON(last
== NULL
);
183 /* mark should be the last entry. last is the current last entry */
184 hlist_add_after_rcu(last
, &mark
->m
.m_list
);
186 fsnotify_recalc_vfsmount_mask_locked(mnt
);
187 spin_unlock(&mnt
->mnt_root
->d_lock
);