fsnotify.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #ifdef __KERNEL__
  13. #include <linux/dnotify.h>
  14. #include <linux/inotify.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. inotify_d_instantiate(entry, inode);
  24. }
  25. /*
  26. * fsnotify_d_move - entry has been moved
  27. * Called with dcache_lock and entry->d_lock held.
  28. */
  29. static inline void fsnotify_d_move(struct dentry *entry)
  30. {
  31. inotify_d_move(entry);
  32. }
  33. /*
  34. * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
  35. */
  36. static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
  37. const char *old_name, const char *new_name,
  38. int isdir, struct inode *target, struct inode *source)
  39. {
  40. u32 cookie = inotify_get_cookie();
  41. if (old_dir == new_dir)
  42. inode_dir_notify(old_dir, DN_RENAME);
  43. else {
  44. inode_dir_notify(old_dir, DN_DELETE);
  45. inode_dir_notify(new_dir, DN_CREATE);
  46. }
  47. if (isdir)
  48. isdir = IN_ISDIR;
  49. inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name,
  50. source);
  51. inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name,
  52. source);
  53. if (target) {
  54. inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
  55. inotify_inode_is_dead(target);
  56. }
  57. if (source) {
  58. inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL);
  59. }
  60. audit_inode_child(new_name, source, new_dir);
  61. }
  62. /*
  63. * fsnotify_nameremove - a filename was removed from a directory
  64. */
  65. static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
  66. {
  67. if (isdir)
  68. isdir = IN_ISDIR;
  69. dnotify_parent(dentry, DN_DELETE);
  70. inotify_dentry_parent_queue_event(dentry, IN_DELETE|isdir, 0, dentry->d_name.name);
  71. }
  72. /*
  73. * fsnotify_inoderemove - an inode is going away
  74. */
  75. static inline void fsnotify_inoderemove(struct inode *inode)
  76. {
  77. inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL, NULL);
  78. inotify_inode_is_dead(inode);
  79. }
  80. /*
  81. * fsnotify_create - 'name' was linked in
  82. */
  83. static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
  84. {
  85. inode_dir_notify(inode, DN_CREATE);
  86. inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name,
  87. dentry->d_inode);
  88. audit_inode_child(dentry->d_name.name, dentry->d_inode, inode);
  89. }
  90. /*
  91. * fsnotify_mkdir - directory 'name' was created
  92. */
  93. static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
  94. {
  95. inode_dir_notify(inode, DN_CREATE);
  96. inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0,
  97. dentry->d_name.name, dentry->d_inode);
  98. audit_inode_child(dentry->d_name.name, dentry->d_inode, inode);
  99. }
  100. /*
  101. * fsnotify_access - file was read
  102. */
  103. static inline void fsnotify_access(struct dentry *dentry)
  104. {
  105. struct inode *inode = dentry->d_inode;
  106. u32 mask = IN_ACCESS;
  107. if (S_ISDIR(inode->i_mode))
  108. mask |= IN_ISDIR;
  109. dnotify_parent(dentry, DN_ACCESS);
  110. inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
  111. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  112. }
  113. /*
  114. * fsnotify_modify - file was modified
  115. */
  116. static inline void fsnotify_modify(struct dentry *dentry)
  117. {
  118. struct inode *inode = dentry->d_inode;
  119. u32 mask = IN_MODIFY;
  120. if (S_ISDIR(inode->i_mode))
  121. mask |= IN_ISDIR;
  122. dnotify_parent(dentry, DN_MODIFY);
  123. inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
  124. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  125. }
  126. /*
  127. * fsnotify_open - file was opened
  128. */
  129. static inline void fsnotify_open(struct dentry *dentry)
  130. {
  131. struct inode *inode = dentry->d_inode;
  132. u32 mask = IN_OPEN;
  133. if (S_ISDIR(inode->i_mode))
  134. mask |= IN_ISDIR;
  135. inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
  136. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  137. }
  138. /*
  139. * fsnotify_close - file was closed
  140. */
  141. static inline void fsnotify_close(struct file *file)
  142. {
  143. struct dentry *dentry = file->f_path.dentry;
  144. struct inode *inode = dentry->d_inode;
  145. const char *name = dentry->d_name.name;
  146. mode_t mode = file->f_mode;
  147. u32 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
  148. if (S_ISDIR(inode->i_mode))
  149. mask |= IN_ISDIR;
  150. inotify_dentry_parent_queue_event(dentry, mask, 0, name);
  151. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  152. }
  153. /*
  154. * fsnotify_xattr - extended attributes were changed
  155. */
  156. static inline void fsnotify_xattr(struct dentry *dentry)
  157. {
  158. struct inode *inode = dentry->d_inode;
  159. u32 mask = IN_ATTRIB;
  160. if (S_ISDIR(inode->i_mode))
  161. mask |= IN_ISDIR;
  162. inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
  163. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  164. }
  165. /*
  166. * fsnotify_change - notify_change event. file was modified and/or metadata
  167. * was changed.
  168. */
  169. static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
  170. {
  171. struct inode *inode = dentry->d_inode;
  172. int dn_mask = 0;
  173. u32 in_mask = 0;
  174. if (ia_valid & ATTR_UID) {
  175. in_mask |= IN_ATTRIB;
  176. dn_mask |= DN_ATTRIB;
  177. }
  178. if (ia_valid & ATTR_GID) {
  179. in_mask |= IN_ATTRIB;
  180. dn_mask |= DN_ATTRIB;
  181. }
  182. if (ia_valid & ATTR_SIZE) {
  183. in_mask |= IN_MODIFY;
  184. dn_mask |= DN_MODIFY;
  185. }
  186. /* both times implies a utime(s) call */
  187. if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
  188. {
  189. in_mask |= IN_ATTRIB;
  190. dn_mask |= DN_ATTRIB;
  191. } else if (ia_valid & ATTR_ATIME) {
  192. in_mask |= IN_ACCESS;
  193. dn_mask |= DN_ACCESS;
  194. } else if (ia_valid & ATTR_MTIME) {
  195. in_mask |= IN_MODIFY;
  196. dn_mask |= DN_MODIFY;
  197. }
  198. if (ia_valid & ATTR_MODE) {
  199. in_mask |= IN_ATTRIB;
  200. dn_mask |= DN_ATTRIB;
  201. }
  202. if (dn_mask)
  203. dnotify_parent(dentry, dn_mask);
  204. if (in_mask) {
  205. if (S_ISDIR(inode->i_mode))
  206. in_mask |= IN_ISDIR;
  207. inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
  208. inotify_dentry_parent_queue_event(dentry, in_mask, 0,
  209. dentry->d_name.name);
  210. }
  211. }
  212. #ifdef CONFIG_INOTIFY /* inotify helpers */
  213. /*
  214. * fsnotify_oldname_init - save off the old filename before we change it
  215. */
  216. static inline const char *fsnotify_oldname_init(const char *name)
  217. {
  218. return kstrdup(name, GFP_KERNEL);
  219. }
  220. /*
  221. * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
  222. */
  223. static inline void fsnotify_oldname_free(const char *old_name)
  224. {
  225. kfree(old_name);
  226. }
  227. #else /* CONFIG_INOTIFY */
  228. static inline const char *fsnotify_oldname_init(const char *name)
  229. {
  230. return NULL;
  231. }
  232. static inline void fsnotify_oldname_free(const char *old_name)
  233. {
  234. }
  235. #endif /* ! CONFIG_INOTIFY */
  236. #endif /* __KERNEL__ */
  237. #endif /* _LINUX_FS_NOTIFY_H */