1 #ifndef _LINUX_FS_NOTIFY_H
2 #define _LINUX_FS_NOTIFY_H
5 * include/linux/fsnotify.h - generic hooks for filesystem notification, to
6 * reduce in-source duplication from both dnotify and inotify.
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.
11 * (C) Copyright 2005 Robert Love
14 #include <linux/dnotify.h>
15 #include <linux/inotify.h>
16 #include <linux/fsnotify_backend.h>
17 #include <linux/audit.h>
20 * fsnotify_d_instantiate - instantiate a dentry for inode
21 * Called with dcache_lock held.
23 static inline void fsnotify_d_instantiate(struct dentry *entry,
26 __fsnotify_d_instantiate(entry, inode);
28 inotify_d_instantiate(entry, inode);
31 /* Notify this dentry's parent about a child's events. */
32 static inline void fsnotify_parent(struct dentry *dentry, __u32 mask)
34 __fsnotify_parent(dentry, mask);
36 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
40 * fsnotify_d_move - entry has been moved
41 * Called with dcache_lock and entry->d_lock held.
43 static inline void fsnotify_d_move(struct dentry *entry)
46 * On move we need to update entry->d_flags to indicate if the new parent
47 * cares about events from this entry.
49 __fsnotify_update_dcache_flags(entry);
51 inotify_d_move(entry);
55 * fsnotify_link_count - inode's link count changed
57 static inline void fsnotify_link_count(struct inode *inode)
59 inotify_inode_queue_event(inode, IN_ATTRIB, 0, NULL, NULL);
61 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE);
65 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
67 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
68 const char *old_name, const char *new_name,
69 int isdir, struct inode *target, struct dentry *moved)
71 struct inode *source = moved->d_inode;
72 u32 cookie = inotify_get_cookie();
73 __u32 old_dir_mask = 0;
74 __u32 new_dir_mask = 0;
76 if (old_dir == new_dir) {
77 inode_dir_notify(old_dir, DN_RENAME);
78 old_dir_mask = FS_DN_RENAME;
80 inode_dir_notify(old_dir, DN_DELETE);
81 old_dir_mask = FS_DELETE;
82 inode_dir_notify(new_dir, DN_CREATE);
83 new_dir_mask = FS_CREATE;
88 old_dir_mask |= FS_IN_ISDIR;
89 new_dir_mask |= FS_IN_ISDIR;
92 old_dir_mask |= FS_MOVED_FROM;
93 new_dir_mask |= FS_MOVED_TO;
95 inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name,
97 inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name,
100 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE);
101 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE);
104 inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
105 inotify_inode_is_dead(target);
107 /* this is really a link_count change not a removal */
108 fsnotify_link_count(target);
112 inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL);
113 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE);
115 audit_inode_child(new_name, moved, new_dir);
119 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
121 static inline void fsnotify_inode_delete(struct inode *inode)
123 __fsnotify_inode_delete(inode);
127 * fsnotify_nameremove - a filename was removed from a directory
129 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
131 __u32 mask = FS_DELETE;
135 dnotify_parent(dentry, DN_DELETE);
137 fsnotify_parent(dentry, mask);
141 * fsnotify_inoderemove - an inode is going away
143 static inline void fsnotify_inoderemove(struct inode *inode)
145 inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL, NULL);
146 inotify_inode_is_dead(inode);
148 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE);
149 __fsnotify_inode_delete(inode);
153 * fsnotify_create - 'name' was linked in
155 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
157 inode_dir_notify(inode, DN_CREATE);
158 inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name,
160 audit_inode_child(dentry->d_name.name, dentry, inode);
162 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE);
166 * fsnotify_link - new hardlink in 'inode' directory
167 * Note: We have to pass also the linked inode ptr as some filesystems leave
168 * new_dentry->d_inode NULL and instantiate inode pointer later
170 static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
172 inode_dir_notify(dir, DN_CREATE);
173 inotify_inode_queue_event(dir, IN_CREATE, 0, new_dentry->d_name.name,
175 fsnotify_link_count(inode);
176 audit_inode_child(new_dentry->d_name.name, new_dentry, dir);
178 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE);
182 * fsnotify_mkdir - directory 'name' was created
184 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
186 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
187 struct inode *d_inode = dentry->d_inode;
189 inode_dir_notify(inode, DN_CREATE);
190 inotify_inode_queue_event(inode, mask, 0, dentry->d_name.name, d_inode);
191 audit_inode_child(dentry->d_name.name, dentry, inode);
193 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE);
197 * fsnotify_access - file was read
199 static inline void fsnotify_access(struct dentry *dentry)
201 struct inode *inode = dentry->d_inode;
202 __u32 mask = FS_ACCESS;
204 if (S_ISDIR(inode->i_mode))
207 dnotify_parent(dentry, DN_ACCESS);
208 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
210 fsnotify_parent(dentry, mask);
211 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
215 * fsnotify_modify - file was modified
217 static inline void fsnotify_modify(struct dentry *dentry)
219 struct inode *inode = dentry->d_inode;
220 __u32 mask = FS_MODIFY;
222 if (S_ISDIR(inode->i_mode))
225 dnotify_parent(dentry, DN_MODIFY);
226 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
228 fsnotify_parent(dentry, mask);
229 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
233 * fsnotify_open - file was opened
235 static inline void fsnotify_open(struct dentry *dentry)
237 struct inode *inode = dentry->d_inode;
238 __u32 mask = FS_OPEN;
240 if (S_ISDIR(inode->i_mode))
243 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
245 fsnotify_parent(dentry, mask);
246 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
250 * fsnotify_close - file was closed
252 static inline void fsnotify_close(struct file *file)
254 struct dentry *dentry = file->f_path.dentry;
255 struct inode *inode = dentry->d_inode;
256 fmode_t mode = file->f_mode;
257 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
259 if (S_ISDIR(inode->i_mode))
262 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
264 fsnotify_parent(dentry, mask);
265 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE);
269 * fsnotify_xattr - extended attributes were changed
271 static inline void fsnotify_xattr(struct dentry *dentry)
273 struct inode *inode = dentry->d_inode;
274 __u32 mask = FS_ATTRIB;
276 if (S_ISDIR(inode->i_mode))
279 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
281 fsnotify_parent(dentry, mask);
282 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
286 * fsnotify_change - notify_change event. file was modified and/or metadata
289 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
291 struct inode *inode = dentry->d_inode;
295 if (ia_valid & ATTR_UID) {
296 in_mask |= FS_ATTRIB;
297 dn_mask |= DN_ATTRIB;
299 if (ia_valid & ATTR_GID) {
300 in_mask |= FS_ATTRIB;
301 dn_mask |= DN_ATTRIB;
303 if (ia_valid & ATTR_SIZE) {
304 in_mask |= FS_MODIFY;
305 dn_mask |= DN_MODIFY;
307 /* both times implies a utime(s) call */
308 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
310 in_mask |= FS_ATTRIB;
311 dn_mask |= DN_ATTRIB;
312 } else if (ia_valid & ATTR_ATIME) {
313 in_mask |= FS_ACCESS;
314 dn_mask |= DN_ACCESS;
315 } else if (ia_valid & ATTR_MTIME) {
316 in_mask |= FS_MODIFY;
317 dn_mask |= DN_MODIFY;
319 if (ia_valid & ATTR_MODE) {
320 in_mask |= FS_ATTRIB;
321 dn_mask |= DN_ATTRIB;
325 dnotify_parent(dentry, dn_mask);
327 if (S_ISDIR(inode->i_mode))
328 in_mask |= FS_IN_ISDIR;
329 inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
331 fsnotify_parent(dentry, in_mask);
332 fsnotify(inode, in_mask, inode, FSNOTIFY_EVENT_INODE);
336 #if defined(CONFIG_INOTIFY) || defined(CONFIG_FSNOTIFY) /* notify helpers */
339 * fsnotify_oldname_init - save off the old filename before we change it
341 static inline const char *fsnotify_oldname_init(const char *name)
343 return kstrdup(name, GFP_KERNEL);
347 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
349 static inline void fsnotify_oldname_free(const char *old_name)
354 #else /* CONFIG_INOTIFY || CONFIG_FSNOTIFY */
356 static inline const char *fsnotify_oldname_init(const char *name)
361 static inline void fsnotify_oldname_free(const char *old_name)
365 #endif /* ! CONFIG_INOTIFY */
367 #endif /* _LINUX_FS_NOTIFY_H */