]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - include/linux/fsnotify.h
fsnotify: include pathnames with entries when possible
[mirror_ubuntu-focal-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
0eeca283
RL
14#include <linux/dnotify.h>
15#include <linux/inotify.h>
90586523 16#include <linux/fsnotify_backend.h>
73241ccc 17#include <linux/audit.h>
0eeca283 18
c32ccd87
NP
19/*
20 * fsnotify_d_instantiate - instantiate a dentry for inode
21 * Called with dcache_lock held.
22 */
23static inline void fsnotify_d_instantiate(struct dentry *entry,
24 struct inode *inode)
25{
c28f7e56
EP
26 __fsnotify_d_instantiate(entry, inode);
27
c32ccd87
NP
28 inotify_d_instantiate(entry, inode);
29}
30
c28f7e56
EP
31/* Notify this dentry's parent about a child's events. */
32static inline void fsnotify_parent(struct dentry *dentry, __u32 mask)
33{
34 __fsnotify_parent(dentry, mask);
35
36 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
37}
38
c32ccd87
NP
39/*
40 * fsnotify_d_move - entry has been moved
41 * Called with dcache_lock and entry->d_lock held.
42 */
43static inline void fsnotify_d_move(struct dentry *entry)
44{
c28f7e56
EP
45 /*
46 * On move we need to update entry->d_flags to indicate if the new parent
47 * cares about events from this entry.
48 */
49 __fsnotify_update_dcache_flags(entry);
50
c32ccd87
NP
51 inotify_d_move(entry);
52}
53
90586523
EP
54/*
55 * fsnotify_link_count - inode's link count changed
56 */
57static inline void fsnotify_link_count(struct inode *inode)
58{
59 inotify_inode_queue_event(inode, IN_ATTRIB, 0, NULL, NULL);
60
62ffe5df 61 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL);
90586523
EP
62}
63
0eeca283
RL
64/*
65 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
66 */
67static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
68 const char *old_name, const char *new_name,
5a190ae6 69 int isdir, struct inode *target, struct dentry *moved)
0eeca283 70{
5a190ae6 71 struct inode *source = moved->d_inode;
0eeca283 72 u32 cookie = inotify_get_cookie();
90586523
EP
73 __u32 old_dir_mask = 0;
74 __u32 new_dir_mask = 0;
0eeca283 75
90586523 76 if (old_dir == new_dir) {
90586523 77 old_dir_mask = FS_DN_RENAME;
0eeca283
RL
78 }
79
90586523 80 if (isdir) {
0eeca283 81 isdir = IN_ISDIR;
90586523
EP
82 old_dir_mask |= FS_IN_ISDIR;
83 new_dir_mask |= FS_IN_ISDIR;
84 }
85
86 old_dir_mask |= FS_MOVED_FROM;
87 new_dir_mask |= FS_MOVED_TO;
88
7c297722
AG
89 inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name,
90 source);
91 inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name,
92 source);
75449536 93
62ffe5df
EP
94 fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name);
95 fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name);
90586523 96
75449536 97 if (target) {
7c297722 98 inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
75449536 99 inotify_inode_is_dead(target);
90586523
EP
100
101 /* this is really a link_count change not a removal */
102 fsnotify_link_count(target);
75449536 103 }
89204c40
JM
104
105 if (source) {
7c297722 106 inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL);
62ffe5df 107 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL);
89204c40 108 }
5a190ae6 109 audit_inode_child(new_name, moved, new_dir);
0eeca283
RL
110}
111
3be25f49
EP
112/*
113 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
114 */
115static inline void fsnotify_inode_delete(struct inode *inode)
116{
117 __fsnotify_inode_delete(inode);
118}
119
7a91bf7f
JM
120/*
121 * fsnotify_nameremove - a filename was removed from a directory
122 */
123static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
124{
90586523
EP
125 __u32 mask = FS_DELETE;
126
7a91bf7f 127 if (isdir)
90586523 128 mask |= FS_IN_ISDIR;
c28f7e56
EP
129
130 fsnotify_parent(dentry, mask);
7a91bf7f
JM
131}
132
133/*
134 * fsnotify_inoderemove - an inode is going away
135 */
136static inline void fsnotify_inoderemove(struct inode *inode)
137{
7c297722 138 inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL, NULL);
7a91bf7f 139 inotify_inode_is_dead(inode);
7a91bf7f 140
62ffe5df 141 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL);
3be25f49 142 __fsnotify_inode_delete(inode);
ece95912
JK
143}
144
0eeca283
RL
145/*
146 * fsnotify_create - 'name' was linked in
147 */
f38aa942 148static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
0eeca283 149{
7c297722
AG
150 inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name,
151 dentry->d_inode);
5a190ae6 152 audit_inode_child(dentry->d_name.name, dentry, inode);
90586523 153
62ffe5df 154 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name);
0eeca283
RL
155}
156
ece95912
JK
157/*
158 * 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{
ece95912
JK
164 inotify_inode_queue_event(dir, IN_CREATE, 0, new_dentry->d_name.name,
165 inode);
166 fsnotify_link_count(inode);
167 audit_inode_child(new_dentry->d_name.name, new_dentry, dir);
90586523 168
62ffe5df 169 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name);
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
90586523 180 inotify_inode_queue_event(inode, mask, 0, dentry->d_name.name, d_inode);
5a190ae6 181 audit_inode_child(dentry->d_name.name, dentry, inode);
90586523 182
62ffe5df 183 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name);
0eeca283
RL
184}
185
186/*
187 * fsnotify_access - file was read
188 */
189static inline void fsnotify_access(struct dentry *dentry)
190{
191 struct inode *inode = 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
7c297722 197 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
90586523 198
c28f7e56 199 fsnotify_parent(dentry, mask);
62ffe5df 200 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL);
0eeca283
RL
201}
202
203/*
204 * fsnotify_modify - file was modified
205 */
206static inline void fsnotify_modify(struct dentry *dentry)
207{
208 struct inode *inode = dentry->d_inode;
90586523 209 __u32 mask = FS_MODIFY;
0eeca283
RL
210
211 if (S_ISDIR(inode->i_mode))
90586523 212 mask |= FS_IN_ISDIR;
0eeca283 213
7c297722 214 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
90586523 215
c28f7e56 216 fsnotify_parent(dentry, mask);
62ffe5df 217 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL);
0eeca283
RL
218}
219
220/*
221 * fsnotify_open - file was opened
222 */
223static inline void fsnotify_open(struct dentry *dentry)
224{
225 struct inode *inode = dentry->d_inode;
90586523 226 __u32 mask = FS_OPEN;
0eeca283
RL
227
228 if (S_ISDIR(inode->i_mode))
90586523 229 mask |= FS_IN_ISDIR;
0eeca283 230
7c297722 231 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
90586523 232
c28f7e56 233 fsnotify_parent(dentry, mask);
62ffe5df 234 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL);
0eeca283
RL
235}
236
237/*
238 * fsnotify_close - file was closed
239 */
240static inline void fsnotify_close(struct file *file)
241{
0f7fc9e4 242 struct dentry *dentry = file->f_path.dentry;
0eeca283 243 struct inode *inode = dentry->d_inode;
aeb5d727 244 fmode_t mode = file->f_mode;
90586523 245 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
0eeca283
RL
246
247 if (S_ISDIR(inode->i_mode))
90586523 248 mask |= FS_IN_ISDIR;
0eeca283 249
7c297722 250 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
90586523 251
c28f7e56 252 fsnotify_parent(dentry, mask);
62ffe5df 253 fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL);
0eeca283
RL
254}
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;
90586523 262 __u32 mask = FS_ATTRIB;
0eeca283
RL
263
264 if (S_ISDIR(inode->i_mode))
90586523 265 mask |= FS_IN_ISDIR;
0eeca283 266
7c297722 267 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
90586523 268
c28f7e56 269 fsnotify_parent(dentry, mask);
62ffe5df 270 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL);
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
EP
302 mask |= FS_IN_ISDIR;
303 inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
c28f7e56 304
3c5119c0 305 fsnotify_parent(dentry, mask);
62ffe5df 306 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL);
0eeca283
RL
307 }
308}
309
90586523 310#if defined(CONFIG_INOTIFY) || defined(CONFIG_FSNOTIFY) /* notify helpers */
0eeca283
RL
311
312/*
313 * fsnotify_oldname_init - save off the old filename before we change it
314 */
315static inline const char *fsnotify_oldname_init(const char *name)
316{
317 return kstrdup(name, GFP_KERNEL);
318}
319
320/*
321 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
322 */
323static inline void fsnotify_oldname_free(const char *old_name)
324{
325 kfree(old_name);
326}
327
90586523 328#else /* CONFIG_INOTIFY || CONFIG_FSNOTIFY */
0eeca283
RL
329
330static inline const char *fsnotify_oldname_init(const char *name)
331{
332 return NULL;
333}
334
335static inline void fsnotify_oldname_free(const char *old_name)
336{
337}
338
339#endif /* ! CONFIG_INOTIFY */
340
0eeca283 341#endif /* _LINUX_FS_NOTIFY_H */