fsnotify: use hash table for faster events merge
[linux-2.6-microblaze.git] / fs / notify / fanotify / fanotify.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fanotify.h>
3 #include <linux/fdtable.h>
4 #include <linux/fsnotify_backend.h>
5 #include <linux/init.h>
6 #include <linux/jiffies.h>
7 #include <linux/kernel.h> /* UINT_MAX */
8 #include <linux/mount.h>
9 #include <linux/sched.h>
10 #include <linux/sched/user.h>
11 #include <linux/sched/signal.h>
12 #include <linux/types.h>
13 #include <linux/wait.h>
14 #include <linux/audit.h>
15 #include <linux/sched/mm.h>
16 #include <linux/statfs.h>
17 #include <linux/stringhash.h>
18
19 #include "fanotify.h"
20
21 static bool fanotify_path_equal(struct path *p1, struct path *p2)
22 {
23         return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
24 }
25
26 static unsigned int fanotify_hash_path(const struct path *path)
27 {
28         return hash_ptr(path->dentry, FANOTIFY_EVENT_HASH_BITS) ^
29                 hash_ptr(path->mnt, FANOTIFY_EVENT_HASH_BITS);
30 }
31
32 static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
33                                        __kernel_fsid_t *fsid2)
34 {
35         return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
36 }
37
38 static unsigned int fanotify_hash_fsid(__kernel_fsid_t *fsid)
39 {
40         return hash_32(fsid->val[0], FANOTIFY_EVENT_HASH_BITS) ^
41                 hash_32(fsid->val[1], FANOTIFY_EVENT_HASH_BITS);
42 }
43
44 static bool fanotify_fh_equal(struct fanotify_fh *fh1,
45                               struct fanotify_fh *fh2)
46 {
47         if (fh1->type != fh2->type || fh1->len != fh2->len)
48                 return false;
49
50         return !fh1->len ||
51                 !memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
52 }
53
54 static unsigned int fanotify_hash_fh(struct fanotify_fh *fh)
55 {
56         long salt = (long)fh->type | (long)fh->len << 8;
57
58         /*
59          * full_name_hash() works long by long, so it handles fh buf optimally.
60          */
61         return full_name_hash((void *)salt, fanotify_fh_buf(fh), fh->len);
62 }
63
64 static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
65                                      struct fanotify_fid_event *ffe2)
66 {
67         /* Do not merge fid events without object fh */
68         if (!ffe1->object_fh.len)
69                 return false;
70
71         return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
72                 fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
73 }
74
75 static bool fanotify_info_equal(struct fanotify_info *info1,
76                                 struct fanotify_info *info2)
77 {
78         if (info1->dir_fh_totlen != info2->dir_fh_totlen ||
79             info1->file_fh_totlen != info2->file_fh_totlen ||
80             info1->name_len != info2->name_len)
81                 return false;
82
83         if (info1->dir_fh_totlen &&
84             !fanotify_fh_equal(fanotify_info_dir_fh(info1),
85                                fanotify_info_dir_fh(info2)))
86                 return false;
87
88         if (info1->file_fh_totlen &&
89             !fanotify_fh_equal(fanotify_info_file_fh(info1),
90                                fanotify_info_file_fh(info2)))
91                 return false;
92
93         return !info1->name_len ||
94                 !memcmp(fanotify_info_name(info1), fanotify_info_name(info2),
95                         info1->name_len);
96 }
97
98 static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
99                                       struct fanotify_name_event *fne2)
100 {
101         struct fanotify_info *info1 = &fne1->info;
102         struct fanotify_info *info2 = &fne2->info;
103
104         /* Do not merge name events without dir fh */
105         if (!info1->dir_fh_totlen)
106                 return false;
107
108         if (!fanotify_fsid_equal(&fne1->fsid, &fne2->fsid))
109                 return false;
110
111         return fanotify_info_equal(info1, info2);
112 }
113
114 static bool fanotify_should_merge(struct fanotify_event *old,
115                                   struct fanotify_event *new)
116 {
117         pr_debug("%s: old=%p new=%p\n", __func__, old, new);
118
119         if (old->hash != new->hash ||
120             old->type != new->type || old->pid != new->pid)
121                 return false;
122
123         /*
124          * We want to merge many dirent events in the same dir (i.e.
125          * creates/unlinks/renames), but we do not want to merge dirent
126          * events referring to subdirs with dirent events referring to
127          * non subdirs, otherwise, user won't be able to tell from a
128          * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
129          * unlink pair or rmdir+create pair of events.
130          */
131         if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
132                 return false;
133
134         switch (old->type) {
135         case FANOTIFY_EVENT_TYPE_PATH:
136                 return fanotify_path_equal(fanotify_event_path(old),
137                                            fanotify_event_path(new));
138         case FANOTIFY_EVENT_TYPE_FID:
139                 return fanotify_fid_event_equal(FANOTIFY_FE(old),
140                                                 FANOTIFY_FE(new));
141         case FANOTIFY_EVENT_TYPE_FID_NAME:
142                 return fanotify_name_event_equal(FANOTIFY_NE(old),
143                                                  FANOTIFY_NE(new));
144         default:
145                 WARN_ON_ONCE(1);
146         }
147
148         return false;
149 }
150
151 /* and the list better be locked by something too! */
152 static int fanotify_merge(struct fsnotify_group *group,
153                           struct fsnotify_event *event)
154 {
155         struct fanotify_event *old, *new = FANOTIFY_E(event);
156         unsigned int bucket = fanotify_event_hash_bucket(group, new);
157         struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
158
159         pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
160                  group, event, bucket);
161
162         /*
163          * Don't merge a permission event with any other event so that we know
164          * the event structure we have created in fanotify_handle_event() is the
165          * one we should check for permission response.
166          */
167         if (fanotify_is_perm_event(new->mask))
168                 return 0;
169
170         hlist_for_each_entry(old, hlist, merge_list) {
171                 if (fanotify_should_merge(old, new)) {
172                         old->mask |= new->mask;
173                         return 1;
174                 }
175         }
176
177         return 0;
178 }
179
180 /*
181  * Wait for response to permission event. The function also takes care of
182  * freeing the permission event (or offloads that in case the wait is canceled
183  * by a signal). The function returns 0 in case access got allowed by userspace,
184  * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
185  * the wait got interrupted by a signal.
186  */
187 static int fanotify_get_response(struct fsnotify_group *group,
188                                  struct fanotify_perm_event *event,
189                                  struct fsnotify_iter_info *iter_info)
190 {
191         int ret;
192
193         pr_debug("%s: group=%p event=%p\n", __func__, group, event);
194
195         ret = wait_event_killable(group->fanotify_data.access_waitq,
196                                   event->state == FAN_EVENT_ANSWERED);
197         /* Signal pending? */
198         if (ret < 0) {
199                 spin_lock(&group->notification_lock);
200                 /* Event reported to userspace and no answer yet? */
201                 if (event->state == FAN_EVENT_REPORTED) {
202                         /* Event will get freed once userspace answers to it */
203                         event->state = FAN_EVENT_CANCELED;
204                         spin_unlock(&group->notification_lock);
205                         return ret;
206                 }
207                 /* Event not yet reported? Just remove it. */
208                 if (event->state == FAN_EVENT_INIT) {
209                         fsnotify_remove_queued_event(group, &event->fae.fse);
210                         /* Permission events are not supposed to be hashed */
211                         WARN_ON_ONCE(!hlist_unhashed(&event->fae.merge_list));
212                 }
213                 /*
214                  * Event may be also answered in case signal delivery raced
215                  * with wakeup. In that case we have nothing to do besides
216                  * freeing the event and reporting error.
217                  */
218                 spin_unlock(&group->notification_lock);
219                 goto out;
220         }
221
222         /* userspace responded, convert to something usable */
223         switch (event->response & ~FAN_AUDIT) {
224         case FAN_ALLOW:
225                 ret = 0;
226                 break;
227         case FAN_DENY:
228         default:
229                 ret = -EPERM;
230         }
231
232         /* Check if the response should be audited */
233         if (event->response & FAN_AUDIT)
234                 audit_fanotify(event->response & ~FAN_AUDIT);
235
236         pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
237                  group, event, ret);
238 out:
239         fsnotify_destroy_event(group, &event->fae.fse);
240
241         return ret;
242 }
243
244 /*
245  * This function returns a mask for an event that only contains the flags
246  * that have been specifically requested by the user. Flags that may have
247  * been included within the event mask, but have not been explicitly
248  * requested by the user, will not be present in the returned mask.
249  */
250 static u32 fanotify_group_event_mask(struct fsnotify_group *group,
251                                      struct fsnotify_iter_info *iter_info,
252                                      u32 event_mask, const void *data,
253                                      int data_type, struct inode *dir)
254 {
255         __u32 marks_mask = 0, marks_ignored_mask = 0;
256         __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS |
257                                      FANOTIFY_EVENT_FLAGS;
258         const struct path *path = fsnotify_data_path(data, data_type);
259         unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
260         struct fsnotify_mark *mark;
261         int type;
262
263         pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
264                  __func__, iter_info->report_mask, event_mask, data, data_type);
265
266         if (!fid_mode) {
267                 /* Do we have path to open a file descriptor? */
268                 if (!path)
269                         return 0;
270                 /* Path type events are only relevant for files and dirs */
271                 if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
272                         return 0;
273         } else if (!(fid_mode & FAN_REPORT_FID)) {
274                 /* Do we have a directory inode to report? */
275                 if (!dir && !(event_mask & FS_ISDIR))
276                         return 0;
277         }
278
279         fsnotify_foreach_obj_type(type) {
280                 if (!fsnotify_iter_should_report_type(iter_info, type))
281                         continue;
282                 mark = iter_info->marks[type];
283
284                 /* Apply ignore mask regardless of ISDIR and ON_CHILD flags */
285                 marks_ignored_mask |= mark->ignored_mask;
286
287                 /*
288                  * If the event is on dir and this mark doesn't care about
289                  * events on dir, don't send it!
290                  */
291                 if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
292                         continue;
293
294                 /*
295                  * If the event is on a child and this mark is on a parent not
296                  * watching children, don't send it!
297                  */
298                 if (type == FSNOTIFY_OBJ_TYPE_PARENT &&
299                     !(mark->mask & FS_EVENT_ON_CHILD))
300                         continue;
301
302                 marks_mask |= mark->mask;
303         }
304
305         test_mask = event_mask & marks_mask & ~marks_ignored_mask;
306
307         /*
308          * For dirent modification events (create/delete/move) that do not carry
309          * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
310          * so user can differentiate them from creat/unlink.
311          *
312          * For backward compatibility and consistency, do not report FAN_ONDIR
313          * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
314          * to user in fid mode for all event types.
315          *
316          * We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to
317          * fanotify_alloc_event() when group is reporting fid as indication
318          * that event happened on child.
319          */
320         if (fid_mode) {
321                 /* Do not report event flags without any event */
322                 if (!(test_mask & ~FANOTIFY_EVENT_FLAGS))
323                         return 0;
324         } else {
325                 user_mask &= ~FANOTIFY_EVENT_FLAGS;
326         }
327
328         return test_mask & user_mask;
329 }
330
331 /*
332  * Check size needed to encode fanotify_fh.
333  *
334  * Return size of encoded fh without fanotify_fh header.
335  * Return 0 on failure to encode.
336  */
337 static int fanotify_encode_fh_len(struct inode *inode)
338 {
339         int dwords = 0;
340
341         if (!inode)
342                 return 0;
343
344         exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
345
346         return dwords << 2;
347 }
348
349 /*
350  * Encode fanotify_fh.
351  *
352  * Return total size of encoded fh including fanotify_fh header.
353  * Return 0 on failure to encode.
354  */
355 static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
356                               unsigned int fh_len, unsigned int *hash,
357                               gfp_t gfp)
358 {
359         int dwords, type = 0;
360         char *ext_buf = NULL;
361         void *buf = fh->buf;
362         int err;
363
364         fh->type = FILEID_ROOT;
365         fh->len = 0;
366         fh->flags = 0;
367         if (!inode)
368                 return 0;
369
370         /*
371          * !gpf means preallocated variable size fh, but fh_len could
372          * be zero in that case if encoding fh len failed.
373          */
374         err = -ENOENT;
375         if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4))
376                 goto out_err;
377
378         /* No external buffer in a variable size allocated fh */
379         if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) {
380                 /* Treat failure to allocate fh as failure to encode fh */
381                 err = -ENOMEM;
382                 ext_buf = kmalloc(fh_len, gfp);
383                 if (!ext_buf)
384                         goto out_err;
385
386                 *fanotify_fh_ext_buf_ptr(fh) = ext_buf;
387                 buf = ext_buf;
388                 fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF;
389         }
390
391         dwords = fh_len >> 2;
392         type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
393         err = -EINVAL;
394         if (!type || type == FILEID_INVALID || fh_len != dwords << 2)
395                 goto out_err;
396
397         fh->type = type;
398         fh->len = fh_len;
399
400         /* Mix fh into event merge key */
401         *hash ^= fanotify_hash_fh(fh);
402
403         return FANOTIFY_FH_HDR_LEN + fh_len;
404
405 out_err:
406         pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
407                             type, fh_len, err);
408         kfree(ext_buf);
409         *fanotify_fh_ext_buf_ptr(fh) = NULL;
410         /* Report the event without a file identifier on encode error */
411         fh->type = FILEID_INVALID;
412         fh->len = 0;
413         return 0;
414 }
415
416 /*
417  * The inode to use as identifier when reporting fid depends on the event.
418  * Report the modified directory inode on dirent modification events.
419  * Report the "victim" inode otherwise.
420  * For example:
421  * FS_ATTRIB reports the child inode even if reported on a watched parent.
422  * FS_CREATE reports the modified dir inode and not the created inode.
423  */
424 static struct inode *fanotify_fid_inode(u32 event_mask, const void *data,
425                                         int data_type, struct inode *dir)
426 {
427         if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
428                 return dir;
429
430         return fsnotify_data_inode(data, data_type);
431 }
432
433 /*
434  * The inode to use as identifier when reporting dir fid depends on the event.
435  * Report the modified directory inode on dirent modification events.
436  * Report the "victim" inode if "victim" is a directory.
437  * Report the parent inode if "victim" is not a directory and event is
438  * reported to parent.
439  * Otherwise, do not report dir fid.
440  */
441 static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data,
442                                          int data_type, struct inode *dir)
443 {
444         struct inode *inode = fsnotify_data_inode(data, data_type);
445
446         if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
447                 return dir;
448
449         if (S_ISDIR(inode->i_mode))
450                 return inode;
451
452         return dir;
453 }
454
455 static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
456                                                         unsigned int *hash,
457                                                         gfp_t gfp)
458 {
459         struct fanotify_path_event *pevent;
460
461         pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
462         if (!pevent)
463                 return NULL;
464
465         pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
466         pevent->path = *path;
467         *hash ^= fanotify_hash_path(path);
468         path_get(path);
469
470         return &pevent->fae;
471 }
472
473 static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
474                                                         gfp_t gfp)
475 {
476         struct fanotify_perm_event *pevent;
477
478         pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
479         if (!pevent)
480                 return NULL;
481
482         pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
483         pevent->response = 0;
484         pevent->state = FAN_EVENT_INIT;
485         pevent->path = *path;
486         path_get(path);
487
488         return &pevent->fae;
489 }
490
491 static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
492                                                        __kernel_fsid_t *fsid,
493                                                        unsigned int *hash,
494                                                        gfp_t gfp)
495 {
496         struct fanotify_fid_event *ffe;
497
498         ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
499         if (!ffe)
500                 return NULL;
501
502         ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
503         ffe->fsid = *fsid;
504         *hash ^= fanotify_hash_fsid(fsid);
505         fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id),
506                            hash, gfp);
507
508         return &ffe->fae;
509 }
510
511 static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
512                                                         __kernel_fsid_t *fsid,
513                                                         const struct qstr *name,
514                                                         struct inode *child,
515                                                         unsigned int *hash,
516                                                         gfp_t gfp)
517 {
518         struct fanotify_name_event *fne;
519         struct fanotify_info *info;
520         struct fanotify_fh *dfh, *ffh;
521         unsigned int dir_fh_len = fanotify_encode_fh_len(id);
522         unsigned int child_fh_len = fanotify_encode_fh_len(child);
523         unsigned int size;
524
525         size = sizeof(*fne) + FANOTIFY_FH_HDR_LEN + dir_fh_len;
526         if (child_fh_len)
527                 size += FANOTIFY_FH_HDR_LEN + child_fh_len;
528         if (name)
529                 size += name->len + 1;
530         fne = kmalloc(size, gfp);
531         if (!fne)
532                 return NULL;
533
534         fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
535         fne->fsid = *fsid;
536         *hash ^= fanotify_hash_fsid(fsid);
537         info = &fne->info;
538         fanotify_info_init(info);
539         dfh = fanotify_info_dir_fh(info);
540         info->dir_fh_totlen = fanotify_encode_fh(dfh, id, dir_fh_len, hash, 0);
541         if (child_fh_len) {
542                 ffh = fanotify_info_file_fh(info);
543                 info->file_fh_totlen = fanotify_encode_fh(ffh, child,
544                                                         child_fh_len, hash, 0);
545         }
546         if (name) {
547                 long salt = name->len;
548
549                 fanotify_info_copy_name(info, name);
550                 *hash ^= full_name_hash((void *)salt, name->name, name->len);
551         }
552
553         pr_debug("%s: ino=%lu size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n",
554                  __func__, id->i_ino, size, dir_fh_len, child_fh_len,
555                  info->name_len, info->name_len, fanotify_info_name(info));
556
557         return &fne->fae;
558 }
559
560 static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
561                                                    u32 mask, const void *data,
562                                                    int data_type, struct inode *dir,
563                                                    const struct qstr *file_name,
564                                                    __kernel_fsid_t *fsid)
565 {
566         struct fanotify_event *event = NULL;
567         gfp_t gfp = GFP_KERNEL_ACCOUNT;
568         struct inode *id = fanotify_fid_inode(mask, data, data_type, dir);
569         struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir);
570         const struct path *path = fsnotify_data_path(data, data_type);
571         unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
572         struct mem_cgroup *old_memcg;
573         struct inode *child = NULL;
574         bool name_event = false;
575         unsigned int hash = 0;
576         bool ondir = mask & FAN_ONDIR;
577         struct pid *pid;
578
579         if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
580                 /*
581                  * With both flags FAN_REPORT_DIR_FID and FAN_REPORT_FID, we
582                  * report the child fid for events reported on a non-dir child
583                  * in addition to reporting the parent fid and maybe child name.
584                  */
585                 if ((fid_mode & FAN_REPORT_FID) && id != dirid && !ondir)
586                         child = id;
587
588                 id = dirid;
589
590                 /*
591                  * We record file name only in a group with FAN_REPORT_NAME
592                  * and when we have a directory inode to report.
593                  *
594                  * For directory entry modification event, we record the fid of
595                  * the directory and the name of the modified entry.
596                  *
597                  * For event on non-directory that is reported to parent, we
598                  * record the fid of the parent and the name of the child.
599                  *
600                  * Even if not reporting name, we need a variable length
601                  * fanotify_name_event if reporting both parent and child fids.
602                  */
603                 if (!(fid_mode & FAN_REPORT_NAME)) {
604                         name_event = !!child;
605                         file_name = NULL;
606                 } else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) || !ondir) {
607                         name_event = true;
608                 }
609         }
610
611         /*
612          * For queues with unlimited length lost events are not expected and
613          * can possibly have security implications. Avoid losing events when
614          * memory is short. For the limited size queues, avoid OOM killer in the
615          * target monitoring memcg as it may have security repercussion.
616          */
617         if (group->max_events == UINT_MAX)
618                 gfp |= __GFP_NOFAIL;
619         else
620                 gfp |= __GFP_RETRY_MAYFAIL;
621
622         /* Whoever is interested in the event, pays for the allocation. */
623         old_memcg = set_active_memcg(group->memcg);
624
625         if (fanotify_is_perm_event(mask)) {
626                 event = fanotify_alloc_perm_event(path, gfp);
627         } else if (name_event && (file_name || child)) {
628                 event = fanotify_alloc_name_event(id, fsid, file_name, child,
629                                                   &hash, gfp);
630         } else if (fid_mode) {
631                 event = fanotify_alloc_fid_event(id, fsid, &hash, gfp);
632         } else {
633                 event = fanotify_alloc_path_event(path, &hash, gfp);
634         }
635
636         if (!event)
637                 goto out;
638
639         if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
640                 pid = get_pid(task_pid(current));
641         else
642                 pid = get_pid(task_tgid(current));
643
644         /* Mix event info, FAN_ONDIR flag and pid into event merge key */
645         hash ^= hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS);
646         fanotify_init_event(event, hash, mask);
647         event->pid = pid;
648
649 out:
650         set_active_memcg(old_memcg);
651         return event;
652 }
653
654 /*
655  * Get cached fsid of the filesystem containing the object from any connector.
656  * All connectors are supposed to have the same fsid, but we do not verify that
657  * here.
658  */
659 static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
660 {
661         int type;
662         __kernel_fsid_t fsid = {};
663
664         fsnotify_foreach_obj_type(type) {
665                 struct fsnotify_mark_connector *conn;
666
667                 if (!fsnotify_iter_should_report_type(iter_info, type))
668                         continue;
669
670                 conn = READ_ONCE(iter_info->marks[type]->connector);
671                 /* Mark is just getting destroyed or created? */
672                 if (!conn)
673                         continue;
674                 if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID))
675                         continue;
676                 /* Pairs with smp_wmb() in fsnotify_add_mark_list() */
677                 smp_rmb();
678                 fsid = conn->fsid;
679                 if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
680                         continue;
681                 return fsid;
682         }
683
684         return fsid;
685 }
686
687 /*
688  * Add an event to hash table for faster merge.
689  */
690 static void fanotify_insert_event(struct fsnotify_group *group,
691                                   struct fsnotify_event *fsn_event)
692 {
693         struct fanotify_event *event = FANOTIFY_E(fsn_event);
694         unsigned int bucket = fanotify_event_hash_bucket(group, event);
695         struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
696
697         assert_spin_locked(&group->notification_lock);
698
699         pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
700                  group, event, bucket);
701
702         hlist_add_head(&event->merge_list, hlist);
703 }
704
705 static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
706                                  const void *data, int data_type,
707                                  struct inode *dir,
708                                  const struct qstr *file_name, u32 cookie,
709                                  struct fsnotify_iter_info *iter_info)
710 {
711         int ret = 0;
712         struct fanotify_event *event;
713         struct fsnotify_event *fsn_event;
714         __kernel_fsid_t fsid = {};
715
716         BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
717         BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
718         BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
719         BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
720         BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
721         BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
722         BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
723         BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
724         BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
725         BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
726         BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
727         BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
728         BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
729         BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
730         BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
731         BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
732         BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
733         BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
734         BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
735
736         BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19);
737
738         mask = fanotify_group_event_mask(group, iter_info, mask, data,
739                                          data_type, dir);
740         if (!mask)
741                 return 0;
742
743         pr_debug("%s: group=%p mask=%x\n", __func__, group, mask);
744
745         if (fanotify_is_perm_event(mask)) {
746                 /*
747                  * fsnotify_prepare_user_wait() fails if we race with mark
748                  * deletion.  Just let the operation pass in that case.
749                  */
750                 if (!fsnotify_prepare_user_wait(iter_info))
751                         return 0;
752         }
753
754         if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) {
755                 fsid = fanotify_get_fsid(iter_info);
756                 /* Racing with mark destruction or creation? */
757                 if (!fsid.val[0] && !fsid.val[1])
758                         return 0;
759         }
760
761         event = fanotify_alloc_event(group, mask, data, data_type, dir,
762                                      file_name, &fsid);
763         ret = -ENOMEM;
764         if (unlikely(!event)) {
765                 /*
766                  * We don't queue overflow events for permission events as
767                  * there the access is denied and so no event is in fact lost.
768                  */
769                 if (!fanotify_is_perm_event(mask))
770                         fsnotify_queue_overflow(group);
771                 goto finish;
772         }
773
774         fsn_event = &event->fse;
775         ret = fsnotify_add_event(group, fsn_event, fanotify_merge,
776                                  fanotify_is_hashed_event(mask) ?
777                                  fanotify_insert_event : NULL);
778         if (ret) {
779                 /* Permission events shouldn't be merged */
780                 BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
781                 /* Our event wasn't used in the end. Free it. */
782                 fsnotify_destroy_event(group, fsn_event);
783
784                 ret = 0;
785         } else if (fanotify_is_perm_event(mask)) {
786                 ret = fanotify_get_response(group, FANOTIFY_PERM(event),
787                                             iter_info);
788         }
789 finish:
790         if (fanotify_is_perm_event(mask))
791                 fsnotify_finish_user_wait(iter_info);
792
793         return ret;
794 }
795
796 static void fanotify_free_group_priv(struct fsnotify_group *group)
797 {
798         struct user_struct *user;
799
800         kfree(group->fanotify_data.merge_hash);
801         user = group->fanotify_data.user;
802         atomic_dec(&user->fanotify_listeners);
803         free_uid(user);
804 }
805
806 static void fanotify_free_path_event(struct fanotify_event *event)
807 {
808         path_put(fanotify_event_path(event));
809         kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
810 }
811
812 static void fanotify_free_perm_event(struct fanotify_event *event)
813 {
814         path_put(fanotify_event_path(event));
815         kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
816 }
817
818 static void fanotify_free_fid_event(struct fanotify_event *event)
819 {
820         struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
821
822         if (fanotify_fh_has_ext_buf(&ffe->object_fh))
823                 kfree(fanotify_fh_ext_buf(&ffe->object_fh));
824         kmem_cache_free(fanotify_fid_event_cachep, ffe);
825 }
826
827 static void fanotify_free_name_event(struct fanotify_event *event)
828 {
829         kfree(FANOTIFY_NE(event));
830 }
831
832 static void fanotify_free_event(struct fsnotify_event *fsn_event)
833 {
834         struct fanotify_event *event;
835
836         event = FANOTIFY_E(fsn_event);
837         put_pid(event->pid);
838         switch (event->type) {
839         case FANOTIFY_EVENT_TYPE_PATH:
840                 fanotify_free_path_event(event);
841                 break;
842         case FANOTIFY_EVENT_TYPE_PATH_PERM:
843                 fanotify_free_perm_event(event);
844                 break;
845         case FANOTIFY_EVENT_TYPE_FID:
846                 fanotify_free_fid_event(event);
847                 break;
848         case FANOTIFY_EVENT_TYPE_FID_NAME:
849                 fanotify_free_name_event(event);
850                 break;
851         case FANOTIFY_EVENT_TYPE_OVERFLOW:
852                 kfree(event);
853                 break;
854         default:
855                 WARN_ON_ONCE(1);
856         }
857 }
858
859 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
860 {
861         kmem_cache_free(fanotify_mark_cache, fsn_mark);
862 }
863
864 const struct fsnotify_ops fanotify_fsnotify_ops = {
865         .handle_event = fanotify_handle_event,
866         .free_group_priv = fanotify_free_group_priv,
867         .free_event = fanotify_free_event,
868         .free_mark = fanotify_free_mark,
869 };