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