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