group.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
  4. */
  5. #include <linux/list.h>
  6. #include <linux/mutex.h>
  7. #include <linux/slab.h>
  8. #include <linux/srcu.h>
  9. #include <linux/rculist.h>
  10. #include <linux/wait.h>
  11. #include <linux/memcontrol.h>
  12. #include <linux/fsnotify_backend.h>
  13. #include "fsnotify.h"
  14. #include <linux/atomic.h>
  15. /*
  16. * Final freeing of a group
  17. */
  18. static void fsnotify_final_destroy_group(struct fsnotify_group *group)
  19. {
  20. if (group->ops->free_group_priv)
  21. group->ops->free_group_priv(group);
  22. mem_cgroup_put(group->memcg);
  23. mutex_destroy(&group->mark_mutex);
  24. kfree(group);
  25. }
  26. /*
  27. * Stop queueing new events for this group. Once this function returns
  28. * fsnotify_add_event() will not add any new events to the group's queue.
  29. */
  30. void fsnotify_group_stop_queueing(struct fsnotify_group *group)
  31. {
  32. spin_lock(&group->notification_lock);
  33. group->shutdown = true;
  34. spin_unlock(&group->notification_lock);
  35. }
  36. /*
  37. * Trying to get rid of a group. Remove all marks, flush all events and release
  38. * the group reference.
  39. * Note that another thread calling fsnotify_clear_marks_by_group() may still
  40. * hold a ref to the group.
  41. */
  42. void fsnotify_destroy_group(struct fsnotify_group *group)
  43. {
  44. /*
  45. * Stop queueing new events. The code below is careful enough to not
  46. * require this but fanotify needs to stop queuing events even before
  47. * fsnotify_destroy_group() is called and this makes the other callers
  48. * of fsnotify_destroy_group() to see the same behavior.
  49. */
  50. fsnotify_group_stop_queueing(group);
  51. /* Clear all marks for this group and queue them for destruction */
  52. fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_ALL_TYPES_MASK);
  53. /*
  54. * Some marks can still be pinned when waiting for response from
  55. * userspace. Wait for those now. fsnotify_prepare_user_wait() will
  56. * not succeed now so this wait is race-free.
  57. */
  58. wait_event(group->notification_waitq, !atomic_read(&group->user_waits));
  59. /*
  60. * Wait until all marks get really destroyed. We could actually destroy
  61. * them ourselves instead of waiting for worker to do it, however that
  62. * would be racy as worker can already be processing some marks before
  63. * we even entered fsnotify_destroy_group().
  64. */
  65. fsnotify_wait_marks_destroyed();
  66. /*
  67. * Since we have waited for fsnotify_mark_srcu in
  68. * fsnotify_mark_destroy_list() there can be no outstanding event
  69. * notification against this group. So clearing the notification queue
  70. * of all events is reliable now.
  71. */
  72. fsnotify_flush_notify(group);
  73. /*
  74. * Destroy overflow event (we cannot use fsnotify_destroy_event() as
  75. * that deliberately ignores overflow events.
  76. */
  77. if (group->overflow_event)
  78. group->ops->free_event(group->overflow_event);
  79. fsnotify_put_group(group);
  80. }
  81. /*
  82. * Get reference to a group.
  83. */
  84. void fsnotify_get_group(struct fsnotify_group *group)
  85. {
  86. refcount_inc(&group->refcnt);
  87. }
  88. /*
  89. * Drop a reference to a group. Free it if it's through.
  90. */
  91. void fsnotify_put_group(struct fsnotify_group *group)
  92. {
  93. if (refcount_dec_and_test(&group->refcnt))
  94. fsnotify_final_destroy_group(group);
  95. }
  96. EXPORT_SYMBOL_GPL(fsnotify_put_group);
  97. /*
  98. * Create a new fsnotify_group and hold a reference for the group returned.
  99. */
  100. struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
  101. {
  102. struct fsnotify_group *group;
  103. group = kzalloc(sizeof(struct fsnotify_group), GFP_KERNEL);
  104. if (!group)
  105. return ERR_PTR(-ENOMEM);
  106. /* set to 0 when there a no external references to this group */
  107. refcount_set(&group->refcnt, 1);
  108. atomic_set(&group->num_marks, 0);
  109. atomic_set(&group->user_waits, 0);
  110. spin_lock_init(&group->notification_lock);
  111. INIT_LIST_HEAD(&group->notification_list);
  112. init_waitqueue_head(&group->notification_waitq);
  113. group->max_events = UINT_MAX;
  114. mutex_init(&group->mark_mutex);
  115. INIT_LIST_HEAD(&group->marks_list);
  116. group->ops = ops;
  117. return group;
  118. }
  119. EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
  120. int fsnotify_fasync(int fd, struct file *file, int on)
  121. {
  122. struct fsnotify_group *group = file->private_data;
  123. return fasync_helper(fd, file, on, &group->fsn_fa) >= 0 ? 0 : -EIO;
  124. }