blob: 94972e8eb6d1e018f5005e43fdd8d71ac76d59e7 [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,
Al Viro25b229d2019-04-26 13:37:25 -040030 &dentry->d_name, 0);
Amir Goldstein5f02a872019-01-10 19:04:28 +020031}
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
Amir Goldstein0321e032019-01-10 19:04:41 +020082 if (S_ISDIR(inode->i_mode))
83 fsnotify_mask |= FS_ISDIR;
84
Matthew Bobrowskia704bba2018-11-08 14:10:03 +110085 return fsnotify_path(inode, path, fsnotify_mask);
Eric Parisc4ec54b2009-12-17 21:24:34 -050086}
87
Nick Pigginc32ccd82006-03-25 03:07:09 -080088/*
Eric Paris90586522009-05-21 17:01:20 -040089 * fsnotify_link_count - inode's link count changed
90 */
91static inline void fsnotify_link_count(struct inode *inode)
92{
Amir Goldstein0a20df72019-01-10 19:04:40 +020093 __u32 mask = FS_ATTRIB;
94
95 if (S_ISDIR(inode->i_mode))
96 mask |= FS_ISDIR;
97
98 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040099}
100
101/*
Robert Love0eeca282005-07-12 17:06:03 -0400102 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
103 */
104static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Al Virof4ec3a32019-04-26 13:21:24 -0400105 const struct qstr *old_name,
Amir Goldstein0a20df72019-01-10 19:04:40 +0200106 int isdir, struct inode *target,
107 struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -0400108{
Al Viro5a190ae2007-06-07 12:19:32 -0400109 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -0400110 u32 fs_cookie = fsnotify_get_cookie();
Amir Goldstein5f02a872019-01-10 19:04:28 +0200111 __u32 old_dir_mask = FS_MOVED_FROM;
112 __u32 new_dir_mask = FS_MOVED_TO;
Amir Goldstein0a20df72019-01-10 19:04:40 +0200113 __u32 mask = FS_MOVE_SELF;
Al Viro25b229d2019-04-26 13:37:25 -0400114 const struct qstr *new_name = &moved->d_name;
Robert Love0eeca282005-07-12 17:06:03 -0400115
Eric Parisff52cc22009-06-11 11:09:47 -0400116 if (old_dir == new_dir)
117 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -0400118
Eric Paris90586522009-05-21 17:01:20 -0400119 if (isdir) {
Eric Parisb29866a2010-10-28 17:21:58 -0400120 old_dir_mask |= FS_ISDIR;
121 new_dir_mask |= FS_ISDIR;
Amir Goldstein0a20df72019-01-10 19:04:40 +0200122 mask |= FS_ISDIR;
Eric Paris90586522009-05-21 17:01:20 -0400123 }
124
Al Viro25b229d2019-04-26 13:37:25 -0400125 fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
Jan Kara6ee8e252015-02-10 14:08:32 -0800126 fs_cookie);
127 fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
128 fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -0400129
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500130 if (target)
Eric Paris90586522009-05-21 17:01:20 -0400131 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -0400132
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500133 if (source)
Amir Goldstein0a20df72019-01-10 19:04:40 +0200134 fsnotify(source, mask, source, FSNOTIFY_EVENT_INODE, NULL, 0);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400135 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
Robert Love0eeca282005-07-12 17:06:03 -0400136}
137
138/*
Eric Paris3be25f42009-05-21 17:01:26 -0400139 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
140 */
141static inline void fsnotify_inode_delete(struct inode *inode)
142{
143 __fsnotify_inode_delete(inode);
144}
145
146/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500147 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
148 */
149static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
150{
151 __fsnotify_vfsmount_delete(mnt);
152}
153
154/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400155 * fsnotify_inoderemove - an inode is going away
156 */
157static inline void fsnotify_inoderemove(struct inode *inode)
158{
Amir Goldstein0a20df72019-01-10 19:04:40 +0200159 __u32 mask = FS_DELETE_SELF;
160
161 if (S_ISDIR(inode->i_mode))
162 mask |= FS_ISDIR;
163
164 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400165 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800166}
167
168/*
Robert Love0eeca282005-07-12 17:06:03 -0400169 * fsnotify_create - 'name' was linked in
170 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000171static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400172{
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400173 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400174
Amir Goldstein5f02a872019-01-10 19:04:28 +0200175 fsnotify_dirent(inode, dentry, FS_CREATE);
Robert Love0eeca282005-07-12 17:06:03 -0400176}
177
178/*
Jan Karaece95912008-02-06 01:37:13 -0800179 * fsnotify_link - new hardlink in 'inode' directory
180 * Note: We have to pass also the linked inode ptr as some filesystems leave
181 * new_dentry->d_inode NULL and instantiate inode pointer later
182 */
183static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
184{
Jan Karaece95912008-02-06 01:37:13 -0800185 fsnotify_link_count(inode);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400186 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400187
Al Viro25b229d2019-04-26 13:37:25 -0400188 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, &new_dentry->d_name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800189}
190
191/*
Robert Love0eeca282005-07-12 17:06:03 -0400192 * fsnotify_mkdir - directory 'name' was created
193 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000194static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400195{
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400196 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400197
Amir Goldstein5f02a872019-01-10 19:04:28 +0200198 fsnotify_dirent(inode, dentry, FS_CREATE | FS_ISDIR);
Robert Love0eeca282005-07-12 17:06:03 -0400199}
200
201/*
202 * fsnotify_access - file was read
203 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500204static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400205{
Al Viro40212d532016-11-20 20:24:41 -0500206 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200207 struct inode *inode = file_inode(file);
Eric Paris90586522009-05-21 17:01:20 -0400208 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400209
210 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400211 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400212
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100213 if (!(file->f_mode & FMODE_NONOTIFY))
214 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400215}
216
217/*
218 * fsnotify_modify - file was modified
219 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500220static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400221{
Al Viro40212d532016-11-20 20:24:41 -0500222 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200223 struct inode *inode = file_inode(file);
Eric Paris90586522009-05-21 17:01:20 -0400224 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400225
226 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400227 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400228
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100229 if (!(file->f_mode & FMODE_NONOTIFY))
230 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400231}
232
233/*
234 * fsnotify_open - file was opened
235 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500236static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400237{
Al Viro40212d532016-11-20 20:24:41 -0500238 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200239 struct inode *inode = file_inode(file);
Eric Paris90586522009-05-21 17:01:20 -0400240 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400241
242 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400243 mask |= FS_ISDIR;
Matthew Bobrowski9b076f12018-11-08 14:07:14 +1100244 if (file->f_flags & __FMODE_EXEC)
245 mask |= FS_OPEN_EXEC;
Robert Love0eeca282005-07-12 17:06:03 -0400246
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100247 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400248}
249
250/*
251 * fsnotify_close - file was closed
252 */
253static inline void fsnotify_close(struct file *file)
254{
Al Viro40212d532016-11-20 20:24:41 -0500255 const struct path *path = &file->f_path;
Miklos Szeredi573e1782018-07-18 15:44:44 +0200256 struct inode *inode = file_inode(file);
Al Viroaeb5d722008-09-02 15:28:45 -0400257 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400258 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400259
260 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400261 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400262
Matthew Bobrowskia704bba2018-11-08 14:10:03 +1100263 if (!(file->f_mode & FMODE_NONOTIFY))
264 fsnotify_path(inode, path, mask);
Robert Love0eeca282005-07-12 17:06:03 -0400265}
266
267/*
268 * fsnotify_xattr - extended attributes were changed
269 */
270static inline void fsnotify_xattr(struct dentry *dentry)
271{
272 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400273 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400274
275 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400276 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400277
Eric Paris28c60e32009-12-17 21:24:21 -0500278 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400279 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400280}
281
282/*
283 * fsnotify_change - notify_change event. file was modified and/or metadata
284 * was changed.
285 */
286static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
287{
288 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400289 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400290
Eric Paris3c5119c2009-05-21 17:01:33 -0400291 if (ia_valid & ATTR_UID)
292 mask |= FS_ATTRIB;
293 if (ia_valid & ATTR_GID)
294 mask |= FS_ATTRIB;
295 if (ia_valid & ATTR_SIZE)
296 mask |= FS_MODIFY;
297
Robert Love0eeca282005-07-12 17:06:03 -0400298 /* both times implies a utime(s) call */
299 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400300 mask |= FS_ATTRIB;
301 else if (ia_valid & ATTR_ATIME)
302 mask |= FS_ACCESS;
303 else if (ia_valid & ATTR_MTIME)
304 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400305
Eric Paris3c5119c2009-05-21 17:01:33 -0400306 if (ia_valid & ATTR_MODE)
307 mask |= FS_ATTRIB;
308
309 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400310 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400311 mask |= FS_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500312
313 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400314 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400315 }
316}
317
Robert Love0eeca282005-07-12 17:06:03 -0400318#endif /* _LINUX_FS_NOTIFY_H */