[PATCH] inotify: fix file deletion by rename detection
[safe/jmp/linux-2.6] / include / linux / fsnotify.h
1 #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  */
22 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
23                                  const char *old_name, const char *new_name,
24                                  int isdir, struct inode *target)
25 {
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);
39
40         if (target) {
41                 inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL);
42                 inotify_inode_is_dead(target);
43         }
44 }
45
46 /*
47  * fsnotify_unlink - file was unlinked
48  */
49 static inline void fsnotify_unlink(struct dentry *dentry, struct inode *dir)
50 {
51         struct inode *inode = dentry->d_inode;
52
53         inode_dir_notify(dir, DN_DELETE);
54         inotify_inode_queue_event(dir, IN_DELETE, 0, dentry->d_name.name);
55         inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL);
56
57         inotify_inode_is_dead(inode);
58 }
59
60 /*
61  * fsnotify_rmdir - directory was removed
62  */
63 static inline void fsnotify_rmdir(struct dentry *dentry, struct inode *inode,
64                                   struct inode *dir)
65 {
66         inode_dir_notify(dir, DN_DELETE);
67         inotify_inode_queue_event(dir,IN_DELETE|IN_ISDIR,0,dentry->d_name.name);
68         inotify_inode_queue_event(inode, IN_DELETE_SELF | IN_ISDIR, 0, NULL);
69         inotify_inode_is_dead(inode);
70 }
71
72 /*
73  * fsnotify_create - 'name' was linked in
74  */
75 static inline void fsnotify_create(struct inode *inode, const char *name)
76 {
77         inode_dir_notify(inode, DN_CREATE);
78         inotify_inode_queue_event(inode, IN_CREATE, 0, name);
79 }
80
81 /*
82  * fsnotify_mkdir - directory 'name' was created
83  */
84 static inline void fsnotify_mkdir(struct inode *inode, const char *name)
85 {
86         inode_dir_notify(inode, DN_CREATE);
87         inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0, name);
88 }
89
90 /*
91  * fsnotify_access - file was read
92  */
93 static inline void fsnotify_access(struct dentry *dentry)
94 {
95         struct inode *inode = dentry->d_inode;
96         u32 mask = IN_ACCESS;
97
98         if (S_ISDIR(inode->i_mode))
99                 mask |= IN_ISDIR;
100
101         dnotify_parent(dentry, DN_ACCESS);
102         inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
103         inotify_inode_queue_event(inode, mask, 0, NULL);
104 }
105
106 /*
107  * fsnotify_modify - file was modified
108  */
109 static inline void fsnotify_modify(struct dentry *dentry)
110 {
111         struct inode *inode = dentry->d_inode;
112         u32 mask = IN_MODIFY;
113
114         if (S_ISDIR(inode->i_mode))
115                 mask |= IN_ISDIR;
116
117         dnotify_parent(dentry, DN_MODIFY);
118         inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
119         inotify_inode_queue_event(inode, mask, 0, NULL);
120 }
121
122 /*
123  * fsnotify_open - file was opened
124  */
125 static inline void fsnotify_open(struct dentry *dentry)
126 {
127         struct inode *inode = dentry->d_inode;
128         u32 mask = IN_OPEN;
129
130         if (S_ISDIR(inode->i_mode))
131                 mask |= IN_ISDIR;
132
133         inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
134         inotify_inode_queue_event(inode, mask, 0, NULL);        
135 }
136
137 /*
138  * fsnotify_close - file was closed
139  */
140 static inline void fsnotify_close(struct file *file)
141 {
142         struct dentry *dentry = file->f_dentry;
143         struct inode *inode = dentry->d_inode;
144         const char *name = dentry->d_name.name;
145         mode_t mode = file->f_mode;
146         u32 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
147
148         if (S_ISDIR(inode->i_mode))
149                 mask |= IN_ISDIR;
150
151         inotify_dentry_parent_queue_event(dentry, mask, 0, name);
152         inotify_inode_queue_event(inode, mask, 0, NULL);
153 }
154
155 /*
156  * fsnotify_xattr - extended attributes were changed
157  */
158 static inline void fsnotify_xattr(struct dentry *dentry)
159 {
160         struct inode *inode = dentry->d_inode;
161         u32 mask = IN_ATTRIB;
162
163         if (S_ISDIR(inode->i_mode))
164                 mask |= IN_ISDIR;
165
166         inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
167         inotify_inode_queue_event(inode, mask, 0, NULL);
168 }
169
170 /*
171  * fsnotify_change - notify_change event.  file was modified and/or metadata
172  * was changed.
173  */
174 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
175 {
176         struct inode *inode = dentry->d_inode;
177         int dn_mask = 0;
178         u32 in_mask = 0;
179
180         if (ia_valid & ATTR_UID) {
181                 in_mask |= IN_ATTRIB;
182                 dn_mask |= DN_ATTRIB;
183         }
184         if (ia_valid & ATTR_GID) {
185                 in_mask |= IN_ATTRIB;
186                 dn_mask |= DN_ATTRIB;
187         }
188         if (ia_valid & ATTR_SIZE) {
189                 in_mask |= IN_MODIFY;
190                 dn_mask |= DN_MODIFY;
191         }
192         /* both times implies a utime(s) call */
193         if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
194         {
195                 in_mask |= IN_ATTRIB;
196                 dn_mask |= DN_ATTRIB;
197         } else if (ia_valid & ATTR_ATIME) {
198                 in_mask |= IN_ACCESS;
199                 dn_mask |= DN_ACCESS;
200         } else if (ia_valid & ATTR_MTIME) {
201                 in_mask |= IN_MODIFY;
202                 dn_mask |= DN_MODIFY;
203         }
204         if (ia_valid & ATTR_MODE) {
205                 in_mask |= IN_ATTRIB;
206                 dn_mask |= DN_ATTRIB;
207         }
208
209         if (dn_mask)
210                 dnotify_parent(dentry, dn_mask);
211         if (in_mask) {
212                 if (S_ISDIR(inode->i_mode))
213                         in_mask |= IN_ISDIR;
214                 inotify_inode_queue_event(inode, in_mask, 0, NULL);
215                 inotify_dentry_parent_queue_event(dentry, in_mask, 0,
216                                                   dentry->d_name.name);
217         }
218 }
219
220 #ifdef CONFIG_INOTIFY   /* inotify helpers */
221
222 /*
223  * fsnotify_oldname_init - save off the old filename before we change it
224  */
225 static inline const char *fsnotify_oldname_init(const char *name)
226 {
227         return kstrdup(name, GFP_KERNEL);
228 }
229
230 /*
231  * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
232  */
233 static inline void fsnotify_oldname_free(const char *old_name)
234 {
235         kfree(old_name);
236 }
237
238 #else   /* CONFIG_INOTIFY */
239
240 static inline const char *fsnotify_oldname_init(const char *name)
241 {
242         return NULL;
243 }
244
245 static inline void fsnotify_oldname_free(const char *old_name)
246 {
247 }
248
249 #endif  /* ! CONFIG_INOTIFY */
250
251 #endif  /* __KERNEL__ */
252
253 #endif  /* _LINUX_FS_NOTIFY_H */