fsnotify.h 8.6 KB
Newer Older
R
Robert Love 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#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
 */

#include <linux/dnotify.h>
#include <linux/inotify.h>
16
#include <linux/fsnotify_backend.h>
17
#include <linux/audit.h>
18
#include <linux/slab.h>
R
Robert Love 已提交
19

20 21 22 23 24 25 26
/*
 * fsnotify_d_instantiate - instantiate a dentry for inode
 * Called with dcache_lock held.
 */
static inline void fsnotify_d_instantiate(struct dentry *entry,
						struct inode *inode)
{
E
Eric Paris 已提交
27 28
	__fsnotify_d_instantiate(entry, inode);

29 30 31
	inotify_d_instantiate(entry, inode);
}

E
Eric Paris 已提交
32 33 34 35 36 37 38 39
/* Notify this dentry's parent about a child's events. */
static inline void fsnotify_parent(struct dentry *dentry, __u32 mask)
{
	__fsnotify_parent(dentry, mask);

	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
}

40 41 42 43 44 45
/*
 * fsnotify_d_move - entry has been moved
 * Called with dcache_lock and entry->d_lock held.
 */
static inline void fsnotify_d_move(struct dentry *entry)
{
E
Eric Paris 已提交
46 47 48 49 50 51
	/*
	 * On move we need to update entry->d_flags to indicate if the new parent
	 * cares about events from this entry.
	 */
	__fsnotify_update_dcache_flags(entry);

52 53 54
	inotify_d_move(entry);
}

55 56 57 58 59 60 61
/*
 * fsnotify_link_count - inode's link count changed
 */
static inline void fsnotify_link_count(struct inode *inode)
{
	inotify_inode_queue_event(inode, IN_ATTRIB, 0, NULL, NULL);

62
	fsnotify(inode, FS_ATTRIB, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
63 64
}

R
Robert Love 已提交
65 66 67 68
/*
 * 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,
69
				 const char *old_name,
70
				 int isdir, struct inode *target, struct dentry *moved)
R
Robert Love 已提交
71
{
72
	struct inode *source = moved->d_inode;
73 74
	u32 in_cookie = inotify_get_cookie();
	u32 fs_cookie = fsnotify_get_cookie();
75 76
	__u32 old_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_FROM);
	__u32 new_dir_mask = (FS_EVENT_ON_CHILD | FS_MOVED_TO);
77
	const char *new_name = moved->d_name.name;
R
Robert Love 已提交
78

79 80
	if (old_dir == new_dir)
		old_dir_mask |= FS_DN_RENAME;
R
Robert Love 已提交
81

82
	if (isdir) {
R
Robert Love 已提交
83
		isdir = IN_ISDIR;
84 85 86 87
		old_dir_mask |= FS_IN_ISDIR;
		new_dir_mask |= FS_IN_ISDIR;
	}

88
	inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir, in_cookie, old_name,
89
				  source);
90
	inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, in_cookie, new_name,
91
				  source);
92

93 94
	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);
95

96
	if (target) {
97
		inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
98
		inotify_inode_is_dead(target);
99 100 101

		/* this is really a link_count change not a removal */
		fsnotify_link_count(target);
102
	}
103 104

	if (source) {
105
		inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL);
106
		fsnotify(source, FS_MOVE_SELF, moved->d_inode, FSNOTIFY_EVENT_INODE, NULL, 0);
107
	}
108
	audit_inode_child(moved, new_dir);
R
Robert Love 已提交
109 110
}

111 112 113 114 115 116 117 118
/*
 * 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);
}

119 120 121 122 123
/*
 * fsnotify_nameremove - a filename was removed from a directory
 */
static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
{
124 125
	__u32 mask = FS_DELETE;

126
	if (isdir)
127
		mask |= FS_IN_ISDIR;
E
Eric Paris 已提交
128 129

	fsnotify_parent(dentry, mask);
130 131 132 133 134 135 136
}

/*
 * fsnotify_inoderemove - an inode is going away
 */
static inline void fsnotify_inoderemove(struct inode *inode)
{
137
	inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL, NULL);
138 139
	inotify_inode_is_dead(inode);

140
	fsnotify(inode, FS_DELETE_SELF, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
141
	__fsnotify_inode_delete(inode);
142 143
}

R
Robert Love 已提交
144 145 146
/*
 * fsnotify_create - 'name' was linked in
 */
147
static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
R
Robert Love 已提交
148
{
149 150
	inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name,
				  dentry->d_inode);
151
	audit_inode_child(dentry, inode);
152

153
	fsnotify(inode, FS_CREATE, dentry->d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
R
Robert Love 已提交
154 155
}

156 157 158 159 160 161 162 163 164 165
/*
 * 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)
{
	inotify_inode_queue_event(dir, IN_CREATE, 0, new_dentry->d_name.name,
				  inode);
	fsnotify_link_count(inode);
166
	audit_inode_child(new_dentry, dir);
167

168
	fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
169 170
}

R
Robert Love 已提交
171 172 173
/*
 * fsnotify_mkdir - directory 'name' was created
 */
174
static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
R
Robert Love 已提交
175
{
176 177 178 179
	__u32 mask = (FS_CREATE | FS_IN_ISDIR);
	struct inode *d_inode = dentry->d_inode;

	inotify_inode_queue_event(inode, mask, 0, dentry->d_name.name, d_inode);
180
	audit_inode_child(dentry, inode);
181

182
	fsnotify(inode, mask, d_inode, FSNOTIFY_EVENT_INODE, dentry->d_name.name, 0);
R
Robert Love 已提交
183 184 185 186 187 188 189 190
}

/*
 * fsnotify_access - file was read
 */
static inline void fsnotify_access(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
191
	__u32 mask = FS_ACCESS;
R
Robert Love 已提交
192 193

	if (S_ISDIR(inode->i_mode))
194
		mask |= FS_IN_ISDIR;
R
Robert Love 已提交
195

196
	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
197

E
Eric Paris 已提交
198
	fsnotify_parent(dentry, mask);
199
	fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
R
Robert Love 已提交
200 201 202 203 204 205 206 207
}

/*
 * fsnotify_modify - file was modified
 */
static inline void fsnotify_modify(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
208
	__u32 mask = FS_MODIFY;
R
Robert Love 已提交
209 210

	if (S_ISDIR(inode->i_mode))
211
		mask |= FS_IN_ISDIR;
R
Robert Love 已提交
212

213
	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
214

E
Eric Paris 已提交
215
	fsnotify_parent(dentry, mask);
216
	fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
R
Robert Love 已提交
217 218 219 220 221 222 223 224
}

/*
 * fsnotify_open - file was opened
 */
static inline void fsnotify_open(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
225
	__u32 mask = FS_OPEN;
R
Robert Love 已提交
226 227

	if (S_ISDIR(inode->i_mode))
228
		mask |= FS_IN_ISDIR;
R
Robert Love 已提交
229

230
	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
231

E
Eric Paris 已提交
232
	fsnotify_parent(dentry, mask);
233
	fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
R
Robert Love 已提交
234 235 236 237 238 239 240
}

/*
 * fsnotify_close - file was closed
 */
static inline void fsnotify_close(struct file *file)
{
241
	struct dentry *dentry = file->f_path.dentry;
R
Robert Love 已提交
242
	struct inode *inode = dentry->d_inode;
243
	fmode_t mode = file->f_mode;
244
	__u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
R
Robert Love 已提交
245 246

	if (S_ISDIR(inode->i_mode))
247
		mask |= FS_IN_ISDIR;
R
Robert Love 已提交
248

249
	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
250

E
Eric Paris 已提交
251
	fsnotify_parent(dentry, mask);
252
	fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
R
Robert Love 已提交
253 254 255 256 257 258 259 260
}

/*
 * fsnotify_xattr - extended attributes were changed
 */
static inline void fsnotify_xattr(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;
261
	__u32 mask = FS_ATTRIB;
R
Robert Love 已提交
262 263

	if (S_ISDIR(inode->i_mode))
264
		mask |= FS_IN_ISDIR;
R
Robert Love 已提交
265

266
	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
267

E
Eric Paris 已提交
268
	fsnotify_parent(dentry, mask);
269
	fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
R
Robert Love 已提交
270 271 272 273 274 275 276 277 278
}

/*
 * 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;
279 280 281 282 283 284 285 286
	__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;
R
Robert Love 已提交
287 288 289

	/* both times implies a utime(s) call */
	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
290 291 292 293 294 295 296 297
		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;
R
Robert Love 已提交
298

299
	if (mask) {
R
Robert Love 已提交
300
		if (S_ISDIR(inode->i_mode))
301 302
			mask |= FS_IN_ISDIR;
		inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
E
Eric Paris 已提交
303

304
		fsnotify_parent(dentry, mask);
305
		fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
R
Robert Love 已提交
306 307 308
	}
}

309
#if defined(CONFIG_INOTIFY) || defined(CONFIG_FSNOTIFY)	/* notify helpers */
R
Robert Love 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

/*
 * fsnotify_oldname_init - save off the old filename before we change it
 */
static inline const char *fsnotify_oldname_init(const char *name)
{
	return kstrdup(name, GFP_KERNEL);
}

/*
 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
 */
static inline void fsnotify_oldname_free(const char *old_name)
{
	kfree(old_name);
}

327
#else	/* CONFIG_INOTIFY || CONFIG_FSNOTIFY */
R
Robert Love 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340

static inline const char *fsnotify_oldname_init(const char *name)
{
	return NULL;
}

static inline void fsnotify_oldname_free(const char *old_name)
{
}

#endif	/* ! CONFIG_INOTIFY */

#endif	/* _LINUX_FS_NOTIFY_H */