blob: 64efda9aae628c3d2ee3581fb351be9a36d6c902 [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. */
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050029static 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)
32 dentry = path->dentry;
Eric Paris28c60e32009-12-17 21:24:21 -050033
Andreas Gruenbacher72acc852009-12-17 21:24:24 -050034 __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{
40 struct path *path = &file->f_path;
41 struct inode *inode = path->dentry->d_inode;
42 __u32 fsnotify_mask;
43
44 if (file->f_mode & FMODE_NONOTIFY)
45 return 0;
46 if (!(mask & (MAY_READ | MAY_OPEN)))
47 return 0;
48 if (mask & MAY_READ)
49 fsnotify_mask = FS_ACCESS_PERM;
50 if (mask & MAY_OPEN)
51 fsnotify_mask = FS_OPEN_PERM;
52
53 return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
54}
55
Nick Pigginc32ccd82006-03-25 03:07:09 -080056/*
Eric Parise61ce862009-12-17 21:24:24 -050057 * fsnotify_d_move - dentry has been moved
58 * Called with dcache_lock and dentry->d_lock held.
Nick Pigginc32ccd82006-03-25 03:07:09 -080059 */
Eric Parise61ce862009-12-17 21:24:24 -050060static inline void fsnotify_d_move(struct dentry *dentry)
Nick Pigginc32ccd82006-03-25 03:07:09 -080061{
Eric Parisc28f7e52009-05-21 17:01:29 -040062 /*
Eric Parise61ce862009-12-17 21:24:24 -050063 * On move we need to update dentry->d_flags to indicate if the new parent
64 * cares about events from this dentry.
Eric Parisc28f7e52009-05-21 17:01:29 -040065 */
Eric Parise61ce862009-12-17 21:24:24 -050066 __fsnotify_update_dcache_flags(dentry);
Nick Pigginc32ccd82006-03-25 03:07:09 -080067}
68
69/*
Eric Paris90586522009-05-21 17:01:20 -040070 * fsnotify_link_count - inode's link count changed
71 */
72static inline void fsnotify_link_count(struct inode *inode)
73{
Eric Paris47882c62009-05-21 17:01:47 -040074 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris90586522009-05-21 17:01:20 -040075}
76
77/*
Robert Love0eeca282005-07-12 17:06:03 -040078 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
79 */
80static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
Eric Paris59b0df22010-02-08 12:53:52 -050081 const unsigned char *old_name,
Al Viro5a190ae2007-06-07 12:19:32 -040082 int isdir, struct inode *target, struct dentry *moved)
Robert Love0eeca282005-07-12 17:06:03 -040083{
Al Viro5a190ae2007-06-07 12:19:32 -040084 struct inode *source = moved->d_inode;
Eric Paris47882c62009-05-21 17:01:47 -040085 u32 fs_cookie = fsnotify_get_cookie();
Eric Parisff52cc22009-06-11 11:09:47 -040086 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
87 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
Eric Paris59b0df22010-02-08 12:53:52 -050088 const unsigned char *new_name = moved->d_name.name;
Robert Love0eeca282005-07-12 17:06:03 -040089
Eric Parisff52cc22009-06-11 11:09:47 -040090 if (old_dir == new_dir)
91 old_dir_mask |= FS_DN_RENAME;
Robert Love0eeca282005-07-12 17:06:03 -040092
Eric Paris90586522009-05-21 17:01:20 -040093 if (isdir) {
Eric Paris90586522009-05-21 17:01:20 -040094 old_dir_mask |= FS_IN_ISDIR;
95 new_dir_mask |= FS_IN_ISDIR;
96 }
97
Eric Paris47882c62009-05-21 17:01:47 -040098 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
99 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
Eric Paris90586522009-05-21 17:01:20 -0400100
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500101 if (target)
Eric Paris90586522009-05-21 17:01:20 -0400102 fsnotify_link_count(target);
John McCutchan89204c42005-08-15 12:13:28 -0400103
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500104 if (source)
Eric Paris47882c62009-05-21 17:01:47 -0400105 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Al Virocccc6bb2009-12-25 05:07:33 -0500106 audit_inode_child(moved, new_dir);
Robert Love0eeca282005-07-12 17:06:03 -0400107}
108
109/*
Eric Paris3be25f42009-05-21 17:01:26 -0400110 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
111 */
112static inline void fsnotify_inode_delete(struct inode *inode)
113{
114 __fsnotify_inode_delete(inode);
115}
116
117/*
Andreas Gruenbacherca9c7262009-12-17 21:24:27 -0500118 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
119 */
120static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
121{
122 __fsnotify_vfsmount_delete(mnt);
123}
124
125/*
John McCutchan7a91bf72005-08-08 13:52:16 -0400126 * fsnotify_nameremove - a filename was removed from a directory
127 */
128static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
129{
Eric Paris90586522009-05-21 17:01:20 -0400130 __u32 mask = FS_DELETE;
131
John McCutchan7a91bf72005-08-08 13:52:16 -0400132 if (isdir)
Eric Paris90586522009-05-21 17:01:20 -0400133 mask |= FS_IN_ISDIR;
Eric Parisc28f7e52009-05-21 17:01:29 -0400134
Eric Paris28c60e32009-12-17 21:24:21 -0500135 fsnotify_parent(NULL, dentry, mask);
John McCutchan7a91bf72005-08-08 13:52:16 -0400136}
137
138/*
139 * fsnotify_inoderemove - an inode is going away
140 */
141static inline void fsnotify_inoderemove(struct inode *inode)
142{
Eric Paris47882c62009-05-21 17:01:47 -0400143 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Eric Paris3be25f42009-05-21 17:01:26 -0400144 __fsnotify_inode_delete(inode);
Jan Karaece95912008-02-06 01:37:13 -0800145}
146
147/*
Robert Love0eeca282005-07-12 17:06:03 -0400148 * fsnotify_create - 'name' was linked in
149 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000150static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400151{
Al Virocccc6bb2009-12-25 05:07:33 -0500152 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400153
Eric Paris47882c62009-05-21 17:01:47 -0400154 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400155}
156
157/*
Jan Karaece95912008-02-06 01:37:13 -0800158 * fsnotify_link - new hardlink in 'inode' directory
159 * Note: We have to pass also the linked inode ptr as some filesystems leave
160 * new_dentry->d_inode NULL and instantiate inode pointer later
161 */
162static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
163{
Jan Karaece95912008-02-06 01:37:13 -0800164 fsnotify_link_count(inode);
Al Virocccc6bb2009-12-25 05:07:33 -0500165 audit_inode_child(new_dentry, dir);
Eric Paris90586522009-05-21 17:01:20 -0400166
Eric Paris47882c62009-05-21 17:01:47 -0400167 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
Jan Karaece95912008-02-06 01:37:13 -0800168}
169
170/*
Robert Love0eeca282005-07-12 17:06:03 -0400171 * fsnotify_mkdir - directory 'name' was created
172 */
Amy Griffisf38aa942005-11-03 15:57:06 +0000173static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -0400174{
Eric Paris90586522009-05-21 17:01:20 -0400175 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
176 struct inode *d_inode = dentry->d_inode;
177
Al Virocccc6bb2009-12-25 05:07:33 -0500178 audit_inode_child(dentry, inode);
Eric Paris90586522009-05-21 17:01:20 -0400179
Eric Paris47882c62009-05-21 17:01:47 -0400180 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400181}
182
183/*
184 * fsnotify_access - file was read
185 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500186static inline void fsnotify_access(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400187{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -0500188 struct path *path = &file->f_path;
189 struct inode *inode = 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)) {
196 fsnotify_parent(path, NULL, mask);
197 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
198 }
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{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -0500206 struct path *path = &file->f_path;
207 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400208 __u32 mask = FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400209
210 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400211 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400212
Eric Parisecf081d2009-12-17 21:24:25 -0500213 if (!(file->f_mode & FMODE_NONOTIFY)) {
214 fsnotify_parent(path, NULL, mask);
215 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
216 }
Robert Love0eeca282005-07-12 17:06:03 -0400217}
218
219/*
220 * fsnotify_open - file was opened
221 */
Eric Paris2a12a9d2009-12-17 21:24:21 -0500222static inline void fsnotify_open(struct file *file)
Robert Love0eeca282005-07-12 17:06:03 -0400223{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -0500224 struct path *path = &file->f_path;
225 struct inode *inode = path->dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400226 __u32 mask = FS_OPEN;
Robert Love0eeca282005-07-12 17:06:03 -0400227
228 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400229 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400230
Eric Parisecf081d2009-12-17 21:24:25 -0500231 if (!(file->f_mode & FMODE_NONOTIFY)) {
232 fsnotify_parent(path, NULL, mask);
233 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
234 }
Robert Love0eeca282005-07-12 17:06:03 -0400235}
236
237/*
238 * fsnotify_close - file was closed
239 */
240static inline void fsnotify_close(struct file *file)
241{
Andreas Gruenbacher72acc852009-12-17 21:24:24 -0500242 struct path *path = &file->f_path;
Eric Paris28c60e32009-12-17 21:24:21 -0500243 struct inode *inode = file->f_path.dentry->d_inode;
Al Viroaeb5d722008-09-02 15:28:45 -0400244 fmode_t mode = file->f_mode;
Eric Paris90586522009-05-21 17:01:20 -0400245 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
Robert Love0eeca282005-07-12 17:06:03 -0400246
247 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400248 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400249
Eric Parisecf081d2009-12-17 21:24:25 -0500250 if (!(file->f_mode & FMODE_NONOTIFY)) {
251 fsnotify_parent(path, NULL, mask);
252 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
253 }
Robert Love0eeca282005-07-12 17:06:03 -0400254}
255
256/*
257 * fsnotify_xattr - extended attributes were changed
258 */
259static inline void fsnotify_xattr(struct dentry *dentry)
260{
261 struct inode *inode = dentry->d_inode;
Eric Paris90586522009-05-21 17:01:20 -0400262 __u32 mask = FS_ATTRIB;
Robert Love0eeca282005-07-12 17:06:03 -0400263
264 if (S_ISDIR(inode->i_mode))
Eric Paris90586522009-05-21 17:01:20 -0400265 mask |= FS_IN_ISDIR;
Robert Love0eeca282005-07-12 17:06:03 -0400266
Eric Paris28c60e32009-12-17 21:24:21 -0500267 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400268 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400269}
270
271/*
272 * fsnotify_change - notify_change event. file was modified and/or metadata
273 * was changed.
274 */
275static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
276{
277 struct inode *inode = dentry->d_inode;
Eric Paris3c5119c2009-05-21 17:01:33 -0400278 __u32 mask = 0;
Robert Love0eeca282005-07-12 17:06:03 -0400279
Eric Paris3c5119c2009-05-21 17:01:33 -0400280 if (ia_valid & ATTR_UID)
281 mask |= FS_ATTRIB;
282 if (ia_valid & ATTR_GID)
283 mask |= FS_ATTRIB;
284 if (ia_valid & ATTR_SIZE)
285 mask |= FS_MODIFY;
286
Robert Love0eeca282005-07-12 17:06:03 -0400287 /* both times implies a utime(s) call */
288 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
Eric Paris3c5119c2009-05-21 17:01:33 -0400289 mask |= FS_ATTRIB;
290 else if (ia_valid & ATTR_ATIME)
291 mask |= FS_ACCESS;
292 else if (ia_valid & ATTR_MTIME)
293 mask |= FS_MODIFY;
Robert Love0eeca282005-07-12 17:06:03 -0400294
Eric Paris3c5119c2009-05-21 17:01:33 -0400295 if (ia_valid & ATTR_MODE)
296 mask |= FS_ATTRIB;
297
298 if (mask) {
Robert Love0eeca282005-07-12 17:06:03 -0400299 if (S_ISDIR(inode->i_mode))
Eric Paris3c5119c2009-05-21 17:01:33 -0400300 mask |= FS_IN_ISDIR;
Eric Paris28c60e32009-12-17 21:24:21 -0500301
302 fsnotify_parent(NULL, dentry, mask);
Eric Paris47882c62009-05-21 17:01:47 -0400303 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
Robert Love0eeca282005-07-12 17:06:03 -0400304 }
305}
306
Eric Paris2dfc1ca2009-12-17 20:30:52 -0500307#if defined(CONFIG_FSNOTIFY) /* notify helpers */
Robert Love0eeca282005-07-12 17:06:03 -0400308
309/*
310 * fsnotify_oldname_init - save off the old filename before we change it
311 */
Eric Paris59b0df22010-02-08 12:53:52 -0500312static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name)
Robert Love0eeca282005-07-12 17:06:03 -0400313{
314 return kstrdup(name, GFP_KERNEL);
315}
316
317/*
318 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
319 */
Eric Paris59b0df22010-02-08 12:53:52 -0500320static inline void fsnotify_oldname_free(const unsigned char *old_name)
Robert Love0eeca282005-07-12 17:06:03 -0400321{
322 kfree(old_name);
323}
324
Eric Paris28c60e32009-12-17 21:24:21 -0500325#else /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400326
Eric Paris59b0df22010-02-08 12:53:52 -0500327static inline const char *fsnotify_oldname_init(const unsigned char *name)
Robert Love0eeca282005-07-12 17:06:03 -0400328{
329 return NULL;
330}
331
Eric Paris59b0df22010-02-08 12:53:52 -0500332static inline void fsnotify_oldname_free(const unsigned char *old_name)
Robert Love0eeca282005-07-12 17:06:03 -0400333{
334}
335
Eric Paris28c60e32009-12-17 21:24:21 -0500336#endif /* CONFIG_FSNOTIFY */
Robert Love0eeca282005-07-12 17:06:03 -0400337
Robert Love0eeca282005-07-12 17:06:03 -0400338#endif /* _LINUX_FS_NOTIFY_H */