blob: 6a662ed0bc8ac1d431c17e13f37435ae2f255bbb [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
Robert Love0eeca282005-07-12 17:06:03 -040014#include <linux/dnotify.h>
15#include <linux/inotify.h>
Eric Paris90586522009-05-21 17:01:20 -040016#include <linux/fsnotify_backend.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000017#include <linux/audit.h>
Robert Love0eeca282005-07-12 17:06:03 -040018
19/*
Nick Pigginc32ccd82006-03-25 03:07:09 -080020 * fsnotify_d_instantiate - instantiate a dentry for inode
21 * Called with dcache_lock held.
22 */
23static inline void fsnotify_d_instantiate(struct dentry *entry,
24 struct inode *inode)
25{
Eric Parisc28f7e52009-05-21 17:01:29 -040026 __fsnotify_d_instantiate(entry, inode);
27
Nick Pigginc32ccd82006-03-25 03:07:09 -080028 inotify_d_instantiate(entry, inode);
29}
30
Eric Parisc28f7e52009-05-21 17:01:29 -040031/* Notify this dentry's parent about a child's events. */
32static inline void fsnotify_parent(struct dentry *dentry, __u32 mask)
33{
34 __fsnotify_parent(dentry, mask);
35
36 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
37}
38
Nick Pigginc32ccd82006-03-25 03:07:09 -080039/*
40 * fsnotify_d_move - entry has been moved
41 * Called with dcache_lock and entry->d_lock held.
42 */
43static inline void fsnotify_d_move(struct dentry *entry)
44{
Eric Parisc28f7e52009-05-21 17:01:29 -040045 /*
46 * On move we need to update entry->d_flags to indicate if the new parent
47 * cares about events from this entry.
48 */
49 __fsnotify_update_dcache_flags(entry);
50
Nick Pigginc32ccd82006-03-25 03:07:09 -080051 inotify_d_move(entry);
52}
53
54/*
Eric Paris90586522009-05-21 17:01:20 -040055 * fsnotify_link_count - inode's link count changed
56 */
57static inline void fsnotify_link_count(struct inode *inode)
58{
59 inotify_inode_queue_event(inode, IN_ATTRIB, 0, NULL, NULL);
60
61 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE);
62}
63
64/*
Robert Love0eeca282005-07-12 17:06:03 -040065 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
66 */
67static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
68 const char *old_name, const char *new_name,
Al Viro5a190ae2007-06-07 12:19:32 -040069 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040070{
Al Viro5a190ae2007-06-07 12:19:32 -040071 struct inode *source = moved->d_inode;
Robert Love0eeca282005-07-12 17:06:03 -040072 u32 cookie = inotify_get_cookie();
Eric Paris90586522009-05-21 17:01:20 -040073 __u32 old_dir_mask = 0;
74 __u32 new_dir_mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -040075
Eric Paris90586522009-05-21 17:01:20 -040076 if (old_dir == new_dir) {
Robert Love0eeca282005-07-12 17:06:03 -040077 inode_dir_notify(old_dir, DN_RENAME);
Eric Paris90586522009-05-21 17:01:20 -040078 old_dir_mask = FS_DN_RENAME;
79 } else {
Robert Love0eeca282005-07-12 17:06:03 -040080 inode_dir_notify(old_dir, DN_DELETE);
Eric Paris90586522009-05-21 17:01:20 -040081 old_dir_mask = FS_DELETE;
Robert Love0eeca282005-07-12 17:06:03 -040082 inode_dir_notify(new_dir, DN_CREATE);
Eric Paris90586522009-05-21 17:01:20 -040083 new_dir_mask = FS_CREATE;
Robert Love0eeca282005-07-12 17:06:03 -040084 }
85
Eric Paris90586522009-05-21 17:01:20 -040086 if (isdir) {
Robert Love0eeca282005-07-12 17:06:03 -040087 isdir = IN_ISDIR;
Eric Paris90586522009-05-21 17:01:20 -040088 old_dir_mask |= FS_IN_ISDIR;
89 new_dir_mask |= FS_IN_ISDIR;
90 }
91
92 old_dir_mask |= FS_MOVED_FROM;
93 new_dir_mask |= FS_MOVED_TO;
94
Amy Griffis7c297722006-06-01 13:11:01 -070095 inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name,
96 source);
97 inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name,
98 source);
John McCutchan75449532005-08-01 11:00:45 -040099
Eric Paris90586522009-05-21 17:01:20 -0400100 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE);
101 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE);
102
John McCutchan75449532005-08-01 11:00:45 -0400103 if (target) {
Amy Griffis7c297722006-06-01 13:11:01 -0700104 inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
John McCutchan75449532005-08-01 11:00:45 -0400105 inotify_inode_is_dead(target);
Eric Paris90586522009-05-21 17:01:20 -0400106
107 /* this is really a link_count change not a removal */
108 fsnotify_link_count(target);
John McCutchan75449532005-08-01 11:00:45 -0400109 }
John McCutchan89204c42005-08-15 12:13:28 -0400110
111 if (source) {
Amy Griffis7c297722006-06-01 13:11:01 -0700112 inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL);
Eric Paris90586522009-05-21 17:01:20 -0400113 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE);
John McCutchan89204c42005-08-15 12:13:28 -0400114 }
Al Viro5a190ae2007-06-07 12:19:32 -0400115 audit_inode_child(new_name, moved, new_dir);
Robert Love0eeca282005-07-12 17:06:03 -0400116}
117
118/*
Eric Paris3be25f42009-05-21 17:01:26 -0400119 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
120 */
121static inline void fsnotify_inode_delete(struct inode *inode)
122{
123 __fsnotify_inode_delete(inode);
124}
125
126/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400127 * fsnotify_nameremove - a filename was removed from a directory
128 */
129static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
130{
Eric Paris90586522009-05-21 17:01:20 -0400131 __u32 mask = FS_DELETE;
132
John McCutchan7a91bf72005-08-08 13:52:16 -0400133 if (isdir)
Eric Paris90586522009-05-21 17:01:20 -0400134 mask |= FS_IN_ISDIR;
John McCutchan7a91bf72005-08-08 13:52:16 -0400135 dnotify_parent(dentry, DN_DELETE);
Eric Parisc28f7e52009-05-21 17:01:29 -0400136
137 fsnotify_parent(dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400138}
139
140/*
141 * fsnotify_inoderemove - an inode is going away
142 */
143static inline void fsnotify_inoderemove(struct inode *inode)
144{
Amy Griffis7c297722006-06-01 13:11:01 -0700145 inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL, NULL);
John McCutchan7a91bf72005-08-08 13:52:16 -0400146 inotify_inode_is_dead(inode);
John McCutchan7a91bf72005-08-08 13:52:16 -0400147
Eric Paris90586522009-05-21 17:01:20 -0400148 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE);
Eric Paris3be25f42009-05-21 17:01:26 -0400149 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800150}
151
152/*
Robert Love0eeca282005-07-12 17:06:03 -0400153 * fsnotify_create - 'name' was linked in
154 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000155static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400156{
157 inode_dir_notify(inode, DN_CREATE);
Amy Griffis7c297722006-06-01 13:11:01 -0700158 inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name,
159 dentry->d_inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400160 audit_inode_child(dentry->d_name.name, dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400161
162 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE);
Robert Love0eeca282005-07-12 17:06:03 -0400163}
164
165/*
Jan Karaece95912008-02-06 01:37:13 -0800166 * fsnotify_link - new hardlink in 'inode' directory
167 * Note: We have to pass also the linked inode ptr as some filesystems leave
168 * new_dentry->d_inode NULL and instantiate inode pointer later
169 */
170static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
171{
172 inode_dir_notify(dir, DN_CREATE);
173 inotify_inode_queue_event(dir, IN_CREATE, 0, new_dentry->d_name.name,
174 inode);
175 fsnotify_link_count(inode);
176 audit_inode_child(new_dentry->d_name.name, new_dentry, dir);
Eric Paris90586522009-05-21 17:01:20 -0400177
178 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE);
Jan Karaece95912008-02-06 01:37:13 -0800179}
180
181/*
Robert Love0eeca282005-07-12 17:06:03 -0400182 * fsnotify_mkdir - directory 'name' was created
183 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000184static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400185{
Eric Paris90586522009-05-21 17:01:20 -0400186 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
187 struct inode *d_inode = dentry->d_inode;
188
Robert Love0eeca282005-07-12 17:06:03 -0400189 inode_dir_notify(inode, DN_CREATE);
Eric Paris90586522009-05-21 17:01:20 -0400190 inotify_inode_queue_event(inode, mask, 0, dentry->d_name.name, d_inode);
Al Viro5a190ae2007-06-07 12:19:32 -0400191 audit_inode_child(dentry->d_name.name, dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400192
193 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE);
Robert Love0eeca282005-07-12 17:06:03 -0400194}
195
196/*
197 * fsnotify_access - file was read
198 */
199static inline void fsnotify_access(struct dentry *dentry)
200{
201 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400202 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400203
204 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400205 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400206
207 dnotify_parent(dentry, DN_ACCESS);
Amy Griffis7c297722006-06-01 13:11:01 -0700208 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
Eric Paris90586522009-05-21 17:01:20 -0400209
Eric Parisc28f7e52009-05-21 17:01:29 -0400210 fsnotify_parent(dentry, mask);
Eric Paris90586522009-05-21 17:01:20 -0400211 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
Robert Love0eeca282005-07-12 17:06:03 -0400212}
213
214/*
215 * fsnotify_modify - file was modified
216 */
217static inline void fsnotify_modify(struct dentry *dentry)
218{
219 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400220 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400221
222 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400223 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400224
225 dnotify_parent(dentry, DN_MODIFY);
Amy Griffis7c297722006-06-01 13:11:01 -0700226 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
Eric Paris90586522009-05-21 17:01:20 -0400227
Eric Parisc28f7e52009-05-21 17:01:29 -0400228 fsnotify_parent(dentry, mask);
Eric Paris90586522009-05-21 17:01:20 -0400229 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
Robert Love0eeca282005-07-12 17:06:03 -0400230}
231
232/*
233 * fsnotify_open - file was opened
234 */
235static inline void fsnotify_open(struct dentry *dentry)
236{
237 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400238 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400239
240 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400241 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400242
Amy Griffis7c297722006-06-01 13:11:01 -0700243 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
Eric Paris90586522009-05-21 17:01:20 -0400244
Eric Parisc28f7e52009-05-21 17:01:29 -0400245 fsnotify_parent(dentry, mask);
Eric Paris90586522009-05-21 17:01:20 -0400246 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
Robert Love0eeca282005-07-12 17:06:03 -0400247}
248
249/*
250 * fsnotify_close - file was closed
251 */
252static inline void fsnotify_close(struct file *file)
253{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800254 struct dentry *dentry = file->f_path.dentry;
Robert Love0eeca282005-07-12 17:06:03 -0400255 struct inode *inode = dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400256 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400257 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400258
259 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400260 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400261
Amy Griffis7c297722006-06-01 13:11:01 -0700262 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
Eric Paris90586522009-05-21 17:01:20 -0400263
Eric Parisc28f7e52009-05-21 17:01:29 -0400264 fsnotify_parent(dentry, mask);
Eric Paris90586522009-05-21 17:01:20 -0400265 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE);
Robert Love0eeca282005-07-12 17:06:03 -0400266}
267
268/*
269 * fsnotify_xattr - extended attributes were changed
270 */
271static inline void fsnotify_xattr(struct dentry *dentry)
272{
273 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400274 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400275
276 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400277 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400278
Amy Griffis7c297722006-06-01 13:11:01 -0700279 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
Eric Paris90586522009-05-21 17:01:20 -0400280
Eric Parisc28f7e52009-05-21 17:01:29 -0400281 fsnotify_parent(dentry, mask);
Eric Paris90586522009-05-21 17:01:20 -0400282 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
Robert Love0eeca282005-07-12 17:06:03 -0400283}
284
285/*
286 * fsnotify_change - notify_change event. file was modified and/or metadata
287 * was changed.
288 */
289static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
290{
291 struct inode *inode = dentry->d_inode;
292 int dn_mask = 0;
Eric Paris90586522009-05-21 17:01:20 -0400293 __u32 in_mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400294
295 if (ia_valid & ATTR_UID) {
Eric Paris90586522009-05-21 17:01:20 -0400296 in_mask |= FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400297 dn_mask |= DN_ATTRIB;
298 }
299 if (ia_valid & ATTR_GID) {
Eric Paris90586522009-05-21 17:01:20 -0400300 in_mask |= FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400301 dn_mask |= DN_ATTRIB;
302 }
303 if (ia_valid & ATTR_SIZE) {
Eric Paris90586522009-05-21 17:01:20 -0400304 in_mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400305 dn_mask |= DN_MODIFY;
306 }
307 /* both times implies a utime(s) call */
308 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
309 {
Eric Paris90586522009-05-21 17:01:20 -0400310 in_mask |= FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400311 dn_mask |= DN_ATTRIB;
312 } else if (ia_valid & ATTR_ATIME) {
Eric Paris90586522009-05-21 17:01:20 -0400313 in_mask |= FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400314 dn_mask |= DN_ACCESS;
315 } else if (ia_valid & ATTR_MTIME) {
Eric Paris90586522009-05-21 17:01:20 -0400316 in_mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400317 dn_mask |= DN_MODIFY;
318 }
319 if (ia_valid & ATTR_MODE) {
Eric Paris90586522009-05-21 17:01:20 -0400320 in_mask |= FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400321 dn_mask |= DN_ATTRIB;
322 }
323
324 if (dn_mask)
325 dnotify_parent(dentry, dn_mask);
326 if (in_mask) {
327 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400328 in_mask |= FS_IN_ISDIR;
Amy Griffis7c297722006-06-01 13:11:01 -0700329 inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
Eric Parisc28f7e52009-05-21 17:01:29 -0400330
331 fsnotify_parent(dentry, in_mask);
Eric Paris90586522009-05-21 17:01:20 -0400332 fsnotify(inode, in_mask, inode, FSNOTIFY_EVENT_INODE);
Robert Love0eeca282005-07-12 17:06:03 -0400333 }
334}
335
Eric Paris90586522009-05-21 17:01:20 -0400336#if defined(CONFIG_INOTIFY) || defined(CONFIG_FSNOTIFY) /* notify helpers */
Robert Love0eeca282005-07-12 17:06:03 -0400337
338/*
339 * fsnotify_oldname_init - save off the old filename before we change it
340 */
341static inline const char *fsnotify_oldname_init(const char *name)
342{
343 return kstrdup(name, GFP_KERNEL);
344}
345
346/*
347 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
348 */
349static inline void fsnotify_oldname_free(const char *old_name)
350{
351 kfree(old_name);
352}
353
Eric Paris90586522009-05-21 17:01:20 -0400354#else /* CONFIG_INOTIFY || CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400355
356static inline const char *fsnotify_oldname_init(const char *name)
357{
358 return NULL;
359}
360
361static inline void fsnotify_oldname_free(const char *old_name)
362{
363}
364
365#endif /* ! CONFIG_INOTIFY */
366
Robert Love0eeca282005-07-12 17:06:03 -0400367#endif /* _LINUX_FS_NOTIFY_H */