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