]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/fsnotify.h
vfs: introduce FMODE_NONOTIFY
[karo-tx-linux.git] / include / linux / fsnotify.h
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
14 #include <linux/fsnotify_backend.h>
15 #include <linux/audit.h>
16 #include <linux/slab.h>
17
18 /*
19  * fsnotify_d_instantiate - instantiate a dentry for inode
20  * Called with dcache_lock held.
21  */
22 static inline void fsnotify_d_instantiate(struct dentry *dentry,
23                                           struct inode *inode)
24 {
25         __fsnotify_d_instantiate(dentry, inode);
26 }
27
28 /* Notify this dentry's parent about a child's events. */
29 static inline void fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
30 {
31         if (!dentry)
32                 dentry = path->dentry;
33
34         __fsnotify_parent(path, dentry, mask);
35 }
36
37 /*
38  * fsnotify_d_move - dentry has been moved
39  * Called with dcache_lock and dentry->d_lock held.
40  */
41 static inline void fsnotify_d_move(struct dentry *dentry)
42 {
43         /*
44          * On move we need to update dentry->d_flags to indicate if the new parent
45          * cares about events from this dentry.
46          */
47         __fsnotify_update_dcache_flags(dentry);
48 }
49
50 /*
51  * fsnotify_link_count - inode's link count changed
52  */
53 static inline void fsnotify_link_count(struct inode *inode)
54 {
55         fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
56 }
57
58 /*
59  * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
60  */
61 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
62                                  const char *old_name,
63                                  int isdir, struct inode *target, struct dentry *moved)
64 {
65         struct inode *source = moved->d_inode;
66         u32 fs_cookie = fsnotify_get_cookie();
67         __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
68         __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
69         const char *new_name = moved->d_name.name;
70
71         if (old_dir == new_dir)
72                 old_dir_mask |= FS_DN_RENAME;
73
74         if (isdir) {
75                 old_dir_mask |= FS_IN_ISDIR;
76                 new_dir_mask |= FS_IN_ISDIR;
77         }
78
79         fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
80         fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
81
82         if (target)
83                 fsnotify_link_count(target);
84
85         if (source)
86                 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
87         audit_inode_child(moved, new_dir);
88 }
89
90 /*
91  * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
92  */
93 static inline void fsnotify_inode_delete(struct inode *inode)
94 {
95         __fsnotify_inode_delete(inode);
96 }
97
98 /*
99  * fsnotify_nameremove - a filename was removed from a directory
100  */
101 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
102 {
103         __u32 mask = FS_DELETE;
104
105         if (isdir)
106                 mask |= FS_IN_ISDIR;
107
108         fsnotify_parent(NULL, dentry, mask);
109 }
110
111 /*
112  * fsnotify_inoderemove - an inode is going away
113  */
114 static inline void fsnotify_inoderemove(struct inode *inode)
115 {
116         fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
117         __fsnotify_inode_delete(inode);
118 }
119
120 /*
121  * fsnotify_create - 'name' was linked in
122  */
123 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
124 {
125         audit_inode_child(dentry, inode);
126
127         fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
128 }
129
130 /*
131  * fsnotify_link - new hardlink in 'inode' directory
132  * Note: We have to pass also the linked inode ptr as some filesystems leave
133  *   new_dentry->d_inode NULL and instantiate inode pointer later
134  */
135 static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
136 {
137         fsnotify_link_count(inode);
138         audit_inode_child(new_dentry, dir);
139
140         fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
141 }
142
143 /*
144  * fsnotify_mkdir - directory 'name' was created
145  */
146 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
147 {
148         __u32 mask = (FS_CREATE | FS_IN_ISDIR);
149         struct inode *d_inode = dentry->d_inode;
150
151         audit_inode_child(dentry, inode);
152
153         fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
154 }
155
156 /*
157  * fsnotify_access - file was read
158  */
159 static inline void fsnotify_access(struct file *file)
160 {
161         struct path *path = &file->f_path;
162         struct inode *inode = path->dentry->d_inode;
163         __u32 mask = FS_ACCESS;
164
165         if (S_ISDIR(inode->i_mode))
166                 mask |= FS_IN_ISDIR;
167
168         if (!(file->f_mode & FMODE_NONOTIFY)) {
169                 fsnotify_parent(path, NULL, mask);
170                 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
171         }
172 }
173
174 /*
175  * fsnotify_modify - file was modified
176  */
177 static inline void fsnotify_modify(struct file *file)
178 {
179         struct path *path = &file->f_path;
180         struct inode *inode = path->dentry->d_inode;
181         __u32 mask = FS_MODIFY;
182
183         if (S_ISDIR(inode->i_mode))
184                 mask |= FS_IN_ISDIR;
185
186         if (!(file->f_mode & FMODE_NONOTIFY)) {
187                 fsnotify_parent(path, NULL, mask);
188                 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
189         }
190 }
191
192 /*
193  * fsnotify_open - file was opened
194  */
195 static inline void fsnotify_open(struct file *file)
196 {
197         struct path *path = &file->f_path;
198         struct inode *inode = path->dentry->d_inode;
199         __u32 mask = FS_OPEN;
200
201         if (S_ISDIR(inode->i_mode))
202                 mask |= FS_IN_ISDIR;
203
204         if (!(file->f_mode & FMODE_NONOTIFY)) {
205                 fsnotify_parent(path, NULL, mask);
206                 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
207         }
208 }
209
210 /*
211  * fsnotify_close - file was closed
212  */
213 static inline void fsnotify_close(struct file *file)
214 {
215         struct path *path = &file->f_path;
216         struct inode *inode = file->f_path.dentry->d_inode;
217         fmode_t mode = file->f_mode;
218         __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
219
220         if (S_ISDIR(inode->i_mode))
221                 mask |= FS_IN_ISDIR;
222
223         if (!(file->f_mode & FMODE_NONOTIFY)) {
224                 fsnotify_parent(path, NULL, mask);
225                 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
226         }
227 }
228
229 /*
230  * fsnotify_xattr - extended attributes were changed
231  */
232 static inline void fsnotify_xattr(struct dentry *dentry)
233 {
234         struct inode *inode = dentry->d_inode;
235         __u32 mask = FS_ATTRIB;
236
237         if (S_ISDIR(inode->i_mode))
238                 mask |= FS_IN_ISDIR;
239
240         fsnotify_parent(NULL, dentry, mask);
241         fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
242 }
243
244 /*
245  * fsnotify_change - notify_change event.  file was modified and/or metadata
246  * was changed.
247  */
248 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
249 {
250         struct inode *inode = dentry->d_inode;
251         __u32 mask = 0;
252
253         if (ia_valid & ATTR_UID)
254                 mask |= FS_ATTRIB;
255         if (ia_valid & ATTR_GID)
256                 mask |= FS_ATTRIB;
257         if (ia_valid & ATTR_SIZE)
258                 mask |= FS_MODIFY;
259
260         /* both times implies a utime(s) call */
261         if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
262                 mask |= FS_ATTRIB;
263         else if (ia_valid & ATTR_ATIME)
264                 mask |= FS_ACCESS;
265         else if (ia_valid & ATTR_MTIME)
266                 mask |= FS_MODIFY;
267
268         if (ia_valid & ATTR_MODE)
269                 mask |= FS_ATTRIB;
270
271         if (mask) {
272                 if (S_ISDIR(inode->i_mode))
273                         mask |= FS_IN_ISDIR;
274
275                 fsnotify_parent(NULL, dentry, mask);
276                 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
277         }
278 }
279
280 #if defined(CONFIG_FSNOTIFY)    /* notify helpers */
281
282 /*
283  * fsnotify_oldname_init - save off the old filename before we change it
284  */
285 static inline const char *fsnotify_oldname_init(const char *name)
286 {
287         return kstrdup(name, GFP_KERNEL);
288 }
289
290 /*
291  * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
292  */
293 static inline void fsnotify_oldname_free(const char *old_name)
294 {
295         kfree(old_name);
296 }
297
298 #else   /* CONFIG_FSNOTIFY */
299
300 static inline const char *fsnotify_oldname_init(const char *name)
301 {
302         return NULL;
303 }
304
305 static inline void fsnotify_oldname_free(const char *old_name)
306 {
307 }
308
309 #endif  /*  CONFIG_FSNOTIFY */
310
311 #endif  /* _LINUX_FS_NOTIFY_H */