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