fsnotify.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 = path->dentry->d_inode;
  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(moved, new_dir);
  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(dentry, inode);
  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(new_dentry, dir);
  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(dentry, inode);
  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 = path->dentry->d_inode;
  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 = path->dentry->d_inode;
  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 path lower_path;
  197. struct inode *inode = path->dentry->d_inode;
  198. __u32 mask = FS_OPEN;
  199. if (S_ISDIR(inode->i_mode))
  200. mask |= FS_ISDIR;
  201. if (path->dentry->d_op && path->dentry->d_op->d_canonical_path) {
  202. path->dentry->d_op->d_canonical_path(path, &lower_path);
  203. fsnotify_parent(&lower_path, NULL, mask);
  204. fsnotify(lower_path.dentry->d_inode, mask, &lower_path, FSNOTIFY_EVENT_PATH, NULL, 0);
  205. path_put(&lower_path);
  206. }
  207. fsnotify_parent(path, NULL, mask);
  208. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  209. }
  210. /*
  211. * fsnotify_close - file was closed
  212. */
  213. static inline void fsnotify_close(struct file *file)
  214. {
  215. struct path *path = &file->f_path;
  216. struct inode *inode = file->f_path.dentry->d_inode;
  217. fmode_t mode = file->f_mode;
  218. __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
  219. if (S_ISDIR(inode->i_mode))
  220. mask |= FS_ISDIR;
  221. if (!(file->f_mode & FMODE_NONOTIFY)) {
  222. fsnotify_parent(path, NULL, mask);
  223. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  224. }
  225. }
  226. /*
  227. * fsnotify_xattr - extended attributes were changed
  228. */
  229. static inline void fsnotify_xattr(struct dentry *dentry)
  230. {
  231. struct inode *inode = dentry->d_inode;
  232. __u32 mask = FS_ATTRIB;
  233. if (S_ISDIR(inode->i_mode))
  234. mask |= FS_ISDIR;
  235. fsnotify_parent(NULL, dentry, mask);
  236. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  237. }
  238. /*
  239. * fsnotify_change - notify_change event. file was modified and/or metadata
  240. * was changed.
  241. */
  242. static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
  243. {
  244. struct inode *inode = dentry->d_inode;
  245. __u32 mask = 0;
  246. if (ia_valid & ATTR_UID)
  247. mask |= FS_ATTRIB;
  248. if (ia_valid & ATTR_GID)
  249. mask |= FS_ATTRIB;
  250. if (ia_valid & ATTR_SIZE)
  251. mask |= FS_MODIFY;
  252. /* both times implies a utime(s) call */
  253. if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
  254. mask |= FS_ATTRIB;
  255. else if (ia_valid & ATTR_ATIME)
  256. mask |= FS_ACCESS;
  257. else if (ia_valid & ATTR_MTIME)
  258. mask |= FS_MODIFY;
  259. if (ia_valid & ATTR_MODE)
  260. mask |= FS_ATTRIB;
  261. if (mask) {
  262. if (S_ISDIR(inode->i_mode))
  263. mask |= FS_ISDIR;
  264. fsnotify_parent(NULL, dentry, mask);
  265. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  266. }
  267. }
  268. #endif /* _LINUX_FS_NOTIFY_H */