blob: ac851e8376b1931d88adcf4ff5eaa8bd2445a635 [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"
Al Viroc63181e2011-11-25 02:35:16 -050031#include "../mount.h"
Eric Paris0d48b7f2009-12-17 21:24:27 -050032
33void fsnotify_clear_marks_by_mount(struct vfsmount *mnt)
34{
35 struct fsnotify_mark *mark, *lmark;
Sasha Levinb67bfe02013-02-27 17:06:00 -080036 struct hlist_node *n;
Al Viroc63181e2011-11-25 02:35:16 -050037 struct mount *m = real_mount(mnt);
Eric Paris0d48b7f2009-12-17 21:24:27 -050038 LIST_HEAD(free_list);
39
40 spin_lock(&mnt->mnt_root->d_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -080041 hlist_for_each_entry_safe(mark, n, &m->mnt_fsnotify_marks, m.m_list) {
Eric Paris0d48b7f2009-12-17 21:24:27 -050042 list_add(&mark->m.free_m_list, &free_list);
Eric Parisa4c6e992010-07-28 10:18:38 -040043 hlist_del_init_rcu(&mark->m.m_list);
Eric Paris0d48b7f2009-12-17 21:24:27 -050044 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) {
Lino Sanfilippoe2a29942011-06-14 17:29:51 +020049 struct fsnotify_group *group;
50
51 spin_lock(&mark->lock);
52 fsnotify_get_group(mark->group);
53 group = mark->group;
54 spin_unlock(&mark->lock);
55
56 fsnotify_destroy_mark(mark, group);
Eric Paris0d48b7f2009-12-17 21:24:27 -050057 fsnotify_put_mark(mark);
Lino Sanfilippoe2a29942011-06-14 17:29:51 +020058 fsnotify_put_group(group);
Eric Paris0d48b7f2009-12-17 21:24:27 -050059 }
60}
61
Eric Paris4d926042009-12-17 21:24:34 -050062void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
63{
64 fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_VFSMOUNT);
65}
66
Eric Paris0d48b7f2009-12-17 21:24:27 -050067/*
68 * Recalculate the mask of events relevant to a given vfsmount locked.
69 */
70static void fsnotify_recalc_vfsmount_mask_locked(struct vfsmount *mnt)
71{
Al Viroc63181e2011-11-25 02:35:16 -050072 struct mount *m = real_mount(mnt);
Eric Paris0d48b7f2009-12-17 21:24:27 -050073 struct fsnotify_mark *mark;
Eric Paris0d48b7f2009-12-17 21:24:27 -050074 __u32 new_mask = 0;
75
76 assert_spin_locked(&mnt->mnt_root->d_lock);
77
Sasha Levinb67bfe02013-02-27 17:06:00 -080078 hlist_for_each_entry(mark, &m->mnt_fsnotify_marks, m.m_list)
Eric Paris0d48b7f2009-12-17 21:24:27 -050079 new_mask |= mark->mask;
Al Viroc63181e2011-11-25 02:35:16 -050080 m->mnt_fsnotify_mask = new_mask;
Eric Paris0d48b7f2009-12-17 21:24:27 -050081}
82
83/*
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
86 */
87void fsnotify_recalc_vfsmount_mask(struct vfsmount *mnt)
88{
89 spin_lock(&mnt->mnt_root->d_lock);
90 fsnotify_recalc_vfsmount_mask_locked(mnt);
91 spin_unlock(&mnt->mnt_root->d_lock);
92}
93
94void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark *mark)
95{
96 struct vfsmount *mnt = mark->m.mnt;
97
Lino Sanfilippo986ab092011-06-14 17:29:50 +020098 BUG_ON(!mutex_is_locked(&mark->group->mark_mutex));
Eric Paris0d48b7f2009-12-17 21:24:27 -050099 assert_spin_locked(&mark->lock);
Eric Paris0d48b7f2009-12-17 21:24:27 -0500100
101 spin_lock(&mnt->mnt_root->d_lock);
102
Eric Parisa4c6e992010-07-28 10:18:38 -0400103 hlist_del_init_rcu(&mark->m.m_list);
Eric Paris0d48b7f2009-12-17 21:24:27 -0500104 mark->m.mnt = NULL;
105
106 fsnotify_recalc_vfsmount_mask_locked(mnt);
107
108 spin_unlock(&mnt->mnt_root->d_lock);
109}
110
111static struct fsnotify_mark *fsnotify_find_vfsmount_mark_locked(struct fsnotify_group *group,
112 struct vfsmount *mnt)
113{
Al Viroc63181e2011-11-25 02:35:16 -0500114 struct mount *m = real_mount(mnt);
Eric Paris0d48b7f2009-12-17 21:24:27 -0500115 struct fsnotify_mark *mark;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500116
117 assert_spin_locked(&mnt->mnt_root->d_lock);
118
Sasha Levinb67bfe02013-02-27 17:06:00 -0800119 hlist_for_each_entry(mark, &m->mnt_fsnotify_marks, m.m_list) {
Eric Paris0d48b7f2009-12-17 21:24:27 -0500120 if (mark->group == group) {
121 fsnotify_get_mark(mark);
122 return mark;
123 }
124 }
125 return NULL;
126}
127
128/*
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
131 */
132struct fsnotify_mark *fsnotify_find_vfsmount_mark(struct fsnotify_group *group,
133 struct vfsmount *mnt)
134{
135 struct fsnotify_mark *mark;
136
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);
140
141 return mark;
142}
143
144/*
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.
148 */
149int fsnotify_add_vfsmount_mark(struct fsnotify_mark *mark,
150 struct fsnotify_group *group, struct vfsmount *mnt,
151 int allow_dups)
152{
Al Viroc63181e2011-11-25 02:35:16 -0500153 struct mount *m = real_mount(mnt);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800154 struct fsnotify_mark *lmark, *last = NULL;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500155 int ret = 0;
156
Eric Paris700307a2010-07-28 10:18:38 -0400157 mark->flags |= FSNOTIFY_MARK_FLAG_VFSMOUNT;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500158
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200159 BUG_ON(!mutex_is_locked(&group->mark_mutex));
Eric Paris0d48b7f2009-12-17 21:24:27 -0500160 assert_spin_locked(&mark->lock);
Eric Paris0d48b7f2009-12-17 21:24:27 -0500161
162 spin_lock(&mnt->mnt_root->d_lock);
163
Eric Paris0c6532e2010-07-28 10:18:38 -0400164 mark->m.mnt = mnt;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500165
Eric Paris0c6532e2010-07-28 10:18:38 -0400166 /* is mark the first mark? */
Al Viroc63181e2011-11-25 02:35:16 -0500167 if (hlist_empty(&m->mnt_fsnotify_marks)) {
168 hlist_add_head_rcu(&mark->m.m_list, &m->mnt_fsnotify_marks);
Eric Paris0c6532e2010-07-28 10:18:38 -0400169 goto out;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500170 }
171
Eric Paris0c6532e2010-07-28 10:18:38 -0400172 /* should mark be in the middle of the current list? */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800173 hlist_for_each_entry(lmark, &m->mnt_fsnotify_marks, m.m_list) {
174 last = lmark;
Eric Paris0c6532e2010-07-28 10:18:38 -0400175
176 if ((lmark->group == group) && !allow_dups) {
177 ret = -EEXIST;
178 goto out;
179 }
180
Eric Paris6ad2d4e2010-10-28 17:21:56 -0400181 if (mark->group->priority < lmark->group->priority)
182 continue;
183
184 if ((mark->group->priority == lmark->group->priority) &&
185 (mark->group < lmark->group))
Eric Paris0c6532e2010-07-28 10:18:38 -0400186 continue;
187
Eric Parisa4c6e992010-07-28 10:18:38 -0400188 hlist_add_before_rcu(&mark->m.m_list, &lmark->m.m_list);
Eric Paris0c6532e2010-07-28 10:18:38 -0400189 goto out;
190 }
191
192 BUG_ON(last == NULL);
193 /* mark should be the last entry. last is the current last entry */
Ken Helias1d023282014-08-06 16:09:16 -0700194 hlist_add_behind_rcu(&mark->m.m_list, &last->m.m_list);
Eric Paris0c6532e2010-07-28 10:18:38 -0400195out:
196 fsnotify_recalc_vfsmount_mask_locked(mnt);
Eric Paris0d48b7f2009-12-17 21:24:27 -0500197 spin_unlock(&mnt->mnt_root->d_lock);
198
199 return ret;
200}