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