]>
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 | ||
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 | |
c32ccd87 | 20 | */ |
e61ce867 EP |
21 | static inline void fsnotify_d_instantiate(struct dentry *dentry, |
22 | struct inode *inode) | |
c32ccd87 | 23 | { |
e61ce867 | 24 | __fsnotify_d_instantiate(dentry, inode); |
c32ccd87 NP |
25 | } |
26 | ||
c28f7e56 | 27 | /* Notify this dentry's parent about a child's events. */ |
52420392 | 28 | static inline int fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask) |
c28f7e56 | 29 | { |
72acc854 | 30 | if (!dentry) |
2069601b | 31 | dentry = path->dentry; |
28c60e37 | 32 | |
52420392 | 33 | return __fsnotify_parent(path, dentry, mask); |
c28f7e56 EP |
34 | } |
35 | ||
c4ec54b4 EP |
36 | /* simple call site for access decisions */ |
37 | static inline int fsnotify_perm(struct file *file, int mask) | |
38 | { | |
2069601b LT |
39 | struct path *path = &file->f_path; |
40 | struct inode *inode = path->dentry->d_inode; | |
fb1cfb88 | 41 | __u32 fsnotify_mask = 0; |
52420392 | 42 | int ret; |
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 | |
52420392 EP |
55 | ret = fsnotify_parent(path, NULL, fsnotify_mask); |
56 | if (ret) | |
57 | return ret; | |
58 | ||
2069601b | 59 | return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); |
c4ec54b4 EP |
60 | } |
61 | ||
c32ccd87 | 62 | /* |
e61ce867 | 63 | * fsnotify_d_move - dentry has been moved |
c32ccd87 | 64 | */ |
e61ce867 | 65 | static inline void fsnotify_d_move(struct dentry *dentry) |
c32ccd87 | 66 | { |
c28f7e56 | 67 | /* |
e61ce867 EP |
68 | * On move we need to update dentry->d_flags to indicate if the new parent |
69 | * cares about events from this dentry. | |
c28f7e56 | 70 | */ |
e61ce867 | 71 | __fsnotify_update_dcache_flags(dentry); |
c32ccd87 NP |
72 | } |
73 | ||
90586523 EP |
74 | /* |
75 | * fsnotify_link_count - inode's link count changed | |
76 | */ | |
77 | static inline void fsnotify_link_count(struct inode *inode) | |
78 | { | |
47882c6f | 79 | fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
90586523 EP |
80 | } |
81 | ||
0eeca283 RL |
82 | /* |
83 | * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir | |
84 | */ | |
85 | static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, | |
59b0df21 | 86 | const unsigned char *old_name, |
5a190ae6 | 87 | int isdir, struct inode *target, struct dentry *moved) |
0eeca283 | 88 | { |
5a190ae6 | 89 | struct inode *source = moved->d_inode; |
47882c6f | 90 | u32 fs_cookie = fsnotify_get_cookie(); |
ff52cc21 EP |
91 | __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM); |
92 | __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO); | |
59b0df21 | 93 | const unsigned char *new_name = moved->d_name.name; |
0eeca283 | 94 | |
ff52cc21 EP |
95 | if (old_dir == new_dir) |
96 | old_dir_mask |= FS_DN_RENAME; | |
0eeca283 | 97 | |
90586523 | 98 | if (isdir) { |
b29866aa EP |
99 | old_dir_mask |= FS_ISDIR; |
100 | new_dir_mask |= FS_ISDIR; | |
90586523 EP |
101 | } |
102 | ||
47882c6f EP |
103 | fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie); |
104 | fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie); | |
90586523 | 105 | |
2dfc1cae | 106 | if (target) |
90586523 | 107 | fsnotify_link_count(target); |
89204c40 | 108 | |
2dfc1cae | 109 | if (source) |
47882c6f | 110 | fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
cccc6bba | 111 | audit_inode_child(moved, new_dir); |
0eeca283 RL |
112 | } |
113 | ||
3be25f49 EP |
114 | /* |
115 | * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed | |
116 | */ | |
117 | static inline void fsnotify_inode_delete(struct inode *inode) | |
118 | { | |
119 | __fsnotify_inode_delete(inode); | |
120 | } | |
121 | ||
ca9c726e AG |
122 | /* |
123 | * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed | |
124 | */ | |
125 | static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt) | |
126 | { | |
127 | __fsnotify_vfsmount_delete(mnt); | |
128 | } | |
129 | ||
7a91bf7f JM |
130 | /* |
131 | * fsnotify_nameremove - a filename was removed from a directory | |
132 | */ | |
133 | static inline void fsnotify_nameremove(struct dentry *dentry, int isdir) | |
134 | { | |
90586523 EP |
135 | __u32 mask = FS_DELETE; |
136 | ||
7a91bf7f | 137 | if (isdir) |
b29866aa | 138 | mask |= FS_ISDIR; |
c28f7e56 | 139 | |
28c60e37 | 140 | fsnotify_parent(NULL, dentry, mask); |
7a91bf7f JM |
141 | } |
142 | ||
143 | /* | |
144 | * fsnotify_inoderemove - an inode is going away | |
145 | */ | |
146 | static inline void fsnotify_inoderemove(struct inode *inode) | |
147 | { | |
47882c6f | 148 | fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
3be25f49 | 149 | __fsnotify_inode_delete(inode); |
ece95912 JK |
150 | } |
151 | ||
0eeca283 RL |
152 | /* |
153 | * fsnotify_create - 'name' was linked in | |
154 | */ | |
f38aa942 | 155 | static inline void fsnotify_create(struct inode *inode, struct dentry *dentry) |
0eeca283 | 156 | { |
cccc6bba | 157 | audit_inode_child(dentry, inode); |
90586523 | 158 | |
47882c6f | 159 | fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0); |
0eeca283 RL |
160 | } |
161 | ||
ece95912 JK |
162 | /* |
163 | * fsnotify_link - new hardlink in 'inode' directory | |
164 | * Note: We have to pass also the linked inode ptr as some filesystems leave | |
165 | * new_dentry->d_inode NULL and instantiate inode pointer later | |
166 | */ | |
167 | static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry) | |
168 | { | |
ece95912 | 169 | fsnotify_link_count(inode); |
cccc6bba | 170 | audit_inode_child(new_dentry, dir); |
90586523 | 171 | |
47882c6f | 172 | fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0); |
ece95912 JK |
173 | } |
174 | ||
0eeca283 RL |
175 | /* |
176 | * fsnotify_mkdir - directory 'name' was created | |
177 | */ | |
f38aa942 | 178 | static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry) |
0eeca283 | 179 | { |
b29866aa | 180 | __u32 mask = (FS_CREATE | FS_ISDIR); |
90586523 EP |
181 | struct inode *d_inode = dentry->d_inode; |
182 | ||
cccc6bba | 183 | audit_inode_child(dentry, inode); |
90586523 | 184 | |
47882c6f | 185 | fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0); |
0eeca283 RL |
186 | } |
187 | ||
188 | /* | |
189 | * fsnotify_access - file was read | |
190 | */ | |
2a12a9d7 | 191 | static inline void fsnotify_access(struct file *file) |
0eeca283 | 192 | { |
2069601b LT |
193 | struct path *path = &file->f_path; |
194 | struct inode *inode = path->dentry->d_inode; | |
90586523 | 195 | __u32 mask = FS_ACCESS; |
0eeca283 RL |
196 | |
197 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 198 | mask |= FS_ISDIR; |
0eeca283 | 199 | |
ecf081d1 | 200 | if (!(file->f_mode & FMODE_NONOTIFY)) { |
2069601b LT |
201 | fsnotify_parent(path, NULL, mask); |
202 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
ecf081d1 | 203 | } |
0eeca283 RL |
204 | } |
205 | ||
206 | /* | |
207 | * fsnotify_modify - file was modified | |
208 | */ | |
2a12a9d7 | 209 | static inline void fsnotify_modify(struct file *file) |
0eeca283 | 210 | { |
2069601b LT |
211 | struct path *path = &file->f_path; |
212 | struct inode *inode = path->dentry->d_inode; | |
90586523 | 213 | __u32 mask = FS_MODIFY; |
0eeca283 RL |
214 | |
215 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 216 | mask |= FS_ISDIR; |
0eeca283 | 217 | |
ecf081d1 | 218 | if (!(file->f_mode & FMODE_NONOTIFY)) { |
2069601b LT |
219 | fsnotify_parent(path, NULL, mask); |
220 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
ecf081d1 | 221 | } |
0eeca283 RL |
222 | } |
223 | ||
224 | /* | |
225 | * fsnotify_open - file was opened | |
226 | */ | |
2a12a9d7 | 227 | static inline void fsnotify_open(struct file *file) |
0eeca283 | 228 | { |
2069601b LT |
229 | struct path *path = &file->f_path; |
230 | struct inode *inode = path->dentry->d_inode; | |
90586523 | 231 | __u32 mask = FS_OPEN; |
0eeca283 RL |
232 | |
233 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 234 | mask |= FS_ISDIR; |
0eeca283 | 235 | |
6bff7ecc LS |
236 | fsnotify_parent(path, NULL, mask); |
237 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
0eeca283 RL |
238 | } |
239 | ||
240 | /* | |
241 | * fsnotify_close - file was closed | |
242 | */ | |
243 | static inline void fsnotify_close(struct file *file) | |
244 | { | |
2069601b | 245 | struct path *path = &file->f_path; |
28c60e37 | 246 | struct inode *inode = file->f_path.dentry->d_inode; |
aeb5d727 | 247 | fmode_t mode = file->f_mode; |
90586523 | 248 | __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE; |
0eeca283 RL |
249 | |
250 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 251 | mask |= FS_ISDIR; |
0eeca283 | 252 | |
ecf081d1 | 253 | if (!(file->f_mode & FMODE_NONOTIFY)) { |
2069601b LT |
254 | fsnotify_parent(path, NULL, mask); |
255 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
ecf081d1 | 256 | } |
0eeca283 RL |
257 | } |
258 | ||
259 | /* | |
260 | * fsnotify_xattr - extended attributes were changed | |
261 | */ | |
262 | static inline void fsnotify_xattr(struct dentry *dentry) | |
263 | { | |
264 | struct inode *inode = dentry->d_inode; | |
90586523 | 265 | __u32 mask = FS_ATTRIB; |
0eeca283 RL |
266 | |
267 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 268 | mask |= FS_ISDIR; |
0eeca283 | 269 | |
28c60e37 | 270 | fsnotify_parent(NULL, 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)) |
b29866aa | 303 | mask |= FS_ISDIR; |
28c60e37 EP |
304 | |
305 | fsnotify_parent(NULL, dentry, mask); | |
47882c6f | 306 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
307 | } |
308 | } | |
309 | ||
2dfc1cae | 310 | #if defined(CONFIG_FSNOTIFY) /* notify helpers */ |
0eeca283 RL |
311 | |
312 | /* | |
313 | * fsnotify_oldname_init - save off the old filename before we change it | |
314 | */ | |
59b0df21 | 315 | static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name) |
0eeca283 RL |
316 | { |
317 | return kstrdup(name, GFP_KERNEL); | |
318 | } | |
319 | ||
320 | /* | |
321 | * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init | |
322 | */ | |
59b0df21 | 323 | static inline void fsnotify_oldname_free(const unsigned char *old_name) |
0eeca283 RL |
324 | { |
325 | kfree(old_name); | |
326 | } | |
327 | ||
28c60e37 | 328 | #else /* CONFIG_FSNOTIFY */ |
0eeca283 | 329 | |
59b0df21 | 330 | static inline const char *fsnotify_oldname_init(const unsigned char *name) |
0eeca283 RL |
331 | { |
332 | return NULL; | |
333 | } | |
334 | ||
59b0df21 | 335 | static inline void fsnotify_oldname_free(const unsigned char *old_name) |
0eeca283 RL |
336 | { |
337 | } | |
338 | ||
28c60e37 | 339 | #endif /* CONFIG_FSNOTIFY */ |
0eeca283 | 340 | |
0eeca283 | 341 | #endif /* _LINUX_FS_NOTIFY_H */ |