fsnotify.h 8.6 KB

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