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