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