xref: /linux/fs/notify/fanotify/fanotify.c (revision 0045fb1b)
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_hash_fsid(__kernel_fsid_t * fsid)327e3e5c69SAmir Goldstein static unsigned int fanotify_hash_fsid(__kernel_fsid_t *fsid)
337e3e5c69SAmir Goldstein {
347e3e5c69SAmir Goldstein 	return hash_32(fsid->val[0], FANOTIFY_EVENT_HASH_BITS) ^
357e3e5c69SAmir Goldstein 		hash_32(fsid->val[1], FANOTIFY_EVENT_HASH_BITS);
367e3e5c69SAmir Goldstein }
377e3e5c69SAmir Goldstein 
fanotify_fh_equal(struct fanotify_fh * fh1,struct fanotify_fh * fh2)38afc894c7SJan Kara static bool fanotify_fh_equal(struct fanotify_fh *fh1,
39afc894c7SJan Kara 			      struct fanotify_fh *fh2)
40afc894c7SJan Kara {
41afc894c7SJan Kara 	if (fh1->type != fh2->type || fh1->len != fh2->len)
42afc894c7SJan Kara 		return false;
43afc894c7SJan Kara 
44afc894c7SJan Kara 	return !fh1->len ||
45afc894c7SJan Kara 		!memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
46afc894c7SJan Kara }
47afc894c7SJan Kara 
fanotify_hash_fh(struct fanotify_fh * fh)487e3e5c69SAmir Goldstein static unsigned int fanotify_hash_fh(struct fanotify_fh *fh)
497e3e5c69SAmir Goldstein {
507e3e5c69SAmir Goldstein 	long salt = (long)fh->type | (long)fh->len << 8;
517e3e5c69SAmir Goldstein 
527e3e5c69SAmir Goldstein 	/*
537e3e5c69SAmir Goldstein 	 * full_name_hash() works long by long, so it handles fh buf optimally.
547e3e5c69SAmir Goldstein 	 */
557e3e5c69SAmir Goldstein 	return full_name_hash((void *)salt, fanotify_fh_buf(fh), fh->len);
567e3e5c69SAmir Goldstein }
577e3e5c69SAmir Goldstein 
fanotify_fid_event_equal(struct fanotify_fid_event * ffe1,struct fanotify_fid_event * ffe2)587088f357SJan Kara static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
597088f357SJan Kara 				     struct fanotify_fid_event *ffe2)
607088f357SJan Kara {
617088f357SJan Kara 	/* Do not merge fid events without object fh */
627088f357SJan Kara 	if (!ffe1->object_fh.len)
637088f357SJan Kara 		return false;
647088f357SJan Kara 
657088f357SJan Kara 	return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
667088f357SJan Kara 		fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
677088f357SJan Kara }
687088f357SJan Kara 
fanotify_info_equal(struct fanotify_info * info1,struct fanotify_info * info2)69f454fa61SAmir Goldstein static bool fanotify_info_equal(struct fanotify_info *info1,
70f454fa61SAmir Goldstein 				struct fanotify_info *info2)
71f454fa61SAmir Goldstein {
72f454fa61SAmir Goldstein 	if (info1->dir_fh_totlen != info2->dir_fh_totlen ||
733cf984e9SAmir Goldstein 	    info1->dir2_fh_totlen != info2->dir2_fh_totlen ||
74f454fa61SAmir Goldstein 	    info1->file_fh_totlen != info2->file_fh_totlen ||
753cf984e9SAmir Goldstein 	    info1->name_len != info2->name_len ||
763cf984e9SAmir Goldstein 	    info1->name2_len != info2->name2_len)
77f454fa61SAmir Goldstein 		return false;
78f454fa61SAmir Goldstein 
79f454fa61SAmir Goldstein 	if (info1->dir_fh_totlen &&
80f454fa61SAmir Goldstein 	    !fanotify_fh_equal(fanotify_info_dir_fh(info1),
81f454fa61SAmir Goldstein 			       fanotify_info_dir_fh(info2)))
82f454fa61SAmir Goldstein 		return false;
83f454fa61SAmir Goldstein 
843cf984e9SAmir Goldstein 	if (info1->dir2_fh_totlen &&
853cf984e9SAmir Goldstein 	    !fanotify_fh_equal(fanotify_info_dir2_fh(info1),
863cf984e9SAmir Goldstein 			       fanotify_info_dir2_fh(info2)))
873cf984e9SAmir Goldstein 		return false;
883cf984e9SAmir Goldstein 
89f454fa61SAmir Goldstein 	if (info1->file_fh_totlen &&
90f454fa61SAmir Goldstein 	    !fanotify_fh_equal(fanotify_info_file_fh(info1),
91f454fa61SAmir Goldstein 			       fanotify_info_file_fh(info2)))
92f454fa61SAmir Goldstein 		return false;
93f454fa61SAmir Goldstein 
943cf984e9SAmir Goldstein 	if (info1->name_len &&
953cf984e9SAmir Goldstein 	    memcmp(fanotify_info_name(info1), fanotify_info_name(info2),
963cf984e9SAmir Goldstein 		   info1->name_len))
973cf984e9SAmir Goldstein 		return false;
983cf984e9SAmir Goldstein 
993cf984e9SAmir Goldstein 	return !info1->name2_len ||
1003cf984e9SAmir Goldstein 		!memcmp(fanotify_info_name2(info1), fanotify_info_name2(info2),
1013cf984e9SAmir Goldstein 			info1->name2_len);
102f454fa61SAmir Goldstein }
103f454fa61SAmir Goldstein 
fanotify_name_event_equal(struct fanotify_name_event * fne1,struct fanotify_name_event * fne2)104cacfb956SAmir Goldstein static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
105cacfb956SAmir Goldstein 				      struct fanotify_name_event *fne2)
106cacfb956SAmir Goldstein {
107f454fa61SAmir Goldstein 	struct fanotify_info *info1 = &fne1->info;
108f454fa61SAmir Goldstein 	struct fanotify_info *info2 = &fne2->info;
109f454fa61SAmir Goldstein 
1106ad1aaddSAmir Goldstein 	/* Do not merge name events without dir fh */
111f454fa61SAmir Goldstein 	if (!info1->dir_fh_totlen)
112cacfb956SAmir Goldstein 		return false;
113cacfb956SAmir Goldstein 
1148aed8cebSJan Kara 	if (!fanotify_fsid_equal(&fne1->fsid, &fne2->fsid))
1158aed8cebSJan Kara 		return false;
1168aed8cebSJan Kara 
117f454fa61SAmir Goldstein 	return fanotify_info_equal(info1, info2);
118cacfb956SAmir Goldstein }
119cacfb956SAmir Goldstein 
fanotify_error_event_equal(struct fanotify_error_event * fee1,struct fanotify_error_event * fee2)1208a6ae641SGabriel Krisman Bertazi static bool fanotify_error_event_equal(struct fanotify_error_event *fee1,
1218a6ae641SGabriel Krisman Bertazi 				       struct fanotify_error_event *fee2)
1228a6ae641SGabriel Krisman Bertazi {
1238a6ae641SGabriel Krisman Bertazi 	/* Error events against the same file system are always merged. */
1248a6ae641SGabriel Krisman Bertazi 	if (!fanotify_fsid_equal(&fee1->fsid, &fee2->fsid))
1258a6ae641SGabriel Krisman Bertazi 		return false;
1268a6ae641SGabriel Krisman Bertazi 
1278a6ae641SGabriel Krisman Bertazi 	return true;
1288a6ae641SGabriel Krisman Bertazi }
1298a6ae641SGabriel Krisman Bertazi 
fanotify_should_merge(struct fanotify_event * old,struct fanotify_event * new)1308988f11aSAmir Goldstein static bool fanotify_should_merge(struct fanotify_event *old,
1318988f11aSAmir Goldstein 				  struct fanotify_event *new)
1327053aee2SJan Kara {
1338988f11aSAmir Goldstein 	pr_debug("%s: old=%p new=%p\n", __func__, old, new);
1347053aee2SJan Kara 
1358988f11aSAmir Goldstein 	if (old->hash != new->hash ||
1367088f357SJan Kara 	    old->type != new->type || old->pid != new->pid)
137e9e0c890SAmir Goldstein 		return false;
138e9e0c890SAmir Goldstein 
139e7fce6d9SAmir Goldstein 	/*
140e7fce6d9SAmir Goldstein 	 * We want to merge many dirent events in the same dir (i.e.
141e7fce6d9SAmir Goldstein 	 * creates/unlinks/renames), but we do not want to merge dirent
142e7fce6d9SAmir Goldstein 	 * events referring to subdirs with dirent events referring to
143e7fce6d9SAmir Goldstein 	 * non subdirs, otherwise, user won't be able to tell from a
144e7fce6d9SAmir Goldstein 	 * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
145e7fce6d9SAmir Goldstein 	 * unlink pair or rmdir+create pair of events.
146e7fce6d9SAmir Goldstein 	 */
147afc894c7SJan Kara 	if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
148afc894c7SJan Kara 		return false;
149afc894c7SJan Kara 
1507326e382SAmir Goldstein 	/*
1517326e382SAmir Goldstein 	 * FAN_RENAME event is reported with special info record types,
1527326e382SAmir Goldstein 	 * so we cannot merge it with other events.
1537326e382SAmir Goldstein 	 */
1547326e382SAmir Goldstein 	if ((old->mask & FAN_RENAME) != (new->mask & FAN_RENAME))
1557326e382SAmir Goldstein 		return false;
1567326e382SAmir Goldstein 
157103ff6a5SAmir Goldstein 	switch (old->type) {
158103ff6a5SAmir Goldstein 	case FANOTIFY_EVENT_TYPE_PATH:
159103ff6a5SAmir Goldstein 		return fanotify_path_equal(fanotify_event_path(old),
160103ff6a5SAmir Goldstein 					   fanotify_event_path(new));
161103ff6a5SAmir Goldstein 	case FANOTIFY_EVENT_TYPE_FID:
1627088f357SJan Kara 		return fanotify_fid_event_equal(FANOTIFY_FE(old),
1637088f357SJan Kara 						FANOTIFY_FE(new));
164cacfb956SAmir Goldstein 	case FANOTIFY_EVENT_TYPE_FID_NAME:
165cacfb956SAmir Goldstein 		return fanotify_name_event_equal(FANOTIFY_NE(old),
166cacfb956SAmir Goldstein 						 FANOTIFY_NE(new));
1678a6ae641SGabriel Krisman Bertazi 	case FANOTIFY_EVENT_TYPE_FS_ERROR:
1688a6ae641SGabriel Krisman Bertazi 		return fanotify_error_event_equal(FANOTIFY_EE(old),
1698a6ae641SGabriel Krisman Bertazi 						  FANOTIFY_EE(new));
1707088f357SJan Kara 	default:
1717088f357SJan Kara 		WARN_ON_ONCE(1);
172e9e0c890SAmir Goldstein 	}
173e9e0c890SAmir Goldstein 
174767cd46cSEric Paris 	return false;
175767cd46cSEric Paris }
176767cd46cSEric Paris 
177b8cd0ee8SAmir Goldstein /* Limit event merges to limit CPU overhead per event */
178b8cd0ee8SAmir Goldstein #define FANOTIFY_MAX_MERGE_EVENTS 128
179b8cd0ee8SAmir Goldstein 
180f70ab54cSEric Paris /* and the list better be locked by something too! */
fanotify_merge(struct fsnotify_group * group,struct fsnotify_event * event)18194e00d28SAmir Goldstein static int fanotify_merge(struct fsnotify_group *group,
18294e00d28SAmir Goldstein 			  struct fsnotify_event *event)
183767cd46cSEric Paris {
1848988f11aSAmir Goldstein 	struct fanotify_event *old, *new = FANOTIFY_E(event);
18594e00d28SAmir Goldstein 	unsigned int bucket = fanotify_event_hash_bucket(group, new);
18694e00d28SAmir Goldstein 	struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
187b8cd0ee8SAmir Goldstein 	int i = 0;
188767cd46cSEric Paris 
18994e00d28SAmir Goldstein 	pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
19094e00d28SAmir Goldstein 		 group, event, bucket);
191767cd46cSEric Paris 
19213116dfdSJan Kara 	/*
19313116dfdSJan Kara 	 * Don't merge a permission event with any other event so that we know
19413116dfdSJan Kara 	 * the event structure we have created in fanotify_handle_event() is the
19513116dfdSJan Kara 	 * one we should check for permission response.
19613116dfdSJan Kara 	 */
197a0a92d26SAmir Goldstein 	if (fanotify_is_perm_event(new->mask))
19883c0e1b4SJan Kara 		return 0;
19913116dfdSJan Kara 
20094e00d28SAmir Goldstein 	hlist_for_each_entry(old, hlist, merge_list) {
201b8cd0ee8SAmir Goldstein 		if (++i > FANOTIFY_MAX_MERGE_EVENTS)
202b8cd0ee8SAmir Goldstein 			break;
2038988f11aSAmir Goldstein 		if (fanotify_should_merge(old, new)) {
2048988f11aSAmir Goldstein 			old->mask |= new->mask;
2058a6ae641SGabriel Krisman Bertazi 
2068a6ae641SGabriel Krisman Bertazi 			if (fanotify_is_error_event(old->mask))
2078a6ae641SGabriel Krisman Bertazi 				FANOTIFY_EE(old)->err_count++;
2088a6ae641SGabriel Krisman Bertazi 
20983c0e1b4SJan Kara 			return 1;
2109dced01aSEric Paris 		}
2116c71100dSKinglong Mee 	}
2126c71100dSKinglong Mee 
2136c71100dSKinglong Mee 	return 0;
2146c71100dSKinglong Mee }
2159dced01aSEric Paris 
216fabf7f29SJan Kara /*
217fabf7f29SJan Kara  * Wait for response to permission event. The function also takes care of
218fabf7f29SJan Kara  * freeing the permission event (or offloads that in case the wait is canceled
219fabf7f29SJan Kara  * by a signal). The function returns 0 in case access got allowed by userspace,
220fabf7f29SJan Kara  * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
221fabf7f29SJan Kara  * the wait got interrupted by a signal.
222fabf7f29SJan Kara  */
fanotify_get_response(struct fsnotify_group * group,struct fanotify_perm_event * event,struct fsnotify_iter_info * iter_info)223f083441bSJan Kara static int fanotify_get_response(struct fsnotify_group *group,
22433913997SAmir Goldstein 				 struct fanotify_perm_event *event,
22505f0e387SJan Kara 				 struct fsnotify_iter_info *iter_info)
2269e66e423SEric Paris {
2279e66e423SEric Paris 	int ret;
2289e66e423SEric Paris 
2299e66e423SEric Paris 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
2309e66e423SEric Paris 
231*0045fb1bSWinston Wen 	ret = wait_event_state(group->fanotify_data.access_waitq,
232*0045fb1bSWinston Wen 				  event->state == FAN_EVENT_ANSWERED,
233*0045fb1bSWinston Wen 				  (TASK_KILLABLE|TASK_FREEZABLE));
234*0045fb1bSWinston Wen 
235fabf7f29SJan Kara 	/* Signal pending? */
236fabf7f29SJan Kara 	if (ret < 0) {
237fabf7f29SJan Kara 		spin_lock(&group->notification_lock);
238fabf7f29SJan Kara 		/* Event reported to userspace and no answer yet? */
239fabf7f29SJan Kara 		if (event->state == FAN_EVENT_REPORTED) {
240fabf7f29SJan Kara 			/* Event will get freed once userspace answers to it */
241fabf7f29SJan Kara 			event->state = FAN_EVENT_CANCELED;
242fabf7f29SJan Kara 			spin_unlock(&group->notification_lock);
243fabf7f29SJan Kara 			return ret;
244fabf7f29SJan Kara 		}
245fabf7f29SJan Kara 		/* Event not yet reported? Just remove it. */
24694e00d28SAmir Goldstein 		if (event->state == FAN_EVENT_INIT) {
247fabf7f29SJan Kara 			fsnotify_remove_queued_event(group, &event->fae.fse);
24894e00d28SAmir Goldstein 			/* Permission events are not supposed to be hashed */
24994e00d28SAmir Goldstein 			WARN_ON_ONCE(!hlist_unhashed(&event->fae.merge_list));
25094e00d28SAmir Goldstein 		}
251fabf7f29SJan Kara 		/*
252fabf7f29SJan Kara 		 * Event may be also answered in case signal delivery raced
253fabf7f29SJan Kara 		 * with wakeup. In that case we have nothing to do besides
254fabf7f29SJan Kara 		 * freeing the event and reporting error.
255fabf7f29SJan Kara 		 */
256fabf7f29SJan Kara 		spin_unlock(&group->notification_lock);
257fabf7f29SJan Kara 		goto out;
258fabf7f29SJan Kara 	}
2599e66e423SEric Paris 
2609e66e423SEric Paris 	/* userspace responded, convert to something usable */
26170529a19SRichard Guy Briggs 	switch (event->response & FANOTIFY_RESPONSE_ACCESS) {
2629e66e423SEric Paris 	case FAN_ALLOW:
2639e66e423SEric Paris 		ret = 0;
2649e66e423SEric Paris 		break;
2659e66e423SEric Paris 	case FAN_DENY:
2669e66e423SEric Paris 	default:
2679e66e423SEric Paris 		ret = -EPERM;
2689e66e423SEric Paris 	}
269de8cd83eSSteve Grubb 
270de8cd83eSSteve Grubb 	/* Check if the response should be audited */
271de8cd83eSSteve Grubb 	if (event->response & FAN_AUDIT)
272032bffd4SRichard Guy Briggs 		audit_fanotify(event->response & ~FAN_AUDIT,
273032bffd4SRichard Guy Briggs 			       &event->audit_rule);
274de8cd83eSSteve Grubb 
275b2d87909SEric Paris 	pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
276b2d87909SEric Paris 		 group, event, ret);
277fabf7f29SJan Kara out:
278fabf7f29SJan Kara 	fsnotify_destroy_event(group, &event->fae.fse);
279b2d87909SEric Paris 
2809e66e423SEric Paris 	return ret;
2819e66e423SEric Paris }
2829e66e423SEric Paris 
2832d10b230SMatthew Bobrowski /*
2842d10b230SMatthew Bobrowski  * This function returns a mask for an event that only contains the flags
2852d10b230SMatthew Bobrowski  * that have been specifically requested by the user. Flags that may have
2862d10b230SMatthew Bobrowski  * been included within the event mask, but have not been explicitly
2872d10b230SMatthew Bobrowski  * requested by the user, will not be present in the returned mask.
2882d10b230SMatthew 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)28983b535d2SAmir Goldstein static u32 fanotify_group_event_mask(struct fsnotify_group *group,
29083b535d2SAmir Goldstein 				     struct fsnotify_iter_info *iter_info,
2912bfbcccdSAmir Goldstein 				     u32 *match_mask, u32 event_mask,
2922bfbcccdSAmir Goldstein 				     const void *data, int data_type,
2932bfbcccdSAmir Goldstein 				     struct inode *dir)
2941c529063SEric Paris {
29531a371e4SAmir Goldstein 	__u32 marks_mask = 0, marks_ignore_mask = 0;
2960badfa02SAmir Goldstein 	__u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS |
2970badfa02SAmir Goldstein 				     FANOTIFY_EVENT_FLAGS;
298aa93bdc5SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
299d809daf1SAmir Goldstein 	unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
300837a3934SAmir Goldstein 	struct fsnotify_mark *mark;
30131a371e4SAmir Goldstein 	bool ondir = event_mask & FAN_ONDIR;
302837a3934SAmir Goldstein 	int type;
3031968f5eeSEric Paris 
304837a3934SAmir Goldstein 	pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
305837a3934SAmir Goldstein 		 __func__, iter_info->report_mask, event_mask, data, data_type);
3061968f5eeSEric Paris 
307d809daf1SAmir Goldstein 	if (!fid_mode) {
30883b535d2SAmir Goldstein 		/* Do we have path to open a file descriptor? */
309aa93bdc5SAmir Goldstein 		if (!path)
3102d10b230SMatthew Bobrowski 			return 0;
31183b535d2SAmir Goldstein 		/* Path type events are only relevant for files and dirs */
31283b535d2SAmir Goldstein 		if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
3132d10b230SMatthew Bobrowski 			return 0;
31483b7a598SAmir Goldstein 	} else if (!(fid_mode & FAN_REPORT_FID)) {
31583b7a598SAmir Goldstein 		/* Do we have a directory inode to report? */
31631a371e4SAmir Goldstein 		if (!dir && !ondir)
31783b7a598SAmir Goldstein 			return 0;
31883b535d2SAmir Goldstein 	}
319e1c048baSEric Paris 
32014362a25SAmir Goldstein 	fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
32131a371e4SAmir Goldstein 		/*
32231a371e4SAmir Goldstein 		 * Apply ignore mask depending on event flags in ignore mask.
32331a371e4SAmir Goldstein 		 */
32431a371e4SAmir Goldstein 		marks_ignore_mask |=
32531a371e4SAmir Goldstein 			fsnotify_effective_ignore_mask(mark, ondir, type);
3262f02fd3fSAmir Goldstein 
3271968f5eeSEric Paris 		/*
32831a371e4SAmir Goldstein 		 * Send the event depending on event flags in mark mask.
32955bf882cSAmir Goldstein 		 */
33031a371e4SAmir Goldstein 		if (!fsnotify_mask_applicable(mark->mask, ondir, type))
33155bf882cSAmir Goldstein 			continue;
33255bf882cSAmir Goldstein 
333837a3934SAmir Goldstein 		marks_mask |= mark->mask;
3342bfbcccdSAmir Goldstein 
3352bfbcccdSAmir Goldstein 		/* Record the mark types of this group that matched the event */
3362bfbcccdSAmir Goldstein 		*match_mask |= 1U << type;
3371968f5eeSEric Paris 	}
3381968f5eeSEric Paris 
33931a371e4SAmir Goldstein 	test_mask = event_mask & marks_mask & ~marks_ignore_mask;
340e7fce6d9SAmir Goldstein 
341e7fce6d9SAmir Goldstein 	/*
3429e2ba2c3SAmir Goldstein 	 * For dirent modification events (create/delete/move) that do not carry
3439e2ba2c3SAmir Goldstein 	 * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
3449e2ba2c3SAmir Goldstein 	 * so user can differentiate them from creat/unlink.
345e7fce6d9SAmir Goldstein 	 *
346e7fce6d9SAmir Goldstein 	 * For backward compatibility and consistency, do not report FAN_ONDIR
347e7fce6d9SAmir Goldstein 	 * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
3480badfa02SAmir Goldstein 	 * to user in fid mode for all event types.
3490badfa02SAmir Goldstein 	 *
3500badfa02SAmir Goldstein 	 * We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to
3510badfa02SAmir Goldstein 	 * fanotify_alloc_event() when group is reporting fid as indication
3520badfa02SAmir Goldstein 	 * that event happened on child.
353e7fce6d9SAmir Goldstein 	 */
354d809daf1SAmir Goldstein 	if (fid_mode) {
3550badfa02SAmir Goldstein 		/* Do not report event flags without any event */
3560badfa02SAmir Goldstein 		if (!(test_mask & ~FANOTIFY_EVENT_FLAGS))
357e7fce6d9SAmir Goldstein 			return 0;
358e7fce6d9SAmir Goldstein 	} else {
3590badfa02SAmir Goldstein 		user_mask &= ~FANOTIFY_EVENT_FLAGS;
360e7fce6d9SAmir Goldstein 	}
361e7fce6d9SAmir Goldstein 
362e7fce6d9SAmir Goldstein 	return test_mask & user_mask;
3631c529063SEric Paris }
3641c529063SEric Paris 
365f454fa61SAmir Goldstein /*
366f35c4156SAmir Goldstein  * Check size needed to encode fanotify_fh.
367f35c4156SAmir Goldstein  *
368f35c4156SAmir Goldstein  * Return size of encoded fh without fanotify_fh header.
369f35c4156SAmir Goldstein  * Return 0 on failure to encode.
370f35c4156SAmir Goldstein  */
fanotify_encode_fh_len(struct inode * inode)371f35c4156SAmir Goldstein static int fanotify_encode_fh_len(struct inode *inode)
372f35c4156SAmir Goldstein {
373f35c4156SAmir Goldstein 	int dwords = 0;
374572c28f2SGabriel Krisman Bertazi 	int fh_len;
375f35c4156SAmir Goldstein 
376f35c4156SAmir Goldstein 	if (!inode)
377f35c4156SAmir Goldstein 		return 0;
378f35c4156SAmir Goldstein 
379a95aef69SAmir Goldstein 	exportfs_encode_fid(inode, NULL, &dwords);
380572c28f2SGabriel Krisman Bertazi 	fh_len = dwords << 2;
381f35c4156SAmir Goldstein 
382572c28f2SGabriel Krisman Bertazi 	/*
383572c28f2SGabriel Krisman Bertazi 	 * struct fanotify_error_event might be preallocated and is
384572c28f2SGabriel Krisman Bertazi 	 * limited to MAX_HANDLE_SZ.  This should never happen, but
385572c28f2SGabriel Krisman Bertazi 	 * safeguard by forcing an invalid file handle.
386572c28f2SGabriel Krisman Bertazi 	 */
387572c28f2SGabriel Krisman Bertazi 	if (WARN_ON_ONCE(fh_len > MAX_HANDLE_SZ))
388572c28f2SGabriel Krisman Bertazi 		return 0;
389572c28f2SGabriel Krisman Bertazi 
390572c28f2SGabriel Krisman Bertazi 	return fh_len;
391f35c4156SAmir Goldstein }
392f35c4156SAmir Goldstein 
393f35c4156SAmir Goldstein /*
394f454fa61SAmir Goldstein  * Encode fanotify_fh.
395f454fa61SAmir Goldstein  *
396f454fa61SAmir Goldstein  * Return total size of encoded fh including fanotify_fh header.
397f454fa61SAmir Goldstein  * Return 0 on failure to encode.
398f454fa61SAmir Goldstein  */
fanotify_encode_fh(struct fanotify_fh * fh,struct inode * inode,unsigned int fh_len,unsigned int * hash,gfp_t gfp)399f454fa61SAmir Goldstein static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
4007e3e5c69SAmir Goldstein 			      unsigned int fh_len, unsigned int *hash,
4017e3e5c69SAmir Goldstein 			      gfp_t gfp)
402e9e0c890SAmir Goldstein {
403f35c4156SAmir Goldstein 	int dwords, type = 0;
404afc894c7SJan Kara 	char *ext_buf = NULL;
405afc894c7SJan Kara 	void *buf = fh->buf;
406afc894c7SJan Kara 	int err;
407e9e0c890SAmir Goldstein 
4086ad1aaddSAmir Goldstein 	fh->type = FILEID_ROOT;
4096ad1aaddSAmir Goldstein 	fh->len = 0;
410f35c4156SAmir Goldstein 	fh->flags = 0;
411272531acSGabriel Krisman Bertazi 
412272531acSGabriel Krisman Bertazi 	/*
413272531acSGabriel Krisman Bertazi 	 * Invalid FHs are used by FAN_FS_ERROR for errors not
414272531acSGabriel Krisman Bertazi 	 * linked to any inode. The f_handle won't be reported
415272531acSGabriel Krisman Bertazi 	 * back to userspace.
416272531acSGabriel Krisman Bertazi 	 */
417cacfb956SAmir Goldstein 	if (!inode)
418272531acSGabriel Krisman Bertazi 		goto out;
419cacfb956SAmir Goldstein 
420f35c4156SAmir Goldstein 	/*
421f35c4156SAmir Goldstein 	 * !gpf means preallocated variable size fh, but fh_len could
422f35c4156SAmir Goldstein 	 * be zero in that case if encoding fh len failed.
423f35c4156SAmir Goldstein 	 */
424e9e0c890SAmir Goldstein 	err = -ENOENT;
4252d9374f0SAmir Goldstein 	if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4) || fh_len > MAX_HANDLE_SZ)
426e9e0c890SAmir Goldstein 		goto out_err;
427e9e0c890SAmir Goldstein 
428f35c4156SAmir Goldstein 	/* No external buffer in a variable size allocated fh */
429f35c4156SAmir Goldstein 	if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) {
430f35c4156SAmir Goldstein 		/* Treat failure to allocate fh as failure to encode fh */
431e9e0c890SAmir Goldstein 		err = -ENOMEM;
432f35c4156SAmir Goldstein 		ext_buf = kmalloc(fh_len, gfp);
433afc894c7SJan Kara 		if (!ext_buf)
434e9e0c890SAmir Goldstein 			goto out_err;
435afc894c7SJan Kara 
436afc894c7SJan Kara 		*fanotify_fh_ext_buf_ptr(fh) = ext_buf;
437afc894c7SJan Kara 		buf = ext_buf;
438f35c4156SAmir Goldstein 		fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF;
439e9e0c890SAmir Goldstein 	}
440e9e0c890SAmir Goldstein 
441f35c4156SAmir Goldstein 	dwords = fh_len >> 2;
442a95aef69SAmir Goldstein 	type = exportfs_encode_fid(inode, buf, &dwords);
443e9e0c890SAmir Goldstein 	err = -EINVAL;
4447cdafe6cSAmir Goldstein 	if (type <= 0 || type == FILEID_INVALID || fh_len != dwords << 2)
445e9e0c890SAmir Goldstein 		goto out_err;
446e9e0c890SAmir Goldstein 
447afc894c7SJan Kara 	fh->type = type;
448f35c4156SAmir Goldstein 	fh->len = fh_len;
449e9e0c890SAmir Goldstein 
450272531acSGabriel Krisman Bertazi out:
45174fe4734SGabriel Krisman Bertazi 	/*
45274fe4734SGabriel Krisman Bertazi 	 * Mix fh into event merge key.  Hash might be NULL in case of
45374fe4734SGabriel Krisman Bertazi 	 * unhashed FID events (i.e. FAN_FS_ERROR).
45474fe4734SGabriel Krisman Bertazi 	 */
45574fe4734SGabriel Krisman Bertazi 	if (hash)
4567e3e5c69SAmir Goldstein 		*hash ^= fanotify_hash_fh(fh);
4577e3e5c69SAmir Goldstein 
458f35c4156SAmir Goldstein 	return FANOTIFY_FH_HDR_LEN + fh_len;
459e9e0c890SAmir Goldstein 
460e9e0c890SAmir Goldstein out_err:
461afc894c7SJan Kara 	pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
462f35c4156SAmir Goldstein 			    type, fh_len, err);
463afc894c7SJan Kara 	kfree(ext_buf);
464afc894c7SJan Kara 	*fanotify_fh_ext_buf_ptr(fh) = NULL;
465afc894c7SJan Kara 	/* Report the event without a file identifier on encode error */
466afc894c7SJan Kara 	fh->type = FILEID_INVALID;
467afc894c7SJan Kara 	fh->len = 0;
468f454fa61SAmir Goldstein 	return 0;
469e9e0c890SAmir Goldstein }
470e9e0c890SAmir Goldstein 
47183b535d2SAmir Goldstein /*
472d61fd650SAmir Goldstein  * FAN_REPORT_FID is ambiguous in that it reports the fid of the child for
473d61fd650SAmir Goldstein  * some events and the fid of the parent for create/delete/move events.
474d61fd650SAmir Goldstein  *
475d61fd650SAmir Goldstein  * With the FAN_REPORT_TARGET_FID flag, the fid of the child is reported
476d61fd650SAmir Goldstein  * also in create/delete/move events in addition to the fid of the parent
477d61fd650SAmir Goldstein  * and the name of the child.
478d61fd650SAmir Goldstein  */
fanotify_report_child_fid(unsigned int fid_mode,u32 mask)479d61fd650SAmir Goldstein static inline bool fanotify_report_child_fid(unsigned int fid_mode, u32 mask)
480d61fd650SAmir Goldstein {
481d61fd650SAmir Goldstein 	if (mask & ALL_FSNOTIFY_DIRENT_EVENTS)
482d61fd650SAmir Goldstein 		return (fid_mode & FAN_REPORT_TARGET_FID);
483d61fd650SAmir Goldstein 
484d61fd650SAmir Goldstein 	return (fid_mode & FAN_REPORT_FID) && !(mask & FAN_ONDIR);
485d61fd650SAmir Goldstein }
486d61fd650SAmir Goldstein 
487d61fd650SAmir Goldstein /*
488d61fd650SAmir Goldstein  * The inode to use as identifier when reporting fid depends on the event
489d61fd650SAmir Goldstein  * and the group flags.
490d61fd650SAmir Goldstein  *
491d61fd650SAmir Goldstein  * With the group flag FAN_REPORT_TARGET_FID, always report the child fid.
492d61fd650SAmir Goldstein  *
493d61fd650SAmir Goldstein  * Without the group flag FAN_REPORT_TARGET_FID, report the modified directory
494d61fd650SAmir Goldstein  * fid on dirent events and the child fid otherwise.
495d61fd650SAmir Goldstein  *
49683b535d2SAmir Goldstein  * For example:
497d61fd650SAmir Goldstein  * FS_ATTRIB reports the child fid even if reported on a watched parent.
498d61fd650SAmir Goldstein  * FS_CREATE reports the modified dir fid without FAN_REPORT_TARGET_FID.
499d61fd650SAmir Goldstein  *       and reports the created child fid with FAN_REPORT_TARGET_FID.
50083b535d2SAmir Goldstein  */
fanotify_fid_inode(u32 event_mask,const void * data,int data_type,struct inode * dir,unsigned int fid_mode)501b54cecf5SAmir Goldstein static struct inode *fanotify_fid_inode(u32 event_mask, const void *data,
502d61fd650SAmir Goldstein 					int data_type, struct inode *dir,
503d61fd650SAmir Goldstein 					unsigned int fid_mode)
50483b535d2SAmir Goldstein {
505d61fd650SAmir Goldstein 	if ((event_mask & ALL_FSNOTIFY_DIRENT_EVENTS) &&
506d61fd650SAmir Goldstein 	    !(fid_mode & FAN_REPORT_TARGET_FID))
507b54cecf5SAmir Goldstein 		return dir;
508aa93bdc5SAmir Goldstein 
509cbcf47adSAmir Goldstein 	return fsnotify_data_inode(data, data_type);
51083b535d2SAmir Goldstein }
51183b535d2SAmir Goldstein 
51283b7a598SAmir Goldstein /*
51383b7a598SAmir Goldstein  * The inode to use as identifier when reporting dir fid depends on the event.
51483b7a598SAmir Goldstein  * Report the modified directory inode on dirent modification events.
51583b7a598SAmir Goldstein  * Report the "victim" inode if "victim" is a directory.
51683b7a598SAmir Goldstein  * Report the parent inode if "victim" is not a directory and event is
51783b7a598SAmir Goldstein  * reported to parent.
51883b7a598SAmir Goldstein  * Otherwise, do not report dir fid.
51983b7a598SAmir Goldstein  */
fanotify_dfid_inode(u32 event_mask,const void * data,int data_type,struct inode * dir)52083b7a598SAmir Goldstein static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data,
52183b7a598SAmir Goldstein 					 int data_type, struct inode *dir)
52283b7a598SAmir Goldstein {
52383b7a598SAmir Goldstein 	struct inode *inode = fsnotify_data_inode(data, data_type);
52483b7a598SAmir Goldstein 
52583b7a598SAmir Goldstein 	if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
52683b7a598SAmir Goldstein 		return dir;
52783b7a598SAmir Goldstein 
52812f47bf0SGabriel Krisman Bertazi 	if (inode && S_ISDIR(inode->i_mode))
52983b7a598SAmir Goldstein 		return inode;
53083b7a598SAmir Goldstein 
53183b7a598SAmir Goldstein 	return dir;
53283b7a598SAmir Goldstein }
53383b7a598SAmir Goldstein 
fanotify_alloc_path_event(const struct path * path,unsigned int * hash,gfp_t gfp)5349c61f3b5SAmir Goldstein static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
5357e3e5c69SAmir Goldstein 							unsigned int *hash,
5369c61f3b5SAmir Goldstein 							gfp_t gfp)
5379c61f3b5SAmir Goldstein {
5389c61f3b5SAmir Goldstein 	struct fanotify_path_event *pevent;
5399c61f3b5SAmir Goldstein 
5409c61f3b5SAmir Goldstein 	pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
5419c61f3b5SAmir Goldstein 	if (!pevent)
5429c61f3b5SAmir Goldstein 		return NULL;
5439c61f3b5SAmir Goldstein 
5449c61f3b5SAmir Goldstein 	pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
5459c61f3b5SAmir Goldstein 	pevent->path = *path;
5467e3e5c69SAmir Goldstein 	*hash ^= fanotify_hash_path(path);
5479c61f3b5SAmir Goldstein 	path_get(path);
5489c61f3b5SAmir Goldstein 
5499c61f3b5SAmir Goldstein 	return &pevent->fae;
5509c61f3b5SAmir Goldstein }
5519c61f3b5SAmir Goldstein 
fanotify_alloc_perm_event(const struct path * path,gfp_t gfp)5529c61f3b5SAmir Goldstein static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
5539c61f3b5SAmir Goldstein 							gfp_t gfp)
5549c61f3b5SAmir Goldstein {
5559c61f3b5SAmir Goldstein 	struct fanotify_perm_event *pevent;
5569c61f3b5SAmir Goldstein 
5579c61f3b5SAmir Goldstein 	pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
5589c61f3b5SAmir Goldstein 	if (!pevent)
5599c61f3b5SAmir Goldstein 		return NULL;
5609c61f3b5SAmir Goldstein 
5619c61f3b5SAmir Goldstein 	pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
5629c61f3b5SAmir Goldstein 	pevent->response = 0;
56370529a19SRichard Guy Briggs 	pevent->hdr.type = FAN_RESPONSE_INFO_NONE;
56470529a19SRichard Guy Briggs 	pevent->hdr.pad = 0;
56570529a19SRichard Guy Briggs 	pevent->hdr.len = 0;
5669c61f3b5SAmir Goldstein 	pevent->state = FAN_EVENT_INIT;
5679c61f3b5SAmir Goldstein 	pevent->path = *path;
5689c61f3b5SAmir Goldstein 	path_get(path);
5699c61f3b5SAmir Goldstein 
5709c61f3b5SAmir Goldstein 	return &pevent->fae;
5719c61f3b5SAmir Goldstein }
5729c61f3b5SAmir Goldstein 
fanotify_alloc_fid_event(struct inode * id,__kernel_fsid_t * fsid,unsigned int * hash,gfp_t gfp)5739c61f3b5SAmir Goldstein static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
5749c61f3b5SAmir Goldstein 						       __kernel_fsid_t *fsid,
5757e3e5c69SAmir Goldstein 						       unsigned int *hash,
5769c61f3b5SAmir Goldstein 						       gfp_t gfp)
5779c61f3b5SAmir Goldstein {
5789c61f3b5SAmir Goldstein 	struct fanotify_fid_event *ffe;
5799c61f3b5SAmir Goldstein 
5809c61f3b5SAmir Goldstein 	ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
5819c61f3b5SAmir Goldstein 	if (!ffe)
5829c61f3b5SAmir Goldstein 		return NULL;
5839c61f3b5SAmir Goldstein 
5849c61f3b5SAmir Goldstein 	ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
5859c61f3b5SAmir Goldstein 	ffe->fsid = *fsid;
5867e3e5c69SAmir Goldstein 	*hash ^= fanotify_hash_fsid(fsid);
587f35c4156SAmir Goldstein 	fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id),
5887e3e5c69SAmir Goldstein 			   hash, gfp);
5899c61f3b5SAmir Goldstein 
5909c61f3b5SAmir Goldstein 	return &ffe->fae;
5919c61f3b5SAmir Goldstein }
5929c61f3b5SAmir 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)5931a9515acSAmir Goldstein static struct fanotify_event *fanotify_alloc_name_event(struct inode *dir,
5949c61f3b5SAmir Goldstein 							__kernel_fsid_t *fsid,
5957e3e5c69SAmir Goldstein 							const struct qstr *name,
5967e8283afSAmir Goldstein 							struct inode *child,
5973982534bSAmir Goldstein 							struct dentry *moved,
5987e3e5c69SAmir Goldstein 							unsigned int *hash,
5999c61f3b5SAmir Goldstein 							gfp_t gfp)
6009c61f3b5SAmir Goldstein {
6019c61f3b5SAmir Goldstein 	struct fanotify_name_event *fne;
602f454fa61SAmir Goldstein 	struct fanotify_info *info;
6037e8283afSAmir Goldstein 	struct fanotify_fh *dfh, *ffh;
6043982534bSAmir Goldstein 	struct inode *dir2 = moved ? d_inode(moved->d_parent) : NULL;
6053982534bSAmir Goldstein 	const struct qstr *name2 = moved ? &moved->d_name : NULL;
6061a9515acSAmir Goldstein 	unsigned int dir_fh_len = fanotify_encode_fh_len(dir);
6073982534bSAmir Goldstein 	unsigned int dir2_fh_len = fanotify_encode_fh_len(dir2);
6087e8283afSAmir Goldstein 	unsigned int child_fh_len = fanotify_encode_fh_len(child);
6091a9515acSAmir Goldstein 	unsigned long name_len = name ? name->len : 0;
6103982534bSAmir Goldstein 	unsigned long name2_len = name2 ? name2->len : 0;
6111a9515acSAmir Goldstein 	unsigned int len, size;
6129c61f3b5SAmir Goldstein 
6131a9515acSAmir Goldstein 	/* Reserve terminating null byte even for empty name */
6143982534bSAmir Goldstein 	size = sizeof(*fne) + name_len + name2_len + 2;
6151a9515acSAmir Goldstein 	if (dir_fh_len)
6161a9515acSAmir Goldstein 		size += FANOTIFY_FH_HDR_LEN + dir_fh_len;
6173982534bSAmir Goldstein 	if (dir2_fh_len)
6183982534bSAmir Goldstein 		size += FANOTIFY_FH_HDR_LEN + dir2_fh_len;
6197e8283afSAmir Goldstein 	if (child_fh_len)
6207e8283afSAmir Goldstein 		size += FANOTIFY_FH_HDR_LEN + child_fh_len;
621f35c4156SAmir Goldstein 	fne = kmalloc(size, gfp);
6229c61f3b5SAmir Goldstein 	if (!fne)
6239c61f3b5SAmir Goldstein 		return NULL;
6249c61f3b5SAmir Goldstein 
6259c61f3b5SAmir Goldstein 	fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
6269c61f3b5SAmir Goldstein 	fne->fsid = *fsid;
6277e3e5c69SAmir Goldstein 	*hash ^= fanotify_hash_fsid(fsid);
628f454fa61SAmir Goldstein 	info = &fne->info;
629f454fa61SAmir Goldstein 	fanotify_info_init(info);
6301a9515acSAmir Goldstein 	if (dir_fh_len) {
631f454fa61SAmir Goldstein 		dfh = fanotify_info_dir_fh(info);
6321a9515acSAmir Goldstein 		len = fanotify_encode_fh(dfh, dir, dir_fh_len, hash, 0);
6331a9515acSAmir Goldstein 		fanotify_info_set_dir_fh(info, len);
6341a9515acSAmir Goldstein 	}
6353982534bSAmir Goldstein 	if (dir2_fh_len) {
6363982534bSAmir Goldstein 		dfh = fanotify_info_dir2_fh(info);
6373982534bSAmir Goldstein 		len = fanotify_encode_fh(dfh, dir2, dir2_fh_len, hash, 0);
6383982534bSAmir Goldstein 		fanotify_info_set_dir2_fh(info, len);
6393982534bSAmir Goldstein 	}
6407e8283afSAmir Goldstein 	if (child_fh_len) {
6417e8283afSAmir Goldstein 		ffh = fanotify_info_file_fh(info);
6421a9515acSAmir Goldstein 		len = fanotify_encode_fh(ffh, child, child_fh_len, hash, 0);
6431a9515acSAmir Goldstein 		fanotify_info_set_file_fh(info, len);
6447e8283afSAmir Goldstein 	}
6451a9515acSAmir Goldstein 	if (name_len) {
6467e3e5c69SAmir Goldstein 		fanotify_info_copy_name(info, name);
6471a9515acSAmir Goldstein 		*hash ^= full_name_hash((void *)name_len, name->name, name_len);
6487e3e5c69SAmir Goldstein 	}
6493982534bSAmir Goldstein 	if (name2_len) {
6503982534bSAmir Goldstein 		fanotify_info_copy_name2(info, name2);
6513982534bSAmir Goldstein 		*hash ^= full_name_hash((void *)name2_len, name2->name,
6523982534bSAmir Goldstein 					name2_len);
6533982534bSAmir Goldstein 	}
6549c61f3b5SAmir Goldstein 
6551a9515acSAmir Goldstein 	pr_debug("%s: size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n",
6561a9515acSAmir Goldstein 		 __func__, size, dir_fh_len, child_fh_len,
657f35c4156SAmir Goldstein 		 info->name_len, info->name_len, fanotify_info_name(info));
658f35c4156SAmir Goldstein 
6593982534bSAmir Goldstein 	if (dir2_fh_len) {
6603982534bSAmir Goldstein 		pr_debug("%s: dir2_fh_len=%u name2_len=%u name2='%.*s'\n",
6613982534bSAmir Goldstein 			 __func__, dir2_fh_len, info->name2_len,
6623982534bSAmir Goldstein 			 info->name2_len, fanotify_info_name2(info));
6633982534bSAmir Goldstein 	}
6643982534bSAmir Goldstein 
6659c61f3b5SAmir Goldstein 	return &fne->fae;
6669c61f3b5SAmir Goldstein }
6679c61f3b5SAmir Goldstein 
fanotify_alloc_error_event(struct fsnotify_group * group,__kernel_fsid_t * fsid,const void * data,int data_type,unsigned int * hash)66883e9acbeSGabriel Krisman Bertazi static struct fanotify_event *fanotify_alloc_error_event(
66983e9acbeSGabriel Krisman Bertazi 						struct fsnotify_group *group,
67083e9acbeSGabriel Krisman Bertazi 						__kernel_fsid_t *fsid,
6718a6ae641SGabriel Krisman Bertazi 						const void *data, int data_type,
6728a6ae641SGabriel Krisman Bertazi 						unsigned int *hash)
67383e9acbeSGabriel Krisman Bertazi {
67483e9acbeSGabriel Krisman Bertazi 	struct fs_error_report *report =
67583e9acbeSGabriel Krisman Bertazi 			fsnotify_data_error_report(data, data_type);
676936d6a38SGabriel Krisman Bertazi 	struct inode *inode;
67783e9acbeSGabriel Krisman Bertazi 	struct fanotify_error_event *fee;
678936d6a38SGabriel Krisman Bertazi 	int fh_len;
67983e9acbeSGabriel Krisman Bertazi 
68083e9acbeSGabriel Krisman Bertazi 	if (WARN_ON_ONCE(!report))
68183e9acbeSGabriel Krisman Bertazi 		return NULL;
68283e9acbeSGabriel Krisman Bertazi 
68383e9acbeSGabriel Krisman Bertazi 	fee = mempool_alloc(&group->fanotify_data.error_events_pool, GFP_NOFS);
68483e9acbeSGabriel Krisman Bertazi 	if (!fee)
68583e9acbeSGabriel Krisman Bertazi 		return NULL;
68683e9acbeSGabriel Krisman Bertazi 
68783e9acbeSGabriel Krisman Bertazi 	fee->fae.type = FANOTIFY_EVENT_TYPE_FS_ERROR;
688130a3c74SGabriel Krisman Bertazi 	fee->error = report->error;
6898a6ae641SGabriel Krisman Bertazi 	fee->err_count = 1;
6908a6ae641SGabriel Krisman Bertazi 	fee->fsid = *fsid;
6918a6ae641SGabriel Krisman Bertazi 
692936d6a38SGabriel Krisman Bertazi 	inode = report->inode;
693936d6a38SGabriel Krisman Bertazi 	fh_len = fanotify_encode_fh_len(inode);
694936d6a38SGabriel Krisman Bertazi 
695936d6a38SGabriel Krisman Bertazi 	/* Bad fh_len. Fallback to using an invalid fh. Should never happen. */
696936d6a38SGabriel Krisman Bertazi 	if (!fh_len && inode)
697936d6a38SGabriel Krisman Bertazi 		inode = NULL;
698936d6a38SGabriel Krisman Bertazi 
699936d6a38SGabriel Krisman Bertazi 	fanotify_encode_fh(&fee->object_fh, inode, fh_len, NULL, 0);
700936d6a38SGabriel Krisman Bertazi 
7018a6ae641SGabriel Krisman Bertazi 	*hash ^= fanotify_hash_fsid(fsid);
70283e9acbeSGabriel Krisman Bertazi 
70383e9acbeSGabriel Krisman Bertazi 	return &fee->fae;
70483e9acbeSGabriel Krisman Bertazi }
70583e9acbeSGabriel 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)7062bfbcccdSAmir Goldstein static struct fanotify_event *fanotify_alloc_event(
7072bfbcccdSAmir Goldstein 				struct fsnotify_group *group,
7082bfbcccdSAmir Goldstein 				u32 mask, const void *data, int data_type,
7092bfbcccdSAmir Goldstein 				struct inode *dir, const struct qstr *file_name,
7102bfbcccdSAmir Goldstein 				__kernel_fsid_t *fsid, u32 match_mask)
711f083441bSJan Kara {
71233913997SAmir Goldstein 	struct fanotify_event *event = NULL;
713d46eb14bSShakeel Butt 	gfp_t gfp = GFP_KERNEL_ACCOUNT;
714d61fd650SAmir Goldstein 	unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
715d61fd650SAmir Goldstein 	struct inode *id = fanotify_fid_inode(mask, data, data_type, dir,
716d61fd650SAmir Goldstein 					      fid_mode);
71783b7a598SAmir Goldstein 	struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir);
718aa93bdc5SAmir Goldstein 	const struct path *path = fsnotify_data_path(data, data_type);
719b87d8cefSRoman Gushchin 	struct mem_cgroup *old_memcg;
7203982534bSAmir Goldstein 	struct dentry *moved = NULL;
7217e8283afSAmir Goldstein 	struct inode *child = NULL;
72208b95c33SAmir Goldstein 	bool name_event = false;
7238988f11aSAmir Goldstein 	unsigned int hash = 0;
7247e3e5c69SAmir Goldstein 	bool ondir = mask & FAN_ONDIR;
7257e3e5c69SAmir Goldstein 	struct pid *pid;
7261f5eaa90SJan Kara 
727929943b3SAmir Goldstein 	if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
7287e8283afSAmir Goldstein 		/*
729d61fd650SAmir Goldstein 		 * For certain events and group flags, report the child fid
730691d9763SAmir Goldstein 		 * in addition to reporting the parent fid and maybe child name.
7317e8283afSAmir Goldstein 		 */
732d61fd650SAmir Goldstein 		if (fanotify_report_child_fid(fid_mode, mask) && id != dirid)
7337e8283afSAmir Goldstein 			child = id;
7347e8283afSAmir Goldstein 
73583b7a598SAmir Goldstein 		id = dirid;
73683b7a598SAmir Goldstein 
7371f5eaa90SJan Kara 		/*
738929943b3SAmir Goldstein 		 * We record file name only in a group with FAN_REPORT_NAME
739929943b3SAmir Goldstein 		 * and when we have a directory inode to report.
740929943b3SAmir Goldstein 		 *
741929943b3SAmir Goldstein 		 * For directory entry modification event, we record the fid of
742929943b3SAmir Goldstein 		 * the directory and the name of the modified entry.
743929943b3SAmir Goldstein 		 *
744929943b3SAmir Goldstein 		 * For event on non-directory that is reported to parent, we
745929943b3SAmir Goldstein 		 * record the fid of the parent and the name of the child.
746691d9763SAmir Goldstein 		 *
747691d9763SAmir Goldstein 		 * Even if not reporting name, we need a variable length
748691d9763SAmir Goldstein 		 * fanotify_name_event if reporting both parent and child fids.
749929943b3SAmir Goldstein 		 */
750691d9763SAmir Goldstein 		if (!(fid_mode & FAN_REPORT_NAME)) {
751691d9763SAmir Goldstein 			name_event = !!child;
752691d9763SAmir Goldstein 			file_name = NULL;
7537e3e5c69SAmir Goldstein 		} else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) || !ondir) {
754929943b3SAmir Goldstein 			name_event = true;
755929943b3SAmir Goldstein 		}
7563982534bSAmir Goldstein 
7573982534bSAmir Goldstein 		/*
7582bfbcccdSAmir Goldstein 		 * In the special case of FAN_RENAME event, use the match_mask
7592bfbcccdSAmir Goldstein 		 * to determine if we need to report only the old parent+name,
7602bfbcccdSAmir Goldstein 		 * only the new parent+name or both.
7613982534bSAmir Goldstein 		 * 'dirid' and 'file_name' are the old parent+name and
7623982534bSAmir Goldstein 		 * 'moved' has the new parent+name.
7633982534bSAmir Goldstein 		 */
7642bfbcccdSAmir Goldstein 		if (mask & FAN_RENAME) {
7652bfbcccdSAmir Goldstein 			bool report_old, report_new;
7662bfbcccdSAmir Goldstein 
7672bfbcccdSAmir Goldstein 			if (WARN_ON_ONCE(!match_mask))
7682bfbcccdSAmir Goldstein 				return NULL;
7692bfbcccdSAmir Goldstein 
7702bfbcccdSAmir Goldstein 			/* Report both old and new parent+name if sb watching */
7712bfbcccdSAmir Goldstein 			report_old = report_new =
7722bfbcccdSAmir Goldstein 				match_mask & (1U << FSNOTIFY_ITER_TYPE_SB);
7732bfbcccdSAmir Goldstein 			report_old |=
7742bfbcccdSAmir Goldstein 				match_mask & (1U << FSNOTIFY_ITER_TYPE_INODE);
7752bfbcccdSAmir Goldstein 			report_new |=
7762bfbcccdSAmir Goldstein 				match_mask & (1U << FSNOTIFY_ITER_TYPE_INODE2);
7772bfbcccdSAmir Goldstein 
7782bfbcccdSAmir Goldstein 			if (!report_old) {
7792bfbcccdSAmir Goldstein 				/* Do not report old parent+name */
7802bfbcccdSAmir Goldstein 				dirid = NULL;
7812bfbcccdSAmir Goldstein 				file_name = NULL;
7822bfbcccdSAmir Goldstein 			}
7832bfbcccdSAmir Goldstein 			if (report_new) {
7842bfbcccdSAmir Goldstein 				/* Report new parent+name */
7853982534bSAmir Goldstein 				moved = fsnotify_data_dentry(data, data_type);
786691d9763SAmir Goldstein 			}
7872bfbcccdSAmir Goldstein 		}
7882bfbcccdSAmir Goldstein 	}
789929943b3SAmir Goldstein 
790929943b3SAmir Goldstein 	/*
7911f5eaa90SJan Kara 	 * For queues with unlimited length lost events are not expected and
7921f5eaa90SJan Kara 	 * can possibly have security implications. Avoid losing events when
793ec165450SShakeel Butt 	 * memory is short. For the limited size queues, avoid OOM killer in the
794ec165450SShakeel Butt 	 * target monitoring memcg as it may have security repercussion.
7951f5eaa90SJan Kara 	 */
7961f5eaa90SJan Kara 	if (group->max_events == UINT_MAX)
7971f5eaa90SJan Kara 		gfp |= __GFP_NOFAIL;
798ec165450SShakeel Butt 	else
799ec165450SShakeel Butt 		gfp |= __GFP_RETRY_MAYFAIL;
800f083441bSJan Kara 
801d46eb14bSShakeel Butt 	/* Whoever is interested in the event, pays for the allocation. */
802b87d8cefSRoman Gushchin 	old_memcg = set_active_memcg(group->memcg);
803d46eb14bSShakeel Butt 
8046685df31SMiklos Szeredi 	if (fanotify_is_perm_event(mask)) {
8059c61f3b5SAmir Goldstein 		event = fanotify_alloc_perm_event(path, gfp);
80683e9acbeSGabriel Krisman Bertazi 	} else if (fanotify_is_error_event(mask)) {
80783e9acbeSGabriel Krisman Bertazi 		event = fanotify_alloc_error_event(group, fsid, data,
8088a6ae641SGabriel Krisman Bertazi 						   data_type, &hash);
8093982534bSAmir Goldstein 	} else if (name_event && (file_name || moved || child)) {
8103982534bSAmir Goldstein 		event = fanotify_alloc_name_event(dirid, fsid, file_name, child,
8113982534bSAmir Goldstein 						  moved, &hash, gfp);
812d809daf1SAmir Goldstein 	} else if (fid_mode) {
8137e3e5c69SAmir Goldstein 		event = fanotify_alloc_fid_event(id, fsid, &hash, gfp);
8147088f357SJan Kara 	} else {
8157e3e5c69SAmir Goldstein 		event = fanotify_alloc_path_event(path, &hash, gfp);
8167088f357SJan Kara 	}
8177088f357SJan Kara 
8189c61f3b5SAmir Goldstein 	if (!event)
8199c61f3b5SAmir Goldstein 		goto out;
8209c61f3b5SAmir Goldstein 
821d0a6a87eSAmir Goldstein 	if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
8227e3e5c69SAmir Goldstein 		pid = get_pid(task_pid(current));
823d0a6a87eSAmir Goldstein 	else
8247e3e5c69SAmir Goldstein 		pid = get_pid(task_tgid(current));
8257e3e5c69SAmir Goldstein 
8267e3e5c69SAmir Goldstein 	/* Mix event info, FAN_ONDIR flag and pid into event merge key */
8277e3e5c69SAmir Goldstein 	hash ^= hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS);
8287e3e5c69SAmir Goldstein 	fanotify_init_event(event, hash, mask);
8297e3e5c69SAmir Goldstein 	event->pid = pid;
8307088f357SJan Kara 
831d46eb14bSShakeel Butt out:
832b87d8cefSRoman Gushchin 	set_active_memcg(old_memcg);
833f083441bSJan Kara 	return event;
834f083441bSJan Kara }
835f083441bSJan Kara 
83677115225SAmir Goldstein /*
8377232522eSAmir Goldstein  * Get cached fsid of the filesystem containing the object from any mark.
8387232522eSAmir Goldstein  * All marks are supposed to have the same fsid, but we do not verify that here.
83977115225SAmir Goldstein  */
fanotify_get_fsid(struct fsnotify_iter_info * iter_info)84077115225SAmir Goldstein static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
84177115225SAmir Goldstein {
84214362a25SAmir Goldstein 	struct fsnotify_mark *mark;
84377115225SAmir Goldstein 	int type;
84477115225SAmir Goldstein 	__kernel_fsid_t fsid = {};
84577115225SAmir Goldstein 
84614362a25SAmir Goldstein 	fsnotify_foreach_iter_mark_type(iter_info, mark, type) {
8477232522eSAmir Goldstein 		if (!(mark->flags & FSNOTIFY_MARK_FLAG_HAS_FSID))
848b1da6a51SJan Kara 			continue;
8497232522eSAmir Goldstein 		fsid = FANOTIFY_MARK(mark)->fsid;
85030ad1938SAmir Goldstein 		if (!(mark->flags & FSNOTIFY_MARK_FLAG_WEAK_FSID) &&
85130ad1938SAmir Goldstein 		    WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
85277115225SAmir Goldstein 			continue;
85377115225SAmir Goldstein 		return fsid;
85477115225SAmir Goldstein 	}
85577115225SAmir Goldstein 
85677115225SAmir Goldstein 	return fsid;
85777115225SAmir Goldstein }
85877115225SAmir Goldstein 
85994e00d28SAmir Goldstein /*
86094e00d28SAmir Goldstein  * Add an event to hash table for faster merge.
86194e00d28SAmir Goldstein  */
fanotify_insert_event(struct fsnotify_group * group,struct fsnotify_event * fsn_event)86294e00d28SAmir Goldstein static void fanotify_insert_event(struct fsnotify_group *group,
86394e00d28SAmir Goldstein 				  struct fsnotify_event *fsn_event)
86494e00d28SAmir Goldstein {
86594e00d28SAmir Goldstein 	struct fanotify_event *event = FANOTIFY_E(fsn_event);
86694e00d28SAmir Goldstein 	unsigned int bucket = fanotify_event_hash_bucket(group, event);
86794e00d28SAmir Goldstein 	struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
86894e00d28SAmir Goldstein 
86994e00d28SAmir Goldstein 	assert_spin_locked(&group->notification_lock);
87094e00d28SAmir Goldstein 
871cc53b55fSGabriel Krisman Bertazi 	if (!fanotify_is_hashed_event(event->mask))
872cc53b55fSGabriel Krisman Bertazi 		return;
873cc53b55fSGabriel Krisman Bertazi 
87494e00d28SAmir Goldstein 	pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
87594e00d28SAmir Goldstein 		 group, event, bucket);
87694e00d28SAmir Goldstein 
87794e00d28SAmir Goldstein 	hlist_add_head(&event->merge_list, hlist);
87894e00d28SAmir Goldstein }
87994e00d28SAmir 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)880b54cecf5SAmir Goldstein static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
881b54cecf5SAmir Goldstein 				 const void *data, int data_type,
882b54cecf5SAmir Goldstein 				 struct inode *dir,
883e43e9c33SAl Viro 				 const struct qstr *file_name, u32 cookie,
8849385a84dSJan Kara 				 struct fsnotify_iter_info *iter_info)
8857053aee2SJan Kara {
8867053aee2SJan Kara 	int ret = 0;
88733913997SAmir Goldstein 	struct fanotify_event *event;
8887053aee2SJan Kara 	struct fsnotify_event *fsn_event;
88977115225SAmir Goldstein 	__kernel_fsid_t fsid = {};
8902bfbcccdSAmir Goldstein 	u32 match_mask = 0;
8917053aee2SJan Kara 
8927053aee2SJan Kara 	BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
8937053aee2SJan Kara 	BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
894235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
8957053aee2SJan Kara 	BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
8967053aee2SJan Kara 	BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
8977053aee2SJan Kara 	BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
898235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
899235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
900235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
901235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
902235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
903235328d1SAmir Goldstein 	BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
9047053aee2SJan Kara 	BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
9057053aee2SJan Kara 	BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
9067053aee2SJan Kara 	BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
9077053aee2SJan Kara 	BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
9087053aee2SJan Kara 	BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
9099b076f1cSMatthew Bobrowski 	BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
91066917a31SMatthew Bobrowski 	BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
9118d11a4f4SGabriel Krisman Bertazi 	BUILD_BUG_ON(FAN_FS_ERROR != FS_ERROR);
9123982534bSAmir Goldstein 	BUILD_BUG_ON(FAN_RENAME != FS_RENAME);
9137053aee2SJan Kara 
9148cc3b1ccSAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 21);
915bdd5a46fSAmir Goldstein 
9162bfbcccdSAmir Goldstein 	mask = fanotify_group_event_mask(group, iter_info, &match_mask,
9172bfbcccdSAmir Goldstein 					 mask, data, data_type, dir);
9182d10b230SMatthew Bobrowski 	if (!mask)
91983c4c4b0SJan Kara 		return 0;
92083c4c4b0SJan Kara 
9212bfbcccdSAmir Goldstein 	pr_debug("%s: group=%p mask=%x report_mask=%x\n", __func__,
9222bfbcccdSAmir Goldstein 		 group, mask, match_mask);
9237053aee2SJan Kara 
9246685df31SMiklos Szeredi 	if (fanotify_is_perm_event(mask)) {
925f37650f1SMiklos Szeredi 		/*
926f37650f1SMiklos Szeredi 		 * fsnotify_prepare_user_wait() fails if we race with mark
927f37650f1SMiklos Szeredi 		 * deletion.  Just let the operation pass in that case.
928f37650f1SMiklos Szeredi 		 */
929f37650f1SMiklos Szeredi 		if (!fsnotify_prepare_user_wait(iter_info))
930f37650f1SMiklos Szeredi 			return 0;
931f37650f1SMiklos Szeredi 	}
932f37650f1SMiklos Szeredi 
93330ad1938SAmir Goldstein 	if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS))
93477115225SAmir Goldstein 		fsid = fanotify_get_fsid(iter_info);
93577115225SAmir Goldstein 
936b54cecf5SAmir Goldstein 	event = fanotify_alloc_event(group, mask, data, data_type, dir,
9372bfbcccdSAmir Goldstein 				     file_name, &fsid, match_mask);
938f37650f1SMiklos Szeredi 	ret = -ENOMEM;
9397b1f6417SJan Kara 	if (unlikely(!event)) {
9407b1f6417SJan Kara 		/*
9417b1f6417SJan Kara 		 * We don't queue overflow events for permission events as
9427b1f6417SJan Kara 		 * there the access is denied and so no event is in fact lost.
9437b1f6417SJan Kara 		 */
9447b1f6417SJan Kara 		if (!fanotify_is_perm_event(mask))
9457b1f6417SJan Kara 			fsnotify_queue_overflow(group);
946f37650f1SMiklos Szeredi 		goto finish;
9477b1f6417SJan Kara 	}
9487053aee2SJan Kara 
9497053aee2SJan Kara 	fsn_event = &event->fse;
9501ad03c3aSGabriel Krisman Bertazi 	ret = fsnotify_insert_event(group, fsn_event, fanotify_merge,
951cc53b55fSGabriel Krisman Bertazi 				    fanotify_insert_event);
95283c0e1b4SJan Kara 	if (ret) {
953482ef06cSJan Kara 		/* Permission events shouldn't be merged */
95423c9deebSAmir Goldstein 		BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
9557053aee2SJan Kara 		/* Our event wasn't used in the end. Free it. */
9567053aee2SJan Kara 		fsnotify_destroy_event(group, fsn_event);
957482ef06cSJan Kara 
958f37650f1SMiklos Szeredi 		ret = 0;
9596685df31SMiklos Szeredi 	} else if (fanotify_is_perm_event(mask)) {
9607088f357SJan Kara 		ret = fanotify_get_response(group, FANOTIFY_PERM(event),
96105f0e387SJan Kara 					    iter_info);
96285816794SJan Kara 	}
963f37650f1SMiklos Szeredi finish:
9646685df31SMiklos Szeredi 	if (fanotify_is_perm_event(mask))
965f37650f1SMiklos Szeredi 		fsnotify_finish_user_wait(iter_info);
9666685df31SMiklos Szeredi 
9677053aee2SJan Kara 	return ret;
9687053aee2SJan Kara }
9697053aee2SJan Kara 
fanotify_free_group_priv(struct fsnotify_group * group)9704afeff85SEric Paris static void fanotify_free_group_priv(struct fsnotify_group *group)
9714afeff85SEric Paris {
97294e00d28SAmir Goldstein 	kfree(group->fanotify_data.merge_hash);
9735b8fea65SAmir Goldstein 	if (group->fanotify_data.ucounts)
9745b8fea65SAmir Goldstein 		dec_ucount(group->fanotify_data.ucounts,
9755b8fea65SAmir Goldstein 			   UCOUNT_FANOTIFY_GROUPS);
976734a1a5eSGabriel Krisman Bertazi 
977734a1a5eSGabriel Krisman Bertazi 	if (mempool_initialized(&group->fanotify_data.error_events_pool))
978734a1a5eSGabriel Krisman Bertazi 		mempool_exit(&group->fanotify_data.error_events_pool);
9794afeff85SEric Paris }
9804afeff85SEric Paris 
fanotify_free_path_event(struct fanotify_event * event)9817088f357SJan Kara static void fanotify_free_path_event(struct fanotify_event *event)
9827088f357SJan Kara {
9837088f357SJan Kara 	path_put(fanotify_event_path(event));
9847088f357SJan Kara 	kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
9857088f357SJan Kara }
9867088f357SJan Kara 
fanotify_free_perm_event(struct fanotify_event * event)9877088f357SJan Kara static void fanotify_free_perm_event(struct fanotify_event *event)
9887088f357SJan Kara {
9897088f357SJan Kara 	path_put(fanotify_event_path(event));
9907088f357SJan Kara 	kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
9917088f357SJan Kara }
9927088f357SJan Kara 
fanotify_free_fid_event(struct fanotify_event * event)9937088f357SJan Kara static void fanotify_free_fid_event(struct fanotify_event *event)
9947088f357SJan Kara {
9957088f357SJan Kara 	struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
9967088f357SJan Kara 
9977088f357SJan Kara 	if (fanotify_fh_has_ext_buf(&ffe->object_fh))
9987088f357SJan Kara 		kfree(fanotify_fh_ext_buf(&ffe->object_fh));
9997088f357SJan Kara 	kmem_cache_free(fanotify_fid_event_cachep, ffe);
10007088f357SJan Kara }
10017088f357SJan Kara 
fanotify_free_name_event(struct fanotify_event * event)1002cacfb956SAmir Goldstein static void fanotify_free_name_event(struct fanotify_event *event)
1003cacfb956SAmir Goldstein {
1004f35c4156SAmir Goldstein 	kfree(FANOTIFY_NE(event));
1005cacfb956SAmir Goldstein }
1006cacfb956SAmir Goldstein 
fanotify_free_error_event(struct fsnotify_group * group,struct fanotify_event * event)100783e9acbeSGabriel Krisman Bertazi static void fanotify_free_error_event(struct fsnotify_group *group,
100883e9acbeSGabriel Krisman Bertazi 				      struct fanotify_event *event)
100983e9acbeSGabriel Krisman Bertazi {
101083e9acbeSGabriel Krisman Bertazi 	struct fanotify_error_event *fee = FANOTIFY_EE(event);
101183e9acbeSGabriel Krisman Bertazi 
101283e9acbeSGabriel Krisman Bertazi 	mempool_free(fee, &group->fanotify_data.error_events_pool);
101383e9acbeSGabriel Krisman Bertazi }
101483e9acbeSGabriel Krisman Bertazi 
fanotify_free_event(struct fsnotify_group * group,struct fsnotify_event * fsn_event)1015330ae77dSGabriel Krisman Bertazi static void fanotify_free_event(struct fsnotify_group *group,
1016330ae77dSGabriel Krisman Bertazi 				struct fsnotify_event *fsn_event)
10177053aee2SJan Kara {
101833913997SAmir Goldstein 	struct fanotify_event *event;
10197053aee2SJan Kara 
10207053aee2SJan Kara 	event = FANOTIFY_E(fsn_event);
1021d0a6a87eSAmir Goldstein 	put_pid(event->pid);
10227088f357SJan Kara 	switch (event->type) {
10237088f357SJan Kara 	case FANOTIFY_EVENT_TYPE_PATH:
10247088f357SJan Kara 		fanotify_free_path_event(event);
10257088f357SJan Kara 		break;
10267088f357SJan Kara 	case FANOTIFY_EVENT_TYPE_PATH_PERM:
10277088f357SJan Kara 		fanotify_free_perm_event(event);
10287088f357SJan Kara 		break;
10297088f357SJan Kara 	case FANOTIFY_EVENT_TYPE_FID:
10307088f357SJan Kara 		fanotify_free_fid_event(event);
10317088f357SJan Kara 		break;
1032cacfb956SAmir Goldstein 	case FANOTIFY_EVENT_TYPE_FID_NAME:
1033cacfb956SAmir Goldstein 		fanotify_free_name_event(event);
1034cacfb956SAmir Goldstein 		break;
1035b8a6c3a2SAmir Goldstein 	case FANOTIFY_EVENT_TYPE_OVERFLOW:
1036b8a6c3a2SAmir Goldstein 		kfree(event);
1037b8a6c3a2SAmir Goldstein 		break;
103883e9acbeSGabriel Krisman Bertazi 	case FANOTIFY_EVENT_TYPE_FS_ERROR:
103983e9acbeSGabriel Krisman Bertazi 		fanotify_free_error_event(group, event);
104083e9acbeSGabriel Krisman Bertazi 		break;
10417088f357SJan Kara 	default:
10427088f357SJan Kara 		WARN_ON_ONCE(1);
1043f083441bSJan Kara 	}
10447053aee2SJan Kara }
10457053aee2SJan Kara 
fanotify_freeing_mark(struct fsnotify_mark * mark,struct fsnotify_group * group)10465b8fea65SAmir Goldstein static void fanotify_freeing_mark(struct fsnotify_mark *mark,
10475b8fea65SAmir Goldstein 				  struct fsnotify_group *group)
10485b8fea65SAmir Goldstein {
10495b8fea65SAmir Goldstein 	if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
10505b8fea65SAmir Goldstein 		dec_ucount(group->fanotify_data.ucounts, UCOUNT_FANOTIFY_MARKS);
10515b8fea65SAmir Goldstein }
10525b8fea65SAmir Goldstein 
fanotify_free_mark(struct fsnotify_mark * fsn_mark)1053054c636eSJan Kara static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
1054054c636eSJan Kara {
10557232522eSAmir Goldstein 	kmem_cache_free(fanotify_mark_cache, FANOTIFY_MARK(fsn_mark));
1056054c636eSJan Kara }
1057054c636eSJan Kara 
1058ff0b16a9SEric Paris const struct fsnotify_ops fanotify_fsnotify_ops = {
1059ff0b16a9SEric Paris 	.handle_event = fanotify_handle_event,
10604afeff85SEric Paris 	.free_group_priv = fanotify_free_group_priv,
10617053aee2SJan Kara 	.free_event = fanotify_free_event,
10625b8fea65SAmir Goldstein 	.freeing_mark = fanotify_freeing_mark,
1063054c636eSJan Kara 	.free_mark = fanotify_free_mark,
1064ff0b16a9SEric Paris };
1065