fsnotify.h 8.4 KB

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