fsnotify.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /*
  17. * fsnotify_d_instantiate - instantiate a dentry for inode
  18. */
  19. static inline void fsnotify_d_instantiate(struct dentry *dentry,
  20. struct inode *inode)
  21. {
  22. __fsnotify_d_instantiate(dentry, inode);
  23. }
  24. /* Notify this dentry's parent about a child's events. */
  25. static inline int fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
  26. {
  27. if (!dentry)
  28. dentry = path->dentry;
  29. return __fsnotify_parent(path, dentry, mask);
  30. }
  31. /* simple call site for access decisions */
  32. static inline int fsnotify_perm(struct file *file, int mask)
  33. {
  34. struct path *path = &file->f_path;
  35. struct inode *inode = file_inode(file);
  36. __u32 fsnotify_mask = 0;
  37. int ret;
  38. if (file->f_mode & FMODE_NONOTIFY)
  39. return 0;
  40. if (!(mask & (MAY_READ | MAY_OPEN)))
  41. return 0;
  42. if (mask & MAY_OPEN)
  43. fsnotify_mask = FS_OPEN_PERM;
  44. else if (mask & MAY_READ)
  45. fsnotify_mask = FS_ACCESS_PERM;
  46. else
  47. BUG();
  48. ret = fsnotify_parent(path, NULL, fsnotify_mask);
  49. if (ret)
  50. return ret;
  51. return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  52. }
  53. /*
  54. * fsnotify_d_move - dentry has been moved
  55. */
  56. static inline void fsnotify_d_move(struct dentry *dentry)
  57. {
  58. /*
  59. * On move we need to update dentry->d_flags to indicate if the new parent
  60. * cares about events from this dentry.
  61. */
  62. __fsnotify_update_dcache_flags(dentry);
  63. }
  64. /*
  65. * fsnotify_link_count - inode's link count changed
  66. */
  67. static inline void fsnotify_link_count(struct inode *inode)
  68. {
  69. fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  70. }
  71. /*
  72. * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
  73. */
  74. static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
  75. const unsigned char *old_name,
  76. int isdir, struct inode *target, struct dentry *moved)
  77. {
  78. struct inode *source = moved->d_inode;
  79. u32 fs_cookie = fsnotify_get_cookie();
  80. __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
  81. __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
  82. const unsigned char *new_name = moved->d_name.name;
  83. if (old_dir == new_dir)
  84. old_dir_mask |= FS_DN_RENAME;
  85. if (isdir) {
  86. old_dir_mask |= FS_ISDIR;
  87. new_dir_mask |= FS_ISDIR;
  88. }
  89. fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name,
  90. fs_cookie);
  91. fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
  92. fs_cookie);
  93. if (target)
  94. fsnotify_link_count(target);
  95. if (source)
  96. fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  97. audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
  98. }
  99. /*
  100. * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
  101. */
  102. static inline void fsnotify_inode_delete(struct inode *inode)
  103. {
  104. __fsnotify_inode_delete(inode);
  105. }
  106. /*
  107. * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
  108. */
  109. static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
  110. {
  111. __fsnotify_vfsmount_delete(mnt);
  112. }
  113. /*
  114. * fsnotify_nameremove - a filename was removed from a directory
  115. */
  116. static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
  117. {
  118. __u32 mask = FS_DELETE;
  119. if (isdir)
  120. mask |= FS_ISDIR;
  121. fsnotify_parent(NULL, dentry, mask);
  122. }
  123. /*
  124. * fsnotify_inoderemove - an inode is going away
  125. */
  126. static inline void fsnotify_inoderemove(struct inode *inode)
  127. {
  128. fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  129. __fsnotify_inode_delete(inode);
  130. }
  131. /*
  132. * fsnotify_create - 'name' was linked in
  133. */
  134. static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
  135. {
  136. audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
  137. fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
  138. }
  139. /*
  140. * fsnotify_link - new hardlink in 'inode' directory
  141. * Note: We have to pass also the linked inode ptr as some filesystems leave
  142. * new_dentry->d_inode NULL and instantiate inode pointer later
  143. */
  144. static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
  145. {
  146. fsnotify_link_count(inode);
  147. audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
  148. fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
  149. }
  150. /*
  151. * fsnotify_mkdir - directory 'name' was created
  152. */
  153. static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
  154. {
  155. __u32 mask = (FS_CREATE | FS_ISDIR);
  156. struct inode *d_inode = dentry->d_inode;
  157. audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
  158. fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
  159. }
  160. /*
  161. * fsnotify_access - file was read
  162. */
  163. static inline void fsnotify_access(struct file *file)
  164. {
  165. struct path *path = &file->f_path;
  166. struct inode *inode = file_inode(file);
  167. __u32 mask = FS_ACCESS;
  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_modify - file was modified
  177. */
  178. static inline void fsnotify_modify(struct file *file)
  179. {
  180. struct path *path = &file->f_path;
  181. struct inode *inode = file_inode(file);
  182. __u32 mask = FS_MODIFY;
  183. if (S_ISDIR(inode->i_mode))
  184. mask |= FS_ISDIR;
  185. if (!(file->f_mode & FMODE_NONOTIFY)) {
  186. fsnotify_parent(path, NULL, mask);
  187. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  188. }
  189. }
  190. /*
  191. * fsnotify_open - file was opened
  192. */
  193. static inline void fsnotify_open(struct file *file)
  194. {
  195. struct path *path = &file->f_path;
  196. struct inode *inode = file_inode(file);
  197. __u32 mask = FS_OPEN;
  198. if (S_ISDIR(inode->i_mode))
  199. mask |= FS_ISDIR;
  200. fsnotify_parent(path, NULL, mask);
  201. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  202. }
  203. /*
  204. * fsnotify_close - file was closed
  205. */
  206. static inline void fsnotify_close(struct file *file)
  207. {
  208. struct path *path = &file->f_path;
  209. struct inode *inode = file_inode(file);
  210. fmode_t mode = file->f_mode;
  211. __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
  212. if (S_ISDIR(inode->i_mode))
  213. mask |= FS_ISDIR;
  214. if (!(file->f_mode & FMODE_NONOTIFY)) {
  215. fsnotify_parent(path, NULL, mask);
  216. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  217. }
  218. }
  219. /*
  220. * fsnotify_xattr - extended attributes were changed
  221. */
  222. static inline void fsnotify_xattr(struct dentry *dentry)
  223. {
  224. struct inode *inode = dentry->d_inode;
  225. __u32 mask = FS_ATTRIB;
  226. if (S_ISDIR(inode->i_mode))
  227. mask |= FS_ISDIR;
  228. fsnotify_parent(NULL, dentry, mask);
  229. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  230. }
  231. /*
  232. * fsnotify_change - notify_change event. file was modified and/or metadata
  233. * was changed.
  234. */
  235. static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
  236. {
  237. struct inode *inode = dentry->d_inode;
  238. __u32 mask = 0;
  239. if (ia_valid & ATTR_UID)
  240. mask |= FS_ATTRIB;
  241. if (ia_valid & ATTR_GID)
  242. mask |= FS_ATTRIB;
  243. if (ia_valid & ATTR_SIZE)
  244. mask |= FS_MODIFY;
  245. /* both times implies a utime(s) call */
  246. if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
  247. mask |= FS_ATTRIB;
  248. else if (ia_valid & ATTR_ATIME)
  249. mask |= FS_ACCESS;
  250. else if (ia_valid & ATTR_MTIME)
  251. mask |= FS_MODIFY;
  252. if (ia_valid & ATTR_MODE)
  253. mask |= FS_ATTRIB;
  254. if (mask) {
  255. if (S_ISDIR(inode->i_mode))
  256. mask |= FS_ISDIR;
  257. fsnotify_parent(NULL, dentry, mask);
  258. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  259. }
  260. }
  261. #endif /* _LINUX_FS_NOTIFY_H */