blob: 5c185fa27089f03b4e9194445ee7bfd842f125c6 [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. */
Eric Paris52420392010-10-28 17:21:56 -040029static inline int 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
Eric Paris52420392010-10-28 17:21:56 -040034 return __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 Paris52420392010-10-28 17:21:56 -040043 int ret;
Eric Parisc4ec54b2009-12-17 21:24:34 -050044
45 if (file->f_mode & FMODE_NONOTIFY)
46 return 0;
47 if (!(mask & (MAY_READ | MAY_OPEN)))
48 return 0;
Eric Parisc4ec54b2009-12-17 21:24:34 -050049 if (mask & MAY_OPEN)
50 fsnotify_mask = FS_OPEN_PERM;
Eric Parisfb1cfb82010-05-12 11:42:29 -040051 else if (mask & MAY_READ)
52 fsnotify_mask = FS_ACCESS_PERM;
53 else
54 BUG();
Eric Parisc4ec54b2009-12-17 21:24:34 -050055
Eric Paris52420392010-10-28 17:21:56 -040056 ret = fsnotify_parent(path, NULL, fsnotify_mask);
57 if (ret)
58 return ret;
59
Linus Torvalds20696012010-08-12 14:23:04 -070060 return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisc4ec54b2009-12-17 21:24:34 -050061}
62
Nick Pigginc32ccd82006-03-25 03:07:09 -080063/*
Eric Parise61ce862009-12-17 21:24:24 -050064 * fsnotify_d_move - dentry has been moved
65 * Called with dcache_lock and dentry->d_lock held.
Nick Pigginc32ccd82006-03-25 03:07:09 -080066 */
Eric Parise61ce862009-12-17 21:24:24 -050067static inline void fsnotify_d_move(struct dentry *dentry)
Nick Pigginc32ccd82006-03-25 03:07:09 -080068{
Eric Parisc28f7e52009-05-21 17:01:29 -040069 /*
Eric Parise61ce862009-12-17 21:24:24 -050070 * On move we need to update dentry->d_flags to indicate if the new parent
71 * cares about events from this dentry.
Eric Parisc28f7e52009-05-21 17:01:29 -040072 */
Eric Parise61ce862009-12-17 21:24:24 -050073 __fsnotify_update_dcache_flags(dentry);
Nick Pigginc32ccd82006-03-25 03:07:09 -080074}
75
76/*
Eric Paris90586522009-05-21 17:01:20 -040077 * fsnotify_link_count - inode's link count changed
78 */
79static inline void fsnotify_link_count(struct inode *inode)
80{
Eric Paris47882c62009-05-21 17:01:47 -040081 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040082}
83
84/*
Robert Love0eeca282005-07-12 17:06:03 -040085 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
86 */
87static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Eric Paris59b0df22010-02-08 12:53:52 -050088 const unsigned char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040089 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040090{
Al Viro5a190ae2007-06-07 12:19:32 -040091 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040092 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040093 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
94 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Eric Paris59b0df22010-02-08 12:53:52 -050095 const unsigned char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040096
Eric Parisff52cc22009-06-11 11:09:47 -040097 if (old_dir == new_dir)
98 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040099
Eric Paris90586522009-05-21 17:01:20 -0400100 if (isdir) {
Eric Parisb29866a2010-10-28 17:21:58 -0400101 old_dir_mask |= FS_ISDIR;
102 new_dir_mask |= FS_ISDIR;
Eric Paris90586522009-05-21 17:01:20 -0400103 }
104
Eric Paris47882c62009-05-21 17:01:47 -0400105 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
106 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -0400107
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500108 if (target)
Eric Paris90586522009-05-21 17:01:20 -0400109 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -0400110
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500111 if (source)
Eric Paris47882c62009-05-21 17:01:47 -0400112 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Al Virocccc6bb2009-12-25 05:07:33 -0500113 audit_inode_child(moved, new_dir);
Robert Love0eeca282005-07-12 17:06:03 -0400114}
115
116/*
Eric Paris3be25f42009-05-21 17:01:26 -0400117 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
118 */
119static inline void fsnotify_inode_delete(struct inode *inode)
120{
121 __fsnotify_inode_delete(inode);
122}
123
124/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500125 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
126 */
127static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
128{
129 __fsnotify_vfsmount_delete(mnt);
130}
131
132/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400133 * fsnotify_nameremove - a filename was removed from a directory
134 */
135static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
136{
Eric Paris90586522009-05-21 17:01:20 -0400137 __u32 mask = FS_DELETE;
138
John McCutchan7a91bf72005-08-08 13:52:16 -0400139 if (isdir)
Eric Parisb29866a2010-10-28 17:21:58 -0400140 mask |= FS_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400141
Eric Paris28c60e32009-12-17 21:24:21 -0500142 fsnotify_parent(NULL, dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400143}
144
145/*
146 * fsnotify_inoderemove - an inode is going away
147 */
148static inline void fsnotify_inoderemove(struct inode *inode)
149{
Eric Paris47882c62009-05-21 17:01:47 -0400150 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400151 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800152}
153
154/*
Robert Love0eeca282005-07-12 17:06:03 -0400155 * fsnotify_create - 'name' was linked in
156 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000157static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400158{
Al Virocccc6bb2009-12-25 05:07:33 -0500159 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400160
Eric Paris47882c62009-05-21 17:01:47 -0400161 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400162}
163
164/*
Jan Karaece95912008-02-06 01:37:13 -0800165 * fsnotify_link - new hardlink in 'inode' directory
166 * Note: We have to pass also the linked inode ptr as some filesystems leave
167 * new_dentry->d_inode NULL and instantiate inode pointer later
168 */
169static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
170{
Jan Karaece95912008-02-06 01:37:13 -0800171 fsnotify_link_count(inode);
Al Virocccc6bb2009-12-25 05:07:33 -0500172 audit_inode_child(new_dentry, dir);
Eric Paris90586522009-05-21 17:01:20 -0400173
Eric Paris47882c62009-05-21 17:01:47 -0400174 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800175}
176
177/*
Robert Love0eeca282005-07-12 17:06:03 -0400178 * fsnotify_mkdir - directory 'name' was created
179 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000180static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400181{
Eric Parisb29866a2010-10-28 17:21:58 -0400182 __u32 mask = (FS_CREATE | FS_ISDIR);
Eric Paris90586522009-05-21 17:01:20 -0400183 struct inode *d_inode = dentry->d_inode;
184
Al Virocccc6bb2009-12-25 05:07:33 -0500185 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400186
Eric Paris47882c62009-05-21 17:01:47 -0400187 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400188}
189
190/*
191 * fsnotify_access - file was read
192 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500193static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400194{
Linus Torvalds20696012010-08-12 14:23:04 -0700195 struct path *path = &file->f_path;
196 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400197 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400198
199 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400200 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400201
Eric Parisecf081d2009-12-17 21:24:25 -0500202 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700203 fsnotify_parent(path, NULL, mask);
204 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500205 }
Robert Love0eeca282005-07-12 17:06:03 -0400206}
207
208/*
209 * fsnotify_modify - file was modified
210 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500211static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400212{
Linus Torvalds20696012010-08-12 14:23:04 -0700213 struct path *path = &file->f_path;
214 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400215 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400216
217 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400218 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400219
Eric Parisecf081d2009-12-17 21:24:25 -0500220 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700221 fsnotify_parent(path, NULL, mask);
222 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500223 }
Robert Love0eeca282005-07-12 17:06:03 -0400224}
225
226/*
227 * fsnotify_open - file was opened
228 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500229static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400230{
Linus Torvalds20696012010-08-12 14:23:04 -0700231 struct path *path = &file->f_path;
232 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400233 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400234
235 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400236 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400237
Lino Sanfilippo6bff7ec2010-10-29 12:02:17 +0200238 /* FMODE_NONOTIFY must never be set from user */
239 file->f_mode &= ~FMODE_NONOTIFY;
240
241 fsnotify_parent(path, NULL, mask);
242 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400243}
244
245/*
246 * fsnotify_close - file was closed
247 */
248static inline void fsnotify_close(struct file *file)
249{
Linus Torvalds20696012010-08-12 14:23:04 -0700250 struct path *path = &file->f_path;
Eric Paris28c60e32009-12-17 21:24:21 -0500251 struct inode *inode = file->f_path.dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400252 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400253 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400254
255 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400256 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400257
Eric Parisecf081d2009-12-17 21:24:25 -0500258 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700259 fsnotify_parent(path, NULL, mask);
260 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500261 }
Robert Love0eeca282005-07-12 17:06:03 -0400262}
263
264/*
265 * fsnotify_xattr - extended attributes were changed
266 */
267static inline void fsnotify_xattr(struct dentry *dentry)
268{
269 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400270 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400271
272 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400273 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400274
Eric Paris28c60e32009-12-17 21:24:21 -0500275 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400276 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400277}
278
279/*
280 * fsnotify_change - notify_change event. file was modified and/or metadata
281 * was changed.
282 */
283static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
284{
285 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400286 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400287
Eric Paris3c5119c2009-05-21 17:01:33 -0400288 if (ia_valid & ATTR_UID)
289 mask |= FS_ATTRIB;
290 if (ia_valid & ATTR_GID)
291 mask |= FS_ATTRIB;
292 if (ia_valid & ATTR_SIZE)
293 mask |= FS_MODIFY;
294
Robert Love0eeca282005-07-12 17:06:03 -0400295 /* both times implies a utime(s) call */
296 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400297 mask |= FS_ATTRIB;
298 else if (ia_valid & ATTR_ATIME)
299 mask |= FS_ACCESS;
300 else if (ia_valid & ATTR_MTIME)
301 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400302
Eric Paris3c5119c2009-05-21 17:01:33 -0400303 if (ia_valid & ATTR_MODE)
304 mask |= FS_ATTRIB;
305
306 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400307 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400308 mask |= FS_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500309
310 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400311 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400312 }
313}
314
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500315#if defined(CONFIG_FSNOTIFY) /* notify helpers */
Robert Love0eeca282005-07-12 17:06:03 -0400316
317/*
318 * fsnotify_oldname_init - save off the old filename before we change it
319 */
Eric Paris59b0df22010-02-08 12:53:52 -0500320static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name)
Robert Love0eeca282005-07-12 17:06:03 -0400321{
322 return kstrdup(name, GFP_KERNEL);
323}
324
325/*
326 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
327 */
Eric Paris59b0df22010-02-08 12:53:52 -0500328static inline void fsnotify_oldname_free(const unsigned char *old_name)
Robert Love0eeca282005-07-12 17:06:03 -0400329{
330 kfree(old_name);
331}
332
Eric Paris28c60e32009-12-17 21:24:21 -0500333#else /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400334
Eric Paris59b0df22010-02-08 12:53:52 -0500335static inline const char *fsnotify_oldname_init(const unsigned char *name)
Robert Love0eeca282005-07-12 17:06:03 -0400336{
337 return NULL;
338}
339
Eric Paris59b0df22010-02-08 12:53:52 -0500340static inline void fsnotify_oldname_free(const unsigned char *old_name)
Robert Love0eeca282005-07-12 17:06:03 -0400341{
342}
343
Eric Paris28c60e32009-12-17 21:24:21 -0500344#endif /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400345
Robert Love0eeca282005-07-12 17:06:03 -0400346#endif /* _LINUX_FS_NOTIFY_H */