blob: bbcfccd4a8ea70138d59f65f398692df33d6cfb7 [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 Paris43ed7e12009-12-17 21:24:34 -050033/* Note, if we return an event in *arg that a reference is being held... */
Eric Paris6e5f77b2009-12-17 21:24:34 -050034static int fanotify_merge(struct list_head *list,
35 struct fsnotify_event *event,
36 void **arg)
Eric Paris767cd462009-12-17 21:24:25 -050037{
Eric Parisa12a7dd2009-12-17 21:24:25 -050038 struct fsnotify_event_holder *test_holder;
Eric Paris767cd462009-12-17 21:24:25 -050039 struct fsnotify_event *test_event;
Eric Parisa12a7dd2009-12-17 21:24:25 -050040 struct fsnotify_event *new_event;
Eric Paris43ed7e12009-12-17 21:24:34 -050041 struct fsnotify_event **return_event = (struct fsnotify_event **)arg;
Eric Parisa12a7dd2009-12-17 21:24:25 -050042 int ret = 0;
Eric Paris767cd462009-12-17 21:24:25 -050043
44 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
45
Eric Paris43ed7e12009-12-17 21:24:34 -050046 *return_event = NULL;
47
Eric Paris767cd462009-12-17 21:24:25 -050048 /* and the list better be locked by something too! */
49
Eric Parisa12a7dd2009-12-17 21:24:25 -050050 list_for_each_entry_reverse(test_holder, list, event_list) {
51 test_event = test_holder->event;
52 if (should_merge(test_event, event)) {
Eric Paris43ed7e12009-12-17 21:24:34 -050053 fsnotify_get_event(test_event);
54 *return_event = test_event;
Eric Paris767cd462009-12-17 21:24:25 -050055
Eric Paris43ed7e12009-12-17 21:24:34 -050056 ret = -EEXIST;
Eric Parisa12a7dd2009-12-17 21:24:25 -050057 /* if they are exactly the same we are done */
58 if (test_event->mask == event->mask)
59 goto out;
60
Eric Paris9dced012009-12-17 21:24:25 -050061 /*
62 * if the refcnt == 1 this is the only queue
63 * for this event and so we can update the mask
64 * in place.
65 */
66 if (atomic_read(&test_event->refcnt) == 1) {
67 test_event->mask |= event->mask;
68 goto out;
69 }
70
Eric Parisa12a7dd2009-12-17 21:24:25 -050071 /* can't allocate memory, merge was no possible */
72 new_event = fsnotify_clone_event(test_event);
73 if (unlikely(!new_event)) {
74 ret = 0;
75 goto out;
76 }
77
Eric Paris43ed7e12009-12-17 21:24:34 -050078 /* we didn't return the test_event, so drop that ref */
79 fsnotify_put_event(test_event);
80 /* the reference we return on new_event is from clone */
81 *return_event = new_event;
82
Eric Parisa12a7dd2009-12-17 21:24:25 -050083 /* build new event and replace it on the list */
84 new_event->mask = (test_event->mask | event->mask);
85 fsnotify_replace_event(test_holder, new_event);
Eric Parisa12a7dd2009-12-17 21:24:25 -050086
87 break;
88 }
89 }
90out:
91 return ret;
Eric Paris767cd462009-12-17 21:24:25 -050092}
93
Eric Paris9e66e422009-12-17 21:24:34 -050094#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
95static int fanotify_get_response_from_access(struct fsnotify_group *group,
96 struct fsnotify_event *event)
97{
98 int ret;
99
100 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
101
102 wait_event(group->fanotify_data.access_waitq, event->response);
103
104 /* userspace responded, convert to something usable */
105 spin_lock(&event->lock);
106 switch (event->response) {
107 case FAN_ALLOW:
108 ret = 0;
109 break;
110 case FAN_DENY:
111 default:
112 ret = -EPERM;
113 }
114 event->response = 0;
115 spin_unlock(&event->lock);
116
Eric Parisb2d87902009-12-17 21:24:34 -0500117 pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
118 group, event, ret);
119
Eric Paris9e66e422009-12-17 21:24:34 -0500120 return ret;
121}
122#endif
123
Eric Parisff0b16a2009-12-17 21:24:25 -0500124static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
125{
126 int ret;
Eric Paris9e66e422009-12-17 21:24:34 -0500127 struct fsnotify_event *notify_event = NULL;
Eric Parisff0b16a2009-12-17 21:24:25 -0500128
129 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
130 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
131 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
132 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
133 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
134 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
135 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
Eric Paris9e66e422009-12-17 21:24:34 -0500136 BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
137 BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
Eric Parisff0b16a2009-12-17 21:24:25 -0500138
139 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
140
Eric Paris9e66e422009-12-17 21:24:34 -0500141 ret = fsnotify_add_notify_event(group, event, NULL, fanotify_merge,
142 (void **)&notify_event);
Eric Paris767cd462009-12-17 21:24:25 -0500143 /* -EEXIST means this event was merged with another, not that it was an error */
144 if (ret == -EEXIST)
145 ret = 0;
Eric Paris9e66e422009-12-17 21:24:34 -0500146 if (ret)
147 goto out;
148
149#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
150 if (event->mask & FAN_ALL_PERM_EVENTS) {
151 /* if we merged we need to wait on the new event */
152 if (notify_event)
153 event = notify_event;
154 ret = fanotify_get_response_from_access(group, event);
155 }
156#endif
157
158out:
159 if (notify_event)
160 fsnotify_put_event(notify_event);
Eric Parisff0b16a2009-12-17 21:24:25 -0500161 return ret;
162}
163
Eric Paris1c529062009-12-17 21:24:28 -0500164static bool should_send_vfsmount_event(struct fsnotify_group *group, struct vfsmount *mnt,
Eric Paris32a4df12009-12-17 21:24:33 -0500165 struct inode *inode, __u32 mask)
Eric Parisff0b16a2009-12-17 21:24:25 -0500166{
Eric Paris32a4df12009-12-17 21:24:33 -0500167 struct fsnotify_mark *mnt_mark;
168 struct fsnotify_mark *inode_mark;
Eric Parisff0b16a2009-12-17 21:24:25 -0500169
Eric Paris1c529062009-12-17 21:24:28 -0500170 pr_debug("%s: group=%p vfsmount=%p mask=%x\n",
171 __func__, group, mnt, mask);
Eric Parisff0b16a2009-12-17 21:24:25 -0500172
Eric Paris32a4df12009-12-17 21:24:33 -0500173 mnt_mark = fsnotify_find_vfsmount_mark(group, mnt);
174 if (!mnt_mark)
Eric Parisff0b16a2009-12-17 21:24:25 -0500175 return false;
176
Eric Paris32a4df12009-12-17 21:24:33 -0500177 mask &= mnt_mark->mask;
178 mask &= ~mnt_mark->ignored_mask;
179
180 if (mask) {
181 inode_mark = fsnotify_find_inode_mark(group, inode);
182 if (inode_mark) {
183 mask &= ~inode_mark->ignored_mask;
184 fsnotify_put_mark(inode_mark);
185 }
186 }
Eric Paris1c529062009-12-17 21:24:28 -0500187
188 /* find took a reference */
Eric Paris32a4df12009-12-17 21:24:33 -0500189 fsnotify_put_mark(mnt_mark);
Eric Paris1c529062009-12-17 21:24:28 -0500190
Eric Paris32a4df12009-12-17 21:24:33 -0500191 return mask;
Eric Paris1c529062009-12-17 21:24:28 -0500192}
193
194static bool should_send_inode_event(struct fsnotify_group *group, struct inode *inode,
195 __u32 mask)
196{
197 struct fsnotify_mark *fsn_mark;
Eric Paris1c529062009-12-17 21:24:28 -0500198
199 pr_debug("%s: group=%p inode=%p mask=%x\n",
200 __func__, group, inode, mask);
Eric Parisff0b16a2009-12-17 21:24:25 -0500201
Eric Paris5444e292009-12-17 21:24:27 -0500202 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Parisff0b16a2009-12-17 21:24:25 -0500203 if (!fsn_mark)
204 return false;
205
206 /* if the event is for a child and this inode doesn't care about
207 * events on the child, don't send it! */
208 if ((mask & FS_EVENT_ON_CHILD) &&
209 !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
Eric Paris32a4df12009-12-17 21:24:33 -0500210 mask = 0;
Eric Parisff0b16a2009-12-17 21:24:25 -0500211 } else {
212 /*
213 * We care about children, but do we care about this particular
214 * type of event?
215 */
Eric Paris32a4df12009-12-17 21:24:33 -0500216 mask &= ~FS_EVENT_ON_CHILD;
217 mask &= fsn_mark->mask;
218 mask &= ~fsn_mark->ignored_mask;
Eric Parisff0b16a2009-12-17 21:24:25 -0500219 }
220
221 /* find took a reference */
222 fsnotify_put_mark(fsn_mark);
223
Eric Paris32a4df12009-12-17 21:24:33 -0500224 return mask;
Eric Parisff0b16a2009-12-17 21:24:25 -0500225}
226
Eric Paris1c529062009-12-17 21:24:28 -0500227static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *to_tell,
228 struct vfsmount *mnt, __u32 mask, void *data,
229 int data_type)
230{
231 pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_type=%d\n",
232 __func__, group, to_tell, mnt, mask, data, data_type);
233
234 /* sorry, fanotify only gives a damn about files and dirs */
235 if (!S_ISREG(to_tell->i_mode) &&
236 !S_ISDIR(to_tell->i_mode))
237 return false;
238
239 /* if we don't have enough info to send an event to userspace say no */
240 if (data_type != FSNOTIFY_EVENT_PATH)
241 return false;
242
243 if (mnt)
Eric Paris32a4df12009-12-17 21:24:33 -0500244 return should_send_vfsmount_event(group, mnt, to_tell, mask);
Eric Paris1c529062009-12-17 21:24:28 -0500245 else
246 return should_send_inode_event(group, to_tell, mask);
247}
248
Eric Parisff0b16a2009-12-17 21:24:25 -0500249const struct fsnotify_ops fanotify_fsnotify_ops = {
250 .handle_event = fanotify_handle_event,
251 .should_send_event = fanotify_should_send_event,
252 .free_group_priv = NULL,
253 .free_event_priv = NULL,
254 .freeing_mark = NULL,
255};