fsnotify.h 5.9 KB

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