Commit | Line | Data |
---|---|---|
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 | */ | |
23 | static 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. */ |
32 | static 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 | */ | |
43 | static 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 | */ | |
57 | static inline void fsnotify_link_count(struct inode *inode) | |
58 | { | |
59 | inotify_inode_queue_event(inode, IN_ATTRIB, 0, NULL, NULL); | |
60 | ||
47882c6f | 61 | fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
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 | */ | |
67 | static 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; |
47882c6f EP |
72 | u32 in_cookie = inotify_get_cookie(); |
73 | u32 fs_cookie = fsnotify_get_cookie(); | |
90586523 EP |
74 | __u32 old_dir_mask = 0; |
75 | __u32 new_dir_mask = 0; | |
0eeca283 | 76 | |
90586523 | 77 | if (old_dir == new_dir) { |
90586523 | 78 | old_dir_mask = FS_DN_RENAME; |
0eeca283 RL |
79 | } |
80 | ||
90586523 | 81 | if (isdir) { |
0eeca283 | 82 | isdir = IN_ISDIR; |
90586523 EP |
83 | old_dir_mask |= FS_IN_ISDIR; |
84 | new_dir_mask |= FS_IN_ISDIR; | |
85 | } | |
86 | ||
87 | old_dir_mask |= FS_MOVED_FROM; | |
88 | new_dir_mask |= FS_MOVED_TO; | |
89 | ||
47882c6f | 90 | inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir, in_cookie, old_name, |
7c297722 | 91 | source); |
47882c6f | 92 | inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, in_cookie, new_name, |
7c297722 | 93 | source); |
75449536 | 94 | |
47882c6f EP |
95 | fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie); |
96 | fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie); | |
90586523 | 97 | |
75449536 | 98 | if (target) { |
7c297722 | 99 | inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL); |
75449536 | 100 | inotify_inode_is_dead(target); |
90586523 EP |
101 | |
102 | /* this is really a link_count change not a removal */ | |
103 | fsnotify_link_count(target); | |
75449536 | 104 | } |
89204c40 JM |
105 | |
106 | if (source) { | |
7c297722 | 107 | inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL); |
47882c6f | 108 | fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
89204c40 | 109 | } |
5a190ae6 | 110 | audit_inode_child(new_name, moved, new_dir); |
0eeca283 RL |
111 | } |
112 | ||
3be25f49 EP |
113 | /* |
114 | * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed | |
115 | */ | |
116 | static inline void fsnotify_inode_delete(struct inode *inode) | |
117 | { | |
118 | __fsnotify_inode_delete(inode); | |
119 | } | |
120 | ||
7a91bf7f JM |
121 | /* |
122 | * fsnotify_nameremove - a filename was removed from a directory | |
123 | */ | |
124 | static inline void fsnotify_nameremove(struct dentry *dentry, int isdir) | |
125 | { | |
90586523 EP |
126 | __u32 mask = FS_DELETE; |
127 | ||
7a91bf7f | 128 | if (isdir) |
90586523 | 129 | mask |= FS_IN_ISDIR; |
c28f7e56 EP |
130 | |
131 | fsnotify_parent(dentry, mask); | |
7a91bf7f JM |
132 | } |
133 | ||
134 | /* | |
135 | * fsnotify_inoderemove - an inode is going away | |
136 | */ | |
137 | static inline void fsnotify_inoderemove(struct inode *inode) | |
138 | { | |
7c297722 | 139 | inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL, NULL); |
7a91bf7f | 140 | inotify_inode_is_dead(inode); |
7a91bf7f | 141 | |
47882c6f | 142 | fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
3be25f49 | 143 | __fsnotify_inode_delete(inode); |
ece95912 JK |
144 | } |
145 | ||
0eeca283 RL |
146 | /* |
147 | * fsnotify_create - 'name' was linked in | |
148 | */ | |
f38aa942 | 149 | static inline void fsnotify_create(struct inode *inode, struct dentry *dentry) |
0eeca283 | 150 | { |
7c297722 AG |
151 | inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name, |
152 | dentry->d_inode); | |
5a190ae6 | 153 | audit_inode_child(dentry->d_name.name, dentry, inode); |
90586523 | 154 | |
47882c6f | 155 | fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0); |
0eeca283 RL |
156 | } |
157 | ||
ece95912 JK |
158 | /* |
159 | * fsnotify_link - new hardlink in 'inode' directory | |
160 | * Note: We have to pass also the linked inode ptr as some filesystems leave | |
161 | * new_dentry->d_inode NULL and instantiate inode pointer later | |
162 | */ | |
163 | static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry) | |
164 | { | |
ece95912 JK |
165 | inotify_inode_queue_event(dir, IN_CREATE, 0, new_dentry->d_name.name, |
166 | inode); | |
167 | fsnotify_link_count(inode); | |
168 | audit_inode_child(new_dentry->d_name.name, new_dentry, dir); | |
90586523 | 169 | |
47882c6f | 170 | fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0); |
ece95912 JK |
171 | } |
172 | ||
0eeca283 RL |
173 | /* |
174 | * fsnotify_mkdir - directory 'name' was created | |
175 | */ | |
f38aa942 | 176 | static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry) |
0eeca283 | 177 | { |
90586523 EP |
178 | __u32 mask = (FS_CREATE | FS_IN_ISDIR); |
179 | struct inode *d_inode = dentry->d_inode; | |
180 | ||
90586523 | 181 | inotify_inode_queue_event(inode, mask, 0, dentry->d_name.name, d_inode); |
5a190ae6 | 182 | audit_inode_child(dentry->d_name.name, dentry, inode); |
90586523 | 183 | |
47882c6f | 184 | fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0); |
0eeca283 RL |
185 | } |
186 | ||
187 | /* | |
188 | * fsnotify_access - file was read | |
189 | */ | |
190 | static inline void fsnotify_access(struct dentry *dentry) | |
191 | { | |
192 | struct inode *inode = dentry->d_inode; | |
90586523 | 193 | __u32 mask = FS_ACCESS; |
0eeca283 RL |
194 | |
195 | if (S_ISDIR(inode->i_mode)) | |
90586523 | 196 | mask |= FS_IN_ISDIR; |
0eeca283 | 197 | |
7c297722 | 198 | inotify_inode_queue_event(inode, mask, 0, NULL, NULL); |
90586523 | 199 | |
c28f7e56 | 200 | fsnotify_parent(dentry, mask); |
47882c6f | 201 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
202 | } |
203 | ||
204 | /* | |
205 | * fsnotify_modify - file was modified | |
206 | */ | |
207 | static inline void fsnotify_modify(struct dentry *dentry) | |
208 | { | |
209 | struct inode *inode = 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 | |
7c297722 | 215 | inotify_inode_queue_event(inode, mask, 0, NULL, NULL); |
90586523 | 216 | |
c28f7e56 | 217 | fsnotify_parent(dentry, mask); |
47882c6f | 218 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
219 | } |
220 | ||
221 | /* | |
222 | * fsnotify_open - file was opened | |
223 | */ | |
224 | static inline void fsnotify_open(struct dentry *dentry) | |
225 | { | |
226 | struct inode *inode = dentry->d_inode; | |
90586523 | 227 | __u32 mask = FS_OPEN; |
0eeca283 RL |
228 | |
229 | if (S_ISDIR(inode->i_mode)) | |
90586523 | 230 | mask |= FS_IN_ISDIR; |
0eeca283 | 231 | |
7c297722 | 232 | inotify_inode_queue_event(inode, mask, 0, NULL, NULL); |
90586523 | 233 | |
c28f7e56 | 234 | fsnotify_parent(dentry, mask); |
47882c6f | 235 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
236 | } |
237 | ||
238 | /* | |
239 | * fsnotify_close - file was closed | |
240 | */ | |
241 | static inline void fsnotify_close(struct file *file) | |
242 | { | |
0f7fc9e4 | 243 | struct dentry *dentry = file->f_path.dentry; |
0eeca283 | 244 | struct inode *inode = dentry->d_inode; |
aeb5d727 | 245 | fmode_t mode = file->f_mode; |
90586523 | 246 | __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE; |
0eeca283 RL |
247 | |
248 | if (S_ISDIR(inode->i_mode)) | |
90586523 | 249 | mask |= FS_IN_ISDIR; |
0eeca283 | 250 | |
7c297722 | 251 | inotify_inode_queue_event(inode, mask, 0, NULL, NULL); |
90586523 | 252 | |
c28f7e56 | 253 | fsnotify_parent(dentry, mask); |
47882c6f | 254 | fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0); |
0eeca283 RL |
255 | } |
256 | ||
257 | /* | |
258 | * fsnotify_xattr - extended attributes were changed | |
259 | */ | |
260 | static inline void fsnotify_xattr(struct dentry *dentry) | |
261 | { | |
262 | struct inode *inode = dentry->d_inode; | |
90586523 | 263 | __u32 mask = FS_ATTRIB; |
0eeca283 RL |
264 | |
265 | if (S_ISDIR(inode->i_mode)) | |
90586523 | 266 | mask |= FS_IN_ISDIR; |
0eeca283 | 267 | |
7c297722 | 268 | inotify_inode_queue_event(inode, mask, 0, NULL, NULL); |
90586523 | 269 | |
c28f7e56 | 270 | fsnotify_parent(dentry, mask); |
47882c6f | 271 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
272 | } |
273 | ||
274 | /* | |
275 | * fsnotify_change - notify_change event. file was modified and/or metadata | |
276 | * was changed. | |
277 | */ | |
278 | static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid) | |
279 | { | |
280 | struct inode *inode = dentry->d_inode; | |
3c5119c0 EP |
281 | __u32 mask = 0; |
282 | ||
283 | if (ia_valid & ATTR_UID) | |
284 | mask |= FS_ATTRIB; | |
285 | if (ia_valid & ATTR_GID) | |
286 | mask |= FS_ATTRIB; | |
287 | if (ia_valid & ATTR_SIZE) | |
288 | mask |= FS_MODIFY; | |
0eeca283 | 289 | |
0eeca283 RL |
290 | /* both times implies a utime(s) call */ |
291 | if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) | |
3c5119c0 EP |
292 | mask |= FS_ATTRIB; |
293 | else if (ia_valid & ATTR_ATIME) | |
294 | mask |= FS_ACCESS; | |
295 | else if (ia_valid & ATTR_MTIME) | |
296 | mask |= FS_MODIFY; | |
297 | ||
298 | if (ia_valid & ATTR_MODE) | |
299 | mask |= FS_ATTRIB; | |
0eeca283 | 300 | |
3c5119c0 | 301 | if (mask) { |
0eeca283 | 302 | if (S_ISDIR(inode->i_mode)) |
3c5119c0 EP |
303 | mask |= FS_IN_ISDIR; |
304 | inotify_inode_queue_event(inode, mask, 0, NULL, NULL); | |
c28f7e56 | 305 | |
3c5119c0 | 306 | fsnotify_parent(dentry, mask); |
47882c6f | 307 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
308 | } |
309 | } | |
310 | ||
90586523 | 311 | #if defined(CONFIG_INOTIFY) || defined(CONFIG_FSNOTIFY) /* notify helpers */ |
0eeca283 RL |
312 | |
313 | /* | |
314 | * fsnotify_oldname_init - save off the old filename before we change it | |
315 | */ | |
316 | static inline const char *fsnotify_oldname_init(const char *name) | |
317 | { | |
318 | return kstrdup(name, GFP_KERNEL); | |
319 | } | |
320 | ||
321 | /* | |
322 | * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init | |
323 | */ | |
324 | static inline void fsnotify_oldname_free(const char *old_name) | |
325 | { | |
326 | kfree(old_name); | |
327 | } | |
328 | ||
90586523 | 329 | #else /* CONFIG_INOTIFY || CONFIG_FSNOTIFY */ |
0eeca283 RL |
330 | |
331 | static inline const char *fsnotify_oldname_init(const char *name) | |
332 | { | |
333 | return NULL; | |
334 | } | |
335 | ||
336 | static inline void fsnotify_oldname_free(const char *old_name) | |
337 | { | |
338 | } | |
339 | ||
340 | #endif /* ! CONFIG_INOTIFY */ | |
341 | ||
0eeca283 | 342 | #endif /* _LINUX_FS_NOTIFY_H */ |