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