1 #include <linux/fdtable.h>
2 #include <linux/fsnotify_backend.h>
3 #include <linux/init.h>
4 #include <linux/kernel.h> /* UINT_MAX */
5 #include <linux/types.h>
9 static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
11 pr_debug("%s: old=%p new=%p\n", __func__, old, new);
13 if ((old->to_tell == new->to_tell) &&
14 (old->data_type == new->data_type)) {
15 switch (old->data_type) {
16 case (FSNOTIFY_EVENT_PATH):
17 if ((old->path.mnt == new->path.mnt) &&
18 (old->path.dentry == new->path.dentry))
20 case (FSNOTIFY_EVENT_NONE):
29 static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
31 struct fsnotify_event_holder *test_holder;
32 struct fsnotify_event *test_event;
33 struct fsnotify_event *new_event;
36 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
38 /* and the list better be locked by something too! */
40 list_for_each_entry_reverse(test_holder, list, event_list) {
41 test_event = test_holder->event;
42 if (should_merge(test_event, event)) {
45 /* if they are exactly the same we are done */
46 if (test_event->mask == event->mask)
49 /* can't allocate memory, merge was no possible */
50 new_event = fsnotify_clone_event(test_event);
51 if (unlikely(!new_event)) {
56 /* build new event and replace it on the list */
57 new_event->mask = (test_event->mask | event->mask);
58 fsnotify_replace_event(test_holder, new_event);
59 /* match ref from fsnotify_clone_event() */
60 fsnotify_put_event(new_event);
69 static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
74 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
75 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
76 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
77 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
78 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
79 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
80 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
82 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
84 ret = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
85 /* -EEXIST means this event was merged with another, not that it was an error */
91 static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *inode,
92 struct vfsmount *mnt, __u32 mask, void *data,
95 struct fsnotify_mark *fsn_mark;
98 pr_debug("%s: group=%p inode=%p mask=%x data=%p data_type=%d\n",
99 __func__, group, inode, mask, data, data_type);
101 /* sorry, fanotify only gives a damn about files and dirs */
102 if (!S_ISREG(inode->i_mode) &&
103 !S_ISDIR(inode->i_mode))
106 /* if we don't have enough info to send an event to userspace say no */
107 if (data_type != FSNOTIFY_EVENT_PATH)
110 fsn_mark = fsnotify_find_mark(group, inode);
114 /* if the event is for a child and this inode doesn't care about
115 * events on the child, don't send it! */
116 if ((mask & FS_EVENT_ON_CHILD) &&
117 !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
121 * We care about children, but do we care about this particular
124 mask = (mask & ~FS_EVENT_ON_CHILD);
125 send = (fsn_mark->mask & mask);
128 /* find took a reference */
129 fsnotify_put_mark(fsn_mark);
134 const struct fsnotify_ops fanotify_fsnotify_ops = {
135 .handle_event = fanotify_handle_event,
136 .should_send_event = fanotify_should_send_event,
137 .free_group_priv = NULL,
138 .free_event_priv = NULL,
139 .freeing_mark = NULL,