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