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 inotify_d_instantiate(entry, inode);
30 * fsnotify_d_move - entry has been moved
31 * Called with dcache_lock and entry->d_lock held.
33 static inline void fsnotify_d_move(struct dentry *entry)
35 inotify_d_move(entry);
39 * fsnotify_link_count - inode's link count changed
41 static inline void fsnotify_link_count(struct inode *inode)
43 inotify_inode_queue_event(inode, IN_ATTRIB, 0, NULL, NULL);
45 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE);
49 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
51 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
52 const char *old_name, const char *new_name,
53 int isdir, struct inode *target, struct dentry *moved)
55 struct inode *source = moved->d_inode;
56 u32 cookie = inotify_get_cookie();
57 __u32 old_dir_mask = 0;
58 __u32 new_dir_mask = 0;
60 if (old_dir == new_dir) {
61 inode_dir_notify(old_dir, DN_RENAME);
62 old_dir_mask = FS_DN_RENAME;
64 inode_dir_notify(old_dir, DN_DELETE);
65 old_dir_mask = FS_DELETE;
66 inode_dir_notify(new_dir, DN_CREATE);
67 new_dir_mask = FS_CREATE;
72 old_dir_mask |= FS_IN_ISDIR;
73 new_dir_mask |= FS_IN_ISDIR;
76 old_dir_mask |= FS_MOVED_FROM;
77 new_dir_mask |= FS_MOVED_TO;
79 inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name,
81 inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name,
84 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE);
85 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE);
88 inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
89 inotify_inode_is_dead(target);
91 /* this is really a link_count change not a removal */
92 fsnotify_link_count(target);
96 inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL);
97 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE);
99 audit_inode_child(new_name, moved, new_dir);
103 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
105 static inline void fsnotify_inode_delete(struct inode *inode)
107 __fsnotify_inode_delete(inode);
111 * fsnotify_nameremove - a filename was removed from a directory
113 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
115 __u32 mask = FS_DELETE;
119 dnotify_parent(dentry, DN_DELETE);
120 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
124 * fsnotify_inoderemove - an inode is going away
126 static inline void fsnotify_inoderemove(struct inode *inode)
128 inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL, NULL);
129 inotify_inode_is_dead(inode);
131 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE);
132 __fsnotify_inode_delete(inode);
136 * fsnotify_create - 'name' was linked in
138 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
140 inode_dir_notify(inode, DN_CREATE);
141 inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name,
143 audit_inode_child(dentry->d_name.name, dentry, inode);
145 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE);
149 * fsnotify_link - new hardlink in 'inode' directory
150 * Note: We have to pass also the linked inode ptr as some filesystems leave
151 * new_dentry->d_inode NULL and instantiate inode pointer later
153 static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
155 inode_dir_notify(dir, DN_CREATE);
156 inotify_inode_queue_event(dir, IN_CREATE, 0, new_dentry->d_name.name,
158 fsnotify_link_count(inode);
159 audit_inode_child(new_dentry->d_name.name, new_dentry, dir);
161 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE);
165 * fsnotify_mkdir - directory 'name' was created
167 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
169 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
170 struct inode *d_inode = dentry->d_inode;
172 inode_dir_notify(inode, DN_CREATE);
173 inotify_inode_queue_event(inode, mask, 0, dentry->d_name.name, d_inode);
174 audit_inode_child(dentry->d_name.name, dentry, inode);
176 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE);
180 * fsnotify_access - file was read
182 static inline void fsnotify_access(struct dentry *dentry)
184 struct inode *inode = dentry->d_inode;
185 __u32 mask = FS_ACCESS;
187 if (S_ISDIR(inode->i_mode))
190 dnotify_parent(dentry, DN_ACCESS);
191 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
192 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
194 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
198 * fsnotify_modify - file was modified
200 static inline void fsnotify_modify(struct dentry *dentry)
202 struct inode *inode = dentry->d_inode;
203 __u32 mask = FS_MODIFY;
205 if (S_ISDIR(inode->i_mode))
208 dnotify_parent(dentry, DN_MODIFY);
209 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
210 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
212 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
216 * fsnotify_open - file was opened
218 static inline void fsnotify_open(struct dentry *dentry)
220 struct inode *inode = dentry->d_inode;
221 __u32 mask = FS_OPEN;
223 if (S_ISDIR(inode->i_mode))
226 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
227 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
229 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
233 * fsnotify_close - file was closed
235 static inline void fsnotify_close(struct file *file)
237 struct dentry *dentry = file->f_path.dentry;
238 struct inode *inode = dentry->d_inode;
239 const char *name = dentry->d_name.name;
240 fmode_t mode = file->f_mode;
241 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
243 if (S_ISDIR(inode->i_mode))
246 inotify_dentry_parent_queue_event(dentry, mask, 0, name);
247 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
249 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE);
253 * fsnotify_xattr - extended attributes were changed
255 static inline void fsnotify_xattr(struct dentry *dentry)
257 struct inode *inode = dentry->d_inode;
258 __u32 mask = FS_ATTRIB;
260 if (S_ISDIR(inode->i_mode))
263 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
264 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
266 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
270 * fsnotify_change - notify_change event. file was modified and/or metadata
273 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
275 struct inode *inode = dentry->d_inode;
279 if (ia_valid & ATTR_UID) {
280 in_mask |= FS_ATTRIB;
281 dn_mask |= DN_ATTRIB;
283 if (ia_valid & ATTR_GID) {
284 in_mask |= FS_ATTRIB;
285 dn_mask |= DN_ATTRIB;
287 if (ia_valid & ATTR_SIZE) {
288 in_mask |= FS_MODIFY;
289 dn_mask |= DN_MODIFY;
291 /* both times implies a utime(s) call */
292 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
294 in_mask |= FS_ATTRIB;
295 dn_mask |= DN_ATTRIB;
296 } else if (ia_valid & ATTR_ATIME) {
297 in_mask |= FS_ACCESS;
298 dn_mask |= DN_ACCESS;
299 } else if (ia_valid & ATTR_MTIME) {
300 in_mask |= FS_MODIFY;
301 dn_mask |= DN_MODIFY;
303 if (ia_valid & ATTR_MODE) {
304 in_mask |= FS_ATTRIB;
305 dn_mask |= DN_ATTRIB;
309 dnotify_parent(dentry, dn_mask);
311 if (S_ISDIR(inode->i_mode))
312 in_mask |= FS_IN_ISDIR;
313 inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
314 inotify_dentry_parent_queue_event(dentry, in_mask, 0,
315 dentry->d_name.name);
316 fsnotify(inode, in_mask, inode, FSNOTIFY_EVENT_INODE);
320 #if defined(CONFIG_INOTIFY) || defined(CONFIG_FSNOTIFY) /* notify helpers */
323 * fsnotify_oldname_init - save off the old filename before we change it
325 static inline const char *fsnotify_oldname_init(const char *name)
327 return kstrdup(name, GFP_KERNEL);
331 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
333 static inline void fsnotify_oldname_free(const char *old_name)
338 #else /* CONFIG_INOTIFY || CONFIG_FSNOTIFY */
340 static inline const char *fsnotify_oldname_init(const char *name)
345 static inline void fsnotify_oldname_free(const char *old_name)
349 #endif /* ! CONFIG_INOTIFY */
351 #endif /* _LINUX_FS_NOTIFY_H */