Blame view

include/linux/fsnotify.h 8.32 KB
0eeca2830   Robert Love   [PATCH] inotify
1
2
3
4
5
6
7
8
9
10
11
12
  #ifndef _LINUX_FS_NOTIFY_H
  #define _LINUX_FS_NOTIFY_H
  
  /*
   * include/linux/fsnotify.h - generic hooks for filesystem notification, to
   * reduce in-source duplication from both dnotify and inotify.
   *
   * We don't compile any of this away in some complicated menagerie of ifdefs.
   * Instead, we rely on the code inside to optimize away as needed.
   *
   * (C) Copyright 2005 Robert Love
   */
90586523e   Eric Paris   fsnotify: unified...
13
  #include <linux/fsnotify_backend.h>
73241ccca   Amy Griffis   [PATCH] Collect m...
14
  #include <linux/audit.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
15
  #include <linux/slab.h>
187f1882b   Paul Gortmaker   BUG: headers with...
16
  #include <linux/bug.h>
0eeca2830   Robert Love   [PATCH] inotify
17
18
  
  /*
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
19
   * fsnotify_d_instantiate - instantiate a dentry for inode
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
20
   */
e61ce8673   Eric Paris   fsnotify: rename ...
21
22
  static inline void fsnotify_d_instantiate(struct dentry *dentry,
  					  struct inode *inode)
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
23
  {
e61ce8673   Eric Paris   fsnotify: rename ...
24
  	__fsnotify_d_instantiate(dentry, inode);
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
25
  }
c28f7e56e   Eric Paris   fsnotify: parent ...
26
  /* Notify this dentry's parent about a child's events. */
52420392c   Eric Paris   fsnotify: call fs...
27
  static inline int fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
c28f7e56e   Eric Paris   fsnotify: parent ...
28
  {
72acc8544   Andreas Gruenbacher   fsnotify: kill FS...
29
  	if (!dentry)
2069601b3   Linus Torvalds   Revert "fsnotify:...
30
  		dentry = path->dentry;
28c60e37f   Eric Paris   fsnotify: send st...
31

52420392c   Eric Paris   fsnotify: call fs...
32
  	return __fsnotify_parent(path, dentry, mask);
c28f7e56e   Eric Paris   fsnotify: parent ...
33
  }
c4ec54b40   Eric Paris   fsnotify: new fsn...
34
35
36
  /* simple call site for access decisions */
  static inline int fsnotify_perm(struct file *file, int mask)
  {
2069601b3   Linus Torvalds   Revert "fsnotify:...
37
  	struct path *path = &file->f_path;
c77cecee5   David Howells   Replace a bunch o...
38
  	struct inode *inode = file_inode(file);
fb1cfb88c   Eric Paris   fsnotify: initial...
39
  	__u32 fsnotify_mask = 0;
52420392c   Eric Paris   fsnotify: call fs...
40
  	int ret;
c4ec54b40   Eric Paris   fsnotify: new fsn...
41
42
43
44
45
  
  	if (file->f_mode & FMODE_NONOTIFY)
  		return 0;
  	if (!(mask & (MAY_READ | MAY_OPEN)))
  		return 0;
c4ec54b40   Eric Paris   fsnotify: new fsn...
46
47
  	if (mask & MAY_OPEN)
  		fsnotify_mask = FS_OPEN_PERM;
fb1cfb88c   Eric Paris   fsnotify: initial...
48
49
50
51
  	else if (mask & MAY_READ)
  		fsnotify_mask = FS_ACCESS_PERM;
  	else
  		BUG();
c4ec54b40   Eric Paris   fsnotify: new fsn...
52

52420392c   Eric Paris   fsnotify: call fs...
53
54
55
  	ret = fsnotify_parent(path, NULL, fsnotify_mask);
  	if (ret)
  		return ret;
2069601b3   Linus Torvalds   Revert "fsnotify:...
56
  	return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
c4ec54b40   Eric Paris   fsnotify: new fsn...
57
  }
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
58
  /*
e61ce8673   Eric Paris   fsnotify: rename ...
59
   * fsnotify_d_move - dentry has been moved
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
60
   */
e61ce8673   Eric Paris   fsnotify: rename ...
61
  static inline void fsnotify_d_move(struct dentry *dentry)
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
62
  {
c28f7e56e   Eric Paris   fsnotify: parent ...
63
  	/*
e61ce8673   Eric Paris   fsnotify: rename ...
64
65
  	 * On move we need to update dentry->d_flags to indicate if the new parent
  	 * cares about events from this dentry.
c28f7e56e   Eric Paris   fsnotify: parent ...
66
  	 */
e61ce8673   Eric Paris   fsnotify: rename ...
67
  	__fsnotify_update_dcache_flags(dentry);
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
68
69
70
  }
  
  /*
90586523e   Eric Paris   fsnotify: unified...
71
72
73
74
   * fsnotify_link_count - inode's link count changed
   */
  static inline void fsnotify_link_count(struct inode *inode)
  {
47882c6f5   Eric Paris   fsnotify: add cor...
75
  	fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
90586523e   Eric Paris   fsnotify: unified...
76
77
78
  }
  
  /*
0eeca2830   Robert Love   [PATCH] inotify
79
80
81
   * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
   */
  static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
59b0df211   Eric Paris   fsnotify: use uns...
82
  				 const unsigned char *old_name,
5a190ae69   Al Viro   [PATCH] pass dent...
83
  				 int isdir, struct inode *target, struct dentry *moved)
0eeca2830   Robert Love   [PATCH] inotify
84
  {
5a190ae69   Al Viro   [PATCH] pass dent...
85
  	struct inode *source = moved->d_inode;
47882c6f5   Eric Paris   fsnotify: add cor...
86
  	u32 fs_cookie = fsnotify_get_cookie();
ff52cc215   Eric Paris   fsnotify: move ev...
87
88
  	__u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
  	__u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
59b0df211   Eric Paris   fsnotify: use uns...
89
  	const unsigned char *new_name = moved->d_name.name;
0eeca2830   Robert Love   [PATCH] inotify
90

ff52cc215   Eric Paris   fsnotify: move ev...
91
92
  	if (old_dir == new_dir)
  		old_dir_mask |= FS_DN_RENAME;
0eeca2830   Robert Love   [PATCH] inotify
93

90586523e   Eric Paris   fsnotify: unified...
94
  	if (isdir) {
b29866aab   Eric Paris   fsnotify: rename ...
95
96
  		old_dir_mask |= FS_ISDIR;
  		new_dir_mask |= FS_ISDIR;
90586523e   Eric Paris   fsnotify: unified...
97
  	}
47882c6f5   Eric Paris   fsnotify: add cor...
98
99
  	fsnotify(old_dir, old_dir_mask, old_dir, FSNOTIFY_EVENT_INODE, old_name, fs_cookie);
  	fsnotify(new_dir, new_dir_mask, new_dir, FSNOTIFY_EVENT_INODE, new_name, fs_cookie);
90586523e   Eric Paris   fsnotify: unified...
100

2dfc1cae4   Eric Paris   inotify: remove i...
101
  	if (target)
90586523e   Eric Paris   fsnotify: unified...
102
  		fsnotify_link_count(target);
89204c40a   John McCutchan   [PATCH] inotify: ...
103

2dfc1cae4   Eric Paris   inotify: remove i...
104
  	if (source)
47882c6f5   Eric Paris   fsnotify: add cor...
105
  		fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
4fa6b5ecb   Jeff Layton   audit: overhaul _...
106
  	audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
0eeca2830   Robert Love   [PATCH] inotify
107
108
109
  }
  
  /*
3be25f49b   Eric Paris   fsnotify: add mar...
110
111
112
113
114
115
116
117
   * fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed
   */
  static inline void fsnotify_inode_delete(struct inode *inode)
  {
  	__fsnotify_inode_delete(inode);
  }
  
  /*
ca9c726ee   Andreas Gruenbacher   fsnotify: Infrast...
118
119
120
121
122
123
124
125
   * fsnotify_vfsmount_delete - a vfsmount is being destroyed, clean up is needed
   */
  static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
  {
  	__fsnotify_vfsmount_delete(mnt);
  }
  
  /*
7a91bf7f5   John McCutchan   [PATCH] fsnotify_...
126
127
128
129
   * fsnotify_nameremove - a filename was removed from a directory
   */
  static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
  {
90586523e   Eric Paris   fsnotify: unified...
130
  	__u32 mask = FS_DELETE;
7a91bf7f5   John McCutchan   [PATCH] fsnotify_...
131
  	if (isdir)
b29866aab   Eric Paris   fsnotify: rename ...
132
  		mask |= FS_ISDIR;
c28f7e56e   Eric Paris   fsnotify: parent ...
133

28c60e37f   Eric Paris   fsnotify: send st...
134
  	fsnotify_parent(NULL, dentry, mask);
7a91bf7f5   John McCutchan   [PATCH] fsnotify_...
135
136
137
138
139
140
141
  }
  
  /*
   * fsnotify_inoderemove - an inode is going away
   */
  static inline void fsnotify_inoderemove(struct inode *inode)
  {
47882c6f5   Eric Paris   fsnotify: add cor...
142
  	fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
3be25f49b   Eric Paris   fsnotify: add mar...
143
  	__fsnotify_inode_delete(inode);
ece95912d   Jan Kara   inotify: send IN_...
144
145
146
  }
  
  /*
0eeca2830   Robert Love   [PATCH] inotify
147
148
   * fsnotify_create - 'name' was linked in
   */
f38aa9422   Amy Griffis   [PATCH] Pass dent...
149
  static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
0eeca2830   Robert Love   [PATCH] inotify
150
  {
4fa6b5ecb   Jeff Layton   audit: overhaul _...
151
  	audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
90586523e   Eric Paris   fsnotify: unified...
152

47882c6f5   Eric Paris   fsnotify: add cor...
153
  	fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
0eeca2830   Robert Love   [PATCH] inotify
154
155
156
  }
  
  /*
ece95912d   Jan Kara   inotify: send IN_...
157
158
159
160
161
162
   * fsnotify_link - new hardlink in 'inode' directory
   * Note: We have to pass also the linked inode ptr as some filesystems leave
   *   new_dentry->d_inode NULL and instantiate inode pointer later
   */
  static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct dentry *new_dentry)
  {
ece95912d   Jan Kara   inotify: send IN_...
163
  	fsnotify_link_count(inode);
4fa6b5ecb   Jeff Layton   audit: overhaul _...
164
  	audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
90586523e   Eric Paris   fsnotify: unified...
165

47882c6f5   Eric Paris   fsnotify: add cor...
166
  	fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
ece95912d   Jan Kara   inotify: send IN_...
167
168
169
  }
  
  /*
0eeca2830   Robert Love   [PATCH] inotify
170
171
   * fsnotify_mkdir - directory 'name' was created
   */
f38aa9422   Amy Griffis   [PATCH] Pass dent...
172
  static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
0eeca2830   Robert Love   [PATCH] inotify
173
  {
b29866aab   Eric Paris   fsnotify: rename ...
174
  	__u32 mask = (FS_CREATE | FS_ISDIR);
90586523e   Eric Paris   fsnotify: unified...
175
  	struct inode *d_inode = dentry->d_inode;
4fa6b5ecb   Jeff Layton   audit: overhaul _...
176
  	audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
90586523e   Eric Paris   fsnotify: unified...
177

47882c6f5   Eric Paris   fsnotify: add cor...
178
  	fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
0eeca2830   Robert Love   [PATCH] inotify
179
180
181
182
183
  }
  
  /*
   * fsnotify_access - file was read
   */
2a12a9d78   Eric Paris   fsnotify: pass a ...
184
  static inline void fsnotify_access(struct file *file)
0eeca2830   Robert Love   [PATCH] inotify
185
  {
2069601b3   Linus Torvalds   Revert "fsnotify:...
186
  	struct path *path = &file->f_path;
c77cecee5   David Howells   Replace a bunch o...
187
  	struct inode *inode = file_inode(file);
90586523e   Eric Paris   fsnotify: unified...
188
  	__u32 mask = FS_ACCESS;
0eeca2830   Robert Love   [PATCH] inotify
189
190
  
  	if (S_ISDIR(inode->i_mode))
b29866aab   Eric Paris   fsnotify: rename ...
191
  		mask |= FS_ISDIR;
0eeca2830   Robert Love   [PATCH] inotify
192

ecf081d1a   Eric Paris   vfs: introduce FM...
193
  	if (!(file->f_mode & FMODE_NONOTIFY)) {
2069601b3   Linus Torvalds   Revert "fsnotify:...
194
195
  		fsnotify_parent(path, NULL, mask);
  		fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
ecf081d1a   Eric Paris   vfs: introduce FM...
196
  	}
0eeca2830   Robert Love   [PATCH] inotify
197
198
199
200
201
  }
  
  /*
   * fsnotify_modify - file was modified
   */
2a12a9d78   Eric Paris   fsnotify: pass a ...
202
  static inline void fsnotify_modify(struct file *file)
0eeca2830   Robert Love   [PATCH] inotify
203
  {
2069601b3   Linus Torvalds   Revert "fsnotify:...
204
  	struct path *path = &file->f_path;
c77cecee5   David Howells   Replace a bunch o...
205
  	struct inode *inode = file_inode(file);
90586523e   Eric Paris   fsnotify: unified...
206
  	__u32 mask = FS_MODIFY;
0eeca2830   Robert Love   [PATCH] inotify
207
208
  
  	if (S_ISDIR(inode->i_mode))
b29866aab   Eric Paris   fsnotify: rename ...
209
  		mask |= FS_ISDIR;
0eeca2830   Robert Love   [PATCH] inotify
210

ecf081d1a   Eric Paris   vfs: introduce FM...
211
  	if (!(file->f_mode & FMODE_NONOTIFY)) {
2069601b3   Linus Torvalds   Revert "fsnotify:...
212
213
  		fsnotify_parent(path, NULL, mask);
  		fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
ecf081d1a   Eric Paris   vfs: introduce FM...
214
  	}
0eeca2830   Robert Love   [PATCH] inotify
215
216
217
218
219
  }
  
  /*
   * fsnotify_open - file was opened
   */
2a12a9d78   Eric Paris   fsnotify: pass a ...
220
  static inline void fsnotify_open(struct file *file)
0eeca2830   Robert Love   [PATCH] inotify
221
  {
2069601b3   Linus Torvalds   Revert "fsnotify:...
222
  	struct path *path = &file->f_path;
c77cecee5   David Howells   Replace a bunch o...
223
  	struct inode *inode = file_inode(file);
90586523e   Eric Paris   fsnotify: unified...
224
  	__u32 mask = FS_OPEN;
0eeca2830   Robert Love   [PATCH] inotify
225
226
  
  	if (S_ISDIR(inode->i_mode))
b29866aab   Eric Paris   fsnotify: rename ...
227
  		mask |= FS_ISDIR;
0eeca2830   Robert Love   [PATCH] inotify
228

6bff7eccb   Lino Sanfilippo   Ensure FMODE_NONO...
229
230
  	fsnotify_parent(path, NULL, mask);
  	fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
0eeca2830   Robert Love   [PATCH] inotify
231
232
233
234
235
236
237
  }
  
  /*
   * fsnotify_close - file was closed
   */
  static inline void fsnotify_close(struct file *file)
  {
2069601b3   Linus Torvalds   Revert "fsnotify:...
238
  	struct path *path = &file->f_path;
496ad9aa8   Al Viro   new helper: file_...
239
  	struct inode *inode = file_inode(file);
aeb5d7270   Al Viro   [PATCH] introduce...
240
  	fmode_t mode = file->f_mode;
90586523e   Eric Paris   fsnotify: unified...
241
  	__u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
0eeca2830   Robert Love   [PATCH] inotify
242
243
  
  	if (S_ISDIR(inode->i_mode))
b29866aab   Eric Paris   fsnotify: rename ...
244
  		mask |= FS_ISDIR;
0eeca2830   Robert Love   [PATCH] inotify
245

ecf081d1a   Eric Paris   vfs: introduce FM...
246
  	if (!(file->f_mode & FMODE_NONOTIFY)) {
2069601b3   Linus Torvalds   Revert "fsnotify:...
247
248
  		fsnotify_parent(path, NULL, mask);
  		fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
ecf081d1a   Eric Paris   vfs: introduce FM...
249
  	}
0eeca2830   Robert Love   [PATCH] inotify
250
251
252
253
254
255
256
257
  }
  
  /*
   * fsnotify_xattr - extended attributes were changed
   */
  static inline void fsnotify_xattr(struct dentry *dentry)
  {
  	struct inode *inode = dentry->d_inode;
90586523e   Eric Paris   fsnotify: unified...
258
  	__u32 mask = FS_ATTRIB;
0eeca2830   Robert Love   [PATCH] inotify
259
260
  
  	if (S_ISDIR(inode->i_mode))
b29866aab   Eric Paris   fsnotify: rename ...
261
  		mask |= FS_ISDIR;
0eeca2830   Robert Love   [PATCH] inotify
262

28c60e37f   Eric Paris   fsnotify: send st...
263
  	fsnotify_parent(NULL, dentry, mask);
47882c6f5   Eric Paris   fsnotify: add cor...
264
  	fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
0eeca2830   Robert Love   [PATCH] inotify
265
266
267
268
269
270
271
272
273
  }
  
  /*
   * fsnotify_change - notify_change event.  file was modified and/or metadata
   * was changed.
   */
  static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
  {
  	struct inode *inode = dentry->d_inode;
3c5119c05   Eric Paris   dnotify: reimplem...
274
275
276
277
278
279
280
281
  	__u32 mask = 0;
  
  	if (ia_valid & ATTR_UID)
  		mask |= FS_ATTRIB;
  	if (ia_valid & ATTR_GID)
  		mask |= FS_ATTRIB;
  	if (ia_valid & ATTR_SIZE)
  		mask |= FS_MODIFY;
0eeca2830   Robert Love   [PATCH] inotify
282

0eeca2830   Robert Love   [PATCH] inotify
283
284
  	/* both times implies a utime(s) call */
  	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
3c5119c05   Eric Paris   dnotify: reimplem...
285
286
287
288
289
290
291
292
  		mask |= FS_ATTRIB;
  	else if (ia_valid & ATTR_ATIME)
  		mask |= FS_ACCESS;
  	else if (ia_valid & ATTR_MTIME)
  		mask |= FS_MODIFY;
  
  	if (ia_valid & ATTR_MODE)
  		mask |= FS_ATTRIB;
0eeca2830   Robert Love   [PATCH] inotify
293

3c5119c05   Eric Paris   dnotify: reimplem...
294
  	if (mask) {
0eeca2830   Robert Love   [PATCH] inotify
295
  		if (S_ISDIR(inode->i_mode))
b29866aab   Eric Paris   fsnotify: rename ...
296
  			mask |= FS_ISDIR;
28c60e37f   Eric Paris   fsnotify: send st...
297
298
  
  		fsnotify_parent(NULL, dentry, mask);
47882c6f5   Eric Paris   fsnotify: add cor...
299
  		fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
0eeca2830   Robert Love   [PATCH] inotify
300
301
  	}
  }
2dfc1cae4   Eric Paris   inotify: remove i...
302
  #if defined(CONFIG_FSNOTIFY)	/* notify helpers */
0eeca2830   Robert Love   [PATCH] inotify
303
304
305
306
  
  /*
   * fsnotify_oldname_init - save off the old filename before we change it
   */
59b0df211   Eric Paris   fsnotify: use uns...
307
  static inline const unsigned char *fsnotify_oldname_init(const unsigned char *name)
0eeca2830   Robert Love   [PATCH] inotify
308
309
310
311
312
313
314
  {
  	return kstrdup(name, GFP_KERNEL);
  }
  
  /*
   * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
   */
59b0df211   Eric Paris   fsnotify: use uns...
315
  static inline void fsnotify_oldname_free(const unsigned char *old_name)
0eeca2830   Robert Love   [PATCH] inotify
316
317
318
  {
  	kfree(old_name);
  }
28c60e37f   Eric Paris   fsnotify: send st...
319
  #else	/* CONFIG_FSNOTIFY */
0eeca2830   Robert Love   [PATCH] inotify
320

59b0df211   Eric Paris   fsnotify: use uns...
321
  static inline const char *fsnotify_oldname_init(const unsigned char *name)
0eeca2830   Robert Love   [PATCH] inotify
322
323
324
  {
  	return NULL;
  }
59b0df211   Eric Paris   fsnotify: use uns...
325
  static inline void fsnotify_oldname_free(const unsigned char *old_name)
0eeca2830   Robert Love   [PATCH] inotify
326
327
  {
  }
28c60e37f   Eric Paris   fsnotify: send st...
328
  #endif	/*  CONFIG_FSNOTIFY */
0eeca2830   Robert Love   [PATCH] inotify
329

0eeca2830   Robert Love   [PATCH] inotify
330
  #endif	/* _LINUX_FS_NOTIFY_H */