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