Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 1 | #include <linux/fdtable.h> |
| 2 | #include <linux/fsnotify_backend.h> |
| 3 | #include <linux/init.h> |
| 4 | #include <linux/kernel.h> /* UINT_MAX */ |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 5 | #include <linux/mount.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 6 | #include <linux/types.h> |
| 7 | |
| 8 | #include "fanotify.h" |
| 9 | |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 10 | static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new) |
| 11 | { |
| 12 | pr_debug("%s: old=%p new=%p\n", __func__, old, new); |
| 13 | |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 14 | if (old->to_tell == new->to_tell && |
| 15 | old->data_type == new->data_type && |
| 16 | old->tgid == new->tgid) { |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 17 | switch (old->data_type) { |
| 18 | case (FSNOTIFY_EVENT_PATH): |
| 19 | if ((old->path.mnt == new->path.mnt) && |
| 20 | (old->path.dentry == new->path.dentry)) |
| 21 | return true; |
| 22 | case (FSNOTIFY_EVENT_NONE): |
| 23 | return true; |
| 24 | default: |
| 25 | BUG(); |
| 26 | }; |
| 27 | } |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | static int fanotify_merge(struct list_head *list, struct fsnotify_event *event) |
| 32 | { |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 33 | struct fsnotify_event_holder *test_holder; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 34 | struct fsnotify_event *test_event; |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 35 | struct fsnotify_event *new_event; |
| 36 | int ret = 0; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 37 | |
| 38 | pr_debug("%s: list=%p event=%p\n", __func__, list, event); |
| 39 | |
| 40 | /* and the list better be locked by something too! */ |
| 41 | |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 42 | list_for_each_entry_reverse(test_holder, list, event_list) { |
| 43 | test_event = test_holder->event; |
| 44 | if (should_merge(test_event, event)) { |
| 45 | ret = -EEXIST; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 46 | |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 47 | /* if they are exactly the same we are done */ |
| 48 | if (test_event->mask == event->mask) |
| 49 | goto out; |
| 50 | |
Eric Paris | 9dced01 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 51 | /* |
| 52 | * if the refcnt == 1 this is the only queue |
| 53 | * for this event and so we can update the mask |
| 54 | * in place. |
| 55 | */ |
| 56 | if (atomic_read(&test_event->refcnt) == 1) { |
| 57 | test_event->mask |= event->mask; |
| 58 | goto out; |
| 59 | } |
| 60 | |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 61 | /* can't allocate memory, merge was no possible */ |
| 62 | new_event = fsnotify_clone_event(test_event); |
| 63 | if (unlikely(!new_event)) { |
| 64 | ret = 0; |
| 65 | goto out; |
| 66 | } |
| 67 | |
| 68 | /* build new event and replace it on the list */ |
| 69 | new_event->mask = (test_event->mask | event->mask); |
| 70 | fsnotify_replace_event(test_holder, new_event); |
| 71 | /* match ref from fsnotify_clone_event() */ |
| 72 | fsnotify_put_event(new_event); |
| 73 | |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | out: |
| 78 | return ret; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 81 | static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event) |
| 82 | { |
| 83 | int ret; |
| 84 | |
| 85 | |
| 86 | BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); |
| 87 | BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); |
| 88 | BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); |
| 89 | BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); |
| 90 | BUILD_BUG_ON(FAN_OPEN != FS_OPEN); |
| 91 | BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); |
| 92 | BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); |
| 93 | |
| 94 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 95 | |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 96 | ret = fsnotify_add_notify_event(group, event, NULL, fanotify_merge); |
| 97 | /* -EEXIST means this event was merged with another, not that it was an error */ |
| 98 | if (ret == -EEXIST) |
| 99 | ret = 0; |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 100 | return ret; |
| 101 | } |
| 102 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 103 | static bool should_send_vfsmount_event(struct fsnotify_group *group, struct vfsmount *mnt, |
| 104 | __u32 mask) |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 105 | { |
| 106 | struct fsnotify_mark *fsn_mark; |
| 107 | bool send; |
| 108 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 109 | pr_debug("%s: group=%p vfsmount=%p mask=%x\n", |
| 110 | __func__, group, mnt, mask); |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 111 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 112 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 113 | if (!fsn_mark) |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 114 | return false; |
| 115 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 116 | send = (mask & fsn_mark->mask); |
| 117 | |
| 118 | /* find took a reference */ |
| 119 | fsnotify_put_mark(fsn_mark); |
| 120 | |
| 121 | return send; |
| 122 | } |
| 123 | |
| 124 | static bool should_send_inode_event(struct fsnotify_group *group, struct inode *inode, |
| 125 | __u32 mask) |
| 126 | { |
| 127 | struct fsnotify_mark *fsn_mark; |
| 128 | bool send; |
| 129 | |
| 130 | pr_debug("%s: group=%p inode=%p mask=%x\n", |
| 131 | __func__, group, inode, mask); |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 132 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 133 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 134 | if (!fsn_mark) |
| 135 | return false; |
| 136 | |
| 137 | /* if the event is for a child and this inode doesn't care about |
| 138 | * events on the child, don't send it! */ |
| 139 | if ((mask & FS_EVENT_ON_CHILD) && |
| 140 | !(fsn_mark->mask & FS_EVENT_ON_CHILD)) { |
| 141 | send = false; |
| 142 | } else { |
| 143 | /* |
| 144 | * We care about children, but do we care about this particular |
| 145 | * type of event? |
| 146 | */ |
| 147 | mask = (mask & ~FS_EVENT_ON_CHILD); |
| 148 | send = (fsn_mark->mask & mask); |
| 149 | } |
| 150 | |
| 151 | /* find took a reference */ |
| 152 | fsnotify_put_mark(fsn_mark); |
| 153 | |
| 154 | return send; |
| 155 | } |
| 156 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame^] | 157 | static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *to_tell, |
| 158 | struct vfsmount *mnt, __u32 mask, void *data, |
| 159 | int data_type) |
| 160 | { |
| 161 | pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_type=%d\n", |
| 162 | __func__, group, to_tell, mnt, mask, data, data_type); |
| 163 | |
| 164 | /* sorry, fanotify only gives a damn about files and dirs */ |
| 165 | if (!S_ISREG(to_tell->i_mode) && |
| 166 | !S_ISDIR(to_tell->i_mode)) |
| 167 | return false; |
| 168 | |
| 169 | /* if we don't have enough info to send an event to userspace say no */ |
| 170 | if (data_type != FSNOTIFY_EVENT_PATH) |
| 171 | return false; |
| 172 | |
| 173 | if (mnt) |
| 174 | return should_send_vfsmount_event(group, mnt, mask); |
| 175 | else |
| 176 | return should_send_inode_event(group, to_tell, mask); |
| 177 | } |
| 178 | |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 179 | const struct fsnotify_ops fanotify_fsnotify_ops = { |
| 180 | .handle_event = fanotify_handle_event, |
| 181 | .should_send_event = fanotify_should_send_event, |
| 182 | .free_group_priv = NULL, |
| 183 | .free_event_priv = NULL, |
| 184 | .freeing_mark = NULL, |
| 185 | }; |