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 | |
c28f7e56 | 20 | /* Notify this dentry's parent about a child's events. */ |
12c7f9dc | 21 | static inline int fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask) |
c28f7e56 | 22 | { |
72acc854 | 23 | if (!dentry) |
2069601b | 24 | dentry = path->dentry; |
28c60e37 | 25 | |
52420392 | 26 | return __fsnotify_parent(path, dentry, mask); |
c28f7e56 EP |
27 | } |
28 | ||
c4ec54b4 EP |
29 | /* simple call site for access decisions */ |
30 | static inline int fsnotify_perm(struct file *file, int mask) | |
31 | { | |
40212d53 | 32 | const struct path *path = &file->f_path; |
573e1784 | 33 | struct inode *inode = file_inode(file); |
fb1cfb88 | 34 | __u32 fsnotify_mask = 0; |
52420392 | 35 | int ret; |
c4ec54b4 EP |
36 | |
37 | if (file->f_mode & FMODE_NONOTIFY) | |
38 | return 0; | |
39 | if (!(mask & (MAY_READ | MAY_OPEN))) | |
40 | return 0; | |
c4ec54b4 EP |
41 | if (mask & MAY_OPEN) |
42 | fsnotify_mask = FS_OPEN_PERM; | |
fb1cfb88 EP |
43 | else if (mask & MAY_READ) |
44 | fsnotify_mask = FS_ACCESS_PERM; | |
45 | else | |
46 | BUG(); | |
c4ec54b4 | 47 | |
52420392 EP |
48 | ret = fsnotify_parent(path, NULL, fsnotify_mask); |
49 | if (ret) | |
50 | return ret; | |
51 | ||
2069601b | 52 | return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); |
c4ec54b4 EP |
53 | } |
54 | ||
90586523 EP |
55 | /* |
56 | * fsnotify_link_count - inode's link count changed | |
57 | */ | |
58 | static inline void fsnotify_link_count(struct inode *inode) | |
59 | { | |
47882c6f | 60 | fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
90586523 EP |
61 | } |
62 | ||
0eeca283 RL |
63 | /* |
64 | * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir | |
65 | */ | |
66 | static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, | |
59b0df21 | 67 | const unsigned char *old_name, |
5a190ae6 | 68 | int isdir, struct inode *target, struct dentry *moved) |
0eeca283 | 69 | { |
5a190ae6 | 70 | struct inode *source = moved->d_inode; |
47882c6f | 71 | u32 fs_cookie = fsnotify_get_cookie(); |
ff52cc21 EP |
72 | __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM); |
73 | __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO); | |
59b0df21 | 74 | const unsigned char *new_name = moved->d_name.name; |
0eeca283 | 75 | |
ff52cc21 EP |
76 | if (old_dir == new_dir) |
77 | old_dir_mask |= FS_DN_RENAME; | |
0eeca283 | 78 | |
90586523 | 79 | if (isdir) { |
b29866aa EP |
80 | old_dir_mask |= FS_ISDIR; |
81 | new_dir_mask |= FS_ISDIR; | |
90586523 EP |
82 | } |
83 | ||
6ee8e25f JK |
84 | fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name, |
85 | fs_cookie); | |
86 | fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name, | |
87 | fs_cookie); | |
90586523 | 88 | |
2dfc1cae | 89 | if (target) |
90586523 | 90 | fsnotify_link_count(target); |
89204c40 | 91 | |
2dfc1cae | 92 | if (source) |
47882c6f | 93 | fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
4fa6b5ec | 94 | audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE); |
0eeca283 RL |
95 | } |
96 | ||
3be25f49 EP |
97 | /* |
98 | * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed | |
99 | */ | |
100 | static inline void fsnotify_inode_delete(struct inode *inode) | |
101 | { | |
102 | __fsnotify_inode_delete(inode); | |
103 | } | |
104 | ||
ca9c726e AG |
105 | /* |
106 | * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed | |
107 | */ | |
108 | static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt) | |
109 | { | |
110 | __fsnotify_vfsmount_delete(mnt); | |
111 | } | |
112 | ||
7a91bf7f JM |
113 | /* |
114 | * fsnotify_nameremove - a filename was removed from a directory | |
115 | */ | |
116 | static inline void fsnotify_nameremove(struct dentry *dentry, int isdir) | |
117 | { | |
90586523 EP |
118 | __u32 mask = FS_DELETE; |
119 | ||
7a91bf7f | 120 | if (isdir) |
b29866aa | 121 | mask |= FS_ISDIR; |
c28f7e56 | 122 | |
28c60e37 | 123 | fsnotify_parent(NULL, dentry, mask); |
7a91bf7f JM |
124 | } |
125 | ||
126 | /* | |
127 | * fsnotify_inoderemove - an inode is going away | |
128 | */ | |
129 | static inline void fsnotify_inoderemove(struct inode *inode) | |
130 | { | |
47882c6f | 131 | fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
3be25f49 | 132 | __fsnotify_inode_delete(inode); |
ece95912 JK |
133 | } |
134 | ||
0eeca283 RL |
135 | /* |
136 | * fsnotify_create - 'name' was linked in | |
137 | */ | |
f38aa942 | 138 | static inline void fsnotify_create(struct inode *inode, struct dentry *dentry) |
0eeca283 | 139 | { |
4fa6b5ec | 140 | audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 141 | |
47882c6f | 142 | fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0); |
0eeca283 RL |
143 | } |
144 | ||
ece95912 JK |
145 | /* |
146 | * fsnotify_link - new hardlink in 'inode' directory | |
147 | * Note: We have to pass also the linked inode ptr as some filesystems leave | |
148 | * new_dentry->d_inode NULL and instantiate inode pointer later | |
149 | */ | |
150 | static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry) | |
151 | { | |
ece95912 | 152 | fsnotify_link_count(inode); |
4fa6b5ec | 153 | audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 154 | |
47882c6f | 155 | fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0); |
ece95912 JK |
156 | } |
157 | ||
0eeca283 RL |
158 | /* |
159 | * fsnotify_mkdir - directory 'name' was created | |
160 | */ | |
f38aa942 | 161 | static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry) |
0eeca283 | 162 | { |
b29866aa | 163 | __u32 mask = (FS_CREATE | FS_ISDIR); |
90586523 EP |
164 | struct inode *d_inode = dentry->d_inode; |
165 | ||
4fa6b5ec | 166 | audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 167 | |
47882c6f | 168 | fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0); |
0eeca283 RL |
169 | } |
170 | ||
171 | /* | |
172 | * fsnotify_access - file was read | |
173 | */ | |
2a12a9d7 | 174 | static inline void fsnotify_access(struct file *file) |
0eeca283 | 175 | { |
40212d53 | 176 | const struct path *path = &file->f_path; |
573e1784 | 177 | struct inode *inode = file_inode(file); |
90586523 | 178 | __u32 mask = FS_ACCESS; |
0eeca283 RL |
179 | |
180 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 181 | mask |= FS_ISDIR; |
0eeca283 | 182 | |
ecf081d1 | 183 | if (!(file->f_mode & FMODE_NONOTIFY)) { |
2069601b LT |
184 | fsnotify_parent(path, NULL, mask); |
185 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
ecf081d1 | 186 | } |
0eeca283 RL |
187 | } |
188 | ||
189 | /* | |
190 | * fsnotify_modify - file was modified | |
191 | */ | |
2a12a9d7 | 192 | static inline void fsnotify_modify(struct file *file) |
0eeca283 | 193 | { |
40212d53 | 194 | const struct path *path = &file->f_path; |
573e1784 | 195 | struct inode *inode = file_inode(file); |
90586523 | 196 | __u32 mask = FS_MODIFY; |
0eeca283 RL |
197 | |
198 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 199 | mask |= FS_ISDIR; |
0eeca283 | 200 | |
ecf081d1 | 201 | if (!(file->f_mode & FMODE_NONOTIFY)) { |
2069601b LT |
202 | fsnotify_parent(path, NULL, mask); |
203 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
ecf081d1 | 204 | } |
0eeca283 RL |
205 | } |
206 | ||
207 | /* | |
208 | * fsnotify_open - file was opened | |
209 | */ | |
2a12a9d7 | 210 | static inline void fsnotify_open(struct file *file) |
0eeca283 | 211 | { |
40212d53 | 212 | const struct path *path = &file->f_path; |
573e1784 | 213 | struct inode *inode = file_inode(file); |
90586523 | 214 | __u32 mask = FS_OPEN; |
0eeca283 RL |
215 | |
216 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 217 | mask |= FS_ISDIR; |
9b076f1c MB |
218 | if (file->f_flags & __FMODE_EXEC) |
219 | mask |= FS_OPEN_EXEC; | |
0eeca283 | 220 | |
6bff7ecc LS |
221 | fsnotify_parent(path, NULL, mask); |
222 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
0eeca283 RL |
223 | } |
224 | ||
225 | /* | |
226 | * fsnotify_close - file was closed | |
227 | */ | |
228 | static inline void fsnotify_close(struct file *file) | |
229 | { | |
40212d53 | 230 | const struct path *path = &file->f_path; |
573e1784 | 231 | struct inode *inode = file_inode(file); |
aeb5d727 | 232 | fmode_t mode = file->f_mode; |
90586523 | 233 | __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE; |
0eeca283 RL |
234 | |
235 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 236 | mask |= FS_ISDIR; |
0eeca283 | 237 | |
ecf081d1 | 238 | if (!(file->f_mode & FMODE_NONOTIFY)) { |
2069601b LT |
239 | fsnotify_parent(path, NULL, mask); |
240 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
ecf081d1 | 241 | } |
0eeca283 RL |
242 | } |
243 | ||
244 | /* | |
245 | * fsnotify_xattr - extended attributes were changed | |
246 | */ | |
247 | static inline void fsnotify_xattr(struct dentry *dentry) | |
248 | { | |
249 | struct inode *inode = dentry->d_inode; | |
90586523 | 250 | __u32 mask = FS_ATTRIB; |
0eeca283 RL |
251 | |
252 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 253 | mask |= FS_ISDIR; |
0eeca283 | 254 | |
28c60e37 | 255 | fsnotify_parent(NULL, dentry, mask); |
47882c6f | 256 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
257 | } |
258 | ||
259 | /* | |
260 | * fsnotify_change - notify_change event. file was modified and/or metadata | |
261 | * was changed. | |
262 | */ | |
263 | static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid) | |
264 | { | |
265 | struct inode *inode = dentry->d_inode; | |
3c5119c0 EP |
266 | __u32 mask = 0; |
267 | ||
268 | if (ia_valid & ATTR_UID) | |
269 | mask |= FS_ATTRIB; | |
270 | if (ia_valid & ATTR_GID) | |
271 | mask |= FS_ATTRIB; | |
272 | if (ia_valid & ATTR_SIZE) | |
273 | mask |= FS_MODIFY; | |
0eeca283 | 274 | |
0eeca283 RL |
275 | /* both times implies a utime(s) call */ |
276 | if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) | |
3c5119c0 EP |
277 | mask |= FS_ATTRIB; |
278 | else if (ia_valid & ATTR_ATIME) | |
279 | mask |= FS_ACCESS; | |
280 | else if (ia_valid & ATTR_MTIME) | |
281 | mask |= FS_MODIFY; | |
282 | ||
283 | if (ia_valid & ATTR_MODE) | |
284 | mask |= FS_ATTRIB; | |
0eeca283 | 285 | |
3c5119c0 | 286 | if (mask) { |
0eeca283 | 287 | if (S_ISDIR(inode->i_mode)) |
b29866aa | 288 | mask |= FS_ISDIR; |
28c60e37 EP |
289 | |
290 | fsnotify_parent(NULL, dentry, mask); | |
47882c6f | 291 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
292 | } |
293 | } | |
294 | ||
0eeca283 | 295 | #endif /* _LINUX_FS_NOTIFY_H */ |