Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1 | #include <linux/fanotify.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 2 | #include <linux/fdtable.h> |
| 3 | #include <linux/fsnotify_backend.h> |
| 4 | #include <linux/init.h> |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 5 | #include <linux/jiffies.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 6 | #include <linux/kernel.h> /* UINT_MAX */ |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 7 | #include <linux/mount.h> |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 8 | #include <linux/sched.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 9 | #include <linux/types.h> |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 10 | #include <linux/wait.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 11 | |
Eric Paris | 767cd46c | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 12 | static 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 Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 16 | if (old->to_tell == new->to_tell && |
| 17 | old->data_type == new->data_type && |
| 18 | old->tgid == new->tgid) { |
Eric Paris | 767cd46c | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 19 | switch (old->data_type) { |
Linus Torvalds | 2069601 | 2010-08-12 14:23:04 -0700 | [diff] [blame] | 20 | case (FSNOTIFY_EVENT_PATH): |
| 21 | if ((old->path.mnt == new->path.mnt) && |
| 22 | (old->path.dentry == new->path.dentry)) |
Eric Paris | 767cd46c | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 23 | return true; |
| 24 | case (FSNOTIFY_EVENT_NONE): |
| 25 | return true; |
| 26 | default: |
| 27 | BUG(); |
| 28 | }; |
| 29 | } |
| 30 | return false; |
| 31 | } |
| 32 | |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 33 | /* and the list better be locked by something too! */ |
| 34 | static struct fsnotify_event *fanotify_merge(struct list_head *list, |
| 35 | struct fsnotify_event *event) |
Eric Paris | 767cd46c | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 36 | { |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 37 | struct fsnotify_event_holder *test_holder; |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 38 | struct fsnotify_event *test_event = NULL; |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 39 | struct fsnotify_event *new_event; |
Eric Paris | 767cd46c | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 40 | |
| 41 | pr_debug("%s: list=%p event=%p\n", __func__, list, event); |
| 42 | |
Eric Paris | 767cd46c | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 43 | |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 44 | list_for_each_entry_reverse(test_holder, list, event_list) { |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 45 | if (should_merge(test_holder->event, event)) { |
| 46 | test_event = test_holder->event; |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 47 | break; |
| 48 | } |
| 49 | } |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 50 | |
| 51 | if (!test_event) |
| 52 | return NULL; |
| 53 | |
| 54 | fsnotify_get_event(test_event); |
| 55 | |
| 56 | /* if they are exactly the same we are done */ |
| 57 | if (test_event->mask == event->mask) |
| 58 | return test_event; |
| 59 | |
| 60 | /* |
| 61 | * if the refcnt == 2 this is the only queue |
| 62 | * for this event and so we can update the mask |
| 63 | * in place. |
| 64 | */ |
| 65 | if (atomic_read(&test_event->refcnt) == 2) { |
| 66 | test_event->mask |= event->mask; |
| 67 | return test_event; |
| 68 | } |
| 69 | |
| 70 | new_event = fsnotify_clone_event(test_event); |
| 71 | |
| 72 | /* done with test_event */ |
| 73 | fsnotify_put_event(test_event); |
| 74 | |
| 75 | /* couldn't allocate memory, merge was not possible */ |
| 76 | if (unlikely(!new_event)) |
| 77 | return ERR_PTR(-ENOMEM); |
| 78 | |
| 79 | /* build new event and replace it on the list */ |
| 80 | new_event->mask = (test_event->mask | event->mask); |
| 81 | fsnotify_replace_event(test_holder, new_event); |
| 82 | |
| 83 | /* we hold a reference on new_event from clone_event */ |
| 84 | return new_event; |
Eric Paris | 767cd46c | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 87 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 88 | static int fanotify_get_response_from_access(struct fsnotify_group *group, |
| 89 | struct fsnotify_event *event) |
| 90 | { |
| 91 | int ret; |
| 92 | |
| 93 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 94 | |
| 95 | wait_event(group->fanotify_data.access_waitq, event->response); |
| 96 | |
| 97 | /* userspace responded, convert to something usable */ |
| 98 | spin_lock(&event->lock); |
| 99 | switch (event->response) { |
| 100 | case FAN_ALLOW: |
| 101 | ret = 0; |
| 102 | break; |
| 103 | case FAN_DENY: |
| 104 | default: |
| 105 | ret = -EPERM; |
| 106 | } |
| 107 | event->response = 0; |
| 108 | spin_unlock(&event->lock); |
| 109 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 110 | pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__, |
| 111 | group, event, ret); |
| 112 | |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 113 | return ret; |
| 114 | } |
| 115 | #endif |
| 116 | |
Eric Paris | 3a9b16b | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 117 | static int fanotify_handle_event(struct fsnotify_group *group, |
Eric Paris | ce8f76f | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 118 | struct fsnotify_mark *inode_mark, |
| 119 | struct fsnotify_mark *fanotify_mark, |
Eric Paris | 3a9b16b | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 120 | struct fsnotify_event *event) |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 121 | { |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 122 | int ret = 0; |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 123 | struct fsnotify_event *notify_event = NULL; |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 124 | |
| 125 | BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); |
| 126 | BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); |
| 127 | BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); |
| 128 | BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); |
| 129 | BUILD_BUG_ON(FAN_OPEN != FS_OPEN); |
| 130 | BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); |
| 131 | BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 132 | BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM); |
| 133 | BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM); |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 134 | |
| 135 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 136 | |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 137 | notify_event = fsnotify_add_notify_event(group, event, NULL, fanotify_merge); |
| 138 | if (IS_ERR(notify_event)) |
| 139 | return PTR_ERR(notify_event); |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 140 | |
| 141 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 142 | if (event->mask & FAN_ALL_PERM_EVENTS) { |
| 143 | /* if we merged we need to wait on the new event */ |
| 144 | if (notify_event) |
| 145 | event = notify_event; |
| 146 | ret = fanotify_get_response_from_access(group, event); |
| 147 | } |
| 148 | #endif |
| 149 | |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 150 | if (notify_event) |
| 151 | fsnotify_put_event(notify_event); |
Eric Paris | f70ab54 | 2010-07-28 10:18:37 -0400 | [diff] [blame] | 152 | |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 153 | return ret; |
| 154 | } |
| 155 | |
Eric Paris | ce8f76f | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 156 | static bool fanotify_should_send_event(struct fsnotify_group *group, |
| 157 | struct inode *to_tell, |
Eric Paris | ce8f76f | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 158 | struct fsnotify_mark *inode_mark, |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 159 | struct fsnotify_mark *vfsmnt_mark, |
| 160 | __u32 event_mask, void *data, int data_type) |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 161 | { |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 162 | __u32 marks_mask, marks_ignored_mask; |
Eric Paris | e1c048b | 2010-10-28 17:21:58 -0400 | [diff] [blame^] | 163 | struct path *path = data; |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 164 | |
| 165 | pr_debug("%s: group=%p to_tell=%p inode_mark=%p vfsmnt_mark=%p " |
| 166 | "mask=%x data=%p data_type=%d\n", __func__, group, to_tell, |
| 167 | inode_mark, vfsmnt_mark, event_mask, data, data_type); |
| 168 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 169 | /* if we don't have enough info to send an event to userspace say no */ |
Linus Torvalds | 2069601 | 2010-08-12 14:23:04 -0700 | [diff] [blame] | 170 | if (data_type != FSNOTIFY_EVENT_PATH) |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 171 | return false; |
| 172 | |
Eric Paris | e1c048b | 2010-10-28 17:21:58 -0400 | [diff] [blame^] | 173 | /* sorry, fanotify only gives a damn about files and dirs */ |
| 174 | if (!S_ISREG(path->dentry->d_inode->i_mode) && |
| 175 | !S_ISDIR(path->dentry->d_inode->i_mode)) |
| 176 | return false; |
| 177 | |
Eric Paris | 1968f5e | 2010-07-28 10:18:39 -0400 | [diff] [blame] | 178 | if (inode_mark && vfsmnt_mark) { |
| 179 | marks_mask = (vfsmnt_mark->mask | inode_mark->mask); |
| 180 | marks_ignored_mask = (vfsmnt_mark->ignored_mask | inode_mark->ignored_mask); |
| 181 | } else if (inode_mark) { |
| 182 | /* |
| 183 | * if the event is for a child and this inode doesn't care about |
| 184 | * events on the child, don't send it! |
| 185 | */ |
| 186 | if ((event_mask & FS_EVENT_ON_CHILD) && |
| 187 | !(inode_mark->mask & FS_EVENT_ON_CHILD)) |
| 188 | return false; |
| 189 | marks_mask = inode_mark->mask; |
| 190 | marks_ignored_mask = inode_mark->ignored_mask; |
| 191 | } else if (vfsmnt_mark) { |
| 192 | marks_mask = vfsmnt_mark->mask; |
| 193 | marks_ignored_mask = vfsmnt_mark->ignored_mask; |
| 194 | } else { |
| 195 | BUG(); |
| 196 | } |
| 197 | |
| 198 | if (event_mask & marks_mask & ~marks_ignored_mask) |
| 199 | return true; |
| 200 | |
| 201 | return false; |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 202 | } |
| 203 | |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 204 | static void fanotify_free_group_priv(struct fsnotify_group *group) |
| 205 | { |
| 206 | struct user_struct *user; |
| 207 | |
| 208 | user = group->fanotify_data.user; |
| 209 | atomic_dec(&user->fanotify_listeners); |
| 210 | free_uid(user); |
| 211 | } |
| 212 | |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 213 | const struct fsnotify_ops fanotify_fsnotify_ops = { |
| 214 | .handle_event = fanotify_handle_event, |
| 215 | .should_send_event = fanotify_should_send_event, |
Eric Paris | 4afeff8 | 2010-10-28 17:21:58 -0400 | [diff] [blame] | 216 | .free_group_priv = fanotify_free_group_priv, |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 217 | .free_event_priv = NULL, |
| 218 | .freeing_mark = NULL, |
| 219 | }; |