fsnotify.h 6.0 KB

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