blob: 4feed8601e2916b981dff9edda63eb43c1d4f492 [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>
5#include <linux/kernel.h> /* UINT_MAX */
Eric Paris1c529062009-12-17 21:24:28 -05006#include <linux/mount.h>
Eric Parisff0b16a2009-12-17 21:24:25 -05007#include <linux/types.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
Eric Paris43ed7e12009-12-17 21:24:34 -050030/* Note, if we return an event in *arg that a reference is being held... */
Eric Paris6e5f77b2009-12-17 21:24:34 -050031static int fanotify_merge(struct list_head *list,
32 struct fsnotify_event *event,
33 void **arg)
Eric Paris767cd462009-12-17 21:24:25 -050034{
Eric Parisa12a7dd2009-12-17 21:24:25 -050035 struct fsnotify_event_holder *test_holder;
Eric Paris767cd462009-12-17 21:24:25 -050036 struct fsnotify_event *test_event;
Eric Parisa12a7dd2009-12-17 21:24:25 -050037 struct fsnotify_event *new_event;
Eric Paris43ed7e12009-12-17 21:24:34 -050038 struct fsnotify_event **return_event = (struct fsnotify_event **)arg;
Eric Parisa12a7dd2009-12-17 21:24:25 -050039 int ret = 0;
Eric Paris767cd462009-12-17 21:24:25 -050040
41 pr_debug("%s: list=%p event=%p\n", __func__, list, event);
42
Eric Paris43ed7e12009-12-17 21:24:34 -050043 *return_event = NULL;
44
Eric Paris767cd462009-12-17 21:24:25 -050045 /* and the list better be locked by something too! */
46
Eric Parisa12a7dd2009-12-17 21:24:25 -050047 list_for_each_entry_reverse(test_holder, list, event_list) {
48 test_event = test_holder->event;
49 if (should_merge(test_event, event)) {
Eric Paris43ed7e12009-12-17 21:24:34 -050050 fsnotify_get_event(test_event);
51 *return_event = test_event;
Eric Paris767cd462009-12-17 21:24:25 -050052
Eric Paris43ed7e12009-12-17 21:24:34 -050053 ret = -EEXIST;
Eric Parisa12a7dd2009-12-17 21:24:25 -050054 /* if they are exactly the same we are done */
55 if (test_event->mask == event->mask)
56 goto out;
57
Eric Paris9dced012009-12-17 21:24:25 -050058 /*
59 * if the refcnt == 1 this is the only queue
60 * for this event and so we can update the mask
61 * in place.
62 */
63 if (atomic_read(&test_event->refcnt) == 1) {
64 test_event->mask |= event->mask;
65 goto out;
66 }
67
Eric Parisa12a7dd2009-12-17 21:24:25 -050068 /* can't allocate memory, merge was no possible */
69 new_event = fsnotify_clone_event(test_event);
70 if (unlikely(!new_event)) {
71 ret = 0;
72 goto out;
73 }
74
Eric Paris43ed7e12009-12-17 21:24:34 -050075 /* we didn't return the test_event, so drop that ref */
76 fsnotify_put_event(test_event);
77 /* the reference we return on new_event is from clone */
78 *return_event = new_event;
79
Eric Parisa12a7dd2009-12-17 21:24:25 -050080 /* build new event and replace it on the list */
81 new_event->mask = (test_event->mask | event->mask);
82 fsnotify_replace_event(test_holder, new_event);
Eric Parisa12a7dd2009-12-17 21:24:25 -050083
84 break;
85 }
86 }
87out:
88 return ret;
Eric Paris767cd462009-12-17 21:24:25 -050089}
90
Eric Parisff0b16a2009-12-17 21:24:25 -050091static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
92{
93 int ret;
Eric Paris43ed7e12009-12-17 21:24:34 -050094 struct fsnotify_event *used_event;
Eric Parisff0b16a2009-12-17 21:24:25 -050095
96 BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
97 BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
98 BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
99 BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
100 BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
101 BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
102 BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
103
104 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
105
Eric Paris43ed7e12009-12-17 21:24:34 -0500106 ret = fsnotify_add_notify_event(group, event, NULL, fanotify_merge, (void **)&used_event);
Eric Paris767cd462009-12-17 21:24:25 -0500107 /* -EEXIST means this event was merged with another, not that it was an error */
108 if (ret == -EEXIST)
109 ret = 0;
Eric Paris43ed7e12009-12-17 21:24:34 -0500110 if (used_event)
111 fsnotify_put_event(used_event);
Eric Parisff0b16a2009-12-17 21:24:25 -0500112 return ret;
113}
114
Eric Paris1c529062009-12-17 21:24:28 -0500115static bool should_send_vfsmount_event(struct fsnotify_group *group, struct vfsmount *mnt,
Eric Paris32a4df12009-12-17 21:24:33 -0500116 struct inode *inode, __u32 mask)
Eric Parisff0b16a2009-12-17 21:24:25 -0500117{
Eric Paris32a4df12009-12-17 21:24:33 -0500118 struct fsnotify_mark *mnt_mark;
119 struct fsnotify_mark *inode_mark;
Eric Parisff0b16a2009-12-17 21:24:25 -0500120
Eric Paris1c529062009-12-17 21:24:28 -0500121 pr_debug("%s: group=%p vfsmount=%p mask=%x\n",
122 __func__, group, mnt, mask);
Eric Parisff0b16a2009-12-17 21:24:25 -0500123
Eric Paris32a4df12009-12-17 21:24:33 -0500124 mnt_mark = fsnotify_find_vfsmount_mark(group, mnt);
125 if (!mnt_mark)
Eric Parisff0b16a2009-12-17 21:24:25 -0500126 return false;
127
Eric Paris32a4df12009-12-17 21:24:33 -0500128 mask &= mnt_mark->mask;
129 mask &= ~mnt_mark->ignored_mask;
130
131 if (mask) {
132 inode_mark = fsnotify_find_inode_mark(group, inode);
133 if (inode_mark) {
134 mask &= ~inode_mark->ignored_mask;
135 fsnotify_put_mark(inode_mark);
136 }
137 }
Eric Paris1c529062009-12-17 21:24:28 -0500138
139 /* find took a reference */
Eric Paris32a4df12009-12-17 21:24:33 -0500140 fsnotify_put_mark(mnt_mark);
Eric Paris1c529062009-12-17 21:24:28 -0500141
Eric Paris32a4df12009-12-17 21:24:33 -0500142 return mask;
Eric Paris1c529062009-12-17 21:24:28 -0500143}
144
145static bool should_send_inode_event(struct fsnotify_group *group, struct inode *inode,
146 __u32 mask)
147{
148 struct fsnotify_mark *fsn_mark;
Eric Paris1c529062009-12-17 21:24:28 -0500149
150 pr_debug("%s: group=%p inode=%p mask=%x\n",
151 __func__, group, inode, mask);
Eric Parisff0b16a2009-12-17 21:24:25 -0500152
Eric Paris5444e292009-12-17 21:24:27 -0500153 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Parisff0b16a2009-12-17 21:24:25 -0500154 if (!fsn_mark)
155 return false;
156
157 /* if the event is for a child and this inode doesn't care about
158 * events on the child, don't send it! */
159 if ((mask & FS_EVENT_ON_CHILD) &&
160 !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
Eric Paris32a4df12009-12-17 21:24:33 -0500161 mask = 0;
Eric Parisff0b16a2009-12-17 21:24:25 -0500162 } else {
163 /*
164 * We care about children, but do we care about this particular
165 * type of event?
166 */
Eric Paris32a4df12009-12-17 21:24:33 -0500167 mask &= ~FS_EVENT_ON_CHILD;
168 mask &= fsn_mark->mask;
169 mask &= ~fsn_mark->ignored_mask;
Eric Parisff0b16a2009-12-17 21:24:25 -0500170 }
171
172 /* find took a reference */
173 fsnotify_put_mark(fsn_mark);
174
Eric Paris32a4df12009-12-17 21:24:33 -0500175 return mask;
Eric Parisff0b16a2009-12-17 21:24:25 -0500176}
177
Eric Paris1c529062009-12-17 21:24:28 -0500178static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *to_tell,
179 struct vfsmount *mnt, __u32 mask, void *data,
180 int data_type)
181{
182 pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_type=%d\n",
183 __func__, group, to_tell, mnt, mask, data, data_type);
184
185 /* sorry, fanotify only gives a damn about files and dirs */
186 if (!S_ISREG(to_tell->i_mode) &&
187 !S_ISDIR(to_tell->i_mode))
188 return false;
189
190 /* if we don't have enough info to send an event to userspace say no */
191 if (data_type != FSNOTIFY_EVENT_PATH)
192 return false;
193
194 if (mnt)
Eric Paris32a4df12009-12-17 21:24:33 -0500195 return should_send_vfsmount_event(group, mnt, to_tell, mask);
Eric Paris1c529062009-12-17 21:24:28 -0500196 else
197 return should_send_inode_event(group, to_tell, mask);
198}
199
Eric Parisff0b16a2009-12-17 21:24:25 -0500200const struct fsnotify_ops fanotify_fsnotify_ops = {
201 .handle_event = fanotify_handle_event,
202 .should_send_event = fanotify_should_send_event,
203 .free_group_priv = NULL,
204 .free_event_priv = NULL,
205 .freeing_mark = NULL,
206};