fanotify.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include <linux/fanotify.h>
  2. #include <linux/fdtable.h>
  3. #include <linux/fsnotify_backend.h>
  4. #include <linux/init.h>
  5. #include <linux/kernel.h> /* UINT_MAX */
  6. #include <linux/mount.h>
  7. #include <linux/types.h>
  8. static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
  9. {
  10. pr_debug("%s: old=%p new=%p\n", __func__, old, new);
  11. if (old->to_tell == new->to_tell &&
  12. old->data_type == new->data_type &&
  13. old->tgid == new->tgid) {
  14. switch (old->data_type) {
  15. case (FSNOTIFY_EVENT_PATH):
  16. if ((old->path.mnt == new->path.mnt) &&
  17. (old->path.dentry == new->path.dentry))
  18. return true;
  19. case (FSNOTIFY_EVENT_NONE):
  20. return true;
  21. default:
  22. BUG();
  23. };
  24. }
  25. return false;
  26. }
  27. static int fanotify_merge(struct list_head *list,
  28. struct fsnotify_event *event,
  29. void **arg)
  30. {
  31. struct fsnotify_event_holder *test_holder;
  32. struct fsnotify_event *test_event;
  33. struct fsnotify_event *new_event;
  34. int ret = 0;
  35. pr_debug("%s: list=%p event=%p\n", __func__, list, event);
  36. /* and the list better be locked by something too! */
  37. list_for_each_entry_reverse(test_holder, list, event_list) {
  38. test_event = test_holder->event;
  39. if (should_merge(test_event, event)) {
  40. ret = -EEXIST;
  41. /* if they are exactly the same we are done */
  42. if (test_event->mask == event->mask)
  43. goto out;
  44. /*
  45. * if the refcnt == 1 this is the only queue
  46. * for this event and so we can update the mask
  47. * in place.
  48. */
  49. if (atomic_read(&test_event->refcnt) == 1) {
  50. test_event->mask |= event->mask;
  51. goto out;
  52. }
  53. /* can't allocate memory, merge was no possible */
  54. new_event = fsnotify_clone_event(test_event);
  55. if (unlikely(!new_event)) {
  56. ret = 0;
  57. goto out;
  58. }
  59. /* build new event and replace it on the list */
  60. new_event->mask = (test_event->mask | event->mask);
  61. fsnotify_replace_event(test_holder, new_event);
  62. /* match ref from fsnotify_clone_event() */
  63. fsnotify_put_event(new_event);
  64. break;
  65. }
  66. }
  67. out:
  68. return ret;
  69. }
  70. static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
  71. {
  72. int ret;
  73. BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
  74. BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
  75. BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
  76. BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
  77. BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
  78. BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
  79. BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
  80. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  81. ret = fsnotify_add_notify_event(group, event, NULL, fanotify_merge, NULL);
  82. /* -EEXIST means this event was merged with another, not that it was an error */
  83. if (ret == -EEXIST)
  84. ret = 0;
  85. return ret;
  86. }
  87. static bool should_send_vfsmount_event(struct fsnotify_group *group, struct vfsmount *mnt,
  88. struct inode *inode, __u32 mask)
  89. {
  90. struct fsnotify_mark *mnt_mark;
  91. struct fsnotify_mark *inode_mark;
  92. pr_debug("%s: group=%p vfsmount=%p mask=%x\n",
  93. __func__, group, mnt, mask);
  94. mnt_mark = fsnotify_find_vfsmount_mark(group, mnt);
  95. if (!mnt_mark)
  96. return false;
  97. mask &= mnt_mark->mask;
  98. mask &= ~mnt_mark->ignored_mask;
  99. if (mask) {
  100. inode_mark = fsnotify_find_inode_mark(group, inode);
  101. if (inode_mark) {
  102. mask &= ~inode_mark->ignored_mask;
  103. fsnotify_put_mark(inode_mark);
  104. }
  105. }
  106. /* find took a reference */
  107. fsnotify_put_mark(mnt_mark);
  108. return mask;
  109. }
  110. static bool should_send_inode_event(struct fsnotify_group *group, struct inode *inode,
  111. __u32 mask)
  112. {
  113. struct fsnotify_mark *fsn_mark;
  114. pr_debug("%s: group=%p inode=%p mask=%x\n",
  115. __func__, group, inode, mask);
  116. fsn_mark = fsnotify_find_inode_mark(group, inode);
  117. if (!fsn_mark)
  118. return false;
  119. /* if the event is for a child and this inode doesn't care about
  120. * events on the child, don't send it! */
  121. if ((mask & FS_EVENT_ON_CHILD) &&
  122. !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
  123. mask = 0;
  124. } else {
  125. /*
  126. * We care about children, but do we care about this particular
  127. * type of event?
  128. */
  129. mask &= ~FS_EVENT_ON_CHILD;
  130. mask &= fsn_mark->mask;
  131. mask &= ~fsn_mark->ignored_mask;
  132. }
  133. /* find took a reference */
  134. fsnotify_put_mark(fsn_mark);
  135. return mask;
  136. }
  137. static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *to_tell,
  138. struct vfsmount *mnt, __u32 mask, void *data,
  139. int data_type)
  140. {
  141. pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_type=%d\n",
  142. __func__, group, to_tell, mnt, mask, data, data_type);
  143. /* sorry, fanotify only gives a damn about files and dirs */
  144. if (!S_ISREG(to_tell->i_mode) &&
  145. !S_ISDIR(to_tell->i_mode))
  146. return false;
  147. /* if we don't have enough info to send an event to userspace say no */
  148. if (data_type != FSNOTIFY_EVENT_PATH)
  149. return false;
  150. if (mnt)
  151. return should_send_vfsmount_event(group, mnt, to_tell, mask);
  152. else
  153. return should_send_inode_event(group, to_tell, mask);
  154. }
  155. const struct fsnotify_ops fanotify_fsnotify_ops = {
  156. .handle_event = fanotify_handle_event,
  157. .should_send_event = fanotify_should_send_event,
  158. .free_group_priv = NULL,
  159. .free_event_priv = NULL,
  160. .freeing_mark = NULL,
  161. };