fanotify.c 5.0 KB

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