fanotify.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. old->tgid == new->tgid) {
  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 *test_holder;
  29. struct fsnotify_event *test_event;
  30. struct fsnotify_event *new_event;
  31. int ret = 0;
  32. pr_debug("%s: list=%p event=%p\n", __func__, list, event);
  33. /* and the list better be locked by something too! */
  34. list_for_each_entry_reverse(test_holder, list, event_list) {
  35. test_event = test_holder->event;
  36. if (should_merge(test_event, event)) {
  37. ret = -EEXIST;
  38. /* if they are exactly the same we are done */
  39. if (test_event->mask == event->mask)
  40. goto out;
  41. /*
  42. * if the refcnt == 1 this is the only queue
  43. * for this event and so we can update the mask
  44. * in place.
  45. */
  46. if (atomic_read(&test_event->refcnt) == 1) {
  47. test_event->mask |= event->mask;
  48. goto out;
  49. }
  50. /* can't allocate memory, merge was no possible */
  51. new_event = fsnotify_clone_event(test_event);
  52. if (unlikely(!new_event)) {
  53. ret = 0;
  54. goto out;
  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. break;
  62. }
  63. }
  64. out:
  65. return ret;
  66. }
  67. static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
  68. {
  69. int ret;
  70. BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
  71. BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
  72. BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
  73. BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
  74. BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
  75. BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
  76. BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
  77. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  78. ret = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
  79. /* -EEXIST means this event was merged with another, not that it was an error */
  80. if (ret == -EEXIST)
  81. ret = 0;
  82. return ret;
  83. }
  84. static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *inode,
  85. struct vfsmount *mnt, __u32 mask, void *data,
  86. int data_type)
  87. {
  88. struct fsnotify_mark *fsn_mark;
  89. bool send;
  90. pr_debug("%s: group=%p inode=%p mask=%x data=%p data_type=%d\n",
  91. __func__, group, inode, mask, data, data_type);
  92. /* sorry, fanotify only gives a damn about files and dirs */
  93. if (!S_ISREG(inode->i_mode) &&
  94. !S_ISDIR(inode->i_mode))
  95. return false;
  96. /* if we don't have enough info to send an event to userspace say no */
  97. if (data_type != FSNOTIFY_EVENT_PATH)
  98. return false;
  99. fsn_mark = fsnotify_find_inode_mark(group, inode);
  100. if (!fsn_mark)
  101. return false;
  102. /* if the event is for a child and this inode doesn't care about
  103. * events on the child, don't send it! */
  104. if ((mask & FS_EVENT_ON_CHILD) &&
  105. !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
  106. send = false;
  107. } else {
  108. /*
  109. * We care about children, but do we care about this particular
  110. * type of event?
  111. */
  112. mask = (mask & ~FS_EVENT_ON_CHILD);
  113. send = (fsn_mark->mask & mask);
  114. }
  115. /* find took a reference */
  116. fsnotify_put_mark(fsn_mark);
  117. return send;
  118. }
  119. const struct fsnotify_ops fanotify_fsnotify_ops = {
  120. .handle_event = fanotify_handle_event,
  121. .should_send_event = fanotify_should_send_event,
  122. .free_group_priv = NULL,
  123. .free_event_priv = NULL,
  124. .freeing_mark = NULL,
  125. };