Commit | Line | Data |
---|---|---|
3be25f49 EP |
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 | ||
3be25f49 EP |
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> | |
3be25f49 EP |
24 | #include <linux/spinlock.h> |
25 | ||
60063497 | 26 | #include <linux/atomic.h> |
3be25f49 EP |
27 | |
28 | #include <linux/fsnotify_backend.h> | |
29 | #include "fsnotify.h" | |
30 | ||
55fa6091 DC |
31 | #include "../internal.h" |
32 | ||
3be25f49 EP |
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); | |
0809ab69 | 40 | inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks); |
3be25f49 | 41 | spin_unlock(&inode->i_lock); |
c28f7e56 EP |
42 | |
43 | __fsnotify_update_child_dentry_flags(inode); | |
3be25f49 EP |
44 | } |
45 | ||
5444e298 | 46 | void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark) |
3be25f49 | 47 | { |
0809ab69 | 48 | struct inode *inode = mark->inode; |
3be25f49 | 49 | |
986ab098 | 50 | BUG_ON(!mutex_is_locked(&mark->group->mark_mutex)); |
5444e298 | 51 | assert_spin_locked(&mark->lock); |
3be25f49 | 52 | |
3be25f49 EP |
53 | spin_lock(&inode->i_lock); |
54 | ||
0809ab69 JK |
55 | hlist_del_init_rcu(&mark->obj_list); |
56 | mark->inode = NULL; | |
3be25f49 EP |
57 | |
58 | /* | |
e61ce867 | 59 | * this mark is now off the inode->i_fsnotify_marks list and we |
3be25f49 EP |
60 | * hold the inode->i_lock, so this is the perfect time to update the |
61 | * inode->i_fsnotify_mask | |
62 | */ | |
0809ab69 | 63 | inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks); |
3be25f49 | 64 | spin_unlock(&inode->i_lock); |
3be25f49 EP |
65 | } |
66 | ||
4d92604c EP |
67 | /* |
68 | * Given a group clear all of the inode marks associated with that group. | |
69 | */ | |
70 | void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group) | |
71 | { | |
72 | fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_INODE); | |
73 | } | |
74 | ||
3be25f49 | 75 | /* |
35566087 AG |
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 | |
3be25f49 | 78 | */ |
5444e298 EP |
79 | struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group, |
80 | struct inode *inode) | |
35566087 AG |
81 | { |
82 | struct fsnotify_mark *mark; | |
83 | ||
84 | spin_lock(&inode->i_lock); | |
0809ab69 | 85 | mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group); |
35566087 AG |
86 | spin_unlock(&inode->i_lock); |
87 | ||
88 | return mark; | |
89 | } | |
3be25f49 | 90 | |
3be25f49 | 91 | /* |
90b1e7a5 EP |
92 | * If we are setting a mark mask on an inode mark we should pin the inode |
93 | * in memory. | |
94 | */ | |
95 | void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *mark, | |
96 | __u32 mask) | |
3be25f49 | 97 | { |
90b1e7a5 EP |
98 | struct inode *inode; |
99 | ||
100 | assert_spin_locked(&mark->lock); | |
101 | ||
102 | if (mask && | |
0809ab69 | 103 | mark->inode && |
90b1e7a5 EP |
104 | !(mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) { |
105 | mark->flags |= FSNOTIFY_MARK_FLAG_OBJECT_PINNED; | |
0809ab69 | 106 | inode = igrab(mark->inode); |
90b1e7a5 EP |
107 | /* |
108 | * we shouldn't be able to get here if the inode wasn't | |
109 | * already safely held in memory. But bug in case it | |
110 | * ever is wrong. | |
111 | */ | |
112 | BUG_ON(!inode); | |
113 | } | |
3be25f49 EP |
114 | } |
115 | ||
116 | /* | |
0c6532e4 | 117 | * Attach an initialized mark to a given inode. |
3be25f49 | 118 | * These marks may be used for the fsnotify backend to determine which |
0c6532e4 | 119 | * event types should be delivered to which group and for which inodes. These |
6ad2d4e3 EP |
120 | * marks are ordered according to priority, highest number first, and then by |
121 | * the group's location in memory. | |
3be25f49 | 122 | */ |
5444e298 EP |
123 | int fsnotify_add_inode_mark(struct fsnotify_mark *mark, |
124 | struct fsnotify_group *group, struct inode *inode, | |
125 | int allow_dups) | |
3be25f49 | 126 | { |
0809ab69 | 127 | int ret; |
3be25f49 | 128 | |
700307a2 | 129 | mark->flags |= FSNOTIFY_MARK_FLAG_INODE; |
098cf2fc | 130 | |
986ab098 | 131 | BUG_ON(!mutex_is_locked(&group->mark_mutex)); |
5444e298 | 132 | assert_spin_locked(&mark->lock); |
1ef5f13c | 133 | |
3be25f49 | 134 | spin_lock(&inode->i_lock); |
0809ab69 JK |
135 | mark->inode = inode; |
136 | ret = fsnotify_add_mark_list(&inode->i_fsnotify_marks, mark, | |
137 | allow_dups); | |
138 | inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks); | |
3be25f49 | 139 | spin_unlock(&inode->i_lock); |
3be25f49 EP |
140 | |
141 | return ret; | |
142 | } | |
164bc619 EP |
143 | |
144 | /** | |
145 | * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes. | |
74278da9 | 146 | * @sb: superblock being unmounted. |
164bc619 | 147 | * |
55fa6091 | 148 | * Called during unmount with no locks held, so needs to be safe against |
74278da9 | 149 | * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block. |
164bc619 | 150 | */ |
74278da9 | 151 | void fsnotify_unmount_inodes(struct super_block *sb) |
164bc619 EP |
152 | { |
153 | struct inode *inode, *next_i, *need_iput = NULL; | |
154 | ||
74278da9 DC |
155 | spin_lock(&sb->s_inode_list_lock); |
156 | list_for_each_entry_safe(inode, next_i, &sb->s_inodes, i_sb_list) { | |
164bc619 EP |
157 | struct inode *need_iput_tmp; |
158 | ||
159 | /* | |
a4ffdde6 | 160 | * We cannot __iget() an inode in state I_FREEING, |
164bc619 EP |
161 | * I_WILL_FREE, or I_NEW which is fine because by that point |
162 | * the inode cannot have any associated watches. | |
163 | */ | |
250df6ed DC |
164 | spin_lock(&inode->i_lock); |
165 | if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) { | |
166 | spin_unlock(&inode->i_lock); | |
164bc619 | 167 | continue; |
250df6ed | 168 | } |
164bc619 EP |
169 | |
170 | /* | |
171 | * If i_count is zero, the inode cannot have any watches and | |
172 | * doing an __iget/iput with MS_ACTIVE clear would actually | |
173 | * evict all inodes with zero i_count from icache which is | |
174 | * unnecessarily violent and may in fact be illegal to do. | |
175 | */ | |
250df6ed DC |
176 | if (!atomic_read(&inode->i_count)) { |
177 | spin_unlock(&inode->i_lock); | |
164bc619 | 178 | continue; |
250df6ed | 179 | } |
164bc619 EP |
180 | |
181 | need_iput_tmp = need_iput; | |
182 | need_iput = NULL; | |
183 | ||
184 | /* In case fsnotify_inode_delete() drops a reference. */ | |
185 | if (inode != need_iput_tmp) | |
186 | __iget(inode); | |
187 | else | |
188 | need_iput_tmp = NULL; | |
250df6ed | 189 | spin_unlock(&inode->i_lock); |
164bc619 EP |
190 | |
191 | /* In case the dropping of a reference would nuke next_i. */ | |
74278da9 | 192 | while (&next_i->i_sb_list != &sb->s_inodes) { |
250df6ed | 193 | spin_lock(&next_i->i_lock); |
6424babf JH |
194 | if (!(next_i->i_state & (I_FREEING | I_WILL_FREE)) && |
195 | atomic_read(&next_i->i_count)) { | |
250df6ed DC |
196 | __iget(next_i); |
197 | need_iput = next_i; | |
6424babf JH |
198 | spin_unlock(&next_i->i_lock); |
199 | break; | |
250df6ed DC |
200 | } |
201 | spin_unlock(&next_i->i_lock); | |
1deaf9d1 | 202 | next_i = list_next_entry(next_i, i_sb_list); |
164bc619 EP |
203 | } |
204 | ||
205 | /* | |
74278da9 | 206 | * We can safely drop s_inode_list_lock here because either |
6424babf JH |
207 | * we actually hold references on both inode and next_i or |
208 | * end of list. Also no new inodes will be added since the | |
209 | * umount has begun. | |
164bc619 | 210 | */ |
74278da9 | 211 | spin_unlock(&sb->s_inode_list_lock); |
164bc619 EP |
212 | |
213 | if (need_iput_tmp) | |
214 | iput(need_iput_tmp); | |
215 | ||
216 | /* for each watch, send FS_UNMOUNT and then remove it */ | |
217 | fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0); | |
218 | ||
219 | fsnotify_inode_delete(inode); | |
220 | ||
221 | iput(inode); | |
222 | ||
74278da9 | 223 | spin_lock(&sb->s_inode_list_lock); |
164bc619 | 224 | } |
74278da9 | 225 | spin_unlock(&sb->s_inode_list_lock); |
164bc619 | 226 | } |