fanotify.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /*
  41. * if the refcnt == 1 this is the only queue
  42. * for this event and so we can update the mask
  43. * in place.
  44. */
  45. if (atomic_read(&test_event->refcnt) == 1) {
  46. 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. /* build new event and replace it on the list */
  56. new_event->mask = (test_event->mask | event->mask);
  57. fsnotify_replace_event(test_holder, new_event);
  58. /* match ref from fsnotify_clone_event() */
  59. fsnotify_put_event(new_event);
  60. break;
  61. }
  62. }
  63. out:
  64. return ret;
  65. }
  66. static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
  67. {
  68. int ret;
  69. BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
  70. BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
  71. BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
  72. BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
  73. BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
  74. BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
  75. BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
  76. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  77. ret = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
  78. /* -EEXIST means this event was merged with another, not that it was an error */
  79. if (ret == -EEXIST)
  80. ret = 0;
  81. return ret;
  82. }
  83. static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *inode,
  84. struct vfsmount *mnt, __u32 mask, void *data,
  85. int data_type)
  86. {
  87. struct fsnotify_mark *fsn_mark;
  88. bool send;
  89. pr_debug("%s: group=%p inode=%p mask=%x data=%p data_type=%d\n",
  90. __func__, group, inode, mask, data, data_type);
  91. /* sorry, fanotify only gives a damn about files and dirs */
  92. if (!S_ISREG(inode->i_mode) &&
  93. !S_ISDIR(inode->i_mode))
  94. return false;
  95. /* if we don't have enough info to send an event to userspace say no */
  96. if (data_type != FSNOTIFY_EVENT_PATH)
  97. return false;
  98. fsn_mark = fsnotify_find_mark(group, inode);
  99. if (!fsn_mark)
  100. return false;
  101. /* if the event is for a child and this inode doesn't care about
  102. * events on the child, don't send it! */
  103. if ((mask & FS_EVENT_ON_CHILD) &&
  104. !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
  105. send = false;
  106. } else {
  107. /*
  108. * We care about children, but do we care about this particular
  109. * type of event?
  110. */
  111. mask = (mask & ~FS_EVENT_ON_CHILD);
  112. send = (fsn_mark->mask & mask);
  113. }
  114. /* find took a reference */
  115. fsnotify_put_mark(fsn_mark);
  116. return send;
  117. }
  118. const struct fsnotify_ops fanotify_fsnotify_ops = {
  119. .handle_event = fanotify_handle_event,
  120. .should_send_event = fanotify_should_send_event,
  121. .free_group_priv = NULL,
  122. .free_event_priv = NULL,
  123. .freeing_mark = NULL,
  124. };