blob: 2a53f10712b36097b648e4a61cb6323e5a42de84 [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
Nick Pigginc32ccd82006-03-25 03:07:09 -080020 */
Eric Parise61ce862009-12-17 21:24:24 -050021static inline void fsnotify_d_instantiate(struct dentry *dentry,
22 struct inode *inode)
Nick Pigginc32ccd82006-03-25 03:07:09 -080023{
Eric Parise61ce862009-12-17 21:24:24 -050024 __fsnotify_d_instantiate(dentry, inode);
Nick Pigginc32ccd82006-03-25 03:07:09 -080025}
26
Eric Parisc28f7e52009-05-21 17:01:29 -040027/* Notify this dentry's parent about a child's events. */
Eric Paris52420392010-10-28 17:21:56 -040028static inline int fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
Eric Parisc28f7e52009-05-21 17:01:29 -040029{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050030 if (!dentry)
Linus Torvalds20696012010-08-12 14:23:04 -070031 dentry = path->dentry;
Eric Paris28c60e32009-12-17 21:24:21 -050032
Eric Paris52420392010-10-28 17:21:56 -040033 return __fsnotify_parent(path, dentry, mask);
Eric Parisc28f7e52009-05-21 17:01:29 -040034}
35
Eric Parisc4ec54b2009-12-17 21:24:34 -050036/* simple call site for access decisions */
37static inline int fsnotify_perm(struct file *file, int mask)
38{
Linus Torvalds20696012010-08-12 14:23:04 -070039 struct path *path = &file->f_path;
40 struct inode *inode = path->dentry->d_inode;
Eric Parisfb1cfb82010-05-12 11:42:29 -040041 __u32 fsnotify_mask = 0;
Eric Paris52420392010-10-28 17:21:56 -040042 int ret;
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
Eric Paris52420392010-10-28 17:21:56 -040055 ret = fsnotify_parent(path, NULL, fsnotify_mask);
56 if (ret)
57 return ret;
58
Linus Torvalds20696012010-08-12 14:23:04 -070059 return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisc4ec54b2009-12-17 21:24:34 -050060}
61
Nick Pigginc32ccd82006-03-25 03:07:09 -080062/*
Eric Parise61ce862009-12-17 21:24:24 -050063 * fsnotify_d_move - dentry has been moved
Nick Pigginc32ccd82006-03-25 03:07:09 -080064 */
Eric Parise61ce862009-12-17 21:24:24 -050065static inline void fsnotify_d_move(struct dentry *dentry)
Nick Pigginc32ccd82006-03-25 03:07:09 -080066{
Eric Parisc28f7e52009-05-21 17:01:29 -040067 /*
Eric Parise61ce862009-12-17 21:24:24 -050068 * On move we need to update dentry->d_flags to indicate if the new parent
69 * cares about events from this dentry.
Eric Parisc28f7e52009-05-21 17:01:29 -040070 */
Eric Parise61ce862009-12-17 21:24:24 -050071 __fsnotify_update_dcache_flags(dentry);
Nick Pigginc32ccd82006-03-25 03:07:09 -080072}
73
74/*
Eric Paris90586522009-05-21 17:01:20 -040075 * fsnotify_link_count - inode's link count changed
76 */
77static inline void fsnotify_link_count(struct inode *inode)
78{
Eric Paris47882c62009-05-21 17:01:47 -040079 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040080}
81
82/*
Robert Love0eeca282005-07-12 17:06:03 -040083 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
84 */
85static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Eric Paris59b0df22010-02-08 12:53:52 -050086 const unsigned char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040087 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040088{
Al Viro5a190ae2007-06-07 12:19:32 -040089 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040090 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040091 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
92 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Eric Paris59b0df22010-02-08 12:53:52 -050093 const unsigned char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040094
Eric Parisff52cc22009-06-11 11:09:47 -040095 if (old_dir == new_dir)
96 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040097
Eric Paris90586522009-05-21 17:01:20 -040098 if (isdir) {
Eric Parisb29866a2010-10-28 17:21:58 -040099 old_dir_mask |= FS_ISDIR;
100 new_dir_mask |= FS_ISDIR;
Eric Paris90586522009-05-21 17:01:20 -0400101 }
102
Eric Paris47882c62009-05-21 17:01:47 -0400103 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
104 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -0400105
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500106 if (target)
Eric Paris90586522009-05-21 17:01:20 -0400107 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -0400108
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500109 if (source)
Eric Paris47882c62009-05-21 17:01:47 -0400110 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Al Virocccc6bb2009-12-25 05:07:33 -0500111 audit_inode_child(moved, new_dir);
Robert Love0eeca282005-07-12 17:06:03 -0400112}
113
114/*
Eric Paris3be25f42009-05-21 17:01:26 -0400115 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
116 */
117static inline void fsnotify_inode_delete(struct inode *inode)
118{
119 __fsnotify_inode_delete(inode);
120}
121
122/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500123 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
124 */
125static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
126{
127 __fsnotify_vfsmount_delete(mnt);
128}
129
130/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400131 * fsnotify_nameremove - a filename was removed from a directory
132 */
133static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
134{
Eric Paris90586522009-05-21 17:01:20 -0400135 __u32 mask = FS_DELETE;
136
John McCutchan7a91bf72005-08-08 13:52:16 -0400137 if (isdir)
Eric Parisb29866a2010-10-28 17:21:58 -0400138 mask |= FS_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400139
Eric Paris28c60e32009-12-17 21:24:21 -0500140 fsnotify_parent(NULL, dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400141}
142
143/*
144 * fsnotify_inoderemove - an inode is going away
145 */
146static inline void fsnotify_inoderemove(struct inode *inode)
147{
Eric Paris47882c62009-05-21 17:01:47 -0400148 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
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{
Al Virocccc6bb2009-12-25 05:07:33 -0500157 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400158
Eric Paris47882c62009-05-21 17:01:47 -0400159 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400160}
161
162/*
Jan Karaece95912008-02-06 01:37:13 -0800163 * fsnotify_link - new hardlink in 'inode' directory
164 * Note: We have to pass also the linked inode ptr as some filesystems leave
165 * new_dentry->d_inode NULL and instantiate inode pointer later
166 */
167static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
168{
Jan Karaece95912008-02-06 01:37:13 -0800169 fsnotify_link_count(inode);
Al Virocccc6bb2009-12-25 05:07:33 -0500170 audit_inode_child(new_dentry, dir);
Eric Paris90586522009-05-21 17:01:20 -0400171
Eric Paris47882c62009-05-21 17:01:47 -0400172 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800173}
174
175/*
Robert Love0eeca282005-07-12 17:06:03 -0400176 * fsnotify_mkdir - directory 'name' was created
177 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000178static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400179{
Eric Parisb29866a2010-10-28 17:21:58 -0400180 __u32 mask = (FS_CREATE | FS_ISDIR);
Eric Paris90586522009-05-21 17:01:20 -0400181 struct inode *d_inode = dentry->d_inode;
182
Al Virocccc6bb2009-12-25 05:07:33 -0500183 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400184
Eric Paris47882c62009-05-21 17:01:47 -0400185 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400186}
187
188/*
189 * fsnotify_access - file was read
190 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500191static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400192{
Linus Torvalds20696012010-08-12 14:23:04 -0700193 struct path *path = &file->f_path;
194 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400195 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400196
197 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400198 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400199
Eric Parisecf081d2009-12-17 21:24:25 -0500200 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700201 fsnotify_parent(path, NULL, mask);
202 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500203 }
Robert Love0eeca282005-07-12 17:06:03 -0400204}
205
206/*
207 * fsnotify_modify - file was modified
208 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500209static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400210{
Linus Torvalds20696012010-08-12 14:23:04 -0700211 struct path *path = &file->f_path;
212 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400213 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400214
215 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400216 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400217
Eric Parisecf081d2009-12-17 21:24:25 -0500218 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700219 fsnotify_parent(path, NULL, mask);
220 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500221 }
Robert Love0eeca282005-07-12 17:06:03 -0400222}
223
224/*
225 * fsnotify_open - file was opened
226 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500227static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400228{
Linus Torvalds20696012010-08-12 14:23:04 -0700229 struct path *path = &file->f_path;
230 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400231 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400232
233 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400234 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400235
Lino Sanfilippo6bff7ec2010-10-29 12:02:17 +0200236 fsnotify_parent(path, NULL, mask);
237 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400238}
239
240/*
241 * fsnotify_close - file was closed
242 */
243static inline void fsnotify_close(struct file *file)
244{
Linus Torvalds20696012010-08-12 14:23:04 -0700245 struct path *path = &file->f_path;
Eric Paris28c60e32009-12-17 21:24:21 -0500246 struct inode *inode = file->f_path.dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400247 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400248 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400249
250 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400251 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400252
Eric Parisecf081d2009-12-17 21:24:25 -0500253 if (!(file->f_mode & FMODE_NONOTIFY)) {
Linus Torvalds20696012010-08-12 14:23:04 -0700254 fsnotify_parent(path, NULL, mask);
255 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500256 }
Robert Love0eeca282005-07-12 17:06:03 -0400257}
258
259/*
260 * fsnotify_xattr - extended attributes were changed
261 */
262static inline void fsnotify_xattr(struct dentry *dentry)
263{
264 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400265 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400266
267 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400268 mask |= FS_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400269
Eric Paris28c60e32009-12-17 21:24:21 -0500270 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400271 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400272}
273
274/*
275 * fsnotify_change - notify_change event. file was modified and/or metadata
276 * was changed.
277 */
278static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
279{
280 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400281 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400282
Eric Paris3c5119c2009-05-21 17:01:33 -0400283 if (ia_valid & ATTR_UID)
284 mask |= FS_ATTRIB;
285 if (ia_valid & ATTR_GID)
286 mask |= FS_ATTRIB;
287 if (ia_valid & ATTR_SIZE)
288 mask |= FS_MODIFY;
289
Robert Love0eeca282005-07-12 17:06:03 -0400290 /* both times implies a utime(s) call */
291 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400292 mask |= FS_ATTRIB;
293 else if (ia_valid & ATTR_ATIME)
294 mask |= FS_ACCESS;
295 else if (ia_valid & ATTR_MTIME)
296 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400297
Eric Paris3c5119c2009-05-21 17:01:33 -0400298 if (ia_valid & ATTR_MODE)
299 mask |= FS_ATTRIB;
300
301 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400302 if (S_ISDIR(inode->i_mode))
Eric Parisb29866a2010-10-28 17:21:58 -0400303 mask |= FS_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500304
305 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400306 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400307 }
308}
309
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500310#if defined(CONFIG_FSNOTIFY) /* notify helpers */
Robert Love0eeca282005-07-12 17:06:03 -0400311
312/*
313 * fsnotify_oldname_init - save off the old filename before we change it
314 */
Eric Paris59b0df22010-02-08 12:53:52 -0500315static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name)
Robert Love0eeca282005-07-12 17:06:03 -0400316{
317 return kstrdup(name, GFP_KERNEL);
318}
319
320/*
321 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
322 */
Eric Paris59b0df22010-02-08 12:53:52 -0500323static inline void fsnotify_oldname_free(const unsigned char *old_name)
Robert Love0eeca282005-07-12 17:06:03 -0400324{
325 kfree(old_name);
326}
327
Eric Paris28c60e32009-12-17 21:24:21 -0500328#else /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400329
Eric Paris59b0df22010-02-08 12:53:52 -0500330static inline const char *fsnotify_oldname_init(const unsigned char *name)
Robert Love0eeca282005-07-12 17:06:03 -0400331{
332 return NULL;
333}
334
Eric Paris59b0df22010-02-08 12:53:52 -0500335static inline void fsnotify_oldname_free(const unsigned char *old_name)
Robert Love0eeca282005-07-12 17:06:03 -0400336{
337}
338
Eric Paris28c60e32009-12-17 21:24:21 -0500339#endif /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400340
Robert Love0eeca282005-07-12 17:06:03 -0400341#endif /* _LINUX_FS_NOTIFY_H */