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> |
| 5 | #include <linux/kernel.h> /* UINT_MAX */ |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 6 | #include <linux/mount.h> |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 7 | #include <linux/types.h> |
| 8 | |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 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 | |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 13 | if (old->to_tell == new->to_tell && |
| 14 | old->data_type == new->data_type && |
| 15 | old->tgid == new->tgid) { |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 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 | { |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 32 | struct fsnotify_event_holder *test_holder; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 33 | struct fsnotify_event *test_event; |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 34 | struct fsnotify_event *new_event; |
| 35 | int ret = 0; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 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 | |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 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; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 45 | |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 46 | /* if they are exactly the same we are done */ |
| 47 | if (test_event->mask == event->mask) |
| 48 | goto out; |
| 49 | |
Eric Paris | 9dced01 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 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 | |
Eric Paris | a12a7dd | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 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; |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 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 | |
Eric Paris | 767cd46 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 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; |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 99 | return ret; |
| 100 | } |
| 101 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 102 | static bool should_send_vfsmount_event(struct fsnotify_group *group, struct vfsmount *mnt, |
| 103 | __u32 mask) |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 104 | { |
| 105 | struct fsnotify_mark *fsn_mark; |
| 106 | bool send; |
| 107 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 108 | pr_debug("%s: group=%p vfsmount=%p mask=%x\n", |
| 109 | __func__, group, mnt, mask); |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 110 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 111 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 112 | if (!fsn_mark) |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 113 | return false; |
| 114 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 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); |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 131 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 132 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 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 | |
Eric Paris | 1c52906 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 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 | |
Eric Paris | ff0b16a | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 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 | }; |