fsnotify: Remove indirection from mark list addition
[sfrench/cifs-2.6.git] / fs / notify / inode_mark.c
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/mutex.h>
24 #include <linux/spinlock.h>
25
26 #include <linux/atomic.h>
27
28 #include <linux/fsnotify_backend.h>
29 #include "fsnotify.h"
30
31 #include "../internal.h"
32
33 /*
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.
36  */
37 void fsnotify_recalc_inode_mask(struct inode *inode)
38 {
39         spin_lock(&inode->i_lock);
40         inode->i_fsnotify_mask = fsnotify_recalc_mask(inode->i_fsnotify_marks);
41         spin_unlock(&inode->i_lock);
42
43         __fsnotify_update_child_dentry_flags(inode);
44 }
45
46 struct inode *fsnotify_destroy_inode_mark(struct fsnotify_mark *mark)
47 {
48         struct inode *inode = mark->connector->inode;
49         bool empty;
50
51         BUG_ON(!mutex_is_locked(&mark->group->mark_mutex));
52         assert_spin_locked(&mark->lock);
53
54         spin_lock(&inode->i_lock);
55
56         hlist_del_init_rcu(&mark->obj_list);
57         empty = hlist_empty(&mark->connector->list);
58         mark->connector = NULL;
59
60         /*
61          * this mark is now off the inode->i_fsnotify_marks list and we
62          * hold the inode->i_lock, so this is the perfect time to update the
63          * inode->i_fsnotify_mask
64          */
65         inode->i_fsnotify_mask = fsnotify_recalc_mask(inode->i_fsnotify_marks);
66         spin_unlock(&inode->i_lock);
67
68         return empty ? inode : NULL;
69 }
70
71 /*
72  * Given a group clear all of the inode marks associated with that group.
73  */
74 void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
75 {
76         fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_OBJ_TYPE_INODE);
77 }
78
79 /*
80  * given a group and inode, find the mark associated with that combination.
81  * if found take a reference to that mark and return it, else return NULL
82  */
83 struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group,
84                                                struct inode *inode)
85 {
86         struct fsnotify_mark *mark;
87
88         spin_lock(&inode->i_lock);
89         mark = fsnotify_find_mark(inode->i_fsnotify_marks, group);
90         spin_unlock(&inode->i_lock);
91
92         return mark;
93 }
94
95 /**
96  * fsnotify_unmount_inodes - an sb is unmounting.  handle any watched inodes.
97  * @sb: superblock being unmounted.
98  *
99  * Called during unmount with no locks held, so needs to be safe against
100  * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
101  */
102 void fsnotify_unmount_inodes(struct super_block *sb)
103 {
104         struct inode *inode, *iput_inode = NULL;
105
106         spin_lock(&sb->s_inode_list_lock);
107         list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
108                 /*
109                  * We cannot __iget() an inode in state I_FREEING,
110                  * I_WILL_FREE, or I_NEW which is fine because by that point
111                  * the inode cannot have any associated watches.
112                  */
113                 spin_lock(&inode->i_lock);
114                 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
115                         spin_unlock(&inode->i_lock);
116                         continue;
117                 }
118
119                 /*
120                  * If i_count is zero, the inode cannot have any watches and
121                  * doing an __iget/iput with MS_ACTIVE clear would actually
122                  * evict all inodes with zero i_count from icache which is
123                  * unnecessarily violent and may in fact be illegal to do.
124                  */
125                 if (!atomic_read(&inode->i_count)) {
126                         spin_unlock(&inode->i_lock);
127                         continue;
128                 }
129
130                 __iget(inode);
131                 spin_unlock(&inode->i_lock);
132                 spin_unlock(&sb->s_inode_list_lock);
133
134                 if (iput_inode)
135                         iput(iput_inode);
136
137                 /* for each watch, send FS_UNMOUNT and then remove it */
138                 fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
139
140                 fsnotify_inode_delete(inode);
141
142                 iput_inode = inode;
143
144                 spin_lock(&sb->s_inode_list_lock);
145         }
146         spin_unlock(&sb->s_inode_list_lock);
147
148         if (iput_inode)
149                 iput(iput_inode);
150 }