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