| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 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> | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 25 | #include <linux/spinlock.h> | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 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); | 
| Eric Paris | a4c6e99 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 41 | hlist_del_init_rcu(&mark->m.m_list); | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 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 |  | 
| Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 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 |  | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 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 |  | 
| Eric Paris | a4c6e99 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 93 | hlist_del_init_rcu(&mark->m.m_list); | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 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 | { | 
| Eric Paris | 0c6532e | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 143 | struct fsnotify_mark *lmark; | 
|  | 144 | struct hlist_node *node, *last = NULL; | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 145 | int ret = 0; | 
|  | 146 |  | 
| Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 147 | mark->flags |= FSNOTIFY_MARK_FLAG_VFSMOUNT; | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 148 |  | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 149 | assert_spin_locked(&mark->lock); | 
|  | 150 | assert_spin_locked(&group->mark_lock); | 
|  | 151 |  | 
|  | 152 | spin_lock(&mnt->mnt_root->d_lock); | 
|  | 153 |  | 
| Eric Paris | 0c6532e | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 154 | mark->m.mnt = mnt; | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 155 |  | 
| Eric Paris | 0c6532e | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 156 | /* is mark the first mark? */ | 
|  | 157 | if (hlist_empty(&mnt->mnt_fsnotify_marks)) { | 
| Eric Paris | a4c6e99 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 158 | hlist_add_head_rcu(&mark->m.m_list, &mnt->mnt_fsnotify_marks); | 
| Eric Paris | 0c6532e | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 159 | goto out; | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 160 | } | 
|  | 161 |  | 
| Eric Paris | 0c6532e | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 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 |  | 
| Eric Paris | 6ad2d4e | 2010-10-28 17:21:56 -0400 | [diff] [blame] | 171 | if (mark->group->priority < lmark->group->priority) | 
|  | 172 | continue; | 
|  | 173 |  | 
|  | 174 | if ((mark->group->priority == lmark->group->priority) && | 
|  | 175 | (mark->group < lmark->group)) | 
| Eric Paris | 0c6532e | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 176 | continue; | 
|  | 177 |  | 
| Eric Paris | a4c6e99 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 178 | hlist_add_before_rcu(&mark->m.m_list, &lmark->m.m_list); | 
| Eric Paris | 0c6532e | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 179 | goto out; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | BUG_ON(last == NULL); | 
|  | 183 | /* mark should be the last entry.  last is the current last entry */ | 
| Eric Paris | a4c6e99 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 184 | hlist_add_after_rcu(last, &mark->m.m_list); | 
| Eric Paris | 0c6532e | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 185 | out: | 
|  | 186 | fsnotify_recalc_vfsmount_mask_locked(mnt); | 
| Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 187 | spin_unlock(&mnt->mnt_root->d_lock); | 
|  | 188 |  | 
|  | 189 | return ret; | 
|  | 190 | } |