fsnotify.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/fsnotify_backend.h>
  13. #include <linux/audit.h>
  14. #include <linux/slab.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 *dentry,
  20. struct inode *inode)
  21. {
  22. __fsnotify_d_instantiate(dentry, inode);
  23. }
  24. /* Notify this dentry's parent about a child's events. */
  25. static inline void fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
  26. {
  27. if (!dentry)
  28. dentry = path->dentry;
  29. __fsnotify_parent(path, dentry, mask);
  30. }
  31. /*
  32. * fsnotify_d_move - dentry has been moved
  33. * Called with dcache_lock and dentry->d_lock held.
  34. */
  35. static inline void fsnotify_d_move(struct dentry *dentry)
  36. {
  37. /*
  38. * On move we need to update dentry->d_flags to indicate if the new parent
  39. * cares about events from this dentry.
  40. */
  41. __fsnotify_update_dcache_flags(dentry);
  42. }
  43. /*
  44. * fsnotify_link_count - inode's link count changed
  45. */
  46. static inline void fsnotify_link_count(struct inode *inode)
  47. {
  48. fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  49. }
  50. /*
  51. * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
  52. */
  53. static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
  54. const char *old_name,
  55. int isdir, struct inode *target, struct dentry *moved)
  56. {
  57. struct inode *source = moved->d_inode;
  58. u32 fs_cookie = fsnotify_get_cookie();
  59. __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
  60. __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
  61. const char *new_name = moved->d_name.name;
  62. if (old_dir == new_dir)
  63. old_dir_mask |= FS_DN_RENAME;
  64. if (isdir) {
  65. old_dir_mask |= FS_IN_ISDIR;
  66. new_dir_mask |= FS_IN_ISDIR;
  67. }
  68. fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
  69. fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
  70. if (target)
  71. fsnotify_link_count(target);
  72. if (source)
  73. fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  74. audit_inode_child(moved, new_dir);
  75. }
  76. /*
  77. * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
  78. */
  79. static inline void fsnotify_inode_delete(struct inode *inode)
  80. {
  81. __fsnotify_inode_delete(inode);
  82. }
  83. /*
  84. * fsnotify_nameremove - a filename was removed from a directory
  85. */
  86. static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
  87. {
  88. __u32 mask = FS_DELETE;
  89. if (isdir)
  90. mask |= FS_IN_ISDIR;
  91. fsnotify_parent(NULL, dentry, mask);
  92. }
  93. /*
  94. * fsnotify_inoderemove - an inode is going away
  95. */
  96. static inline void fsnotify_inoderemove(struct inode *inode)
  97. {
  98. fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  99. __fsnotify_inode_delete(inode);
  100. }
  101. /*
  102. * fsnotify_create - 'name' was linked in
  103. */
  104. static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
  105. {
  106. audit_inode_child(dentry, inode);
  107. fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
  108. }
  109. /*
  110. * fsnotify_link - new hardlink in 'inode' directory
  111. * Note: We have to pass also the linked inode ptr as some filesystems leave
  112. * new_dentry->d_inode NULL and instantiate inode pointer later
  113. */
  114. static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
  115. {
  116. fsnotify_link_count(inode);
  117. audit_inode_child(new_dentry, dir);
  118. fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
  119. }
  120. /*
  121. * fsnotify_mkdir - directory 'name' was created
  122. */
  123. static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
  124. {
  125. __u32 mask = (FS_CREATE | FS_IN_ISDIR);
  126. struct inode *d_inode = dentry->d_inode;
  127. audit_inode_child(dentry, inode);
  128. fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
  129. }
  130. /*
  131. * fsnotify_access - file was read
  132. */
  133. static inline void fsnotify_access(struct file *file)
  134. {
  135. struct path *path = &file->f_path;
  136. struct inode *inode = path->dentry->d_inode;
  137. __u32 mask = FS_ACCESS;
  138. if (S_ISDIR(inode->i_mode))
  139. mask |= FS_IN_ISDIR;
  140. if (!(file->f_mode & FMODE_NONOTIFY)) {
  141. fsnotify_parent(path, NULL, mask);
  142. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  143. }
  144. }
  145. /*
  146. * fsnotify_modify - file was modified
  147. */
  148. static inline void fsnotify_modify(struct file *file)
  149. {
  150. struct path *path = &file->f_path;
  151. struct inode *inode = path->dentry->d_inode;
  152. __u32 mask = FS_MODIFY;
  153. if (S_ISDIR(inode->i_mode))
  154. mask |= FS_IN_ISDIR;
  155. if (!(file->f_mode & FMODE_NONOTIFY)) {
  156. fsnotify_parent(path, NULL, mask);
  157. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  158. }
  159. }
  160. /*
  161. * fsnotify_open - file was opened
  162. */
  163. static inline void fsnotify_open(struct file *file)
  164. {
  165. struct path *path = &file->f_path;
  166. struct inode *inode = path->dentry->d_inode;
  167. __u32 mask = FS_OPEN;
  168. if (S_ISDIR(inode->i_mode))
  169. mask |= FS_IN_ISDIR;
  170. if (!(file->f_mode & FMODE_NONOTIFY)) {
  171. fsnotify_parent(path, NULL, mask);
  172. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  173. }
  174. }
  175. /*
  176. * fsnotify_close - file was closed
  177. */
  178. static inline void fsnotify_close(struct file *file)
  179. {
  180. struct path *path = &file->f_path;
  181. struct inode *inode = file->f_path.dentry->d_inode;
  182. fmode_t mode = file->f_mode;
  183. __u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
  184. if (S_ISDIR(inode->i_mode))
  185. mask |= FS_IN_ISDIR;
  186. if (!(file->f_mode & FMODE_NONOTIFY)) {
  187. fsnotify_parent(path, NULL, mask);
  188. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  189. }
  190. }
  191. /*
  192. * fsnotify_xattr - extended attributes were changed
  193. */
  194. static inline void fsnotify_xattr(struct dentry *dentry)
  195. {
  196. struct inode *inode = dentry->d_inode;
  197. __u32 mask = FS_ATTRIB;
  198. if (S_ISDIR(inode->i_mode))
  199. mask |= FS_IN_ISDIR;
  200. fsnotify_parent(NULL, dentry, mask);
  201. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  202. }
  203. /*
  204. * fsnotify_change - notify_change event. file was modified and/or metadata
  205. * was changed.
  206. */
  207. static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
  208. {
  209. struct inode *inode = dentry->d_inode;
  210. __u32 mask = 0;
  211. if (ia_valid & ATTR_UID)
  212. mask |= FS_ATTRIB;
  213. if (ia_valid & ATTR_GID)
  214. mask |= FS_ATTRIB;
  215. if (ia_valid & ATTR_SIZE)
  216. mask |= FS_MODIFY;
  217. /* both times implies a utime(s) call */
  218. if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
  219. mask |= FS_ATTRIB;
  220. else if (ia_valid & ATTR_ATIME)
  221. mask |= FS_ACCESS;
  222. else if (ia_valid & ATTR_MTIME)
  223. mask |= FS_MODIFY;
  224. if (ia_valid & ATTR_MODE)
  225. mask |= FS_ATTRIB;
  226. if (mask) {
  227. if (S_ISDIR(inode->i_mode))
  228. mask |= FS_IN_ISDIR;
  229. fsnotify_parent(NULL, dentry, mask);
  230. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  231. }
  232. }
  233. #if defined(CONFIG_FSNOTIFY) /* notify 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_FSNOTIFY */
  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_FSNOTIFY */
  257. #endif /* _LINUX_FS_NOTIFY_H */