fsnotify.h 7.7 KB

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