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/fsnotify_backend.h>
15 #include <linux/audit.h>
16 #include <linux/slab.h>
19 * fsnotify_d_instantiate - instantiate a dentry for inode
20 * Called with dcache_lock held.
22 static inline void fsnotify_d_instantiate(struct dentry *entry,
25 __fsnotify_d_instantiate(entry, inode);
28 /* Notify this dentry's parent about a child's events. */
29 static inline void fsnotify_parent(struct file *file, struct dentry *dentry, __u32 mask)
31 BUG_ON(file && dentry);
34 dentry = file->f_path.dentry;
36 __fsnotify_parent(file, dentry, mask);
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);
53 * fsnotify_link_count - inode's link count changed
55 static inline void fsnotify_link_count(struct inode *inode)
57 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
61 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
63 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
65 int isdir, struct inode *target, struct dentry *moved)
67 struct inode *source = moved->d_inode;
68 u32 fs_cookie = fsnotify_get_cookie();
69 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
70 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
71 const char *new_name = moved->d_name.name;
73 if (old_dir == new_dir)
74 old_dir_mask |= FS_DN_RENAME;
77 old_dir_mask |= FS_IN_ISDIR;
78 new_dir_mask |= FS_IN_ISDIR;
81 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);
85 fsnotify_link_count(target);
88 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
89 audit_inode_child(moved, new_dir);
93 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
95 static inline void fsnotify_inode_delete(struct inode *inode)
97 __fsnotify_inode_delete(inode);
101 * fsnotify_nameremove - a filename was removed from a directory
103 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
105 __u32 mask = FS_DELETE;
110 fsnotify_parent(NULL, dentry, mask);
114 * fsnotify_inoderemove - an inode is going away
116 static inline void fsnotify_inoderemove(struct inode *inode)
118 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
119 __fsnotify_inode_delete(inode);
123 * fsnotify_create - 'name' was linked in
125 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
127 audit_inode_child(dentry, inode);
129 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
133 * 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
137 static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
139 fsnotify_link_count(inode);
140 audit_inode_child(new_dentry, dir);
142 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
146 * fsnotify_mkdir - directory 'name' was created
148 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
150 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
151 struct inode *d_inode = dentry->d_inode;
153 audit_inode_child(dentry, inode);
155 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
159 * fsnotify_access - file was read
161 static inline void fsnotify_access(struct file *file)
163 struct inode *inode = file->f_path.dentry->d_inode;
164 __u32 mask = FS_ACCESS;
166 if (S_ISDIR(inode->i_mode))
169 fsnotify_parent(file, NULL, mask);
170 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
174 * fsnotify_modify - file was modified
176 static inline void fsnotify_modify(struct file *file)
178 struct inode *inode = file->f_path.dentry->d_inode;
179 __u32 mask = FS_MODIFY;
181 if (S_ISDIR(inode->i_mode))
184 fsnotify_parent(file, NULL, mask);
185 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
189 * fsnotify_open - file was opened
191 static inline void fsnotify_open(struct file *file)
193 struct inode *inode = file->f_path.dentry->d_inode;
194 __u32 mask = FS_OPEN;
196 if (S_ISDIR(inode->i_mode))
199 fsnotify_parent(file, NULL, mask);
200 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
204 * fsnotify_close - file was closed
206 static inline void fsnotify_close(struct file *file)
208 struct inode *inode = file->f_path.dentry->d_inode;
209 fmode_t mode = file->f_mode;
210 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
212 if (S_ISDIR(inode->i_mode))
215 fsnotify_parent(file, NULL, mask);
216 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
220 * fsnotify_xattr - extended attributes were changed
222 static inline void fsnotify_xattr(struct dentry *dentry)
224 struct inode *inode = dentry->d_inode;
225 __u32 mask = FS_ATTRIB;
227 if (S_ISDIR(inode->i_mode))
230 fsnotify_parent(NULL, dentry, mask);
231 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
235 * fsnotify_change - notify_change event. file was modified and/or metadata
238 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
240 struct inode *inode = dentry->d_inode;
243 if (ia_valid & ATTR_UID)
245 if (ia_valid & ATTR_GID)
247 if (ia_valid & ATTR_SIZE)
250 /* both times implies a utime(s) call */
251 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
253 else if (ia_valid & ATTR_ATIME)
255 else if (ia_valid & ATTR_MTIME)
258 if (ia_valid & ATTR_MODE)
262 if (S_ISDIR(inode->i_mode))
265 fsnotify_parent(NULL, dentry, mask);
266 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
270 #if defined(CONFIG_FSNOTIFY) /* notify helpers */
273 * fsnotify_oldname_init - save off the old filename before we change it
275 static inline const char *fsnotify_oldname_init(const char *name)
277 return kstrdup(name, GFP_KERNEL);
281 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
283 static inline void fsnotify_oldname_free(const char *old_name)
288 #else /* CONFIG_FSNOTIFY */
290 static inline const char *fsnotify_oldname_init(const char *name)
295 static inline void fsnotify_oldname_free(const char *old_name)
299 #endif /* CONFIG_FSNOTIFY */
301 #endif /* _LINUX_FS_NOTIFY_H */