blob: d4b7c4ac72e6dd70e9bbda61158b36847e83bb2a [file] [log] [blame]
Robert Love0eeca282005-07-12 17:06:03 -04001#ifndef _LINUX_FS_NOTIFY_H
2#define _LINUX_FS_NOTIFY_H
3
4/*
5 * include/linux/fsnotify.h - generic hooks for filesystem notification, to
6 * reduce in-source duplication from both dnotify and inotify.
7 *
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.
10 *
11 * (C) Copyright 2005 Robert Love
12 */
13
14#ifdef __KERNEL__
15
16#include <linux/dnotify.h>
17#include <linux/inotify.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000018#include <linux/audit.h>
Robert Love0eeca282005-07-12 17:06:03 -040019
20/*
Nick Pigginc32ccd82006-03-25 03:07:09 -080021 * fsnotify_d_instantiate - instantiate a dentry for inode
22 * Called with dcache_lock held.
23 */
24static inline void fsnotify_d_instantiate(struct dentry *entry,
25 struct inode *inode)
26{
27 inotify_d_instantiate(entry, inode);
28}
29
30/*
31 * fsnotify_d_move - entry has been moved
32 * Called with dcache_lock and entry->d_lock held.
33 */
34static inline void fsnotify_d_move(struct dentry *entry)
35{
36 inotify_d_move(entry);
37}
38
39/*
Robert Love0eeca282005-07-12 17:06:03 -040040 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
41 */
42static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
43 const char *old_name, const char *new_name,
Al Viro5a190ae2007-06-07 12:19:32 -040044 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040045{
Al Viro5a190ae2007-06-07 12:19:32 -040046 struct inode *source = moved->d_inode;
Robert Love0eeca282005-07-12 17:06:03 -040047 u32 cookie = inotify_get_cookie();
48
49 if (old_dir == new_dir)
50 inode_dir_notify(old_dir, DN_RENAME);
51 else {
52 inode_dir_notify(old_dir, DN_DELETE);
53 inode_dir_notify(new_dir, DN_CREATE);
54 }
55
56 if (isdir)
57 isdir = IN_ISDIR;
Amy Griffis7c297722006-06-01 13:11:01 -070058 inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name,
59 source);
60 inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name,
61 source);
John McCutchan75449532005-08-01 11:00:45 -040062
63 if (target) {
Amy Griffis7c297722006-06-01 13:11:01 -070064 inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
John McCutchan75449532005-08-01 11:00:45 -040065 inotify_inode_is_dead(target);
66 }
John McCutchan89204c42005-08-15 12:13:28 -040067
68 if (source) {
Amy Griffis7c297722006-06-01 13:11:01 -070069 inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL);
John McCutchan89204c42005-08-15 12:13:28 -040070 }
Al Viro5a190ae2007-06-07 12:19:32 -040071 audit_inode_child(new_name, moved, new_dir);
Robert Love0eeca282005-07-12 17:06:03 -040072}
73
74/*
John McCutchan7a91bf72005-08-08 13:52:16 -040075 * fsnotify_nameremove - a filename was removed from a directory
76 */
77static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
78{
79 if (isdir)
80 isdir = IN_ISDIR;
81 dnotify_parent(dentry, DN_DELETE);
82 inotify_dentry_parent_queue_event(dentry, IN_DELETE|isdir, 0, dentry->d_name.name);
83}
84
85/*
86 * fsnotify_inoderemove - an inode is going away
87 */
88static inline void fsnotify_inoderemove(struct inode *inode)
89{
Amy Griffis7c297722006-06-01 13:11:01 -070090 inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL, NULL);
John McCutchan7a91bf72005-08-08 13:52:16 -040091 inotify_inode_is_dead(inode);
92}
93
94/*
Jan Karaece95912008-02-06 01:37:13 -080095 * fsnotify_link_count - inode's link count changed
96 */
97static inline void fsnotify_link_count(struct inode *inode)
98{
99 inotify_inode_queue_event(inode, IN_ATTRIB, 0, NULL, NULL);
100}
101
102/*
Robert Love0eeca282005-07-12 17:06:03 -0400103 * fsnotify_create - 'name' was linked in
104 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000105static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400106{
107 inode_dir_notify(inode, DN_CREATE);
Amy Griffis7c297722006-06-01 13:11:01 -0700108 inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name,
109 dentry->d_inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400110 audit_inode_child(dentry->d_name.name, dentry, inode);
Robert Love0eeca282005-07-12 17:06:03 -0400111}
112
113/*
Jan Karaece95912008-02-06 01:37:13 -0800114 * fsnotify_link - new hardlink in 'inode' directory
115 * Note: We have to pass also the linked inode ptr as some filesystems leave
116 * new_dentry->d_inode NULL and instantiate inode pointer later
117 */
118static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
119{
120 inode_dir_notify(dir, DN_CREATE);
121 inotify_inode_queue_event(dir, IN_CREATE, 0, new_dentry->d_name.name,
122 inode);
123 fsnotify_link_count(inode);
124 audit_inode_child(new_dentry->d_name.name, new_dentry, dir);
125}
126
127/*
Robert Love0eeca282005-07-12 17:06:03 -0400128 * fsnotify_mkdir - directory 'name' was created
129 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000130static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400131{
132 inode_dir_notify(inode, DN_CREATE);
Amy Griffisf38aa942005-11-03 15:57:06 +0000133 inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0,
Amy Griffis7c297722006-06-01 13:11:01 -0700134 dentry->d_name.name, dentry->d_inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400135 audit_inode_child(dentry->d_name.name, dentry, inode);
Robert Love0eeca282005-07-12 17:06:03 -0400136}
137
138/*
139 * fsnotify_access - file was read
140 */
141static inline void fsnotify_access(struct dentry *dentry)
142{
143 struct inode *inode = dentry->d_inode;
144 u32 mask = IN_ACCESS;
145
146 if (S_ISDIR(inode->i_mode))
147 mask |= IN_ISDIR;
148
149 dnotify_parent(dentry, DN_ACCESS);
150 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
Amy Griffis7c297722006-06-01 13:11:01 -0700151 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400152}
153
154/*
155 * fsnotify_modify - file was modified
156 */
157static inline void fsnotify_modify(struct dentry *dentry)
158{
159 struct inode *inode = dentry->d_inode;
160 u32 mask = IN_MODIFY;
161
162 if (S_ISDIR(inode->i_mode))
163 mask |= IN_ISDIR;
164
165 dnotify_parent(dentry, DN_MODIFY);
166 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
Amy Griffis7c297722006-06-01 13:11:01 -0700167 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400168}
169
170/*
171 * fsnotify_open - file was opened
172 */
173static inline void fsnotify_open(struct dentry *dentry)
174{
175 struct inode *inode = dentry->d_inode;
176 u32 mask = IN_OPEN;
177
178 if (S_ISDIR(inode->i_mode))
179 mask |= IN_ISDIR;
180
Robert Love0eeca282005-07-12 17:06:03 -0400181 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
Amy Griffis7c297722006-06-01 13:11:01 -0700182 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400183}
184
185/*
186 * fsnotify_close - file was closed
187 */
188static inline void fsnotify_close(struct file *file)
189{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800190 struct dentry *dentry = file->f_path.dentry;
Robert Love0eeca282005-07-12 17:06:03 -0400191 struct inode *inode = dentry->d_inode;
192 const char *name = dentry->d_name.name;
193 mode_t mode = file->f_mode;
194 u32 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
195
196 if (S_ISDIR(inode->i_mode))
197 mask |= IN_ISDIR;
198
199 inotify_dentry_parent_queue_event(dentry, mask, 0, name);
Amy Griffis7c297722006-06-01 13:11:01 -0700200 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400201}
202
203/*
204 * fsnotify_xattr - extended attributes were changed
205 */
206static inline void fsnotify_xattr(struct dentry *dentry)
207{
208 struct inode *inode = dentry->d_inode;
209 u32 mask = IN_ATTRIB;
210
211 if (S_ISDIR(inode->i_mode))
212 mask |= IN_ISDIR;
213
214 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
Amy Griffis7c297722006-06-01 13:11:01 -0700215 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400216}
217
218/*
219 * fsnotify_change - notify_change event. file was modified and/or metadata
220 * was changed.
221 */
222static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
223{
224 struct inode *inode = dentry->d_inode;
225 int dn_mask = 0;
226 u32 in_mask = 0;
227
228 if (ia_valid & ATTR_UID) {
229 in_mask |= IN_ATTRIB;
230 dn_mask |= DN_ATTRIB;
231 }
232 if (ia_valid & ATTR_GID) {
233 in_mask |= IN_ATTRIB;
234 dn_mask |= DN_ATTRIB;
235 }
236 if (ia_valid & ATTR_SIZE) {
237 in_mask |= IN_MODIFY;
238 dn_mask |= DN_MODIFY;
239 }
240 /* both times implies a utime(s) call */
241 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
242 {
243 in_mask |= IN_ATTRIB;
244 dn_mask |= DN_ATTRIB;
245 } else if (ia_valid & ATTR_ATIME) {
246 in_mask |= IN_ACCESS;
247 dn_mask |= DN_ACCESS;
248 } else if (ia_valid & ATTR_MTIME) {
249 in_mask |= IN_MODIFY;
250 dn_mask |= DN_MODIFY;
251 }
252 if (ia_valid & ATTR_MODE) {
253 in_mask |= IN_ATTRIB;
254 dn_mask |= DN_ATTRIB;
255 }
256
257 if (dn_mask)
258 dnotify_parent(dentry, dn_mask);
259 if (in_mask) {
260 if (S_ISDIR(inode->i_mode))
261 in_mask |= IN_ISDIR;
Amy Griffis7c297722006-06-01 13:11:01 -0700262 inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400263 inotify_dentry_parent_queue_event(dentry, in_mask, 0,
264 dentry->d_name.name);
265 }
266}
267
268#ifdef CONFIG_INOTIFY /* inotify helpers */
269
270/*
271 * fsnotify_oldname_init - save off the old filename before we change it
272 */
273static inline const char *fsnotify_oldname_init(const char *name)
274{
275 return kstrdup(name, GFP_KERNEL);
276}
277
278/*
279 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
280 */
281static inline void fsnotify_oldname_free(const char *old_name)
282{
283 kfree(old_name);
284}
285
286#else /* CONFIG_INOTIFY */
287
288static inline const char *fsnotify_oldname_init(const char *name)
289{
290 return NULL;
291}
292
293static inline void fsnotify_oldname_free(const char *old_name)
294{
295}
296
297#endif /* ! CONFIG_INOTIFY */
298
299#endif /* __KERNEL__ */
300
301#endif /* _LINUX_FS_NOTIFY_H */