fsnotify.h 8.4 KB

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