blob: 326b148e623cdf26d30935a985ab398033cb0950 [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{
Jan Kara0809ab62014-12-12 16:58:36 -080035 struct fsnotify_mark *mark;
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);
Jan Kara0809ab62014-12-12 16:58:36 -080041 hlist_for_each_entry_safe(mark, n, &m->mnt_fsnotify_marks, obj_list) {
42 list_add(&mark->free_list, &free_list);
43 hlist_del_init_rcu(&mark->obj_list);
Eric Paris0d48b7f2009-12-17 21:24:27 -050044 fsnotify_get_mark(mark);
45 }
46 spin_unlock(&mnt->mnt_root->d_lock);
47
Jan Kara0809ab62014-12-12 16:58:36 -080048 fsnotify_destroy_marks(&free_list);
Eric Paris0d48b7f2009-12-17 21:24:27 -050049}
50
Eric Paris4d926042009-12-17 21:24:34 -050051void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
52{
53 fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_VFSMOUNT);
54}
55
Eric Paris0d48b7f2009-12-17 21:24:27 -050056/*
Eric Paris0d48b7f2009-12-17 21:24:27 -050057 * Recalculate the mnt->mnt_fsnotify_mask, or the mask of all FS_* event types
58 * any notifier is interested in hearing for this mount point
59 */
60void fsnotify_recalc_vfsmount_mask(struct vfsmount *mnt)
61{
Jan Kara0809ab62014-12-12 16:58:36 -080062 struct mount *m = real_mount(mnt);
63
Eric Paris0d48b7f2009-12-17 21:24:27 -050064 spin_lock(&mnt->mnt_root->d_lock);
Jan Kara0809ab62014-12-12 16:58:36 -080065 m->mnt_fsnotify_mask = fsnotify_recalc_mask(&m->mnt_fsnotify_marks);
Eric Paris0d48b7f2009-12-17 21:24:27 -050066 spin_unlock(&mnt->mnt_root->d_lock);
67}
68
69void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark *mark)
70{
Jan Kara0809ab62014-12-12 16:58:36 -080071 struct vfsmount *mnt = mark->mnt;
72 struct mount *m = real_mount(mnt);
Eric Paris0d48b7f2009-12-17 21:24:27 -050073
Lino Sanfilippo986ab092011-06-14 17:29:50 +020074 BUG_ON(!mutex_is_locked(&mark->group->mark_mutex));
Eric Paris0d48b7f2009-12-17 21:24:27 -050075 assert_spin_locked(&mark->lock);
Eric Paris0d48b7f2009-12-17 21:24:27 -050076
77 spin_lock(&mnt->mnt_root->d_lock);
78
Jan Kara0809ab62014-12-12 16:58:36 -080079 hlist_del_init_rcu(&mark->obj_list);
80 mark->mnt = NULL;
Eric Paris0d48b7f2009-12-17 21:24:27 -050081
Jan Kara0809ab62014-12-12 16:58:36 -080082 m->mnt_fsnotify_mask = fsnotify_recalc_mask(&m->mnt_fsnotify_marks);
Eric Paris0d48b7f2009-12-17 21:24:27 -050083 spin_unlock(&mnt->mnt_root->d_lock);
84}
85
Eric Paris0d48b7f2009-12-17 21:24:27 -050086/*
87 * given a group and vfsmount, find the mark associated with that combination.
88 * if found take a reference to that mark and return it, else return NULL
89 */
90struct fsnotify_mark *fsnotify_find_vfsmount_mark(struct fsnotify_group *group,
91 struct vfsmount *mnt)
92{
Jan Kara0809ab62014-12-12 16:58:36 -080093 struct mount *m = real_mount(mnt);
Eric Paris0d48b7f2009-12-17 21:24:27 -050094 struct fsnotify_mark *mark;
95
96 spin_lock(&mnt->mnt_root->d_lock);
Jan Kara0809ab62014-12-12 16:58:36 -080097 mark = fsnotify_find_mark(&m->mnt_fsnotify_marks, group);
Eric Paris0d48b7f2009-12-17 21:24:27 -050098 spin_unlock(&mnt->mnt_root->d_lock);
99
100 return mark;
101}
102
103/*
104 * Attach an initialized mark to a given group and vfsmount.
105 * These marks may be used for the fsnotify backend to determine which
106 * event types should be delivered to which groups.
107 */
108int fsnotify_add_vfsmount_mark(struct fsnotify_mark *mark,
109 struct fsnotify_group *group, struct vfsmount *mnt,
110 int allow_dups)
111{
Al Viroc63181e2011-11-25 02:35:16 -0500112 struct mount *m = real_mount(mnt);
Jan Kara0809ab62014-12-12 16:58:36 -0800113 int ret;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500114
Eric Paris700307a2010-07-28 10:18:38 -0400115 mark->flags |= FSNOTIFY_MARK_FLAG_VFSMOUNT;
Eric Paris0d48b7f2009-12-17 21:24:27 -0500116
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200117 BUG_ON(!mutex_is_locked(&group->mark_mutex));
Eric Paris0d48b7f2009-12-17 21:24:27 -0500118 assert_spin_locked(&mark->lock);
Eric Paris0d48b7f2009-12-17 21:24:27 -0500119
120 spin_lock(&mnt->mnt_root->d_lock);
Jan Kara0809ab62014-12-12 16:58:36 -0800121 mark->mnt = mnt;
122 ret = fsnotify_add_mark_list(&m->mnt_fsnotify_marks, mark, allow_dups);
123 m->mnt_fsnotify_mask = fsnotify_recalc_mask(&m->mnt_fsnotify_marks);
Eric Paris0d48b7f2009-12-17 21:24:27 -0500124 spin_unlock(&mnt->mnt_root->d_lock);
125
126 return ret;
127}