]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/notify/fanotify/fanotify.c
060b177146e845b341277084302449425a17fa54
[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                                        struct inode *inode, __u32 mask)
104 {
105         struct fsnotify_mark *mnt_mark;
106         struct fsnotify_mark *inode_mark;
107
108         pr_debug("%s: group=%p vfsmount=%p mask=%x\n",
109                  __func__, group, mnt, mask);
110
111         mnt_mark = fsnotify_find_vfsmount_mark(group, mnt);
112         if (!mnt_mark)
113                 return false;
114
115         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         }
125
126         /* find took a reference */
127         fsnotify_put_mark(mnt_mark);
128
129         return mask;
130 }
131
132 static bool should_send_inode_event(struct fsnotify_group *group, struct inode *inode,
133                                     __u32 mask)
134 {
135         struct fsnotify_mark *fsn_mark;
136
137         pr_debug("%s: group=%p inode=%p mask=%x\n",
138                  __func__, group, inode, mask);
139
140         fsn_mark = fsnotify_find_inode_mark(group, inode);
141         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)) {
148                 mask = 0;
149         } else {
150                 /*
151                  * We care about children, but do we care about this particular
152                  * type of event?
153                  */
154                 mask &= ~FS_EVENT_ON_CHILD;
155                 mask &= fsn_mark->mask;
156                 mask &= ~fsn_mark->ignored_mask;
157         }
158
159         /* find took a reference */
160         fsnotify_put_mark(fsn_mark);
161
162         return mask;
163 }
164
165 static 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)
182                 return should_send_vfsmount_event(group, mnt, to_tell, mask);
183         else
184                 return should_send_inode_event(group, to_tell, mask);
185 }
186
187 const 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 };