xref: /openbmc/linux/fs/notify/fanotify/fanotify.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
233d3dfffSAndreas Gruenbacher #include <linux/fanotify.h>
3ff0b16a9SEric Paris #include <linux/fdtable.h>
4ff0b16a9SEric Paris #include <linux/fsnotify_backend.h>
5ff0b16a9SEric Paris #include <linux/init.h>
69e66e423SEric Paris #include <linux/jiffies.h>
7ff0b16a9SEric Paris #include <linux/kernel.h> /* UINT_MAX */
81c529063SEric Paris #include <linux/mount.h>
99e66e423SEric Paris #include <linux/sched.h>
105b825c3aSIngo Molnar #include <linux/sched/user.h>
117a36094dSEric W. Biederman #include <linux/sched/signal.h>
12ff0b16a9SEric Paris #include <linux/types.h>
139e66e423SEric Paris #include <linux/wait.h>
14de8cd83eSSteve Grubb #include <linux/audit.h>
15d46eb14bSShakeel Butt #include <linux/sched/mm.h>
16e9e0c890SAmir Goldstein #include <linux/statfs.h>
177e3e5c69SAmir Goldstein #include <linux/stringhash.h>
18ff0b16a9SEric Paris 
197053aee2SJan Kara #include "fanotify.h"
20767cd46cSEric Paris 
fanotify_path_equal(const struct path * p1,const struct path * p2)21d5bf8889SAl Viro static bool fanotify_path_equal(const struct path *p1, const struct path *p2)
22afc894c7SJan Kara {
23afc894c7SJan Kara 	return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
24afc894c7SJan Kara }
25afc894c7SJan Kara 
fanotify_hash_path(const struct path * path)267e3e5c69SAmir Goldstein static unsigned int fanotify_hash_path(const struct path *path)
277e3e5c69SAmir Goldstein {
287e3e5c69SAmir Goldstein 	return hash_ptr(path->dentry, FANOTIFY_EVENT_HASH_BITS) ^
297e3e5c69SAmir Goldstein 		hash_ptr(path->mnt, FANOTIFY_EVENT_HASH_BITS);
307e3e5c69SAmir Goldstein }
317e3e5c69SAmir Goldstein 
fanotify_fsid_equal(__kernel_fsid_t * fsid1,__kernel_fsid_t * fsid2)32afc894c7SJan Kara static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
33afc894c7SJan Kara 				       __kernel_fsid_t *fsid2)
34afc894c7SJan Kara {
356def1a1dSNathan Chancellor 	return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
36afc894c7SJan Kara }
37afc894c7SJan Kara 
fanotify_hash_fsid(__kernel_fsid_t * fsid)387e3e5c69SAmir Goldstein static unsigned int fanotify_hash_fsid(__kernel_fsid_t *fsid)
397e3e5c69SAmir Goldstein {
407e3e5c69SAmir Goldstein 	return hash_32(fsid->val[0], FANOTIFY_EVENT_HASH_BITS) ^
417e3e5c69SAmir Goldstein 		hash_32(fsid->val[1], FANOTIFY_EVENT_HASH_BITS);
427e3e5c69SAmir Goldstein }
437e3e5c69SAmir Goldstein 
fanotify_fh_equal(struct fanotify_fh * fh1,struct fanotify_fh * fh2)44afc894c7SJan Kara static bool fanotify_fh_equal(struct fanotify_fh *fh1,
45afc894c7SJan Kara 			      struct fanotify_fh *fh2)
46afc894c7SJan Kara {
47afc894c7SJan Kara 	if (fh1->type != fh2->type || fh1->len != fh2->len)
48afc894c7SJan Kara 		return false;
49afc894c7SJan Kara 
50afc894c7SJan Kara 	return !fh1->len ||
51afc894c7SJan Kara 		!memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
52afc894c7SJan Kara }
53afc894c7SJan Kara 
fanotify_hash_fh(struct fanotify_fh * fh)547e3e5c69SAmir Goldstein static unsigned int fanotify_hash_fh(struct fanotify_fh *fh)
557e3e5c69SAmir Goldstein {
567e3e5c69SAmir Goldstein 	long salt = (long)fh->type | (long)fh->len << 8;
577e3e5c69SAmir Goldstein 
587e3e5c69SAmir Goldstein 	/*
597e3e5c69SAmir Goldstein 	 * full_name_hash() works long by long, so it handles fh buf optimally.
607e3e5c69SAmir Goldstein 	 */
617e3e5c69SAmir Goldstein 	return full_name_hash((void *)salt, fanotify_fh_buf(fh), fh->len);
627e3e5c69SAmir Goldstein }
637e3e5c69SAmir Goldstein 
fanotify_fid_event_equal(struct fanotify_fid_event * ffe1,struct fanotify_fid_event * ffe2)647088f357SJan Kara static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
657088f357SJan Kara 				     struct fanotify_fid_event *ffe2)
667088f357SJan Kara {
677088f357SJan Kara 	/* Do not merge fid events without object fh */
687088f357SJan Kara 	if (!ffe1->object_fh.len)
697088f357SJan Kara 		return false;
707088f357SJan Kara 
717088f357SJan Kara 	return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
727088f357SJan Kara 		fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
737088f357SJan Kara }
747088f357SJan Kara 
fanotify_info_equal(struct fanotify_info * info1,struct fanotify_info * info2)75f454fa61SAmir Goldstein static bool fanotify_info_equal(struct fanotify_info *info1,
76f454fa61SAmir Goldstein 				struct fanotify_info *info2)
77f454fa61SAmir Goldstein {
78f454fa61SAmir Goldstein 	if (info1->dir_fh_totlen != info2->dir_fh_totlen ||
793cf984e9SAmir Goldstein 	    info1->dir2_fh_totlen != info2->dir2_fh_totlen ||
80f454fa61SAmir Goldstein 	    info1->file_fh_totlen != info2->file_fh_totlen ||
813cf984e9SAmir Goldstein 	    info1->name_len != info2->name_len ||
823cf984e9SAmir Goldstein 	    info1->name2_len != info2->name2_len)
83f454fa61SAmir Goldstein 		return false;
84f454fa61SAmir Goldstein 
85f454fa61SAmir Goldstein 	if (info1->dir_fh_totlen &&
86f454fa61SAmir Goldstein 	    !fanotify_fh_equal(fanotify_info_dir_fh(info1),
87f454fa61SAmir Goldstein 			       fanotify_info_dir_fh(info2)))
88f454fa61SAmir Goldstein 		return false;
89f454fa61SAmir Goldstein 
903cf984e9SAmir Goldstein 	if (info1->dir2_fh_totlen &&
913cf984e9SAmir Goldstein 	    !fanotify_fh_equal(fanotify_info_dir2_fh(info1),
923cf984e9SAmir Goldstein 			       fanotify_info_dir2_fh(info2)))
933cf984e9SAmir Goldstein 		return false;
943cf984e9SAmir Goldstein 
95f454fa61SAmir Goldstein 	if (info1->file_fh_totlen &&
96f454fa61SAmir Goldstein 	    !fanotify_fh_equal(fanotify_info_file_fh(info1),
97f454fa61SAmir Goldstein 			       fanotify_info_file_fh(info2)))
98f454fa61SAmir Goldstein 		return false;
99f454fa61SAmir Goldstein 
1003cf984e9SAmir Goldstein 	if (info1->name_len &&
1013cf984e9SAmir Goldstein 	    memcmp(fanotify_info_name(info1), fanotify_info_name(info2),
1023cf984e9SAmir Goldstein 		   info1->name_len))
1033cf984e9SAmir Goldstein 		return false;
1043cf984e9SAmir Goldstein 
1053cf984e9SAmir Goldstein 	return !info1->name2_len ||
1063cf984e9SAmir Goldstein 		!memcmp(fanotify_info_name2(info1), fanotify_info_name2(info2),
1073cf984e9SAmir Goldstein 			info1->name2_len);
108f454fa61SAmir Goldstein }
109f454fa61SAmir Goldstein 
fanotify_name_event_equal(struct fanotify_name_event * fne1,struct fanotify_name_event * fne2)110cacfb956SAmir Goldstein static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
111cacfb956SAmir Goldstein 				      struct fanotify_name_event *fne2)
112cacfb956SAmir Goldstein {
113f454fa61SAmir Goldstein 	struct fanotify_info *info1 = &fne1->info;
114f454fa61SAmir Goldstein 	struct fanotify_info *info2 = &fne2->info;
115f454fa61SAmir Goldstein 
1166ad1aaddSAmir Goldstein 	/* Do not merge name events without dir fh */
117f454fa61SAmir Goldstein 	if (!info1->dir_fh_totlen)
118cacfb956SAmir Goldstein 		return false;
119cacfb956SAmir Goldstein 
1208aed8cebSJan Kara 	if (!fanotify_fsid_equal(&fne1->fsid, &fne2->fsid))
1218aed8cebSJan Kara 		return false;
1228aed8cebSJan Kara 
123f454fa61SAmir Goldstein 	return fanotify_info_equal(info1, info2);
124cacfb956SAmir Goldstein }
125cacfb956SAmir Goldstein 
fanotify_error_event_equal(struct fanotify_error_event * fee1,struct fanotify_error_event * fee2)1268a6ae641SGabriel Krisman Bertazi static bool fanotify_error_event_equal(struct fanotify_error_event *fee1,
1278a6ae641SGabriel Krisman Bertazi 				       struct fanotify_error_event *fee2)
1288a6ae641SGabriel Krisman Bertazi {
1298a6ae641SGabriel Krisman Bertazi 	/* Error events against the same file system are always merged. */
1308a6ae641SGabriel Krisman Bertazi 	if (!fanotify_fsid_equal(&fee1->fsid, &fee2->fsid))
1318a6ae641SGabriel Krisman Bertazi 		return false;
1328a6ae641SGabriel Krisman Bertazi 
1338a6ae641SGabriel Krisman Bertazi 	return true;
1348a6ae641SGabriel Krisman Bertazi }
1358a6ae641SGabriel Krisman Bertazi 
fanotify_should_merge(struct fanotify_event * old,struct fanotify_event * new)1368988f11aSAmir Goldstein static bool fanotify_should_merge(struct fanotify_event *old,
1378988f11aSAmir Goldstein 				  struct fanotify_event *new)
1387053aee2SJan Kara {
1398988f11aSAmir Goldstein 	pr_debug("%s: old=%p new=%p\n", __func__, old, new);
1407053aee2SJan Kara 
1418988f11aSAmir Goldstein 	if (old->hash != new->hash ||
1427088f357SJan Kara 	    old->type != new->type || old->pid != new->pid)
143e9e0c890SAmir Goldstein 		return false;
144e9e0c890SAmir Goldstein 
145e7fce6d9SAmir Goldstein 	/*
146e7fce6d9SAmir Goldstein 	 * We want to merge many dirent events in the same dir (i.e.
147e7fce6d9SAmir Goldstein 	 * creates/unlinks/renames), but we do not want to merge dirent
148e7fce6d9SAmir Goldstein 	 * events referring to subdirs with dirent events referring to
149e7fce6d9SAmir Goldstein 	 * non subdirs, otherwise, user won't be able to tell from a
150e7fce6d9SAmir Goldstein 	 * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
151e7fce6d9SAmir Goldstein 	 * unlink pair or rmdir+create pair of events.
152e7fce6d9SAmir Goldstein 	 */
153afc894c7SJan Kara 	if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
154afc894c7SJan Kara 		return false;
155afc894c7SJan Kara 
1567326e382SAmir Goldstein 	/*
1577326e382SAmir Goldstein 	 * FAN_RENAME event is reported with special info record types,
1587326e382SAmir Goldstein 	 * so we cannot merge it with other events.
1597326e382SAmir Goldstein 	 */
1607326e382SAmir Goldstein 	if ((old->mask & FAN_RENAME) != (new->mask & FAN_RENAME))
1617326e382SAmir Goldstein 		return false;
1627326e382SAmir Goldstein 
163103ff6a5SAmir Goldstein 	switch (old->type) {
164103ff6a5SAmir Goldstein 	case FANOTIFY_EVENT_TYPE_PATH:
165103ff6a5SAmir Goldstein 		return fanotify_path_equal(fanotify_event_path(old),
166103ff6a5SAmir Goldstein 					   fanotify_event_path(new));
167103ff6a5SAmir Goldstein 	case FANOTIFY_EVENT_TYPE_FID:
1687088f357SJan Kara 		return fanotify_fid_event_equal(FANOTIFY_FE(old),
1697088f357SJan Kara 						FANOTIFY_FE(new));
170cacfb956SAmir Goldstein 	case FANOTIFY_EVENT_TYPE_FID_NAME:
171cacfb956SAmir Goldstein 		return fanotify_name_event_equal(FANOTIFY_NE(old),
172cacfb956SAmir Goldstein 						 FANOTIFY_NE(new));
1738a6ae641SGabriel Krisman Bertazi 	case FANOTIFY_EVENT_TYPE_FS_ERROR:
1748a6ae641SGabriel Krisman Bertazi 		return fanotify_error_event_equal(FANOTIFY_EE(old),
1758a6ae641SGabriel Krisman Bertazi 						  FANOTIFY_EE(new));
1767088f357SJan Kara 	default:
1777088f357SJan Kara 		WARN_ON_ONCE(1);
178e9e0c890SAmir Goldstein 	}
179e9e0c890SAmir Goldstein 
180767cd46cSEric Paris 	return false;
181767cd46cSEric Paris }
182767cd46cSEric Paris 
183b8cd0ee8SAmir Goldstein /* Limit event merges to limit CPU overhead per event */
184b8cd0ee8SAmir Goldstein #define FANOTIFY_MAX_MERGE_EVENTS 128
185b8cd0ee8SAmir Goldstein 
186f70ab54cSEric Paris /* and the list better be locked by something too! */
fanotify_merge(struct fsnotify_group * group,struct fsnotify_event * event)18794e00d28SAmir Goldstein static int fanotify_merge(struct fsnotify_group *group,
18894e00d28SAmir Goldstein 			  struct fsnotify_event *event)
189767cd46cSEric Paris {
1908988f11aSAmir Goldstein 	struct fanotify_event *old, *new = FANOTIFY_E(event);
19194e00d28SAmir Goldstein 	unsigned int bucket = fanotify_event_hash_bucket(group, new);
19294e00d28SAmir Goldstein 	struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
193b8cd0ee8SAmir Goldstein 	int i = 0;
194767cd46cSEric Paris 
19594e00d28SAmir Goldstein 	pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
19694e00d28SAmir Goldstein 		 group, event, bucket);
197767cd46cSEric Paris 
19813116dfdSJan Kara 	/*
19913116dfdSJan Kara 	 * Don't merge a permission event with any other event so that we know
20013116dfdSJan Kara 	 * the event structure we have created in fanotify_handle_event() is the
20113116dfdSJan Kara 	 * one we should check for permission response.
20213116dfdSJan Kara 	 */
203a0a92d26SAmir Goldstein 	if (fanotify_is_perm_event(new->mask))
20483c0e1b4SJan Kara 		return 0;
20513116dfdSJan Kara 
20694e00d28SAmir Goldstein 	hlist_for_each_entry(old, hlist, merge_list) {
207b8cd0ee8SAmir Goldstein 		if (++i > FANOTIFY_MAX_MERGE_EVENTS)
208b8cd0ee8SAmir Goldstein 			break;
2098988f11aSAmir Goldstein 		if (fanotify_should_merge(old, new)) {
2108988f11aSAmir Goldstein 			old->mask |= new->mask;
2118a6ae641SGabriel Krisman Bertazi 
2128a6ae641SGabriel Krisman Bertazi 			if (fanotify_is_error_event(old->mask))
2138a6ae641SGabriel Krisman Bertazi 				FANOTIFY_EE(old)->err_count++;
2148a6ae641SGabriel Krisman Bertazi 
21583c0e1b4SJan Kara 			return 1;
2169dced01aSEric Paris 		}
2176c71100dSKinglong Mee 	}
2186c71100dSKinglong Mee 
2196c71100dSKinglong Mee 	return 0;
2206c71100dSKinglong Mee }
2219dced01aSEric Paris 
222fabf7f29SJan Kara /*
223fabf7f29SJan Kara  * Wait for response to permission event. The function also takes care of
224fabf7f29SJan Kara  * freeing the permission event (or offloads that in case the wait is canceled
225fabf7f29SJan Kara  * by a signal). The function returns 0 in case access got allowed by userspace,
226fabf7f29SJan Kara  * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
227fabf7f29SJan Kara  * the wait got interrupted by a signal.
228fabf7f29SJan Kara  */
fanotify_get_response(struct fsnotify_group * group,struct fanotify_perm_event * event,struct fsnotify_iter_info * iter_info)229f083441bSJan Kara static int fanotify_get_response(struct fsnotify_group *group,
23033913997SAmir Goldstein 				 struct fanotify_perm_event *event,
23105f0e387SJan Kara 				 struct fsnotify_iter_info *iter_info)
2329e66e423SEric Paris {
2339e66e423SEric Paris 	int ret;
2349e66e423SEric Paris 
2359e66e423SEric Paris 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
2369e66e423SEric Paris 
237b5190579SJan Kara 	ret = wait_event_killable(group->fanotify_data.access_waitq,
23840873284SJan Kara 				  event->state == FAN_EVENT_ANSWERED);
239fabf7f29SJan Kara 	/* Signal pending? */
240fabf7f29SJan Kara 	if (ret < 0) {
241fabf7f29SJan Kara 		spin_lock(&group->notification_lock);
242fabf7f29SJan Kara 		/* Event reported to userspace and no answer yet? */
243fabf7f29SJan Kara 		if (event->state == FAN_EVENT_REPORTED) {
244fabf7f29SJan Kara 			/* Event will get freed once userspace answers to it */
245fabf7f29SJan Kara 			event->state = FAN_EVENT_CANCELED;
246fabf7f29SJan Kara 			spin_unlock(&group->notification_lock);
247fabf7f29SJan Kara 			return ret;
248fabf7f29SJan Kara 		}
249fabf7f29SJan Kara 		/* Event not yet reported? Just remove it. */
25094e00d28SAmir Goldstein 		if (event->state == FAN_EVENT_INIT) {
251fabf7f29SJan Kara 			fsnotify_remove_queued_event(group, &event->fae.fse);
25294e00d28SAmir Goldstein 			/* Permission events are not supposed to be hashed */
25394e00d28SAmir Goldstein 			WARN_ON_ONCE(!hlist_unhashed(&event->fae.merge_list));
25494e00d28SAmir Goldstein 		}
255fabf7f29SJan Kara 		/*
256fabf7f29SJan Kara 		 * Event may be also answered in case signal delivery raced
257fabf7f29SJan Kara 		 * with wakeup. In that case we have nothing to do besides
258fabf7f29SJan Kara 		 * freeing the event and reporting error.
259fabf7f29SJan Kara 		 */
260fabf7f29SJan Kara 		spin_unlock(&group->notification_lock);
261fabf7f29SJan Kara 		goto out;
262fabf7f29SJan Kara 	}
2639e66e423SEric Paris 
2649e66e423SEric Paris 	/* userspace responded, convert to something usable */
26570529a19SRichard Guy Briggs 	switch (event->response & FANOTIFY_RESPONSE_ACCESS) {
2669e66e423SEric Paris 	case FAN_ALLOW:
2679e66e423SEric Paris 		ret = 0;
2689e66e423SEric Paris 		break;
2699e66e423SEric Paris 	case FAN_DENY:
2709e66e423SEric Paris 	default:
2719e66e423SEric Paris 		ret = -EPERM;
2729e66e423SEric Paris 	}
273de8cd83eSSteve Grubb 
274de8cd83eSSteve Grubb 	/* Check if the response should be audited */
275de8cd83eSSteve Grubb 	if (event->response & FAN_AUDIT)
276032bffd4SRichard Guy Briggs 		audit_fanotify(event->response & ~FAN_AUDIT,
277032bffd4SRichard Guy Briggs 			       &event->audit_rule);
278de8cd83eSSteve Grubb 
279b2d87909SEric Paris 	pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
280b2d87909SEric Paris 		 group, event, ret);
281fabf7f29SJan Kara out:
282fabf7f29SJan Kara 	fsnotify_destroy_event(group, &event->fae.fse);
283b2d87909SEric Paris 
2849e66e423SEric Paris 	return ret;
2859e66e423SEric Paris }
2869e66e423SEric Paris 
2872d10b230SMatthew Bobrowski /*
2882d10b230SMatthew Bobrowski  * This function returns a mask for an event that only contains the flags
2892d10b230SMatthew Bobrowski  * that have been specifically requested by the user. Flags that may have
2902d10b230SMatthew Bobrowski  * been included within the event mask, but have not been explicitly
2912d10b230SMatthew Bobrowski  * requested by the user, will not be present in the returned mask.
2922d10b230SMatthew Bobrowski  */
fanotify_group_event_mask(struct fsnotify_group * group,struct fsnotify_iter_info * iter_info,u32 * match_mask,u32 event_mask,const void * data,int data_type,struct inode * dir)29383b535d2SAmir Goldstein static u32 fanotify_group_event_mask(struct fsnotify_group *group,
29483b535d2SAmir Goldstein 				     struct fsnotify_iter_info *iter_info,
2952bfbcccdSAmir Goldstein 				     u32 *match_mask, u32 event_mask,
2962bfbcccdSAmir Goldstein 				     const void *data, int data_type,
2972bfbcccdSAmir Goldstein 				     struct inode *dir)
2981c529063SEric Paris {
29931a371e4SAmir Goldstein 	__u32 marks_mask = 0, marks_ignore_mask = 0;
3000badfa02SAmir Goldstein 	__u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS |
3010badfa02SAmir Goldstein 				     FANOTIFY_EVENT_FLAGS;
302aa93bdc5SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
303d809daf1SAmir Goldstein 	unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
304837a3934SAmir Goldstein 	struct fsnotify_mark *mark;
30531a371e4SAmir Goldstein 	bool ondir = event_mask & FAN_ONDIR;
306837a3934SAmir Goldstein 	int type;
3071968f5eeSEric Paris 
308837a3934SAmir Goldstein 	pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
309837a3934SAmir Goldstein 		 __func__, iter_info->report_mask, event_mask, data, data_type);
3101968f5eeSEric Paris 
311d809daf1SAmir Goldstein 	if (!fid_mode) {
31283b535d2SAmir Goldstein 		/* Do we have path to open a file descriptor? */
313aa93bdc5SAmir Goldstein 		if (!path)
3142d10b230SMatthew Bobrowski 			return 0;
31583b535d2SAmir Goldstein 		/* Path type events are only relevant for files and dirs */
31683b535d2SAmir Goldstein 		if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
3172d10b230SMatthew Bobrowski 			return 0;
31883b7a598SAmir Goldstein 	} else if (!(fid_mode & FAN_REPORT_FID)) {
31983b7a598SAmir Goldstein 		/* Do we have a directory inode to report? */
32031a371e4SAmir Goldstein 		if (!dir && !ondir)
32183b7a598SAmir Goldstein 			return 0;
32283b535d2SAmir Goldstein 	}
323e1c048baSEric Paris 
32414362a25SAmir Goldstein 	fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
32531a371e4SAmir Goldstein 		/*
32631a371e4SAmir Goldstein 		 * Apply ignore mask depending on event flags in ignore mask.
32731a371e4SAmir Goldstein 		 */
32831a371e4SAmir Goldstein 		marks_ignore_mask |=
32931a371e4SAmir Goldstein 			fsnotify_effective_ignore_mask(mark, ondir, type);
3302f02fd3fSAmir Goldstein 
3311968f5eeSEric Paris 		/*
33231a371e4SAmir Goldstein 		 * Send the event depending on event flags in mark mask.
33355bf882cSAmir Goldstein 		 */
33431a371e4SAmir Goldstein 		if (!fsnotify_mask_applicable(mark->mask, ondir, type))
33555bf882cSAmir Goldstein 			continue;
33655bf882cSAmir Goldstein 
337837a3934SAmir Goldstein 		marks_mask |= mark->mask;
3382bfbcccdSAmir Goldstein 
3392bfbcccdSAmir Goldstein 		/* Record the mark types of this group that matched the event */
3402bfbcccdSAmir Goldstein 		*match_mask |= 1U << type;
3411968f5eeSEric Paris 	}
3421968f5eeSEric Paris 
34331a371e4SAmir Goldstein 	test_mask = event_mask & marks_mask & ~marks_ignore_mask;
344e7fce6d9SAmir Goldstein 
345e7fce6d9SAmir Goldstein 	/*
3469e2ba2c3SAmir Goldstein 	 * For dirent modification events (create/delete/move) that do not carry
3479e2ba2c3SAmir Goldstein 	 * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
3489e2ba2c3SAmir Goldstein 	 * so user can differentiate them from creat/unlink.
349e7fce6d9SAmir Goldstein 	 *
350e7fce6d9SAmir Goldstein 	 * For backward compatibility and consistency, do not report FAN_ONDIR
351e7fce6d9SAmir Goldstein 	 * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
3520badfa02SAmir Goldstein 	 * to user in fid mode for all event types.
3530badfa02SAmir Goldstein 	 *
3540badfa02SAmir Goldstein 	 * We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to
3550badfa02SAmir Goldstein 	 * fanotify_alloc_event() when group is reporting fid as indication
3560badfa02SAmir Goldstein 	 * that event happened on child.
357e7fce6d9SAmir Goldstein 	 */
358d809daf1SAmir Goldstein 	if (fid_mode) {
3590badfa02SAmir Goldstein 		/* Do not report event flags without any event */
3600badfa02SAmir Goldstein 		if (!(test_mask & ~FANOTIFY_EVENT_FLAGS))
361e7fce6d9SAmir Goldstein 			return 0;
362e7fce6d9SAmir Goldstein 	} else {
3630badfa02SAmir Goldstein 		user_mask &= ~FANOTIFY_EVENT_FLAGS;
364e7fce6d9SAmir Goldstein 	}
365e7fce6d9SAmir Goldstein 
366e7fce6d9SAmir Goldstein 	return test_mask & user_mask;
3671c529063SEric Paris }
3681c529063SEric Paris 
369f454fa61SAmir Goldstein /*
370f35c4156SAmir Goldstein  * Check size needed to encode fanotify_fh.
371f35c4156SAmir Goldstein  *
372f35c4156SAmir Goldstein  * Return size of encoded fh without fanotify_fh header.
373f35c4156SAmir Goldstein  * Return 0 on failure to encode.
374f35c4156SAmir Goldstein  */
fanotify_encode_fh_len(struct inode * inode)375f35c4156SAmir Goldstein static int fanotify_encode_fh_len(struct inode *inode)
376f35c4156SAmir Goldstein {
377f35c4156SAmir Goldstein 	int dwords = 0;
378572c28f2SGabriel Krisman Bertazi 	int fh_len;
379f35c4156SAmir Goldstein 
380f35c4156SAmir Goldstein 	if (!inode)
381f35c4156SAmir Goldstein 		return 0;
382f35c4156SAmir Goldstein 
383a95aef69SAmir Goldstein 	exportfs_encode_fid(inode, NULL, &dwords);
384572c28f2SGabriel Krisman Bertazi 	fh_len = dwords << 2;
385f35c4156SAmir Goldstein 
386572c28f2SGabriel Krisman Bertazi 	/*
387572c28f2SGabriel Krisman Bertazi 	 * struct fanotify_error_event might be preallocated and is
388572c28f2SGabriel Krisman Bertazi 	 * limited to MAX_HANDLE_SZ.  This should never happen, but
389572c28f2SGabriel Krisman Bertazi 	 * safeguard by forcing an invalid file handle.
390572c28f2SGabriel Krisman Bertazi 	 */
391572c28f2SGabriel Krisman Bertazi 	if (WARN_ON_ONCE(fh_len > MAX_HANDLE_SZ))
392572c28f2SGabriel Krisman Bertazi 		return 0;
393572c28f2SGabriel Krisman Bertazi 
394572c28f2SGabriel Krisman Bertazi 	return fh_len;
395f35c4156SAmir Goldstein }
396f35c4156SAmir Goldstein 
397f35c4156SAmir Goldstein /*
398f454fa61SAmir Goldstein  * Encode fanotify_fh.
399f454fa61SAmir Goldstein  *
400f454fa61SAmir Goldstein  * Return total size of encoded fh including fanotify_fh header.
401f454fa61SAmir Goldstein  * Return 0 on failure to encode.
402f454fa61SAmir Goldstein  */
fanotify_encode_fh(struct fanotify_fh * fh,struct inode * inode,unsigned int fh_len,unsigned int * hash,gfp_t gfp)403f454fa61SAmir Goldstein static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
4047e3e5c69SAmir Goldstein 			      unsigned int fh_len, unsigned int *hash,
4057e3e5c69SAmir Goldstein 			      gfp_t gfp)
406e9e0c890SAmir Goldstein {
407f35c4156SAmir Goldstein 	int dwords, type = 0;
408afc894c7SJan Kara 	char *ext_buf = NULL;
409afc894c7SJan Kara 	void *buf = fh->buf;
410afc894c7SJan Kara 	int err;
411e9e0c890SAmir Goldstein 
4126ad1aaddSAmir Goldstein 	fh->type = FILEID_ROOT;
4136ad1aaddSAmir Goldstein 	fh->len = 0;
414f35c4156SAmir Goldstein 	fh->flags = 0;
415272531acSGabriel Krisman Bertazi 
416272531acSGabriel Krisman Bertazi 	/*
417272531acSGabriel Krisman Bertazi 	 * Invalid FHs are used by FAN_FS_ERROR for errors not
418272531acSGabriel Krisman Bertazi 	 * linked to any inode. The f_handle won't be reported
419272531acSGabriel Krisman Bertazi 	 * back to userspace.
420272531acSGabriel Krisman Bertazi 	 */
421cacfb956SAmir Goldstein 	if (!inode)
422272531acSGabriel Krisman Bertazi 		goto out;
423cacfb956SAmir Goldstein 
424f35c4156SAmir Goldstein 	/*
425f35c4156SAmir Goldstein 	 * !gpf means preallocated variable size fh, but fh_len could
426f35c4156SAmir Goldstein 	 * be zero in that case if encoding fh len failed.
427f35c4156SAmir Goldstein 	 */
428e9e0c890SAmir Goldstein 	err = -ENOENT;
4292d9374f0SAmir Goldstein 	if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4) || fh_len > MAX_HANDLE_SZ)
430e9e0c890SAmir Goldstein 		goto out_err;
431e9e0c890SAmir Goldstein 
432f35c4156SAmir Goldstein 	/* No external buffer in a variable size allocated fh */
433f35c4156SAmir Goldstein 	if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) {
434f35c4156SAmir Goldstein 		/* Treat failure to allocate fh as failure to encode fh */
435e9e0c890SAmir Goldstein 		err = -ENOMEM;
436f35c4156SAmir Goldstein 		ext_buf = kmalloc(fh_len, gfp);
437afc894c7SJan Kara 		if (!ext_buf)
438e9e0c890SAmir Goldstein 			goto out_err;
439afc894c7SJan Kara 
440afc894c7SJan Kara 		*fanotify_fh_ext_buf_ptr(fh) = ext_buf;
441afc894c7SJan Kara 		buf = ext_buf;
442f35c4156SAmir Goldstein 		fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF;
443e9e0c890SAmir Goldstein 	}
444e9e0c890SAmir Goldstein 
445f35c4156SAmir Goldstein 	dwords = fh_len >> 2;
446a95aef69SAmir Goldstein 	type = exportfs_encode_fid(inode, buf, &dwords);
447e9e0c890SAmir Goldstein 	err = -EINVAL;
448*7cdafe6cSAmir Goldstein 	if (type <= 0 || type == FILEID_INVALID || fh_len != dwords << 2)
449e9e0c890SAmir Goldstein 		goto out_err;
450e9e0c890SAmir Goldstein 
451afc894c7SJan Kara 	fh->type = type;
452f35c4156SAmir Goldstein 	fh->len = fh_len;
453e9e0c890SAmir Goldstein 
454272531acSGabriel Krisman Bertazi out:
45574fe4734SGabriel Krisman Bertazi 	/*
45674fe4734SGabriel Krisman Bertazi 	 * Mix fh into event merge key.  Hash might be NULL in case of
45774fe4734SGabriel Krisman Bertazi 	 * unhashed FID events (i.e. FAN_FS_ERROR).
45874fe4734SGabriel Krisman Bertazi 	 */
45974fe4734SGabriel Krisman Bertazi 	if (hash)
4607e3e5c69SAmir Goldstein 		*hash ^= fanotify_hash_fh(fh);
4617e3e5c69SAmir Goldstein 
462f35c4156SAmir Goldstein 	return FANOTIFY_FH_HDR_LEN + fh_len;
463e9e0c890SAmir Goldstein 
464e9e0c890SAmir Goldstein out_err:
465afc894c7SJan Kara 	pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
466f35c4156SAmir Goldstein 			    type, fh_len, err);
467afc894c7SJan Kara 	kfree(ext_buf);
468afc894c7SJan Kara 	*fanotify_fh_ext_buf_ptr(fh) = NULL;
469afc894c7SJan Kara 	/* Report the event without a file identifier on encode error */
470afc894c7SJan Kara 	fh->type = FILEID_INVALID;
471afc894c7SJan Kara 	fh->len = 0;
472f454fa61SAmir Goldstein 	return 0;
473e9e0c890SAmir Goldstein }
474e9e0c890SAmir Goldstein 
47583b535d2SAmir Goldstein /*
476d61fd650SAmir Goldstein  * FAN_REPORT_FID is ambiguous in that it reports the fid of the child for
477d61fd650SAmir Goldstein  * some events and the fid of the parent for create/delete/move events.
478d61fd650SAmir Goldstein  *
479d61fd650SAmir Goldstein  * With the FAN_REPORT_TARGET_FID flag, the fid of the child is reported
480d61fd650SAmir Goldstein  * also in create/delete/move events in addition to the fid of the parent
481d61fd650SAmir Goldstein  * and the name of the child.
482d61fd650SAmir Goldstein  */
fanotify_report_child_fid(unsigned int fid_mode,u32 mask)483d61fd650SAmir Goldstein static inline bool fanotify_report_child_fid(unsigned int fid_mode, u32 mask)
484d61fd650SAmir Goldstein {
485d61fd650SAmir Goldstein 	if (mask & ALL_FSNOTIFY_DIRENT_EVENTS)
486d61fd650SAmir Goldstein 		return (fid_mode & FAN_REPORT_TARGET_FID);
487d61fd650SAmir Goldstein 
488d61fd650SAmir Goldstein 	return (fid_mode & FAN_REPORT_FID) && !(mask & FAN_ONDIR);
489d61fd650SAmir Goldstein }
490d61fd650SAmir Goldstein 
491d61fd650SAmir Goldstein /*
492d61fd650SAmir Goldstein  * The inode to use as identifier when reporting fid depends on the event
493d61fd650SAmir Goldstein  * and the group flags.
494d61fd650SAmir Goldstein  *
495d61fd650SAmir Goldstein  * With the group flag FAN_REPORT_TARGET_FID, always report the child fid.
496d61fd650SAmir Goldstein  *
497d61fd650SAmir Goldstein  * Without the group flag FAN_REPORT_TARGET_FID, report the modified directory
498d61fd650SAmir Goldstein  * fid on dirent events and the child fid otherwise.
499d61fd650SAmir Goldstein  *
50083b535d2SAmir Goldstein  * For example:
501d61fd650SAmir Goldstein  * FS_ATTRIB reports the child fid even if reported on a watched parent.
502d61fd650SAmir Goldstein  * FS_CREATE reports the modified dir fid without FAN_REPORT_TARGET_FID.
503d61fd650SAmir Goldstein  *       and reports the created child fid with FAN_REPORT_TARGET_FID.
50483b535d2SAmir Goldstein  */
fanotify_fid_inode(u32 event_mask,const void * data,int data_type,struct inode * dir,unsigned int fid_mode)505b54cecf5SAmir Goldstein static struct inode *fanotify_fid_inode(u32 event_mask, const void *data,
506d61fd650SAmir Goldstein 					int data_type, struct inode *dir,
507d61fd650SAmir Goldstein 					unsigned int fid_mode)
50883b535d2SAmir Goldstein {
509d61fd650SAmir Goldstein 	if ((event_mask & ALL_FSNOTIFY_DIRENT_EVENTS) &&
510d61fd650SAmir Goldstein 	    !(fid_mode & FAN_REPORT_TARGET_FID))
511b54cecf5SAmir Goldstein 		return dir;
512aa93bdc5SAmir Goldstein 
513cbcf47adSAmir Goldstein 	return fsnotify_data_inode(data, data_type);
51483b535d2SAmir Goldstein }
51583b535d2SAmir Goldstein 
51683b7a598SAmir Goldstein /*
51783b7a598SAmir Goldstein  * The inode to use as identifier when reporting dir fid depends on the event.
51883b7a598SAmir Goldstein  * Report the modified directory inode on dirent modification events.
51983b7a598SAmir Goldstein  * Report the "victim" inode if "victim" is a directory.
52083b7a598SAmir Goldstein  * Report the parent inode if "victim" is not a directory and event is
52183b7a598SAmir Goldstein  * reported to parent.
52283b7a598SAmir Goldstein  * Otherwise, do not report dir fid.
52383b7a598SAmir Goldstein  */
fanotify_dfid_inode(u32 event_mask,const void * data,int data_type,struct inode * dir)52483b7a598SAmir Goldstein static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data,
52583b7a598SAmir Goldstein 					 int data_type, struct inode *dir)
52683b7a598SAmir Goldstein {
52783b7a598SAmir Goldstein 	struct inode *inode = fsnotify_data_inode(data, data_type);
52883b7a598SAmir Goldstein 
52983b7a598SAmir Goldstein 	if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
53083b7a598SAmir Goldstein 		return dir;
53183b7a598SAmir Goldstein 
53212f47bf0SGabriel Krisman Bertazi 	if (inode && S_ISDIR(inode->i_mode))
53383b7a598SAmir Goldstein 		return inode;
53483b7a598SAmir Goldstein 
53583b7a598SAmir Goldstein 	return dir;
53683b7a598SAmir Goldstein }
53783b7a598SAmir Goldstein 
fanotify_alloc_path_event(const struct path * path,unsigned int * hash,gfp_t gfp)5389c61f3b5SAmir Goldstein static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
5397e3e5c69SAmir Goldstein 							unsigned int *hash,
5409c61f3b5SAmir Goldstein 							gfp_t gfp)
5419c61f3b5SAmir Goldstein {
5429c61f3b5SAmir Goldstein 	struct fanotify_path_event *pevent;
5439c61f3b5SAmir Goldstein 
5449c61f3b5SAmir Goldstein 	pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
5459c61f3b5SAmir Goldstein 	if (!pevent)
5469c61f3b5SAmir Goldstein 		return NULL;
5479c61f3b5SAmir Goldstein 
5489c61f3b5SAmir Goldstein 	pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
5499c61f3b5SAmir Goldstein 	pevent->path = *path;
5507e3e5c69SAmir Goldstein 	*hash ^= fanotify_hash_path(path);
5519c61f3b5SAmir Goldstein 	path_get(path);
5529c61f3b5SAmir Goldstein 
5539c61f3b5SAmir Goldstein 	return &pevent->fae;
5549c61f3b5SAmir Goldstein }
5559c61f3b5SAmir Goldstein 
fanotify_alloc_perm_event(const struct path * path,gfp_t gfp)5569c61f3b5SAmir Goldstein static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
5579c61f3b5SAmir Goldstein 							gfp_t gfp)
5589c61f3b5SAmir Goldstein {
5599c61f3b5SAmir Goldstein 	struct fanotify_perm_event *pevent;
5609c61f3b5SAmir Goldstein 
5619c61f3b5SAmir Goldstein 	pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
5629c61f3b5SAmir Goldstein 	if (!pevent)
5639c61f3b5SAmir Goldstein 		return NULL;
5649c61f3b5SAmir Goldstein 
5659c61f3b5SAmir Goldstein 	pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
5669c61f3b5SAmir Goldstein 	pevent->response = 0;
56770529a19SRichard Guy Briggs 	pevent->hdr.type = FAN_RESPONSE_INFO_NONE;
56870529a19SRichard Guy Briggs 	pevent->hdr.pad = 0;
56970529a19SRichard Guy Briggs 	pevent->hdr.len = 0;
5709c61f3b5SAmir Goldstein 	pevent->state = FAN_EVENT_INIT;
5719c61f3b5SAmir Goldstein 	pevent->path = *path;
5729c61f3b5SAmir Goldstein 	path_get(path);
5739c61f3b5SAmir Goldstein 
5749c61f3b5SAmir Goldstein 	return &pevent->fae;
5759c61f3b5SAmir Goldstein }
5769c61f3b5SAmir Goldstein 
fanotify_alloc_fid_event(struct inode * id,__kernel_fsid_t * fsid,unsigned int * hash,gfp_t gfp)5779c61f3b5SAmir Goldstein static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
5789c61f3b5SAmir Goldstein 						       __kernel_fsid_t *fsid,
5797e3e5c69SAmir Goldstein 						       unsigned int *hash,
5809c61f3b5SAmir Goldstein 						       gfp_t gfp)
5819c61f3b5SAmir Goldstein {
5829c61f3b5SAmir Goldstein 	struct fanotify_fid_event *ffe;
5839c61f3b5SAmir Goldstein 
5849c61f3b5SAmir Goldstein 	ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
5859c61f3b5SAmir Goldstein 	if (!ffe)
5869c61f3b5SAmir Goldstein 		return NULL;
5879c61f3b5SAmir Goldstein 
5889c61f3b5SAmir Goldstein 	ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
5899c61f3b5SAmir Goldstein 	ffe->fsid = *fsid;
5907e3e5c69SAmir Goldstein 	*hash ^= fanotify_hash_fsid(fsid);
591f35c4156SAmir Goldstein 	fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id),
5927e3e5c69SAmir Goldstein 			   hash, gfp);
5939c61f3b5SAmir Goldstein 
5949c61f3b5SAmir Goldstein 	return &ffe->fae;
5959c61f3b5SAmir Goldstein }
5969c61f3b5SAmir Goldstein 
fanotify_alloc_name_event(struct inode * dir,__kernel_fsid_t * fsid,const struct qstr * name,struct inode * child,struct dentry * moved,unsigned int * hash,gfp_t gfp)5971a9515acSAmir Goldstein static struct fanotify_event *fanotify_alloc_name_event(struct inode *dir,
5989c61f3b5SAmir Goldstein 							__kernel_fsid_t *fsid,
5997e3e5c69SAmir Goldstein 							const struct qstr *name,
6007e8283afSAmir Goldstein 							struct inode *child,
6013982534bSAmir Goldstein 							struct dentry *moved,
6027e3e5c69SAmir Goldstein 							unsigned int *hash,
6039c61f3b5SAmir Goldstein 							gfp_t gfp)
6049c61f3b5SAmir Goldstein {
6059c61f3b5SAmir Goldstein 	struct fanotify_name_event *fne;
606f454fa61SAmir Goldstein 	struct fanotify_info *info;
6077e8283afSAmir Goldstein 	struct fanotify_fh *dfh, *ffh;
6083982534bSAmir Goldstein 	struct inode *dir2 = moved ? d_inode(moved->d_parent) : NULL;
6093982534bSAmir Goldstein 	const struct qstr *name2 = moved ? &moved->d_name : NULL;
6101a9515acSAmir Goldstein 	unsigned int dir_fh_len = fanotify_encode_fh_len(dir);
6113982534bSAmir Goldstein 	unsigned int dir2_fh_len = fanotify_encode_fh_len(dir2);
6127e8283afSAmir Goldstein 	unsigned int child_fh_len = fanotify_encode_fh_len(child);
6131a9515acSAmir Goldstein 	unsigned long name_len = name ? name->len : 0;
6143982534bSAmir Goldstein 	unsigned long name2_len = name2 ? name2->len : 0;
6151a9515acSAmir Goldstein 	unsigned int len, size;
6169c61f3b5SAmir Goldstein 
6171a9515acSAmir Goldstein 	/* Reserve terminating null byte even for empty name */
6183982534bSAmir Goldstein 	size = sizeof(*fne) + name_len + name2_len + 2;
6191a9515acSAmir Goldstein 	if (dir_fh_len)
6201a9515acSAmir Goldstein 		size += FANOTIFY_FH_HDR_LEN + dir_fh_len;
6213982534bSAmir Goldstein 	if (dir2_fh_len)
6223982534bSAmir Goldstein 		size += FANOTIFY_FH_HDR_LEN + dir2_fh_len;
6237e8283afSAmir Goldstein 	if (child_fh_len)
6247e8283afSAmir Goldstein 		size += FANOTIFY_FH_HDR_LEN + child_fh_len;
625f35c4156SAmir Goldstein 	fne = kmalloc(size, gfp);
6269c61f3b5SAmir Goldstein 	if (!fne)
6279c61f3b5SAmir Goldstein 		return NULL;
6289c61f3b5SAmir Goldstein 
6299c61f3b5SAmir Goldstein 	fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
6309c61f3b5SAmir Goldstein 	fne->fsid = *fsid;
6317e3e5c69SAmir Goldstein 	*hash ^= fanotify_hash_fsid(fsid);
632f454fa61SAmir Goldstein 	info = &fne->info;
633f454fa61SAmir Goldstein 	fanotify_info_init(info);
6341a9515acSAmir Goldstein 	if (dir_fh_len) {
635f454fa61SAmir Goldstein 		dfh = fanotify_info_dir_fh(info);
6361a9515acSAmir Goldstein 		len = fanotify_encode_fh(dfh, dir, dir_fh_len, hash, 0);
6371a9515acSAmir Goldstein 		fanotify_info_set_dir_fh(info, len);
6381a9515acSAmir Goldstein 	}
6393982534bSAmir Goldstein 	if (dir2_fh_len) {
6403982534bSAmir Goldstein 		dfh = fanotify_info_dir2_fh(info);
6413982534bSAmir Goldstein 		len = fanotify_encode_fh(dfh, dir2, dir2_fh_len, hash, 0);
6423982534bSAmir Goldstein 		fanotify_info_set_dir2_fh(info, len);
6433982534bSAmir Goldstein 	}
6447e8283afSAmir Goldstein 	if (child_fh_len) {
6457e8283afSAmir Goldstein 		ffh = fanotify_info_file_fh(info);
6461a9515acSAmir Goldstein 		len = fanotify_encode_fh(ffh, child, child_fh_len, hash, 0);
6471a9515acSAmir Goldstein 		fanotify_info_set_file_fh(info, len);
6487e8283afSAmir Goldstein 	}
6491a9515acSAmir Goldstein 	if (name_len) {
6507e3e5c69SAmir Goldstein 		fanotify_info_copy_name(info, name);
6511a9515acSAmir Goldstein 		*hash ^= full_name_hash((void *)name_len, name->name, name_len);
6527e3e5c69SAmir Goldstein 	}
6533982534bSAmir Goldstein 	if (name2_len) {
6543982534bSAmir Goldstein 		fanotify_info_copy_name2(info, name2);
6553982534bSAmir Goldstein 		*hash ^= full_name_hash((void *)name2_len, name2->name,
6563982534bSAmir Goldstein 					name2_len);
6573982534bSAmir Goldstein 	}
6589c61f3b5SAmir Goldstein 
6591a9515acSAmir Goldstein 	pr_debug("%s: size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n",
6601a9515acSAmir Goldstein 		 __func__, size, dir_fh_len, child_fh_len,
661f35c4156SAmir Goldstein 		 info->name_len, info->name_len, fanotify_info_name(info));
662f35c4156SAmir Goldstein 
6633982534bSAmir Goldstein 	if (dir2_fh_len) {
6643982534bSAmir Goldstein 		pr_debug("%s: dir2_fh_len=%u name2_len=%u name2='%.*s'\n",
6653982534bSAmir Goldstein 			 __func__, dir2_fh_len, info->name2_len,
6663982534bSAmir Goldstein 			 info->name2_len, fanotify_info_name2(info));
6673982534bSAmir Goldstein 	}
6683982534bSAmir Goldstein 
6699c61f3b5SAmir Goldstein 	return &fne->fae;
6709c61f3b5SAmir Goldstein }
6719c61f3b5SAmir Goldstein 
fanotify_alloc_error_event(struct fsnotify_group * group,__kernel_fsid_t * fsid,const void * data,int data_type,unsigned int * hash)67283e9acbeSGabriel Krisman Bertazi static struct fanotify_event *fanotify_alloc_error_event(
67383e9acbeSGabriel Krisman Bertazi 						struct fsnotify_group *group,
67483e9acbeSGabriel Krisman Bertazi 						__kernel_fsid_t *fsid,
6758a6ae641SGabriel Krisman Bertazi 						const void *data, int data_type,
6768a6ae641SGabriel Krisman Bertazi 						unsigned int *hash)
67783e9acbeSGabriel Krisman Bertazi {
67883e9acbeSGabriel Krisman Bertazi 	struct fs_error_report *report =
67983e9acbeSGabriel Krisman Bertazi 			fsnotify_data_error_report(data, data_type);
680936d6a38SGabriel Krisman Bertazi 	struct inode *inode;
68183e9acbeSGabriel Krisman Bertazi 	struct fanotify_error_event *fee;
682936d6a38SGabriel Krisman Bertazi 	int fh_len;
68383e9acbeSGabriel Krisman Bertazi 
68483e9acbeSGabriel Krisman Bertazi 	if (WARN_ON_ONCE(!report))
68583e9acbeSGabriel Krisman Bertazi 		return NULL;
68683e9acbeSGabriel Krisman Bertazi 
68783e9acbeSGabriel Krisman Bertazi 	fee = mempool_alloc(&group->fanotify_data.error_events_pool, GFP_NOFS);
68883e9acbeSGabriel Krisman Bertazi 	if (!fee)
68983e9acbeSGabriel Krisman Bertazi 		return NULL;
69083e9acbeSGabriel Krisman Bertazi 
69183e9acbeSGabriel Krisman Bertazi 	fee->fae.type = FANOTIFY_EVENT_TYPE_FS_ERROR;
692130a3c74SGabriel Krisman Bertazi 	fee->error = report->error;
6938a6ae641SGabriel Krisman Bertazi 	fee->err_count = 1;
6948a6ae641SGabriel Krisman Bertazi 	fee->fsid = *fsid;
6958a6ae641SGabriel Krisman Bertazi 
696936d6a38SGabriel Krisman Bertazi 	inode = report->inode;
697936d6a38SGabriel Krisman Bertazi 	fh_len = fanotify_encode_fh_len(inode);
698936d6a38SGabriel Krisman Bertazi 
699936d6a38SGabriel Krisman Bertazi 	/* Bad fh_len. Fallback to using an invalid fh. Should never happen. */
700936d6a38SGabriel Krisman Bertazi 	if (!fh_len && inode)
701936d6a38SGabriel Krisman Bertazi 		inode = NULL;
702936d6a38SGabriel Krisman Bertazi 
703936d6a38SGabriel Krisman Bertazi 	fanotify_encode_fh(&fee->object_fh, inode, fh_len, NULL, 0);
704936d6a38SGabriel Krisman Bertazi 
7058a6ae641SGabriel Krisman Bertazi 	*hash ^= fanotify_hash_fsid(fsid);
70683e9acbeSGabriel Krisman Bertazi 
70783e9acbeSGabriel Krisman Bertazi 	return &fee->fae;
70883e9acbeSGabriel Krisman Bertazi }
70983e9acbeSGabriel Krisman Bertazi 
fanotify_alloc_event(struct fsnotify_group * group,u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * file_name,__kernel_fsid_t * fsid,u32 match_mask)7102bfbcccdSAmir Goldstein static struct fanotify_event *fanotify_alloc_event(
7112bfbcccdSAmir Goldstein 				struct fsnotify_group *group,
7122bfbcccdSAmir Goldstein 				u32 mask, const void *data, int data_type,
7132bfbcccdSAmir Goldstein 				struct inode *dir, const struct qstr *file_name,
7142bfbcccdSAmir Goldstein 				__kernel_fsid_t *fsid, u32 match_mask)
715f083441bSJan Kara {
71633913997SAmir Goldstein 	struct fanotify_event *event = NULL;
717d46eb14bSShakeel Butt 	gfp_t gfp = GFP_KERNEL_ACCOUNT;
718d61fd650SAmir Goldstein 	unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
719d61fd650SAmir Goldstein 	struct inode *id = fanotify_fid_inode(mask, data, data_type, dir,
720d61fd650SAmir Goldstein 					      fid_mode);
72183b7a598SAmir Goldstein 	struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir);
722aa93bdc5SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
723b87d8cefSRoman Gushchin 	struct mem_cgroup *old_memcg;
7243982534bSAmir Goldstein 	struct dentry *moved = NULL;
7257e8283afSAmir Goldstein 	struct inode *child = NULL;
72608b95c33SAmir Goldstein 	bool name_event = false;
7278988f11aSAmir Goldstein 	unsigned int hash = 0;
7287e3e5c69SAmir Goldstein 	bool ondir = mask & FAN_ONDIR;
7297e3e5c69SAmir Goldstein 	struct pid *pid;
7301f5eaa90SJan Kara 
731929943b3SAmir Goldstein 	if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
7327e8283afSAmir Goldstein 		/*
733d61fd650SAmir Goldstein 		 * For certain events and group flags, report the child fid
734691d9763SAmir Goldstein 		 * in addition to reporting the parent fid and maybe child name.
7357e8283afSAmir Goldstein 		 */
736d61fd650SAmir Goldstein 		if (fanotify_report_child_fid(fid_mode, mask) && id != dirid)
7377e8283afSAmir Goldstein 			child = id;
7387e8283afSAmir Goldstein 
73983b7a598SAmir Goldstein 		id = dirid;
74083b7a598SAmir Goldstein 
7411f5eaa90SJan Kara 		/*
742929943b3SAmir Goldstein 		 * We record file name only in a group with FAN_REPORT_NAME
743929943b3SAmir Goldstein 		 * and when we have a directory inode to report.
744929943b3SAmir Goldstein 		 *
745929943b3SAmir Goldstein 		 * For directory entry modification event, we record the fid of
746929943b3SAmir Goldstein 		 * the directory and the name of the modified entry.
747929943b3SAmir Goldstein 		 *
748929943b3SAmir Goldstein 		 * For event on non-directory that is reported to parent, we
749929943b3SAmir Goldstein 		 * record the fid of the parent and the name of the child.
750691d9763SAmir Goldstein 		 *
751691d9763SAmir Goldstein 		 * Even if not reporting name, we need a variable length
752691d9763SAmir Goldstein 		 * fanotify_name_event if reporting both parent and child fids.
753929943b3SAmir Goldstein 		 */
754691d9763SAmir Goldstein 		if (!(fid_mode & FAN_REPORT_NAME)) {
755691d9763SAmir Goldstein 			name_event = !!child;
756691d9763SAmir Goldstein 			file_name = NULL;
7577e3e5c69SAmir Goldstein 		} else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) || !ondir) {
758929943b3SAmir Goldstein 			name_event = true;
759929943b3SAmir Goldstein 		}
7603982534bSAmir Goldstein 
7613982534bSAmir Goldstein 		/*
7622bfbcccdSAmir Goldstein 		 * In the special case of FAN_RENAME event, use the match_mask
7632bfbcccdSAmir Goldstein 		 * to determine if we need to report only the old parent+name,
7642bfbcccdSAmir Goldstein 		 * only the new parent+name or both.
7653982534bSAmir Goldstein 		 * 'dirid' and 'file_name' are the old parent+name and
7663982534bSAmir Goldstein 		 * 'moved' has the new parent+name.
7673982534bSAmir Goldstein 		 */
7682bfbcccdSAmir Goldstein 		if (mask & FAN_RENAME) {
7692bfbcccdSAmir Goldstein 			bool report_old, report_new;
7702bfbcccdSAmir Goldstein 
7712bfbcccdSAmir Goldstein 			if (WARN_ON_ONCE(!match_mask))
7722bfbcccdSAmir Goldstein 				return NULL;
7732bfbcccdSAmir Goldstein 
7742bfbcccdSAmir Goldstein 			/* Report both old and new parent+name if sb watching */
7752bfbcccdSAmir Goldstein 			report_old = report_new =
7762bfbcccdSAmir Goldstein 				match_mask & (1U << FSNOTIFY_ITER_TYPE_SB);
7772bfbcccdSAmir Goldstein 			report_old |=
7782bfbcccdSAmir Goldstein 				match_mask & (1U << FSNOTIFY_ITER_TYPE_INODE);
7792bfbcccdSAmir Goldstein 			report_new |=
7802bfbcccdSAmir Goldstein 				match_mask & (1U << FSNOTIFY_ITER_TYPE_INODE2);
7812bfbcccdSAmir Goldstein 
7822bfbcccdSAmir Goldstein 			if (!report_old) {
7832bfbcccdSAmir Goldstein 				/* Do not report old parent+name */
7842bfbcccdSAmir Goldstein 				dirid = NULL;
7852bfbcccdSAmir Goldstein 				file_name = NULL;
7862bfbcccdSAmir Goldstein 			}
7872bfbcccdSAmir Goldstein 			if (report_new) {
7882bfbcccdSAmir Goldstein 				/* Report new parent+name */
7893982534bSAmir Goldstein 				moved = fsnotify_data_dentry(data, data_type);
790691d9763SAmir Goldstein 			}
7912bfbcccdSAmir Goldstein 		}
7922bfbcccdSAmir Goldstein 	}
793929943b3SAmir Goldstein 
794929943b3SAmir Goldstein 	/*
7951f5eaa90SJan Kara 	 * For queues with unlimited length lost events are not expected and
7961f5eaa90SJan Kara 	 * can possibly have security implications. Avoid losing events when
797ec165450SShakeel Butt 	 * memory is short. For the limited size queues, avoid OOM killer in the
798ec165450SShakeel Butt 	 * target monitoring memcg as it may have security repercussion.
7991f5eaa90SJan Kara 	 */
8001f5eaa90SJan Kara 	if (group->max_events == UINT_MAX)
8011f5eaa90SJan Kara 		gfp |= __GFP_NOFAIL;
802ec165450SShakeel Butt 	else
803ec165450SShakeel Butt 		gfp |= __GFP_RETRY_MAYFAIL;
804f083441bSJan Kara 
805d46eb14bSShakeel Butt 	/* Whoever is interested in the event, pays for the allocation. */
806b87d8cefSRoman Gushchin 	old_memcg = set_active_memcg(group->memcg);
807d46eb14bSShakeel Butt 
8086685df31SMiklos Szeredi 	if (fanotify_is_perm_event(mask)) {
8099c61f3b5SAmir Goldstein 		event = fanotify_alloc_perm_event(path, gfp);
81083e9acbeSGabriel Krisman Bertazi 	} else if (fanotify_is_error_event(mask)) {
81183e9acbeSGabriel Krisman Bertazi 		event = fanotify_alloc_error_event(group, fsid, data,
8128a6ae641SGabriel Krisman Bertazi 						   data_type, &hash);
8133982534bSAmir Goldstein 	} else if (name_event && (file_name || moved || child)) {
8143982534bSAmir Goldstein 		event = fanotify_alloc_name_event(dirid, fsid, file_name, child,
8153982534bSAmir Goldstein 						  moved, &hash, gfp);
816d809daf1SAmir Goldstein 	} else if (fid_mode) {
8177e3e5c69SAmir Goldstein 		event = fanotify_alloc_fid_event(id, fsid, &hash, gfp);
8187088f357SJan Kara 	} else {
8197e3e5c69SAmir Goldstein 		event = fanotify_alloc_path_event(path, &hash, gfp);
8207088f357SJan Kara 	}
8217088f357SJan Kara 
8229c61f3b5SAmir Goldstein 	if (!event)
8239c61f3b5SAmir Goldstein 		goto out;
8249c61f3b5SAmir Goldstein 
825d0a6a87eSAmir Goldstein 	if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
8267e3e5c69SAmir Goldstein 		pid = get_pid(task_pid(current));
827d0a6a87eSAmir Goldstein 	else
8287e3e5c69SAmir Goldstein 		pid = get_pid(task_tgid(current));
8297e3e5c69SAmir Goldstein 
8307e3e5c69SAmir Goldstein 	/* Mix event info, FAN_ONDIR flag and pid into event merge key */
8317e3e5c69SAmir Goldstein 	hash ^= hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS);
8327e3e5c69SAmir Goldstein 	fanotify_init_event(event, hash, mask);
8337e3e5c69SAmir Goldstein 	event->pid = pid;
8347088f357SJan Kara 
835d46eb14bSShakeel Butt out:
836b87d8cefSRoman Gushchin 	set_active_memcg(old_memcg);
837f083441bSJan Kara 	return event;
838f083441bSJan Kara }
839f083441bSJan Kara 
84077115225SAmir Goldstein /*
84177115225SAmir Goldstein  * Get cached fsid of the filesystem containing the object from any connector.
84277115225SAmir Goldstein  * All connectors are supposed to have the same fsid, but we do not verify that
84377115225SAmir Goldstein  * here.
84477115225SAmir Goldstein  */
fanotify_get_fsid(struct fsnotify_iter_info * iter_info)84577115225SAmir Goldstein static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
84677115225SAmir Goldstein {
84714362a25SAmir Goldstein 	struct fsnotify_mark *mark;
84877115225SAmir Goldstein 	int type;
84977115225SAmir Goldstein 	__kernel_fsid_t fsid = {};
85077115225SAmir Goldstein 
85114362a25SAmir Goldstein 	fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
852b1da6a51SJan Kara 		struct fsnotify_mark_connector *conn;
853b1da6a51SJan Kara 
85414362a25SAmir Goldstein 		conn = READ_ONCE(mark->connector);
855b1da6a51SJan Kara 		/* Mark is just getting destroyed or created? */
856b1da6a51SJan Kara 		if (!conn)
857b1da6a51SJan Kara 			continue;
858c285a2f0SAmir Goldstein 		if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID))
859c285a2f0SAmir Goldstein 			continue;
860c285a2f0SAmir Goldstein 		/* Pairs with smp_wmb() in fsnotify_add_mark_list() */
861c285a2f0SAmir Goldstein 		smp_rmb();
862b1da6a51SJan Kara 		fsid = conn->fsid;
86377115225SAmir Goldstein 		if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
86477115225SAmir Goldstein 			continue;
86577115225SAmir Goldstein 		return fsid;
86677115225SAmir Goldstein 	}
86777115225SAmir Goldstein 
86877115225SAmir Goldstein 	return fsid;
86977115225SAmir Goldstein }
87077115225SAmir Goldstein 
87194e00d28SAmir Goldstein /*
87294e00d28SAmir Goldstein  * Add an event to hash table for faster merge.
87394e00d28SAmir Goldstein  */
fanotify_insert_event(struct fsnotify_group * group,struct fsnotify_event * fsn_event)87494e00d28SAmir Goldstein static void fanotify_insert_event(struct fsnotify_group *group,
87594e00d28SAmir Goldstein 				  struct fsnotify_event *fsn_event)
87694e00d28SAmir Goldstein {
87794e00d28SAmir Goldstein 	struct fanotify_event *event = FANOTIFY_E(fsn_event);
87894e00d28SAmir Goldstein 	unsigned int bucket = fanotify_event_hash_bucket(group, event);
87994e00d28SAmir Goldstein 	struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
88094e00d28SAmir Goldstein 
88194e00d28SAmir Goldstein 	assert_spin_locked(&group->notification_lock);
88294e00d28SAmir Goldstein 
883cc53b55fSGabriel Krisman Bertazi 	if (!fanotify_is_hashed_event(event->mask))
884cc53b55fSGabriel Krisman Bertazi 		return;
885cc53b55fSGabriel Krisman Bertazi 
88694e00d28SAmir Goldstein 	pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
88794e00d28SAmir Goldstein 		 group, event, bucket);
88894e00d28SAmir Goldstein 
88994e00d28SAmir Goldstein 	hlist_add_head(&event->merge_list, hlist);
89094e00d28SAmir Goldstein }
89194e00d28SAmir Goldstein 
fanotify_handle_event(struct fsnotify_group * group,u32 mask,const void * data,int data_type,struct inode * dir,const struct qstr * file_name,u32 cookie,struct fsnotify_iter_info * iter_info)892b54cecf5SAmir Goldstein static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
893b54cecf5SAmir Goldstein 				 const void *data, int data_type,
894b54cecf5SAmir Goldstein 				 struct inode *dir,
895e43e9c33SAl Viro 				 const struct qstr *file_name, u32 cookie,
8969385a84dSJan Kara 				 struct fsnotify_iter_info *iter_info)
8977053aee2SJan Kara {
8987053aee2SJan Kara 	int ret = 0;
89933913997SAmir Goldstein 	struct fanotify_event *event;
9007053aee2SJan Kara 	struct fsnotify_event *fsn_event;
90177115225SAmir Goldstein 	__kernel_fsid_t fsid = {};
9022bfbcccdSAmir Goldstein 	u32 match_mask = 0;
9037053aee2SJan Kara 
9047053aee2SJan Kara 	BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
9057053aee2SJan Kara 	BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
906235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
9077053aee2SJan Kara 	BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
9087053aee2SJan Kara 	BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
9097053aee2SJan Kara 	BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
910235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
911235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
912235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
913235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
914235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
915235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
9167053aee2SJan Kara 	BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
9177053aee2SJan Kara 	BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
9187053aee2SJan Kara 	BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
9197053aee2SJan Kara 	BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
9207053aee2SJan Kara 	BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
9219b076f1cSMatthew Bobrowski 	BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
92266917a31SMatthew Bobrowski 	BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
9238d11a4f4SGabriel Krisman Bertazi 	BUILD_BUG_ON(FAN_FS_ERROR != FS_ERROR);
9243982534bSAmir Goldstein 	BUILD_BUG_ON(FAN_RENAME != FS_RENAME);
9257053aee2SJan Kara 
9268cc3b1ccSAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 21);
927bdd5a46fSAmir Goldstein 
9282bfbcccdSAmir Goldstein 	mask = fanotify_group_event_mask(group, iter_info, &match_mask,
9292bfbcccdSAmir Goldstein 					 mask, data, data_type, dir);
9302d10b230SMatthew Bobrowski 	if (!mask)
93183c4c4b0SJan Kara 		return 0;
93283c4c4b0SJan Kara 
9332bfbcccdSAmir Goldstein 	pr_debug("%s: group=%p mask=%x report_mask=%x\n", __func__,
9342bfbcccdSAmir Goldstein 		 group, mask, match_mask);
9357053aee2SJan Kara 
9366685df31SMiklos Szeredi 	if (fanotify_is_perm_event(mask)) {
937f37650f1SMiklos Szeredi 		/*
938f37650f1SMiklos Szeredi 		 * fsnotify_prepare_user_wait() fails if we race with mark
939f37650f1SMiklos Szeredi 		 * deletion.  Just let the operation pass in that case.
940f37650f1SMiklos Szeredi 		 */
941f37650f1SMiklos Szeredi 		if (!fsnotify_prepare_user_wait(iter_info))
942f37650f1SMiklos Szeredi 			return 0;
943f37650f1SMiklos Szeredi 	}
944f37650f1SMiklos Szeredi 
945d809daf1SAmir Goldstein 	if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) {
94677115225SAmir Goldstein 		fsid = fanotify_get_fsid(iter_info);
947b1da6a51SJan Kara 		/* Racing with mark destruction or creation? */
948b1da6a51SJan Kara 		if (!fsid.val[0] && !fsid.val[1])
949b1da6a51SJan Kara 			return 0;
950b1da6a51SJan Kara 	}
95177115225SAmir Goldstein 
952b54cecf5SAmir Goldstein 	event = fanotify_alloc_event(group, mask, data, data_type, dir,
9532bfbcccdSAmir Goldstein 				     file_name, &fsid, match_mask);
954f37650f1SMiklos Szeredi 	ret = -ENOMEM;
9557b1f6417SJan Kara 	if (unlikely(!event)) {
9567b1f6417SJan Kara 		/*
9577b1f6417SJan Kara 		 * We don't queue overflow events for permission events as
9587b1f6417SJan Kara 		 * there the access is denied and so no event is in fact lost.
9597b1f6417SJan Kara 		 */
9607b1f6417SJan Kara 		if (!fanotify_is_perm_event(mask))
9617b1f6417SJan Kara 			fsnotify_queue_overflow(group);
962f37650f1SMiklos Szeredi 		goto finish;
9637b1f6417SJan Kara 	}
9647053aee2SJan Kara 
9657053aee2SJan Kara 	fsn_event = &event->fse;
9661ad03c3aSGabriel Krisman Bertazi 	ret = fsnotify_insert_event(group, fsn_event, fanotify_merge,
967cc53b55fSGabriel Krisman Bertazi 				    fanotify_insert_event);
96883c0e1b4SJan Kara 	if (ret) {
969482ef06cSJan Kara 		/* Permission events shouldn't be merged */
97023c9deebSAmir Goldstein 		BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
9717053aee2SJan Kara 		/* Our event wasn't used in the end. Free it. */
9727053aee2SJan Kara 		fsnotify_destroy_event(group, fsn_event);
973482ef06cSJan Kara 
974f37650f1SMiklos Szeredi 		ret = 0;
9756685df31SMiklos Szeredi 	} else if (fanotify_is_perm_event(mask)) {
9767088f357SJan Kara 		ret = fanotify_get_response(group, FANOTIFY_PERM(event),
97705f0e387SJan Kara 					    iter_info);
97885816794SJan Kara 	}
979f37650f1SMiklos Szeredi finish:
9806685df31SMiklos Szeredi 	if (fanotify_is_perm_event(mask))
981f37650f1SMiklos Szeredi 		fsnotify_finish_user_wait(iter_info);
9826685df31SMiklos Szeredi 
9837053aee2SJan Kara 	return ret;
9847053aee2SJan Kara }
9857053aee2SJan Kara 
fanotify_free_group_priv(struct fsnotify_group * group)9864afeff85SEric Paris static void fanotify_free_group_priv(struct fsnotify_group *group)
9874afeff85SEric Paris {
98894e00d28SAmir Goldstein 	kfree(group->fanotify_data.merge_hash);
9895b8fea65SAmir Goldstein 	if (group->fanotify_data.ucounts)
9905b8fea65SAmir Goldstein 		dec_ucount(group->fanotify_data.ucounts,
9915b8fea65SAmir Goldstein 			   UCOUNT_FANOTIFY_GROUPS);
992734a1a5eSGabriel Krisman Bertazi 
993734a1a5eSGabriel Krisman Bertazi 	if (mempool_initialized(&group->fanotify_data.error_events_pool))
994734a1a5eSGabriel Krisman Bertazi 		mempool_exit(&group->fanotify_data.error_events_pool);
9954afeff85SEric Paris }
9964afeff85SEric Paris 
fanotify_free_path_event(struct fanotify_event * event)9977088f357SJan Kara static void fanotify_free_path_event(struct fanotify_event *event)
9987088f357SJan Kara {
9997088f357SJan Kara 	path_put(fanotify_event_path(event));
10007088f357SJan Kara 	kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
10017088f357SJan Kara }
10027088f357SJan Kara 
fanotify_free_perm_event(struct fanotify_event * event)10037088f357SJan Kara static void fanotify_free_perm_event(struct fanotify_event *event)
10047088f357SJan Kara {
10057088f357SJan Kara 	path_put(fanotify_event_path(event));
10067088f357SJan Kara 	kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
10077088f357SJan Kara }
10087088f357SJan Kara 
fanotify_free_fid_event(struct fanotify_event * event)10097088f357SJan Kara static void fanotify_free_fid_event(struct fanotify_event *event)
10107088f357SJan Kara {
10117088f357SJan Kara 	struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
10127088f357SJan Kara 
10137088f357SJan Kara 	if (fanotify_fh_has_ext_buf(&ffe->object_fh))
10147088f357SJan Kara 		kfree(fanotify_fh_ext_buf(&ffe->object_fh));
10157088f357SJan Kara 	kmem_cache_free(fanotify_fid_event_cachep, ffe);
10167088f357SJan Kara }
10177088f357SJan Kara 
fanotify_free_name_event(struct fanotify_event * event)1018cacfb956SAmir Goldstein static void fanotify_free_name_event(struct fanotify_event *event)
1019cacfb956SAmir Goldstein {
1020f35c4156SAmir Goldstein 	kfree(FANOTIFY_NE(event));
1021cacfb956SAmir Goldstein }
1022cacfb956SAmir Goldstein 
fanotify_free_error_event(struct fsnotify_group * group,struct fanotify_event * event)102383e9acbeSGabriel Krisman Bertazi static void fanotify_free_error_event(struct fsnotify_group *group,
102483e9acbeSGabriel Krisman Bertazi 				      struct fanotify_event *event)
102583e9acbeSGabriel Krisman Bertazi {
102683e9acbeSGabriel Krisman Bertazi 	struct fanotify_error_event *fee = FANOTIFY_EE(event);
102783e9acbeSGabriel Krisman Bertazi 
102883e9acbeSGabriel Krisman Bertazi 	mempool_free(fee, &group->fanotify_data.error_events_pool);
102983e9acbeSGabriel Krisman Bertazi }
103083e9acbeSGabriel Krisman Bertazi 
fanotify_free_event(struct fsnotify_group * group,struct fsnotify_event * fsn_event)1031330ae77dSGabriel Krisman Bertazi static void fanotify_free_event(struct fsnotify_group *group,
1032330ae77dSGabriel Krisman Bertazi 				struct fsnotify_event *fsn_event)
10337053aee2SJan Kara {
103433913997SAmir Goldstein 	struct fanotify_event *event;
10357053aee2SJan Kara 
10367053aee2SJan Kara 	event = FANOTIFY_E(fsn_event);
1037d0a6a87eSAmir Goldstein 	put_pid(event->pid);
10387088f357SJan Kara 	switch (event->type) {
10397088f357SJan Kara 	case FANOTIFY_EVENT_TYPE_PATH:
10407088f357SJan Kara 		fanotify_free_path_event(event);
10417088f357SJan Kara 		break;
10427088f357SJan Kara 	case FANOTIFY_EVENT_TYPE_PATH_PERM:
10437088f357SJan Kara 		fanotify_free_perm_event(event);
10447088f357SJan Kara 		break;
10457088f357SJan Kara 	case FANOTIFY_EVENT_TYPE_FID:
10467088f357SJan Kara 		fanotify_free_fid_event(event);
10477088f357SJan Kara 		break;
1048cacfb956SAmir Goldstein 	case FANOTIFY_EVENT_TYPE_FID_NAME:
1049cacfb956SAmir Goldstein 		fanotify_free_name_event(event);
1050cacfb956SAmir Goldstein 		break;
1051b8a6c3a2SAmir Goldstein 	case FANOTIFY_EVENT_TYPE_OVERFLOW:
1052b8a6c3a2SAmir Goldstein 		kfree(event);
1053b8a6c3a2SAmir Goldstein 		break;
105483e9acbeSGabriel Krisman Bertazi 	case FANOTIFY_EVENT_TYPE_FS_ERROR:
105583e9acbeSGabriel Krisman Bertazi 		fanotify_free_error_event(group, event);
105683e9acbeSGabriel Krisman Bertazi 		break;
10577088f357SJan Kara 	default:
10587088f357SJan Kara 		WARN_ON_ONCE(1);
1059f083441bSJan Kara 	}
10607053aee2SJan Kara }
10617053aee2SJan Kara 
fanotify_freeing_mark(struct fsnotify_mark * mark,struct fsnotify_group * group)10625b8fea65SAmir Goldstein static void fanotify_freeing_mark(struct fsnotify_mark *mark,
10635b8fea65SAmir Goldstein 				  struct fsnotify_group *group)
10645b8fea65SAmir Goldstein {
10655b8fea65SAmir Goldstein 	if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
10665b8fea65SAmir Goldstein 		dec_ucount(group->fanotify_data.ucounts, UCOUNT_FANOTIFY_MARKS);
10675b8fea65SAmir Goldstein }
10685b8fea65SAmir Goldstein 
fanotify_free_mark(struct fsnotify_mark * fsn_mark)1069054c636eSJan Kara static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
1070054c636eSJan Kara {
1071054c636eSJan Kara 	kmem_cache_free(fanotify_mark_cache, fsn_mark);
1072054c636eSJan Kara }
1073054c636eSJan Kara 
1074ff0b16a9SEric Paris const struct fsnotify_ops fanotify_fsnotify_ops = {
1075ff0b16a9SEric Paris 	.handle_event = fanotify_handle_event,
10764afeff85SEric Paris 	.free_group_priv = fanotify_free_group_priv,
10777053aee2SJan Kara 	.free_event = fanotify_free_event,
10785b8fea65SAmir Goldstein 	.freeing_mark = fanotify_freeing_mark,
1079054c636eSJan Kara 	.free_mark = fanotify_free_mark,
1080ff0b16a9SEric Paris };
1081