blob: 04ea03ea80905c5edf9f23440a1d1b805b8fedc7 [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 */
22static inline void fsnotify_d_instantiate(struct dentry *entry,
23 struct inode *inode)
24{
Eric Parisc28f7e52009-05-21 17:01:29 -040025 __fsnotify_d_instantiate(entry, 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. */
Eric Paris28c60e32009-12-17 21:24:21 -050029static inline void fsnotify_parent(struct file *file, struct dentry *dentry, __u32 mask)
Eric Parisc28f7e52009-05-21 17:01:29 -040030{
Eric Paris28c60e32009-12-17 21:24:21 -050031 BUG_ON(file && dentry);
32
33 if (file)
34 dentry = file->f_path.dentry;
35
36 __fsnotify_parent(file, dentry, mask);
Eric Parisc28f7e52009-05-21 17:01:29 -040037}
38
Nick Pigginc32ccd82006-03-25 03:07:09 -080039/*
40 * fsnotify_d_move - entry has been moved
41 * Called with dcache_lock and entry->d_lock held.
42 */
43static inline void fsnotify_d_move(struct dentry *entry)
44{
Eric Parisc28f7e52009-05-21 17:01:29 -040045 /*
46 * On move we need to update entry->d_flags to indicate if the new parent
47 * cares about events from this entry.
48 */
49 __fsnotify_update_dcache_flags(entry);
Nick Pigginc32ccd82006-03-25 03:07:09 -080050}
51
52/*
Eric Paris90586522009-05-21 17:01:20 -040053 * fsnotify_link_count - inode's link count changed
54 */
55static inline void fsnotify_link_count(struct inode *inode)
56{
Eric Paris47882c62009-05-21 17:01:47 -040057 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040058}
59
60/*
Robert Love0eeca282005-07-12 17:06:03 -040061 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
62 */
63static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Al Viro123df292009-12-25 04:57:57 -050064 const char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040065 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040066{
Al Viro5a190ae2007-06-07 12:19:32 -040067 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040068 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040069 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
70 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Al Viro123df292009-12-25 04:57:57 -050071 const char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040072
Eric Parisff52cc22009-06-11 11:09:47 -040073 if (old_dir == new_dir)
74 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040075
Eric Paris90586522009-05-21 17:01:20 -040076 if (isdir) {
Eric Paris90586522009-05-21 17:01:20 -040077 old_dir_mask |= FS_IN_ISDIR;
78 new_dir_mask |= FS_IN_ISDIR;
79 }
80
Eric Paris47882c62009-05-21 17:01:47 -040081 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
82 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -040083
Eric Paris2dfc1ca2009-12-17 20:30:52 -050084 if (target)
Eric Paris90586522009-05-21 17:01:20 -040085 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -040086
Eric Paris2dfc1ca2009-12-17 20:30:52 -050087 if (source)
Eric Paris47882c62009-05-21 17:01:47 -040088 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Al Virocccc6bb2009-12-25 05:07:33 -050089 audit_inode_child(moved, new_dir);
Robert Love0eeca282005-07-12 17:06:03 -040090}
91
92/*
Eric Paris3be25f42009-05-21 17:01:26 -040093 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
94 */
95static inline void fsnotify_inode_delete(struct inode *inode)
96{
97 __fsnotify_inode_delete(inode);
98}
99
100/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400101 * fsnotify_nameremove - a filename was removed from a directory
102 */
103static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
104{
Eric Paris90586522009-05-21 17:01:20 -0400105 __u32 mask = FS_DELETE;
106
John McCutchan7a91bf72005-08-08 13:52:16 -0400107 if (isdir)
Eric Paris90586522009-05-21 17:01:20 -0400108 mask |= FS_IN_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400109
Eric Paris28c60e32009-12-17 21:24:21 -0500110 fsnotify_parent(NULL, dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400111}
112
113/*
114 * fsnotify_inoderemove - an inode is going away
115 */
116static inline void fsnotify_inoderemove(struct inode *inode)
117{
Eric Paris47882c62009-05-21 17:01:47 -0400118 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400119 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800120}
121
122/*
Robert Love0eeca282005-07-12 17:06:03 -0400123 * fsnotify_create - 'name' was linked in
124 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000125static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400126{
Al Virocccc6bb2009-12-25 05:07:33 -0500127 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400128
Eric Paris47882c62009-05-21 17:01:47 -0400129 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400130}
131
132/*
Jan Karaece95912008-02-06 01:37:13 -0800133 * fsnotify_link - new hardlink in 'inode' directory
134 * Note: We have to pass also the linked inode ptr as some filesystems leave
135 * new_dentry->d_inode NULL and instantiate inode pointer later
136 */
137static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
138{
Jan Karaece95912008-02-06 01:37:13 -0800139 fsnotify_link_count(inode);
Al Virocccc6bb2009-12-25 05:07:33 -0500140 audit_inode_child(new_dentry, dir);
Eric Paris90586522009-05-21 17:01:20 -0400141
Eric Paris47882c62009-05-21 17:01:47 -0400142 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800143}
144
145/*
Robert Love0eeca282005-07-12 17:06:03 -0400146 * fsnotify_mkdir - directory 'name' was created
147 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000148static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400149{
Eric Paris90586522009-05-21 17:01:20 -0400150 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
151 struct inode *d_inode = dentry->d_inode;
152
Al Virocccc6bb2009-12-25 05:07:33 -0500153 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400154
Eric Paris47882c62009-05-21 17:01:47 -0400155 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400156}
157
158/*
159 * fsnotify_access - file was read
160 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500161static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400162{
Eric Paris28c60e32009-12-17 21:24:21 -0500163 struct inode *inode = file->f_path.dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400164 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400165
166 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400167 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400168
Eric Paris28c60e32009-12-17 21:24:21 -0500169 fsnotify_parent(file, NULL, mask);
Eric Paris2a12a9d2009-12-17 21:24:21 -0500170 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400171}
172
173/*
174 * fsnotify_modify - file was modified
175 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500176static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400177{
Eric Paris28c60e32009-12-17 21:24:21 -0500178 struct inode *inode = file->f_path.dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400179 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400180
181 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400182 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400183
Eric Paris28c60e32009-12-17 21:24:21 -0500184 fsnotify_parent(file, NULL, mask);
Eric Paris2a12a9d2009-12-17 21:24:21 -0500185 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400186}
187
188/*
189 * fsnotify_open - file was opened
190 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500191static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400192{
Eric Paris28c60e32009-12-17 21:24:21 -0500193 struct inode *inode = file->f_path.dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400194 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400195
196 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400197 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400198
Eric Paris28c60e32009-12-17 21:24:21 -0500199 fsnotify_parent(file, NULL, mask);
Eric Paris2a12a9d2009-12-17 21:24:21 -0500200 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400201}
202
203/*
204 * fsnotify_close - file was closed
205 */
206static inline void fsnotify_close(struct file *file)
207{
Eric Paris28c60e32009-12-17 21:24:21 -0500208 struct inode *inode = file->f_path.dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400209 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400210 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400211
212 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400213 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400214
Eric Paris28c60e32009-12-17 21:24:21 -0500215 fsnotify_parent(file, NULL, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400216 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400217}
218
219/*
220 * fsnotify_xattr - extended attributes were changed
221 */
222static inline void fsnotify_xattr(struct dentry *dentry)
223{
224 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400225 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400226
227 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400228 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400229
Eric Paris28c60e32009-12-17 21:24:21 -0500230 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400231 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400232}
233
234/*
235 * fsnotify_change - notify_change event. file was modified and/or metadata
236 * was changed.
237 */
238static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
239{
240 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400241 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400242
Eric Paris3c5119c2009-05-21 17:01:33 -0400243 if (ia_valid & ATTR_UID)
244 mask |= FS_ATTRIB;
245 if (ia_valid & ATTR_GID)
246 mask |= FS_ATTRIB;
247 if (ia_valid & ATTR_SIZE)
248 mask |= FS_MODIFY;
249
Robert Love0eeca282005-07-12 17:06:03 -0400250 /* both times implies a utime(s) call */
251 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400252 mask |= FS_ATTRIB;
253 else if (ia_valid & ATTR_ATIME)
254 mask |= FS_ACCESS;
255 else if (ia_valid & ATTR_MTIME)
256 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400257
Eric Paris3c5119c2009-05-21 17:01:33 -0400258 if (ia_valid & ATTR_MODE)
259 mask |= FS_ATTRIB;
260
261 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400262 if (S_ISDIR(inode->i_mode))
Eric Paris3c5119c2009-05-21 17:01:33 -0400263 mask |= FS_IN_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500264
265 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400266 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400267 }
268}
269
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500270#if defined(CONFIG_FSNOTIFY) /* notify helpers */
Robert Love0eeca282005-07-12 17:06:03 -0400271
272/*
273 * fsnotify_oldname_init - save off the old filename before we change it
274 */
275static inline const char *fsnotify_oldname_init(const char *name)
276{
277 return kstrdup(name, GFP_KERNEL);
278}
279
280/*
281 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
282 */
283static inline void fsnotify_oldname_free(const char *old_name)
284{
285 kfree(old_name);
286}
287
Eric Paris28c60e32009-12-17 21:24:21 -0500288#else /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400289
290static inline const char *fsnotify_oldname_init(const char *name)
291{
292 return NULL;
293}
294
295static inline void fsnotify_oldname_free(const char *old_name)
296{
297}
298
Eric Paris28c60e32009-12-17 21:24:21 -0500299#endif /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400300
Robert Love0eeca282005-07-12 17:06:03 -0400301#endif /* _LINUX_FS_NOTIFY_H */