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 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 27 | #include <linux/atomic.h> |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 28 | |
| 29 | #include <linux/fsnotify_backend.h> |
| 30 | #include "fsnotify.h" |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 31 | |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 32 | void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group) |
| 33 | { |
| 34 | fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_VFSMOUNT); |
| 35 | } |
| 36 | |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 37 | /* |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 38 | * Recalculate the mnt->mnt_fsnotify_mask, or the mask of all FS_* event types |
| 39 | * any notifier is interested in hearing for this mount point |
| 40 | */ |
| 41 | void fsnotify_recalc_vfsmount_mask(struct vfsmount *mnt) |
| 42 | { |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 43 | struct mount *m = real_mount(mnt); |
| 44 | |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 45 | spin_lock(&mnt->mnt_root->d_lock); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 46 | m->mnt_fsnotify_mask = fsnotify_recalc_mask(&m->mnt_fsnotify_marks); |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 47 | spin_unlock(&mnt->mnt_root->d_lock); |
| 48 | } |
| 49 | |
| 50 | void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark *mark) |
| 51 | { |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 52 | struct vfsmount *mnt = mark->mnt; |
| 53 | struct mount *m = real_mount(mnt); |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 54 | |
Lino Sanfilippo | 986ab09 | 2011-06-14 17:29:50 +0200 | [diff] [blame] | 55 | BUG_ON(!mutex_is_locked(&mark->group->mark_mutex)); |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 56 | assert_spin_locked(&mark->lock); |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 57 | |
| 58 | spin_lock(&mnt->mnt_root->d_lock); |
| 59 | |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 60 | hlist_del_init_rcu(&mark->obj_list); |
| 61 | mark->mnt = NULL; |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 62 | |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 63 | m->mnt_fsnotify_mask = fsnotify_recalc_mask(&m->mnt_fsnotify_marks); |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 64 | spin_unlock(&mnt->mnt_root->d_lock); |
| 65 | } |
| 66 | |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 67 | /* |
| 68 | * given a group and vfsmount, find the mark associated with that combination. |
| 69 | * if found take a reference to that mark and return it, else return NULL |
| 70 | */ |
| 71 | struct fsnotify_mark *fsnotify_find_vfsmount_mark(struct fsnotify_group *group, |
| 72 | struct vfsmount *mnt) |
| 73 | { |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 74 | struct mount *m = real_mount(mnt); |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 75 | struct fsnotify_mark *mark; |
| 76 | |
| 77 | spin_lock(&mnt->mnt_root->d_lock); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 78 | mark = fsnotify_find_mark(&m->mnt_fsnotify_marks, group); |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 79 | spin_unlock(&mnt->mnt_root->d_lock); |
| 80 | |
| 81 | return mark; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Attach an initialized mark to a given group and vfsmount. |
| 86 | * These marks may be used for the fsnotify backend to determine which |
| 87 | * event types should be delivered to which groups. |
| 88 | */ |
| 89 | int fsnotify_add_vfsmount_mark(struct fsnotify_mark *mark, |
| 90 | struct fsnotify_group *group, struct vfsmount *mnt, |
| 91 | int allow_dups) |
| 92 | { |
Al Viro | c63181e | 2011-11-25 02:35:16 -0500 | [diff] [blame] | 93 | struct mount *m = real_mount(mnt); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 94 | int ret; |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 95 | |
Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 96 | mark->flags |= FSNOTIFY_MARK_FLAG_VFSMOUNT; |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 97 | |
Lino Sanfilippo | 986ab09 | 2011-06-14 17:29:50 +0200 | [diff] [blame] | 98 | BUG_ON(!mutex_is_locked(&group->mark_mutex)); |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 99 | assert_spin_locked(&mark->lock); |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 100 | |
| 101 | spin_lock(&mnt->mnt_root->d_lock); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 102 | mark->mnt = mnt; |
| 103 | ret = fsnotify_add_mark_list(&m->mnt_fsnotify_marks, mark, allow_dups); |
| 104 | m->mnt_fsnotify_mask = fsnotify_recalc_mask(&m->mnt_fsnotify_marks); |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 105 | spin_unlock(&mnt->mnt_root->d_lock); |
| 106 | |
| 107 | return ret; |
| 108 | } |