fanotify.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "fanotify.h"
  7. static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
  8. {
  9. pr_debug("%s: old=%p new=%p\n", __func__, old, new);
  10. if ((old->mask == new->mask) &&
  11. (old->to_tell == new->to_tell) &&
  12. (old->data_type == new->data_type)) {
  13. switch (old->data_type) {
  14. case (FSNOTIFY_EVENT_PATH):
  15. if ((old->path.mnt == new->path.mnt) &&
  16. (old->path.dentry == new->path.dentry))
  17. return true;
  18. case (FSNOTIFY_EVENT_NONE):
  19. return true;
  20. default:
  21. BUG();
  22. };
  23. }
  24. return false;
  25. }
  26. static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)
  27. {
  28. struct fsnotify_event_holder *holder;
  29. struct fsnotify_event *test_event;
  30. pr_debug("%s: list=%p event=%p\n", __func__, list, event);
  31. /* and the list better be locked by something too! */
  32. list_for_each_entry_reverse(holder, list, event_list) {
  33. test_event = holder->event;
  34. if (should_merge(test_event, event))
  35. return -EEXIST;
  36. }
  37. return 0;
  38. }
  39. static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
  40. {
  41. int ret;
  42. BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
  43. BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
  44. BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
  45. BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
  46. BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
  47. BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
  48. BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
  49. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  50. ret = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
  51. /* -EEXIST means this event was merged with another, not that it was an error */
  52. if (ret == -EEXIST)
  53. ret = 0;
  54. return ret;
  55. }
  56. static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *inode,
  57. struct vfsmount *mnt, __u32 mask, void *data,
  58. int data_type)
  59. {
  60. struct fsnotify_mark *fsn_mark;
  61. bool send;
  62. pr_debug("%s: group=%p inode=%p mask=%x data=%p data_type=%d\n",
  63. __func__, group, inode, mask, data, data_type);
  64. /* sorry, fanotify only gives a damn about files and dirs */
  65. if (!S_ISREG(inode->i_mode) &&
  66. !S_ISDIR(inode->i_mode))
  67. return false;
  68. /* if we don't have enough info to send an event to userspace say no */
  69. if (data_type != FSNOTIFY_EVENT_PATH)
  70. return false;
  71. fsn_mark = fsnotify_find_mark(group, inode);
  72. if (!fsn_mark)
  73. return false;
  74. /* if the event is for a child and this inode doesn't care about
  75. * events on the child, don't send it! */
  76. if ((mask & FS_EVENT_ON_CHILD) &&
  77. !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
  78. send = false;
  79. } else {
  80. /*
  81. * We care about children, but do we care about this particular
  82. * type of event?
  83. */
  84. mask = (mask & ~FS_EVENT_ON_CHILD);
  85. send = (fsn_mark->mask & mask);
  86. }
  87. /* find took a reference */
  88. fsnotify_put_mark(fsn_mark);
  89. return send;
  90. }
  91. const struct fsnotify_ops fanotify_fsnotify_ops = {
  92. .handle_event = fanotify_handle_event,
  93. .should_send_event = fanotify_should_send_event,
  94. .free_group_priv = NULL,
  95. .free_event_priv = NULL,
  96. .freeing_mark = NULL,
  97. };