blob: e4e2204187ee2ae12e8eb0601e0fc078af68bb51 [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 Paris3bcf3862010-07-28 10:18:37 -040029static inline void fsnotify_parent(struct file *file, struct dentry *dentry, __u32 mask)
Eric Parisc28f7e52009-05-21 17:01:29 -040030{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050031 if (!dentry)
Eric Paris3bcf3862010-07-28 10:18:37 -040032 dentry = file->f_path.dentry;
Eric Paris28c60e32009-12-17 21:24:21 -050033
Eric Paris3bcf3862010-07-28 10:18:37 -040034 __fsnotify_parent(file, 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{
Eric Paris3bcf3862010-07-28 10:18:37 -040040 struct inode *inode = file->f_path.dentry->d_inode;
Eric Parisfb1cfb82010-05-12 11:42:29 -040041 __u32 fsnotify_mask = 0;
Eric Parisc4ec54b2009-12-17 21:24:34 -050042
43 if (file->f_mode & FMODE_NONOTIFY)
44 return 0;
45 if (!(mask & (MAY_READ | MAY_OPEN)))
46 return 0;
Eric Parisc4ec54b2009-12-17 21:24:34 -050047 if (mask & MAY_OPEN)
48 fsnotify_mask = FS_OPEN_PERM;
Eric Parisfb1cfb82010-05-12 11:42:29 -040049 else if (mask & MAY_READ)
50 fsnotify_mask = FS_ACCESS_PERM;
51 else
52 BUG();
Eric Parisc4ec54b2009-12-17 21:24:34 -050053
Eric Paris3bcf3862010-07-28 10:18:37 -040054 return fsnotify(inode, fsnotify_mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
Eric Parisc4ec54b2009-12-17 21:24:34 -050055}
56
Nick Pigginc32ccd82006-03-25 03:07:09 -080057/*
Eric Parise61ce862009-12-17 21:24:24 -050058 * fsnotify_d_move - dentry has been moved
59 * Called with dcache_lock and dentry->d_lock held.
Nick Pigginc32ccd82006-03-25 03:07:09 -080060 */
Eric Parise61ce862009-12-17 21:24:24 -050061static inline void fsnotify_d_move(struct dentry *dentry)
Nick Pigginc32ccd82006-03-25 03:07:09 -080062{
Eric Parisc28f7e52009-05-21 17:01:29 -040063 /*
Eric Parise61ce862009-12-17 21:24:24 -050064 * On move we need to update dentry->d_flags to indicate if the new parent
65 * cares about events from this dentry.
Eric Parisc28f7e52009-05-21 17:01:29 -040066 */
Eric Parise61ce862009-12-17 21:24:24 -050067 __fsnotify_update_dcache_flags(dentry);
Nick Pigginc32ccd82006-03-25 03:07:09 -080068}
69
70/*
Eric Paris90586522009-05-21 17:01:20 -040071 * fsnotify_link_count - inode's link count changed
72 */
73static inline void fsnotify_link_count(struct inode *inode)
74{
Eric Paris47882c62009-05-21 17:01:47 -040075 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040076}
77
78/*
Robert Love0eeca282005-07-12 17:06:03 -040079 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
80 */
81static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Eric Paris59b0df22010-02-08 12:53:52 -050082 const unsigned char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040083 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040084{
Al Viro5a190ae2007-06-07 12:19:32 -040085 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040086 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040087 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
88 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Eric Paris59b0df22010-02-08 12:53:52 -050089 const unsigned char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040090
Eric Parisff52cc22009-06-11 11:09:47 -040091 if (old_dir == new_dir)
92 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040093
Eric Paris90586522009-05-21 17:01:20 -040094 if (isdir) {
Eric Paris90586522009-05-21 17:01:20 -040095 old_dir_mask |= FS_IN_ISDIR;
96 new_dir_mask |= FS_IN_ISDIR;
97 }
98
Eric Paris47882c62009-05-21 17:01:47 -040099 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
100 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -0400101
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500102 if (target)
Eric Paris90586522009-05-21 17:01:20 -0400103 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -0400104
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500105 if (source)
Eric Paris47882c62009-05-21 17:01:47 -0400106 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Al Virocccc6bb2009-12-25 05:07:33 -0500107 audit_inode_child(moved, new_dir);
Robert Love0eeca282005-07-12 17:06:03 -0400108}
109
110/*
Eric Paris3be25f42009-05-21 17:01:26 -0400111 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
112 */
113static inline void fsnotify_inode_delete(struct inode *inode)
114{
115 __fsnotify_inode_delete(inode);
116}
117
118/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500119 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
120 */
121static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
122{
123 __fsnotify_vfsmount_delete(mnt);
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;
Eric Parisc28f7e52009-05-21 17:01:29 -0400135
Eric Paris28c60e32009-12-17 21:24:21 -0500136 fsnotify_parent(NULL, dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400137}
138
139/*
140 * fsnotify_inoderemove - an inode is going away
141 */
142static inline void fsnotify_inoderemove(struct inode *inode)
143{
Eric Paris47882c62009-05-21 17:01:47 -0400144 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400145 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800146}
147
148/*
Robert Love0eeca282005-07-12 17:06:03 -0400149 * fsnotify_create - 'name' was linked in
150 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000151static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400152{
Al Virocccc6bb2009-12-25 05:07:33 -0500153 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400154
Eric Paris47882c62009-05-21 17:01:47 -0400155 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400156}
157
158/*
Jan Karaece95912008-02-06 01:37:13 -0800159 * fsnotify_link - new hardlink in 'inode' directory
160 * Note: We have to pass also the linked inode ptr as some filesystems leave
161 * new_dentry->d_inode NULL and instantiate inode pointer later
162 */
163static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
164{
Jan Karaece95912008-02-06 01:37:13 -0800165 fsnotify_link_count(inode);
Al Virocccc6bb2009-12-25 05:07:33 -0500166 audit_inode_child(new_dentry, dir);
Eric Paris90586522009-05-21 17:01:20 -0400167
Eric Paris47882c62009-05-21 17:01:47 -0400168 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800169}
170
171/*
Robert Love0eeca282005-07-12 17:06:03 -0400172 * fsnotify_mkdir - directory 'name' was created
173 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000174static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400175{
Eric Paris90586522009-05-21 17:01:20 -0400176 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
177 struct inode *d_inode = dentry->d_inode;
178
Al Virocccc6bb2009-12-25 05:07:33 -0500179 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400180
Eric Paris47882c62009-05-21 17:01:47 -0400181 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400182}
183
184/*
185 * fsnotify_access - file was read
186 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500187static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400188{
Eric Paris3bcf3862010-07-28 10:18:37 -0400189 struct inode *inode = file->f_path.dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400190 __u32 mask = FS_ACCESS;
Robert Love0eeca282005-07-12 17:06:03 -0400191
192 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400193 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400194
Eric Parisecf081d2009-12-17 21:24:25 -0500195 if (!(file->f_mode & FMODE_NONOTIFY)) {
Eric Paris3bcf3862010-07-28 10:18:37 -0400196 fsnotify_parent(file, NULL, mask);
197 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500198 }
Robert Love0eeca282005-07-12 17:06:03 -0400199}
200
201/*
202 * fsnotify_modify - file was modified
203 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500204static inline void fsnotify_modify(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400205{
Eric Paris3bcf3862010-07-28 10:18:37 -0400206 struct inode *inode = file->f_path.dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400207 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400208
209 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400210 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400211
Eric Parisecf081d2009-12-17 21:24:25 -0500212 if (!(file->f_mode & FMODE_NONOTIFY)) {
Eric Paris3bcf3862010-07-28 10:18:37 -0400213 fsnotify_parent(file, NULL, mask);
214 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500215 }
Robert Love0eeca282005-07-12 17:06:03 -0400216}
217
218/*
219 * fsnotify_open - file was opened
220 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500221static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400222{
Eric Paris3bcf3862010-07-28 10:18:37 -0400223 struct inode *inode = file->f_path.dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400224 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400225
226 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400227 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400228
Eric Parisecf081d2009-12-17 21:24:25 -0500229 if (!(file->f_mode & FMODE_NONOTIFY)) {
Eric Paris3bcf3862010-07-28 10:18:37 -0400230 fsnotify_parent(file, NULL, mask);
231 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500232 }
Robert Love0eeca282005-07-12 17:06:03 -0400233}
234
235/*
236 * fsnotify_close - file was closed
237 */
238static inline void fsnotify_close(struct file *file)
239{
Eric Paris28c60e32009-12-17 21:24:21 -0500240 struct inode *inode = file->f_path.dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400241 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400242 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400243
244 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400245 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400246
Eric Parisecf081d2009-12-17 21:24:25 -0500247 if (!(file->f_mode & FMODE_NONOTIFY)) {
Eric Paris3bcf3862010-07-28 10:18:37 -0400248 fsnotify_parent(file, NULL, mask);
249 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
Eric Parisecf081d2009-12-17 21:24:25 -0500250 }
Robert Love0eeca282005-07-12 17:06:03 -0400251}
252
253/*
254 * fsnotify_xattr - extended attributes were changed
255 */
256static inline void fsnotify_xattr(struct dentry *dentry)
257{
258 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400259 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400260
261 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400262 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400263
Eric Paris28c60e32009-12-17 21:24:21 -0500264 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400265 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400266}
267
268/*
269 * fsnotify_change - notify_change event. file was modified and/or metadata
270 * was changed.
271 */
272static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
273{
274 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400275 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400276
Eric Paris3c5119c2009-05-21 17:01:33 -0400277 if (ia_valid & ATTR_UID)
278 mask |= FS_ATTRIB;
279 if (ia_valid & ATTR_GID)
280 mask |= FS_ATTRIB;
281 if (ia_valid & ATTR_SIZE)
282 mask |= FS_MODIFY;
283
Robert Love0eeca282005-07-12 17:06:03 -0400284 /* both times implies a utime(s) call */
285 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400286 mask |= FS_ATTRIB;
287 else if (ia_valid & ATTR_ATIME)
288 mask |= FS_ACCESS;
289 else if (ia_valid & ATTR_MTIME)
290 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400291
Eric Paris3c5119c2009-05-21 17:01:33 -0400292 if (ia_valid & ATTR_MODE)
293 mask |= FS_ATTRIB;
294
295 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400296 if (S_ISDIR(inode->i_mode))
Eric Paris3c5119c2009-05-21 17:01:33 -0400297 mask |= FS_IN_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500298
299 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400300 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400301 }
302}
303
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500304#if defined(CONFIG_FSNOTIFY) /* notify helpers */
Robert Love0eeca282005-07-12 17:06:03 -0400305
306/*
307 * fsnotify_oldname_init - save off the old filename before we change it
308 */
Eric Paris59b0df22010-02-08 12:53:52 -0500309static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name)
Robert Love0eeca282005-07-12 17:06:03 -0400310{
311 return kstrdup(name, GFP_KERNEL);
312}
313
314/*
315 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
316 */
Eric Paris59b0df22010-02-08 12:53:52 -0500317static inline void fsnotify_oldname_free(const unsigned char *old_name)
Robert Love0eeca282005-07-12 17:06:03 -0400318{
319 kfree(old_name);
320}
321
Eric Paris28c60e32009-12-17 21:24:21 -0500322#else /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400323
Eric Paris59b0df22010-02-08 12:53:52 -0500324static inline const char *fsnotify_oldname_init(const unsigned char *name)
Robert Love0eeca282005-07-12 17:06:03 -0400325{
326 return NULL;
327}
328
Eric Paris59b0df22010-02-08 12:53:52 -0500329static inline void fsnotify_oldname_free(const unsigned char *old_name)
Robert Love0eeca282005-07-12 17:06:03 -0400330{
331}
332
Eric Paris28c60e32009-12-17 21:24:21 -0500333#endif /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400334
Robert Love0eeca282005-07-12 17:06:03 -0400335#endif /* _LINUX_FS_NOTIFY_H */