fanotify.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. __u32 mask)
  87. {
  88. struct fsnotify_mark *fsn_mark;
  89. bool send;
  90. pr_debug("%s: group=%p vfsmount=%p mask=%x\n",
  91. __func__, group, mnt, mask);
  92. fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
  93. if (!fsn_mark)
  94. return false;
  95. send = (mask & fsn_mark->mask);
  96. /* find took a reference */
  97. fsnotify_put_mark(fsn_mark);
  98. return send;
  99. }
  100. static bool should_send_inode_event(struct fsnotify_group *group, struct inode *inode,
  101. __u32 mask)
  102. {
  103. struct fsnotify_mark *fsn_mark;
  104. bool send;
  105. pr_debug("%s: group=%p inode=%p mask=%x\n",
  106. __func__, group, inode, mask);
  107. fsn_mark = fsnotify_find_inode_mark(group, inode);
  108. if (!fsn_mark)
  109. return false;
  110. /* if the event is for a child and this inode doesn't care about
  111. * events on the child, don't send it! */
  112. if ((mask & FS_EVENT_ON_CHILD) &&
  113. !(fsn_mark->mask & FS_EVENT_ON_CHILD)) {
  114. send = false;
  115. } else {
  116. /*
  117. * We care about children, but do we care about this particular
  118. * type of event?
  119. */
  120. mask = (mask & ~FS_EVENT_ON_CHILD);
  121. send = (fsn_mark->mask & mask);
  122. }
  123. /* find took a reference */
  124. fsnotify_put_mark(fsn_mark);
  125. return send;
  126. }
  127. static bool fanotify_should_send_event(struct fsnotify_group *group, struct inode *to_tell,
  128. struct vfsmount *mnt, __u32 mask, void *data,
  129. int data_type)
  130. {
  131. pr_debug("%s: group=%p to_tell=%p mnt=%p mask=%x data=%p data_type=%d\n",
  132. __func__, group, to_tell, mnt, mask, data, data_type);
  133. /* sorry, fanotify only gives a damn about files and dirs */
  134. if (!S_ISREG(to_tell->i_mode) &&
  135. !S_ISDIR(to_tell->i_mode))
  136. return false;
  137. /* if we don't have enough info to send an event to userspace say no */
  138. if (data_type != FSNOTIFY_EVENT_PATH)
  139. return false;
  140. if (mnt)
  141. return should_send_vfsmount_event(group, mnt, mask);
  142. else
  143. return should_send_inode_event(group, to_tell, mask);
  144. }
  145. const struct fsnotify_ops fanotify_fsnotify_ops = {
  146. .handle_event = fanotify_handle_event,
  147. .should_send_event = fanotify_should_send_event,
  148. .free_group_priv = NULL,
  149. .free_event_priv = NULL,
  150. .freeing_mark = NULL,
  151. };