fsnotify.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_link_count - inode's link count changed
  83. */
  84. static inline void fsnotify_link_count(struct inode *inode)
  85. {
  86. inotify_inode_queue_event(inode, IN_ATTRIB, 0, NULL, NULL);
  87. }
  88. /*
  89. * fsnotify_create - 'name' was linked in
  90. */
  91. static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
  92. {
  93. inode_dir_notify(inode, DN_CREATE);
  94. inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name,
  95. dentry->d_inode);
  96. audit_inode_child(dentry->d_name.name, dentry, inode);
  97. }
  98. /*
  99. * fsnotify_link - new hardlink in 'inode' directory
  100. * Note: We have to pass also the linked inode ptr as some filesystems leave
  101. * new_dentry->d_inode NULL and instantiate inode pointer later
  102. */
  103. static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
  104. {
  105. inode_dir_notify(dir, DN_CREATE);
  106. inotify_inode_queue_event(dir, IN_CREATE, 0, new_dentry->d_name.name,
  107. inode);
  108. fsnotify_link_count(inode);
  109. audit_inode_child(new_dentry->d_name.name, new_dentry, dir);
  110. }
  111. /*
  112. * fsnotify_mkdir - directory 'name' was created
  113. */
  114. static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
  115. {
  116. inode_dir_notify(inode, DN_CREATE);
  117. inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0,
  118. dentry->d_name.name, dentry->d_inode);
  119. audit_inode_child(dentry->d_name.name, dentry, inode);
  120. }
  121. /*
  122. * fsnotify_access - file was read
  123. */
  124. static inline void fsnotify_access(struct dentry *dentry)
  125. {
  126. struct inode *inode = dentry->d_inode;
  127. u32 mask = IN_ACCESS;
  128. if (S_ISDIR(inode->i_mode))
  129. mask |= IN_ISDIR;
  130. dnotify_parent(dentry, DN_ACCESS);
  131. inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
  132. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  133. }
  134. /*
  135. * fsnotify_modify - file was modified
  136. */
  137. static inline void fsnotify_modify(struct dentry *dentry)
  138. {
  139. struct inode *inode = dentry->d_inode;
  140. u32 mask = IN_MODIFY;
  141. if (S_ISDIR(inode->i_mode))
  142. mask |= IN_ISDIR;
  143. dnotify_parent(dentry, DN_MODIFY);
  144. inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
  145. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  146. }
  147. /*
  148. * fsnotify_open - file was opened
  149. */
  150. static inline void fsnotify_open(struct dentry *dentry)
  151. {
  152. struct inode *inode = dentry->d_inode;
  153. u32 mask = IN_OPEN;
  154. if (S_ISDIR(inode->i_mode))
  155. mask |= IN_ISDIR;
  156. inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
  157. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  158. }
  159. /*
  160. * fsnotify_close - file was closed
  161. */
  162. static inline void fsnotify_close(struct file *file)
  163. {
  164. struct dentry *dentry = file->f_path.dentry;
  165. struct inode *inode = dentry->d_inode;
  166. const char *name = dentry->d_name.name;
  167. mode_t mode = file->f_mode;
  168. u32 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
  169. if (S_ISDIR(inode->i_mode))
  170. mask |= IN_ISDIR;
  171. inotify_dentry_parent_queue_event(dentry, mask, 0, name);
  172. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  173. }
  174. /*
  175. * fsnotify_xattr - extended attributes were changed
  176. */
  177. static inline void fsnotify_xattr(struct dentry *dentry)
  178. {
  179. struct inode *inode = dentry->d_inode;
  180. u32 mask = IN_ATTRIB;
  181. if (S_ISDIR(inode->i_mode))
  182. mask |= IN_ISDIR;
  183. inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
  184. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  185. }
  186. /*
  187. * fsnotify_change - notify_change event. file was modified and/or metadata
  188. * was changed.
  189. */
  190. static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
  191. {
  192. struct inode *inode = dentry->d_inode;
  193. int dn_mask = 0;
  194. u32 in_mask = 0;
  195. if (ia_valid & ATTR_UID) {
  196. in_mask |= IN_ATTRIB;
  197. dn_mask |= DN_ATTRIB;
  198. }
  199. if (ia_valid & ATTR_GID) {
  200. in_mask |= IN_ATTRIB;
  201. dn_mask |= DN_ATTRIB;
  202. }
  203. if (ia_valid & ATTR_SIZE) {
  204. in_mask |= IN_MODIFY;
  205. dn_mask |= DN_MODIFY;
  206. }
  207. /* both times implies a utime(s) call */
  208. if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
  209. {
  210. in_mask |= IN_ATTRIB;
  211. dn_mask |= DN_ATTRIB;
  212. } else if (ia_valid & ATTR_ATIME) {
  213. in_mask |= IN_ACCESS;
  214. dn_mask |= DN_ACCESS;
  215. } else if (ia_valid & ATTR_MTIME) {
  216. in_mask |= IN_MODIFY;
  217. dn_mask |= DN_MODIFY;
  218. }
  219. if (ia_valid & ATTR_MODE) {
  220. in_mask |= IN_ATTRIB;
  221. dn_mask |= DN_ATTRIB;
  222. }
  223. if (dn_mask)
  224. dnotify_parent(dentry, dn_mask);
  225. if (in_mask) {
  226. if (S_ISDIR(inode->i_mode))
  227. in_mask |= IN_ISDIR;
  228. inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
  229. inotify_dentry_parent_queue_event(dentry, in_mask, 0,
  230. dentry->d_name.name);
  231. }
  232. }
  233. #ifdef CONFIG_INOTIFY /* inotify helpers */
  234. /*
  235. * fsnotify_oldname_init - save off the old filename before we change it
  236. */
  237. static inline const char *fsnotify_oldname_init(const char *name)
  238. {
  239. return kstrdup(name, GFP_KERNEL);
  240. }
  241. /*
  242. * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
  243. */
  244. static inline void fsnotify_oldname_free(const char *old_name)
  245. {
  246. kfree(old_name);
  247. }
  248. #else /* CONFIG_INOTIFY */
  249. static inline const char *fsnotify_oldname_init(const char *name)
  250. {
  251. return NULL;
  252. }
  253. static inline void fsnotify_oldname_free(const char *old_name)
  254. {
  255. }
  256. #endif /* ! CONFIG_INOTIFY */
  257. #endif /* __KERNEL__ */
  258. #endif /* _LINUX_FS_NOTIFY_H */