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> |
187f1882 | 17 | #include <linux/bug.h> |
0eeca283 | 18 | |
c28f7e56 | 19 | /* Notify this dentry's parent about a child's events. */ |
52420392 | 20 | static inline int fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask) |
c28f7e56 | 21 | { |
72acc854 | 22 | if (!dentry) |
2069601b | 23 | dentry = path->dentry; |
28c60e37 | 24 | |
52420392 | 25 | return __fsnotify_parent(path, dentry, mask); |
c28f7e56 EP |
26 | } |
27 | ||
c4ec54b4 EP |
28 | /* simple call site for access decisions */ |
29 | static inline int fsnotify_perm(struct file *file, int mask) | |
30 | { | |
2069601b | 31 | struct path *path = &file->f_path; |
c77cecee | 32 | struct inode *inode = file_inode(file); |
fb1cfb88 | 33 | __u32 fsnotify_mask = 0; |
52420392 | 34 | int ret; |
c4ec54b4 EP |
35 | |
36 | if (file->f_mode & FMODE_NONOTIFY) | |
37 | return 0; | |
38 | if (!(mask & (MAY_READ | MAY_OPEN))) | |
39 | return 0; | |
c4ec54b4 EP |
40 | if (mask & MAY_OPEN) |
41 | fsnotify_mask = FS_OPEN_PERM; | |
fb1cfb88 EP |
42 | else if (mask & MAY_READ) |
43 | fsnotify_mask = FS_ACCESS_PERM; | |
44 | else | |
45 | BUG(); | |
c4ec54b4 | 46 | |
52420392 EP |
47 | ret = fsnotify_parent(path, NULL, fsnotify_mask); |
48 | if (ret) | |
49 | return ret; | |
50 | ||
2069601b | 51 | return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); |
c4ec54b4 EP |
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 | { | |
47882c6f | 59 | fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
90586523 EP |
60 | } |
61 | ||
0eeca283 RL |
62 | /* |
63 | * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir | |
64 | */ | |
65 | static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, | |
59b0df21 | 66 | const unsigned char *old_name, |
5a190ae6 | 67 | int isdir, struct inode *target, struct dentry *moved) |
0eeca283 | 68 | { |
5a190ae6 | 69 | struct inode *source = moved->d_inode; |
47882c6f | 70 | u32 fs_cookie = fsnotify_get_cookie(); |
ff52cc21 EP |
71 | __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM); |
72 | __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO); | |
59b0df21 | 73 | const unsigned char *new_name = moved->d_name.name; |
0eeca283 | 74 | |
ff52cc21 EP |
75 | if (old_dir == new_dir) |
76 | old_dir_mask |= FS_DN_RENAME; | |
0eeca283 | 77 | |
90586523 | 78 | if (isdir) { |
b29866aa EP |
79 | old_dir_mask |= FS_ISDIR; |
80 | new_dir_mask |= FS_ISDIR; | |
90586523 EP |
81 | } |
82 | ||
6ee8e25f JK |
83 | fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name, |
84 | fs_cookie); | |
85 | fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name, | |
86 | fs_cookie); | |
90586523 | 87 | |
2dfc1cae | 88 | if (target) |
90586523 | 89 | fsnotify_link_count(target); |
89204c40 | 90 | |
2dfc1cae | 91 | if (source) |
47882c6f | 92 | fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
4fa6b5ec | 93 | audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE); |
0eeca283 RL |
94 | } |
95 | ||
3be25f49 EP |
96 | /* |
97 | * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed | |
98 | */ | |
99 | static inline void fsnotify_inode_delete(struct inode *inode) | |
100 | { | |
101 | __fsnotify_inode_delete(inode); | |
102 | } | |
103 | ||
ca9c726e AG |
104 | /* |
105 | * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed | |
106 | */ | |
107 | static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt) | |
108 | { | |
109 | __fsnotify_vfsmount_delete(mnt); | |
110 | } | |
111 | ||
7a91bf7f JM |
112 | /* |
113 | * fsnotify_nameremove - a filename was removed from a directory | |
114 | */ | |
115 | static inline void fsnotify_nameremove(struct dentry *dentry, int isdir) | |
116 | { | |
90586523 EP |
117 | __u32 mask = FS_DELETE; |
118 | ||
7a91bf7f | 119 | if (isdir) |
b29866aa | 120 | mask |= FS_ISDIR; |
c28f7e56 | 121 | |
28c60e37 | 122 | fsnotify_parent(NULL, dentry, mask); |
7a91bf7f JM |
123 | } |
124 | ||
125 | /* | |
126 | * fsnotify_inoderemove - an inode is going away | |
127 | */ | |
128 | static inline void fsnotify_inoderemove(struct inode *inode) | |
129 | { | |
47882c6f | 130 | fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
3be25f49 | 131 | __fsnotify_inode_delete(inode); |
ece95912 JK |
132 | } |
133 | ||
0eeca283 RL |
134 | /* |
135 | * fsnotify_create - 'name' was linked in | |
136 | */ | |
f38aa942 | 137 | static inline void fsnotify_create(struct inode *inode, struct dentry *dentry) |
0eeca283 | 138 | { |
4fa6b5ec | 139 | audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 140 | |
47882c6f | 141 | fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0); |
0eeca283 RL |
142 | } |
143 | ||
ece95912 JK |
144 | /* |
145 | * fsnotify_link - new hardlink in 'inode' directory | |
146 | * Note: We have to pass also the linked inode ptr as some filesystems leave | |
147 | * new_dentry->d_inode NULL and instantiate inode pointer later | |
148 | */ | |
149 | static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry) | |
150 | { | |
ece95912 | 151 | fsnotify_link_count(inode); |
4fa6b5ec | 152 | audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 153 | |
47882c6f | 154 | fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0); |
ece95912 JK |
155 | } |
156 | ||
0eeca283 RL |
157 | /* |
158 | * fsnotify_mkdir - directory 'name' was created | |
159 | */ | |
f38aa942 | 160 | static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry) |
0eeca283 | 161 | { |
b29866aa | 162 | __u32 mask = (FS_CREATE | FS_ISDIR); |
90586523 EP |
163 | struct inode *d_inode = dentry->d_inode; |
164 | ||
4fa6b5ec | 165 | audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
90586523 | 166 | |
47882c6f | 167 | fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0); |
0eeca283 RL |
168 | } |
169 | ||
170 | /* | |
171 | * fsnotify_access - file was read | |
172 | */ | |
2a12a9d7 | 173 | static inline void fsnotify_access(struct file *file) |
0eeca283 | 174 | { |
2069601b | 175 | struct path *path = &file->f_path; |
c77cecee | 176 | struct inode *inode = file_inode(file); |
90586523 | 177 | __u32 mask = FS_ACCESS; |
0eeca283 RL |
178 | |
179 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 180 | mask |= FS_ISDIR; |
0eeca283 | 181 | |
ecf081d1 | 182 | if (!(file->f_mode & FMODE_NONOTIFY)) { |
2069601b LT |
183 | fsnotify_parent(path, NULL, mask); |
184 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
ecf081d1 | 185 | } |
0eeca283 RL |
186 | } |
187 | ||
188 | /* | |
189 | * fsnotify_modify - file was modified | |
190 | */ | |
2a12a9d7 | 191 | static inline void fsnotify_modify(struct file *file) |
0eeca283 | 192 | { |
2069601b | 193 | struct path *path = &file->f_path; |
c77cecee | 194 | struct inode *inode = file_inode(file); |
90586523 | 195 | __u32 mask = FS_MODIFY; |
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_open - file was opened | |
208 | */ | |
2a12a9d7 | 209 | static inline void fsnotify_open(struct file *file) |
0eeca283 | 210 | { |
2069601b | 211 | struct path *path = &file->f_path; |
c77cecee | 212 | struct inode *inode = file_inode(file); |
90586523 | 213 | __u32 mask = FS_OPEN; |
0eeca283 RL |
214 | |
215 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 216 | mask |= FS_ISDIR; |
0eeca283 | 217 | |
6bff7ecc LS |
218 | fsnotify_parent(path, NULL, mask); |
219 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
0eeca283 RL |
220 | } |
221 | ||
222 | /* | |
223 | * fsnotify_close - file was closed | |
224 | */ | |
225 | static inline void fsnotify_close(struct file *file) | |
226 | { | |
2069601b | 227 | struct path *path = &file->f_path; |
496ad9aa | 228 | struct inode *inode = file_inode(file); |
aeb5d727 | 229 | fmode_t mode = file->f_mode; |
90586523 | 230 | __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE; |
0eeca283 RL |
231 | |
232 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 233 | mask |= FS_ISDIR; |
0eeca283 | 234 | |
ecf081d1 | 235 | if (!(file->f_mode & FMODE_NONOTIFY)) { |
2069601b LT |
236 | fsnotify_parent(path, NULL, mask); |
237 | fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); | |
ecf081d1 | 238 | } |
0eeca283 RL |
239 | } |
240 | ||
241 | /* | |
242 | * fsnotify_xattr - extended attributes were changed | |
243 | */ | |
244 | static inline void fsnotify_xattr(struct dentry *dentry) | |
245 | { | |
246 | struct inode *inode = dentry->d_inode; | |
90586523 | 247 | __u32 mask = FS_ATTRIB; |
0eeca283 RL |
248 | |
249 | if (S_ISDIR(inode->i_mode)) | |
b29866aa | 250 | mask |= FS_ISDIR; |
0eeca283 | 251 | |
28c60e37 | 252 | fsnotify_parent(NULL, dentry, mask); |
47882c6f | 253 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
254 | } |
255 | ||
256 | /* | |
257 | * fsnotify_change - notify_change event. file was modified and/or metadata | |
258 | * was changed. | |
259 | */ | |
260 | static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid) | |
261 | { | |
262 | struct inode *inode = dentry->d_inode; | |
3c5119c0 EP |
263 | __u32 mask = 0; |
264 | ||
265 | if (ia_valid & ATTR_UID) | |
266 | mask |= FS_ATTRIB; | |
267 | if (ia_valid & ATTR_GID) | |
268 | mask |= FS_ATTRIB; | |
269 | if (ia_valid & ATTR_SIZE) | |
270 | mask |= FS_MODIFY; | |
0eeca283 | 271 | |
0eeca283 RL |
272 | /* both times implies a utime(s) call */ |
273 | if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) | |
3c5119c0 EP |
274 | mask |= FS_ATTRIB; |
275 | else if (ia_valid & ATTR_ATIME) | |
276 | mask |= FS_ACCESS; | |
277 | else if (ia_valid & ATTR_MTIME) | |
278 | mask |= FS_MODIFY; | |
279 | ||
280 | if (ia_valid & ATTR_MODE) | |
281 | mask |= FS_ATTRIB; | |
0eeca283 | 282 | |
3c5119c0 | 283 | if (mask) { |
0eeca283 | 284 | if (S_ISDIR(inode->i_mode)) |
b29866aa | 285 | mask |= FS_ISDIR; |
28c60e37 EP |
286 | |
287 | fsnotify_parent(NULL, dentry, mask); | |
47882c6f | 288 | fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); |
0eeca283 RL |
289 | } |
290 | } | |
291 | ||
2dfc1cae | 292 | #if defined(CONFIG_FSNOTIFY) /* notify helpers */ |
0eeca283 RL |
293 | |
294 | /* | |
295 | * fsnotify_oldname_init - save off the old filename before we change it | |
296 | */ | |
59b0df21 | 297 | static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name) |
0eeca283 RL |
298 | { |
299 | return kstrdup(name, GFP_KERNEL); | |
300 | } | |
301 | ||
302 | /* | |
303 | * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init | |
304 | */ | |
59b0df21 | 305 | static inline void fsnotify_oldname_free(const unsigned char *old_name) |
0eeca283 RL |
306 | { |
307 | kfree(old_name); | |
308 | } | |
309 | ||
28c60e37 | 310 | #else /* CONFIG_FSNOTIFY */ |
0eeca283 | 311 | |
59b0df21 | 312 | static inline const char *fsnotify_oldname_init(const unsigned char *name) |
0eeca283 RL |
313 | { |
314 | return NULL; | |
315 | } | |
316 | ||
59b0df21 | 317 | static inline void fsnotify_oldname_free(const unsigned char *old_name) |
0eeca283 RL |
318 | { |
319 | } | |
320 | ||
28c60e37 | 321 | #endif /* CONFIG_FSNOTIFY */ |
0eeca283 | 322 | |
0eeca283 | 323 | #endif /* _LINUX_FS_NOTIFY_H */ |