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