Commit | Line | Data |
---|---|---|
0d48b7f0 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 | #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> | |
0d48b7f0 | 25 | #include <linux/spinlock.h> |
0d48b7f0 EP |
26 | |
27 | #include <asm/atomic.h> | |
28 | ||
29 | #include <linux/fsnotify_backend.h> | |
30 | #include "fsnotify.h" | |
31 | ||
32 | void 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); | |
a4c6e996 | 41 | hlist_del_init_rcu(&mark->m.m_list); |
0d48b7f0 EP |
42 | 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 | ||
4d92604c EP |
52 | void 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 | ||
0d48b7f0 EP |
57 | /* |
58 | * Recalculate the mask of events relevant to a given vfsmount locked. | |
59 | */ | |
60 | static 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 | */ | |
77 | void 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 | ||
84 | void 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 | ||
a4c6e996 | 93 | hlist_del_init_rcu(&mark->m.m_list); |
0d48b7f0 EP |
94 | mark->m.mnt = NULL; |
95 | ||
96 | fsnotify_recalc_vfsmount_mask_locked(mnt); | |
97 | ||
98 | spin_unlock(&mnt->mnt_root->d_lock); | |
99 | } | |
100 | ||
101 | static 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 | */ | |
122 | struct 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 | */ | |
139 | int fsnotify_add_vfsmount_mark(struct fsnotify_mark *mark, | |
140 | struct fsnotify_group *group, struct vfsmount *mnt, | |
141 | int allow_dups) | |
142 | { | |
0c6532e4 EP |
143 | struct fsnotify_mark *lmark; |
144 | struct hlist_node *node, *last = NULL; | |
0d48b7f0 EP |
145 | int ret = 0; |
146 | ||
700307a2 | 147 | mark->flags |= FSNOTIFY_MARK_FLAG_VFSMOUNT; |
0d48b7f0 | 148 | |
0d48b7f0 EP |
149 | assert_spin_locked(&mark->lock); |
150 | assert_spin_locked(&group->mark_lock); | |
151 | ||
152 | spin_lock(&mnt->mnt_root->d_lock); | |
153 | ||
0c6532e4 | 154 | mark->m.mnt = mnt; |
0d48b7f0 | 155 | |
0c6532e4 EP |
156 | /* is mark the first mark? */ |
157 | if (hlist_empty(&mnt->mnt_fsnotify_marks)) { | |
a4c6e996 | 158 | hlist_add_head_rcu(&mark->m.m_list, &mnt->mnt_fsnotify_marks); |
0c6532e4 EP |
159 | goto out; |
160 | } | |
161 | ||
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) { | |
164 | last = node; | |
165 | ||
166 | if ((lmark->group == group) && !allow_dups) { | |
167 | ret = -EEXIST; | |
168 | goto out; | |
169 | } | |
170 | ||
6ad2d4e3 EP |
171 | if (mark->group->priority < lmark->group->priority) |
172 | continue; | |
173 | ||
174 | if ((mark->group->priority == lmark->group->priority) && | |
175 | (mark->group < lmark->group)) | |
0c6532e4 | 176 | continue; |
0d48b7f0 | 177 | |
a4c6e996 | 178 | hlist_add_before_rcu(&mark->m.m_list, &lmark->m.m_list); |
0c6532e4 | 179 | goto out; |
0d48b7f0 EP |
180 | } |
181 | ||
0c6532e4 EP |
182 | BUG_ON(last == NULL); |
183 | /* mark should be the last entry. last is the current last entry */ | |
a4c6e996 | 184 | hlist_add_after_rcu(last, &mark->m.m_list); |
0c6532e4 EP |
185 | out: |
186 | fsnotify_recalc_vfsmount_mask_locked(mnt); | |
0d48b7f0 EP |
187 | spin_unlock(&mnt->mnt_root->d_lock); |
188 | ||
189 | return ret; | |
190 | } |