fsnotify.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #ifndef _LINUX_FS_NOTIFY_H
  2. #define _LINUX_FS_NOTIFY_H
  3. /*
  4. * include/linux/fsnotify.h - generic hooks for filesystem notification, to
  5. * reduce in-source duplication from both dnotify and inotify.
  6. *
  7. * We don't compile any of this away in some complicated menagerie of ifdefs.
  8. * Instead, we rely on the code inside to optimize away as needed.
  9. *
  10. * (C) Copyright 2005 Robert Love
  11. */
  12. #include <linux/fsnotify_backend.h>
  13. #include <linux/audit.h>
  14. #include <linux/slab.h>
  15. #include <linux/bug.h>
  16. /* Notify this dentry's parent about a child's events. */
  17. static inline int fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
  18. {
  19. if (!dentry)
  20. dentry = path->dentry;
  21. return __fsnotify_parent(path, dentry, mask);
  22. }
  23. /* simple call site for access decisions */
  24. static inline int fsnotify_perm(struct file *file, int mask)
  25. {
  26. struct path *path = &file->f_path;
  27. /*
  28. * Do not use file_inode() here or anywhere in this file to get the
  29. * inode. That would break *notity on overlayfs.
  30. */
  31. struct inode *inode = path->dentry->d_inode;
  32. __u32 fsnotify_mask = 0;
  33. int ret;
  34. if (file->f_mode & FMODE_NONOTIFY)
  35. return 0;
  36. if (!(mask & (MAY_READ | MAY_OPEN)))
  37. return 0;
  38. if (mask & MAY_OPEN)
  39. fsnotify_mask = FS_OPEN_PERM;
  40. else if (mask & MAY_READ)
  41. fsnotify_mask = FS_ACCESS_PERM;
  42. else
  43. BUG();
  44. ret = fsnotify_parent(path, NULL, fsnotify_mask);
  45. if (ret)
  46. return ret;
  47. return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  48. }
  49. /*
  50. * fsnotify_link_count - inode's link count changed
  51. */
  52. static inline void fsnotify_link_count(struct inode *inode)
  53. {
  54. fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  55. }
  56. /*
  57. * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
  58. */
  59. static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
  60. const unsigned char *old_name,
  61. int isdir, struct inode *target, struct dentry *moved)
  62. {
  63. struct inode *source = moved->d_inode;
  64. u32 fs_cookie = fsnotify_get_cookie();
  65. __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
  66. __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
  67. const unsigned char *new_name = moved->d_name.name;
  68. if (old_dir == new_dir)
  69. old_dir_mask |= FS_DN_RENAME;
  70. if (isdir) {
  71. old_dir_mask |= FS_ISDIR;
  72. new_dir_mask |= FS_ISDIR;
  73. }
  74. fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
  75. fs_cookie);
  76. fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
  77. fs_cookie);
  78. if (target)
  79. fsnotify_link_count(target);
  80. if (source)
  81. fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  82. audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
  83. }
  84. /*
  85. * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
  86. */
  87. static inline void fsnotify_inode_delete(struct inode *inode)
  88. {
  89. __fsnotify_inode_delete(inode);
  90. }
  91. /*
  92. * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
  93. */
  94. static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
  95. {
  96. __fsnotify_vfsmount_delete(mnt);
  97. }
  98. /*
  99. * fsnotify_nameremove - a filename was removed from a directory
  100. */
  101. static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
  102. {
  103. __u32 mask = FS_DELETE;
  104. if (isdir)
  105. mask |= FS_ISDIR;
  106. fsnotify_parent(NULL, dentry, mask);
  107. }
  108. /*
  109. * fsnotify_inoderemove - an inode is going away
  110. */
  111. static inline void fsnotify_inoderemove(struct inode *inode)
  112. {
  113. fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  114. __fsnotify_inode_delete(inode);
  115. }
  116. /*
  117. * fsnotify_create - 'name' was linked in
  118. */
  119. static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
  120. {
  121. audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
  122. fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
  123. }
  124. /*
  125. * fsnotify_link - new hardlink in 'inode' directory
  126. * Note: We have to pass also the linked inode ptr as some filesystems leave
  127. * new_dentry->d_inode NULL and instantiate inode pointer later
  128. */
  129. static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
  130. {
  131. fsnotify_link_count(inode);
  132. audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
  133. fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
  134. }
  135. /*
  136. * fsnotify_mkdir - directory 'name' was created
  137. */
  138. static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
  139. {
  140. __u32 mask = (FS_CREATE | FS_ISDIR);
  141. struct inode *d_inode = dentry->d_inode;
  142. audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
  143. fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
  144. }
  145. /*
  146. * fsnotify_access - file was read
  147. */
  148. static inline void fsnotify_access(struct file *file)
  149. {
  150. struct path *path = &file->f_path;
  151. struct inode *inode = path->dentry->d_inode;
  152. __u32 mask = FS_ACCESS;
  153. if (S_ISDIR(inode->i_mode))
  154. mask |= FS_ISDIR;
  155. if (!(file->f_mode & FMODE_NONOTIFY)) {
  156. fsnotify_parent(path, NULL, mask);
  157. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  158. }
  159. }
  160. /*
  161. * fsnotify_modify - file was modified
  162. */
  163. static inline void fsnotify_modify(struct file *file)
  164. {
  165. struct path *path = &file->f_path;
  166. struct inode *inode = path->dentry->d_inode;
  167. __u32 mask = FS_MODIFY;
  168. if (S_ISDIR(inode->i_mode))
  169. mask |= FS_ISDIR;
  170. if (!(file->f_mode & FMODE_NONOTIFY)) {
  171. fsnotify_parent(path, NULL, mask);
  172. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  173. }
  174. }
  175. /*
  176. * fsnotify_open - file was opened
  177. */
  178. static inline void fsnotify_open(struct file *file)
  179. {
  180. struct path *path = &file->f_path;
  181. struct inode *inode = path->dentry->d_inode;
  182. __u32 mask = FS_OPEN;
  183. if (S_ISDIR(inode->i_mode))
  184. mask |= FS_ISDIR;
  185. fsnotify_parent(path, NULL, mask);
  186. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  187. }
  188. /*
  189. * fsnotify_close - file was closed
  190. */
  191. static inline void fsnotify_close(struct file *file)
  192. {
  193. struct path *path = &file->f_path;
  194. struct inode *inode = path->dentry->d_inode;
  195. fmode_t mode = file->f_mode;
  196. __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
  197. if (S_ISDIR(inode->i_mode))
  198. mask |= FS_ISDIR;
  199. if (!(file->f_mode & FMODE_NONOTIFY)) {
  200. fsnotify_parent(path, NULL, mask);
  201. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  202. }
  203. }
  204. /*
  205. * fsnotify_xattr - extended attributes were changed
  206. */
  207. static inline void fsnotify_xattr(struct dentry *dentry)
  208. {
  209. struct inode *inode = dentry->d_inode;
  210. __u32 mask = FS_ATTRIB;
  211. if (S_ISDIR(inode->i_mode))
  212. mask |= FS_ISDIR;
  213. fsnotify_parent(NULL, dentry, mask);
  214. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  215. }
  216. /*
  217. * fsnotify_change - notify_change event. file was modified and/or metadata
  218. * was changed.
  219. */
  220. static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
  221. {
  222. struct inode *inode = dentry->d_inode;
  223. __u32 mask = 0;
  224. if (ia_valid & ATTR_UID)
  225. mask |= FS_ATTRIB;
  226. if (ia_valid & ATTR_GID)
  227. mask |= FS_ATTRIB;
  228. if (ia_valid & ATTR_SIZE)
  229. mask |= FS_MODIFY;
  230. /* both times implies a utime(s) call */
  231. if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
  232. mask |= FS_ATTRIB;
  233. else if (ia_valid & ATTR_ATIME)
  234. mask |= FS_ACCESS;
  235. else if (ia_valid & ATTR_MTIME)
  236. mask |= FS_MODIFY;
  237. if (ia_valid & ATTR_MODE)
  238. mask |= FS_ATTRIB;
  239. if (mask) {
  240. if (S_ISDIR(inode->i_mode))
  241. mask |= FS_ISDIR;
  242. fsnotify_parent(NULL, dentry, mask);
  243. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  244. }
  245. }
  246. #endif /* _LINUX_FS_NOTIFY_H */