blob: 881067dc79237bcc3cf28921cf1a4f8920ca7423 [file] [log] [blame]
Eric Parisff0b16a2009-12-17 21:24:25 -05001#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>
6
7#include "fanotify.h"
8
Eric Paris767cd462009-12-17 21:24:25 -05009static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
10{
11 pr_debug("%s: old=%p new=%p\n", __func__, old, new);
12
Andreas Gruenbacher32c32632009-12-17 21:24:27 -050013 if (old->to_tell == new->to_tell &&
14 old->data_type == new->data_type &&
15 old->tgid == new->tgid) {
Eric Paris767cd462009-12-17 21:24:25 -050016 switch (old->data_type) {
17 case (FSNOTIFY_EVENT_PATH):
18 if ((old->path.mnt == new->path.mnt) &&
19 (old->path.dentry == new->path.dentry))
20 return true;
21 case (FSNOTIFY_EVENT_NONE):
22 return true;
23 default:
24 BUG();
25 };
26 }
27 return false;
28}
29
30static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
31{
Eric Parisa12a7dd2009-12-17 21:24:25 -050032 struct fsnotify_event_holder *test_holder;
Eric Paris767cd462009-12-17 21:24:25 -050033 struct fsnotify_event *test_event;
Eric Parisa12a7dd2009-12-17 21:24:25 -050034 struct fsnotify_event *new_event;
35 int ret = 0;
Eric Paris767cd462009-12-17 21:24:25 -050036
37 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
38
39 /* and the list better be locked by something too! */
40
Eric Parisa12a7dd2009-12-17 21:24:25 -050041 list_for_each_entry_reverse(test_holder, list, event_list) {
42 test_event = test_holder->event;
43 if (should_merge(test_event, event)) {
44 ret = -EEXIST;
Eric Paris767cd462009-12-17 21:24:25 -050045
Eric Parisa12a7dd2009-12-17 21:24:25 -050046 /* if they are exactly the same we are done */
47 if (test_event->mask == event->mask)
48 goto out;
49
Eric Paris9dced012009-12-17 21:24:25 -050050 /*
51 * if the refcnt == 1 this is the only queue
52 * for this event and so we can update the mask
53 * in place.
54 */
55 if (atomic_read(&test_event->refcnt) == 1) {
56 test_event->mask |= event->mask;
57 goto out;
58 }
59
Eric Parisa12a7dd2009-12-17 21:24:25 -050060 /* can't allocate memory, merge was no possible */
61 new_event = fsnotify_clone_event(test_event);
62 if (unlikely(!new_event)) {
63 ret = 0;
64 goto out;
65 }
66
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);
72
73 break;
74 }
75 }
76out:
77 return ret;
Eric Paris767cd462009-12-17 21:24:25 -050078}
79
Eric Parisff0b16a2009-12-17 21:24:25 -050080static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
81{
82 int ret;
83
84
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);
92
93 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
94
Eric Paris767cd462009-12-17 21:24:25 -050095 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 */
97 if (ret == -EEXIST)
98 ret = 0;
Eric Parisff0b16a2009-12-17 21:24:25 -050099 return ret;
100}
101
102static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *inode,
103 struct vfsmount *mnt, __u32 mask, void *data,
104 int data_type)
105{
106 struct fsnotify_mark *fsn_mark;
107 bool send;
108
109 pr_debug("%s: group=%p inode=%p mask=%x data=%p data_type=%d\n",
110 __func__, group, inode, mask, data, data_type);
111
112 /* sorry, fanotify only gives a damn about files and dirs */
113 if (!S_ISREG(inode->i_mode) &&
114 !S_ISDIR(inode->i_mode))
115 return false;
116
117 /* if we don't have enough info to send an event to userspace say no */
118 if (data_type != FSNOTIFY_EVENT_PATH)
119 return false;
120
121 fsn_mark = fsnotify_find_mark(group, inode);
122 if (!fsn_mark)
123 return false;
124
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)) {
129 send = false;
130 } else {
131 /*
132 * We care about children, but do we care about this particular
133 * type of event?
134 */
135 mask = (mask & ~FS_EVENT_ON_CHILD);
136 send = (fsn_mark->mask & mask);
137 }
138
139 /* find took a reference */
140 fsnotify_put_mark(fsn_mark);
141
142 return send;
143}
144
145const 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,
151};