fsnotify.h 6.2 KB

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