blob: 94919c376a723ad50804af9a85921b309b5eb171 [file] [log] [blame]
Robert Love0eeca282005-07-12 17:06:03 -04001#ifndef _LINUX_FS_NOTIFY_H
2#define _LINUX_FS_NOTIFY_H
3
4/*
5 * include/linux/fsnotify.h - generic hooks for filesystem notification, to
6 * reduce in-source duplication from both dnotify and inotify.
7 *
8 * We don't compile any of this away in some complicated menagerie of ifdefs.
9 * Instead, we rely on the code inside to optimize away as needed.
10 *
11 * (C) Copyright 2005 Robert Love
12 */
13
14#ifdef __KERNEL__
15
16#include <linux/dnotify.h>
17#include <linux/inotify.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000018#include <linux/audit.h>
Robert Love0eeca282005-07-12 17:06:03 -040019
20/*
21 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
22 */
23static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
24 const char *old_name, const char *new_name,
John McCutchan89204c42005-08-15 12:13:28 -040025 int isdir, struct inode *target, struct inode *source)
Robert Love0eeca282005-07-12 17:06:03 -040026{
27 u32 cookie = inotify_get_cookie();
28
29 if (old_dir == new_dir)
30 inode_dir_notify(old_dir, DN_RENAME);
31 else {
32 inode_dir_notify(old_dir, DN_DELETE);
33 inode_dir_notify(new_dir, DN_CREATE);
34 }
35
36 if (isdir)
37 isdir = IN_ISDIR;
38 inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name);
39 inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name);
John McCutchan75449532005-08-01 11:00:45 -040040
41 if (target) {
42 inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL);
43 inotify_inode_is_dead(target);
44 }
John McCutchan89204c42005-08-15 12:13:28 -040045
46 if (source) {
47 inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL);
48 }
Amy Griffis73241cc2005-11-03 16:00:25 +000049 audit_inode_child(old_name, source, old_dir->i_ino);
50 audit_inode_child(new_name, target, new_dir->i_ino);
Robert Love0eeca282005-07-12 17:06:03 -040051}
52
53/*
John McCutchan7a91bf72005-08-08 13:52:16 -040054 * fsnotify_nameremove - a filename was removed from a directory
55 */
56static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
57{
58 if (isdir)
59 isdir = IN_ISDIR;
60 dnotify_parent(dentry, DN_DELETE);
61 inotify_dentry_parent_queue_event(dentry, IN_DELETE|isdir, 0, dentry->d_name.name);
62}
63
64/*
65 * fsnotify_inoderemove - an inode is going away
66 */
67static inline void fsnotify_inoderemove(struct inode *inode)
68{
69 inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL);
70 inotify_inode_is_dead(inode);
71}
72
73/*
Robert Love0eeca282005-07-12 17:06:03 -040074 * fsnotify_create - 'name' was linked in
75 */
Amy Griffisf38aa942005-11-03 15:57:06 +000076static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -040077{
78 inode_dir_notify(inode, DN_CREATE);
Amy Griffisf38aa942005-11-03 15:57:06 +000079 inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name);
Amy Griffis73241cc2005-11-03 16:00:25 +000080 audit_inode_child(dentry->d_name.name, dentry->d_inode, inode->i_ino);
Robert Love0eeca282005-07-12 17:06:03 -040081}
82
83/*
84 * fsnotify_mkdir - directory 'name' was created
85 */
Amy Griffisf38aa942005-11-03 15:57:06 +000086static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
Robert Love0eeca282005-07-12 17:06:03 -040087{
88 inode_dir_notify(inode, DN_CREATE);
Amy Griffisf38aa942005-11-03 15:57:06 +000089 inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0,
90 dentry->d_name.name);
Amy Griffis73241cc2005-11-03 16:00:25 +000091 audit_inode_child(dentry->d_name.name, dentry->d_inode, inode->i_ino);
Robert Love0eeca282005-07-12 17:06:03 -040092}
93
94/*
95 * fsnotify_access - file was read
96 */
97static inline void fsnotify_access(struct dentry *dentry)
98{
99 struct inode *inode = dentry->d_inode;
100 u32 mask = IN_ACCESS;
101
102 if (S_ISDIR(inode->i_mode))
103 mask |= IN_ISDIR;
104
105 dnotify_parent(dentry, DN_ACCESS);
106 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
107 inotify_inode_queue_event(inode, mask, 0, NULL);
108}
109
110/*
111 * fsnotify_modify - file was modified
112 */
113static inline void fsnotify_modify(struct dentry *dentry)
114{
115 struct inode *inode = dentry->d_inode;
116 u32 mask = IN_MODIFY;
117
118 if (S_ISDIR(inode->i_mode))
119 mask |= IN_ISDIR;
120
121 dnotify_parent(dentry, DN_MODIFY);
122 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
123 inotify_inode_queue_event(inode, mask, 0, NULL);
124}
125
126/*
127 * fsnotify_open - file was opened
128 */
129static inline void fsnotify_open(struct dentry *dentry)
130{
131 struct inode *inode = dentry->d_inode;
132 u32 mask = IN_OPEN;
133
134 if (S_ISDIR(inode->i_mode))
135 mask |= IN_ISDIR;
136
Robert Love0eeca282005-07-12 17:06:03 -0400137 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
Robert Love5995f162005-07-13 12:38:39 -0400138 inotify_inode_queue_event(inode, mask, 0, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400139}
140
141/*
142 * fsnotify_close - file was closed
143 */
144static inline void fsnotify_close(struct file *file)
145{
146 struct dentry *dentry = file->f_dentry;
147 struct inode *inode = dentry->d_inode;
148 const char *name = dentry->d_name.name;
149 mode_t mode = file->f_mode;
150 u32 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
151
152 if (S_ISDIR(inode->i_mode))
153 mask |= IN_ISDIR;
154
155 inotify_dentry_parent_queue_event(dentry, mask, 0, name);
156 inotify_inode_queue_event(inode, mask, 0, NULL);
157}
158
159/*
160 * fsnotify_xattr - extended attributes were changed
161 */
162static inline void fsnotify_xattr(struct dentry *dentry)
163{
164 struct inode *inode = dentry->d_inode;
165 u32 mask = IN_ATTRIB;
166
167 if (S_ISDIR(inode->i_mode))
168 mask |= IN_ISDIR;
169
170 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
171 inotify_inode_queue_event(inode, mask, 0, NULL);
172}
173
174/*
175 * fsnotify_change - notify_change event. file was modified and/or metadata
176 * was changed.
177 */
178static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
179{
180 struct inode *inode = dentry->d_inode;
181 int dn_mask = 0;
182 u32 in_mask = 0;
183
184 if (ia_valid & ATTR_UID) {
185 in_mask |= IN_ATTRIB;
186 dn_mask |= DN_ATTRIB;
187 }
188 if (ia_valid & ATTR_GID) {
189 in_mask |= IN_ATTRIB;
190 dn_mask |= DN_ATTRIB;
191 }
192 if (ia_valid & ATTR_SIZE) {
193 in_mask |= IN_MODIFY;
194 dn_mask |= DN_MODIFY;
195 }
196 /* both times implies a utime(s) call */
197 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
198 {
199 in_mask |= IN_ATTRIB;
200 dn_mask |= DN_ATTRIB;
201 } else if (ia_valid & ATTR_ATIME) {
202 in_mask |= IN_ACCESS;
203 dn_mask |= DN_ACCESS;
204 } else if (ia_valid & ATTR_MTIME) {
205 in_mask |= IN_MODIFY;
206 dn_mask |= DN_MODIFY;
207 }
208 if (ia_valid & ATTR_MODE) {
209 in_mask |= IN_ATTRIB;
210 dn_mask |= DN_ATTRIB;
211 }
212
213 if (dn_mask)
214 dnotify_parent(dentry, dn_mask);
215 if (in_mask) {
216 if (S_ISDIR(inode->i_mode))
217 in_mask |= IN_ISDIR;
218 inotify_inode_queue_event(inode, in_mask, 0, NULL);
219 inotify_dentry_parent_queue_event(dentry, in_mask, 0,
220 dentry->d_name.name);
221 }
222}
223
224#ifdef CONFIG_INOTIFY /* inotify helpers */
225
226/*
227 * fsnotify_oldname_init - save off the old filename before we change it
228 */
229static inline const char *fsnotify_oldname_init(const char *name)
230{
231 return kstrdup(name, GFP_KERNEL);
232}
233
234/*
235 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
236 */
237static inline void fsnotify_oldname_free(const char *old_name)
238{
239 kfree(old_name);
240}
241
242#else /* CONFIG_INOTIFY */
243
244static inline const char *fsnotify_oldname_init(const char *name)
245{
246 return NULL;
247}
248
249static inline void fsnotify_oldname_free(const char *old_name)
250{
251}
252
253#endif /* ! CONFIG_INOTIFY */
254
255#endif /* __KERNEL__ */
256
257#endif /* _LINUX_FS_NOTIFY_H */