blob: ff51592c4792b45e9beb56fa369f1e7a40c319d4 [file] [log] [blame]
Robert Love0eeca282005-07-12 17:06:03 -04001#ifndef _LINUX_FS_NOTIFY_H
2#define _LINUX_FS_NOTIFY_H
3
4/*
5 * include/linux/fsnotify.h - generic hooks for filesystem notification, to
6 * reduce in-source duplication from both dnotify and inotify.
7 *
8 * We don't compile any of this away in some complicated menagerie of ifdefs.
9 * Instead, we rely on the code inside to optimize away as needed.
10 *
11 * (C) Copyright 2005 Robert Love
12 */
13
Eric Paris90586522009-05-21 17:01:20 -040014#include <linux/fsnotify_backend.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000015#include <linux/audit.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050017#include <linux/bug.h>
Robert Love0eeca282005-07-12 17:06:03 -040018
Eric Parisc28f7e52009-05-21 17:01:29 -040019/* Notify this dentry's parent about a child's events. */
Eric Paris52420392010-10-28 17:21:56 -040020static inline int fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
Eric Parisc28f7e52009-05-21 17:01:29 -040021{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050022 if (!dentry)
Linus Torvalds20696012010-08-12 14:23:04 -070023 dentry = path->dentry;
Eric Paris28c60e32009-12-17 21:24:21 -050024
Eric Paris52420392010-10-28 17:21:56 -040025 return __fsnotify_parent(path, dentry, mask);
Eric Parisc28f7e52009-05-21 17:01:29 -040026}
27
Eric Parisc4ec54b2009-12-17 21:24:34 -050028/* simple call site for access decisions */
29static inline int fsnotify_perm(struct file *file, int mask)
30{
Linus Torvalds20696012010-08-12 14:23:04 -070031 struct path *path = &file->f_path;
Aihua Zhangf3fbbb02016-07-07 15:37:53 +080032 /*
33 * Do not use file_inode() here or anywhere in this file to get the
34 * inode. That would break *notity on overlayfs.
35 */
36 struct inode *inode = path->dentry->d_inode;
Eric Parisfb1cfb82010-05-12 11:42:29 -040037 __u32 fsnotify_mask = 0;
Eric Paris52420392010-10-28 17:21:56 -040038 int ret;
Eric Parisc4ec54b2009-12-17 21:24:34 -050039
40 if (file->f_mode & FMODE_NONOTIFY)
41 return 0;
42 if (!(mask & (MAY_READ | MAY_OPEN)))
43 return 0;
Eric Parisc4ec54b2009-12-17 21:24:34 -050044 if (mask & MAY_OPEN)
45 fsnotify_mask = FS_OPEN_PERM;
Eric Parisfb1cfb82010-05-12 11:42:29 -040046 else if (mask & MAY_READ)
47 fsnotify_mask = FS_ACCESS_PERM;
48 else
49 BUG();
Eric Parisc4ec54b2009-12-17 21:24:34 -050050
Eric Paris52420392010-10-28 17:21:56 -040051 ret = fsnotify_parent(path, NULL, fsnotify_mask);
52 if (ret)
53 return ret;
54
Linus Torvalds20696012010-08-12 14:23:04 -070055 return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisc4ec54b2009-12-17 21:24:34 -050056}
57
Nick Pigginc32ccd82006-03-25 03:07:09 -080058/*
Eric Paris90586522009-05-21 17:01:20 -040059 * fsnotify_link_count - inode's link count changed
60 */
61static inline void fsnotify_link_count(struct inode *inode)
62{
Eric Paris47882c62009-05-21 17:01:47 -040063 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040064}
65
66/*
Robert Love0eeca282005-07-12 17:06:03 -040067 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
68 */
69static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Eric Paris59b0df22010-02-08 12:53:52 -050070 const unsigned char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040071 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040072{
Al Viro5a190ae2007-06-07 12:19:32 -040073 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040074 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040075 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
76 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Eric Paris59b0df22010-02-08 12:53:52 -050077 const unsigned char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040078
Eric Parisff52cc22009-06-11 11:09:47 -040079 if (old_dir == new_dir)
80 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040081
Eric Paris90586522009-05-21 17:01:20 -040082 if (isdir) {
Eric Parisb29866a2010-10-28 17:21:58 -040083 old_dir_mask |= FS_ISDIR;
84 new_dir_mask |= FS_ISDIR;
Eric Paris90586522009-05-21 17:01:20 -040085 }
86
Jan Kara6ee8e252015-02-10 14:08:32 -080087 fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
88 fs_cookie);
89 fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
90 fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -040091
Eric Paris2dfc1ca2009-12-17 20:30:52 -050092 if (target)
Eric Paris90586522009-05-21 17:01:20 -040093 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -040094
Eric Paris2dfc1ca2009-12-17 20:30:52 -050095 if (source)
Eric Paris47882c62009-05-21 17:01:47 -040096 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -040097 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
Robert Love0eeca282005-07-12 17:06:03 -040098}
99
100/*
Eric Paris3be25f42009-05-21 17:01:26 -0400101 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
102 */
103static inline void fsnotify_inode_delete(struct inode *inode)
104{
105 __fsnotify_inode_delete(inode);
106}
107
108/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500109 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
110 */
111static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
112{
113 __fsnotify_vfsmount_delete(mnt);
114}
115
116/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400117 * fsnotify_nameremove - a filename was removed from a directory
118 */
119static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
120{
Eric Paris90586522009-05-21 17:01:20 -0400121 __u32 mask = FS_DELETE;
122
John McCutchan7a91bf72005-08-08 13:52:16 -0400123 if (isdir)
Eric Parisb29866a2010-10-28 17:21:58 -0400124 mask |= FS_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400125
Eric Paris28c60e32009-12-17 21:24:21 -0500126 fsnotify_parent(NULL, dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400127}
128
129/*
130 * fsnotify_inoderemove - an inode is going away
131 */
132static inline void fsnotify_inoderemove(struct inode *inode)
133{
Eric Paris47882c62009-05-21 17:01:47 -0400134 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400135 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800136}
137
138/*
Robert Love0eeca282005-07-12 17:06:03 -0400139 * fsnotify_create - 'name' was linked in
140 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000141static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400142{
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400143 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400144
Eric Paris47882c62009-05-21 17:01:47 -0400145 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400146}
147
148/*
Jan Karaece95912008-02-06 01:37:13 -0800149 * fsnotify_link - new hardlink in 'inode' directory
150 * Note: We have to pass also the linked inode ptr as some filesystems leave
151 * new_dentry->d_inode NULL and instantiate inode pointer later
152 */
153static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
154{
Jan Karaece95912008-02-06 01:37:13 -0800155 fsnotify_link_count(inode);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400156 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400157
Eric Paris47882c62009-05-21 17:01:47 -0400158 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800159}
160
161/*
Robert Love0eeca282005-07-12 17:06:03 -0400162 * fsnotify_mkdir - directory 'name' was created
163 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000164static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400165{
Eric Parisb29866a2010-10-28 17:21:58 -0400166 __u32 mask = (FS_CREATE | FS_ISDIR);
Eric Paris90586522009-05-21 17:01:20 -0400167 struct inode *d_inode = dentry->d_inode;
168
Jeff Layton4fa6b5e2012-10-10 15:25:25 -0400169 audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400170
Eric Paris47882c62009-05-21 17:01:47 -0400171 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400172}
173
174/*
175 * fsnotify_access - file was read
176 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500177static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400178{
Linus Torvalds20696012010-08-12 14:23:04 -0700179 struct path *path = &file->f_path;
Aihua Zhangf3fbbb02016-07-07 15:37:53 +0800180 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400181 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400182
183 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400184 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400185
Eric Parisecf081d2009-12-17 21:24:25 -0500186 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700187 fsnotify_parent(path, NULL, mask);
188 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500189 }
Robert Love0eeca282005-07-12 17:06:03 -0400190}
191
192/*
193 * fsnotify_modify - file was modified
194 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500195static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400196{
Linus Torvalds20696012010-08-12 14:23:04 -0700197 struct path *path = &file->f_path;
Aihua Zhangf3fbbb02016-07-07 15:37:53 +0800198 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400199 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400200
201 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400202 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400203
Eric Parisecf081d2009-12-17 21:24:25 -0500204 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700205 fsnotify_parent(path, NULL, mask);
206 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500207 }
Robert Love0eeca282005-07-12 17:06:03 -0400208}
209
210/*
211 * fsnotify_open - file was opened
212 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500213static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400214{
Linus Torvalds20696012010-08-12 14:23:04 -0700215 struct path *path = &file->f_path;
Daniel Rosenberg3d7d3cd2018-01-23 15:02:50 -0800216 struct path lower_path;
Aihua Zhangf3fbbb02016-07-07 15:37:53 +0800217 struct inode *inode = path->dentry->d_inode;
Daniel Rosenberg3d7d3cd2018-01-23 15:02:50 -0800218
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;
Robert Love0eeca282005-07-12 17:06:03 -0400223
Daniel Rosenberg3d7d3cd2018-01-23 15:02:50 -0800224 if (path->dentry->d_op && path->dentry->d_op->d_canonical_path) {
225 path->dentry->d_op->d_canonical_path(path, &lower_path);
226 fsnotify_parent(&lower_path, NULL, mask);
227 fsnotify(lower_path.dentry->d_inode, mask, &lower_path, FSNOTIFY_EVENT_PATH, NULL, 0);
228 path_put(&lower_path);
229 }
Lino Sanfilippo6bff7ec2010-10-29 12:02:17 +0200230 fsnotify_parent(path, NULL, mask);
231 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400232}
233
234/*
235 * fsnotify_close - file was closed
236 */
237static inline void fsnotify_close(struct file *file)
238{
Linus Torvalds20696012010-08-12 14:23:04 -0700239 struct path *path = &file->f_path;
Aihua Zhangf3fbbb02016-07-07 15:37:53 +0800240 struct inode *inode = path->dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400241 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400242 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400243
244 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400245 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400246
Eric Parisecf081d2009-12-17 21:24:25 -0500247 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700248 fsnotify_parent(path, NULL, mask);
249 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500250 }
Robert Love0eeca282005-07-12 17:06:03 -0400251}
252
253/*
254 * fsnotify_xattr - extended attributes were changed
255 */
256static inline void fsnotify_xattr(struct dentry *dentry)
257{
258 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400259 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400260
261 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400262 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400263
Eric Paris28c60e32009-12-17 21:24:21 -0500264 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400265 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400266}
267
268/*
269 * fsnotify_change - notify_change event. file was modified and/or metadata
270 * was changed.
271 */
272static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
273{
274 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400275 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400276
Eric Paris3c5119c2009-05-21 17:01:33 -0400277 if (ia_valid & ATTR_UID)
278 mask |= FS_ATTRIB;
279 if (ia_valid & ATTR_GID)
280 mask |= FS_ATTRIB;
281 if (ia_valid & ATTR_SIZE)
282 mask |= FS_MODIFY;
283
Robert Love0eeca282005-07-12 17:06:03 -0400284 /* both times implies a utime(s) call */
285 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400286 mask |= FS_ATTRIB;
287 else if (ia_valid & ATTR_ATIME)
288 mask |= FS_ACCESS;
289 else if (ia_valid & ATTR_MTIME)
290 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400291
Eric Paris3c5119c2009-05-21 17:01:33 -0400292 if (ia_valid & ATTR_MODE)
293 mask |= FS_ATTRIB;
294
295 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400296 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400297 mask |= FS_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500298
299 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400300 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400301 }
302}
303
Robert Love0eeca282005-07-12 17:06:03 -0400304#endif /* _LINUX_FS_NOTIFY_H */