fsnotify.h 8.6 KB

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