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 old->tgid == new->tgid) {
16 switch (old->data_type) {
17 case (FSNOTIFY_EVENT_PATH):
18 if ((old->path.mnt == new->path.mnt) &&
19 (old->path.dentry == new->path.dentry))
21 case (FSNOTIFY_EVENT_NONE):
30 static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
32 struct fsnotify_event_holder *test_holder;
33 struct fsnotify_event *test_event;
34 struct fsnotify_event *new_event;
37 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
39 /* and the list better be locked by something too! */
41 list_for_each_entry_reverse(test_holder, list, event_list) {
42 test_event = test_holder->event;
43 if (should_merge(test_event, event)) {
46 /* if they are exactly the same we are done */
47 if (test_event->mask == event->mask)
51 * if the refcnt == 1 this is the only queue
52 * for this event and so we can update the mask
55 if (atomic_read(&test_event->refcnt) == 1) {
56 test_event->mask |= event->mask;
60 /* can't allocate memory, merge was no possible */
61 new_event = fsnotify_clone_event(test_event);
62 if (unlikely(!new_event)) {
67 /* build new event and replace it on the list */
68 new_event->mask = (test_event->mask | event->mask);
69 fsnotify_replace_event(test_holder, new_event);
70 /* match ref from fsnotify_clone_event() */
71 fsnotify_put_event(new_event);
80 static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
85 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
86 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
87 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
88 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
89 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
90 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
91 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
93 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
95 ret = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
96 /* -EEXIST means this event was merged with another, not that it was an error */
102 static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *inode,
103 struct vfsmount *mnt, __u32 mask, void *data,
106 struct fsnotify_mark *fsn_mark;
109 pr_debug("%s: group=%p inode=%p mask=%x data=%p data_type=%d\n",
110 __func__, group, inode, mask, data, data_type);
112 /* sorry, fanotify only gives a damn about files and dirs */
113 if (!S_ISREG(inode->i_mode) &&
114 !S_ISDIR(inode->i_mode))
117 /* if we don't have enough info to send an event to userspace say no */
118 if (data_type != FSNOTIFY_EVENT_PATH)
121 fsn_mark = fsnotify_find_mark(group, inode);
125 /* if the event is for a child and this inode doesn't care about
126 * events on the child, don't send it! */
127 if ((mask & FS_EVENT_ON_CHILD) &&
128 !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
132 * We care about children, but do we care about this particular
135 mask = (mask & ~FS_EVENT_ON_CHILD);
136 send = (fsn_mark->mask & mask);
139 /* find took a reference */
140 fsnotify_put_mark(fsn_mark);
145 const struct fsnotify_ops fanotify_fsnotify_ops = {
146 .handle_event = fanotify_handle_event,
147 .should_send_event = fanotify_should_send_event,
148 .free_group_priv = NULL,
149 .free_event_priv = NULL,
150 .freeing_mark = NULL,