blob: 59d0df43ff9d5ef649fd109228ded45f37579d84 [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
Eric Paris90586522009-05-21 17:01:20 -040014#include <linux/fsnotify_backend.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000015#include <linux/audit.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Robert Love0eeca282005-07-12 17:06:03 -040017
18/*
Nick Pigginc32ccd82006-03-25 03:07:09 -080019 * fsnotify_d_instantiate - instantiate a dentry for inode
20 * Called with dcache_lock held.
21 */
Eric Parise61ce862009-12-17 21:24:24 -050022static inline void fsnotify_d_instantiate(struct dentry *dentry,
23 struct inode *inode)
Nick Pigginc32ccd82006-03-25 03:07:09 -080024{
Eric Parise61ce862009-12-17 21:24:24 -050025 __fsnotify_d_instantiate(dentry, inode);
Nick Pigginc32ccd82006-03-25 03:07:09 -080026}
27
Eric Parisc28f7e52009-05-21 17:01:29 -040028/* Notify this dentry's parent about a child's events. */
Linus Torvalds20696012010-08-12 14:23:04 -070029static inline void fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
Eric Parisc28f7e52009-05-21 17:01:29 -040030{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050031 if (!dentry)
Linus Torvalds20696012010-08-12 14:23:04 -070032 dentry = path->dentry;
Eric Paris28c60e32009-12-17 21:24:21 -050033
Linus Torvalds20696012010-08-12 14:23:04 -070034 __fsnotify_parent(path, dentry, mask);
Eric Parisc28f7e52009-05-21 17:01:29 -040035}
36
Eric Parisc4ec54b2009-12-17 21:24:34 -050037/* simple call site for access decisions */
38static inline int fsnotify_perm(struct file *file, int mask)
39{
Linus Torvalds20696012010-08-12 14:23:04 -070040 struct path *path = &file->f_path;
41 struct inode *inode = path->dentry->d_inode;
Eric Parisfb1cfb82010-05-12 11:42:29 -040042 __u32 fsnotify_mask = 0;
Eric Parisc4ec54b2009-12-17 21:24:34 -050043
44 if (file->f_mode & FMODE_NONOTIFY)
45 return 0;
46 if (!(mask & (MAY_READ | MAY_OPEN)))
47 return 0;
Eric Parisc4ec54b2009-12-17 21:24:34 -050048 if (mask & MAY_OPEN)
49 fsnotify_mask = FS_OPEN_PERM;
Eric Parisfb1cfb82010-05-12 11:42:29 -040050 else if (mask & MAY_READ)
51 fsnotify_mask = FS_ACCESS_PERM;
52 else
53 BUG();
Eric Parisc4ec54b2009-12-17 21:24:34 -050054
Linus Torvalds20696012010-08-12 14:23:04 -070055 return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisc4ec54b2009-12-17 21:24:34 -050056}
57
Nick Pigginc32ccd82006-03-25 03:07:09 -080058/*
Eric Parise61ce862009-12-17 21:24:24 -050059 * fsnotify_d_move - dentry has been moved
60 * Called with dcache_lock and dentry->d_lock held.
Nick Pigginc32ccd82006-03-25 03:07:09 -080061 */
Eric Parise61ce862009-12-17 21:24:24 -050062static inline void fsnotify_d_move(struct dentry *dentry)
Nick Pigginc32ccd82006-03-25 03:07:09 -080063{
Eric Parisc28f7e52009-05-21 17:01:29 -040064 /*
Eric Parise61ce862009-12-17 21:24:24 -050065 * On move we need to update dentry->d_flags to indicate if the new parent
66 * cares about events from this dentry.
Eric Parisc28f7e52009-05-21 17:01:29 -040067 */
Eric Parise61ce862009-12-17 21:24:24 -050068 __fsnotify_update_dcache_flags(dentry);
Nick Pigginc32ccd82006-03-25 03:07:09 -080069}
70
71/*
Eric Paris90586522009-05-21 17:01:20 -040072 * fsnotify_link_count - inode's link count changed
73 */
74static inline void fsnotify_link_count(struct inode *inode)
75{
Eric Paris47882c62009-05-21 17:01:47 -040076 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040077}
78
79/*
Robert Love0eeca282005-07-12 17:06:03 -040080 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
81 */
82static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Eric Paris59b0df22010-02-08 12:53:52 -050083 const unsigned char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040084 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040085{
Al Viro5a190ae2007-06-07 12:19:32 -040086 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040087 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040088 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
89 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Eric Paris59b0df22010-02-08 12:53:52 -050090 const unsigned char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040091
Eric Parisff52cc22009-06-11 11:09:47 -040092 if (old_dir == new_dir)
93 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040094
Eric Paris90586522009-05-21 17:01:20 -040095 if (isdir) {
Eric Paris90586522009-05-21 17:01:20 -040096 old_dir_mask |= FS_IN_ISDIR;
97 new_dir_mask |= FS_IN_ISDIR;
98 }
99
Eric Paris47882c62009-05-21 17:01:47 -0400100 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
101 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -0400102
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500103 if (target)
Eric Paris90586522009-05-21 17:01:20 -0400104 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -0400105
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500106 if (source)
Eric Paris47882c62009-05-21 17:01:47 -0400107 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Al Virocccc6bb2009-12-25 05:07:33 -0500108 audit_inode_child(moved, new_dir);
Robert Love0eeca282005-07-12 17:06:03 -0400109}
110
111/*
Eric Paris3be25f42009-05-21 17:01:26 -0400112 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
113 */
114static inline void fsnotify_inode_delete(struct inode *inode)
115{
116 __fsnotify_inode_delete(inode);
117}
118
119/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500120 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
121 */
122static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
123{
124 __fsnotify_vfsmount_delete(mnt);
125}
126
127/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400128 * fsnotify_nameremove - a filename was removed from a directory
129 */
130static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
131{
Eric Paris90586522009-05-21 17:01:20 -0400132 __u32 mask = FS_DELETE;
133
John McCutchan7a91bf72005-08-08 13:52:16 -0400134 if (isdir)
Eric Paris90586522009-05-21 17:01:20 -0400135 mask |= FS_IN_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400136
Eric Paris28c60e32009-12-17 21:24:21 -0500137 fsnotify_parent(NULL, 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{
Eric Paris47882c62009-05-21 17:01:47 -0400145 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400146 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800147}
148
149/*
Robert Love0eeca282005-07-12 17:06:03 -0400150 * fsnotify_create - 'name' was linked in
151 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000152static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400153{
Al Virocccc6bb2009-12-25 05:07:33 -0500154 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400155
Eric Paris47882c62009-05-21 17:01:47 -0400156 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400157}
158
159/*
Jan Karaece95912008-02-06 01:37:13 -0800160 * fsnotify_link - new hardlink in 'inode' directory
161 * Note: We have to pass also the linked inode ptr as some filesystems leave
162 * new_dentry->d_inode NULL and instantiate inode pointer later
163 */
164static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
165{
Jan Karaece95912008-02-06 01:37:13 -0800166 fsnotify_link_count(inode);
Al Virocccc6bb2009-12-25 05:07:33 -0500167 audit_inode_child(new_dentry, dir);
Eric Paris90586522009-05-21 17:01:20 -0400168
Eric Paris47882c62009-05-21 17:01:47 -0400169 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800170}
171
172/*
Robert Love0eeca282005-07-12 17:06:03 -0400173 * fsnotify_mkdir - directory 'name' was created
174 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000175static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400176{
Eric Paris90586522009-05-21 17:01:20 -0400177 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
178 struct inode *d_inode = dentry->d_inode;
179
Al Virocccc6bb2009-12-25 05:07:33 -0500180 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400181
Eric Paris47882c62009-05-21 17:01:47 -0400182 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400183}
184
185/*
186 * fsnotify_access - file was read
187 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500188static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400189{
Linus Torvalds20696012010-08-12 14:23:04 -0700190 struct path *path = &file->f_path;
191 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400192 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400193
194 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400195 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400196
Eric Parisecf081d2009-12-17 21:24:25 -0500197 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700198 fsnotify_parent(path, NULL, mask);
199 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500200 }
Robert Love0eeca282005-07-12 17:06:03 -0400201}
202
203/*
204 * fsnotify_modify - file was modified
205 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500206static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400207{
Linus Torvalds20696012010-08-12 14:23:04 -0700208 struct path *path = &file->f_path;
209 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400210 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400211
212 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400213 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400214
Eric Parisecf081d2009-12-17 21:24:25 -0500215 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700216 fsnotify_parent(path, NULL, mask);
217 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500218 }
Robert Love0eeca282005-07-12 17:06:03 -0400219}
220
221/*
222 * fsnotify_open - file was opened
223 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500224static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400225{
Linus Torvalds20696012010-08-12 14:23:04 -0700226 struct path *path = &file->f_path;
227 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400228 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400229
230 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400231 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400232
Eric Parisecf081d2009-12-17 21:24:25 -0500233 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700234 fsnotify_parent(path, NULL, mask);
235 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500236 }
Robert Love0eeca282005-07-12 17:06:03 -0400237}
238
239/*
240 * fsnotify_close - file was closed
241 */
242static inline void fsnotify_close(struct file *file)
243{
Linus Torvalds20696012010-08-12 14:23:04 -0700244 struct path *path = &file->f_path;
Eric Paris28c60e32009-12-17 21:24:21 -0500245 struct inode *inode = file->f_path.dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400246 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400247 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400248
249 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400250 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400251
Eric Parisecf081d2009-12-17 21:24:25 -0500252 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700253 fsnotify_parent(path, NULL, mask);
254 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500255 }
Robert Love0eeca282005-07-12 17:06:03 -0400256}
257
258/*
259 * fsnotify_xattr - extended attributes were changed
260 */
261static inline void fsnotify_xattr(struct dentry *dentry)
262{
263 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400264 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400265
266 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400267 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400268
Eric Paris28c60e32009-12-17 21:24:21 -0500269 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400270 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400271}
272
273/*
274 * fsnotify_change - notify_change event. file was modified and/or metadata
275 * was changed.
276 */
277static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
278{
279 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400280 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400281
Eric Paris3c5119c2009-05-21 17:01:33 -0400282 if (ia_valid & ATTR_UID)
283 mask |= FS_ATTRIB;
284 if (ia_valid & ATTR_GID)
285 mask |= FS_ATTRIB;
286 if (ia_valid & ATTR_SIZE)
287 mask |= FS_MODIFY;
288
Robert Love0eeca282005-07-12 17:06:03 -0400289 /* both times implies a utime(s) call */
290 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400291 mask |= FS_ATTRIB;
292 else if (ia_valid & ATTR_ATIME)
293 mask |= FS_ACCESS;
294 else if (ia_valid & ATTR_MTIME)
295 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400296
Eric Paris3c5119c2009-05-21 17:01:33 -0400297 if (ia_valid & ATTR_MODE)
298 mask |= FS_ATTRIB;
299
300 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400301 if (S_ISDIR(inode->i_mode))
Eric Paris3c5119c2009-05-21 17:01:33 -0400302 mask |= FS_IN_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500303
304 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400305 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400306 }
307}
308
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500309#if defined(CONFIG_FSNOTIFY) /* notify helpers */
Robert Love0eeca282005-07-12 17:06:03 -0400310
311/*
312 * fsnotify_oldname_init - save off the old filename before we change it
313 */
Eric Paris59b0df22010-02-08 12:53:52 -0500314static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name)
Robert Love0eeca282005-07-12 17:06:03 -0400315{
316 return kstrdup(name, GFP_KERNEL);
317}
318
319/*
320 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
321 */
Eric Paris59b0df22010-02-08 12:53:52 -0500322static inline void fsnotify_oldname_free(const unsigned char *old_name)
Robert Love0eeca282005-07-12 17:06:03 -0400323{
324 kfree(old_name);
325}
326
Eric Paris28c60e32009-12-17 21:24:21 -0500327#else /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400328
Eric Paris59b0df22010-02-08 12:53:52 -0500329static inline const char *fsnotify_oldname_init(const unsigned char *name)
Robert Love0eeca282005-07-12 17:06:03 -0400330{
331 return NULL;
332}
333
Eric Paris59b0df22010-02-08 12:53:52 -0500334static inline void fsnotify_oldname_free(const unsigned char *old_name)
Robert Love0eeca282005-07-12 17:06:03 -0400335{
336}
337
Eric Paris28c60e32009-12-17 21:24:21 -0500338#endif /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400339
Robert Love0eeca282005-07-12 17:06:03 -0400340#endif /* _LINUX_FS_NOTIFY_H */