fsnotify.h 8.3 KB

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