blob: f958e93feb97f69fc8c3c53ec0ac568fdac9ba31 [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. */
29static inline void fsnotify_parent(struct dentry *dentry, __u32 mask)
30{
31 __fsnotify_parent(dentry, mask);
Eric Parisc28f7e52009-05-21 17:01:29 -040032}
33
Nick Pigginc32ccd82006-03-25 03:07:09 -080034/*
35 * fsnotify_d_move - entry has been moved
36 * Called with dcache_lock and entry->d_lock held.
37 */
38static inline void fsnotify_d_move(struct dentry *entry)
39{
Eric Parisc28f7e52009-05-21 17:01:29 -040040 /*
41 * On move we need to update entry->d_flags to indicate if the new parent
42 * cares about events from this entry.
43 */
44 __fsnotify_update_dcache_flags(entry);
Nick Pigginc32ccd82006-03-25 03:07:09 -080045}
46
47/*
Eric Paris90586522009-05-21 17:01:20 -040048 * fsnotify_link_count - inode's link count changed
49 */
50static inline void fsnotify_link_count(struct inode *inode)
51{
Eric Paris47882c62009-05-21 17:01:47 -040052 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040053}
54
55/*
Robert Love0eeca282005-07-12 17:06:03 -040056 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
57 */
58static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Al Viro123df292009-12-25 04:57:57 -050059 const char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040060 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040061{
Al Viro5a190ae2007-06-07 12:19:32 -040062 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040063 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040064 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
65 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Al Viro123df292009-12-25 04:57:57 -050066 const char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040067
Eric Parisff52cc22009-06-11 11:09:47 -040068 if (old_dir == new_dir)
69 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040070
Eric Paris90586522009-05-21 17:01:20 -040071 if (isdir) {
Eric Paris90586522009-05-21 17:01:20 -040072 old_dir_mask |= FS_IN_ISDIR;
73 new_dir_mask |= FS_IN_ISDIR;
74 }
75
Eric Paris47882c62009-05-21 17:01:47 -040076 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
77 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -040078
Eric Paris2dfc1ca2009-12-17 20:30:52 -050079 if (target)
Eric Paris90586522009-05-21 17:01:20 -040080 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -040081
Eric Paris2dfc1ca2009-12-17 20:30:52 -050082 if (source)
Eric Paris47882c62009-05-21 17:01:47 -040083 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Al Virocccc6bb2009-12-25 05:07:33 -050084 audit_inode_child(moved, new_dir);
Robert Love0eeca282005-07-12 17:06:03 -040085}
86
87/*
Eric Paris3be25f42009-05-21 17:01:26 -040088 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
89 */
90static inline void fsnotify_inode_delete(struct inode *inode)
91{
92 __fsnotify_inode_delete(inode);
93}
94
95/*
John McCutchan7a91bf72005-08-08 13:52:16 -040096 * fsnotify_nameremove - a filename was removed from a directory
97 */
98static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
99{
Eric Paris90586522009-05-21 17:01:20 -0400100 __u32 mask = FS_DELETE;
101
John McCutchan7a91bf72005-08-08 13:52:16 -0400102 if (isdir)
Eric Paris90586522009-05-21 17:01:20 -0400103 mask |= FS_IN_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400104
105 fsnotify_parent(dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400106}
107
108/*
109 * fsnotify_inoderemove - an inode is going away
110 */
111static inline void fsnotify_inoderemove(struct inode *inode)
112{
Eric Paris47882c62009-05-21 17:01:47 -0400113 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400114 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800115}
116
117/*
Robert Love0eeca282005-07-12 17:06:03 -0400118 * fsnotify_create - 'name' was linked in
119 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000120static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400121{
Al Virocccc6bb2009-12-25 05:07:33 -0500122 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400123
Eric Paris47882c62009-05-21 17:01:47 -0400124 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400125}
126
127/*
Jan Karaece95912008-02-06 01:37:13 -0800128 * fsnotify_link - new hardlink in 'inode' directory
129 * Note: We have to pass also the linked inode ptr as some filesystems leave
130 * new_dentry->d_inode NULL and instantiate inode pointer later
131 */
132static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
133{
Jan Karaece95912008-02-06 01:37:13 -0800134 fsnotify_link_count(inode);
Al Virocccc6bb2009-12-25 05:07:33 -0500135 audit_inode_child(new_dentry, dir);
Eric Paris90586522009-05-21 17:01:20 -0400136
Eric Paris47882c62009-05-21 17:01:47 -0400137 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800138}
139
140/*
Robert Love0eeca282005-07-12 17:06:03 -0400141 * fsnotify_mkdir - directory 'name' was created
142 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000143static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400144{
Eric Paris90586522009-05-21 17:01:20 -0400145 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
146 struct inode *d_inode = dentry->d_inode;
147
Al Virocccc6bb2009-12-25 05:07:33 -0500148 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400149
Eric Paris47882c62009-05-21 17:01:47 -0400150 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400151}
152
153/*
154 * fsnotify_access - file was read
155 */
156static inline void fsnotify_access(struct dentry *dentry)
157{
158 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400159 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400160
161 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400162 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400163
Eric Parisc28f7e52009-05-21 17:01:29 -0400164 fsnotify_parent(dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400165 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400166}
167
168/*
169 * fsnotify_modify - file was modified
170 */
171static inline void fsnotify_modify(struct dentry *dentry)
172{
173 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400174 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400175
176 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400177 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400178
Eric Parisc28f7e52009-05-21 17:01:29 -0400179 fsnotify_parent(dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400180 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400181}
182
183/*
184 * fsnotify_open - file was opened
185 */
186static inline void fsnotify_open(struct dentry *dentry)
187{
188 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400189 __u32 mask = FS_OPEN;
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 Parisc28f7e52009-05-21 17:01:29 -0400194 fsnotify_parent(dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400195 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400196}
197
198/*
199 * fsnotify_close - file was closed
200 */
201static inline void fsnotify_close(struct file *file)
202{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800203 struct dentry *dentry = file->f_path.dentry;
Robert Love0eeca282005-07-12 17:06:03 -0400204 struct inode *inode = dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400205 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400206 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400207
208 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400209 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400210
Eric Parisc28f7e52009-05-21 17:01:29 -0400211 fsnotify_parent(dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400212 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400213}
214
215/*
216 * fsnotify_xattr - extended attributes were changed
217 */
218static inline void fsnotify_xattr(struct dentry *dentry)
219{
220 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400221 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400222
223 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400224 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400225
Eric Parisc28f7e52009-05-21 17:01:29 -0400226 fsnotify_parent(dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400227 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400228}
229
230/*
231 * fsnotify_change - notify_change event. file was modified and/or metadata
232 * was changed.
233 */
234static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
235{
236 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400237 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400238
Eric Paris3c5119c2009-05-21 17:01:33 -0400239 if (ia_valid & ATTR_UID)
240 mask |= FS_ATTRIB;
241 if (ia_valid & ATTR_GID)
242 mask |= FS_ATTRIB;
243 if (ia_valid & ATTR_SIZE)
244 mask |= FS_MODIFY;
245
Robert Love0eeca282005-07-12 17:06:03 -0400246 /* both times implies a utime(s) call */
247 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400248 mask |= FS_ATTRIB;
249 else if (ia_valid & ATTR_ATIME)
250 mask |= FS_ACCESS;
251 else if (ia_valid & ATTR_MTIME)
252 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400253
Eric Paris3c5119c2009-05-21 17:01:33 -0400254 if (ia_valid & ATTR_MODE)
255 mask |= FS_ATTRIB;
256
257 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400258 if (S_ISDIR(inode->i_mode))
Eric Paris3c5119c2009-05-21 17:01:33 -0400259 mask |= FS_IN_ISDIR;
Eric Paris3c5119c2009-05-21 17:01:33 -0400260 fsnotify_parent(dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400261 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400262 }
263}
264
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500265#if defined(CONFIG_FSNOTIFY) /* notify helpers */
Robert Love0eeca282005-07-12 17:06:03 -0400266
267/*
268 * fsnotify_oldname_init - save off the old filename before we change it
269 */
270static inline const char *fsnotify_oldname_init(const char *name)
271{
272 return kstrdup(name, GFP_KERNEL);
273}
274
275/*
276 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
277 */
278static inline void fsnotify_oldname_free(const char *old_name)
279{
280 kfree(old_name);
281}
282
Eric Paris90586522009-05-21 17:01:20 -0400283#else /* CONFIG_INOTIFY || CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400284
285static inline const char *fsnotify_oldname_init(const char *name)
286{
287 return NULL;
288}
289
290static inline void fsnotify_oldname_free(const char *old_name)
291{
292}
293
294#endif /* ! CONFIG_INOTIFY */
295
Robert Love0eeca282005-07-12 17:06:03 -0400296#endif /* _LINUX_FS_NOTIFY_H */