blob: 060b177146e845b341277084302449425a17fa54 [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
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
Eric Paris1c529062009-12-17 21:24:28 -0500102static bool should_send_vfsmount_event(struct fsnotify_group *group, struct vfsmount *mnt,
Eric Paris32a4df12009-12-17 21:24:33 -0500103 struct inode *inode, __u32 mask)
Eric Parisff0b16a2009-12-17 21:24:25 -0500104{
Eric Paris32a4df12009-12-17 21:24:33 -0500105 struct fsnotify_mark *mnt_mark;
106 struct fsnotify_mark *inode_mark;
Eric Parisff0b16a2009-12-17 21:24:25 -0500107
Eric Paris1c529062009-12-17 21:24:28 -0500108 pr_debug("%s: group=%p vfsmount=%p mask=%x\n",
109 __func__, group, mnt, mask);
Eric Parisff0b16a2009-12-17 21:24:25 -0500110
Eric Paris32a4df12009-12-17 21:24:33 -0500111 mnt_mark = fsnotify_find_vfsmount_mark(group, mnt);
112 if (!mnt_mark)
Eric Parisff0b16a2009-12-17 21:24:25 -0500113 return false;
114
Eric Paris32a4df12009-12-17 21:24:33 -0500115 mask &= mnt_mark->mask;
116 mask &= ~mnt_mark->ignored_mask;
117
118 if (mask) {
119 inode_mark = fsnotify_find_inode_mark(group, inode);
120 if (inode_mark) {
121 mask &= ~inode_mark->ignored_mask;
122 fsnotify_put_mark(inode_mark);
123 }
124 }
Eric Paris1c529062009-12-17 21:24:28 -0500125
126 /* find took a reference */
Eric Paris32a4df12009-12-17 21:24:33 -0500127 fsnotify_put_mark(mnt_mark);
Eric Paris1c529062009-12-17 21:24:28 -0500128
Eric Paris32a4df12009-12-17 21:24:33 -0500129 return mask;
Eric Paris1c529062009-12-17 21:24:28 -0500130}
131
132static bool should_send_inode_event(struct fsnotify_group *group, struct inode *inode,
133 __u32 mask)
134{
135 struct fsnotify_mark *fsn_mark;
Eric Paris1c529062009-12-17 21:24:28 -0500136
137 pr_debug("%s: group=%p inode=%p mask=%x\n",
138 __func__, group, inode, mask);
Eric Parisff0b16a2009-12-17 21:24:25 -0500139
Eric Paris5444e292009-12-17 21:24:27 -0500140 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Parisff0b16a2009-12-17 21:24:25 -0500141 if (!fsn_mark)
142 return false;
143
144 /* if the event is for a child and this inode doesn't care about
145 * events on the child, don't send it! */
146 if ((mask & FS_EVENT_ON_CHILD) &&
147 !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
Eric Paris32a4df12009-12-17 21:24:33 -0500148 mask = 0;
Eric Parisff0b16a2009-12-17 21:24:25 -0500149 } else {
150 /*
151 * We care about children, but do we care about this particular
152 * type of event?
153 */
Eric Paris32a4df12009-12-17 21:24:33 -0500154 mask &= ~FS_EVENT_ON_CHILD;
155 mask &= fsn_mark->mask;
156 mask &= ~fsn_mark->ignored_mask;
Eric Parisff0b16a2009-12-17 21:24:25 -0500157 }
158
159 /* find took a reference */
160 fsnotify_put_mark(fsn_mark);
161
Eric Paris32a4df12009-12-17 21:24:33 -0500162 return mask;
Eric Parisff0b16a2009-12-17 21:24:25 -0500163}
164
Eric Paris1c529062009-12-17 21:24:28 -0500165static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *to_tell,
166 struct vfsmount *mnt, __u32 mask, void *data,
167 int data_type)
168{
169 pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_type=%d\n",
170 __func__, group, to_tell, mnt, mask, data, data_type);
171
172 /* sorry, fanotify only gives a damn about files and dirs */
173 if (!S_ISREG(to_tell->i_mode) &&
174 !S_ISDIR(to_tell->i_mode))
175 return false;
176
177 /* if we don't have enough info to send an event to userspace say no */
178 if (data_type != FSNOTIFY_EVENT_PATH)
179 return false;
180
181 if (mnt)
Eric Paris32a4df12009-12-17 21:24:33 -0500182 return should_send_vfsmount_event(group, mnt, to_tell, mask);
Eric Paris1c529062009-12-17 21:24:28 -0500183 else
184 return should_send_inode_event(group, to_tell, mask);
185}
186
Eric Parisff0b16a2009-12-17 21:24:25 -0500187const struct fsnotify_ops fanotify_fsnotify_ops = {
188 .handle_event = fanotify_handle_event,
189 .should_send_event = fanotify_should_send_event,
190 .free_group_priv = NULL,
191 .free_event_priv = NULL,
192 .freeing_mark = NULL,
193};