blob: f3c40c0e2b867c0ee7f552a034a506c741f3c015 [file] [log] [blame]
Andreas Gruenbacher33d3dff2009-12-17 21:24:29 -05001#include <linux/fanotify.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05002#include <linux/fdtable.h>
3#include <linux/fsnotify_backend.h>
4#include <linux/init.h>
Eric Paris9e66e422009-12-17 21:24:34 -05005#include <linux/jiffies.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05006#include <linux/kernel.h> /* UINT_MAX */
Eric Paris1c529062009-12-17 21:24:28 -05007#include <linux/mount.h>
Eric Paris9e66e422009-12-17 21:24:34 -05008#include <linux/sched.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05009#include <linux/types.h>
Eric Paris9e66e422009-12-17 21:24:34 -050010#include <linux/wait.h>
Eric Parisff0b16a2009-12-17 21:24:25 -050011
Eric Paris767cd462009-12-17 21:24:25 -050012static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
13{
14 pr_debug("%s: old=%p new=%p\n", __func__, old, new);
15
Andreas Gruenbacher32c32632009-12-17 21:24:27 -050016 if (old->to_tell == new->to_tell &&
17 old->data_type == new->data_type &&
18 old->tgid == new->tgid) {
Eric Paris767cd462009-12-17 21:24:25 -050019 switch (old->data_type) {
20 case (FSNOTIFY_EVENT_PATH):
21 if ((old->path.mnt == new->path.mnt) &&
22 (old->path.dentry == new->path.dentry))
23 return true;
24 case (FSNOTIFY_EVENT_NONE):
25 return true;
26 default:
27 BUG();
28 };
29 }
30 return false;
31}
32
Eric Parisf70ab542010-07-28 10:18:37 -040033/* and the list better be locked by something too! */
34static struct fsnotify_event *fanotify_merge(struct list_head *list,
35 struct fsnotify_event *event)
Eric Paris767cd462009-12-17 21:24:25 -050036{
Eric Parisa12a7dd2009-12-17 21:24:25 -050037 struct fsnotify_event_holder *test_holder;
Eric Parisf70ab542010-07-28 10:18:37 -040038 struct fsnotify_event *test_event = NULL;
Eric Parisa12a7dd2009-12-17 21:24:25 -050039 struct fsnotify_event *new_event;
Eric Paris767cd462009-12-17 21:24:25 -050040
41 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
42
Eric Paris767cd462009-12-17 21:24:25 -050043
Eric Parisa12a7dd2009-12-17 21:24:25 -050044 list_for_each_entry_reverse(test_holder, list, event_list) {
Eric Parisf70ab542010-07-28 10:18:37 -040045 if (should_merge(test_holder->event, event)) {
46 test_event = test_holder->event;
Eric Parisa12a7dd2009-12-17 21:24:25 -050047 break;
48 }
49 }
Eric Parisf70ab542010-07-28 10:18:37 -040050
51 if (!test_event)
52 return NULL;
53
54 fsnotify_get_event(test_event);
55
56 /* if they are exactly the same we are done */
57 if (test_event->mask == event->mask)
58 return test_event;
59
60 /*
61 * if the refcnt == 2 this is the only queue
62 * for this event and so we can update the mask
63 * in place.
64 */
65 if (atomic_read(&test_event->refcnt) == 2) {
66 test_event->mask |= event->mask;
67 return test_event;
68 }
69
70 new_event = fsnotify_clone_event(test_event);
71
72 /* done with test_event */
73 fsnotify_put_event(test_event);
74
75 /* couldn't allocate memory, merge was not possible */
76 if (unlikely(!new_event))
77 return ERR_PTR(-ENOMEM);
78
79 /* build new event and replace it on the list */
80 new_event->mask = (test_event->mask | event->mask);
81 fsnotify_replace_event(test_holder, new_event);
82
83 /* we hold a reference on new_event from clone_event */
84 return new_event;
Eric Paris767cd462009-12-17 21:24:25 -050085}
86
Eric Paris9e66e422009-12-17 21:24:34 -050087#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
88static int fanotify_get_response_from_access(struct fsnotify_group *group,
89 struct fsnotify_event *event)
90{
91 int ret;
92
93 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
94
95 wait_event(group->fanotify_data.access_waitq, event->response);
96
97 /* userspace responded, convert to something usable */
98 spin_lock(&event->lock);
99 switch (event->response) {
100 case FAN_ALLOW:
101 ret = 0;
102 break;
103 case FAN_DENY:
104 default:
105 ret = -EPERM;
106 }
107 event->response = 0;
108 spin_unlock(&event->lock);
109
Eric Parisb2d87902009-12-17 21:24:34 -0500110 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
111 group, event, ret);
112
Eric Paris9e66e422009-12-17 21:24:34 -0500113 return ret;
114}
115#endif
116
Eric Parisff0b16a2009-12-17 21:24:25 -0500117static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
118{
Eric Parisf70ab542010-07-28 10:18:37 -0400119 int ret = 0;
Eric Paris9e66e422009-12-17 21:24:34 -0500120 struct fsnotify_event *notify_event = NULL;
Eric Parisff0b16a2009-12-17 21:24:25 -0500121
122 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
123 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
124 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
125 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
126 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
127 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
128 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
Eric Paris9e66e422009-12-17 21:24:34 -0500129 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
130 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
Eric Parisff0b16a2009-12-17 21:24:25 -0500131
132 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
133
Eric Parisf70ab542010-07-28 10:18:37 -0400134 notify_event = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
135 if (IS_ERR(notify_event))
136 return PTR_ERR(notify_event);
Eric Paris9e66e422009-12-17 21:24:34 -0500137
138#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
139 if (event->mask & FAN_ALL_PERM_EVENTS) {
140 /* if we merged we need to wait on the new event */
141 if (notify_event)
142 event = notify_event;
143 ret = fanotify_get_response_from_access(group, event);
144 }
145#endif
146
Eric Paris9e66e422009-12-17 21:24:34 -0500147 if (notify_event)
148 fsnotify_put_event(notify_event);
Eric Parisf70ab542010-07-28 10:18:37 -0400149
Eric Parisff0b16a2009-12-17 21:24:25 -0500150 return ret;
151}
152
Eric Paris1c529062009-12-17 21:24:28 -0500153static bool should_send_vfsmount_event(struct fsnotify_group *group, struct vfsmount *mnt,
Eric Paris32a4df12009-12-17 21:24:33 -0500154 struct inode *inode, __u32 mask)
Eric Parisff0b16a2009-12-17 21:24:25 -0500155{
Eric Paris32a4df12009-12-17 21:24:33 -0500156 struct fsnotify_mark *mnt_mark;
157 struct fsnotify_mark *inode_mark;
Eric Parisff0b16a2009-12-17 21:24:25 -0500158
Eric Paris1c529062009-12-17 21:24:28 -0500159 pr_debug("%s: group=%p vfsmount=%p mask=%x\n",
160 __func__, group, mnt, mask);
Eric Parisff0b16a2009-12-17 21:24:25 -0500161
Eric Paris32a4df12009-12-17 21:24:33 -0500162 mnt_mark = fsnotify_find_vfsmount_mark(group, mnt);
163 if (!mnt_mark)
Eric Parisff0b16a2009-12-17 21:24:25 -0500164 return false;
165
Eric Paris32a4df12009-12-17 21:24:33 -0500166 mask &= mnt_mark->mask;
167 mask &= ~mnt_mark->ignored_mask;
168
169 if (mask) {
170 inode_mark = fsnotify_find_inode_mark(group, inode);
171 if (inode_mark) {
172 mask &= ~inode_mark->ignored_mask;
173 fsnotify_put_mark(inode_mark);
174 }
175 }
Eric Paris1c529062009-12-17 21:24:28 -0500176
177 /* find took a reference */
Eric Paris32a4df12009-12-17 21:24:33 -0500178 fsnotify_put_mark(mnt_mark);
Eric Paris1c529062009-12-17 21:24:28 -0500179
Eric Paris32a4df12009-12-17 21:24:33 -0500180 return mask;
Eric Paris1c529062009-12-17 21:24:28 -0500181}
182
183static bool should_send_inode_event(struct fsnotify_group *group, struct inode *inode,
184 __u32 mask)
185{
186 struct fsnotify_mark *fsn_mark;
Eric Paris1c529062009-12-17 21:24:28 -0500187
188 pr_debug("%s: group=%p inode=%p mask=%x\n",
189 __func__, group, inode, mask);
Eric Parisff0b16a2009-12-17 21:24:25 -0500190
Eric Paris5444e292009-12-17 21:24:27 -0500191 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Parisff0b16a2009-12-17 21:24:25 -0500192 if (!fsn_mark)
193 return false;
194
195 /* if the event is for a child and this inode doesn't care about
196 * events on the child, don't send it! */
197 if ((mask & FS_EVENT_ON_CHILD) &&
198 !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
Eric Paris32a4df12009-12-17 21:24:33 -0500199 mask = 0;
Eric Parisff0b16a2009-12-17 21:24:25 -0500200 } else {
201 /*
202 * We care about children, but do we care about this particular
203 * type of event?
204 */
Eric Paris32a4df12009-12-17 21:24:33 -0500205 mask &= ~FS_EVENT_ON_CHILD;
206 mask &= fsn_mark->mask;
207 mask &= ~fsn_mark->ignored_mask;
Eric Parisff0b16a2009-12-17 21:24:25 -0500208 }
209
210 /* find took a reference */
211 fsnotify_put_mark(fsn_mark);
212
Eric Paris32a4df12009-12-17 21:24:33 -0500213 return mask;
Eric Parisff0b16a2009-12-17 21:24:25 -0500214}
215
Eric Paris1c529062009-12-17 21:24:28 -0500216static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *to_tell,
217 struct vfsmount *mnt, __u32 mask, void *data,
218 int data_type)
219{
220 pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_type=%d\n",
221 __func__, group, to_tell, mnt, mask, data, data_type);
222
223 /* sorry, fanotify only gives a damn about files and dirs */
224 if (!S_ISREG(to_tell->i_mode) &&
225 !S_ISDIR(to_tell->i_mode))
226 return false;
227
228 /* if we don't have enough info to send an event to userspace say no */
229 if (data_type != FSNOTIFY_EVENT_PATH)
230 return false;
231
232 if (mnt)
Eric Paris32a4df12009-12-17 21:24:33 -0500233 return should_send_vfsmount_event(group, mnt, to_tell, mask);
Eric Paris1c529062009-12-17 21:24:28 -0500234 else
235 return should_send_inode_event(group, to_tell, mask);
236}
237
Eric Parisff0b16a2009-12-17 21:24:25 -0500238const struct fsnotify_ops fanotify_fsnotify_ops = {
239 .handle_event = fanotify_handle_event,
240 .should_send_event = fanotify_should_send_event,
241 .free_group_priv = NULL,
242 .free_event_priv = NULL,
243 .freeing_mark = NULL,
244};