blob: 9db31d251c205358fc528eee3bfa774a3af3ff33 [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>
18
19/*
20 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
21 */
22static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
23 const char *old_name, const char *new_name,
John McCutchan75449532005-08-01 11:00:45 -040024 int isdir, struct inode *target)
Robert Love0eeca282005-07-12 17:06:03 -040025{
26 u32 cookie = inotify_get_cookie();
27
28 if (old_dir == new_dir)
29 inode_dir_notify(old_dir, DN_RENAME);
30 else {
31 inode_dir_notify(old_dir, DN_DELETE);
32 inode_dir_notify(new_dir, DN_CREATE);
33 }
34
35 if (isdir)
36 isdir = IN_ISDIR;
37 inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name);
38 inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name);
John McCutchan75449532005-08-01 11:00:45 -040039
40 if (target) {
41 inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL);
42 inotify_inode_is_dead(target);
43 }
Robert Love0eeca282005-07-12 17:06:03 -040044}
45
46/*
47 * fsnotify_unlink - file was unlinked
48 */
John McCutchan0c3dba12005-08-04 21:12:54 -040049static inline void fsnotify_unlink(struct dentry *dentry, struct inode *inode, struct inode *dir)
Robert Love0eeca282005-07-12 17:06:03 -040050{
Robert Love0eeca282005-07-12 17:06:03 -040051 inode_dir_notify(dir, DN_DELETE);
52 inotify_inode_queue_event(dir, IN_DELETE, 0, dentry->d_name.name);
53 inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL);
54
55 inotify_inode_is_dead(inode);
56}
57
58/*
59 * fsnotify_rmdir - directory was removed
60 */
61static inline void fsnotify_rmdir(struct dentry *dentry, struct inode *inode,
62 struct inode *dir)
63{
64 inode_dir_notify(dir, DN_DELETE);
65 inotify_inode_queue_event(dir,IN_DELETE|IN_ISDIR,0,dentry->d_name.name);
66 inotify_inode_queue_event(inode, IN_DELETE_SELF | IN_ISDIR, 0, NULL);
67 inotify_inode_is_dead(inode);
68}
69
70/*
John McCutchan7a91bf72005-08-08 13:52:16 -040071 * fsnotify_nameremove - a filename was removed from a directory
72 */
73static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
74{
75 if (isdir)
76 isdir = IN_ISDIR;
77 dnotify_parent(dentry, DN_DELETE);
78 inotify_dentry_parent_queue_event(dentry, IN_DELETE|isdir, 0, dentry->d_name.name);
79}
80
81/*
82 * fsnotify_inoderemove - an inode is going away
83 */
84static inline void fsnotify_inoderemove(struct inode *inode)
85{
86 inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL);
87 inotify_inode_is_dead(inode);
88}
89
90/*
Robert Love0eeca282005-07-12 17:06:03 -040091 * fsnotify_create - 'name' was linked in
92 */
93static inline void fsnotify_create(struct inode *inode, const char *name)
94{
95 inode_dir_notify(inode, DN_CREATE);
96 inotify_inode_queue_event(inode, IN_CREATE, 0, name);
97}
98
99/*
100 * fsnotify_mkdir - directory 'name' was created
101 */
102static inline void fsnotify_mkdir(struct inode *inode, const char *name)
103{
104 inode_dir_notify(inode, DN_CREATE);
105 inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0, name);
106}
107
108/*
109 * fsnotify_access - file was read
110 */
111static inline void fsnotify_access(struct dentry *dentry)
112{
113 struct inode *inode = dentry->d_inode;
114 u32 mask = IN_ACCESS;
115
116 if (S_ISDIR(inode->i_mode))
117 mask |= IN_ISDIR;
118
119 dnotify_parent(dentry, DN_ACCESS);
120 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
121 inotify_inode_queue_event(inode, mask, 0, NULL);
122}
123
124/*
125 * fsnotify_modify - file was modified
126 */
127static inline void fsnotify_modify(struct dentry *dentry)
128{
129 struct inode *inode = dentry->d_inode;
130 u32 mask = IN_MODIFY;
131
132 if (S_ISDIR(inode->i_mode))
133 mask |= IN_ISDIR;
134
135 dnotify_parent(dentry, DN_MODIFY);
136 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
137 inotify_inode_queue_event(inode, mask, 0, NULL);
138}
139
140/*
141 * fsnotify_open - file was opened
142 */
143static inline void fsnotify_open(struct dentry *dentry)
144{
145 struct inode *inode = dentry->d_inode;
146 u32 mask = IN_OPEN;
147
148 if (S_ISDIR(inode->i_mode))
149 mask |= IN_ISDIR;
150
Robert Love0eeca282005-07-12 17:06:03 -0400151 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
Robert Love5995f162005-07-13 12:38:39 -0400152 inotify_inode_queue_event(inode, mask, 0, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400153}
154
155/*
156 * fsnotify_close - file was closed
157 */
158static inline void fsnotify_close(struct file *file)
159{
160 struct dentry *dentry = file->f_dentry;
161 struct inode *inode = dentry->d_inode;
162 const char *name = dentry->d_name.name;
163 mode_t mode = file->f_mode;
164 u32 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
165
166 if (S_ISDIR(inode->i_mode))
167 mask |= IN_ISDIR;
168
169 inotify_dentry_parent_queue_event(dentry, mask, 0, name);
170 inotify_inode_queue_event(inode, mask, 0, NULL);
171}
172
173/*
174 * fsnotify_xattr - extended attributes were changed
175 */
176static inline void fsnotify_xattr(struct dentry *dentry)
177{
178 struct inode *inode = dentry->d_inode;
179 u32 mask = IN_ATTRIB;
180
181 if (S_ISDIR(inode->i_mode))
182 mask |= IN_ISDIR;
183
184 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
185 inotify_inode_queue_event(inode, mask, 0, NULL);
186}
187
188/*
189 * fsnotify_change - notify_change event. file was modified and/or metadata
190 * was changed.
191 */
192static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
193{
194 struct inode *inode = dentry->d_inode;
195 int dn_mask = 0;
196 u32 in_mask = 0;
197
198 if (ia_valid & ATTR_UID) {
199 in_mask |= IN_ATTRIB;
200 dn_mask |= DN_ATTRIB;
201 }
202 if (ia_valid & ATTR_GID) {
203 in_mask |= IN_ATTRIB;
204 dn_mask |= DN_ATTRIB;
205 }
206 if (ia_valid & ATTR_SIZE) {
207 in_mask |= IN_MODIFY;
208 dn_mask |= DN_MODIFY;
209 }
210 /* both times implies a utime(s) call */
211 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
212 {
213 in_mask |= IN_ATTRIB;
214 dn_mask |= DN_ATTRIB;
215 } else if (ia_valid & ATTR_ATIME) {
216 in_mask |= IN_ACCESS;
217 dn_mask |= DN_ACCESS;
218 } else if (ia_valid & ATTR_MTIME) {
219 in_mask |= IN_MODIFY;
220 dn_mask |= DN_MODIFY;
221 }
222 if (ia_valid & ATTR_MODE) {
223 in_mask |= IN_ATTRIB;
224 dn_mask |= DN_ATTRIB;
225 }
226
227 if (dn_mask)
228 dnotify_parent(dentry, dn_mask);
229 if (in_mask) {
230 if (S_ISDIR(inode->i_mode))
231 in_mask |= IN_ISDIR;
232 inotify_inode_queue_event(inode, in_mask, 0, NULL);
233 inotify_dentry_parent_queue_event(dentry, in_mask, 0,
234 dentry->d_name.name);
235 }
236}
237
238#ifdef CONFIG_INOTIFY /* inotify helpers */
239
240/*
241 * fsnotify_oldname_init - save off the old filename before we change it
242 */
243static inline const char *fsnotify_oldname_init(const char *name)
244{
245 return kstrdup(name, GFP_KERNEL);
246}
247
248/*
249 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
250 */
251static inline void fsnotify_oldname_free(const char *old_name)
252{
253 kfree(old_name);
254}
255
256#else /* CONFIG_INOTIFY */
257
258static inline const char *fsnotify_oldname_init(const char *name)
259{
260 return NULL;
261}
262
263static inline void fsnotify_oldname_free(const char *old_name)
264{
265}
266
267#endif /* ! CONFIG_INOTIFY */
268
269#endif /* __KERNEL__ */
270
271#endif /* _LINUX_FS_NOTIFY_H */