blob: c29f2f072c2ceb8fad1580670447c18e02d7e38a [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
Eric Parisc28f7e52009-05-21 17:01:29 -040020/* Notify this dentry's parent about a child's events. */
Al Viro12c7f9d2016-11-20 20:23:04 -050021static inline int fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask)
Eric Parisc28f7e52009-05-21 17:01:29 -040022{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050023 if (!dentry)
Linus Torvalds20696012010-08-12 14:23:04 -070024 dentry = path->dentry;
Eric Paris28c60e32009-12-17 21:24:21 -050025
Eric Paris52420392010-10-28 17:21:56 -040026 return __fsnotify_parent(path, dentry, mask);
Eric Parisc28f7e52009-05-21 17:01:29 -040027}
28
Matthew Bobrowskia704bba2018-11-08 14:10:03 +110029/*
30 * Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when
31 * an event is on a path.
32 */
33static inline int fsnotify_path(struct inode *inode, const struct path *path,
34 __u32 mask)
35{
36 int ret = fsnotify_parent(path, NULL, mask);
37
38 if (ret)
39 return ret;
40 return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
41}
42
Eric Parisc4ec54b2009-12-17 21:24:34 -050043/* simple call site for access decisions */
44static inline int fsnotify_perm(struct file *file, int mask)
45{
Al Viro40212d532016-11-20 20:24:41 -050046 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +020047 struct inode *inode = file_inode(file);
Eric Parisfb1cfb82010-05-12 11:42:29 -040048 __u32 fsnotify_mask = 0;
Eric Parisc4ec54b2009-12-17 21:24:34 -050049
50 if (file->f_mode & FMODE_NONOTIFY)
51 return 0;
52 if (!(mask & (MAY_READ | MAY_OPEN)))
53 return 0;
Eric Parisc4ec54b2009-12-17 21:24:34 -050054 if (mask & MAY_OPEN)
55 fsnotify_mask = FS_OPEN_PERM;
Eric Parisfb1cfb82010-05-12 11:42:29 -040056 else if (mask & MAY_READ)
57 fsnotify_mask = FS_ACCESS_PERM;
58 else
59 BUG();
Eric Parisc4ec54b2009-12-17 21:24:34 -050060
Matthew Bobrowskia704bba2018-11-08 14:10:03 +110061 return fsnotify_path(inode, path, fsnotify_mask);
Eric Parisc4ec54b2009-12-17 21:24:34 -050062}
63
Nick Pigginc32ccd82006-03-25 03:07:09 -080064/*
Eric Paris90586522009-05-21 17:01:20 -040065 * fsnotify_link_count - inode's link count changed
66 */
67static inline void fsnotify_link_count(struct inode *inode)
68{
Eric Paris47882c62009-05-21 17:01:47 -040069 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040070}
71
72/*
Robert Love0eeca282005-07-12 17:06:03 -040073 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
74 */
75static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Eric Paris59b0df22010-02-08 12:53:52 -050076 const unsigned char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040077 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040078{
Al Viro5a190ae2007-06-07 12:19:32 -040079 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040080 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040081 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
82 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Eric Paris59b0df22010-02-08 12:53:52 -050083 const unsigned char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040084
Eric Parisff52cc22009-06-11 11:09:47 -040085 if (old_dir == new_dir)
86 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040087
Eric Paris90586522009-05-21 17:01:20 -040088 if (isdir) {
Eric Parisb29866a2010-10-28 17:21:58 -040089 old_dir_mask |= FS_ISDIR;
90 new_dir_mask |= FS_ISDIR;
Eric Paris90586522009-05-21 17:01:20 -040091 }
92
Jan Kara6ee8e252015-02-10 14:08:32 -080093 fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
94 fs_cookie);
95 fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
96 fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -040097
Eric Paris2dfc1ca2009-12-17 20:30:52 -050098 if (target)
Eric Paris90586522009-05-21 17:01:20 -040099 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -0400100
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500101 if (source)
Eric Paris47882c62009-05-21 17:01:47 -0400102 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400103 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
Robert Love0eeca282005-07-12 17:06:03 -0400104}
105
106/*
Eric Paris3be25f42009-05-21 17:01:26 -0400107 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
108 */
109static inline void fsnotify_inode_delete(struct inode *inode)
110{
111 __fsnotify_inode_delete(inode);
112}
113
114/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500115 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
116 */
117static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
118{
119 __fsnotify_vfsmount_delete(mnt);
120}
121
122/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400123 * fsnotify_nameremove - a filename was removed from a directory
124 */
125static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
126{
Eric Paris90586522009-05-21 17:01:20 -0400127 __u32 mask = FS_DELETE;
128
John McCutchan7a91bf72005-08-08 13:52:16 -0400129 if (isdir)
Eric Parisb29866a2010-10-28 17:21:58 -0400130 mask |= FS_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400131
Eric Paris28c60e32009-12-17 21:24:21 -0500132 fsnotify_parent(NULL, dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400133}
134
135/*
136 * fsnotify_inoderemove - an inode is going away
137 */
138static inline void fsnotify_inoderemove(struct inode *inode)
139{
Eric Paris47882c62009-05-21 17:01:47 -0400140 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400141 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800142}
143
144/*
Robert Love0eeca282005-07-12 17:06:03 -0400145 * fsnotify_create - 'name' was linked in
146 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000147static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400148{
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400149 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400150
Eric Paris47882c62009-05-21 17:01:47 -0400151 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400152}
153
154/*
Jan Karaece95912008-02-06 01:37:13 -0800155 * fsnotify_link - new hardlink in 'inode' directory
156 * Note: We have to pass also the linked inode ptr as some filesystems leave
157 * new_dentry->d_inode NULL and instantiate inode pointer later
158 */
159static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
160{
Jan Karaece95912008-02-06 01:37:13 -0800161 fsnotify_link_count(inode);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400162 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400163
Eric Paris47882c62009-05-21 17:01:47 -0400164 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800165}
166
167/*
Robert Love0eeca282005-07-12 17:06:03 -0400168 * fsnotify_mkdir - directory 'name' was created
169 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000170static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400171{
Eric Parisb29866a2010-10-28 17:21:58 -0400172 __u32 mask = (FS_CREATE | FS_ISDIR);
Eric Paris90586522009-05-21 17:01:20 -0400173 struct inode *d_inode = dentry->d_inode;
174
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400175 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400176
Eric Paris47882c62009-05-21 17:01:47 -0400177 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400178}
179
180/*
181 * fsnotify_access - file was read
182 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500183static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400184{
Al Viro40212d532016-11-20 20:24:41 -0500185 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200186 struct inode *inode = file_inode(file);
Eric Paris90586522009-05-21 17:01:20 -0400187 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400188
189 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400190 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400191
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100192 if (!(file->f_mode & FMODE_NONOTIFY))
193 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400194}
195
196/*
197 * fsnotify_modify - file was modified
198 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500199static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400200{
Al Viro40212d532016-11-20 20:24:41 -0500201 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200202 struct inode *inode = file_inode(file);
Eric Paris90586522009-05-21 17:01:20 -0400203 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400204
205 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400206 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400207
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100208 if (!(file->f_mode & FMODE_NONOTIFY))
209 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400210}
211
212/*
213 * fsnotify_open - file was opened
214 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500215static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400216{
Al Viro40212d532016-11-20 20:24:41 -0500217 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200218 struct inode *inode = file_inode(file);
Eric Paris90586522009-05-21 17:01:20 -0400219 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400220
221 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400222 mask |= FS_ISDIR;
Matthew Bobrowski9b076f12018-11-08 14:07:14 +1100223 if (file->f_flags & __FMODE_EXEC)
224 mask |= FS_OPEN_EXEC;
Robert Love0eeca282005-07-12 17:06:03 -0400225
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100226 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400227}
228
229/*
230 * fsnotify_close - file was closed
231 */
232static inline void fsnotify_close(struct file *file)
233{
Al Viro40212d532016-11-20 20:24:41 -0500234 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200235 struct inode *inode = file_inode(file);
Al Viroaeb5d722008-09-02 15:28:45 -0400236 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400237 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400238
239 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400240 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400241
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100242 if (!(file->f_mode & FMODE_NONOTIFY))
243 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400244}
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;
Eric Paris90586522009-05-21 17:01:20 -0400252 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400253
254 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400255 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400256
Eric Paris28c60e32009-12-17 21:24:21 -0500257 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400258 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400259}
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;
Eric Paris3c5119c2009-05-21 17:01:33 -0400268 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400269
Eric Paris3c5119c2009-05-21 17:01:33 -0400270 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;
276
Robert Love0eeca282005-07-12 17:06:03 -0400277 /* both times implies a utime(s) call */
278 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400279 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;
Robert Love0eeca282005-07-12 17:06:03 -0400284
Eric Paris3c5119c2009-05-21 17:01:33 -0400285 if (ia_valid & ATTR_MODE)
286 mask |= FS_ATTRIB;
287
288 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400289 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400290 mask |= FS_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500291
292 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400293 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400294 }
295}
296
Robert Love0eeca282005-07-12 17:06:03 -0400297#endif /* _LINUX_FS_NOTIFY_H */