blob: 9becae61002236fe4c088595dcfac473e3294764 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Robert Love0eeca282005-07-12 17:06:03 -04002#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
Eric Paris90586522009-05-21 17:01:20 -040015#include <linux/fsnotify_backend.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000016#include <linux/audit.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050018#include <linux/bug.h>
Robert Love0eeca282005-07-12 17:06:03 -040019
Amir Goldstein5f02a872019-01-10 19:04:28 +020020/*
21 * Notify this @dir inode about a change in the directory entry @dentry.
22 *
23 * Unlike fsnotify_parent(), the event will be reported regardless of the
24 * FS_EVENT_ON_CHILD mask on the parent inode.
25 */
26static inline int fsnotify_dirent(struct inode *dir, struct dentry *dentry,
27 __u32 mask)
28{
29 return fsnotify(dir, mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
30 dentry->d_name.name, 0);
31}
32
Eric Parisc28f7e52009-05-21 17:01:29 -040033/* Notify this dentry's parent about a child's events. */
Amir Goldstein5f02a872019-01-10 19:04:28 +020034static inline int fsnotify_parent(const struct path *path,
35 struct dentry *dentry, __u32 mask)
Eric Parisc28f7e52009-05-21 17:01:29 -040036{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050037 if (!dentry)
Linus Torvalds20696012010-08-12 14:23:04 -070038 dentry = path->dentry;
Eric Paris28c60e32009-12-17 21:24:21 -050039
Eric Paris52420392010-10-28 17:21:56 -040040 return __fsnotify_parent(path, dentry, mask);
Eric Parisc28f7e52009-05-21 17:01:29 -040041}
42
Matthew Bobrowskia704bba2018-11-08 14:10:03 +110043/*
44 * Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when
45 * an event is on a path.
46 */
47static inline int fsnotify_path(struct inode *inode, const struct path *path,
48 __u32 mask)
49{
50 int ret = fsnotify_parent(path, NULL, mask);
51
52 if (ret)
53 return ret;
54 return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
55}
56
Matthew Bobrowski66917a32018-11-08 14:12:44 +110057/* Simple call site for access decisions */
Eric Parisc4ec54b2009-12-17 21:24:34 -050058static inline int fsnotify_perm(struct file *file, int mask)
59{
Matthew Bobrowski66917a32018-11-08 14:12:44 +110060 int ret;
Al Viro40212d532016-11-20 20:24:41 -050061 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +020062 struct inode *inode = file_inode(file);
Eric Parisfb1cfb82010-05-12 11:42:29 -040063 __u32 fsnotify_mask = 0;
Eric Parisc4ec54b2009-12-17 21:24:34 -050064
65 if (file->f_mode & FMODE_NONOTIFY)
66 return 0;
67 if (!(mask & (MAY_READ | MAY_OPEN)))
68 return 0;
Matthew Bobrowski66917a32018-11-08 14:12:44 +110069 if (mask & MAY_OPEN) {
Eric Parisc4ec54b2009-12-17 21:24:34 -050070 fsnotify_mask = FS_OPEN_PERM;
Matthew Bobrowski66917a32018-11-08 14:12:44 +110071
72 if (file->f_flags & __FMODE_EXEC) {
73 ret = fsnotify_path(inode, path, FS_OPEN_EXEC_PERM);
74
75 if (ret)
76 return ret;
77 }
78 } else if (mask & MAY_READ) {
Eric Parisfb1cfb82010-05-12 11:42:29 -040079 fsnotify_mask = FS_ACCESS_PERM;
Matthew Bobrowski66917a32018-11-08 14:12:44 +110080 }
Eric Parisc4ec54b2009-12-17 21:24:34 -050081
Matthew Bobrowskia704bba2018-11-08 14:10:03 +110082 return fsnotify_path(inode, path, fsnotify_mask);
Eric Parisc4ec54b2009-12-17 21:24:34 -050083}
84
Nick Pigginc32ccd82006-03-25 03:07:09 -080085/*
Eric Paris90586522009-05-21 17:01:20 -040086 * fsnotify_link_count - inode's link count changed
87 */
88static inline void fsnotify_link_count(struct inode *inode)
89{
Amir Goldstein0a20df72019-01-10 19:04:40 +020090 __u32 mask = FS_ATTRIB;
91
92 if (S_ISDIR(inode->i_mode))
93 mask |= FS_ISDIR;
94
95 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040096}
97
98/*
Robert Love0eeca282005-07-12 17:06:03 -040099 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
100 */
101static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Eric Paris59b0df22010-02-08 12:53:52 -0500102 const unsigned char *old_name,
Amir Goldstein0a20df72019-01-10 19:04:40 +0200103 int isdir, struct inode *target,
104 struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -0400105{
Al Viro5a190ae2007-06-07 12:19:32 -0400106 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -0400107 u32 fs_cookie = fsnotify_get_cookie();
Amir Goldstein5f02a872019-01-10 19:04:28 +0200108 __u32 old_dir_mask = FS_MOVED_FROM;
109 __u32 new_dir_mask = FS_MOVED_TO;
Amir Goldstein0a20df72019-01-10 19:04:40 +0200110 __u32 mask = FS_MOVE_SELF;
Eric Paris59b0df22010-02-08 12:53:52 -0500111 const unsigned char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -0400112
Eric Parisff52cc22009-06-11 11:09:47 -0400113 if (old_dir == new_dir)
114 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -0400115
Eric Paris90586522009-05-21 17:01:20 -0400116 if (isdir) {
Eric Parisb29866a2010-10-28 17:21:58 -0400117 old_dir_mask |= FS_ISDIR;
118 new_dir_mask |= FS_ISDIR;
Amir Goldstein0a20df72019-01-10 19:04:40 +0200119 mask |= FS_ISDIR;
Eric Paris90586522009-05-21 17:01:20 -0400120 }
121
Jan Kara6ee8e252015-02-10 14:08:32 -0800122 fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
123 fs_cookie);
124 fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
125 fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -0400126
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500127 if (target)
Eric Paris90586522009-05-21 17:01:20 -0400128 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -0400129
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500130 if (source)
Amir Goldstein0a20df72019-01-10 19:04:40 +0200131 fsnotify(source, mask, source, FSNOTIFY_EVENT_INODE, NULL, 0);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400132 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
Robert Love0eeca282005-07-12 17:06:03 -0400133}
134
135/*
Eric Paris3be25f42009-05-21 17:01:26 -0400136 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
137 */
138static inline void fsnotify_inode_delete(struct inode *inode)
139{
140 __fsnotify_inode_delete(inode);
141}
142
143/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500144 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
145 */
146static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
147{
148 __fsnotify_vfsmount_delete(mnt);
149}
150
151/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400152 * fsnotify_nameremove - a filename was removed from a directory
Amir Goldstein5f02a872019-01-10 19:04:28 +0200153 *
154 * This is mostly called under parent vfs inode lock so name and
155 * dentry->d_parent should be stable. However there are some corner cases where
156 * inode lock is not held. So to be on the safe side and be reselient to future
157 * callers and out of tree users of d_delete(), we do not assume that d_parent
158 * and d_name are stable and we use dget_parent() and
159 * take_dentry_name_snapshot() to grab stable references.
John McCutchan7a91bf72005-08-08 13:52:16 -0400160 */
161static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
162{
Amir Goldstein5f02a872019-01-10 19:04:28 +0200163 struct dentry *parent;
164 struct name_snapshot name;
Eric Paris90586522009-05-21 17:01:20 -0400165 __u32 mask = FS_DELETE;
166
Amir Goldstein5f02a872019-01-10 19:04:28 +0200167 /* d_delete() of pseudo inode? (e.g. __ns_get_path() playing tricks) */
168 if (IS_ROOT(dentry))
169 return;
170
John McCutchan7a91bf72005-08-08 13:52:16 -0400171 if (isdir)
Eric Parisb29866a2010-10-28 17:21:58 -0400172 mask |= FS_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400173
Amir Goldstein5f02a872019-01-10 19:04:28 +0200174 parent = dget_parent(dentry);
175 take_dentry_name_snapshot(&name, dentry);
176
177 fsnotify(d_inode(parent), mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
178 name.name, 0);
179
180 release_dentry_name_snapshot(&name);
181 dput(parent);
John McCutchan7a91bf72005-08-08 13:52:16 -0400182}
183
184/*
185 * fsnotify_inoderemove - an inode is going away
186 */
187static inline void fsnotify_inoderemove(struct inode *inode)
188{
Amir Goldstein0a20df72019-01-10 19:04:40 +0200189 __u32 mask = FS_DELETE_SELF;
190
191 if (S_ISDIR(inode->i_mode))
192 mask |= FS_ISDIR;
193
194 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400195 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800196}
197
198/*
Robert Love0eeca282005-07-12 17:06:03 -0400199 * fsnotify_create - 'name' was linked in
200 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000201static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400202{
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400203 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400204
Amir Goldstein5f02a872019-01-10 19:04:28 +0200205 fsnotify_dirent(inode, dentry, FS_CREATE);
Robert Love0eeca282005-07-12 17:06:03 -0400206}
207
208/*
Jan Karaece95912008-02-06 01:37:13 -0800209 * fsnotify_link - new hardlink in 'inode' directory
210 * Note: We have to pass also the linked inode ptr as some filesystems leave
211 * new_dentry->d_inode NULL and instantiate inode pointer later
212 */
213static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
214{
Jan Karaece95912008-02-06 01:37:13 -0800215 fsnotify_link_count(inode);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400216 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400217
Eric Paris47882c62009-05-21 17:01:47 -0400218 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800219}
220
221/*
Robert Love0eeca282005-07-12 17:06:03 -0400222 * fsnotify_mkdir - directory 'name' was created
223 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000224static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400225{
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400226 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400227
Amir Goldstein5f02a872019-01-10 19:04:28 +0200228 fsnotify_dirent(inode, dentry, FS_CREATE | FS_ISDIR);
Robert Love0eeca282005-07-12 17:06:03 -0400229}
230
231/*
232 * fsnotify_access - file was read
233 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500234static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400235{
Al Viro40212d532016-11-20 20:24:41 -0500236 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200237 struct inode *inode = file_inode(file);
Eric Paris90586522009-05-21 17:01:20 -0400238 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400239
240 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400241 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400242
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100243 if (!(file->f_mode & FMODE_NONOTIFY))
244 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400245}
246
247/*
248 * fsnotify_modify - file was modified
249 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500250static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400251{
Al Viro40212d532016-11-20 20:24:41 -0500252 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200253 struct inode *inode = file_inode(file);
Eric Paris90586522009-05-21 17:01:20 -0400254 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400255
256 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400257 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400258
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100259 if (!(file->f_mode & FMODE_NONOTIFY))
260 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400261}
262
263/*
264 * fsnotify_open - file was opened
265 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500266static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400267{
Al Viro40212d532016-11-20 20:24:41 -0500268 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200269 struct inode *inode = file_inode(file);
Eric Paris90586522009-05-21 17:01:20 -0400270 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400271
272 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400273 mask |= FS_ISDIR;
Matthew Bobrowski9b076f12018-11-08 14:07:14 +1100274 if (file->f_flags & __FMODE_EXEC)
275 mask |= FS_OPEN_EXEC;
Robert Love0eeca282005-07-12 17:06:03 -0400276
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100277 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400278}
279
280/*
281 * fsnotify_close - file was closed
282 */
283static inline void fsnotify_close(struct file *file)
284{
Al Viro40212d532016-11-20 20:24:41 -0500285 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200286 struct inode *inode = file_inode(file);
Al Viroaeb5d722008-09-02 15:28:45 -0400287 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400288 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400289
290 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400291 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400292
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100293 if (!(file->f_mode & FMODE_NONOTIFY))
294 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400295}
296
297/*
298 * fsnotify_xattr - extended attributes were changed
299 */
300static inline void fsnotify_xattr(struct dentry *dentry)
301{
302 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400303 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400304
305 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400306 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400307
Eric Paris28c60e32009-12-17 21:24:21 -0500308 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400309 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400310}
311
312/*
313 * fsnotify_change - notify_change event. file was modified and/or metadata
314 * was changed.
315 */
316static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
317{
318 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400319 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400320
Eric Paris3c5119c2009-05-21 17:01:33 -0400321 if (ia_valid & ATTR_UID)
322 mask |= FS_ATTRIB;
323 if (ia_valid & ATTR_GID)
324 mask |= FS_ATTRIB;
325 if (ia_valid & ATTR_SIZE)
326 mask |= FS_MODIFY;
327
Robert Love0eeca282005-07-12 17:06:03 -0400328 /* both times implies a utime(s) call */
329 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400330 mask |= FS_ATTRIB;
331 else if (ia_valid & ATTR_ATIME)
332 mask |= FS_ACCESS;
333 else if (ia_valid & ATTR_MTIME)
334 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400335
Eric Paris3c5119c2009-05-21 17:01:33 -0400336 if (ia_valid & ATTR_MODE)
337 mask |= FS_ATTRIB;
338
339 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400340 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400341 mask |= FS_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500342
343 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400344 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400345 }
346}
347
Robert Love0eeca282005-07-12 17:06:03 -0400348#endif /* _LINUX_FS_NOTIFY_H */