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 <linux/atomic.h>
29 #include <linux/fsnotify_backend.h>
33 void fsnotify_clear_marks_by_mount(struct vfsmount
*mnt
)
35 struct fsnotify_mark
*mark
, *lmark
;
37 struct mount
*m
= real_mount(mnt
);
40 spin_lock(&mnt
->mnt_root
->d_lock
);
41 hlist_for_each_entry_safe(mark
, n
, &m
->mnt_fsnotify_marks
, m
.m_list
) {
42 list_add(&mark
->m
.free_m_list
, &free_list
);
43 hlist_del_init_rcu(&mark
->m
.m_list
);
44 fsnotify_get_mark(mark
);
46 spin_unlock(&mnt
->mnt_root
->d_lock
);
48 list_for_each_entry_safe(mark
, lmark
, &free_list
, m
.free_m_list
) {
49 struct fsnotify_group
*group
;
51 spin_lock(&mark
->lock
);
52 fsnotify_get_group(mark
->group
);
54 spin_unlock(&mark
->lock
);
56 fsnotify_destroy_mark(mark
, group
);
57 fsnotify_put_mark(mark
);
58 fsnotify_put_group(group
);
62 void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group
*group
)
64 fsnotify_clear_marks_by_group_flags(group
, FSNOTIFY_MARK_FLAG_VFSMOUNT
);
68 * Recalculate the mask of events relevant to a given vfsmount locked.
70 static void fsnotify_recalc_vfsmount_mask_locked(struct vfsmount
*mnt
)
72 struct mount
*m
= real_mount(mnt
);
73 struct fsnotify_mark
*mark
;
76 assert_spin_locked(&mnt
->mnt_root
->d_lock
);
78 hlist_for_each_entry(mark
, &m
->mnt_fsnotify_marks
, m
.m_list
)
79 new_mask
|= mark
->mask
;
80 m
->mnt_fsnotify_mask
= new_mask
;
84 * Recalculate the mnt->mnt_fsnotify_mask, or the mask of all FS_* event types
85 * any notifier is interested in hearing for this mount point
87 void fsnotify_recalc_vfsmount_mask(struct vfsmount
*mnt
)
89 spin_lock(&mnt
->mnt_root
->d_lock
);
90 fsnotify_recalc_vfsmount_mask_locked(mnt
);
91 spin_unlock(&mnt
->mnt_root
->d_lock
);
94 void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark
*mark
)
96 struct vfsmount
*mnt
= mark
->m
.mnt
;
98 BUG_ON(!mutex_is_locked(&mark
->group
->mark_mutex
));
99 assert_spin_locked(&mark
->lock
);
101 spin_lock(&mnt
->mnt_root
->d_lock
);
103 hlist_del_init_rcu(&mark
->m
.m_list
);
106 fsnotify_recalc_vfsmount_mask_locked(mnt
);
108 spin_unlock(&mnt
->mnt_root
->d_lock
);
111 static struct fsnotify_mark
*fsnotify_find_vfsmount_mark_locked(struct fsnotify_group
*group
,
112 struct vfsmount
*mnt
)
114 struct mount
*m
= real_mount(mnt
);
115 struct fsnotify_mark
*mark
;
117 assert_spin_locked(&mnt
->mnt_root
->d_lock
);
119 hlist_for_each_entry(mark
, &m
->mnt_fsnotify_marks
, m
.m_list
) {
120 if (mark
->group
== group
) {
121 fsnotify_get_mark(mark
);
129 * given a group and vfsmount, find the mark associated with that combination.
130 * if found take a reference to that mark and return it, else return NULL
132 struct fsnotify_mark
*fsnotify_find_vfsmount_mark(struct fsnotify_group
*group
,
133 struct vfsmount
*mnt
)
135 struct fsnotify_mark
*mark
;
137 spin_lock(&mnt
->mnt_root
->d_lock
);
138 mark
= fsnotify_find_vfsmount_mark_locked(group
, mnt
);
139 spin_unlock(&mnt
->mnt_root
->d_lock
);
145 * Attach an initialized mark to a given group and vfsmount.
146 * These marks may be used for the fsnotify backend to determine which
147 * event types should be delivered to which groups.
149 int fsnotify_add_vfsmount_mark(struct fsnotify_mark
*mark
,
150 struct fsnotify_group
*group
, struct vfsmount
*mnt
,
153 struct mount
*m
= real_mount(mnt
);
154 struct fsnotify_mark
*lmark
, *last
= NULL
;
157 mark
->flags
|= FSNOTIFY_MARK_FLAG_VFSMOUNT
;
159 BUG_ON(!mutex_is_locked(&group
->mark_mutex
));
160 assert_spin_locked(&mark
->lock
);
162 spin_lock(&mnt
->mnt_root
->d_lock
);
166 /* is mark the first mark? */
167 if (hlist_empty(&m
->mnt_fsnotify_marks
)) {
168 hlist_add_head_rcu(&mark
->m
.m_list
, &m
->mnt_fsnotify_marks
);
172 /* should mark be in the middle of the current list? */
173 hlist_for_each_entry(lmark
, &m
->mnt_fsnotify_marks
, m
.m_list
) {
176 if ((lmark
->group
== group
) && !allow_dups
) {
181 if (mark
->group
->priority
< lmark
->group
->priority
)
184 if ((mark
->group
->priority
== lmark
->group
->priority
) &&
185 (mark
->group
< lmark
->group
))
188 hlist_add_before_rcu(&mark
->m
.m_list
, &lmark
->m
.m_list
);
192 BUG_ON(last
== NULL
);
193 /* mark should be the last entry. last is the current last entry */
194 hlist_add_after_rcu(&last
->m
.m_list
, &mark
->m
.m_list
);
196 fsnotify_recalc_vfsmount_mask_locked(mnt
);
197 spin_unlock(&mnt
->mnt_root
->d_lock
);