fsnotify.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. */
  18. static inline void fsnotify_d_instantiate(struct dentry *dentry,
  19. struct inode *inode)
  20. {
  21. __fsnotify_d_instantiate(dentry, inode);
  22. }
  23. /* Notify this dentry's parent about a child's events. */
  24. static inline int fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
  25. {
  26. if (!dentry)
  27. dentry = path->dentry;
  28. return __fsnotify_parent(path, dentry, mask);
  29. }
  30. /* simple call site for access decisions */
  31. static inline int fsnotify_perm(struct file *file, int mask)
  32. {
  33. struct path *path = &file->f_path;
  34. struct inode *inode = path->dentry->d_inode;
  35. __u32 fsnotify_mask = 0;
  36. int ret;
  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. ret = fsnotify_parent(path, NULL, fsnotify_mask);
  48. if (ret)
  49. return ret;
  50. return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  51. }
  52. /*
  53. * fsnotify_d_move - dentry has been moved
  54. */
  55. static inline void fsnotify_d_move(struct dentry *dentry)
  56. {
  57. /*
  58. * On move we need to update dentry->d_flags to indicate if the new parent
  59. * cares about events from this dentry.
  60. */
  61. __fsnotify_update_dcache_flags(dentry);
  62. }
  63. /*
  64. * fsnotify_link_count - inode's link count changed
  65. */
  66. static inline void fsnotify_link_count(struct inode *inode)
  67. {
  68. fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  69. }
  70. /*
  71. * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
  72. */
  73. static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
  74. const unsigned char *old_name,
  75. int isdir, struct inode *target, struct dentry *moved)
  76. {
  77. struct inode *source = moved->d_inode;
  78. u32 fs_cookie = fsnotify_get_cookie();
  79. __u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
  80. __u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
  81. const unsigned char *new_name = moved->d_name.name;
  82. if (old_dir == new_dir)
  83. old_dir_mask |= FS_DN_RENAME;
  84. if (isdir) {
  85. old_dir_mask |= FS_ISDIR;
  86. new_dir_mask |= FS_ISDIR;
  87. }
  88. fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
  89. fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
  90. if (target)
  91. fsnotify_link_count(target);
  92. if (source)
  93. fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  94. audit_inode_child(moved, new_dir);
  95. }
  96. /*
  97. * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
  98. */
  99. static inline void fsnotify_inode_delete(struct inode *inode)
  100. {
  101. __fsnotify_inode_delete(inode);
  102. }
  103. /*
  104. * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
  105. */
  106. static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
  107. {
  108. __fsnotify_vfsmount_delete(mnt);
  109. }
  110. /*
  111. * fsnotify_nameremove - a filename was removed from a directory
  112. */
  113. static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
  114. {
  115. __u32 mask = FS_DELETE;
  116. if (isdir)
  117. mask |= FS_ISDIR;
  118. fsnotify_parent(NULL, dentry, mask);
  119. }
  120. /*
  121. * fsnotify_inoderemove - an inode is going away
  122. */
  123. static inline void fsnotify_inoderemove(struct inode *inode)
  124. {
  125. fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  126. __fsnotify_inode_delete(inode);
  127. }
  128. /*
  129. * fsnotify_create - 'name' was linked in
  130. */
  131. static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
  132. {
  133. audit_inode_child(dentry, inode);
  134. fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
  135. }
  136. /*
  137. * fsnotify_link - new hardlink in 'inode' directory
  138. * Note: We have to pass also the linked inode ptr as some filesystems leave
  139. * new_dentry->d_inode NULL and instantiate inode pointer later
  140. */
  141. static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
  142. {
  143. fsnotify_link_count(inode);
  144. audit_inode_child(new_dentry, dir);
  145. fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
  146. }
  147. /*
  148. * fsnotify_mkdir - directory 'name' was created
  149. */
  150. static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
  151. {
  152. __u32 mask = (FS_CREATE | FS_ISDIR);
  153. struct inode *d_inode = dentry->d_inode;
  154. audit_inode_child(dentry, inode);
  155. fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
  156. }
  157. /*
  158. * fsnotify_access - file was read
  159. */
  160. static inline void fsnotify_access(struct file *file)
  161. {
  162. struct path *path = &file->f_path;
  163. struct inode *inode = path->dentry->d_inode;
  164. __u32 mask = FS_ACCESS;
  165. if (S_ISDIR(inode->i_mode))
  166. mask |= FS_ISDIR;
  167. if (!(file->f_mode & FMODE_NONOTIFY)) {
  168. fsnotify_parent(path, NULL, mask);
  169. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  170. }
  171. }
  172. /*
  173. * fsnotify_modify - file was modified
  174. */
  175. static inline void fsnotify_modify(struct file *file)
  176. {
  177. struct path *path = &file->f_path;
  178. struct inode *inode = path->dentry->d_inode;
  179. __u32 mask = FS_MODIFY;
  180. if (S_ISDIR(inode->i_mode))
  181. mask |= FS_ISDIR;
  182. if (!(file->f_mode & FMODE_NONOTIFY)) {
  183. fsnotify_parent(path, NULL, mask);
  184. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  185. }
  186. }
  187. /*
  188. * fsnotify_open - file was opened
  189. */
  190. static inline void fsnotify_open(struct file *file)
  191. {
  192. struct path *path = &file->f_path;
  193. struct inode *inode = path->dentry->d_inode;
  194. __u32 mask = FS_OPEN;
  195. if (S_ISDIR(inode->i_mode))
  196. mask |= FS_ISDIR;
  197. fsnotify_parent(path, NULL, mask);
  198. fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
  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_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_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_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 */