]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/notify/fanotify/fanotify.c
f6900022f69e352f921a4481b5520d4b362a81a4
[net-next-2.6.git] / fs / notify / fanotify / fanotify.c
1 #include <linux/fanotify.h>
2 #include <linux/fdtable.h>
3 #include <linux/fsnotify_backend.h>
4 #include <linux/init.h>
5 #include <linux/kernel.h> /* UINT_MAX */
6 #include <linux/mount.h>
7 #include <linux/types.h>
8
9 static 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
13         if (old->to_tell == new->to_tell &&
14             old->data_type == new->data_type &&
15             old->tgid == new->tgid) {
16                 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
30 static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
31 {
32         struct fsnotify_event_holder *test_holder;
33         struct fsnotify_event *test_event;
34         struct fsnotify_event *new_event;
35         int ret = 0;
36
37         pr_debug("%s: list=%p event=%p\n", __func__, list, event);
38
39         /* and the list better be locked by something too! */
40
41         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;
45
46                         /* if they are exactly the same we are done */
47                         if (test_event->mask == event->mask)
48                                 goto out;
49
50                         /*
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
60                         /* 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         }
76 out:
77         return ret;
78 }
79
80 static 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
95         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;
99         return ret;
100 }
101
102 static bool should_send_vfsmount_event(struct fsnotify_group *group, struct vfsmount *mnt,
103                                        __u32 mask)
104 {
105         struct fsnotify_mark *fsn_mark;
106         bool send;
107
108         pr_debug("%s: group=%p vfsmount=%p mask=%x\n",
109                  __func__, group, mnt, mask);
110
111         fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
112         if (!fsn_mark)
113                 return false;
114
115         send = (mask & fsn_mark->mask);
116
117         /* find took a reference */
118         fsnotify_put_mark(fsn_mark);
119
120         return send;
121 }
122
123 static bool should_send_inode_event(struct fsnotify_group *group, struct inode *inode,
124                                     __u32 mask)
125 {
126         struct fsnotify_mark *fsn_mark;
127         bool send;
128
129         pr_debug("%s: group=%p inode=%p mask=%x\n",
130                  __func__, group, inode, mask);
131
132         fsn_mark = fsnotify_find_inode_mark(group, inode);
133         if (!fsn_mark)
134                 return false;
135
136         /* if the event is for a child and this inode doesn't care about
137          * events on the child, don't send it! */
138         if ((mask & FS_EVENT_ON_CHILD) &&
139             !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
140                 send = false;
141         } else {
142                 /*
143                  * We care about children, but do we care about this particular
144                  * type of event?
145                  */
146                 mask = (mask & ~FS_EVENT_ON_CHILD);
147                 send = (fsn_mark->mask & mask);
148         }
149
150         /* find took a reference */
151         fsnotify_put_mark(fsn_mark);
152
153         return send;
154 }
155
156 static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *to_tell,
157                                        struct vfsmount *mnt, __u32 mask, void *data,
158                                        int data_type)
159 {
160         pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_type=%d\n",
161                  __func__, group, to_tell, mnt, mask, data, data_type);
162
163         /* sorry, fanotify only gives a damn about files and dirs */
164         if (!S_ISREG(to_tell->i_mode) &&
165             !S_ISDIR(to_tell->i_mode))
166                 return false;
167
168         /* if we don't have enough info to send an event to userspace say no */
169         if (data_type != FSNOTIFY_EVENT_PATH)
170                 return false;
171
172         if (mnt)
173                 return should_send_vfsmount_event(group, mnt, mask);
174         else
175                 return should_send_inode_event(group, to_tell, mask);
176 }
177
178 const struct fsnotify_ops fanotify_fsnotify_ops = {
179         .handle_event = fanotify_handle_event,
180         .should_send_event = fanotify_should_send_event,
181         .free_group_priv = NULL,
182         .free_event_priv = NULL,
183         .freeing_mark = NULL,
184 };