fsnotify.h 8.3 KB

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