fanotify.c 3.7 KB

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