fsnotify.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_FS_NOTIFY_H
  3. #define _LINUX_FS_NOTIFY_H
  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. #include <linux/fsnotify_backend.h>
  14. #include <linux/audit.h>
  15. #include <linux/slab.h>
  16. #include <linux/bug.h>
  17. /*
  18. * Notify this @dir inode about a change in the directory entry @dentry.
  19. *
  20. * Unlike fsnotify_parent(), the event will be reported regardless of the
  21. * FS_EVENT_ON_CHILD mask on the parent inode.
  22. */
  23. static inline int fsnotify_dirent(struct inode *dir, struct dentry *dentry,
  24. __u32 mask)
  25. {
  26. return fsnotify(dir, mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
  27. &dentry->d_name, 0);
  28. }
  29. /* Notify this dentry's parent about a child's events. */
  30. static inline int fsnotify_parent(const struct path *path,
  31. struct dentry *dentry, __u32 mask)
  32. {
  33. if (!dentry)
  34. dentry = path->dentry;
  35. return __fsnotify_parent(path, dentry, mask);
  36. }
  37. /*
  38. * Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when
  39. * an event is on a path.
  40. */
  41. static inline int fsnotify_path(struct inode *inode, const struct path *path,
  42. __u32 mask)
  43. {
  44. int ret = fsnotify_parent(path, NULL, mask);
  45. if (ret)
  46. return ret;
  47. return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  48. }
  49. /* Simple call site for access decisions */
  50. static inline int fsnotify_perm(struct file *file, int mask)
  51. {
  52. int ret;
  53. const struct path *path = &file->f_path;
  54. struct inode *inode = file_inode(file);
  55. __u32 fsnotify_mask = 0;
  56. if (file->f_mode & FMODE_NONOTIFY)
  57. return 0;
  58. if (!(mask & (MAY_READ | MAY_OPEN)))
  59. return 0;
  60. if (mask & MAY_OPEN) {
  61. fsnotify_mask = FS_OPEN_PERM;
  62. if (file->f_flags & __FMODE_EXEC) {
  63. ret = fsnotify_path(inode, path, FS_OPEN_EXEC_PERM);
  64. if (ret)
  65. return ret;
  66. }
  67. } else if (mask & MAY_READ) {
  68. fsnotify_mask = FS_ACCESS_PERM;
  69. }
  70. if (S_ISDIR(inode->i_mode))
  71. fsnotify_mask |= FS_ISDIR;
  72. return fsnotify_path(inode, path, fsnotify_mask);
  73. }
  74. /*
  75. * fsnotify_link_count - inode's link count changed
  76. */
  77. static inline void fsnotify_link_count(struct inode *inode)
  78. {
  79. __u32 mask = FS_ATTRIB;
  80. if (S_ISDIR(inode->i_mode))
  81. mask |= FS_ISDIR;
  82. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  83. }
  84. /*
  85. * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
  86. */
  87. static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
  88. const struct qstr *old_name,
  89. int isdir, struct inode *target,
  90. struct dentry *moved)
  91. {
  92. struct inode *source = moved->d_inode;
  93. u32 fs_cookie = fsnotify_get_cookie();
  94. __u32 old_dir_mask = FS_MOVED_FROM;
  95. __u32 new_dir_mask = FS_MOVED_TO;
  96. __u32 mask = FS_MOVE_SELF;
  97. const struct qstr *new_name = &moved->d_name;
  98. if (old_dir == new_dir)
  99. old_dir_mask |= FS_DN_RENAME;
  100. if (isdir) {
  101. old_dir_mask |= FS_ISDIR;
  102. new_dir_mask |= FS_ISDIR;
  103. mask |= FS_ISDIR;
  104. }
  105. fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
  106. fs_cookie);
  107. fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
  108. fs_cookie);
  109. if (target)
  110. fsnotify_link_count(target);
  111. if (source)
  112. fsnotify(source, mask, source, FSNOTIFY_EVENT_INODE, NULL, 0);
  113. audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
  114. }
  115. /*
  116. * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
  117. */
  118. static inline void fsnotify_inode_delete(struct inode *inode)
  119. {
  120. __fsnotify_inode_delete(inode);
  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. * fsnotify_inoderemove - an inode is going away
  131. */
  132. static inline void fsnotify_inoderemove(struct inode *inode)
  133. {
  134. __u32 mask = FS_DELETE_SELF;
  135. if (S_ISDIR(inode->i_mode))
  136. mask |= FS_ISDIR;
  137. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  138. __fsnotify_inode_delete(inode);
  139. }
  140. /*
  141. * fsnotify_create - 'name' was linked in
  142. */
  143. static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
  144. {
  145. audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
  146. fsnotify_dirent(inode, dentry, FS_CREATE);
  147. }
  148. /*
  149. * fsnotify_link - new hardlink in 'inode' directory
  150. * Note: We have to pass also the linked inode ptr as some filesystems leave
  151. * new_dentry->d_inode NULL and instantiate inode pointer later
  152. */
  153. static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
  154. {
  155. fsnotify_link_count(inode);
  156. audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
  157. fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, &new_dentry->d_name, 0);
  158. }
  159. /*
  160. * fsnotify_unlink - 'name' was unlinked
  161. *
  162. * Caller must make sure that dentry->d_name is stable.
  163. */
  164. static inline void fsnotify_unlink(struct inode *dir, struct dentry *dentry)
  165. {
  166. /* Expected to be called before d_delete() */
  167. WARN_ON_ONCE(d_is_negative(dentry));
  168. fsnotify_dirent(dir, dentry, FS_DELETE);
  169. }
  170. /*
  171. * fsnotify_mkdir - directory 'name' was created
  172. */
  173. static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
  174. {
  175. audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
  176. fsnotify_dirent(inode, dentry, FS_CREATE | FS_ISDIR);
  177. }
  178. /*
  179. * fsnotify_rmdir - directory 'name' was removed
  180. *
  181. * Caller must make sure that dentry->d_name is stable.
  182. */
  183. static inline void fsnotify_rmdir(struct inode *dir, struct dentry *dentry)
  184. {
  185. /* Expected to be called before d_delete() */
  186. WARN_ON_ONCE(d_is_negative(dentry));
  187. fsnotify_dirent(dir, dentry, FS_DELETE | FS_ISDIR);
  188. }
  189. /*
  190. * fsnotify_access - file was read
  191. */
  192. static inline void fsnotify_access(struct file *file)
  193. {
  194. const struct path *path = &file->f_path;
  195. struct inode *inode = file_inode(file);
  196. __u32 mask = FS_ACCESS;
  197. if (is_sidechannel_device(inode))
  198. return;
  199. if (S_ISDIR(inode->i_mode))
  200. mask |= FS_ISDIR;
  201. if (!(file->f_mode & FMODE_NONOTIFY))
  202. fsnotify_path(inode, path, mask);
  203. }
  204. /*
  205. * fsnotify_modify - file was modified
  206. */
  207. static inline void fsnotify_modify(struct file *file)
  208. {
  209. const struct path *path = &file->f_path;
  210. struct inode *inode = file_inode(file);
  211. __u32 mask = FS_MODIFY;
  212. if (is_sidechannel_device(inode))
  213. return;
  214. if (S_ISDIR(inode->i_mode))
  215. mask |= FS_ISDIR;
  216. if (!(file->f_mode & FMODE_NONOTIFY))
  217. fsnotify_path(inode, path, mask);
  218. }
  219. /*
  220. * fsnotify_open - file was opened
  221. */
  222. static inline void fsnotify_open(struct file *file)
  223. {
  224. const struct path *path = &file->f_path;
  225. struct inode *inode = file_inode(file);
  226. __u32 mask = FS_OPEN;
  227. if (S_ISDIR(inode->i_mode))
  228. mask |= FS_ISDIR;
  229. if (file->f_flags & __FMODE_EXEC)
  230. mask |= FS_OPEN_EXEC;
  231. fsnotify_path(inode, path, mask);
  232. }
  233. /*
  234. * fsnotify_close - file was closed
  235. */
  236. static inline void fsnotify_close(struct file *file)
  237. {
  238. const struct path *path = &file->f_path;
  239. struct inode *inode = file_inode(file);
  240. fmode_t mode = file->f_mode;
  241. __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
  242. if (S_ISDIR(inode->i_mode))
  243. mask |= FS_ISDIR;
  244. if (!(file->f_mode & FMODE_NONOTIFY))
  245. fsnotify_path(inode, path, mask);
  246. }
  247. /*
  248. * fsnotify_xattr - extended attributes were changed
  249. */
  250. static inline void fsnotify_xattr(struct dentry *dentry)
  251. {
  252. struct inode *inode = dentry->d_inode;
  253. __u32 mask = FS_ATTRIB;
  254. if (S_ISDIR(inode->i_mode))
  255. mask |= FS_ISDIR;
  256. fsnotify_parent(NULL, dentry, mask);
  257. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  258. }
  259. /*
  260. * fsnotify_change - notify_change event. file was modified and/or metadata
  261. * was changed.
  262. */
  263. static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
  264. {
  265. struct inode *inode = dentry->d_inode;
  266. __u32 mask = 0;
  267. if (ia_valid & ATTR_UID)
  268. mask |= FS_ATTRIB;
  269. if (ia_valid & ATTR_GID)
  270. mask |= FS_ATTRIB;
  271. if (ia_valid & ATTR_SIZE)
  272. mask |= FS_MODIFY;
  273. /* both times implies a utime(s) call */
  274. if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
  275. mask |= FS_ATTRIB;
  276. else if (ia_valid & ATTR_ATIME)
  277. mask |= FS_ACCESS;
  278. else if (ia_valid & ATTR_MTIME)
  279. mask |= FS_MODIFY;
  280. if (ia_valid & ATTR_MODE)
  281. mask |= FS_ATTRIB;
  282. if (mask) {
  283. if (S_ISDIR(inode->i_mode))
  284. mask |= FS_ISDIR;
  285. fsnotify_parent(NULL, dentry, mask);
  286. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  287. }
  288. }
  289. #endif /* _LINUX_FS_NOTIFY_H */