]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
0eeca283 RL |
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 | ||
90586523 | 15 | #include <linux/fsnotify_backend.h> |
73241ccc | 16 | #include <linux/audit.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
187f1882 | 18 | #include <linux/bug.h> |
0eeca283 | 19 | |
5f02a877 AG |
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, | |
25b229df | 30 | &dentry->d_name, 0); |
5f02a877 AG |
31 | } |
32 | ||
c28f7e56 | 33 | /* Notify this dentry's parent about a child's events. */ |
5f02a877 AG |
34 | static inline int fsnotify_parent(const struct path *path, |
35 | struct dentry *dentry, __u32 mask) | |
c28f7e56 | 36 | { |
72acc854 | 37 | if (!dentry) |
2069601b | 38 | dentry = path->dentry; |
28c60e37 | 39 | |
52420392 | 40 | return __fsnotify_parent(path, dentry, mask); |
c28f7e56 EP |
41 | } |
42 | ||
a704bba5 MB |
43 | /* |
44 | * Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when | |
45 | * an event is on a path. | |
46 | */ | |
47 | static inline int fsnotify_path(struct inode *inode, const struct path *path, | |
48 | __u32 mask) | |
49 | { | |
50 | int ret = fsnotify_parent(path, NULL, mask); | |
51 | ||
52 | if (ret) | |
53 | return ret; | |
54 | return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
55 | } | |
56 | ||
66917a31 | 57 | /* Simple call site for access decisions */ |
c4ec54b4 EP |
58 | static inline int fsnotify_perm(struct file *file, int mask) |
59 | { | |
66917a31 | 60 | int ret; |
40212d53 | 61 | const struct path *path = &file->f_path; |
573e1784 | 62 | struct inode *inode = file_inode(file); |
fb1cfb88 | 63 | __u32 fsnotify_mask = 0; |
c4ec54b4 EP |
64 | |
65 | if (file->f_mode & FMODE_NONOTIFY) | |
66 | return 0; | |
67 | if (!(mask & (MAY_READ | MAY_OPEN))) | |
68 | return 0; | |
66917a31 | 69 | if (mask & MAY_OPEN) { |
c4ec54b4 | 70 | fsnotify_mask = FS_OPEN_PERM; |
66917a31 MB |
71 | |
72 | if (file->f_flags & __FMODE_EXEC) { | |
73 | ret = fsnotify_path(inode, path, FS_OPEN_EXEC_PERM); | |
74 | ||
75 | if (ret) | |
76 | return ret; | |
77 | } | |
78 | } else if (mask & MAY_READ) { | |
fb1cfb88 | 79 | fsnotify_mask = FS_ACCESS_PERM; |
66917a31 | 80 | } |
c4ec54b4 | 81 | |
0321e03c AG |
82 | if (S_ISDIR(inode->i_mode)) |
83 | fsnotify_mask |= FS_ISDIR; | |
84 | ||
a704bba5 | 85 | return fsnotify_path(inode, path, fsnotify_mask); |
c4ec54b4 EP |
86 | } |
87 | ||
90586523 EP |
88 | /* |
89 | * fsnotify_link_count - inode's link count changed | |
90 | */ | |
91 | static inline void fsnotify_link_count(struct inode *inode) | |
92 | { | |
0a20df7e AG |
93 | __u32 mask = FS_ATTRIB; |
94 | ||
95 | if (S_ISDIR(inode->i_mode)) | |
96 | mask |= FS_ISDIR; | |
97 | ||
98 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); | |
90586523 EP |
99 | } |
100 | ||
0eeca283 RL |
101 | /* |
102 | * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir | |
103 | */ | |
104 | static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, | |
f4ec3a3d | 105 | const struct qstr *old_name, |
0a20df7e AG |
106 | int isdir, struct inode *target, |
107 | struct dentry *moved) | |
0eeca283 | 108 | { |
5a190ae6 | 109 | struct inode *source = moved->d_inode; |
47882c6f | 110 | u32 fs_cookie = fsnotify_get_cookie(); |
5f02a877 AG |
111 | __u32 old_dir_mask = FS_MOVED_FROM; |
112 | __u32 new_dir_mask = FS_MOVED_TO; | |
0a20df7e | 113 | __u32 mask = FS_MOVE_SELF; |
25b229df | 114 | const struct qstr *new_name = &moved->d_name; |
0eeca283 | 115 | |
ff52cc21 EP |
116 | if (old_dir == new_dir) |
117 | old_dir_mask |= FS_DN_RENAME; | |
0eeca283 | 118 | |
90586523 | 119 | if (isdir) { |
b29866aa EP |
120 | old_dir_mask |= FS_ISDIR; |
121 | new_dir_mask |= FS_ISDIR; | |
0a20df7e | 122 | mask |= FS_ISDIR; |
90586523 EP |
123 | } |
124 | ||
25b229df | 125 | fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name, |
6ee8e25f JK |
126 | fs_cookie); |
127 | fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name, | |
128 | fs_cookie); | |
90586523 | 129 | |
2dfc1cae | 130 | if (target) |
90586523 | 131 | fsnotify_link_count(target); |
89204c40 | 132 | |
2dfc1cae | 133 | if (source) |
0a20df7e | 134 | fsnotify(source, mask, source, FSNOTIFY_EVENT_INODE, NULL, 0); |
4fa6b5ec | 135 | audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE); |
0eeca283 RL |
136 | } |
137 | ||
3be25f49 EP |
138 | /* |
139 | * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed | |
140 | */ | |
141 | static inline void fsnotify_inode_delete(struct inode *inode) | |
142 | { | |
143 | __fsnotify_inode_delete(inode); | |
144 | } | |
145 | ||
ca9c726e AG |
146 | /* |
147 | * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed | |
148 | */ | |
149 | static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt) | |
150 | { | |
151 | __fsnotify_vfsmount_delete(mnt); | |
152 | } | |
153 | ||
7a91bf7f JM |
154 | /* |
155 | * fsnotify_inoderemove - an inode is going away | |
156 | */ | |
157 | static inline void fsnotify_inoderemove(struct inode *inode) | |
158 | { | |
0a20df7e AG |
159 | __u32 mask = FS_DELETE_SELF; |
160 | ||
161 | if (S_ISDIR(inode->i_mode)) | |
162 | mask |= FS_ISDIR; | |
163 | ||
164 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); | |
3be25f49 | 165 | __fsnotify_inode_delete(inode); |
ece95912 JK |
166 | } |
167 | ||
0eeca283 RL |
168 | /* |
169 | * fsnotify_create - 'name' was linked in | |
170 | */ | |
f38aa942 | 171 | static inline void fsnotify_create(struct inode *inode, struct dentry *dentry) |
0eeca283 | 172 | { |
4fa6b5ec | 173 | audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 174 | |
5f02a877 | 175 | fsnotify_dirent(inode, dentry, FS_CREATE); |
0eeca283 RL |
176 | } |
177 | ||
ece95912 JK |
178 | /* |
179 | * fsnotify_link - new hardlink in 'inode' directory | |
180 | * Note: We have to pass also the linked inode ptr as some filesystems leave | |
181 | * new_dentry->d_inode NULL and instantiate inode pointer later | |
182 | */ | |
183 | static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry) | |
184 | { | |
ece95912 | 185 | fsnotify_link_count(inode); |
4fa6b5ec | 186 | audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 187 | |
25b229df | 188 | fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, &new_dentry->d_name, 0); |
ece95912 JK |
189 | } |
190 | ||
a7dd760a AG |
191 | /* |
192 | * fsnotify_delete - @dentry was unlinked and unhashed | |
193 | * | |
194 | * Caller must make sure that dentry->d_name is stable. | |
195 | * | |
196 | * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode | |
197 | * as this may be called after d_delete() and old_dentry may be negative. | |
198 | */ | |
199 | static inline void fsnotify_delete(struct inode *dir, struct inode *inode, | |
200 | struct dentry *dentry) | |
201 | { | |
202 | __u32 mask = FS_DELETE; | |
203 | ||
204 | if (S_ISDIR(inode->i_mode)) | |
205 | mask |= FS_ISDIR; | |
206 | ||
207 | fsnotify(dir, mask, inode, FSNOTIFY_EVENT_INODE, &dentry->d_name, 0); | |
208 | } | |
209 | ||
210 | /** | |
211 | * d_delete_notify - delete a dentry and call fsnotify_delete() | |
212 | * @dentry: The dentry to delete | |
213 | * | |
214 | * This helper is used to guaranty that the unlinked inode cannot be found | |
215 | * by lookup of this name after fsnotify_delete() event has been delivered. | |
216 | */ | |
217 | static inline void d_delete_notify(struct inode *dir, struct dentry *dentry) | |
218 | { | |
219 | struct inode *inode = d_inode(dentry); | |
220 | ||
221 | ihold(inode); | |
222 | d_delete(dentry); | |
223 | fsnotify_delete(dir, inode, dentry); | |
224 | iput(inode); | |
225 | } | |
226 | ||
116b9731 AG |
227 | /* |
228 | * fsnotify_unlink - 'name' was unlinked | |
229 | * | |
230 | * Caller must make sure that dentry->d_name is stable. | |
231 | */ | |
232 | static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry) | |
233 | { | |
a7dd760a AG |
234 | if (WARN_ON_ONCE(d_is_negative(dentry))) |
235 | return; | |
116b9731 | 236 | |
a7dd760a | 237 | fsnotify_delete(dir, d_inode(dentry), dentry); |
116b9731 AG |
238 | } |
239 | ||
0eeca283 RL |
240 | /* |
241 | * fsnotify_mkdir - directory 'name' was created | |
242 | */ | |
f38aa942 | 243 | static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry) |
0eeca283 | 244 | { |
4fa6b5ec | 245 | audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 246 | |
5f02a877 | 247 | fsnotify_dirent(inode, dentry, FS_CREATE | FS_ISDIR); |
0eeca283 RL |
248 | } |
249 | ||
116b9731 AG |
250 | /* |
251 | * fsnotify_rmdir - directory 'name' was removed | |
252 | * | |
253 | * Caller must make sure that dentry->d_name is stable. | |
254 | */ | |
255 | static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry) | |
256 | { | |
a7dd760a AG |
257 | if (WARN_ON_ONCE(d_is_negative(dentry))) |
258 | return; | |
116b9731 | 259 | |
a7dd760a | 260 | fsnotify_delete(dir, d_inode(dentry), dentry); |
116b9731 AG |
261 | } |
262 | ||
0eeca283 RL |
263 | /* |
264 | * fsnotify_access - file was read | |
265 | */ | |
2a12a9d7 | 266 | static inline void fsnotify_access(struct file *file) |
0eeca283 | 267 | { |
40212d53 | 268 | const struct path *path = &file->f_path; |
573e1784 | 269 | struct inode *inode = file_inode(file); |
90586523 | 270 | __u32 mask = FS_ACCESS; |
0eeca283 RL |
271 | |
272 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 273 | mask |= FS_ISDIR; |
0eeca283 | 274 | |
a704bba5 MB |
275 | if (!(file->f_mode & FMODE_NONOTIFY)) |
276 | fsnotify_path(inode, path, mask); | |
0eeca283 RL |
277 | } |
278 | ||
279 | /* | |
280 | * fsnotify_modify - file was modified | |
281 | */ | |
2a12a9d7 | 282 | static inline void fsnotify_modify(struct file *file) |
0eeca283 | 283 | { |
40212d53 | 284 | const struct path *path = &file->f_path; |
573e1784 | 285 | struct inode *inode = file_inode(file); |
90586523 | 286 | __u32 mask = FS_MODIFY; |
0eeca283 RL |
287 | |
288 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 289 | mask |= FS_ISDIR; |
0eeca283 | 290 | |
a704bba5 MB |
291 | if (!(file->f_mode & FMODE_NONOTIFY)) |
292 | fsnotify_path(inode, path, mask); | |
0eeca283 RL |
293 | } |
294 | ||
295 | /* | |
296 | * fsnotify_open - file was opened | |
297 | */ | |
2a12a9d7 | 298 | static inline void fsnotify_open(struct file *file) |
0eeca283 | 299 | { |
40212d53 | 300 | const struct path *path = &file->f_path; |
573e1784 | 301 | struct inode *inode = file_inode(file); |
90586523 | 302 | __u32 mask = FS_OPEN; |
0eeca283 RL |
303 | |
304 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 305 | mask |= FS_ISDIR; |
9b076f1c MB |
306 | if (file->f_flags & __FMODE_EXEC) |
307 | mask |= FS_OPEN_EXEC; | |
0eeca283 | 308 | |
a704bba5 | 309 | fsnotify_path(inode, path, mask); |
0eeca283 RL |
310 | } |
311 | ||
312 | /* | |
313 | * fsnotify_close - file was closed | |
314 | */ | |
315 | static inline void fsnotify_close(struct file *file) | |
316 | { | |
40212d53 | 317 | const struct path *path = &file->f_path; |
573e1784 | 318 | struct inode *inode = file_inode(file); |
aeb5d727 | 319 | fmode_t mode = file->f_mode; |
90586523 | 320 | __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE; |
0eeca283 RL |
321 | |
322 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 323 | mask |= FS_ISDIR; |
0eeca283 | 324 | |
a704bba5 MB |
325 | if (!(file->f_mode & FMODE_NONOTIFY)) |
326 | fsnotify_path(inode, path, mask); | |
0eeca283 RL |
327 | } |
328 | ||
329 | /* | |
330 | * fsnotify_xattr - extended attributes were changed | |
331 | */ | |
332 | static inline void fsnotify_xattr(struct dentry *dentry) | |
333 | { | |
334 | struct inode *inode = dentry->d_inode; | |
90586523 | 335 | __u32 mask = FS_ATTRIB; |
0eeca283 RL |
336 | |
337 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 338 | mask |= FS_ISDIR; |
0eeca283 | 339 | |
28c60e37 | 340 | fsnotify_parent(NULL, dentry, mask); |
47882c6f | 341 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
342 | } |
343 | ||
344 | /* | |
345 | * fsnotify_change - notify_change event. file was modified and/or metadata | |
346 | * was changed. | |
347 | */ | |
348 | static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid) | |
349 | { | |
350 | struct inode *inode = dentry->d_inode; | |
3c5119c0 EP |
351 | __u32 mask = 0; |
352 | ||
353 | if (ia_valid & ATTR_UID) | |
354 | mask |= FS_ATTRIB; | |
355 | if (ia_valid & ATTR_GID) | |
356 | mask |= FS_ATTRIB; | |
357 | if (ia_valid & ATTR_SIZE) | |
358 | mask |= FS_MODIFY; | |
0eeca283 | 359 | |
0eeca283 RL |
360 | /* both times implies a utime(s) call */ |
361 | if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) | |
3c5119c0 EP |
362 | mask |= FS_ATTRIB; |
363 | else if (ia_valid & ATTR_ATIME) | |
364 | mask |= FS_ACCESS; | |
365 | else if (ia_valid & ATTR_MTIME) | |
366 | mask |= FS_MODIFY; | |
367 | ||
368 | if (ia_valid & ATTR_MODE) | |
369 | mask |= FS_ATTRIB; | |
0eeca283 | 370 | |
3c5119c0 | 371 | if (mask) { |
0eeca283 | 372 | if (S_ISDIR(inode->i_mode)) |
b29866aa | 373 | mask |= FS_ISDIR; |
28c60e37 EP |
374 | |
375 | fsnotify_parent(NULL, dentry, mask); | |
47882c6f | 376 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
377 | } |
378 | } | |
379 | ||
0eeca283 | 380 | #endif /* _LINUX_FS_NOTIFY_H */ |