2 * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
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)
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.
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.
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/spinlock.h>
26 #include <linux/atomic.h>
28 #include <linux/fsnotify_backend.h>
31 #include "../internal.h"
34 * Recalculate the inode->i_fsnotify_mask, or the mask of all FS_* event types
35 * any notifier is interested in hearing for this inode.
37 void fsnotify_recalc_inode_mask(struct inode *inode)
39 spin_lock(&inode->i_lock);
40 inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks);
41 spin_unlock(&inode->i_lock);
43 __fsnotify_update_child_dentry_flags(inode);
46 void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark)
48 struct inode *inode = mark->inode;
50 BUG_ON(!mutex_is_locked(&mark->group->mark_mutex));
51 assert_spin_locked(&mark->lock);
53 spin_lock(&inode->i_lock);
55 hlist_del_init_rcu(&mark->obj_list);
59 * this mark is now off the inode->i_fsnotify_marks list and we
60 * hold the inode->i_lock, so this is the perfect time to update the
61 * inode->i_fsnotify_mask
63 inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks);
64 spin_unlock(&inode->i_lock);
68 * Given a group clear all of the inode marks associated with that group.
70 void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
72 fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_INODE);
76 * given a group and inode, find the mark associated with that combination.
77 * if found take a reference to that mark and return it, else return NULL
79 struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group,
82 struct fsnotify_mark *mark;
84 spin_lock(&inode->i_lock);
85 mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
86 spin_unlock(&inode->i_lock);
90 EXPORT_SYMBOL_GPL(fsnotify_find_inode_mark);
93 * If we are setting a mark mask on an inode mark we should pin the inode
96 void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *mark,
101 assert_spin_locked(&mark->lock);
105 !(mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) {
106 mark->flags |= FSNOTIFY_MARK_FLAG_OBJECT_PINNED;
107 inode = igrab(mark->inode);
109 * we shouldn't be able to get here if the inode wasn't
110 * already safely held in memory. But bug in case it
118 * Attach an initialized mark to a given inode.
119 * These marks may be used for the fsnotify backend to determine which
120 * event types should be delivered to which group and for which inodes. These
121 * marks are ordered according to priority, highest number first, and then by
122 * the group's location in memory.
124 int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
125 struct fsnotify_group *group, struct inode *inode,
130 mark->flags |= FSNOTIFY_MARK_FLAG_INODE;
132 BUG_ON(!mutex_is_locked(&group->mark_mutex));
133 assert_spin_locked(&mark->lock);
135 spin_lock(&inode->i_lock);
137 ret = fsnotify_add_mark_list(&inode->i_fsnotify_marks, mark,
139 inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks);
140 spin_unlock(&inode->i_lock);
146 * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
147 * @sb: superblock being unmounted.
149 * Called during unmount with no locks held, so needs to be safe against
150 * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
152 void fsnotify_unmount_inodes(struct super_block *sb)
154 struct inode *inode, *next_i, *need_iput = NULL;
156 spin_lock(&sb->s_inode_list_lock);
157 list_for_each_entry_safe(inode, next_i, &sb->s_inodes, i_sb_list) {
158 struct inode *need_iput_tmp;
161 * We cannot __iget() an inode in state I_FREEING,
162 * I_WILL_FREE, or I_NEW which is fine because by that point
163 * the inode cannot have any associated watches.
165 spin_lock(&inode->i_lock);
166 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
167 spin_unlock(&inode->i_lock);
172 * If i_count is zero, the inode cannot have any watches and
173 * doing an __iget/iput with MS_ACTIVE clear would actually
174 * evict all inodes with zero i_count from icache which is
175 * unnecessarily violent and may in fact be illegal to do.
177 if (!atomic_read(&inode->i_count)) {
178 spin_unlock(&inode->i_lock);
182 need_iput_tmp = need_iput;
185 /* In case fsnotify_inode_delete() drops a reference. */
186 if (inode != need_iput_tmp)
189 need_iput_tmp = NULL;
190 spin_unlock(&inode->i_lock);
192 /* In case the dropping of a reference would nuke next_i. */
193 while (&next_i->i_sb_list != &sb->s_inodes) {
194 spin_lock(&next_i->i_lock);
195 if (!(next_i->i_state & (I_FREEING | I_WILL_FREE)) &&
196 atomic_read(&next_i->i_count)) {
199 spin_unlock(&next_i->i_lock);
202 spin_unlock(&next_i->i_lock);
203 next_i = list_entry(next_i->i_sb_list.next,
204 struct inode, i_sb_list);
208 * We can safely drop s_inode_list_lock here because either
209 * we actually hold references on both inode and next_i or
210 * end of list. Also no new inodes will be added since the
213 spin_unlock(&sb->s_inode_list_lock);
218 /* for each watch, send FS_UNMOUNT and then remove it */
219 fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
221 fsnotify_inode_delete(inode);
225 spin_lock(&sb->s_inode_list_lock);
227 spin_unlock(&sb->s_inode_list_lock);