]>
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> | |
25 | #include <linux/slab.h> | |
26 | #include <linux/spinlock.h> | |
27 | #include <linux/writeback.h> /* for inode_lock */ | |
28 | ||
29 | #include <asm/atomic.h> | |
30 | ||
31 | #include <linux/fsnotify_backend.h> | |
32 | #include "fsnotify.h" | |
33 | ||
34 | void fsnotify_clear_marks_by_mount(struct vfsmount *mnt) | |
35 | { | |
36 | struct fsnotify_mark *mark, *lmark; | |
37 | struct hlist_node *pos, *n; | |
38 | LIST_HEAD(free_list); | |
39 | ||
40 | spin_lock(&mnt->mnt_root->d_lock); | |
41 | hlist_for_each_entry_safe(mark, pos, n, &mnt->mnt_fsnotify_marks, m.m_list) { | |
42 | list_add(&mark->m.free_m_list, &free_list); | |
43 | hlist_del_init(&mark->m.m_list); | |
44 | fsnotify_get_mark(mark); | |
45 | } | |
46 | spin_unlock(&mnt->mnt_root->d_lock); | |
47 | ||
48 | list_for_each_entry_safe(mark, lmark, &free_list, m.free_m_list) { | |
49 | fsnotify_destroy_mark(mark); | |
50 | fsnotify_put_mark(mark); | |
51 | } | |
52 | } | |
53 | ||
4d92604c EP |
54 | void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group) |
55 | { | |
56 | fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_VFSMOUNT); | |
57 | } | |
58 | ||
0d48b7f0 EP |
59 | /* |
60 | * Recalculate the mask of events relevant to a given vfsmount locked. | |
61 | */ | |
62 | static void fsnotify_recalc_vfsmount_mask_locked(struct vfsmount *mnt) | |
63 | { | |
64 | struct fsnotify_mark *mark; | |
65 | struct hlist_node *pos; | |
66 | __u32 new_mask = 0; | |
67 | ||
68 | assert_spin_locked(&mnt->mnt_root->d_lock); | |
69 | ||
70 | hlist_for_each_entry(mark, pos, &mnt->mnt_fsnotify_marks, m.m_list) | |
71 | new_mask |= mark->mask; | |
72 | mnt->mnt_fsnotify_mask = new_mask; | |
73 | } | |
74 | ||
75 | /* | |
76 | * Recalculate the mnt->mnt_fsnotify_mask, or the mask of all FS_* event types | |
77 | * any notifier is interested in hearing for this mount point | |
78 | */ | |
79 | void fsnotify_recalc_vfsmount_mask(struct vfsmount *mnt) | |
80 | { | |
81 | spin_lock(&mnt->mnt_root->d_lock); | |
82 | fsnotify_recalc_vfsmount_mask_locked(mnt); | |
83 | spin_unlock(&mnt->mnt_root->d_lock); | |
84 | } | |
85 | ||
86 | void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark *mark) | |
87 | { | |
88 | struct vfsmount *mnt = mark->m.mnt; | |
89 | ||
90 | assert_spin_locked(&mark->lock); | |
91 | assert_spin_locked(&mark->group->mark_lock); | |
92 | ||
93 | spin_lock(&mnt->mnt_root->d_lock); | |
94 | ||
95 | hlist_del_init(&mark->m.m_list); | |
96 | mark->m.mnt = NULL; | |
97 | ||
98 | fsnotify_recalc_vfsmount_mask_locked(mnt); | |
99 | ||
100 | spin_unlock(&mnt->mnt_root->d_lock); | |
101 | } | |
102 | ||
103 | static struct fsnotify_mark *fsnotify_find_vfsmount_mark_locked(struct fsnotify_group *group, | |
104 | struct vfsmount *mnt) | |
105 | { | |
106 | struct fsnotify_mark *mark; | |
107 | struct hlist_node *pos; | |
108 | ||
109 | assert_spin_locked(&mnt->mnt_root->d_lock); | |
110 | ||
111 | hlist_for_each_entry(mark, pos, &mnt->mnt_fsnotify_marks, m.m_list) { | |
112 | if (mark->group == group) { | |
113 | fsnotify_get_mark(mark); | |
114 | return mark; | |
115 | } | |
116 | } | |
117 | return NULL; | |
118 | } | |
119 | ||
120 | /* | |
121 | * given a group and vfsmount, find the mark associated with that combination. | |
122 | * if found take a reference to that mark and return it, else return NULL | |
123 | */ | |
124 | struct fsnotify_mark *fsnotify_find_vfsmount_mark(struct fsnotify_group *group, | |
125 | struct vfsmount *mnt) | |
126 | { | |
127 | struct fsnotify_mark *mark; | |
128 | ||
129 | spin_lock(&mnt->mnt_root->d_lock); | |
130 | mark = fsnotify_find_vfsmount_mark_locked(group, mnt); | |
131 | spin_unlock(&mnt->mnt_root->d_lock); | |
132 | ||
133 | return mark; | |
134 | } | |
135 | ||
136 | /* | |
137 | * Attach an initialized mark to a given group and vfsmount. | |
138 | * These marks may be used for the fsnotify backend to determine which | |
139 | * event types should be delivered to which groups. | |
140 | */ | |
141 | int fsnotify_add_vfsmount_mark(struct fsnotify_mark *mark, | |
142 | struct fsnotify_group *group, struct vfsmount *mnt, | |
143 | int allow_dups) | |
144 | { | |
145 | struct fsnotify_mark *lmark = NULL; | |
146 | int ret = 0; | |
147 | ||
148 | mark->flags = FSNOTIFY_MARK_FLAG_VFSMOUNT; | |
149 | ||
150 | /* | |
151 | * LOCKING ORDER!!!! | |
152 | * mark->lock | |
153 | * group->mark_lock | |
154 | * mnt->mnt_root->d_lock | |
155 | */ | |
156 | assert_spin_locked(&mark->lock); | |
157 | assert_spin_locked(&group->mark_lock); | |
158 | ||
159 | spin_lock(&mnt->mnt_root->d_lock); | |
160 | ||
161 | if (!allow_dups) | |
162 | lmark = fsnotify_find_vfsmount_mark_locked(group, mnt); | |
163 | if (!lmark) { | |
164 | mark->m.mnt = mnt; | |
165 | ||
166 | hlist_add_head(&mark->m.m_list, &mnt->mnt_fsnotify_marks); | |
167 | ||
168 | fsnotify_recalc_vfsmount_mask_locked(mnt); | |
169 | } else { | |
170 | ret = -EEXIST; | |
171 | } | |
172 | ||
173 | spin_unlock(&mnt->mnt_root->d_lock); | |
174 | ||
175 | return ret; | |
176 | } |