]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - include/linux/fsnotify.h
fsnotify: correctly handle return codes from listeners
[mirror_ubuntu-zesty-kernel.git] / include / linux / fsnotify.h
CommitLineData
0eeca283
RL
1#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
90586523 14#include <linux/fsnotify_backend.h>
73241ccc 15#include <linux/audit.h>
5a0e3ad6 16#include <linux/slab.h>
0eeca283 17
c32ccd87
NP
18/*
19 * fsnotify_d_instantiate - instantiate a dentry for inode
20 * Called with dcache_lock held.
21 */
e61ce867
EP
22static inline void fsnotify_d_instantiate(struct dentry *dentry,
23 struct inode *inode)
c32ccd87 24{
e61ce867 25 __fsnotify_d_instantiate(dentry, inode);
c32ccd87
NP
26}
27
c28f7e56 28/* Notify this dentry's parent about a child's events. */
2069601b 29static inline void fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
c28f7e56 30{
72acc854 31 if (!dentry)
2069601b 32 dentry = path->dentry;
28c60e37 33
2069601b 34 __fsnotify_parent(path, dentry, mask);
c28f7e56
EP
35}
36
c4ec54b4
EP
37/* simple call site for access decisions */
38static inline int fsnotify_perm(struct file *file, int mask)
39{
2069601b
LT
40 struct path *path = &file->f_path;
41 struct inode *inode = path->dentry->d_inode;
fb1cfb88 42 __u32 fsnotify_mask = 0;
c4ec54b4
EP
43
44 if (file->f_mode & FMODE_NONOTIFY)
45 return 0;
46 if (!(mask & (MAY_READ | MAY_OPEN)))
47 return 0;
c4ec54b4
EP
48 if (mask & MAY_OPEN)
49 fsnotify_mask = FS_OPEN_PERM;
fb1cfb88
EP
50 else if (mask & MAY_READ)
51 fsnotify_mask = FS_ACCESS_PERM;
52 else
53 BUG();
c4ec54b4 54
2069601b 55 return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
c4ec54b4
EP
56}
57
c32ccd87 58/*
e61ce867
EP
59 * fsnotify_d_move - dentry has been moved
60 * Called with dcache_lock and dentry->d_lock held.
c32ccd87 61 */
e61ce867 62static inline void fsnotify_d_move(struct dentry *dentry)
c32ccd87 63{
c28f7e56 64 /*
e61ce867
EP
65 * On move we need to update dentry->d_flags to indicate if the new parent
66 * cares about events from this dentry.
c28f7e56 67 */
e61ce867 68 __fsnotify_update_dcache_flags(dentry);
c32ccd87
NP
69}
70
90586523
EP
71/*
72 * fsnotify_link_count - inode's link count changed
73 */
74static inline void fsnotify_link_count(struct inode *inode)
75{
47882c6f 76 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
90586523
EP
77}
78
0eeca283
RL
79/*
80 * 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,
59b0df21 83 const unsigned char *old_name,
5a190ae6 84 int isdir, struct inode *target, struct dentry *moved)
0eeca283 85{
5a190ae6 86 struct inode *source = moved->d_inode;
47882c6f 87 u32 fs_cookie = fsnotify_get_cookie();
ff52cc21
EP
88 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
89 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
59b0df21 90 const unsigned char *new_name = moved->d_name.name;
0eeca283 91
ff52cc21
EP
92 if (old_dir == new_dir)
93 old_dir_mask |= FS_DN_RENAME;
0eeca283 94
90586523 95 if (isdir) {
90586523
EP
96 old_dir_mask |= FS_IN_ISDIR;
97 new_dir_mask |= FS_IN_ISDIR;
98 }
99
47882c6f
EP
100 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);
90586523 102
2dfc1cae 103 if (target)
90586523 104 fsnotify_link_count(target);
89204c40 105
2dfc1cae 106 if (source)
47882c6f 107 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
cccc6bba 108 audit_inode_child(moved, new_dir);
0eeca283
RL
109}
110
3be25f49
EP
111/*
112 * 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
ca9c726e
AG
119/*
120 * 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
7a91bf7f
JM
127/*
128 * fsnotify_nameremove - a filename was removed from a directory
129 */
130static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
131{
90586523
EP
132 __u32 mask = FS_DELETE;
133
7a91bf7f 134 if (isdir)
90586523 135 mask |= FS_IN_ISDIR;
c28f7e56 136
28c60e37 137 fsnotify_parent(NULL, dentry, mask);
7a91bf7f
JM
138}
139
140/*
141 * fsnotify_inoderemove - an inode is going away
142 */
143static inline void fsnotify_inoderemove(struct inode *inode)
144{
47882c6f 145 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
3be25f49 146 __fsnotify_inode_delete(inode);
ece95912
JK
147}
148
0eeca283
RL
149/*
150 * fsnotify_create - 'name' was linked in
151 */
f38aa942 152static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
0eeca283 153{
cccc6bba 154 audit_inode_child(dentry, inode);
90586523 155
47882c6f 156 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
0eeca283
RL
157}
158
ece95912
JK
159/*
160 * 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{
ece95912 166 fsnotify_link_count(inode);
cccc6bba 167 audit_inode_child(new_dentry, dir);
90586523 168
47882c6f 169 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
ece95912
JK
170}
171
0eeca283
RL
172/*
173 * fsnotify_mkdir - directory 'name' was created
174 */
f38aa942 175static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
0eeca283 176{
90586523
EP
177 __u32 mask = (FS_CREATE | FS_IN_ISDIR);
178 struct inode *d_inode = dentry->d_inode;
179
cccc6bba 180 audit_inode_child(dentry, inode);
90586523 181
47882c6f 182 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
0eeca283
RL
183}
184
185/*
186 * fsnotify_access - file was read
187 */
2a12a9d7 188static inline void fsnotify_access(struct file *file)
0eeca283 189{
2069601b
LT
190 struct path *path = &file->f_path;
191 struct inode *inode = path->dentry->d_inode;
90586523 192 __u32 mask = FS_ACCESS;
0eeca283
RL
193
194 if (S_ISDIR(inode->i_mode))
90586523 195 mask |= FS_IN_ISDIR;
0eeca283 196
ecf081d1 197 if (!(file->f_mode & FMODE_NONOTIFY)) {
2069601b
LT
198 fsnotify_parent(path, NULL, mask);
199 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
ecf081d1 200 }
0eeca283
RL
201}
202
203/*
204 * fsnotify_modify - file was modified
205 */
2a12a9d7 206static inline void fsnotify_modify(struct file *file)
0eeca283 207{
2069601b
LT
208 struct path *path = &file->f_path;
209 struct inode *inode = path->dentry->d_inode;
90586523 210 __u32 mask = FS_MODIFY;
0eeca283
RL
211
212 if (S_ISDIR(inode->i_mode))
90586523 213 mask |= FS_IN_ISDIR;
0eeca283 214
ecf081d1 215 if (!(file->f_mode & FMODE_NONOTIFY)) {
2069601b
LT
216 fsnotify_parent(path, NULL, mask);
217 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
ecf081d1 218 }
0eeca283
RL
219}
220
221/*
222 * fsnotify_open - file was opened
223 */
2a12a9d7 224static inline void fsnotify_open(struct file *file)
0eeca283 225{
2069601b
LT
226 struct path *path = &file->f_path;
227 struct inode *inode = path->dentry->d_inode;
90586523 228 __u32 mask = FS_OPEN;
0eeca283
RL
229
230 if (S_ISDIR(inode->i_mode))
90586523 231 mask |= FS_IN_ISDIR;
0eeca283 232
ecf081d1 233 if (!(file->f_mode & FMODE_NONOTIFY)) {
2069601b
LT
234 fsnotify_parent(path, NULL, mask);
235 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
ecf081d1 236 }
0eeca283
RL
237}
238
239/*
240 * fsnotify_close - file was closed
241 */
242static inline void fsnotify_close(struct file *file)
243{
2069601b 244 struct path *path = &file->f_path;
28c60e37 245 struct inode *inode = file->f_path.dentry->d_inode;
aeb5d727 246 fmode_t mode = file->f_mode;
90586523 247 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
0eeca283
RL
248
249 if (S_ISDIR(inode->i_mode))
90586523 250 mask |= FS_IN_ISDIR;
0eeca283 251
ecf081d1 252 if (!(file->f_mode & FMODE_NONOTIFY)) {
2069601b
LT
253 fsnotify_parent(path, NULL, mask);
254 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
ecf081d1 255 }
0eeca283
RL
256}
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;
90586523 264 __u32 mask = FS_ATTRIB;
0eeca283
RL
265
266 if (S_ISDIR(inode->i_mode))
90586523 267 mask |= FS_IN_ISDIR;
0eeca283 268
28c60e37 269 fsnotify_parent(NULL, dentry, mask);
47882c6f 270 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
0eeca283
RL
271}
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;
3c5119c0
EP
280 __u32 mask = 0;
281
282 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;
0eeca283 288
0eeca283
RL
289 /* both times implies a utime(s) call */
290 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
3c5119c0
EP
291 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;
296
297 if (ia_valid & ATTR_MODE)
298 mask |= FS_ATTRIB;
0eeca283 299
3c5119c0 300 if (mask) {
0eeca283 301 if (S_ISDIR(inode->i_mode))
3c5119c0 302 mask |= FS_IN_ISDIR;
28c60e37
EP
303
304 fsnotify_parent(NULL, dentry, mask);
47882c6f 305 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
0eeca283
RL
306 }
307}
308
2dfc1cae 309#if defined(CONFIG_FSNOTIFY) /* notify helpers */
0eeca283
RL
310
311/*
312 * fsnotify_oldname_init - save off the old filename before we change it
313 */
59b0df21 314static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name)
0eeca283
RL
315{
316 return kstrdup(name, GFP_KERNEL);
317}
318
319/*
320 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
321 */
59b0df21 322static inline void fsnotify_oldname_free(const unsigned char *old_name)
0eeca283
RL
323{
324 kfree(old_name);
325}
326
28c60e37 327#else /* CONFIG_FSNOTIFY */
0eeca283 328
59b0df21 329static inline const char *fsnotify_oldname_init(const unsigned char *name)
0eeca283
RL
330{
331 return NULL;
332}
333
59b0df21 334static inline void fsnotify_oldname_free(const unsigned char *old_name)
0eeca283
RL
335{
336}
337
28c60e37 338#endif /* CONFIG_FSNOTIFY */
0eeca283 339
0eeca283 340#endif /* _LINUX_FS_NOTIFY_H */