blob: 06c0e50c7968046347917b7997b5b4406e26b00a [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>
Robert Love0eeca282005-07-12 17:06:03 -040017
18/*
Nick Pigginc32ccd82006-03-25 03:07:09 -080019 * fsnotify_d_instantiate - instantiate a dentry for inode
20 * Called with dcache_lock held.
21 */
Eric Parise61ce862009-12-17 21:24:24 -050022static inline void fsnotify_d_instantiate(struct dentry *dentry,
23 struct inode *inode)
Nick Pigginc32ccd82006-03-25 03:07:09 -080024{
Eric Parise61ce862009-12-17 21:24:24 -050025 __fsnotify_d_instantiate(dentry, inode);
Nick Pigginc32ccd82006-03-25 03:07:09 -080026}
27
Eric Parisc28f7e52009-05-21 17:01:29 -040028/* Notify this dentry's parent about a child's events. */
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050029static inline void fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
Eric Parisc28f7e52009-05-21 17:01:29 -040030{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050031 if (!dentry)
32 dentry = path->dentry;
Eric Paris28c60e32009-12-17 21:24:21 -050033
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050034 __fsnotify_parent(path, dentry, mask);
Eric Parisc28f7e52009-05-21 17:01:29 -040035}
36
Nick Pigginc32ccd82006-03-25 03:07:09 -080037/*
Eric Parise61ce862009-12-17 21:24:24 -050038 * fsnotify_d_move - dentry has been moved
39 * Called with dcache_lock and dentry->d_lock held.
Nick Pigginc32ccd82006-03-25 03:07:09 -080040 */
Eric Parise61ce862009-12-17 21:24:24 -050041static inline void fsnotify_d_move(struct dentry *dentry)
Nick Pigginc32ccd82006-03-25 03:07:09 -080042{
Eric Parisc28f7e52009-05-21 17:01:29 -040043 /*
Eric Parise61ce862009-12-17 21:24:24 -050044 * On move we need to update dentry->d_flags to indicate if the new parent
45 * cares about events from this dentry.
Eric Parisc28f7e52009-05-21 17:01:29 -040046 */
Eric Parise61ce862009-12-17 21:24:24 -050047 __fsnotify_update_dcache_flags(dentry);
Nick Pigginc32ccd82006-03-25 03:07:09 -080048}
49
50/*
Eric Paris90586522009-05-21 17:01:20 -040051 * fsnotify_link_count - inode's link count changed
52 */
53static inline void fsnotify_link_count(struct inode *inode)
54{
Eric Paris47882c62009-05-21 17:01:47 -040055 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040056}
57
58/*
Robert Love0eeca282005-07-12 17:06:03 -040059 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
60 */
61static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Al Viro123df292009-12-25 04:57:57 -050062 const char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040063 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040064{
Al Viro5a190ae2007-06-07 12:19:32 -040065 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040066 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040067 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
68 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Al Viro123df292009-12-25 04:57:57 -050069 const char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040070
Eric Parisff52cc22009-06-11 11:09:47 -040071 if (old_dir == new_dir)
72 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040073
Eric Paris90586522009-05-21 17:01:20 -040074 if (isdir) {
Eric Paris90586522009-05-21 17:01:20 -040075 old_dir_mask |= FS_IN_ISDIR;
76 new_dir_mask |= FS_IN_ISDIR;
77 }
78
Eric Paris47882c62009-05-21 17:01:47 -040079 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
80 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -040081
Eric Paris2dfc1ca2009-12-17 20:30:52 -050082 if (target)
Eric Paris90586522009-05-21 17:01:20 -040083 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -040084
Eric Paris2dfc1ca2009-12-17 20:30:52 -050085 if (source)
Eric Paris47882c62009-05-21 17:01:47 -040086 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Al Virocccc6bb2009-12-25 05:07:33 -050087 audit_inode_child(moved, new_dir);
Robert Love0eeca282005-07-12 17:06:03 -040088}
89
90/*
Eric Paris3be25f42009-05-21 17:01:26 -040091 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
92 */
93static inline void fsnotify_inode_delete(struct inode *inode)
94{
95 __fsnotify_inode_delete(inode);
96}
97
98/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -050099 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
100 */
101static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
102{
103 __fsnotify_vfsmount_delete(mnt);
104}
105
106/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400107 * fsnotify_nameremove - a filename was removed from a directory
108 */
109static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
110{
Eric Paris90586522009-05-21 17:01:20 -0400111 __u32 mask = FS_DELETE;
112
John McCutchan7a91bf72005-08-08 13:52:16 -0400113 if (isdir)
Eric Paris90586522009-05-21 17:01:20 -0400114 mask |= FS_IN_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400115
Eric Paris28c60e32009-12-17 21:24:21 -0500116 fsnotify_parent(NULL, dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400117}
118
119/*
120 * fsnotify_inoderemove - an inode is going away
121 */
122static inline void fsnotify_inoderemove(struct inode *inode)
123{
Eric Paris47882c62009-05-21 17:01:47 -0400124 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400125 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800126}
127
128/*
Robert Love0eeca282005-07-12 17:06:03 -0400129 * fsnotify_create - 'name' was linked in
130 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000131static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400132{
Al Virocccc6bb2009-12-25 05:07:33 -0500133 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400134
Eric Paris47882c62009-05-21 17:01:47 -0400135 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400136}
137
138/*
Jan Karaece95912008-02-06 01:37:13 -0800139 * fsnotify_link - new hardlink in 'inode' directory
140 * Note: We have to pass also the linked inode ptr as some filesystems leave
141 * new_dentry->d_inode NULL and instantiate inode pointer later
142 */
143static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
144{
Jan Karaece95912008-02-06 01:37:13 -0800145 fsnotify_link_count(inode);
Al Virocccc6bb2009-12-25 05:07:33 -0500146 audit_inode_child(new_dentry, dir);
Eric Paris90586522009-05-21 17:01:20 -0400147
Eric Paris47882c62009-05-21 17:01:47 -0400148 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800149}
150
151/*
Robert Love0eeca282005-07-12 17:06:03 -0400152 * fsnotify_mkdir - directory 'name' was created
153 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000154static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400155{
Eric Paris90586522009-05-21 17:01:20 -0400156 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
157 struct inode *d_inode = dentry->d_inode;
158
Al Virocccc6bb2009-12-25 05:07:33 -0500159 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400160
Eric Paris47882c62009-05-21 17:01:47 -0400161 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400162}
163
164/*
165 * fsnotify_access - file was read
166 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500167static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400168{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -0500169 struct path *path = &file->f_path;
170 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400171 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400172
173 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400174 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400175
Eric Parisecf081d2009-12-17 21:24:25 -0500176 if (!(file->f_mode & FMODE_NONOTIFY)) {
177 fsnotify_parent(path, NULL, mask);
178 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
179 }
Robert Love0eeca282005-07-12 17:06:03 -0400180}
181
182/*
183 * fsnotify_modify - file was modified
184 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500185static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400186{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -0500187 struct path *path = &file->f_path;
188 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400189 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400190
191 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400192 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400193
Eric Parisecf081d2009-12-17 21:24:25 -0500194 if (!(file->f_mode & FMODE_NONOTIFY)) {
195 fsnotify_parent(path, NULL, mask);
196 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
197 }
Robert Love0eeca282005-07-12 17:06:03 -0400198}
199
200/*
201 * fsnotify_open - file was opened
202 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500203static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400204{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -0500205 struct path *path = &file->f_path;
206 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400207 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400208
209 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400210 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400211
Eric Parisecf081d2009-12-17 21:24:25 -0500212 if (!(file->f_mode & FMODE_NONOTIFY)) {
213 fsnotify_parent(path, NULL, mask);
214 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
215 }
Robert Love0eeca282005-07-12 17:06:03 -0400216}
217
218/*
219 * fsnotify_close - file was closed
220 */
221static inline void fsnotify_close(struct file *file)
222{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -0500223 struct path *path = &file->f_path;
Eric Paris28c60e32009-12-17 21:24:21 -0500224 struct inode *inode = file->f_path.dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400225 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400226 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400227
228 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400229 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400230
Eric Parisecf081d2009-12-17 21:24:25 -0500231 if (!(file->f_mode & FMODE_NONOTIFY)) {
232 fsnotify_parent(path, NULL, mask);
233 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
234 }
Robert Love0eeca282005-07-12 17:06:03 -0400235}
236
237/*
238 * fsnotify_xattr - extended attributes were changed
239 */
240static inline void fsnotify_xattr(struct dentry *dentry)
241{
242 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400243 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400244
245 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400246 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400247
Eric Paris28c60e32009-12-17 21:24:21 -0500248 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400249 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400250}
251
252/*
253 * fsnotify_change - notify_change event. file was modified and/or metadata
254 * was changed.
255 */
256static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
257{
258 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400259 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400260
Eric Paris3c5119c2009-05-21 17:01:33 -0400261 if (ia_valid & ATTR_UID)
262 mask |= FS_ATTRIB;
263 if (ia_valid & ATTR_GID)
264 mask |= FS_ATTRIB;
265 if (ia_valid & ATTR_SIZE)
266 mask |= FS_MODIFY;
267
Robert Love0eeca282005-07-12 17:06:03 -0400268 /* both times implies a utime(s) call */
269 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400270 mask |= FS_ATTRIB;
271 else if (ia_valid & ATTR_ATIME)
272 mask |= FS_ACCESS;
273 else if (ia_valid & ATTR_MTIME)
274 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400275
Eric Paris3c5119c2009-05-21 17:01:33 -0400276 if (ia_valid & ATTR_MODE)
277 mask |= FS_ATTRIB;
278
279 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400280 if (S_ISDIR(inode->i_mode))
Eric Paris3c5119c2009-05-21 17:01:33 -0400281 mask |= FS_IN_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500282
283 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400284 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400285 }
286}
287
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500288#if defined(CONFIG_FSNOTIFY) /* notify helpers */
Robert Love0eeca282005-07-12 17:06:03 -0400289
290/*
291 * fsnotify_oldname_init - save off the old filename before we change it
292 */
293static inline const char *fsnotify_oldname_init(const char *name)
294{
295 return kstrdup(name, GFP_KERNEL);
296}
297
298/*
299 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
300 */
301static inline void fsnotify_oldname_free(const char *old_name)
302{
303 kfree(old_name);
304}
305
Eric Paris28c60e32009-12-17 21:24:21 -0500306#else /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400307
308static inline const char *fsnotify_oldname_init(const char *name)
309{
310 return NULL;
311}
312
313static inline void fsnotify_oldname_free(const char *old_name)
314{
315}
316
Eric Paris28c60e32009-12-17 21:24:21 -0500317#endif /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400318
Robert Love0eeca282005-07-12 17:06:03 -0400319#endif /* _LINUX_FS_NOTIFY_H */