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

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

20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Notify this @dir inode about a change in the directory entry @dentry.
 *
 * Unlike fsnotify_parent(), the event will be reported regardless of the
 * FS_EVENT_ON_CHILD mask on the parent inode.
 */
static inline int fsnotify_dirent(struct inode *dir, struct dentry *dentry,
				  __u32 mask)
{
	return fsnotify(dir, mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
			dentry->d_name.name, 0);
}

E
Eric Paris 已提交
33
/* Notify this dentry's parent about a child's events. */
34 35
static inline int fsnotify_parent(const struct path *path,
				  struct dentry *dentry, __u32 mask)
E
Eric Paris 已提交
36
{
37
	if (!dentry)
38
		dentry = path->dentry;
39

40
	return __fsnotify_parent(path, dentry, mask);
E
Eric Paris 已提交
41 42
}

43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 * Simple wrapper to consolidate calls fsnotify_parent()/fsnotify() when
 * an event is on a path.
 */
static inline int fsnotify_path(struct inode *inode, const struct path *path,
				__u32 mask)
{
	int ret = fsnotify_parent(path, NULL, mask);

	if (ret)
		return ret;
	return fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
}

57
/* Simple call site for access decisions */
58 59
static inline int fsnotify_perm(struct file *file, int mask)
{
60
	int ret;
61
	const struct path *path = &file->f_path;
62
	struct inode *inode = file_inode(file);
63
	__u32 fsnotify_mask = 0;
64 65 66 67 68

	if (file->f_mode & FMODE_NONOTIFY)
		return 0;
	if (!(mask & (MAY_READ | MAY_OPEN)))
		return 0;
69
	if (mask & MAY_OPEN) {
70
		fsnotify_mask = FS_OPEN_PERM;
71 72 73 74 75 76 77 78

		if (file->f_flags & __FMODE_EXEC) {
			ret = fsnotify_path(inode, path, FS_OPEN_EXEC_PERM);

			if (ret)
				return ret;
		}
	} else if (mask & MAY_READ) {
79
		fsnotify_mask = FS_ACCESS_PERM;
80
	}
81

82 83 84
	if (S_ISDIR(inode->i_mode))
		fsnotify_mask |= FS_ISDIR;

85
	return fsnotify_path(inode, path, fsnotify_mask);
86 87
}

88 89 90 91 92
/*
 * fsnotify_link_count - inode's link count changed
 */
static inline void fsnotify_link_count(struct inode *inode)
{
93 94 95 96 97 98
	__u32 mask = FS_ATTRIB;

	if (S_ISDIR(inode->i_mode))
		mask |= FS_ISDIR;

	fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
99 100
}

R
Robert Love 已提交
101 102 103 104
/*
 * 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,
105
				 const struct qstr *old_name,
106 107
				 int isdir, struct inode *target,
				 struct dentry *moved)
R
Robert Love 已提交
108
{
109
	struct inode *source = moved->d_inode;
110
	u32 fs_cookie = fsnotify_get_cookie();
111 112
	__u32 old_dir_mask = FS_MOVED_FROM;
	__u32 new_dir_mask = FS_MOVED_TO;
113
	__u32 mask = FS_MOVE_SELF;
114
	const unsigned char *new_name = moved->d_name.name;
R
Robert Love 已提交
115

116 117
	if (old_dir == new_dir)
		old_dir_mask |= FS_DN_RENAME;
R
Robert Love 已提交
118

119
	if (isdir) {
120 121
		old_dir_mask |= FS_ISDIR;
		new_dir_mask |= FS_ISDIR;
122
		mask |= FS_ISDIR;
123 124
	}

125
	fsnotify(old_dir, old_dir_mask, source, FSNOTIFY_EVENT_INODE, old_name->name,
126 127 128
		 fs_cookie);
	fsnotify(new_dir, new_dir_mask, source, FSNOTIFY_EVENT_INODE, new_name,
		 fs_cookie);
129

130
	if (target)
131
		fsnotify_link_count(target);
132

133
	if (source)
134
		fsnotify(source, mask, source, FSNOTIFY_EVENT_INODE, NULL, 0);
135
	audit_inode_child(new_dir, moved, AUDIT_TYPE_CHILD_CREATE);
R
Robert Love 已提交
136 137
}

138 139 140 141 142 143 144 145
/*
 * 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);
}

146 147 148 149 150 151 152 153
/*
 * 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);
}

154 155
/*
 * fsnotify_nameremove - a filename was removed from a directory
156 157 158 159 160 161 162
 *
 * This is mostly called under parent vfs inode lock so name and
 * dentry->d_parent should be stable. However there are some corner cases where
 * inode lock is not held. So to be on the safe side and be reselient to future
 * callers and out of tree users of d_delete(), we do not assume that d_parent
 * and d_name are stable and we use dget_parent() and
 * take_dentry_name_snapshot() to grab stable references.
163 164 165
 */
static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
{
166 167
	struct dentry *parent;
	struct name_snapshot name;
168 169
	__u32 mask = FS_DELETE;

170 171 172 173
	/* d_delete() of pseudo inode? (e.g. __ns_get_path() playing tricks) */
	if (IS_ROOT(dentry))
		return;

174
	if (isdir)
175
		mask |= FS_ISDIR;
E
Eric Paris 已提交
176

177 178 179 180
	parent = dget_parent(dentry);
	take_dentry_name_snapshot(&name, dentry);

	fsnotify(d_inode(parent), mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
181
		 name.name.name, 0);
182 183 184

	release_dentry_name_snapshot(&name);
	dput(parent);
185 186 187 188 189 190 191
}

/*
 * fsnotify_inoderemove - an inode is going away
 */
static inline void fsnotify_inoderemove(struct inode *inode)
{
192 193 194 195 196 197
	__u32 mask = FS_DELETE_SELF;

	if (S_ISDIR(inode->i_mode))
		mask |= FS_ISDIR;

	fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
198
	__fsnotify_inode_delete(inode);
199 200
}

R
Robert Love 已提交
201 202 203
/*
 * fsnotify_create - 'name' was linked in
 */
204
static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
R
Robert Love 已提交
205
{
206
	audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
207

208
	fsnotify_dirent(inode, dentry, FS_CREATE);
R
Robert Love 已提交
209 210
}

211 212 213 214 215 216 217 218
/*
 * 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)
{
	fsnotify_link_count(inode);
219
	audit_inode_child(dir, new_dentry, AUDIT_TYPE_CHILD_CREATE);
220

221
	fsnotify(dir, FS_CREATE, inode, FSNOTIFY_EVENT_INODE, new_dentry->d_name.name, 0);
222 223
}

R
Robert Love 已提交
224 225 226
/*
 * fsnotify_mkdir - directory 'name' was created
 */
227
static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
R
Robert Love 已提交
228
{
229
	audit_inode_child(inode, dentry, AUDIT_TYPE_CHILD_CREATE);
230

231
	fsnotify_dirent(inode, dentry, FS_CREATE | FS_ISDIR);
R
Robert Love 已提交
232 233 234 235 236
}

/*
 * fsnotify_access - file was read
 */
237
static inline void fsnotify_access(struct file *file)
R
Robert Love 已提交
238
{
239
	const struct path *path = &file->f_path;
240
	struct inode *inode = file_inode(file);
241
	__u32 mask = FS_ACCESS;
R
Robert Love 已提交
242 243

	if (S_ISDIR(inode->i_mode))
244
		mask |= FS_ISDIR;
R
Robert Love 已提交
245

246 247
	if (!(file->f_mode & FMODE_NONOTIFY))
		fsnotify_path(inode, path, mask);
R
Robert Love 已提交
248 249 250 251 252
}

/*
 * fsnotify_modify - file was modified
 */
253
static inline void fsnotify_modify(struct file *file)
R
Robert Love 已提交
254
{
255
	const struct path *path = &file->f_path;
256
	struct inode *inode = file_inode(file);
257
	__u32 mask = FS_MODIFY;
R
Robert Love 已提交
258 259

	if (S_ISDIR(inode->i_mode))
260
		mask |= FS_ISDIR;
R
Robert Love 已提交
261

262 263
	if (!(file->f_mode & FMODE_NONOTIFY))
		fsnotify_path(inode, path, mask);
R
Robert Love 已提交
264 265 266 267 268
}

/*
 * fsnotify_open - file was opened
 */
269
static inline void fsnotify_open(struct file *file)
R
Robert Love 已提交
270
{
271
	const struct path *path = &file->f_path;
272
	struct inode *inode = file_inode(file);
273
	__u32 mask = FS_OPEN;
R
Robert Love 已提交
274 275

	if (S_ISDIR(inode->i_mode))
276
		mask |= FS_ISDIR;
277 278
	if (file->f_flags & __FMODE_EXEC)
		mask |= FS_OPEN_EXEC;
R
Robert Love 已提交
279

280
	fsnotify_path(inode, path, mask);
R
Robert Love 已提交
281 282 283 284 285 286 287
}

/*
 * fsnotify_close - file was closed
 */
static inline void fsnotify_close(struct file *file)
{
288
	const struct path *path = &file->f_path;
289
	struct inode *inode = file_inode(file);
290
	fmode_t mode = file->f_mode;
291
	__u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
R
Robert Love 已提交
292 293

	if (S_ISDIR(inode->i_mode))
294
		mask |= FS_ISDIR;
R
Robert Love 已提交
295

296 297
	if (!(file->f_mode & FMODE_NONOTIFY))
		fsnotify_path(inode, path, mask);
R
Robert Love 已提交
298 299 300 301 302 303 304 305
}

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

	if (S_ISDIR(inode->i_mode))
309
		mask |= FS_ISDIR;
R
Robert Love 已提交
310

311
	fsnotify_parent(NULL, dentry, mask);
312
	fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
R
Robert Love 已提交
313 314 315 316 317 318 319 320 321
}

/*
 * 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;
322 323 324 325 326 327 328 329
	__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 已提交
330 331 332

	/* both times implies a utime(s) call */
	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
333 334 335 336 337 338 339 340
		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 已提交
341

342
	if (mask) {
R
Robert Love 已提交
343
		if (S_ISDIR(inode->i_mode))
344
			mask |= FS_ISDIR;
345 346

		fsnotify_parent(NULL, dentry, mask);
347
		fsnotify(inode, mask, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
R
Robert Love 已提交
348 349 350 351
	}
}

#endif	/* _LINUX_FS_NOTIFY_H */