1
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>
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
21static bool fanotify_path_equal(struct path *p1, struct path *p2)
22{
23 return p1->mnt == p2->mnt && p1->dentry == p2->dentry;
24}
25
26static 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
32static 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
38static 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
44static 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
54static unsigned int fanotify_hash_fh(struct fanotify_fh *fh)
55{
56 long salt = (long)fh->type | (long)fh->len << 8;
57
58
59
60
61 return full_name_hash((void *)salt, fanotify_fh_buf(fh), fh->len);
62}
63
64static bool fanotify_fid_event_equal(struct fanotify_fid_event *ffe1,
65 struct fanotify_fid_event *ffe2)
66{
67
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
75static 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
98static 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
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
114static 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
125
126
127
128
129
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
152#define FANOTIFY_MAX_MERGE_EVENTS 128
153
154
155static int fanotify_merge(struct fsnotify_group *group,
156 struct fsnotify_event *event)
157{
158 struct fanotify_event *old, *new = FANOTIFY_E(event);
159 unsigned int bucket = fanotify_event_hash_bucket(group, new);
160 struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
161 int i = 0;
162
163 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
164 group, event, bucket);
165
166
167
168
169
170
171 if (fanotify_is_perm_event(new->mask))
172 return 0;
173
174 hlist_for_each_entry(old, hlist, merge_list) {
175 if (++i > FANOTIFY_MAX_MERGE_EVENTS)
176 break;
177 if (fanotify_should_merge(old, new)) {
178 old->mask |= new->mask;
179 return 1;
180 }
181 }
182
183 return 0;
184}
185
186
187
188
189
190
191
192
193static int fanotify_get_response(struct fsnotify_group *group,
194 struct fanotify_perm_event *event,
195 struct fsnotify_iter_info *iter_info)
196{
197 int ret;
198
199 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
200
201 ret = wait_event_killable(group->fanotify_data.access_waitq,
202 event->state == FAN_EVENT_ANSWERED);
203
204 if (ret < 0) {
205 spin_lock(&group->notification_lock);
206
207 if (event->state == FAN_EVENT_REPORTED) {
208
209 event->state = FAN_EVENT_CANCELED;
210 spin_unlock(&group->notification_lock);
211 return ret;
212 }
213
214 if (event->state == FAN_EVENT_INIT) {
215 fsnotify_remove_queued_event(group, &event->fae.fse);
216
217 WARN_ON_ONCE(!hlist_unhashed(&event->fae.merge_list));
218 }
219
220
221
222
223
224 spin_unlock(&group->notification_lock);
225 goto out;
226 }
227
228
229 switch (event->response & ~FAN_AUDIT) {
230 case FAN_ALLOW:
231 ret = 0;
232 break;
233 case FAN_DENY:
234 default:
235 ret = -EPERM;
236 }
237
238
239 if (event->response & FAN_AUDIT)
240 audit_fanotify(event->response & ~FAN_AUDIT);
241
242 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
243 group, event, ret);
244out:
245 fsnotify_destroy_event(group, &event->fae.fse);
246
247 return ret;
248}
249
250
251
252
253
254
255
256static u32 fanotify_group_event_mask(struct fsnotify_group *group,
257 struct fsnotify_iter_info *iter_info,
258 u32 event_mask, const void *data,
259 int data_type, struct inode *dir)
260{
261 __u32 marks_mask = 0, marks_ignored_mask = 0;
262 __u32 test_mask, user_mask = FANOTIFY_OUTGOING_EVENTS |
263 FANOTIFY_EVENT_FLAGS;
264 const struct path *path = fsnotify_data_path(data, data_type);
265 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
266 struct fsnotify_mark *mark;
267 int type;
268
269 pr_debug("%s: report_mask=%x mask=%x data=%p data_type=%d\n",
270 __func__, iter_info->report_mask, event_mask, data, data_type);
271
272 if (!fid_mode) {
273
274 if (!path)
275 return 0;
276
277 if (!d_is_reg(path->dentry) && !d_can_lookup(path->dentry))
278 return 0;
279 } else if (!(fid_mode & FAN_REPORT_FID)) {
280
281 if (!dir && !(event_mask & FS_ISDIR))
282 return 0;
283 }
284
285 fsnotify_foreach_obj_type(type) {
286 if (!fsnotify_iter_should_report_type(iter_info, type))
287 continue;
288 mark = iter_info->marks[type];
289
290
291 marks_ignored_mask |= mark->ignored_mask;
292
293
294
295
296
297 if (event_mask & FS_ISDIR && !(mark->mask & FS_ISDIR))
298 continue;
299
300
301
302
303
304 if (type == FSNOTIFY_OBJ_TYPE_PARENT &&
305 !(mark->mask & FS_EVENT_ON_CHILD))
306 continue;
307
308 marks_mask |= mark->mask;
309 }
310
311 test_mask = event_mask & marks_mask & ~marks_ignored_mask;
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326 if (fid_mode) {
327
328 if (!(test_mask & ~FANOTIFY_EVENT_FLAGS))
329 return 0;
330 } else {
331 user_mask &= ~FANOTIFY_EVENT_FLAGS;
332 }
333
334 return test_mask & user_mask;
335}
336
337
338
339
340
341
342
343static int fanotify_encode_fh_len(struct inode *inode)
344{
345 int dwords = 0;
346
347 if (!inode)
348 return 0;
349
350 exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
351
352 return dwords << 2;
353}
354
355
356
357
358
359
360
361static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
362 unsigned int fh_len, unsigned int *hash,
363 gfp_t gfp)
364{
365 int dwords, type = 0;
366 char *ext_buf = NULL;
367 void *buf = fh->buf;
368 int err;
369
370 fh->type = FILEID_ROOT;
371 fh->len = 0;
372 fh->flags = 0;
373 if (!inode)
374 return 0;
375
376
377
378
379
380 err = -ENOENT;
381 if (fh_len < 4 || WARN_ON_ONCE(fh_len % 4))
382 goto out_err;
383
384
385 if (gfp && fh_len > FANOTIFY_INLINE_FH_LEN) {
386
387 err = -ENOMEM;
388 ext_buf = kmalloc(fh_len, gfp);
389 if (!ext_buf)
390 goto out_err;
391
392 *fanotify_fh_ext_buf_ptr(fh) = ext_buf;
393 buf = ext_buf;
394 fh->flags |= FANOTIFY_FH_FLAG_EXT_BUF;
395 }
396
397 dwords = fh_len >> 2;
398 type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
399 err = -EINVAL;
400 if (!type || type == FILEID_INVALID || fh_len != dwords << 2)
401 goto out_err;
402
403 fh->type = type;
404 fh->len = fh_len;
405
406
407 *hash ^= fanotify_hash_fh(fh);
408
409 return FANOTIFY_FH_HDR_LEN + fh_len;
410
411out_err:
412 pr_warn_ratelimited("fanotify: failed to encode fid (type=%d, len=%d, err=%i)\n",
413 type, fh_len, err);
414 kfree(ext_buf);
415 *fanotify_fh_ext_buf_ptr(fh) = NULL;
416
417 fh->type = FILEID_INVALID;
418 fh->len = 0;
419 return 0;
420}
421
422
423
424
425
426
427
428
429
430static struct inode *fanotify_fid_inode(u32 event_mask, const void *data,
431 int data_type, struct inode *dir)
432{
433 if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
434 return dir;
435
436 return fsnotify_data_inode(data, data_type);
437}
438
439
440
441
442
443
444
445
446
447static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data,
448 int data_type, struct inode *dir)
449{
450 struct inode *inode = fsnotify_data_inode(data, data_type);
451
452 if (event_mask & ALL_FSNOTIFY_DIRENT_EVENTS)
453 return dir;
454
455 if (S_ISDIR(inode->i_mode))
456 return inode;
457
458 return dir;
459}
460
461static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
462 unsigned int *hash,
463 gfp_t gfp)
464{
465 struct fanotify_path_event *pevent;
466
467 pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
468 if (!pevent)
469 return NULL;
470
471 pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
472 pevent->path = *path;
473 *hash ^= fanotify_hash_path(path);
474 path_get(path);
475
476 return &pevent->fae;
477}
478
479static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
480 gfp_t gfp)
481{
482 struct fanotify_perm_event *pevent;
483
484 pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
485 if (!pevent)
486 return NULL;
487
488 pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
489 pevent->response = 0;
490 pevent->state = FAN_EVENT_INIT;
491 pevent->path = *path;
492 path_get(path);
493
494 return &pevent->fae;
495}
496
497static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
498 __kernel_fsid_t *fsid,
499 unsigned int *hash,
500 gfp_t gfp)
501{
502 struct fanotify_fid_event *ffe;
503
504 ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
505 if (!ffe)
506 return NULL;
507
508 ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
509 ffe->fsid = *fsid;
510 *hash ^= fanotify_hash_fsid(fsid);
511 fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id),
512 hash, gfp);
513
514 return &ffe->fae;
515}
516
517static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
518 __kernel_fsid_t *fsid,
519 const struct qstr *name,
520 struct inode *child,
521 unsigned int *hash,
522 gfp_t gfp)
523{
524 struct fanotify_name_event *fne;
525 struct fanotify_info *info;
526 struct fanotify_fh *dfh, *ffh;
527 unsigned int dir_fh_len = fanotify_encode_fh_len(id);
528 unsigned int child_fh_len = fanotify_encode_fh_len(child);
529 unsigned int size;
530
531 size = sizeof(*fne) + FANOTIFY_FH_HDR_LEN + dir_fh_len;
532 if (child_fh_len)
533 size += FANOTIFY_FH_HDR_LEN + child_fh_len;
534 if (name)
535 size += name->len + 1;
536 fne = kmalloc(size, gfp);
537 if (!fne)
538 return NULL;
539
540 fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
541 fne->fsid = *fsid;
542 *hash ^= fanotify_hash_fsid(fsid);
543 info = &fne->info;
544 fanotify_info_init(info);
545 dfh = fanotify_info_dir_fh(info);
546 info->dir_fh_totlen = fanotify_encode_fh(dfh, id, dir_fh_len, hash, 0);
547 if (child_fh_len) {
548 ffh = fanotify_info_file_fh(info);
549 info->file_fh_totlen = fanotify_encode_fh(ffh, child,
550 child_fh_len, hash, 0);
551 }
552 if (name) {
553 long salt = name->len;
554
555 fanotify_info_copy_name(info, name);
556 *hash ^= full_name_hash((void *)salt, name->name, name->len);
557 }
558
559 pr_debug("%s: ino=%lu size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n",
560 __func__, id->i_ino, size, dir_fh_len, child_fh_len,
561 info->name_len, info->name_len, fanotify_info_name(info));
562
563 return &fne->fae;
564}
565
566static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
567 u32 mask, const void *data,
568 int data_type, struct inode *dir,
569 const struct qstr *file_name,
570 __kernel_fsid_t *fsid)
571{
572 struct fanotify_event *event = NULL;
573 gfp_t gfp = GFP_KERNEL_ACCOUNT;
574 struct inode *id = fanotify_fid_inode(mask, data, data_type, dir);
575 struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir);
576 const struct path *path = fsnotify_data_path(data, data_type);
577 unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
578 struct mem_cgroup *old_memcg;
579 struct inode *child = NULL;
580 bool name_event = false;
581 unsigned int hash = 0;
582 bool ondir = mask & FAN_ONDIR;
583 struct pid *pid;
584
585 if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
586
587
588
589
590
591 if ((fid_mode & FAN_REPORT_FID) && id != dirid && !ondir)
592 child = id;
593
594 id = dirid;
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609 if (!(fid_mode & FAN_REPORT_NAME)) {
610 name_event = !!child;
611 file_name = NULL;
612 } else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) || !ondir) {
613 name_event = true;
614 }
615 }
616
617
618
619
620
621
622
623 if (group->max_events == UINT_MAX)
624 gfp |= __GFP_NOFAIL;
625 else
626 gfp |= __GFP_RETRY_MAYFAIL;
627
628
629 old_memcg = set_active_memcg(group->memcg);
630
631 if (fanotify_is_perm_event(mask)) {
632 event = fanotify_alloc_perm_event(path, gfp);
633 } else if (name_event && (file_name || child)) {
634 event = fanotify_alloc_name_event(id, fsid, file_name, child,
635 &hash, gfp);
636 } else if (fid_mode) {
637 event = fanotify_alloc_fid_event(id, fsid, &hash, gfp);
638 } else {
639 event = fanotify_alloc_path_event(path, &hash, gfp);
640 }
641
642 if (!event)
643 goto out;
644
645 if (FAN_GROUP_FLAG(group, FAN_REPORT_TID))
646 pid = get_pid(task_pid(current));
647 else
648 pid = get_pid(task_tgid(current));
649
650
651 hash ^= hash_long((unsigned long)pid | ondir, FANOTIFY_EVENT_HASH_BITS);
652 fanotify_init_event(event, hash, mask);
653 event->pid = pid;
654
655out:
656 set_active_memcg(old_memcg);
657 return event;
658}
659
660
661
662
663
664
665static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)
666{
667 int type;
668 __kernel_fsid_t fsid = {};
669
670 fsnotify_foreach_obj_type(type) {
671 struct fsnotify_mark_connector *conn;
672
673 if (!fsnotify_iter_should_report_type(iter_info, type))
674 continue;
675
676 conn = READ_ONCE(iter_info->marks[type]->connector);
677
678 if (!conn)
679 continue;
680 if (!(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID))
681 continue;
682
683 smp_rmb();
684 fsid = conn->fsid;
685 if (WARN_ON_ONCE(!fsid.val[0] && !fsid.val[1]))
686 continue;
687 return fsid;
688 }
689
690 return fsid;
691}
692
693
694
695
696static void fanotify_insert_event(struct fsnotify_group *group,
697 struct fsnotify_event *fsn_event)
698{
699 struct fanotify_event *event = FANOTIFY_E(fsn_event);
700 unsigned int bucket = fanotify_event_hash_bucket(group, event);
701 struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
702
703 assert_spin_locked(&group->notification_lock);
704
705 pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
706 group, event, bucket);
707
708 hlist_add_head(&event->merge_list, hlist);
709}
710
711static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
712 const void *data, int data_type,
713 struct inode *dir,
714 const struct qstr *file_name, u32 cookie,
715 struct fsnotify_iter_info *iter_info)
716{
717 int ret = 0;
718 struct fanotify_event *event;
719 struct fsnotify_event *fsn_event;
720 __kernel_fsid_t fsid = {};
721
722 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
723 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
724 BUILD_BUG_ON(FAN_ATTRIB != FS_ATTRIB);
725 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
726 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
727 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
728 BUILD_BUG_ON(FAN_MOVED_TO != FS_MOVED_TO);
729 BUILD_BUG_ON(FAN_MOVED_FROM != FS_MOVED_FROM);
730 BUILD_BUG_ON(FAN_CREATE != FS_CREATE);
731 BUILD_BUG_ON(FAN_DELETE != FS_DELETE);
732 BUILD_BUG_ON(FAN_DELETE_SELF != FS_DELETE_SELF);
733 BUILD_BUG_ON(FAN_MOVE_SELF != FS_MOVE_SELF);
734 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
735 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
736 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
737 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
738 BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
739 BUILD_BUG_ON(FAN_OPEN_EXEC != FS_OPEN_EXEC);
740 BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
741
742 BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 19);
743
744 mask = fanotify_group_event_mask(group, iter_info, mask, data,
745 data_type, dir);
746 if (!mask)
747 return 0;
748
749 pr_debug("%s: group=%p mask=%x\n", __func__, group, mask);
750
751 if (fanotify_is_perm_event(mask)) {
752
753
754
755
756 if (!fsnotify_prepare_user_wait(iter_info))
757 return 0;
758 }
759
760 if (FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS)) {
761 fsid = fanotify_get_fsid(iter_info);
762
763 if (!fsid.val[0] && !fsid.val[1])
764 return 0;
765 }
766
767 event = fanotify_alloc_event(group, mask, data, data_type, dir,
768 file_name, &fsid);
769 ret = -ENOMEM;
770 if (unlikely(!event)) {
771
772
773
774
775 if (!fanotify_is_perm_event(mask))
776 fsnotify_queue_overflow(group);
777 goto finish;
778 }
779
780 fsn_event = &event->fse;
781 ret = fsnotify_add_event(group, fsn_event, fanotify_merge,
782 fanotify_is_hashed_event(mask) ?
783 fanotify_insert_event : NULL);
784 if (ret) {
785
786 BUG_ON(ret == 1 && mask & FANOTIFY_PERM_EVENTS);
787
788 fsnotify_destroy_event(group, fsn_event);
789
790 ret = 0;
791 } else if (fanotify_is_perm_event(mask)) {
792 ret = fanotify_get_response(group, FANOTIFY_PERM(event),
793 iter_info);
794 }
795finish:
796 if (fanotify_is_perm_event(mask))
797 fsnotify_finish_user_wait(iter_info);
798
799 return ret;
800}
801
802static void fanotify_free_group_priv(struct fsnotify_group *group)
803{
804 kfree(group->fanotify_data.merge_hash);
805 if (group->fanotify_data.ucounts)
806 dec_ucount(group->fanotify_data.ucounts,
807 UCOUNT_FANOTIFY_GROUPS);
808}
809
810static void fanotify_free_path_event(struct fanotify_event *event)
811{
812 path_put(fanotify_event_path(event));
813 kmem_cache_free(fanotify_path_event_cachep, FANOTIFY_PE(event));
814}
815
816static void fanotify_free_perm_event(struct fanotify_event *event)
817{
818 path_put(fanotify_event_path(event));
819 kmem_cache_free(fanotify_perm_event_cachep, FANOTIFY_PERM(event));
820}
821
822static void fanotify_free_fid_event(struct fanotify_event *event)
823{
824 struct fanotify_fid_event *ffe = FANOTIFY_FE(event);
825
826 if (fanotify_fh_has_ext_buf(&ffe->object_fh))
827 kfree(fanotify_fh_ext_buf(&ffe->object_fh));
828 kmem_cache_free(fanotify_fid_event_cachep, ffe);
829}
830
831static void fanotify_free_name_event(struct fanotify_event *event)
832{
833 kfree(FANOTIFY_NE(event));
834}
835
836static void fanotify_free_event(struct fsnotify_event *fsn_event)
837{
838 struct fanotify_event *event;
839
840 event = FANOTIFY_E(fsn_event);
841 put_pid(event->pid);
842 switch (event->type) {
843 case FANOTIFY_EVENT_TYPE_PATH:
844 fanotify_free_path_event(event);
845 break;
846 case FANOTIFY_EVENT_TYPE_PATH_PERM:
847 fanotify_free_perm_event(event);
848 break;
849 case FANOTIFY_EVENT_TYPE_FID:
850 fanotify_free_fid_event(event);
851 break;
852 case FANOTIFY_EVENT_TYPE_FID_NAME:
853 fanotify_free_name_event(event);
854 break;
855 case FANOTIFY_EVENT_TYPE_OVERFLOW:
856 kfree(event);
857 break;
858 default:
859 WARN_ON_ONCE(1);
860 }
861}
862
863static void fanotify_freeing_mark(struct fsnotify_mark *mark,
864 struct fsnotify_group *group)
865{
866 if (!FAN_GROUP_FLAG(group, FAN_UNLIMITED_MARKS))
867 dec_ucount(group->fanotify_data.ucounts, UCOUNT_FANOTIFY_MARKS);
868}
869
870static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
871{
872 kmem_cache_free(fanotify_mark_cache, fsn_mark);
873}
874
875const struct fsnotify_ops fanotify_fsnotify_ops = {
876 .handle_event = fanotify_handle_event,
877 .free_group_priv = fanotify_free_group_priv,
878 .free_event = fanotify_free_event,
879 .freeing_mark = fanotify_freeing_mark,
880 .free_mark = fanotify_free_mark,
881};
882