fsnotify.h 5.9 KB

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