fsnotify: fold fsnotify() call into fsnotify_parent()
[linux-2.6-microblaze.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 a child directory entry.
22  * The directory entry may have turned positive or negative or its inode may
23  * have changed (i.e. renamed over).
24  *
25  * Unlike fsnotify_parent(), the event will be reported regardless of the
26  * FS_EVENT_ON_CHILD mask on the parent inode.
27  */
28 static inline void fsnotify_name(struct inode *dir, __u32 mask,
29                                  struct inode *child,
30                                  const struct qstr *name, u32 cookie)
31 {
32         fsnotify(dir, mask, child, FSNOTIFY_EVENT_INODE, name, cookie);
33         /*
34          * Send another flavor of the event without child inode data and
35          * without the specific event type (e.g. FS_CREATE|FS_IS_DIR).
36          * The name is relative to the dir inode the event is reported to.
37          */
38         fsnotify(dir, FS_DIR_MODIFY, dir, FSNOTIFY_EVENT_INODE, name, 0);
39 }
40
41 static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
42                                    __u32 mask)
43 {
44         fsnotify_name(dir, mask, d_inode(dentry), &dentry->d_name, 0);
45 }
46
47 /* Notify this dentry's parent about a child's events. */
48 static inline int fsnotify_parent(struct dentry *dentry, __u32 mask,
49                                   const void *data, int data_type)
50 {
51         struct inode *inode = d_inode(dentry);
52
53         if (S_ISDIR(inode->i_mode))
54                 mask |= FS_ISDIR;
55
56         if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
57                 goto notify_child;
58
59         return __fsnotify_parent(dentry, mask, data, data_type);
60
61 notify_child:
62         return fsnotify(inode, mask, data, data_type, NULL, 0);
63 }
64
65 /*
66  * Simple wrappers to consolidate calls to fsnotify_parent() when an event
67  * is on a file/dentry.
68  */
69 static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
70 {
71         fsnotify_parent(dentry, mask, d_inode(dentry), FSNOTIFY_EVENT_INODE);
72 }
73
74 static inline int fsnotify_file(struct file *file, __u32 mask)
75 {
76         const struct path *path = &file->f_path;
77
78         if (file->f_mode & FMODE_NONOTIFY)
79                 return 0;
80
81         return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
82 }
83
84 /* Simple call site for access decisions */
85 static inline int fsnotify_perm(struct file *file, int mask)
86 {
87         int ret;
88         __u32 fsnotify_mask = 0;
89
90         if (!(mask & (MAY_READ | MAY_OPEN)))
91                 return 0;
92
93         if (mask & MAY_OPEN) {
94                 fsnotify_mask = FS_OPEN_PERM;
95
96                 if (file->f_flags & __FMODE_EXEC) {
97                         ret = fsnotify_file(file, FS_OPEN_EXEC_PERM);
98
99                         if (ret)
100                                 return ret;
101                 }
102         } else if (mask & MAY_READ) {
103                 fsnotify_mask = FS_ACCESS_PERM;
104         }
105
106         return fsnotify_file(file, fsnotify_mask);
107 }
108
109 /*
110  * fsnotify_link_count - inode's link count changed
111  */
112 static inline void fsnotify_link_count(struct inode *inode)
113 {
114         __u32 mask = FS_ATTRIB;
115
116         if (S_ISDIR(inode->i_mode))
117                 mask |= FS_ISDIR;
118
119         fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
120 }
121
122 /*
123  * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
124  */
125 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
126                                  const struct qstr *old_name,
127                                  int isdir, struct inode *target,
128                                  struct dentry *moved)
129 {
130         struct inode *source = moved->d_inode;
131         u32 fs_cookie = fsnotify_get_cookie();
132         __u32 old_dir_mask = FS_MOVED_FROM;
133         __u32 new_dir_mask = FS_MOVED_TO;
134         __u32 mask = FS_MOVE_SELF;
135         const struct qstr *new_name = &moved->d_name;
136
137         if (old_dir == new_dir)
138                 old_dir_mask |= FS_DN_RENAME;
139
140         if (isdir) {
141                 old_dir_mask |= FS_ISDIR;
142                 new_dir_mask |= FS_ISDIR;
143                 mask |= FS_ISDIR;
144         }
145
146         fsnotify_name(old_dir, old_dir_mask, source, old_name, fs_cookie);
147         fsnotify_name(new_dir, new_dir_mask, source, new_name, fs_cookie);
148
149         if (target)
150                 fsnotify_link_count(target);
151
152         if (source)
153                 fsnotify(source, mask, source, FSNOTIFY_EVENT_INODE, NULL, 0);
154         audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
155 }
156
157 /*
158  * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
159  */
160 static inline void fsnotify_inode_delete(struct inode *inode)
161 {
162         __fsnotify_inode_delete(inode);
163 }
164
165 /*
166  * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
167  */
168 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
169 {
170         __fsnotify_vfsmount_delete(mnt);
171 }
172
173 /*
174  * fsnotify_inoderemove - an inode is going away
175  */
176 static inline void fsnotify_inoderemove(struct inode *inode)
177 {
178         __u32 mask = FS_DELETE_SELF;
179
180         if (S_ISDIR(inode->i_mode))
181                 mask |= FS_ISDIR;
182
183         fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
184         __fsnotify_inode_delete(inode);
185 }
186
187 /*
188  * fsnotify_create - 'name' was linked in
189  */
190 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
191 {
192         audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
193
194         fsnotify_dirent(inode, dentry, FS_CREATE);
195 }
196
197 /*
198  * fsnotify_link - new hardlink in 'inode' directory
199  * Note: We have to pass also the linked inode ptr as some filesystems leave
200  *   new_dentry->d_inode NULL and instantiate inode pointer later
201  */
202 static inline void fsnotify_link(struct inode *dir, struct inode *inode,
203                                  struct dentry *new_dentry)
204 {
205         fsnotify_link_count(inode);
206         audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
207
208         fsnotify_name(dir, FS_CREATE, inode, &new_dentry->d_name, 0);
209 }
210
211 /*
212  * fsnotify_unlink - 'name' was unlinked
213  *
214  * Caller must make sure that dentry->d_name is stable.
215  */
216 static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
217 {
218         /* Expected to be called before d_delete() */
219         WARN_ON_ONCE(d_is_negative(dentry));
220
221         fsnotify_dirent(dir, dentry, FS_DELETE);
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_rmdir - directory 'name' was removed
236  *
237  * Caller must make sure that dentry->d_name is stable.
238  */
239 static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
240 {
241         /* Expected to be called before d_delete() */
242         WARN_ON_ONCE(d_is_negative(dentry));
243
244         fsnotify_dirent(dir, dentry, FS_DELETE | FS_ISDIR);
245 }
246
247 /*
248  * fsnotify_access - file was read
249  */
250 static inline void fsnotify_access(struct file *file)
251 {
252         fsnotify_file(file, FS_ACCESS);
253 }
254
255 /*
256  * fsnotify_modify - file was modified
257  */
258 static inline void fsnotify_modify(struct file *file)
259 {
260         fsnotify_file(file, FS_MODIFY);
261 }
262
263 /*
264  * fsnotify_open - file was opened
265  */
266 static inline void fsnotify_open(struct file *file)
267 {
268         __u32 mask = FS_OPEN;
269
270         if (file->f_flags & __FMODE_EXEC)
271                 mask |= FS_OPEN_EXEC;
272
273         fsnotify_file(file, mask);
274 }
275
276 /*
277  * fsnotify_close - file was closed
278  */
279 static inline void fsnotify_close(struct file *file)
280 {
281         __u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
282                                                     FS_CLOSE_NOWRITE;
283
284         fsnotify_file(file, mask);
285 }
286
287 /*
288  * fsnotify_xattr - extended attributes were changed
289  */
290 static inline void fsnotify_xattr(struct dentry *dentry)
291 {
292         fsnotify_dentry(dentry, FS_ATTRIB);
293 }
294
295 /*
296  * fsnotify_change - notify_change event.  file was modified and/or metadata
297  * was changed.
298  */
299 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
300 {
301         __u32 mask = 0;
302
303         if (ia_valid & ATTR_UID)
304                 mask |= FS_ATTRIB;
305         if (ia_valid & ATTR_GID)
306                 mask |= FS_ATTRIB;
307         if (ia_valid & ATTR_SIZE)
308                 mask |= FS_MODIFY;
309
310         /* both times implies a utime(s) call */
311         if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
312                 mask |= FS_ATTRIB;
313         else if (ia_valid & ATTR_ATIME)
314                 mask |= FS_ACCESS;
315         else if (ia_valid & ATTR_MTIME)
316                 mask |= FS_MODIFY;
317
318         if (ia_valid & ATTR_MODE)
319                 mask |= FS_ATTRIB;
320
321         if (mask)
322                 fsnotify_dentry(dentry, mask);
323 }
324
325 #endif  /* _LINUX_FS_NOTIFY_H */