blob: bdaf22582f6ea5c59a1eb9e4e7c319854087df42 [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
Eric Parisc4ec54b2009-12-17 21:24:34 -050029/* simple call site for access decisions */
30static inline int fsnotify_perm(struct file *file, int mask)
31{
Al Viro40212d52016-11-20 20:24:41 -050032 const struct path *path = &file->f_path;
Aihua Zhangf3fbbb02016-07-07 15:37:53 +080033 /*
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;
Eric Parisfb1cfb82010-05-12 11:42:29 -040038 __u32 fsnotify_mask = 0;
Eric Paris52420392010-10-28 17:21:56 -040039 int ret;
Eric Parisc4ec54b2009-12-17 21:24:34 -050040
41 if (file->f_mode & FMODE_NONOTIFY)
42 return 0;
43 if (!(mask & (MAY_READ | MAY_OPEN)))
44 return 0;
Eric Parisc4ec54b2009-12-17 21:24:34 -050045 if (mask & MAY_OPEN)
46 fsnotify_mask = FS_OPEN_PERM;
Eric Parisfb1cfb82010-05-12 11:42:29 -040047 else if (mask & MAY_READ)
48 fsnotify_mask = FS_ACCESS_PERM;
49 else
50 BUG();
Eric Parisc4ec54b2009-12-17 21:24:34 -050051
Eric Paris52420392010-10-28 17:21:56 -040052 ret = fsnotify_parent(path, NULL, fsnotify_mask);
53 if (ret)
54 return ret;
55
Linus Torvalds20696012010-08-12 14:23:04 -070056 return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisc4ec54b2009-12-17 21:24:34 -050057}
58
Nick Pigginc32ccd82006-03-25 03:07:09 -080059/*
Eric Paris90586522009-05-21 17:01:20 -040060 * fsnotify_link_count - inode's link count changed
61 */
62static inline void fsnotify_link_count(struct inode *inode)
63{
Eric Paris47882c62009-05-21 17:01:47 -040064 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040065}
66
67/*
Robert Love0eeca282005-07-12 17:06:03 -040068 * 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,
Eric Paris59b0df22010-02-08 12:53:52 -050071 const unsigned char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040072 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040073{
Al Viro5a190ae2007-06-07 12:19:32 -040074 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040075 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040076 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
77 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Eric Paris59b0df22010-02-08 12:53:52 -050078 const unsigned char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040079
Eric Parisff52cc22009-06-11 11:09:47 -040080 if (old_dir == new_dir)
81 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040082
Eric Paris90586522009-05-21 17:01:20 -040083 if (isdir) {
Eric Parisb29866a2010-10-28 17:21:58 -040084 old_dir_mask |= FS_ISDIR;
85 new_dir_mask |= FS_ISDIR;
Eric Paris90586522009-05-21 17:01:20 -040086 }
87
Jan Kara6ee8e252015-02-10 14:08:32 -080088 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);
Eric Paris90586522009-05-21 17:01:20 -040092
Eric Paris2dfc1ca2009-12-17 20:30:52 -050093 if (target)
Eric Paris90586522009-05-21 17:01:20 -040094 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -040095
Eric Paris2dfc1ca2009-12-17 20:30:52 -050096 if (source)
Eric Paris47882c62009-05-21 17:01:47 -040097 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -040098 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
Robert Love0eeca282005-07-12 17:06:03 -040099}
100
101/*
Eric Paris3be25f42009-05-21 17:01:26 -0400102 * 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
109/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500110 * 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
117/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400118 * fsnotify_nameremove - a filename was removed from a directory
119 */
120static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
121{
Eric Paris90586522009-05-21 17:01:20 -0400122 __u32 mask = FS_DELETE;
123
John McCutchan7a91bf72005-08-08 13:52:16 -0400124 if (isdir)
Eric Parisb29866a2010-10-28 17:21:58 -0400125 mask |= FS_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400126
Eric Paris28c60e32009-12-17 21:24:21 -0500127 fsnotify_parent(NULL, dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400128}
129
130/*
131 * fsnotify_inoderemove - an inode is going away
132 */
133static inline void fsnotify_inoderemove(struct inode *inode)
134{
Eric Paris47882c62009-05-21 17:01:47 -0400135 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400136 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800137}
138
139/*
Robert Love0eeca282005-07-12 17:06:03 -0400140 * fsnotify_create - 'name' was linked in
141 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000142static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400143{
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400144 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400145
Eric Paris47882c62009-05-21 17:01:47 -0400146 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400147}
148
149/*
Jan Karaece95912008-02-06 01:37:13 -0800150 * 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{
Jan Karaece95912008-02-06 01:37:13 -0800156 fsnotify_link_count(inode);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400157 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400158
Eric Paris47882c62009-05-21 17:01:47 -0400159 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800160}
161
162/*
Robert Love0eeca282005-07-12 17:06:03 -0400163 * fsnotify_mkdir - directory 'name' was created
164 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000165static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400166{
Eric Parisb29866a2010-10-28 17:21:58 -0400167 __u32 mask = (FS_CREATE | FS_ISDIR);
Eric Paris90586522009-05-21 17:01:20 -0400168 struct inode *d_inode = dentry->d_inode;
169
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400170 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400171
Eric Paris47882c62009-05-21 17:01:47 -0400172 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400173}
174
175/*
176 * fsnotify_access - file was read
177 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500178static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400179{
Al Viro40212d52016-11-20 20:24:41 -0500180 const struct path *path = &file->f_path;
Aihua Zhangf3fbbb02016-07-07 15:37:53 +0800181 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400182 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400183
184 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400185 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400186
Eric Parisecf081d2009-12-17 21:24:25 -0500187 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700188 fsnotify_parent(path, NULL, mask);
189 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500190 }
Robert Love0eeca282005-07-12 17:06:03 -0400191}
192
193/*
194 * fsnotify_modify - file was modified
195 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500196static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400197{
Al Viro40212d52016-11-20 20:24:41 -0500198 const struct path *path = &file->f_path;
Aihua Zhangf3fbbb02016-07-07 15:37:53 +0800199 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400200 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400201
202 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400203 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400204
Eric Parisecf081d2009-12-17 21:24:25 -0500205 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700206 fsnotify_parent(path, NULL, mask);
207 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500208 }
Robert Love0eeca282005-07-12 17:06:03 -0400209}
210
211/*
212 * fsnotify_open - file was opened
213 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500214static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400215{
Al Viro40212d52016-11-20 20:24:41 -0500216 const struct path *path = &file->f_path;
Aihua Zhangf3fbbb02016-07-07 15:37:53 +0800217 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400218 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400219
220 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400221 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400222
Lino Sanfilippo6bff7ec2010-10-29 12:02:17 +0200223 fsnotify_parent(path, NULL, mask);
224 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400225}
226
227/*
228 * fsnotify_close - file was closed
229 */
230static inline void fsnotify_close(struct file *file)
231{
Al Viro40212d52016-11-20 20:24:41 -0500232 const struct path *path = &file->f_path;
Aihua Zhangf3fbbb02016-07-07 15:37:53 +0800233 struct inode *inode = path->dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400234 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400235 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400236
237 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400238 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400239
Eric Parisecf081d2009-12-17 21:24:25 -0500240 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700241 fsnotify_parent(path, NULL, mask);
242 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500243 }
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 */