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
16 #include <linux/dnotify.h>
17 #include <linux/inotify.h>
18 #include <linux/audit.h>
21 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
23 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
24 const char *old_name, const char *new_name,
25 int isdir, struct inode *target, struct inode *source)
27 u32 cookie = inotify_get_cookie();
29 if (old_dir == new_dir)
30 inode_dir_notify(old_dir, DN_RENAME);
32 inode_dir_notify(old_dir, DN_DELETE);
33 inode_dir_notify(new_dir, DN_CREATE);
38 inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name);
39 inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name);
42 inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL);
43 inotify_inode_is_dead(target);
47 inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL);
49 audit_inode_child(old_name, source, old_dir->i_ino);
50 audit_inode_child(new_name, target, new_dir->i_ino);
54 * fsnotify_nameremove - a filename was removed from a directory
56 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
60 dnotify_parent(dentry, DN_DELETE);
61 inotify_dentry_parent_queue_event(dentry, IN_DELETE|isdir, 0, dentry->d_name.name);
65 * fsnotify_inoderemove - an inode is going away
67 static inline void fsnotify_inoderemove(struct inode *inode)
69 inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL);
70 inotify_inode_is_dead(inode);
74 * fsnotify_create - 'name' was linked in
76 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
78 inode_dir_notify(inode, DN_CREATE);
79 inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name);
80 audit_inode_child(dentry->d_name.name, dentry->d_inode, inode->i_ino);
84 * fsnotify_mkdir - directory 'name' was created
86 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
88 inode_dir_notify(inode, DN_CREATE);
89 inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0,
91 audit_inode_child(dentry->d_name.name, dentry->d_inode, inode->i_ino);
95 * fsnotify_access - file was read
97 static inline void fsnotify_access(struct dentry *dentry)
99 struct inode *inode = dentry->d_inode;
100 u32 mask = IN_ACCESS;
102 if (S_ISDIR(inode->i_mode))
105 dnotify_parent(dentry, DN_ACCESS);
106 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
107 inotify_inode_queue_event(inode, mask, 0, NULL);
111 * fsnotify_modify - file was modified
113 static inline void fsnotify_modify(struct dentry *dentry)
115 struct inode *inode = dentry->d_inode;
116 u32 mask = IN_MODIFY;
118 if (S_ISDIR(inode->i_mode))
121 dnotify_parent(dentry, DN_MODIFY);
122 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
123 inotify_inode_queue_event(inode, mask, 0, NULL);
127 * fsnotify_open - file was opened
129 static inline void fsnotify_open(struct dentry *dentry)
131 struct inode *inode = dentry->d_inode;
134 if (S_ISDIR(inode->i_mode))
137 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
138 inotify_inode_queue_event(inode, mask, 0, NULL);
142 * fsnotify_close - file was closed
144 static inline void fsnotify_close(struct file *file)
146 struct dentry *dentry = file->f_dentry;
147 struct inode *inode = dentry->d_inode;
148 const char *name = dentry->d_name.name;
149 mode_t mode = file->f_mode;
150 u32 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
152 if (S_ISDIR(inode->i_mode))
155 inotify_dentry_parent_queue_event(dentry, mask, 0, name);
156 inotify_inode_queue_event(inode, mask, 0, NULL);
160 * fsnotify_xattr - extended attributes were changed
162 static inline void fsnotify_xattr(struct dentry *dentry)
164 struct inode *inode = dentry->d_inode;
165 u32 mask = IN_ATTRIB;
167 if (S_ISDIR(inode->i_mode))
170 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
171 inotify_inode_queue_event(inode, mask, 0, NULL);
175 * fsnotify_change - notify_change event. file was modified and/or metadata
178 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
180 struct inode *inode = dentry->d_inode;
184 if (ia_valid & ATTR_UID) {
185 in_mask |= IN_ATTRIB;
186 dn_mask |= DN_ATTRIB;
188 if (ia_valid & ATTR_GID) {
189 in_mask |= IN_ATTRIB;
190 dn_mask |= DN_ATTRIB;
192 if (ia_valid & ATTR_SIZE) {
193 in_mask |= IN_MODIFY;
194 dn_mask |= DN_MODIFY;
196 /* both times implies a utime(s) call */
197 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
199 in_mask |= IN_ATTRIB;
200 dn_mask |= DN_ATTRIB;
201 } else if (ia_valid & ATTR_ATIME) {
202 in_mask |= IN_ACCESS;
203 dn_mask |= DN_ACCESS;
204 } else if (ia_valid & ATTR_MTIME) {
205 in_mask |= IN_MODIFY;
206 dn_mask |= DN_MODIFY;
208 if (ia_valid & ATTR_MODE) {
209 in_mask |= IN_ATTRIB;
210 dn_mask |= DN_ATTRIB;
214 dnotify_parent(dentry, dn_mask);
216 if (S_ISDIR(inode->i_mode))
218 inotify_inode_queue_event(inode, in_mask, 0, NULL);
219 inotify_dentry_parent_queue_event(dentry, in_mask, 0,
220 dentry->d_name.name);
224 #ifdef CONFIG_INOTIFY /* inotify helpers */
227 * fsnotify_oldname_init - save off the old filename before we change it
229 static inline const char *fsnotify_oldname_init(const char *name)
231 return kstrdup(name, GFP_KERNEL);
235 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
237 static inline void fsnotify_oldname_free(const char *old_name)
242 #else /* CONFIG_INOTIFY */
244 static inline const char *fsnotify_oldname_init(const char *name)
249 static inline void fsnotify_oldname_free(const char *old_name)
253 #endif /* ! CONFIG_INOTIFY */
255 #endif /* __KERNEL__ */
257 #endif /* _LINUX_FS_NOTIFY_H */