Merge tag '5.2-smb3' of git://git.samba.org/sfrench/cifs-2.6
[sfrench/cifs-2.6.git] / include / linux / fsnotify.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FS_NOTIFY_H
3 #define _LINUX_FS_NOTIFY_H
4
5 /*
6  * include/linux/fsnotify.h - generic hooks for filesystem notification, to
7  * reduce in-source duplication from both dnotify and inotify.
8  *
9  * We don't compile any of this away in some complicated menagerie of ifdefs.
10  * Instead, we rely on the code inside to optimize away as needed.
11  *
12  * (C) Copyright 2005 Robert Love
13  */
14
15 #include <linux/fsnotify_backend.h>
16 #include <linux/audit.h>
17 #include <linux/slab.h>
18 #include <linux/bug.h>
19
20 /*
21  * Notify this @dir inode about a change in the directory entry @dentry.
22  *
23  * Unlike fsnotify_parent(), the event will be reported regardless of the
24  * FS_EVENT_ON_CHILD mask on the parent inode.
25  */
26 static inline int fsnotify_dirent(struct inode *dir, struct dentry *dentry,
27                                   __u32 mask)
28 {
29         return fsnotify(dir, mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
30                         &dentry->d_name, 0);
31 }
32
33 /* Notify this dentry's parent about a child's events. */
34 static inline int fsnotify_parent(const struct path *path,
35                                   struct dentry *dentry, __u32 mask)
36 {
37         if (!dentry)
38                 dentry = path->dentry;
39
40         return __fsnotify_parent(path, dentry, mask);
41 }
42
43 /*
44  * Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when
45  * an event is on a path.
46  */
47 static inline int fsnotify_path(struct inode *inode, const struct path *path,
48                                 __u32 mask)
49 {
50         int ret = fsnotify_parent(path, NULL, mask);
51
52         if (ret)
53                 return ret;
54         return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
55 }
56
57 /* Simple call site for access decisions */
58 static inline int fsnotify_perm(struct file *file, int mask)
59 {
60         int ret;
61         const struct path *path = &file->f_path;
62         struct inode *inode = file_inode(file);
63         __u32 fsnotify_mask = 0;
64
65         if (file->f_mode & FMODE_NONOTIFY)
66                 return 0;
67         if (!(mask & (MAY_READ | MAY_OPEN)))
68                 return 0;
69         if (mask & MAY_OPEN) {
70                 fsnotify_mask = FS_OPEN_PERM;
71
72                 if (file->f_flags & __FMODE_EXEC) {
73                         ret = fsnotify_path(inode, path, FS_OPEN_EXEC_PERM);
74
75                         if (ret)
76                                 return ret;
77                 }
78         } else if (mask & MAY_READ) {
79                 fsnotify_mask = FS_ACCESS_PERM;
80         }
81
82         if (S_ISDIR(inode->i_mode))
83                 fsnotify_mask |= FS_ISDIR;
84
85         return fsnotify_path(inode, path, fsnotify_mask);
86 }
87
88 /*
89  * fsnotify_link_count - inode's link count changed
90  */
91 static inline void fsnotify_link_count(struct inode *inode)
92 {
93         __u32 mask = FS_ATTRIB;
94
95         if (S_ISDIR(inode->i_mode))
96                 mask |= FS_ISDIR;
97
98         fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
99 }
100
101 /*
102  * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
103  */
104 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
105                                  const struct qstr *old_name,
106                                  int isdir, struct inode *target,
107                                  struct dentry *moved)
108 {
109         struct inode *source = moved->d_inode;
110         u32 fs_cookie = fsnotify_get_cookie();
111         __u32 old_dir_mask = FS_MOVED_FROM;
112         __u32 new_dir_mask = FS_MOVED_TO;
113         __u32 mask = FS_MOVE_SELF;
114         const struct qstr *new_name = &moved->d_name;
115
116         if (old_dir == new_dir)
117                 old_dir_mask |= FS_DN_RENAME;
118
119         if (isdir) {
120                 old_dir_mask |= FS_ISDIR;
121                 new_dir_mask |= FS_ISDIR;
122                 mask |= FS_ISDIR;
123         }
124
125         fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
126                  fs_cookie);
127         fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
128                  fs_cookie);
129
130         if (target)
131                 fsnotify_link_count(target);
132
133         if (source)
134                 fsnotify(source, mask, source, FSNOTIFY_EVENT_INODE, NULL, 0);
135         audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
136 }
137
138 /*
139  * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
140  */
141 static inline void fsnotify_inode_delete(struct inode *inode)
142 {
143         __fsnotify_inode_delete(inode);
144 }
145
146 /*
147  * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
148  */
149 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
150 {
151         __fsnotify_vfsmount_delete(mnt);
152 }
153
154 /*
155  * fsnotify_nameremove - a filename was removed from a directory
156  *
157  * This is mostly called under parent vfs inode lock so name and
158  * dentry->d_parent should be stable. However there are some corner cases where
159  * inode lock is not held. So to be on the safe side and be reselient to future
160  * callers and out of tree users of d_delete(), we do not assume that d_parent
161  * and d_name are stable and we use dget_parent() and
162  * take_dentry_name_snapshot() to grab stable references.
163  */
164 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
165 {
166         struct dentry *parent;
167         struct name_snapshot name;
168         __u32 mask = FS_DELETE;
169
170         /* d_delete() of pseudo inode? (e.g. __ns_get_path() playing tricks) */
171         if (IS_ROOT(dentry))
172                 return;
173
174         if (isdir)
175                 mask |= FS_ISDIR;
176
177         parent = dget_parent(dentry);
178         take_dentry_name_snapshot(&name, dentry);
179
180         fsnotify(d_inode(parent), mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
181                  &name.name, 0);
182
183         release_dentry_name_snapshot(&name);
184         dput(parent);
185 }
186
187 /*
188  * fsnotify_inoderemove - an inode is going away
189  */
190 static inline void fsnotify_inoderemove(struct inode *inode)
191 {
192         __u32 mask = FS_DELETE_SELF;
193
194         if (S_ISDIR(inode->i_mode))
195                 mask |= FS_ISDIR;
196
197         fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
198         __fsnotify_inode_delete(inode);
199 }
200
201 /*
202  * fsnotify_create - 'name' was linked in
203  */
204 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
205 {
206         audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
207
208         fsnotify_dirent(inode, dentry, FS_CREATE);
209 }
210
211 /*
212  * fsnotify_link - new hardlink in 'inode' directory
213  * Note: We have to pass also the linked inode ptr as some filesystems leave
214  *   new_dentry->d_inode NULL and instantiate inode pointer later
215  */
216 static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
217 {
218         fsnotify_link_count(inode);
219         audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
220
221         fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, &new_dentry->d_name, 0);
222 }
223
224 /*
225  * fsnotify_mkdir - directory 'name' was created
226  */
227 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
228 {
229         audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
230
231         fsnotify_dirent(inode, dentry, FS_CREATE | FS_ISDIR);
232 }
233
234 /*
235  * fsnotify_access - file was read
236  */
237 static inline void fsnotify_access(struct file *file)
238 {
239         const struct path *path = &file->f_path;
240         struct inode *inode = file_inode(file);
241         __u32 mask = FS_ACCESS;
242
243         if (S_ISDIR(inode->i_mode))
244                 mask |= FS_ISDIR;
245
246         if (!(file->f_mode & FMODE_NONOTIFY))
247                 fsnotify_path(inode, path, mask);
248 }
249
250 /*
251  * fsnotify_modify - file was modified
252  */
253 static inline void fsnotify_modify(struct file *file)
254 {
255         const struct path *path = &file->f_path;
256         struct inode *inode = file_inode(file);
257         __u32 mask = FS_MODIFY;
258
259         if (S_ISDIR(inode->i_mode))
260                 mask |= FS_ISDIR;
261
262         if (!(file->f_mode & FMODE_NONOTIFY))
263                 fsnotify_path(inode, path, mask);
264 }
265
266 /*
267  * fsnotify_open - file was opened
268  */
269 static inline void fsnotify_open(struct file *file)
270 {
271         const struct path *path = &file->f_path;
272         struct inode *inode = file_inode(file);
273         __u32 mask = FS_OPEN;
274
275         if (S_ISDIR(inode->i_mode))
276                 mask |= FS_ISDIR;
277         if (file->f_flags & __FMODE_EXEC)
278                 mask |= FS_OPEN_EXEC;
279
280         fsnotify_path(inode, path, mask);
281 }
282
283 /*
284  * fsnotify_close - file was closed
285  */
286 static inline void fsnotify_close(struct file *file)
287 {
288         const struct path *path = &file->f_path;
289         struct inode *inode = file_inode(file);
290         fmode_t mode = file->f_mode;
291         __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
292
293         if (S_ISDIR(inode->i_mode))
294                 mask |= FS_ISDIR;
295
296         if (!(file->f_mode & FMODE_NONOTIFY))
297                 fsnotify_path(inode, path, mask);
298 }
299
300 /*
301  * fsnotify_xattr - extended attributes were changed
302  */
303 static inline void fsnotify_xattr(struct dentry *dentry)
304 {
305         struct inode *inode = dentry->d_inode;
306         __u32 mask = FS_ATTRIB;
307
308         if (S_ISDIR(inode->i_mode))
309                 mask |= FS_ISDIR;
310
311         fsnotify_parent(NULL, dentry, mask);
312         fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
313 }
314
315 /*
316  * fsnotify_change - notify_change event.  file was modified and/or metadata
317  * was changed.
318  */
319 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
320 {
321         struct inode *inode = dentry->d_inode;
322         __u32 mask = 0;
323
324         if (ia_valid & ATTR_UID)
325                 mask |= FS_ATTRIB;
326         if (ia_valid & ATTR_GID)
327                 mask |= FS_ATTRIB;
328         if (ia_valid & ATTR_SIZE)
329                 mask |= FS_MODIFY;
330
331         /* both times implies a utime(s) call */
332         if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
333                 mask |= FS_ATTRIB;
334         else if (ia_valid & ATTR_ATIME)
335                 mask |= FS_ACCESS;
336         else if (ia_valid & ATTR_MTIME)
337                 mask |= FS_MODIFY;
338
339         if (ia_valid & ATTR_MODE)
340                 mask |= FS_ATTRIB;
341
342         if (mask) {
343                 if (S_ISDIR(inode->i_mode))
344                         mask |= FS_ISDIR;
345
346                 fsnotify_parent(NULL, dentry, mask);
347                 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
348         }
349 }
350
351 #endif  /* _LINUX_FS_NOTIFY_H */