fsnotify.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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/dnotify.h>
  13. #include <linux/inotify.h>
  14. #include <linux/fsnotify_backend.h>
  15. #include <linux/audit.h>
  16. /*
  17. * fsnotify_d_instantiate - instantiate a dentry for inode
  18. * Called with dcache_lock held.
  19. */
  20. static inline void fsnotify_d_instantiate(struct dentry *entry,
  21. struct inode *inode)
  22. {
  23. __fsnotify_d_instantiate(entry, inode);
  24. inotify_d_instantiate(entry, inode);
  25. }
  26. /* Notify this dentry's parent about a child's events. */
  27. static inline void fsnotify_parent(struct dentry *dentry, __u32 mask)
  28. {
  29. __fsnotify_parent(dentry, mask);
  30. inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
  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. inotify_d_move(entry);
  44. }
  45. /*
  46. * fsnotify_link_count - inode's link count changed
  47. */
  48. static inline void fsnotify_link_count(struct inode *inode)
  49. {
  50. inotify_inode_queue_event(inode, IN_ATTRIB, 0, NULL, NULL);
  51. fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE);
  52. }
  53. /*
  54. * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
  55. */
  56. static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
  57. const char *old_name, const char *new_name,
  58. int isdir, struct inode *target, struct dentry *moved)
  59. {
  60. struct inode *source = moved->d_inode;
  61. u32 cookie = inotify_get_cookie();
  62. __u32 old_dir_mask = 0;
  63. __u32 new_dir_mask = 0;
  64. if (old_dir == new_dir) {
  65. inode_dir_notify(old_dir, DN_RENAME);
  66. old_dir_mask = FS_DN_RENAME;
  67. } else {
  68. inode_dir_notify(old_dir, DN_DELETE);
  69. old_dir_mask = FS_DELETE;
  70. inode_dir_notify(new_dir, DN_CREATE);
  71. new_dir_mask = FS_CREATE;
  72. }
  73. if (isdir) {
  74. isdir = IN_ISDIR;
  75. old_dir_mask |= FS_IN_ISDIR;
  76. new_dir_mask |= FS_IN_ISDIR;
  77. }
  78. old_dir_mask |= FS_MOVED_FROM;
  79. new_dir_mask |= FS_MOVED_TO;
  80. inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name,
  81. source);
  82. inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name,
  83. source);
  84. fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE);
  85. fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE);
  86. if (target) {
  87. inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
  88. inotify_inode_is_dead(target);
  89. /* this is really a link_count change not a removal */
  90. fsnotify_link_count(target);
  91. }
  92. if (source) {
  93. inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL);
  94. fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE);
  95. }
  96. audit_inode_child(new_name, 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_nameremove - a filename was removed from a directory
  107. */
  108. static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
  109. {
  110. __u32 mask = FS_DELETE;
  111. if (isdir)
  112. mask |= FS_IN_ISDIR;
  113. dnotify_parent(dentry, DN_DELETE);
  114. fsnotify_parent(dentry, mask);
  115. }
  116. /*
  117. * fsnotify_inoderemove - an inode is going away
  118. */
  119. static inline void fsnotify_inoderemove(struct inode *inode)
  120. {
  121. inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL, NULL);
  122. inotify_inode_is_dead(inode);
  123. fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE);
  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. inode_dir_notify(inode, DN_CREATE);
  132. inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name,
  133. dentry->d_inode);
  134. audit_inode_child(dentry->d_name.name, dentry, inode);
  135. fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE);
  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. inode_dir_notify(dir, DN_CREATE);
  145. inotify_inode_queue_event(dir, IN_CREATE, 0, new_dentry->d_name.name,
  146. inode);
  147. fsnotify_link_count(inode);
  148. audit_inode_child(new_dentry->d_name.name, new_dentry, dir);
  149. fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE);
  150. }
  151. /*
  152. * fsnotify_mkdir - directory 'name' was created
  153. */
  154. static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
  155. {
  156. __u32 mask = (FS_CREATE | FS_IN_ISDIR);
  157. struct inode *d_inode = dentry->d_inode;
  158. inode_dir_notify(inode, DN_CREATE);
  159. inotify_inode_queue_event(inode, mask, 0, dentry->d_name.name, d_inode);
  160. audit_inode_child(dentry->d_name.name, dentry, inode);
  161. fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE);
  162. }
  163. /*
  164. * fsnotify_access - file was read
  165. */
  166. static inline void fsnotify_access(struct dentry *dentry)
  167. {
  168. struct inode *inode = dentry->d_inode;
  169. __u32 mask = FS_ACCESS;
  170. if (S_ISDIR(inode->i_mode))
  171. mask |= FS_IN_ISDIR;
  172. dnotify_parent(dentry, DN_ACCESS);
  173. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  174. fsnotify_parent(dentry, mask);
  175. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
  176. }
  177. /*
  178. * fsnotify_modify - file was modified
  179. */
  180. static inline void fsnotify_modify(struct dentry *dentry)
  181. {
  182. struct inode *inode = dentry->d_inode;
  183. __u32 mask = FS_MODIFY;
  184. if (S_ISDIR(inode->i_mode))
  185. mask |= FS_IN_ISDIR;
  186. dnotify_parent(dentry, DN_MODIFY);
  187. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  188. fsnotify_parent(dentry, mask);
  189. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
  190. }
  191. /*
  192. * fsnotify_open - file was opened
  193. */
  194. static inline void fsnotify_open(struct dentry *dentry)
  195. {
  196. struct inode *inode = dentry->d_inode;
  197. __u32 mask = FS_OPEN;
  198. if (S_ISDIR(inode->i_mode))
  199. mask |= FS_IN_ISDIR;
  200. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  201. fsnotify_parent(dentry, mask);
  202. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
  203. }
  204. /*
  205. * fsnotify_close - file was closed
  206. */
  207. static inline void fsnotify_close(struct file *file)
  208. {
  209. struct dentry *dentry = file->f_path.dentry;
  210. struct inode *inode = 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_IN_ISDIR;
  215. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  216. fsnotify_parent(dentry, mask);
  217. fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE);
  218. }
  219. /*
  220. * fsnotify_xattr - extended attributes were changed
  221. */
  222. static inline void fsnotify_xattr(struct dentry *dentry)
  223. {
  224. struct inode *inode = dentry->d_inode;
  225. __u32 mask = FS_ATTRIB;
  226. if (S_ISDIR(inode->i_mode))
  227. mask |= FS_IN_ISDIR;
  228. inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
  229. fsnotify_parent(dentry, mask);
  230. fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE);
  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. int dn_mask = 0;
  240. __u32 in_mask = 0;
  241. if (ia_valid & ATTR_UID) {
  242. in_mask |= FS_ATTRIB;
  243. dn_mask |= DN_ATTRIB;
  244. }
  245. if (ia_valid & ATTR_GID) {
  246. in_mask |= FS_ATTRIB;
  247. dn_mask |= DN_ATTRIB;
  248. }
  249. if (ia_valid & ATTR_SIZE) {
  250. in_mask |= FS_MODIFY;
  251. dn_mask |= DN_MODIFY;
  252. }
  253. /* both times implies a utime(s) call */
  254. if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
  255. {
  256. in_mask |= FS_ATTRIB;
  257. dn_mask |= DN_ATTRIB;
  258. } else if (ia_valid & ATTR_ATIME) {
  259. in_mask |= FS_ACCESS;
  260. dn_mask |= DN_ACCESS;
  261. } else if (ia_valid & ATTR_MTIME) {
  262. in_mask |= FS_MODIFY;
  263. dn_mask |= DN_MODIFY;
  264. }
  265. if (ia_valid & ATTR_MODE) {
  266. in_mask |= FS_ATTRIB;
  267. dn_mask |= DN_ATTRIB;
  268. }
  269. if (dn_mask)
  270. dnotify_parent(dentry, dn_mask);
  271. if (in_mask) {
  272. if (S_ISDIR(inode->i_mode))
  273. in_mask |= FS_IN_ISDIR;
  274. inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
  275. fsnotify_parent(dentry, in_mask);
  276. fsnotify(inode, in_mask, inode, FSNOTIFY_EVENT_INODE);
  277. }
  278. }
  279. #if defined(CONFIG_INOTIFY) || defined(CONFIG_FSNOTIFY) /* notify helpers */
  280. /*
  281. * fsnotify_oldname_init - save off the old filename before we change it
  282. */
  283. static inline const char *fsnotify_oldname_init(const char *name)
  284. {
  285. return kstrdup(name, GFP_KERNEL);
  286. }
  287. /*
  288. * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
  289. */
  290. static inline void fsnotify_oldname_free(const char *old_name)
  291. {
  292. kfree(old_name);
  293. }
  294. #else /* CONFIG_INOTIFY || CONFIG_FSNOTIFY */
  295. static inline const char *fsnotify_oldname_init(const char *name)
  296. {
  297. return NULL;
  298. }
  299. static inline void fsnotify_oldname_free(const char *old_name)
  300. {
  301. }
  302. #endif /* ! CONFIG_INOTIFY */
  303. #endif /* _LINUX_FS_NOTIFY_H */