1
2#ifndef _LINUX_FS_NOTIFY_H
3#define _LINUX_FS_NOTIFY_H
4
5
6
7
8
9
10
11
12
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
22
23
24
25
26
27
28
29static inline void fsnotify_name(struct inode *dir, __u32 mask,
30 struct inode *child,
31 const struct qstr *name, u32 cookie)
32{
33 if (atomic_long_read(&dir->i_sb->s_fsnotify_connectors) == 0)
34 return;
35
36 fsnotify(mask, child, FSNOTIFY_EVENT_INODE, dir, name, NULL, cookie);
37}
38
39static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry,
40 __u32 mask)
41{
42 fsnotify_name(dir, mask, d_inode(dentry), &dentry->d_name, 0);
43}
44
45static inline void fsnotify_inode(struct inode *inode, __u32 mask)
46{
47 if (atomic_long_read(&inode->i_sb->s_fsnotify_connectors) == 0)
48 return;
49
50 if (S_ISDIR(inode->i_mode))
51 mask |= FS_ISDIR;
52
53 fsnotify(mask, inode, FSNOTIFY_EVENT_INODE, NULL, NULL, inode, 0);
54}
55
56
57static inline int fsnotify_parent(struct dentry *dentry, __u32 mask,
58 const void *data, int data_type)
59{
60 struct inode *inode = d_inode(dentry);
61
62 if (atomic_long_read(&inode->i_sb->s_fsnotify_connectors) == 0)
63 return 0;
64
65 if (S_ISDIR(inode->i_mode)) {
66 mask |= FS_ISDIR;
67
68
69 if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
70 goto notify_child;
71 }
72
73
74 if (IS_ROOT(dentry))
75 goto notify_child;
76
77 return __fsnotify_parent(dentry, mask, data, data_type);
78
79notify_child:
80 return fsnotify(mask, data, data_type, NULL, NULL, inode, 0);
81}
82
83
84
85
86
87static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
88{
89 fsnotify_parent(dentry, mask, d_inode(dentry), FSNOTIFY_EVENT_INODE);
90}
91
92static inline int fsnotify_file(struct file *file, __u32 mask)
93{
94 const struct path *path = &file->f_path;
95
96 if (file->f_mode & FMODE_NONOTIFY)
97 return 0;
98
99 return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
100}
101
102
103static inline int fsnotify_perm(struct file *file, int mask)
104{
105 int ret;
106 __u32 fsnotify_mask = 0;
107
108 if (!(mask & (MAY_READ | MAY_OPEN)))
109 return 0;
110
111 if (mask & MAY_OPEN) {
112 fsnotify_mask = FS_OPEN_PERM;
113
114 if (file->f_flags & __FMODE_EXEC) {
115 ret = fsnotify_file(file, FS_OPEN_EXEC_PERM);
116
117 if (ret)
118 return ret;
119 }
120 } else if (mask & MAY_READ) {
121 fsnotify_mask = FS_ACCESS_PERM;
122 }
123
124 return fsnotify_file(file, fsnotify_mask);
125}
126
127
128
129
130static inline void fsnotify_link_count(struct inode *inode)
131{
132 fsnotify_inode(inode, FS_ATTRIB);
133}
134
135
136
137
138static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
139 const struct qstr *old_name,
140 int isdir, struct inode *target,
141 struct dentry *moved)
142{
143 struct inode *source = moved->d_inode;
144 u32 fs_cookie = fsnotify_get_cookie();
145 __u32 old_dir_mask = FS_MOVED_FROM;
146 __u32 new_dir_mask = FS_MOVED_TO;
147 const struct qstr *new_name = &moved->d_name;
148
149 if (old_dir == new_dir)
150 old_dir_mask |= FS_DN_RENAME;
151
152 if (isdir) {
153 old_dir_mask |= FS_ISDIR;
154 new_dir_mask |= FS_ISDIR;
155 }
156
157 fsnotify_name(old_dir, old_dir_mask, source, old_name, fs_cookie);
158 fsnotify_name(new_dir, new_dir_mask, source, new_name, fs_cookie);
159
160 if (target)
161 fsnotify_link_count(target);
162 fsnotify_inode(source, FS_MOVE_SELF);
163 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
164}
165
166
167
168
169static inline void fsnotify_inode_delete(struct inode *inode)
170{
171 __fsnotify_inode_delete(inode);
172}
173
174
175
176
177static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
178{
179 __fsnotify_vfsmount_delete(mnt);
180}
181
182
183
184
185static inline void fsnotify_inoderemove(struct inode *inode)
186{
187 fsnotify_inode(inode, FS_DELETE_SELF);
188 __fsnotify_inode_delete(inode);
189}
190
191
192
193
194static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
195{
196 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
197
198 fsnotify_dirent(inode, dentry, FS_CREATE);
199}
200
201
202
203
204
205
206static inline void fsnotify_link(struct inode *dir, struct inode *inode,
207 struct dentry *new_dentry)
208{
209 fsnotify_link_count(inode);
210 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
211
212 fsnotify_name(dir, FS_CREATE, inode, &new_dentry->d_name, 0);
213}
214
215
216
217
218
219
220static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
221{
222
223 WARN_ON_ONCE(d_is_negative(dentry));
224
225 fsnotify_dirent(dir, dentry, FS_DELETE);
226}
227
228
229
230
231static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
232{
233 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
234
235 fsnotify_dirent(inode, dentry, FS_CREATE | FS_ISDIR);
236}
237
238
239
240
241
242
243static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
244{
245
246 WARN_ON_ONCE(d_is_negative(dentry));
247
248 fsnotify_dirent(dir, dentry, FS_DELETE | FS_ISDIR);
249}
250
251
252
253
254static inline void fsnotify_access(struct file *file)
255{
256 fsnotify_file(file, FS_ACCESS);
257}
258
259
260
261
262static inline void fsnotify_modify(struct file *file)
263{
264 fsnotify_file(file, FS_MODIFY);
265}
266
267
268
269
270static inline void fsnotify_open(struct file *file)
271{
272 __u32 mask = FS_OPEN;
273
274 if (file->f_flags & __FMODE_EXEC)
275 mask |= FS_OPEN_EXEC;
276
277 fsnotify_file(file, mask);
278}
279
280
281
282
283static inline void fsnotify_close(struct file *file)
284{
285 __u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE :
286 FS_CLOSE_NOWRITE;
287
288 fsnotify_file(file, mask);
289}
290
291
292
293
294static inline void fsnotify_xattr(struct dentry *dentry)
295{
296 fsnotify_dentry(dentry, FS_ATTRIB);
297}
298
299
300
301
302
303static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
304{
305 __u32 mask = 0;
306
307 if (ia_valid & ATTR_UID)
308 mask |= FS_ATTRIB;
309 if (ia_valid & ATTR_GID)
310 mask |= FS_ATTRIB;
311 if (ia_valid & ATTR_SIZE)
312 mask |= FS_MODIFY;
313
314
315 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
316 mask |= FS_ATTRIB;
317 else if (ia_valid & ATTR_ATIME)
318 mask |= FS_ACCESS;
319 else if (ia_valid & ATTR_MTIME)
320 mask |= FS_MODIFY;
321
322 if (ia_valid & ATTR_MODE)
323 mask |= FS_ATTRIB;
324
325 if (mask)
326 fsnotify_dentry(dentry, mask);
327}
328
329#endif
330