fsnotify.h 7.4 KB

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