fsnotify.h 10 KB

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