1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_FS_NOTIFY_H 3 #define _LINUX_FS_NOTIFY_H 4 5 /* 6 * include/linux/fsnotify.h - generic hooks for filesystem notification, to 7 * reduce in-source duplication from both dnotify and inotify. 8 * 9 * We don't compile any of this away in some complicated menagerie of ifdefs. 10 * Instead, we rely on the code inside to optimize away as needed. 11 * 12 * (C) Copyright 2005 Robert Love 13 */ 14 15 #include <linux/fsnotify_backend.h> 16 #include <linux/audit.h> 17 #include <linux/slab.h> 18 #include <linux/bug.h> 19 20 /* Are there any inode/mount/sb objects that are being watched at all? */ 21 static inline bool fsnotify_sb_has_watchers(struct super_block *sb) 22 { 23 struct fsnotify_sb_info *sbinfo = fsnotify_sb_info(sb); 24 25 /* Were any marks ever added to any object on this sb? */ 26 if (!sbinfo) 27 return false; 28 29 return atomic_long_read(&sbinfo->watched_objects); 30 } 31 32 /* 33 * Notify this @dir inode about a change in a child directory entry. 34 * The directory entry may have turned positive or negative or its inode may 35 * have changed (i.e. renamed over). 36 * 37 * Unlike fsnotify_parent(), the event will be reported regardless of the 38 * FS_EVENT_ON_CHILD mask on the parent inode and will not be reported if only 39 * the child is interested and not the parent. 40 */ 41 static inline int fsnotify_name(__u32 mask, const void *data, int data_type, 42 struct inode *dir, const struct qstr *name, 43 u32 cookie) 44 { 45 if (!fsnotify_sb_has_watchers(dir->i_sb)) 46 return 0; 47 48 return fsnotify(mask, data, data_type, dir, name, NULL, cookie); 49 } 50 51 static inline void fsnotify_dirent(struct inode *dir, struct dentry *dentry, 52 __u32 mask) 53 { 54 fsnotify_name(mask, dentry, FSNOTIFY_EVENT_DENTRY, dir, &dentry->d_name, 0); 55 } 56 57 static inline void fsnotify_inode(struct inode *inode, __u32 mask) 58 { 59 if (!fsnotify_sb_has_watchers(inode->i_sb)) 60 return; 61 62 if (S_ISDIR(inode->i_mode)) 63 mask |= FS_ISDIR; 64 65 fsnotify(mask, inode, FSNOTIFY_EVENT_INODE, NULL, NULL, inode, 0); 66 } 67 68 /* Notify this dentry's parent about a child's events. */ 69 static inline int fsnotify_parent(struct dentry *dentry, __u32 mask, 70 const void *data, int data_type) 71 { 72 struct inode *inode = d_inode(dentry); 73 74 if (!fsnotify_sb_has_watchers(inode->i_sb)) 75 return 0; 76 77 if (S_ISDIR(inode->i_mode)) { 78 mask |= FS_ISDIR; 79 80 /* sb/mount marks are not interested in name of directory */ 81 if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED)) 82 goto notify_child; 83 } 84 85 /* disconnected dentry cannot notify parent */ 86 if (IS_ROOT(dentry)) 87 goto notify_child; 88 89 return __fsnotify_parent(dentry, mask, data, data_type); 90 91 notify_child: 92 return fsnotify(mask, data, data_type, NULL, NULL, inode, 0); 93 } 94 95 /* 96 * Simple wrappers to consolidate calls to fsnotify_parent() when an event 97 * is on a file/dentry. 98 */ 99 static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask) 100 { 101 fsnotify_parent(dentry, mask, dentry, FSNOTIFY_EVENT_DENTRY); 102 } 103 104 static inline int fsnotify_file(struct file *file, __u32 mask) 105 { 106 const struct path *path; 107 108 if (file->f_mode & FMODE_NONOTIFY) 109 return 0; 110 111 path = &file->f_path; 112 return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH); 113 } 114 115 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS 116 /* 117 * fsnotify_file_area_perm - permission hook before access to file range 118 */ 119 static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, 120 const loff_t *ppos, size_t count) 121 { 122 __u32 fsnotify_mask = FS_ACCESS_PERM; 123 124 /* 125 * filesystem may be modified in the context of permission events 126 * (e.g. by HSM filling a file on access), so sb freeze protection 127 * must not be held. 128 */ 129 lockdep_assert_once(file_write_not_started(file)); 130 131 if (!(perm_mask & MAY_READ)) 132 return 0; 133 134 return fsnotify_file(file, fsnotify_mask); 135 } 136 137 /* 138 * fsnotify_file_perm - permission hook before file access 139 */ 140 static inline int fsnotify_file_perm(struct file *file, int perm_mask) 141 { 142 return fsnotify_file_area_perm(file, perm_mask, NULL, 0); 143 } 144 145 /* 146 * fsnotify_open_perm - permission hook before file open 147 */ 148 static inline int fsnotify_open_perm(struct file *file) 149 { 150 int ret; 151 152 if (file->f_flags & __FMODE_EXEC) { 153 ret = fsnotify_file(file, FS_OPEN_EXEC_PERM); 154 if (ret) 155 return ret; 156 } 157 158 return fsnotify_file(file, FS_OPEN_PERM); 159 } 160 161 #else 162 static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, 163 const loff_t *ppos, size_t count) 164 { 165 return 0; 166 } 167 168 static inline int fsnotify_file_perm(struct file *file, int perm_mask) 169 { 170 return 0; 171 } 172 173 static inline int fsnotify_open_perm(struct file *file) 174 { 175 return 0; 176 } 177 #endif 178 179 /* 180 * fsnotify_link_count - inode's link count changed 181 */ 182 static inline void fsnotify_link_count(struct inode *inode) 183 { 184 fsnotify_inode(inode, FS_ATTRIB); 185 } 186 187 /* 188 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir 189 */ 190 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, 191 const struct qstr *old_name, 192 int isdir, struct inode *target, 193 struct dentry *moved) 194 { 195 struct inode *source = moved->d_inode; 196 u32 fs_cookie = fsnotify_get_cookie(); 197 __u32 old_dir_mask = FS_MOVED_FROM; 198 __u32 new_dir_mask = FS_MOVED_TO; 199 __u32 rename_mask = FS_RENAME; 200 const struct qstr *new_name = &moved->d_name; 201 202 if (isdir) { 203 old_dir_mask |= FS_ISDIR; 204 new_dir_mask |= FS_ISDIR; 205 rename_mask |= FS_ISDIR; 206 } 207 208 /* Event with information about both old and new parent+name */ 209 fsnotify_name(rename_mask, moved, FSNOTIFY_EVENT_DENTRY, 210 old_dir, old_name, 0); 211 212 fsnotify_name(old_dir_mask, source, FSNOTIFY_EVENT_INODE, 213 old_dir, old_name, fs_cookie); 214 fsnotify_name(new_dir_mask, source, FSNOTIFY_EVENT_INODE, 215 new_dir, new_name, fs_cookie); 216 217 if (target) 218 fsnotify_link_count(target); 219 fsnotify_inode(source, FS_MOVE_SELF); 220 audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE); 221 } 222 223 /* 224 * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed 225 */ 226 static inline void fsnotify_inode_delete(struct inode *inode) 227 { 228 __fsnotify_inode_delete(inode); 229 } 230 231 /* 232 * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed 233 */ 234 static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt) 235 { 236 __fsnotify_vfsmount_delete(mnt); 237 } 238 239 /* 240 * fsnotify_inoderemove - an inode is going away 241 */ 242 static inline void fsnotify_inoderemove(struct inode *inode) 243 { 244 fsnotify_inode(inode, FS_DELETE_SELF); 245 __fsnotify_inode_delete(inode); 246 } 247 248 /* 249 * fsnotify_create - 'name' was linked in 250 * 251 * Caller must make sure that dentry->d_name is stable. 252 * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate 253 * ->d_inode later 254 */ 255 static inline void fsnotify_create(struct inode *dir, struct dentry *dentry) 256 { 257 audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE); 258 259 fsnotify_dirent(dir, dentry, FS_CREATE); 260 } 261 262 /* 263 * fsnotify_link - new hardlink in 'inode' directory 264 * 265 * Caller must make sure that new_dentry->d_name is stable. 266 * Note: We have to pass also the linked inode ptr as some filesystems leave 267 * new_dentry->d_inode NULL and instantiate inode pointer later 268 */ 269 static inline void fsnotify_link(struct inode *dir, struct inode *inode, 270 struct dentry *new_dentry) 271 { 272 fsnotify_link_count(inode); 273 audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE); 274 275 fsnotify_name(FS_CREATE, inode, FSNOTIFY_EVENT_INODE, 276 dir, &new_dentry->d_name, 0); 277 } 278 279 /* 280 * fsnotify_delete - @dentry was unlinked and unhashed 281 * 282 * Caller must make sure that dentry->d_name is stable. 283 * 284 * Note: unlike fsnotify_unlink(), we have to pass also the unlinked inode 285 * as this may be called after d_delete() and old_dentry may be negative. 286 */ 287 static inline void fsnotify_delete(struct inode *dir, struct inode *inode, 288 struct dentry *dentry) 289 { 290 __u32 mask = FS_DELETE; 291 292 if (S_ISDIR(inode->i_mode)) 293 mask |= FS_ISDIR; 294 295 fsnotify_name(mask, inode, FSNOTIFY_EVENT_INODE, dir, &dentry->d_name, 296 0); 297 } 298 299 /** 300 * d_delete_notify - delete a dentry and call fsnotify_delete() 301 * @dentry: The dentry to delete 302 * 303 * This helper is used to guaranty that the unlinked inode cannot be found 304 * by lookup of this name after fsnotify_delete() event has been delivered. 305 */ 306 static inline void d_delete_notify(struct inode *dir, struct dentry *dentry) 307 { 308 struct inode *inode = d_inode(dentry); 309 310 ihold(inode); 311 d_delete(dentry); 312 fsnotify_delete(dir, inode, dentry); 313 iput(inode); 314 } 315 316 /* 317 * fsnotify_unlink - 'name' was unlinked 318 * 319 * Caller must make sure that dentry->d_name is stable. 320 */ 321 static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry) 322 { 323 if (WARN_ON_ONCE(d_is_negative(dentry))) 324 return; 325 326 fsnotify_delete(dir, d_inode(dentry), dentry); 327 } 328 329 /* 330 * fsnotify_mkdir - directory 'name' was created 331 * 332 * Caller must make sure that dentry->d_name is stable. 333 * Note: some filesystems (e.g. kernfs) leave @dentry negative and instantiate 334 * ->d_inode later 335 */ 336 static inline void fsnotify_mkdir(struct inode *dir, struct dentry *dentry) 337 { 338 audit_inode_child(dir, dentry, AUDIT_TYPE_CHILD_CREATE); 339 340 fsnotify_dirent(dir, dentry, FS_CREATE | FS_ISDIR); 341 } 342 343 /* 344 * fsnotify_rmdir - directory 'name' was removed 345 * 346 * Caller must make sure that dentry->d_name is stable. 347 */ 348 static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry) 349 { 350 if (WARN_ON_ONCE(d_is_negative(dentry))) 351 return; 352 353 fsnotify_delete(dir, d_inode(dentry), dentry); 354 } 355 356 /* 357 * fsnotify_access - file was read 358 */ 359 static inline void fsnotify_access(struct file *file) 360 { 361 fsnotify_file(file, FS_ACCESS); 362 } 363 364 /* 365 * fsnotify_modify - file was modified 366 */ 367 static inline void fsnotify_modify(struct file *file) 368 { 369 fsnotify_file(file, FS_MODIFY); 370 } 371 372 /* 373 * fsnotify_open - file was opened 374 */ 375 static inline void fsnotify_open(struct file *file) 376 { 377 __u32 mask = FS_OPEN; 378 379 if (file->f_flags & __FMODE_EXEC) 380 mask |= FS_OPEN_EXEC; 381 382 fsnotify_file(file, mask); 383 } 384 385 /* 386 * fsnotify_close - file was closed 387 */ 388 static inline void fsnotify_close(struct file *file) 389 { 390 __u32 mask = (file->f_mode & FMODE_WRITE) ? FS_CLOSE_WRITE : 391 FS_CLOSE_NOWRITE; 392 393 fsnotify_file(file, mask); 394 } 395 396 /* 397 * fsnotify_xattr - extended attributes were changed 398 */ 399 static inline void fsnotify_xattr(struct dentry *dentry) 400 { 401 fsnotify_dentry(dentry, FS_ATTRIB); 402 } 403 404 /* 405 * fsnotify_change - notify_change event. file was modified and/or metadata 406 * was changed. 407 */ 408 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid) 409 { 410 __u32 mask = 0; 411 412 if (ia_valid & ATTR_UID) 413 mask |= FS_ATTRIB; 414 if (ia_valid & ATTR_GID) 415 mask |= FS_ATTRIB; 416 if (ia_valid & ATTR_SIZE) 417 mask |= FS_MODIFY; 418 419 /* both times implies a utime(s) call */ 420 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) 421 mask |= FS_ATTRIB; 422 else if (ia_valid & ATTR_ATIME) 423 mask |= FS_ACCESS; 424 else if (ia_valid & ATTR_MTIME) 425 mask |= FS_MODIFY; 426 427 if (ia_valid & ATTR_MODE) 428 mask |= FS_ATTRIB; 429 430 if (mask) 431 fsnotify_dentry(dentry, mask); 432 } 433 434 static inline int fsnotify_sb_error(struct super_block *sb, struct inode *inode, 435 int error) 436 { 437 struct fs_error_report report = { 438 .error = error, 439 .inode = inode, 440 .sb = sb, 441 }; 442 443 return fsnotify(FS_ERROR, &report, FSNOTIFY_EVENT_ERROR, 444 NULL, NULL, NULL, 0); 445 } 446 447 #endif /* _LINUX_FS_NOTIFY_H */ 448