fsnotify.h 8.6 KB

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