fanotify: generalize merge logic of events on dir
[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
18 #include "fanotify.h"
19
20 static bool fanotify_path_equal(struct path *p1, struct path *p2)
21 {
22         return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
23 }
24
25 static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1,
26                                        __kernel_fsid_t *fsid2)
27 {
28         return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1];
29 }
30
31 static bool fanotify_fh_equal(struct fanotify_fh *fh1,
32                               struct fanotify_fh *fh2)
33 {
34         if (fh1->type != fh2->type || fh1->len != fh2->len)
35                 return false;
36
37         /* Do not merge events if we failed to encode fh */
38         if (fh1->type == FILEID_INVALID)
39                 return false;
40
41         return !fh1->len ||
42                 !memcmp(fanotify_fh_buf(fh1), fanotify_fh_buf(fh2), fh1->len);
43 }
44
45 static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
46                                      struct fanotify_fid_event *ffe2)
47 {
48         /* Do not merge fid events without object fh */
49         if (!ffe1->object_fh.len)
50                 return false;
51
52         return fanotify_fsid_equal(&ffe1->fsid, &ffe2->fsid) &&
53                 fanotify_fh_equal(&ffe1->object_fh, &ffe2->object_fh);
54 }
55
56 static bool fanotify_name_event_equal(struct fanotify_name_event *fne1,
57                                       struct fanotify_name_event *fne2)
58 {
59         /*
60          * Do not merge name events without dir fh.
61          * FAN_DIR_MODIFY does not encode object fh, so it may be empty.
62          */
63         if (!fne1->dir_fh.len)
64                 return false;
65
66         if (fne1->name_len != fne2->name_len ||
67             !fanotify_fh_equal(&fne1->dir_fh, &fne2->dir_fh))
68                 return false;
69
70         return !memcmp(fne1->name, fne2->name, fne1->name_len);
71 }
72
73 static bool fanotify_should_merge(struct fsnotify_event *old_fsn,
74                          struct fsnotify_event *new_fsn)
75 {
76         struct fanotify_event *old, *new;
77
78         pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
79         old = FANOTIFY_E(old_fsn);
80         new = FANOTIFY_E(new_fsn);
81
82         if (old_fsn->objectid != new_fsn->objectid ||
83             old->type != new->type || old->pid != new->pid)
84                 return false;
85
86         /*
87          * We want to merge many dirent events in the same dir (i.e.
88          * creates/unlinks/renames), but we do not want to merge dirent
89          * events referring to subdirs with dirent events referring to
90          * non subdirs, otherwise, user won't be able to tell from a
91          * mask FAN_CREATE|FAN_DELETE|FAN_ONDIR if it describes mkdir+
92          * unlink pair or rmdir+create pair of events.
93          */
94         if ((old->mask & FS_ISDIR) != (new->mask & FS_ISDIR))
95                 return false;
96
97         switch (old->type) {
98         case FANOTIFY_EVENT_TYPE_PATH:
99                 return fanotify_path_equal(fanotify_event_path(old),
100                                            fanotify_event_path(new));
101         case FANOTIFY_EVENT_TYPE_FID:
102                 return fanotify_fid_event_equal(FANOTIFY_FE(old),
103                                                 FANOTIFY_FE(new));
104         case FANOTIFY_EVENT_TYPE_FID_NAME:
105                 return fanotify_name_event_equal(FANOTIFY_NE(old),
106                                                  FANOTIFY_NE(new));
107         default:
108                 WARN_ON_ONCE(1);
109         }
110
111         return false;
112 }
113
114 /* and the list better be locked by something too! */
115 static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
116 {
117         struct fsnotify_event *test_event;
118         struct fanotify_event *new;
119
120         pr_debug("%s: list=%p event=%p\n", __func__, list, event);
121         new = FANOTIFY_E(event);
122
123         /*
124          * Don't merge a permission event with any other event so that we know
125          * the event structure we have created in fanotify_handle_event() is the
126          * one we should check for permission response.
127          */
128         if (fanotify_is_perm_event(new->mask))
129                 return 0;
130
131         list_for_each_entry_reverse(test_event, list, list) {
132                 if (fanotify_should_merge(test_event, event)) {
133                         FANOTIFY_E(test_event)->mask |= new->mask;
134                         return 1;
135                 }
136         }
137
138         return 0;
139 }
140
141 /*
142  * Wait for response to permission event. The function also takes care of
143  * freeing the permission event (or offloads that in case the wait is canceled
144  * by a signal). The function returns 0 in case access got allowed by userspace,
145  * -EPERM in case userspace disallowed the access, and -ERESTARTSYS in case
146  * the wait got interrupted by a signal.
147  */
148 static int fanotify_get_response(struct fsnotify_group *group,
149                                  struct fanotify_perm_event *event,
150                                  struct fsnotify_iter_info *iter_info)
151 {
152         int ret;
153
154         pr_debug("%s: group=%p event=%p\n", __func__, group, event);
155
156         ret = wait_event_killable(group->fanotify_data.access_waitq,
157                                   event->state == FAN_EVENT_ANSWERED);
158         /* Signal pending? */
159         if (ret < 0) {
160                 spin_lock(&group->notification_lock);
161                 /* Event reported to userspace and no answer yet? */
162                 if (event->state == FAN_EVENT_REPORTED) {
163                         /* Event will get freed once userspace answers to it */
164                         event->state = FAN_EVENT_CANCELED;
165                         spin_unlock(&group->notification_lock);
166                         return ret;
167                 }
168                 /* Event not yet reported? Just remove it. */
169                 if (event->state == FAN_EVENT_INIT)
170                         fsnotify_remove_queued_event(group, &event->fae.fse);
171                 /*
172                  * Event may be also answered in case signal delivery raced
173                  * with wakeup. In that case we have nothing to do besides
174                  * freeing the event and reporting error.
175                  */
176                 spin_unlock(&group->notification_lock);
177                 goto out;
178         }
179
180         /* userspace responded, convert to something usable */
181         switch (event->response & ~FAN_AUDIT) {
182         case FAN_ALLOW:
183                 ret = 0;
184                 break;
185         case FAN_DENY:
186         default:
187                 ret = -EPERM;
188         }
189
190         /* Check if the response should be audited */
191         if (event->response & FAN_AUDIT)
192                 audit_fanotify(event->response & ~FAN_AUDIT);
193
194         pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
195                  group, event, ret);
196 out:
197         fsnotify_destroy_event(group, &event->fae.fse);
198
199         return ret;
200 }
201
202 /*
203  * This function returns a mask for an event that only contains the flags
204  * that have been specifically requested by the user. Flags that may have
205  * been included within the event mask, but have not been explicitly
206  * requested by the user, will not be present in the returned mask.
207  */
208 static u32 fanotify_group_event_mask(struct fsnotify_group *group,
209                                      struct fsnotify_iter_info *iter_info,
210                                      u32 event_mask, const void *data,
211                                      int data_type)
212 {
213         __u32 marks_mask = 0, marks_ignored_mask = 0;
214         __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS |
215                                      FANOTIFY_EVENT_FLAGS;
216         const struct path *path = fsnotify_data_path(data, data_type);
217         struct fsnotify_mark *mark;
218         int type;
219
220         pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
221                  __func__, iter_info->report_mask, event_mask, data, data_type);
222
223         if (!FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
224                 /* Do we have path to open a file descriptor? */
225                 if (!path)
226                         return 0;
227                 /* Path type events are only relevant for files and dirs */
228                 if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
229                         return 0;
230         }
231
232         fsnotify_foreach_obj_type(type) {
233                 if (!fsnotify_iter_should_report_type(iter_info, type))
234                         continue;
235                 mark = iter_info->marks[type];
236
237                 /* Apply ignore mask regardless of ISDIR and ON_CHILD flags */
238                 marks_ignored_mask |= mark->ignored_mask;
239
240                 /*
241                  * If the event is on dir and this mark doesn't care about
242                  * events on dir, don't send it!
243                  */
244                 if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
245                         continue;
246
247                 /*
248                  * If the event is for a child and this mark doesn't care about
249                  * events on a child, don't send it!
250                  */
251                 if (event_mask & FS_EVENT_ON_CHILD &&
252                     (type != FSNOTIFY_OBJ_TYPE_INODE ||
253                      !(mark->mask & FS_EVENT_ON_CHILD)))
254                         continue;
255
256                 marks_mask |= mark->mask;
257         }
258
259         test_mask = event_mask & marks_mask & ~marks_ignored_mask;
260
261         /*
262          * For dirent modification events (create/delete/move) that do not carry
263          * the child entry name information, we report FAN_ONDIR for mkdir/rmdir
264          * so user can differentiate them from creat/unlink.
265          *
266          * For backward compatibility and consistency, do not report FAN_ONDIR
267          * to user in legacy fanotify mode (reporting fd) and report FAN_ONDIR
268          * to user in fid mode for all event types.
269          *
270          * We never report FAN_EVENT_ON_CHILD to user, but we do pass it in to
271          * fanotify_alloc_event() when group is reporting fid as indication
272          * that event happened on child.
273          */
274         if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
275                 /* Do not report event flags without any event */
276                 if (!(test_mask & ~FANOTIFY_EVENT_FLAGS))
277                         return 0;
278         } else {
279                 user_mask &= ~FANOTIFY_EVENT_FLAGS;
280         }
281
282         return test_mask & user_mask;
283 }
284
285 static void fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
286                                gfp_t gfp)
287 {
288         int dwords, type, bytes = 0;
289         char *ext_buf = NULL;
290         void *buf = fh->buf;
291         int err;
292
293         if (!inode)
294                 goto out;
295
296         dwords = 0;
297         err = -ENOENT;
298         type = exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
299         if (!dwords)
300                 goto out_err;
301
302         bytes = dwords << 2;
303         if (bytes > FANOTIFY_INLINE_FH_LEN) {
304                 /* Treat failure to allocate fh as failure to allocate event */
305                 err = -ENOMEM;
306                 ext_buf = kmalloc(bytes, gfp);
307                 if (!ext_buf)
308                         goto out_err;
309
310                 *fanotify_fh_ext_buf_ptr(fh) = ext_buf;
311                 buf = ext_buf;
312         }
313
314         type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
315         err = -EINVAL;
316         if (!type || type == FILEID_INVALID || bytes != dwords << 2)
317                 goto out_err;
318
319         fh->type = type;
320         fh->len = bytes;
321
322         return;
323
324 out_err:
325         pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
326                             type, bytes, err);
327         kfree(ext_buf);
328         *fanotify_fh_ext_buf_ptr(fh) = NULL;
329 out:
330         /* Report the event without a file identifier on encode error */
331         fh->type = FILEID_INVALID;
332         fh->len = 0;
333 }
334
335 /*
336  * The inode to use as identifier when reporting fid depends on the event.
337  * Report the modified directory inode on dirent modification events.
338  * Report the "victim" inode otherwise.
339  * For example:
340  * FS_ATTRIB reports the child inode even if reported on a watched parent.
341  * FS_CREATE reports the modified dir inode and not the created inode.
342  */
343 static struct inode *fanotify_fid_inode(u32 event_mask, const void *data,
344                                         int data_type, struct inode *dir)
345 {
346         if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
347                 return dir;
348
349         return fsnotify_data_inode(data, data_type);
350 }
351
352 static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
353                                                         gfp_t gfp)
354 {
355         struct fanotify_path_event *pevent;
356
357         pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
358         if (!pevent)
359                 return NULL;
360
361         pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
362         pevent->path = *path;
363         path_get(path);
364
365         return &pevent->fae;
366 }
367
368 static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
369                                                         gfp_t gfp)
370 {
371         struct fanotify_perm_event *pevent;
372
373         pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
374         if (!pevent)
375                 return NULL;
376
377         pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
378         pevent->response = 0;
379         pevent->state = FAN_EVENT_INIT;
380         pevent->path = *path;
381         path_get(path);
382
383         return &pevent->fae;
384 }
385
386 static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
387                                                        __kernel_fsid_t *fsid,
388                                                        gfp_t gfp)
389 {
390         struct fanotify_fid_event *ffe;
391
392         ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
393         if (!ffe)
394                 return NULL;
395
396         ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
397         ffe->fsid = *fsid;
398         fanotify_encode_fh(&ffe->object_fh, id, gfp);
399
400         return &ffe->fae;
401 }
402
403 static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
404                                                         __kernel_fsid_t *fsid,
405                                                         const struct qstr *file_name,
406                                                         gfp_t gfp)
407 {
408         struct fanotify_name_event *fne;
409
410         fne = kmalloc(sizeof(*fne) + file_name->len + 1, gfp);
411         if (!fne)
412                 return NULL;
413
414         fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
415         fne->fsid = *fsid;
416         fanotify_encode_fh(&fne->dir_fh, id, gfp);
417         fne->name_len = file_name->len;
418         strcpy(fne->name, file_name->name);
419
420         return &fne->fae;
421 }
422
423 static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
424                                                    u32 mask, const void *data,
425                                                    int data_type, struct inode *dir,
426                                                    const struct qstr *file_name,
427                                                    __kernel_fsid_t *fsid)
428 {
429         struct fanotify_event *event = NULL;
430         gfp_t gfp = GFP_KERNEL_ACCOUNT;
431         struct inode *id = fanotify_fid_inode(mask, data, data_type, dir);
432         const struct path *path = fsnotify_data_path(data, data_type);
433         bool name_event = false;
434
435         /*
436          * For queues with unlimited length lost events are not expected and
437          * can possibly have security implications. Avoid losing events when
438          * memory is short. For the limited size queues, avoid OOM killer in the
439          * target monitoring memcg as it may have security repercussion.
440          */
441         if (group->max_events == UINT_MAX)
442                 gfp |= __GFP_NOFAIL;
443         else
444                 gfp |= __GFP_RETRY_MAYFAIL;
445
446         /* Whoever is interested in the event, pays for the allocation. */
447         memalloc_use_memcg(group->memcg);
448
449         if (fanotify_is_perm_event(mask)) {
450                 event = fanotify_alloc_perm_event(path, gfp);
451         } else if (name_event && file_name) {
452                 event = fanotify_alloc_name_event(id, fsid, file_name, gfp);
453         } else if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
454                 event = fanotify_alloc_fid_event(id, fsid, gfp);
455         } else {
456                 event = fanotify_alloc_path_event(path, gfp);
457         }
458
459         if (!event)
460                 goto out;
461
462         /*
463          * Use the victim inode instead of the watching inode as the id for
464          * event queue, so event reported on parent is merged with event
465          * reported on child when both directory and child watches exist.
466          */
467         fanotify_init_event(event, (unsigned long)id, mask);
468         if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
469                 event->pid = get_pid(task_pid(current));
470         else
471                 event->pid = get_pid(task_tgid(current));
472
473 out:
474         memalloc_unuse_memcg();
475         return event;
476 }
477
478 /*
479  * Get cached fsid of the filesystem containing the object from any connector.
480  * All connectors are supposed to have the same fsid, but we do not verify that
481  * here.
482  */
483 static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
484 {
485         int type;
486         __kernel_fsid_t fsid = {};
487
488         fsnotify_foreach_obj_type(type) {
489                 struct fsnotify_mark_connector *conn;
490
491                 if (!fsnotify_iter_should_report_type(iter_info, type))
492                         continue;
493
494                 conn = READ_ONCE(iter_info->marks[type]->connector);
495                 /* Mark is just getting destroyed or created? */
496                 if (!conn)
497                         continue;
498                 if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID))
499                         continue;
500                 /* Pairs with smp_wmb() in fsnotify_add_mark_list() */
501                 smp_rmb();
502                 fsid = conn->fsid;
503                 if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
504                         continue;
505                 return fsid;
506         }
507
508         return fsid;
509 }
510
511 static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
512                                  const void *data, int data_type,
513                                  struct inode *dir,
514                                  const struct qstr *file_name, u32 cookie,
515                                  struct fsnotify_iter_info *iter_info)
516 {
517         int ret = 0;
518         struct fanotify_event *event;
519         struct fsnotify_event *fsn_event;
520         __kernel_fsid_t fsid = {};
521
522         BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
523         BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
524         BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
525         BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
526         BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
527         BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
528         BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
529         BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
530         BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
531         BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
532         BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
533         BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
534         BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
535         BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
536         BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
537         BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
538         BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
539         BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
540         BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
541
542         BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19);
543
544         mask = fanotify_group_event_mask(group, iter_info, mask, data,
545                                          data_type);
546         if (!mask)
547                 return 0;
548
549         pr_debug("%s: group=%p mask=%x\n", __func__, group, mask);
550
551         if (fanotify_is_perm_event(mask)) {
552                 /*
553                  * fsnotify_prepare_user_wait() fails if we race with mark
554                  * deletion.  Just let the operation pass in that case.
555                  */
556                 if (!fsnotify_prepare_user_wait(iter_info))
557                         return 0;
558         }
559
560         if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
561                 fsid = fanotify_get_fsid(iter_info);
562                 /* Racing with mark destruction or creation? */
563                 if (!fsid.val[0] && !fsid.val[1])
564                         return 0;
565         }
566
567         event = fanotify_alloc_event(group, mask, data, data_type, dir,
568                                      file_name, &fsid);
569         ret = -ENOMEM;
570         if (unlikely(!event)) {
571                 /*
572                  * We don't queue overflow events for permission events as
573                  * there the access is denied and so no event is in fact lost.
574                  */
575                 if (!fanotify_is_perm_event(mask))
576                         fsnotify_queue_overflow(group);
577                 goto finish;
578         }
579
580         fsn_event = &event->fse;
581         ret = fsnotify_add_event(group, fsn_event, fanotify_merge);
582         if (ret) {
583                 /* Permission events shouldn't be merged */
584                 BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
585                 /* Our event wasn't used in the end. Free it. */
586                 fsnotify_destroy_event(group, fsn_event);
587
588                 ret = 0;
589         } else if (fanotify_is_perm_event(mask)) {
590                 ret = fanotify_get_response(group, FANOTIFY_PERM(event),
591                                             iter_info);
592         }
593 finish:
594         if (fanotify_is_perm_event(mask))
595                 fsnotify_finish_user_wait(iter_info);
596
597         return ret;
598 }
599
600 static void fanotify_free_group_priv(struct fsnotify_group *group)
601 {
602         struct user_struct *user;
603
604         user = group->fanotify_data.user;
605         atomic_dec(&user->fanotify_listeners);
606         free_uid(user);
607 }
608
609 static void fanotify_free_path_event(struct fanotify_event *event)
610 {
611         path_put(fanotify_event_path(event));
612         kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
613 }
614
615 static void fanotify_free_perm_event(struct fanotify_event *event)
616 {
617         path_put(fanotify_event_path(event));
618         kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
619 }
620
621 static void fanotify_free_fid_event(struct fanotify_event *event)
622 {
623         struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
624
625         if (fanotify_fh_has_ext_buf(&ffe->object_fh))
626                 kfree(fanotify_fh_ext_buf(&ffe->object_fh));
627         kmem_cache_free(fanotify_fid_event_cachep, ffe);
628 }
629
630 static void fanotify_free_name_event(struct fanotify_event *event)
631 {
632         struct fanotify_name_event *fne = FANOTIFY_NE(event);
633
634         if (fanotify_fh_has_ext_buf(&fne->dir_fh))
635                 kfree(fanotify_fh_ext_buf(&fne->dir_fh));
636         kfree(fne);
637 }
638
639 static void fanotify_free_event(struct fsnotify_event *fsn_event)
640 {
641         struct fanotify_event *event;
642
643         event = FANOTIFY_E(fsn_event);
644         put_pid(event->pid);
645         switch (event->type) {
646         case FANOTIFY_EVENT_TYPE_PATH:
647                 fanotify_free_path_event(event);
648                 break;
649         case FANOTIFY_EVENT_TYPE_PATH_PERM:
650                 fanotify_free_perm_event(event);
651                 break;
652         case FANOTIFY_EVENT_TYPE_FID:
653                 fanotify_free_fid_event(event);
654                 break;
655         case FANOTIFY_EVENT_TYPE_FID_NAME:
656                 fanotify_free_name_event(event);
657                 break;
658         case FANOTIFY_EVENT_TYPE_OVERFLOW:
659                 kfree(event);
660                 break;
661         default:
662                 WARN_ON_ONCE(1);
663         }
664 }
665
666 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
667 {
668         kmem_cache_free(fanotify_mark_cache, fsn_mark);
669 }
670
671 const struct fsnotify_ops fanotify_fsnotify_ops = {
672         .handle_event = fanotify_handle_event,
673         .free_group_priv = fanotify_free_group_priv,
674         .free_event = fanotify_free_event,
675         .free_mark = fanotify_free_mark,
676 };