fsnotify.h 7.3 KB

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