~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/notify/fsnotify.c

Version: ~ [ linux-5.16 ] ~ [ linux-5.15.13 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.90 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.170 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.224 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.261 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.296 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.298 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/dcache.h>
 20 #include <linux/fs.h>
 21 #include <linux/gfp.h>
 22 #include <linux/init.h>
 23 #include <linux/module.h>
 24 #include <linux/mount.h>
 25 #include <linux/srcu.h>
 26 
 27 #include <linux/fsnotify_backend.h>
 28 #include "fsnotify.h"
 29 #include "../mount.h"
 30 
 31 /*
 32  * Clear all of the marks on an inode when it is being evicted from core
 33  */
 34 void __fsnotify_inode_delete(struct inode *inode)
 35 {
 36         fsnotify_clear_marks_by_inode(inode);
 37 }
 38 EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);
 39 
 40 void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
 41 {
 42         fsnotify_clear_marks_by_mount(mnt);
 43 }
 44 
 45 /*
 46  * Given an inode, first check if we care what happens to our children.  Inotify
 47  * and dnotify both tell their parents about events.  If we care about any event
 48  * on a child we run all of our children and set a dentry flag saying that the
 49  * parent cares.  Thus when an event happens on a child it can quickly tell if
 50  * if there is a need to find a parent and send the event to the parent.
 51  */
 52 void __fsnotify_update_child_dentry_flags(struct inode *inode)
 53 {
 54         struct dentry *alias;
 55         int watched;
 56 
 57         if (!S_ISDIR(inode->i_mode))
 58                 return;
 59 
 60         /* determine if the children should tell inode about their events */
 61         watched = fsnotify_inode_watches_children(inode);
 62 
 63         spin_lock(&inode->i_lock);
 64         /* run all of the dentries associated with this inode.  Since this is a
 65          * directory, there damn well better only be one item on this list */
 66         hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
 67                 struct dentry *child;
 68 
 69                 /* run all of the children of the original inode and fix their
 70                  * d_flags to indicate parental interest (their parent is the
 71                  * original inode) */
 72                 spin_lock(&alias->d_lock);
 73                 list_for_each_entry(child, &alias->d_subdirs, d_child) {
 74                         if (!child->d_inode)
 75                                 continue;
 76 
 77                         spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
 78                         if (watched)
 79                                 child->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
 80                         else
 81                                 child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
 82                         spin_unlock(&child->d_lock);
 83                 }
 84                 spin_unlock(&alias->d_lock);
 85         }
 86         spin_unlock(&inode->i_lock);
 87 }
 88 
 89 /* Notify this dentry's parent about a child's events. */
 90 int __fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
 91 {
 92         struct dentry *parent;
 93         struct inode *p_inode;
 94         int ret = 0;
 95 
 96         if (!dentry)
 97                 dentry = path->dentry;
 98 
 99         if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
100                 return 0;
101 
102         parent = dget_parent(dentry);
103         p_inode = parent->d_inode;
104 
105         if (unlikely(!fsnotify_inode_watches_children(p_inode)))
106                 __fsnotify_update_child_dentry_flags(p_inode);
107         else if (p_inode->i_fsnotify_mask & mask) {
108                 struct name_snapshot name;
109 
110                 /* we are notifying a parent so come up with the new mask which
111                  * specifies these are events which came from a child. */
112                 mask |= FS_EVENT_ON_CHILD;
113 
114                 take_dentry_name_snapshot(&name, dentry);
115                 if (path)
116                         ret = fsnotify(p_inode, mask, path, FSNOTIFY_EVENT_PATH,
117                                        name.name, 0);
118                 else
119                         ret = fsnotify(p_inode, mask, dentry->d_inode, FSNOTIFY_EVENT_INODE,
120                                        name.name, 0);
121                 release_dentry_name_snapshot(&name);
122         }
123 
124         dput(parent);
125 
126         return ret;
127 }
128 EXPORT_SYMBOL_GPL(__fsnotify_parent);
129 
130 static int send_to_group(struct inode *to_tell,
131                          struct fsnotify_mark *inode_mark,
132                          struct fsnotify_mark *vfsmount_mark,
133                          __u32 mask, void *data,
134                          int data_is, u32 cookie,
135                          const unsigned char *file_name)
136 {
137         struct fsnotify_group *group = NULL;
138         __u32 test_mask = (mask & ~FS_EVENT_ON_CHILD);
139         __u32 marks_mask = 0;
140         __u32 marks_ignored_mask = 0;
141 
142         if (unlikely(!inode_mark && !vfsmount_mark)) {
143                 BUG();
144                 return 0;
145         }
146 
147         /* clear ignored on inode modification */
148         if (mask & FS_MODIFY) {
149                 if (inode_mark &&
150                     !(inode_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
151                         inode_mark->ignored_mask = 0;
152                 if (vfsmount_mark &&
153                     !(vfsmount_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
154                         vfsmount_mark->ignored_mask = 0;
155         }
156 
157         /* does the inode mark tell us to do something? */
158         if (inode_mark) {
159                 group = inode_mark->group;
160                 marks_mask |= inode_mark->mask;
161                 marks_ignored_mask |= inode_mark->ignored_mask;
162         }
163 
164         /* does the vfsmount_mark tell us to do something? */
165         if (vfsmount_mark) {
166                 group = vfsmount_mark->group;
167                 marks_mask |= vfsmount_mark->mask;
168                 marks_ignored_mask |= vfsmount_mark->ignored_mask;
169         }
170 
171         pr_debug("%s: group=%p to_tell=%p mask=%x inode_mark=%p"
172                  " vfsmount_mark=%p marks_mask=%x marks_ignored_mask=%x"
173                  " data=%p data_is=%d cookie=%d\n",
174                  __func__, group, to_tell, mask, inode_mark, vfsmount_mark,
175                  marks_mask, marks_ignored_mask, data,
176                  data_is, cookie);
177 
178         if (!(test_mask & marks_mask & ~marks_ignored_mask))
179                 return 0;
180 
181         return group->ops->handle_event(group, to_tell, inode_mark,
182                                         vfsmount_mark, mask, data, data_is,
183                                         file_name, cookie);
184 }
185 
186 /*
187  * This is the main call to fsnotify.  The VFS calls into hook specific functions
188  * in linux/fsnotify.h.  Those functions then in turn call here.  Here will call
189  * out to all of the registered fsnotify_group.  Those groups can then use the
190  * notification event in whatever means they feel necessary.
191  */
192 int fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is,
193              const unsigned char *file_name, u32 cookie)
194 {
195         struct hlist_node *inode_node = NULL, *vfsmount_node = NULL;
196         struct fsnotify_mark *inode_mark = NULL, *vfsmount_mark = NULL;
197         struct fsnotify_group *inode_group, *vfsmount_group;
198         struct mount *mnt;
199         int idx, ret = 0;
200         /* global tests shouldn't care about events on child only the specific event */
201         __u32 test_mask = (mask & ~FS_EVENT_ON_CHILD);
202 
203         if (data_is == FSNOTIFY_EVENT_PATH)
204                 mnt = real_mount(((struct path *)data)->mnt);
205         else
206                 mnt = NULL;
207 
208         /*
209          * if this is a modify event we may need to clear the ignored masks
210          * otherwise return if neither the inode nor the vfsmount care about
211          * this type of event.
212          */
213         if (!(mask & FS_MODIFY) &&
214             !(test_mask & to_tell->i_fsnotify_mask) &&
215             !(mnt && test_mask & mnt->mnt_fsnotify_mask))
216                 return 0;
217 
218         idx = srcu_read_lock(&fsnotify_mark_srcu);
219 
220         if ((mask & FS_MODIFY) ||
221             (test_mask & to_tell->i_fsnotify_mask))
222                 inode_node = srcu_dereference(to_tell->i_fsnotify_marks.first,
223                                               &fsnotify_mark_srcu);
224 
225         if (mnt && ((mask & FS_MODIFY) ||
226                     (test_mask & mnt->mnt_fsnotify_mask))) {
227                 vfsmount_node = srcu_dereference(mnt->mnt_fsnotify_marks.first,
228                                                  &fsnotify_mark_srcu);
229                 inode_node = srcu_dereference(to_tell->i_fsnotify_marks.first,
230                                               &fsnotify_mark_srcu);
231         }
232 
233         /*
234          * We need to merge inode & vfsmount mark lists so that inode mark
235          * ignore masks are properly reflected for mount mark notifications.
236          * That's why this traversal is so complicated...
237          */
238         while (inode_node || vfsmount_node) {
239                 inode_group = NULL;
240                 inode_mark = NULL;
241                 vfsmount_group = NULL;
242                 vfsmount_mark = NULL;
243 
244                 if (inode_node) {
245                         inode_mark = hlist_entry(srcu_dereference(inode_node, &fsnotify_mark_srcu),
246                                                  struct fsnotify_mark, i.i_list);
247                         inode_group = inode_mark->group;
248                 }
249 
250                 if (vfsmount_node) {
251                         vfsmount_mark = hlist_entry(srcu_dereference(vfsmount_node, &fsnotify_mark_srcu),
252                                                         struct fsnotify_mark, m.m_list);
253                         vfsmount_group = vfsmount_mark->group;
254                 }
255 
256                 if (inode_group && vfsmount_group) {
257                         int cmp = fsnotify_compare_groups(inode_group,
258                                                           vfsmount_group);
259                         if (cmp > 0) {
260                                 inode_group = NULL;
261                                 inode_mark = NULL;
262                         } else if (cmp < 0) {
263                                 vfsmount_group = NULL;
264                                 vfsmount_mark = NULL;
265                         }
266                 }
267                 ret = send_to_group(to_tell, inode_mark, vfsmount_mark, mask,
268                                     data, data_is, cookie, file_name);
269 
270                 if (ret && (mask & ALL_FSNOTIFY_PERM_EVENTS))
271                         goto out;
272 
273                 if (inode_group)
274                         inode_node = srcu_dereference(inode_node->next,
275                                                       &fsnotify_mark_srcu);
276                 if (vfsmount_group)
277                         vfsmount_node = srcu_dereference(vfsmount_node->next,
278                                                          &fsnotify_mark_srcu);
279         }
280         ret = 0;
281 out:
282         srcu_read_unlock(&fsnotify_mark_srcu, idx);
283 
284         return ret;
285 }
286 EXPORT_SYMBOL_GPL(fsnotify);
287 
288 static __init int fsnotify_init(void)
289 {
290         int ret;
291 
292         BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 23);
293 
294         ret = init_srcu_struct(&fsnotify_mark_srcu);
295         if (ret)
296                 panic("initializing fsnotify_mark_srcu");
297 
298         return 0;
299 }
300 core_initcall(fsnotify_init);
301 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp