fsnotify.h 6.7 KB

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