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>
17 #include <linux/bug.h>
19 /* Notify this dentry's parent about a child's events. */
20 static inline int fsnotify_parent(struct path
*path
, struct dentry
*dentry
, __u32 mask
)
23 dentry
= path
->dentry
;
25 return __fsnotify_parent(path
, dentry
, mask
);
28 /* simple call site for access decisions */
29 static inline int fsnotify_perm(struct file
*file
, int mask
)
31 struct path
*path
= &file
->f_path
;
32 struct inode
*inode
= file_inode(file
);
33 __u32 fsnotify_mask
= 0;
36 if (file
->f_mode
& FMODE_NONOTIFY
)
38 if (!(mask
& (MAY_READ
| MAY_OPEN
)))
41 fsnotify_mask
= FS_OPEN_PERM
;
42 else if (mask
& MAY_READ
)
43 fsnotify_mask
= FS_ACCESS_PERM
;
47 ret
= fsnotify_parent(path
, NULL
, fsnotify_mask
);
51 return fsnotify(inode
, fsnotify_mask
, path
, FSNOTIFY_EVENT_PATH
, NULL
, 0);
55 * fsnotify_link_count - inode's link count changed
57 static inline void fsnotify_link_count(struct inode
*inode
)
59 fsnotify(inode
, FS_ATTRIB
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
63 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
65 static inline void fsnotify_move(struct inode
*old_dir
, struct inode
*new_dir
,
66 const unsigned char *old_name
,
67 int isdir
, struct inode
*target
, struct dentry
*moved
)
69 struct inode
*source
= moved
->d_inode
;
70 u32 fs_cookie
= fsnotify_get_cookie();
71 __u32 old_dir_mask
= (FS_EVENT_ON_CHILD
| FS_MOVED_FROM
);
72 __u32 new_dir_mask
= (FS_EVENT_ON_CHILD
| FS_MOVED_TO
);
73 const unsigned char *new_name
= moved
->d_name
.name
;
75 if (old_dir
== new_dir
)
76 old_dir_mask
|= FS_DN_RENAME
;
79 old_dir_mask
|= FS_ISDIR
;
80 new_dir_mask
|= FS_ISDIR
;
83 fsnotify(old_dir
, old_dir_mask
, source
, FSNOTIFY_EVENT_INODE
, old_name
,
85 fsnotify(new_dir
, new_dir_mask
, source
, FSNOTIFY_EVENT_INODE
, new_name
,
89 fsnotify_link_count(target
);
92 fsnotify(source
, FS_MOVE_SELF
, moved
->d_inode
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
93 audit_inode_child(new_dir
, moved
, AUDIT_TYPE_CHILD_CREATE
);
97 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
99 static inline void fsnotify_inode_delete(struct inode
*inode
)
101 __fsnotify_inode_delete(inode
);
105 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
107 static inline void fsnotify_vfsmount_delete(struct vfsmount
*mnt
)
109 __fsnotify_vfsmount_delete(mnt
);
113 * fsnotify_nameremove - a filename was removed from a directory
115 static inline void fsnotify_nameremove(struct dentry
*dentry
, int isdir
)
117 __u32 mask
= FS_DELETE
;
122 fsnotify_parent(NULL
, dentry
, mask
);
126 * fsnotify_inoderemove - an inode is going away
128 static inline void fsnotify_inoderemove(struct inode
*inode
)
130 fsnotify(inode
, FS_DELETE_SELF
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
131 __fsnotify_inode_delete(inode
);
135 * fsnotify_create - 'name' was linked in
137 static inline void fsnotify_create(struct inode
*inode
, struct dentry
*dentry
)
139 audit_inode_child(inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
141 fsnotify(inode
, FS_CREATE
, dentry
->d_inode
, FSNOTIFY_EVENT_INODE
, dentry
->d_name
.name
, 0);
145 * fsnotify_link - new hardlink in 'inode' directory
146 * Note: We have to pass also the linked inode ptr as some filesystems leave
147 * new_dentry->d_inode NULL and instantiate inode pointer later
149 static inline void fsnotify_link(struct inode
*dir
, struct inode
*inode
, struct dentry
*new_dentry
)
151 fsnotify_link_count(inode
);
152 audit_inode_child(dir
, new_dentry
, AUDIT_TYPE_CHILD_CREATE
);
154 fsnotify(dir
, FS_CREATE
, inode
, FSNOTIFY_EVENT_INODE
, new_dentry
->d_name
.name
, 0);
158 * fsnotify_mkdir - directory 'name' was created
160 static inline void fsnotify_mkdir(struct inode
*inode
, struct dentry
*dentry
)
162 __u32 mask
= (FS_CREATE
| FS_ISDIR
);
163 struct inode
*d_inode
= dentry
->d_inode
;
165 audit_inode_child(inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
167 fsnotify(inode
, mask
, d_inode
, FSNOTIFY_EVENT_INODE
, dentry
->d_name
.name
, 0);
171 * fsnotify_access - file was read
173 static inline void fsnotify_access(struct file
*file
)
175 struct path
*path
= &file
->f_path
;
176 struct inode
*inode
= file_inode(file
);
177 __u32 mask
= FS_ACCESS
;
179 if (S_ISDIR(inode
->i_mode
))
182 if (!(file
->f_mode
& FMODE_NONOTIFY
)) {
183 fsnotify_parent(path
, NULL
, mask
);
184 fsnotify(inode
, mask
, path
, FSNOTIFY_EVENT_PATH
, NULL
, 0);
189 * fsnotify_modify - file was modified
191 static inline void fsnotify_modify(struct file
*file
)
193 struct path
*path
= &file
->f_path
;
194 struct inode
*inode
= file_inode(file
);
195 __u32 mask
= FS_MODIFY
;
197 if (S_ISDIR(inode
->i_mode
))
200 if (!(file
->f_mode
& FMODE_NONOTIFY
)) {
201 fsnotify_parent(path
, NULL
, mask
);
202 fsnotify(inode
, mask
, path
, FSNOTIFY_EVENT_PATH
, NULL
, 0);
207 * fsnotify_open - file was opened
209 static inline void fsnotify_open(struct file
*file
)
211 struct path
*path
= &file
->f_path
;
212 struct inode
*inode
= file_inode(file
);
213 __u32 mask
= FS_OPEN
;
215 if (S_ISDIR(inode
->i_mode
))
218 fsnotify_parent(path
, NULL
, mask
);
219 fsnotify(inode
, mask
, path
, FSNOTIFY_EVENT_PATH
, NULL
, 0);
223 * fsnotify_close - file was closed
225 static inline void fsnotify_close(struct file
*file
)
227 struct path
*path
= &file
->f_path
;
228 struct inode
*inode
= file_inode(file
);
229 fmode_t mode
= file
->f_mode
;
230 __u32 mask
= (mode
& FMODE_WRITE
) ? FS_CLOSE_WRITE
: FS_CLOSE_NOWRITE
;
232 if (S_ISDIR(inode
->i_mode
))
235 if (!(file
->f_mode
& FMODE_NONOTIFY
)) {
236 fsnotify_parent(path
, NULL
, mask
);
237 fsnotify(inode
, mask
, path
, FSNOTIFY_EVENT_PATH
, NULL
, 0);
242 * fsnotify_xattr - extended attributes were changed
244 static inline void fsnotify_xattr(struct dentry
*dentry
)
246 struct inode
*inode
= dentry
->d_inode
;
247 __u32 mask
= FS_ATTRIB
;
249 if (S_ISDIR(inode
->i_mode
))
252 fsnotify_parent(NULL
, dentry
, mask
);
253 fsnotify(inode
, mask
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
257 * fsnotify_change - notify_change event. file was modified and/or metadata
260 static inline void fsnotify_change(struct dentry
*dentry
, unsigned int ia_valid
)
262 struct inode
*inode
= dentry
->d_inode
;
265 if (ia_valid
& ATTR_UID
)
267 if (ia_valid
& ATTR_GID
)
269 if (ia_valid
& ATTR_SIZE
)
272 /* both times implies a utime(s) call */
273 if ((ia_valid
& (ATTR_ATIME
| ATTR_MTIME
)) == (ATTR_ATIME
| ATTR_MTIME
))
275 else if (ia_valid
& ATTR_ATIME
)
277 else if (ia_valid
& ATTR_MTIME
)
280 if (ia_valid
& ATTR_MODE
)
284 if (S_ISDIR(inode
->i_mode
))
287 fsnotify_parent(NULL
, dentry
, mask
);
288 fsnotify(inode
, mask
, inode
, FSNOTIFY_EVENT_INODE
, NULL
, 0);
292 #if defined(CONFIG_FSNOTIFY) /* notify helpers */
295 * fsnotify_oldname_init - save off the old filename before we change it
297 static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name
)
299 return kstrdup(name
, GFP_KERNEL
);
303 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
305 static inline void fsnotify_oldname_free(const unsigned char *old_name
)
310 #else /* CONFIG_FSNOTIFY */
312 static inline const char *fsnotify_oldname_init(const unsigned char *name
)
317 static inline void fsnotify_oldname_free(const unsigned char *old_name
)
321 #endif /* CONFIG_FSNOTIFY */
323 #endif /* _LINUX_FS_NOTIFY_H */