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 | |
| 9 | static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event) |
| 10 | { |
| 11 | int ret; |
| 12 | |
| 13 | |
| 14 | BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS); |
| 15 | BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY); |
| 16 | BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); |
| 17 | BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE); |
| 18 | BUILD_BUG_ON(FAN_OPEN != FS_OPEN); |
| 19 | BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD); |
| 20 | BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW); |
| 21 | |
| 22 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 23 | |
| 24 | ret = fsnotify_add_notify_event(group, event, NULL, NULL); |
| 25 | |
| 26 | return ret; |
| 27 | } |
| 28 | |
| 29 | static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *inode, |
| 30 | struct vfsmount *mnt, __u32 mask, void *data, |
| 31 | int data_type) |
| 32 | { |
| 33 | struct fsnotify_mark *fsn_mark; |
| 34 | bool send; |
| 35 | |
| 36 | pr_debug("%s: group=%p inode=%p mask=%x data=%p data_type=%d\n", |
| 37 | __func__, group, inode, mask, data, data_type); |
| 38 | |
| 39 | /* sorry, fanotify only gives a damn about files and dirs */ |
| 40 | if (!S_ISREG(inode->i_mode) && |
| 41 | !S_ISDIR(inode->i_mode)) |
| 42 | return false; |
| 43 | |
| 44 | /* if we don't have enough info to send an event to userspace say no */ |
| 45 | if (data_type != FSNOTIFY_EVENT_PATH) |
| 46 | return false; |
| 47 | |
| 48 | fsn_mark = fsnotify_find_mark(group, inode); |
| 49 | if (!fsn_mark) |
| 50 | return false; |
| 51 | |
| 52 | /* if the event is for a child and this inode doesn't care about |
| 53 | * events on the child, don't send it! */ |
| 54 | if ((mask & FS_EVENT_ON_CHILD) && |
| 55 | !(fsn_mark->mask & FS_EVENT_ON_CHILD)) { |
| 56 | send = false; |
| 57 | } else { |
| 58 | /* |
| 59 | * We care about children, but do we care about this particular |
| 60 | * type of event? |
| 61 | */ |
| 62 | mask = (mask & ~FS_EVENT_ON_CHILD); |
| 63 | send = (fsn_mark->mask & mask); |
| 64 | } |
| 65 | |
| 66 | /* find took a reference */ |
| 67 | fsnotify_put_mark(fsn_mark); |
| 68 | |
| 69 | return send; |
| 70 | } |
| 71 | |
| 72 | const struct fsnotify_ops fanotify_fsnotify_ops = { |
| 73 | .handle_event = fanotify_handle_event, |
| 74 | .should_send_event = fanotify_should_send_event, |
| 75 | .free_group_priv = NULL, |
| 76 | .free_event_priv = NULL, |
| 77 | .freeing_mark = NULL, |
| 78 | }; |