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