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 */ 21 static inline void fsnotify_d_instantiate(struct dentry *dentry, 22 struct inode *inode) 23 { 24 __fsnotify_d_instantiate(dentry, inode); 25 } 26 27 /* Notify this dentry's parent about a child's events. */ 28 static inline int fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask) 29 { 30 if (!dentry) 31 dentry = path->dentry; 32 33 return __fsnotify_parent(path, dentry, mask); 34 } 35 36 /* simple call site for access decisions */ 37 static inline int fsnotify_perm(struct file *file, int mask) 38 { 39 struct path *path = &file->f_path; 40 struct inode *inode = path->dentry->d_inode; 41 __u32 fsnotify_mask = 0; 42 int ret; 43 44 if (file->f_mode & FMODE_NONOTIFY) 45 return 0; 46 if (!(mask & (MAY_READ | MAY_OPEN))) 47 return 0; 48 if (mask & MAY_OPEN) 49 fsnotify_mask = FS_OPEN_PERM; 50 else if (mask & MAY_READ) 51 fsnotify_mask = FS_ACCESS_PERM; 52 else 53 BUG(); 54 55 ret = fsnotify_parent(path, NULL, fsnotify_mask); 56 if (ret) 57 return ret; 58 59 return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); 60 } 61 62 /* 63 * fsnotify_d_move - dentry has been moved 64 */ 65 static inline void fsnotify_d_move(struct dentry *dentry) 66 { 67 /* 68 * On move we need to update dentry->d_flags to indicate if the new parent 69 * cares about events from this dentry. 70 */ 71 __fsnotify_update_dcache_flags(dentry); 72 } 73 74 /* 75 * fsnotify_link_count - inode's link count changed 76 */ 77 static inline void fsnotify_link_count(struct inode *inode) 78 { 79 fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0); 80 } 81 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, 86 const unsigned char *old_name, 87 int isdir, struct inode *target, struct dentry *moved) 88 { 89 struct inode *source = moved->d_inode; 90 u32 fs_cookie = fsnotify_get_cookie(); 91 __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM); 92 __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO); 93 const unsigned char *new_name = moved->d_name.name; 94 95 if (old_dir == new_dir) 96 old_dir_mask |= FS_DN_RENAME; 97 98 if (isdir) { 99 old_dir_mask |= FS_ISDIR; 100 new_dir_mask |= FS_ISDIR; 101 } 102 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); 105 106 if (target) 107 fsnotify_link_count(target); 108 109 if (source) 110 fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0); 111 audit_inode_child(moved, new_dir); 112 } 113 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 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 130 /* 131 * fsnotify_nameremove - a filename was removed from a directory 132 */ 133 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir) 134 { 135 __u32 mask = FS_DELETE; 136 137 if (isdir) 138 mask |= FS_ISDIR; 139 140 fsnotify_parent(NULL, dentry, mask); 141 } 142 143 /* 144 * fsnotify_inoderemove - an inode is going away 145 */ 146 static inline void fsnotify_inoderemove(struct inode *inode) 147 { 148 fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0); 149 __fsnotify_inode_delete(inode); 150 } 151 152 /* 153 * fsnotify_create - 'name' was linked in 154 */ 155 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry) 156 { 157 audit_inode_child(dentry, inode); 158 159 fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0); 160 } 161 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 { 169 fsnotify_link_count(inode); 170 audit_inode_child(new_dentry, dir); 171 172 fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0); 173 } 174 175 /* 176 * fsnotify_mkdir - directory 'name' was created 177 */ 178 static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry) 179 { 180 __u32 mask = (FS_CREATE | FS_ISDIR); 181 struct inode *d_inode = dentry->d_inode; 182 183 audit_inode_child(dentry, inode); 184 185 fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0); 186 } 187 188 /* 189 * fsnotify_access - file was read 190 */ 191 static inline void fsnotify_access(struct file *file) 192 { 193 struct path *path = &file->f_path; 194 struct inode *inode = path->dentry->d_inode; 195 __u32 mask = FS_ACCESS; 196 197 if (S_ISDIR(inode->i_mode)) 198 mask |= FS_ISDIR; 199 200 if (!(file->f_mode & FMODE_NONOTIFY)) { 201 fsnotify_parent(path, NULL, mask); 202 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); 203 } 204 } 205 206 /* 207 * fsnotify_modify - file was modified 208 */ 209 static inline void fsnotify_modify(struct file *file) 210 { 211 struct path *path = &file->f_path; 212 struct inode *inode = path->dentry->d_inode; 213 __u32 mask = FS_MODIFY; 214 215 if (S_ISDIR(inode->i_mode)) 216 mask |= FS_ISDIR; 217 218 if (!(file->f_mode & FMODE_NONOTIFY)) { 219 fsnotify_parent(path, NULL, mask); 220 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); 221 } 222 } 223 224 /* 225 * fsnotify_open - file was opened 226 */ 227 static inline void fsnotify_open(struct file *file) 228 { 229 struct path *path = &file->f_path; 230 struct inode *inode = path->dentry->d_inode; 231 __u32 mask = FS_OPEN; 232 233 if (S_ISDIR(inode->i_mode)) 234 mask |= FS_ISDIR; 235 236 fsnotify_parent(path, NULL, mask); 237 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); 238 } 239 240 /* 241 * fsnotify_close - file was closed 242 */ 243 static inline void fsnotify_close(struct file *file) 244 { 245 struct path *path = &file->f_path; 246 struct inode *inode = file->f_path.dentry->d_inode; 247 fmode_t mode = file->f_mode; 248 __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE; 249 250 if (S_ISDIR(inode->i_mode)) 251 mask |= FS_ISDIR; 252 253 if (!(file->f_mode & FMODE_NONOTIFY)) { 254 fsnotify_parent(path, NULL, mask); 255 fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0); 256 } 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; 265 __u32 mask = FS_ATTRIB; 266 267 if (S_ISDIR(inode->i_mode)) 268 mask |= FS_ISDIR; 269 270 fsnotify_parent(NULL, dentry, mask); 271 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); 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; 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; 289 290 /* both times implies a utime(s) call */ 291 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) 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; 300 301 if (mask) { 302 if (S_ISDIR(inode->i_mode)) 303 mask |= FS_ISDIR; 304 305 fsnotify_parent(NULL, dentry, mask); 306 fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0); 307 } 308 } 309 310 #if defined(CONFIG_FSNOTIFY) /* notify helpers */ 311 312 /* 313 * fsnotify_oldname_init - save off the old filename before we change it 314 */ 315 static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name) 316 { 317 return kstrdup(name, GFP_KERNEL); 318 } 319 320 /* 321 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init 322 */ 323 static inline void fsnotify_oldname_free(const unsigned char *old_name) 324 { 325 kfree(old_name); 326 } 327 328 #else /* CONFIG_FSNOTIFY */ 329 330 static inline const char *fsnotify_oldname_init(const unsigned char *name) 331 { 332 return NULL; 333 } 334 335 static inline void fsnotify_oldname_free(const unsigned char *old_name) 336 { 337 } 338 339 #endif /* CONFIG_FSNOTIFY */ 340 341 #endif /* _LINUX_FS_NOTIFY_H */ 342