blob: 778fe6cae3b0949ea671246efe52bee6cf158985 [file] [log] [blame]
Eric Paris0d48b7f2009-12-17 21:24:27 -05001/*
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#include <linux/fs.h>
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>
Eric Paris0d48b7f2009-12-17 21:24:27 -050025#include <linux/spinlock.h>
Eric Paris0d48b7f2009-12-17 21:24:27 -050026
Arun Sharma600634972011-07-26 16:09:06 -070027#include <linux/atomic.h>
Eric Paris0d48b7f2009-12-17 21:24:27 -050028
29#include <linux/fsnotify_backend.h>
30#include "fsnotify.h"
31
32void fsnotify_clear_marks_by_mount(struct vfsmount *mnt)
33{
34 struct fsnotify_mark *mark, *lmark;
35 struct hlist_node *pos, *n;
36 LIST_HEAD(free_list);
37
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);
Eric Parisa4c6e992010-07-28 10:18:38 -040041 hlist_del_init_rcu(&mark->m.m_list);
Eric Paris0d48b7f2009-12-17 21:24:27 -050042 fsnotify_get_mark(mark);
43 }
44 spin_unlock(&mnt->mnt_root->d_lock);
45
46 list_for_each_entry_safe(mark, lmark, &free_list, m.free_m_list) {
47 fsnotify_destroy_mark(mark);
48 fsnotify_put_mark(mark);
49 }
50}
51
Eric Paris4d926042009-12-17 21:24:34 -050052void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
53{
54 fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_VFSMOUNT);
55}
56
Eric Paris0d48b7f2009-12-17 21:24:27 -050057/*
58 * Recalculate the mask of events relevant to a given vfsmount locked.
59 */
60static void fsnotify_recalc_vfsmount_mask_locked(struct vfsmount *mnt)
61{
62 struct fsnotify_mark *mark;
63 struct hlist_node *pos;
64 __u32 new_mask = 0;
65
66 assert_spin_locked(&mnt->mnt_root->d_lock);
67
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;
71}
72
73/*
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
76 */
77void fsnotify_recalc_vfsmount_mask(struct vfsmount *mnt)
78{
79 spin_lock(&mnt->mnt_root->d_lock);
80 fsnotify_recalc_vfsmount_mask_locked(mnt);
81 spin_unlock(&mnt->mnt_root->d_lock);
82}
83
84void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark *mark)
85{
86 struct vfsmount *mnt = mark->m.mnt;
87
88 assert_spin_locked(&mark->lock);
89 assert_spin_locked(&mark->group->mark_lock);
90
91 spin_lock(&mnt->mnt_root->d_lock);
92
Eric Parisa4c6e992010-07-28 10:18:38 -040093 hlist_del_init_rcu(&mark->m.m_list);
Eric Paris0d48b7f2009-12-17 21:24:27 -050094 mark->m.mnt = NULL;
95
96 fsnotify_recalc_vfsmount_mask_locked(mnt);
97
98 spin_unlock(&mnt->mnt_root->d_lock);
99}
100
101static struct fsnotify_mark *fsnotify_find_vfsmount_mark_locked(struct fsnotify_group *group,
102 struct vfsmount *mnt)
103{
104 struct fsnotify_mark *mark;
105 struct hlist_node *pos;
106
107 assert_spin_locked(&mnt->mnt_root->d_lock);
108
109 hlist_for_each_entry(mark, pos, &mnt->mnt_fsnotify_marks, m.m_list) {
110 if (mark->group == group) {
111 fsnotify_get_mark(mark);
112 return mark;
113 }
114 }
115 return NULL;
116}
117
118/*
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
121 */
122struct fsnotify_mark *fsnotify_find_vfsmount_mark(struct fsnotify_group *group,
123 struct vfsmount *mnt)
124{
125 struct fsnotify_mark *mark;
126
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);
130
131 return mark;
132}
133
134/*
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.
138 */
139int fsnotify_add_vfsmount_mark(struct fsnotify_mark *mark,
140 struct fsnotify_group *group, struct vfsmount *mnt,
141 int allow_dups)
142{
Eric Paris0c6532e2010-07-28 10:18:38 -0400143 struct fsnotify_mark *lmark;
144 struct hlist_node *node, *last = NULL;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500145 int ret = 0;
146
Eric Paris700307a2010-07-28 10:18:38 -0400147 mark->flags |= FSNOTIFY_MARK_FLAG_VFSMOUNT;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500148
Eric Paris0d48b7f2009-12-17 21:24:27 -0500149 assert_spin_locked(&mark->lock);
150 assert_spin_locked(&group->mark_lock);
151
152 spin_lock(&mnt->mnt_root->d_lock);
153
Eric Paris0c6532e2010-07-28 10:18:38 -0400154 mark->m.mnt = mnt;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500155
Eric Paris0c6532e2010-07-28 10:18:38 -0400156 /* is mark the first mark? */
157 if (hlist_empty(&mnt->mnt_fsnotify_marks)) {
Eric Parisa4c6e992010-07-28 10:18:38 -0400158 hlist_add_head_rcu(&mark->m.m_list, &mnt->mnt_fsnotify_marks);
Eric Paris0c6532e2010-07-28 10:18:38 -0400159 goto out;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500160 }
161
Eric Paris0c6532e2010-07-28 10:18:38 -0400162 /* should mark be in the middle of the current list? */
163 hlist_for_each_entry(lmark, node, &mnt->mnt_fsnotify_marks, m.m_list) {
164 last = node;
165
166 if ((lmark->group == group) && !allow_dups) {
167 ret = -EEXIST;
168 goto out;
169 }
170
Eric Paris6ad2d4e2010-10-28 17:21:56 -0400171 if (mark->group->priority < lmark->group->priority)
172 continue;
173
174 if ((mark->group->priority == lmark->group->priority) &&
175 (mark->group < lmark->group))
Eric Paris0c6532e2010-07-28 10:18:38 -0400176 continue;
177
Eric Parisa4c6e992010-07-28 10:18:38 -0400178 hlist_add_before_rcu(&mark->m.m_list, &lmark->m.m_list);
Eric Paris0c6532e2010-07-28 10:18:38 -0400179 goto out;
180 }
181
182 BUG_ON(last == NULL);
183 /* mark should be the last entry. last is the current last entry */
Eric Parisa4c6e992010-07-28 10:18:38 -0400184 hlist_add_after_rcu(last, &mark->m.m_list);
Eric Paris0c6532e2010-07-28 10:18:38 -0400185out:
186 fsnotify_recalc_vfsmount_mask_locked(mnt);
Eric Paris0d48b7f2009-12-17 21:24:27 -0500187 spin_unlock(&mnt->mnt_root->d_lock);
188
189 return ret;
190}