Commit b6ff1070 authored by Linus Torvalds's avatar Linus Torvalds
Browse files
Pull fsnotify updates from Jan Kara:
 "This implements the fanotify FAN_DIR_MODIFY event.

  This event reports the name in a directory under which a change
  happened and together with the directory filehandle and fstatat()
  allows reliable and efficient implementation of directory
  synchronization"

* tag 'fsnotify_for_v5.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs:
  fanotify: Fix the checks in fanotify_fsid_equal
  fanotify: report name info for FAN_DIR_MODIFY event
  fanotify: record name info for FAN_DIR_MODIFY event
  fanotify: Drop fanotify_event_has_fid()
  fanotify: prepare to report both parent and child fid's
  fanotify: send FAN_DIR_MODIFY event flavor with dir inode and name
  fanotify: divorce fanotify_path_event and fanotify_fid_event
  fanotify: Store fanotify handles differently
  fanotify: Simplify create_fd()
  fanotify: fix merging marks masks with FAN_ONDIR
  fanotify: merge duplicate events on parent and child
  fsnotify: replace inode pointer with an object id
  fsnotify: simplify arguments passing to fsnotify_parent()
  fsnotify: use helpers to access data by data_type
  fsnotify: funnel all dirent events through fsnotify_name()
  fsnotify: factor helpers fsnotify_dentry() and fsnotify_file()
  fsnotify: tidy up FS_ and FAN_ constants
parents 74e934ba 6def1a1d
Loading
Loading
Loading
Loading
+228 −74
Original line number Diff line number Diff line
@@ -17,6 +17,59 @@

#include "fanotify.h"

static bool fanotify_path_equal(struct path *p1, struct path *p2)
{
	return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
}

static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
				       __kernel_fsid_t *fsid2)
{
	return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
}

static bool fanotify_fh_equal(struct fanotify_fh *fh1,
			      struct fanotify_fh *fh2)
{
	if (fh1->type != fh2->type || fh1->len != fh2->len)
		return false;

	/* Do not merge events if we failed to encode fh */
	if (fh1->type == FILEID_INVALID)
		return false;

	return !fh1->len ||
		!memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
}

static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
				     struct fanotify_fid_event *ffe2)
{
	/* Do not merge fid events without object fh */
	if (!ffe1->object_fh.len)
		return false;

	return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
		fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
}

static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
				      struct fanotify_name_event *fne2)
{
	/*
	 * Do not merge name events without dir fh.
	 * FAN_DIR_MODIFY does not encode object fh, so it may be empty.
	 */
	if (!fne1->dir_fh.len)
		return false;

	if (fne1->name_len != fne2->name_len ||
	    !fanotify_fh_equal(&fne1->dir_fh, &fne2->dir_fh))
		return false;

	return !memcmp(fne1->name, fne2->name, fne1->name_len);
}

static bool should_merge(struct fsnotify_event *old_fsn,
			 struct fsnotify_event *new_fsn)
{
@@ -26,14 +79,15 @@ static bool should_merge(struct fsnotify_event *old_fsn,
	old = FANOTIFY_E(old_fsn);
	new = FANOTIFY_E(new_fsn);

	if (old_fsn->inode != new_fsn->inode || old->pid != new->pid ||
	    old->fh_type != new->fh_type || old->fh_len != new->fh_len)
	if (old_fsn->objectid != new_fsn->objectid ||
	    old->type != new->type || old->pid != new->pid)
		return false;

	if (fanotify_event_has_path(old)) {
		return old->path.mnt == new->path.mnt &&
			old->path.dentry == new->path.dentry;
	} else if (fanotify_event_has_fid(old)) {
	switch (old->type) {
	case FANOTIFY_EVENT_TYPE_PATH:
		return fanotify_path_equal(fanotify_event_path(old),
					   fanotify_event_path(new));
	case FANOTIFY_EVENT_TYPE_FID:
		/*
		 * We want to merge many dirent events in the same dir (i.e.
		 * creates/unlinks/renames), but we do not want to merge dirent
@@ -42,11 +96,18 @@ static bool should_merge(struct fsnotify_event *old_fsn,
		 * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
		 * unlink pair or rmdir+create pair of events.
		 */
		return (old->mask & FS_ISDIR) == (new->mask & FS_ISDIR) &&
			fanotify_fid_equal(&old->fid, &new->fid, old->fh_len);
		if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
			return false;

		return fanotify_fid_event_equal(FANOTIFY_FE(old),
						FANOTIFY_FE(new));
	case FANOTIFY_EVENT_TYPE_FID_NAME:
		return fanotify_name_event_equal(FANOTIFY_NE(old),
						 FANOTIFY_NE(new));
	default:
		WARN_ON_ONCE(1);
	}

	/* Do not merge events if we failed to encode fid */
	return false;
}

@@ -151,7 +212,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
{
	__u32 marks_mask = 0, marks_ignored_mask = 0;
	__u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS;
	const struct path *path = data;
	const struct path *path = fsnotify_data_path(data, data_type);
	struct fsnotify_mark *mark;
	int type;

@@ -160,7 +221,7 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,

	if (!FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
		/* Do we have path to open a file descriptor? */
		if (data_type != FSNOTIFY_EVENT_PATH)
		if (!path)
			return 0;
		/* Path type events are only relevant for files and dirs */
		if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
@@ -171,6 +232,13 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
		if (!fsnotify_iter_should_report_type(iter_info, type))
			continue;
		mark = iter_info->marks[type];
		/*
		 * If the event is on dir and this mark doesn't care about
		 * events on dir, don't send it!
		 */
		if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
			continue;

		/*
		 * If the event is for a child and this mark doesn't care about
		 * events on a child, don't send it!
@@ -187,9 +255,9 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
	test_mask = event_mask & marks_mask & ~marks_ignored_mask;

	/*
	 * dirent modification events (create/delete/move) do not carry the
	 * child entry name/inode information. Instead, we report FAN_ONDIR
	 * for mkdir/rmdir so user can differentiate them from creat/unlink.
	 * For dirent modification events (create/delete/move) that do not carry
	 * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
	 * so user can differentiate them from creat/unlink.
	 *
	 * For backward compatibility and consistency, do not report FAN_ONDIR
	 * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
@@ -203,22 +271,20 @@ static u32 fanotify_group_event_mask(struct fsnotify_group *group,
		user_mask &= ~FAN_ONDIR;
	}

	if (event_mask & FS_ISDIR &&
	    !(marks_mask & FS_ISDIR & ~marks_ignored_mask))
		return 0;

	return test_mask & user_mask;
}

static int fanotify_encode_fid(struct fanotify_event *event,
			       struct inode *inode, gfp_t gfp,
			       __kernel_fsid_t *fsid)
static void fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
			       gfp_t gfp)
{
	struct fanotify_fid *fid = &event->fid;
	int dwords, bytes = 0;
	int err, type;
	int dwords, type, bytes = 0;
	char *ext_buf = NULL;
	void *buf = fh->buf;
	int err;

	if (!inode)
		goto out;

	fid->ext_fh = NULL;
	dwords = 0;
	err = -ENOENT;
	type = exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
@@ -229,31 +295,33 @@ static int fanotify_encode_fid(struct fanotify_event *event,
	if (bytes > FANOTIFY_INLINE_FH_LEN) {
		/* Treat failure to allocate fh as failure to allocate event */
		err = -ENOMEM;
		fid->ext_fh = kmalloc(bytes, gfp);
		if (!fid->ext_fh)
		ext_buf = kmalloc(bytes, gfp);
		if (!ext_buf)
			goto out_err;

		*fanotify_fh_ext_buf_ptr(fh) = ext_buf;
		buf = ext_buf;
	}

	type = exportfs_encode_inode_fh(inode, fanotify_fid_fh(fid, bytes),
					&dwords, NULL);
	type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
	err = -EINVAL;
	if (!type || type == FILEID_INVALID || bytes != dwords << 2)
		goto out_err;

	fid->fsid = *fsid;
	event->fh_len = bytes;
	fh->type = type;
	fh->len = bytes;

	return type;
	return;

out_err:
	pr_warn_ratelimited("fanotify: failed to encode fid (fsid=%x.%x, "
			    "type=%d, bytes=%d, err=%i)\n",
			    fsid->val[0], fsid->val[1], type, bytes, err);
	kfree(fid->ext_fh);
	fid->ext_fh = NULL;
	event->fh_len = 0;

	return FILEID_INVALID;
	pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
			    type, bytes, err);
	kfree(ext_buf);
	*fanotify_fh_ext_buf_ptr(fh) = NULL;
out:
	/* Report the event without a file identifier on encode error */
	fh->type = FILEID_INVALID;
	fh->len = 0;
}

/*
@@ -269,21 +337,22 @@ static struct inode *fanotify_fid_inode(struct inode *to_tell, u32 event_mask,
{
	if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
		return to_tell;
	else if (data_type == FSNOTIFY_EVENT_INODE)
		return (struct inode *)data;
	else if (data_type == FSNOTIFY_EVENT_PATH)
		return d_inode(((struct path *)data)->dentry);
	return NULL;

	return (struct inode *)fsnotify_data_inode(data, data_type);
}

struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
					    struct inode *inode, u32 mask,
					    const void *data, int data_type,
					    const struct qstr *file_name,
					    __kernel_fsid_t *fsid)
{
	struct fanotify_event *event = NULL;
	struct fanotify_fid_event *ffe = NULL;
	struct fanotify_name_event *fne = NULL;
	gfp_t gfp = GFP_KERNEL_ACCOUNT;
	struct inode *id = fanotify_fid_inode(inode, mask, data, data_type);
	const struct path *path = fsnotify_data_path(data, data_type);

	/*
	 * For queues with unlimited length lost events are not expected and
@@ -305,33 +374,81 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
		pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
		if (!pevent)
			goto out;

		event = &pevent->fae;
		event->type = FANOTIFY_EVENT_TYPE_PATH_PERM;
		pevent->response = 0;
		pevent->state = FAN_EVENT_INIT;
		goto init;
	}
	event = kmem_cache_alloc(fanotify_event_cachep, gfp);
	if (!event)

	/*
	 * For FAN_DIR_MODIFY event, we report the fid of the directory and
	 * the name of the modified entry.
	 * Allocate an fanotify_name_event struct and copy the name.
	 */
	if (mask & FAN_DIR_MODIFY && !(WARN_ON_ONCE(!file_name))) {
		fne = kmalloc(sizeof(*fne) + file_name->len + 1, gfp);
		if (!fne)
			goto out;

		event = &fne->fae;
		event->type = FANOTIFY_EVENT_TYPE_FID_NAME;
		fne->name_len = file_name->len;
		strcpy(fne->name, file_name->name);
		goto init;
	}

	if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
		ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
		if (!ffe)
			goto out;
init: __maybe_unused
	fsnotify_init_event(&event->fse, inode);

		event = &ffe->fae;
		event->type = FANOTIFY_EVENT_TYPE_FID;
	} else {
		struct fanotify_path_event *pevent;

		pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
		if (!pevent)
			goto out;

		event = &pevent->fae;
		event->type = FANOTIFY_EVENT_TYPE_PATH;
	}

init:
	/*
	 * Use the victim inode instead of the watching inode as the id for
	 * event queue, so event reported on parent is merged with event
	 * reported on child when both directory and child watches exist.
	 */
	fsnotify_init_event(&event->fse, (unsigned long)id);
	event->mask = mask;
	if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
		event->pid = get_pid(task_pid(current));
	else
		event->pid = get_pid(task_tgid(current));
	event->fh_len = 0;
	if (id && FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
		/* Report the event without a file identifier on encode error */
		event->fh_type = fanotify_encode_fid(event, id, gfp, fsid);
	} else if (data_type == FSNOTIFY_EVENT_PATH) {
		event->fh_type = FILEID_ROOT;
		event->path = *((struct path *)data);
		path_get(&event->path);

	if (fsid && fanotify_event_fsid(event))
		*fanotify_event_fsid(event) = *fsid;

	if (fanotify_event_object_fh(event))
		fanotify_encode_fh(fanotify_event_object_fh(event), id, gfp);

	if (fanotify_event_dir_fh(event))
		fanotify_encode_fh(fanotify_event_dir_fh(event), id, gfp);

	if (fanotify_event_has_path(event)) {
		struct path *p = fanotify_event_path(event);

		if (path) {
			*p = *path;
			path_get(path);
		} else {
		event->fh_type = FILEID_INVALID;
		event->path.mnt = NULL;
		event->path.dentry = NULL;
			p->mnt = NULL;
			p->dentry = NULL;
		}
	}
out:
	memalloc_unuse_memcg();
@@ -392,6 +509,7 @@ static int fanotify_handle_event(struct fsnotify_group *group,
	BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
	BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
	BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
	BUILD_BUG_ON(FAN_DIR_MODIFY != FS_DIR_MODIFY);
	BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
	BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
	BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
@@ -402,7 +520,7 @@ static int fanotify_handle_event(struct fsnotify_group *group,
	BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
	BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);

	BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19);
	BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 20);

	mask = fanotify_group_event_mask(group, iter_info, mask, data,
					 data_type);
@@ -429,7 +547,7 @@ static int fanotify_handle_event(struct fsnotify_group *group,
	}

	event = fanotify_alloc_event(group, inode, mask, data, data_type,
				     &fsid);
				     file_name, &fsid);
	ret = -ENOMEM;
	if (unlikely(!event)) {
		/*
@@ -451,7 +569,7 @@ static int fanotify_handle_event(struct fsnotify_group *group,

		ret = 0;
	} else if (fanotify_is_perm_event(mask)) {
		ret = fanotify_get_response(group, FANOTIFY_PE(fsn_event),
		ret = fanotify_get_response(group, FANOTIFY_PERM(event),
					    iter_info);
	}
finish:
@@ -470,22 +588,58 @@ static void fanotify_free_group_priv(struct fsnotify_group *group)
	free_uid(user);
}

static void fanotify_free_path_event(struct fanotify_event *event)
{
	path_put(fanotify_event_path(event));
	kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
}

static void fanotify_free_perm_event(struct fanotify_event *event)
{
	path_put(fanotify_event_path(event));
	kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
}

static void fanotify_free_fid_event(struct fanotify_event *event)
{
	struct fanotify_fid_event *ffe = FANOTIFY_FE(event);

	if (fanotify_fh_has_ext_buf(&ffe->object_fh))
		kfree(fanotify_fh_ext_buf(&ffe->object_fh));
	kmem_cache_free(fanotify_fid_event_cachep, ffe);
}

static void fanotify_free_name_event(struct fanotify_event *event)
{
	struct fanotify_name_event *fne = FANOTIFY_NE(event);

	if (fanotify_fh_has_ext_buf(&fne->dir_fh))
		kfree(fanotify_fh_ext_buf(&fne->dir_fh));
	kfree(fne);
}

static void fanotify_free_event(struct fsnotify_event *fsn_event)
{
	struct fanotify_event *event;

	event = FANOTIFY_E(fsn_event);
	if (fanotify_event_has_path(event))
		path_put(&event->path);
	else if (fanotify_event_has_ext_fh(event))
		kfree(event->fid.ext_fh);
	put_pid(event->pid);
	if (fanotify_is_perm_event(event->mask)) {
		kmem_cache_free(fanotify_perm_event_cachep,
				FANOTIFY_PE(fsn_event));
		return;
	switch (event->type) {
	case FANOTIFY_EVENT_TYPE_PATH:
		fanotify_free_path_event(event);
		break;
	case FANOTIFY_EVENT_TYPE_PATH_PERM:
		fanotify_free_perm_event(event);
		break;
	case FANOTIFY_EVENT_TYPE_FID:
		fanotify_free_fid_event(event);
		break;
	case FANOTIFY_EVENT_TYPE_FID_NAME:
		fanotify_free_name_event(event);
		break;
	default:
		WARN_ON_ONCE(1);
	}
	kmem_cache_free(fanotify_event_cachep, event);
}

static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
+127 −62
Original line number Diff line number Diff line
@@ -5,7 +5,8 @@
#include <linux/exportfs.h>

extern struct kmem_cache *fanotify_mark_cache;
extern struct kmem_cache *fanotify_event_cachep;
extern struct kmem_cache *fanotify_fid_event_cachep;
extern struct kmem_cache *fanotify_path_event_cachep;
extern struct kmem_cache *fanotify_perm_event_cachep;

/* Possible states of the permission event */
@@ -18,94 +19,140 @@ enum {

/*
 * 3 dwords are sufficient for most local fs (64bit ino, 32bit generation).
 * For 32bit arch, fid increases the size of fanotify_event by 12 bytes and
 * fh_* fields increase the size of fanotify_event by another 4 bytes.
 * For 64bit arch, fid increases the size of fanotify_fid by 8 bytes and
 * fh_* fields are packed in a hole after mask.
 * fh buf should be dword aligned. On 64bit arch, the ext_buf pointer is
 * stored in either the first or last 2 dwords.
 */
#if BITS_PER_LONG == 32
#define FANOTIFY_INLINE_FH_LEN	(3 << 2)
#else
#define FANOTIFY_INLINE_FH_LEN	(4 << 2)
#endif

struct fanotify_fid {
	__kernel_fsid_t fsid;
	union {
		unsigned char fh[FANOTIFY_INLINE_FH_LEN];
		unsigned char *ext_fh;
	};
};
struct fanotify_fh {
	unsigned char buf[FANOTIFY_INLINE_FH_LEN];
	u8 type;
	u8 len;
} __aligned(4);

static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh)
{
	return fh->len > FANOTIFY_INLINE_FH_LEN;
}

static inline char **fanotify_fh_ext_buf_ptr(struct fanotify_fh *fh)
{
	BUILD_BUG_ON(__alignof__(char *) - 4 + sizeof(char *) >
		     FANOTIFY_INLINE_FH_LEN);
	return (char **)ALIGN((unsigned long)(fh->buf), __alignof__(char *));
}

static inline void *fanotify_fid_fh(struct fanotify_fid *fid,
				    unsigned int fh_len)
static inline void *fanotify_fh_ext_buf(struct fanotify_fh *fh)
{
	return fh_len <= FANOTIFY_INLINE_FH_LEN ? fid->fh : fid->ext_fh;
	return *fanotify_fh_ext_buf_ptr(fh);
}

static inline bool fanotify_fid_equal(struct fanotify_fid *fid1,
				      struct fanotify_fid *fid2,
				      unsigned int fh_len)
static inline void *fanotify_fh_buf(struct fanotify_fh *fh)
{
	return fid1->fsid.val[0] == fid2->fsid.val[0] &&
		fid1->fsid.val[1] == fid2->fsid.val[1] &&
		!memcmp(fanotify_fid_fh(fid1, fh_len),
			fanotify_fid_fh(fid2, fh_len), fh_len);
	return fanotify_fh_has_ext_buf(fh) ? fanotify_fh_ext_buf(fh) : fh->buf;
}

/*
 * Structure for normal fanotify events. It gets allocated in
 * Common structure for fanotify events. Concrete structs are allocated in
 * fanotify_handle_event() and freed when the information is retrieved by
 * userspace
 * userspace. The type of event determines how it was allocated, how it will
 * be freed and which concrete struct it may be cast to.
 */
enum fanotify_event_type {
	FANOTIFY_EVENT_TYPE_FID, /* fixed length */
	FANOTIFY_EVENT_TYPE_FID_NAME, /* variable length */
	FANOTIFY_EVENT_TYPE_PATH,
	FANOTIFY_EVENT_TYPE_PATH_PERM,
};

struct fanotify_event {
	struct fsnotify_event fse;
	u32 mask;
	/*
	 * Those fields are outside fanotify_fid to pack fanotify_event nicely
	 * on 64bit arch and to use fh_type as an indication of whether path
	 * or fid are used in the union:
	 * FILEID_ROOT (0) for path, > 0 for fid, FILEID_INVALID for neither.
	 */
	u8 fh_type;
	u8 fh_len;
	u16 pad;
	union {
		/*
		 * We hold ref to this path so it may be dereferenced at any
		 * point during this object's lifetime
		 */
		struct path path;
		/*
		 * With FAN_REPORT_FID, we do not hold any reference on the
		 * victim object. Instead we store its NFS file handle and its
		 * filesystem's fsid as a unique identifier.
		 */
		struct fanotify_fid fid;
	};
	enum fanotify_event_type type;
	struct pid *pid;
};

static inline bool fanotify_event_has_path(struct fanotify_event *event)
struct fanotify_fid_event {
	struct fanotify_event fae;
	__kernel_fsid_t fsid;
	struct fanotify_fh object_fh;
};

static inline struct fanotify_fid_event *
FANOTIFY_FE(struct fanotify_event *event)
{
	return container_of(event, struct fanotify_fid_event, fae);
}

struct fanotify_name_event {
	struct fanotify_event fae;
	__kernel_fsid_t fsid;
	struct fanotify_fh dir_fh;
	u8 name_len;
	char name[0];
};

static inline struct fanotify_name_event *
FANOTIFY_NE(struct fanotify_event *event)
{
	return container_of(event, struct fanotify_name_event, fae);
}

static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
{
	if (event->type == FANOTIFY_EVENT_TYPE_FID)
		return &FANOTIFY_FE(event)->fsid;
	else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
		return &FANOTIFY_NE(event)->fsid;
	else
		return NULL;
}

static inline struct fanotify_fh *fanotify_event_object_fh(
						struct fanotify_event *event)
{
	if (event->type == FANOTIFY_EVENT_TYPE_FID)
		return &FANOTIFY_FE(event)->object_fh;
	else
		return NULL;
}

static inline struct fanotify_fh *fanotify_event_dir_fh(
						struct fanotify_event *event)
{
	if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
		return &FANOTIFY_NE(event)->dir_fh;
	else
		return NULL;
}

static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
{
	return event->fh_type == FILEID_ROOT;
	struct fanotify_fh *fh = fanotify_event_object_fh(event);

	return fh ? fh->len : 0;
}

static inline bool fanotify_event_has_fid(struct fanotify_event *event)
static inline bool fanotify_event_has_name(struct fanotify_event *event)
{
	return event->fh_type != FILEID_ROOT &&
		event->fh_type != FILEID_INVALID;
	return event->type == FANOTIFY_EVENT_TYPE_FID_NAME;
}

static inline bool fanotify_event_has_ext_fh(struct fanotify_event *event)
static inline int fanotify_event_name_len(struct fanotify_event *event)
{
	return fanotify_event_has_fid(event) &&
		event->fh_len > FANOTIFY_INLINE_FH_LEN;
	return fanotify_event_has_name(event) ?
		FANOTIFY_NE(event)->name_len : 0;
}

static inline void *fanotify_event_fh(struct fanotify_event *event)
struct fanotify_path_event {
	struct fanotify_event fae;
	struct path path;
};

static inline struct fanotify_path_event *
FANOTIFY_PE(struct fanotify_event *event)
{
	return fanotify_fid_fh(&event->fid, event->fh_len);
	return container_of(event, struct fanotify_path_event, fae);
}

/*
@@ -117,15 +164,16 @@ static inline void *fanotify_event_fh(struct fanotify_event *event)
 */
struct fanotify_perm_event {
	struct fanotify_event fae;
	struct path path;
	unsigned short response;	/* userspace answer to the event */
	unsigned short state;		/* state of the event */
	int fd;		/* fd we passed to userspace for this event */
};

static inline struct fanotify_perm_event *
FANOTIFY_PE(struct fsnotify_event *fse)
FANOTIFY_PERM(struct fanotify_event *event)
{
	return container_of(fse, struct fanotify_perm_event, fae.fse);
	return container_of(event, struct fanotify_perm_event, fae);
}

static inline bool fanotify_is_perm_event(u32 mask)
@@ -139,7 +187,24 @@ static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse)
	return container_of(fse, struct fanotify_event, fse);
}

static inline bool fanotify_event_has_path(struct fanotify_event *event)
{
	return event->type == FANOTIFY_EVENT_TYPE_PATH ||
		event->type == FANOTIFY_EVENT_TYPE_PATH_PERM;
}

static inline struct path *fanotify_event_path(struct fanotify_event *event)
{
	if (event->type == FANOTIFY_EVENT_TYPE_PATH)
		return &FANOTIFY_PE(event)->path;
	else if (event->type == FANOTIFY_EVENT_TYPE_PATH_PERM)
		return &FANOTIFY_PERM(event)->path;
	else
		return NULL;
}

struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
					    struct inode *inode, u32 mask,
					    const void *data, int data_type,
					    const struct qstr *file_name,
					    __kernel_fsid_t *fsid);
+145 −75

File changed.

Preview size limit exceeded, changes collapsed.

+8 −14
Original line number Diff line number Diff line
@@ -143,15 +143,13 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode)
}

/* Notify this dentry's parent about a child's events. */
int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask)
int fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
		    int data_type)
{
	struct dentry *parent;
	struct inode *p_inode;
	int ret = 0;

	if (!dentry)
		dentry = path->dentry;

	if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
		return 0;

@@ -168,12 +166,7 @@ int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask
		mask |= FS_EVENT_ON_CHILD;

		take_dentry_name_snapshot(&name, dentry);
		if (path)
			ret = fsnotify(p_inode, mask, path, FSNOTIFY_EVENT_PATH,
				       &name.name, 0);
		else
			ret = fsnotify(p_inode, mask, dentry->d_inode, FSNOTIFY_EVENT_INODE,
				       &name.name, 0);
		ret = fsnotify(p_inode, mask, data, data_type, &name.name, 0);
		release_dentry_name_snapshot(&name);
	}

@@ -181,7 +174,7 @@ int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask

	return ret;
}
EXPORT_SYMBOL_GPL(__fsnotify_parent);
EXPORT_SYMBOL_GPL(fsnotify_parent);

static int send_to_group(struct inode *to_tell,
			 __u32 mask, const void *data,
@@ -318,6 +311,7 @@ static void fsnotify_iter_next(struct fsnotify_iter_info *iter_info)
int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_is,
	     const struct qstr *file_name, u32 cookie)
{
	const struct path *path = fsnotify_data_path(data, data_is);
	struct fsnotify_iter_info iter_info = {};
	struct super_block *sb = to_tell->i_sb;
	struct mount *mnt = NULL;
@@ -325,8 +319,8 @@ int fsnotify(struct inode *to_tell, __u32 mask, const void *data, int data_is,
	int ret = 0;
	__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);

	if (data_is == FSNOTIFY_EVENT_PATH) {
		mnt = real_mount(((const struct path *)data)->mnt);
	if (path) {
		mnt = real_mount(path->mnt);
		mnt_or_sb_mask |= mnt->mnt_fsnotify_mask;
	}
	/* An event "on child" is not intended for a mount/sb mark */
@@ -389,7 +383,7 @@ static __init int fsnotify_init(void)
{
	int ret;

	BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 25);
	BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 26);

	ret = init_srcu_struct(&fsnotify_mark_srcu);
	if (ret)
+5 −7
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ static bool event_compare(struct fsnotify_event *old_fsn,
	if (old->mask & FS_IN_IGNORED)
		return false;
	if ((old->mask == new->mask) &&
	    (old_fsn->inode == new_fsn->inode) &&
	    (old_fsn->objectid == new_fsn->objectid) &&
	    (old->name_len == new->name_len) &&
	    (!old->name_len || !strcmp(old->name, new->name)))
		return true;
@@ -61,6 +61,7 @@ int inotify_handle_event(struct fsnotify_group *group,
			 const struct qstr *file_name, u32 cookie,
			 struct fsnotify_iter_info *iter_info)
{
	const struct path *path = fsnotify_data_path(data, data_type);
	struct fsnotify_mark *inode_mark = fsnotify_iter_inode_mark(iter_info);
	struct inotify_inode_mark *i_mark;
	struct inotify_event_info *event;
@@ -73,12 +74,9 @@ int inotify_handle_event(struct fsnotify_group *group,
		return 0;

	if ((inode_mark->mask & FS_EXCL_UNLINK) &&
	    (data_type == FSNOTIFY_EVENT_PATH)) {
		const struct path *path = data;

		if (d_unlinked(path->dentry))
	    path && d_unlinked(path->dentry))
		return 0;
	}

	if (file_name) {
		len = file_name->len;
		alloc_len += len + 1;
@@ -118,7 +116,7 @@ int inotify_handle_event(struct fsnotify_group *group,
		mask &= ~IN_ISDIR;

	fsn_event = &event->fse;
	fsnotify_init_event(fsn_event, inode);
	fsnotify_init_event(fsn_event, (unsigned long)inode);
	event->mask = mask;
	event->wd = i_mark->wd;
	event->sync_cookie = cookie;
Loading