vfsmount_mark.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; see the file COPYING. If not, write to
  16. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/mount.h>
  23. #include <linux/mutex.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/atomic.h>
  26. #include <linux/fsnotify_backend.h>
  27. #include "fsnotify.h"
  28. #include "../mount.h"
  29. void fsnotify_clear_marks_by_mount(struct vfsmount *mnt)
  30. {
  31. struct fsnotify_mark *mark, *lmark;
  32. struct hlist_node *pos, *n;
  33. struct mount *m = real_mount(mnt);
  34. LIST_HEAD(free_list);
  35. spin_lock(&mnt->mnt_root->d_lock);
  36. hlist_for_each_entry_safe(mark, pos, n, &m->mnt_fsnotify_marks, m.m_list) {
  37. list_add(&mark->m.free_m_list, &free_list);
  38. hlist_del_init_rcu(&mark->m.m_list);
  39. fsnotify_get_mark(mark);
  40. }
  41. spin_unlock(&mnt->mnt_root->d_lock);
  42. list_for_each_entry_safe(mark, lmark, &free_list, m.free_m_list) {
  43. struct fsnotify_group *group;
  44. spin_lock(&mark->lock);
  45. fsnotify_get_group(mark->group);
  46. group = mark->group;
  47. spin_unlock(&mark->lock);
  48. fsnotify_destroy_mark(mark, group);
  49. fsnotify_put_mark(mark);
  50. fsnotify_put_group(group);
  51. }
  52. }
  53. void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
  54. {
  55. fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_VFSMOUNT);
  56. }
  57. /*
  58. * Recalculate the mask of events relevant to a given vfsmount locked.
  59. */
  60. static void fsnotify_recalc_vfsmount_mask_locked(struct vfsmount *mnt)
  61. {
  62. struct mount *m = real_mount(mnt);
  63. struct fsnotify_mark *mark;
  64. struct hlist_node *pos;
  65. __u32 new_mask = 0;
  66. assert_spin_locked(&mnt->mnt_root->d_lock);
  67. hlist_for_each_entry(mark, pos, &m->mnt_fsnotify_marks, m.m_list)
  68. new_mask |= mark->mask;
  69. m->mnt_fsnotify_mask = new_mask;
  70. }
  71. /*
  72. * Recalculate the mnt->mnt_fsnotify_mask, or the mask of all FS_* event types
  73. * any notifier is interested in hearing for this mount point
  74. */
  75. void fsnotify_recalc_vfsmount_mask(struct vfsmount *mnt)
  76. {
  77. spin_lock(&mnt->mnt_root->d_lock);
  78. fsnotify_recalc_vfsmount_mask_locked(mnt);
  79. spin_unlock(&mnt->mnt_root->d_lock);
  80. }
  81. void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark *mark)
  82. {
  83. struct vfsmount *mnt = mark->m.mnt;
  84. BUG_ON(!mutex_is_locked(&mark->group->mark_mutex));
  85. assert_spin_locked(&mark->lock);
  86. spin_lock(&mnt->mnt_root->d_lock);
  87. hlist_del_init_rcu(&mark->m.m_list);
  88. mark->m.mnt = NULL;
  89. fsnotify_recalc_vfsmount_mask_locked(mnt);
  90. spin_unlock(&mnt->mnt_root->d_lock);
  91. }
  92. static struct fsnotify_mark *fsnotify_find_vfsmount_mark_locked(struct fsnotify_group *group,
  93. struct vfsmount *mnt)
  94. {
  95. struct mount *m = real_mount(mnt);
  96. struct fsnotify_mark *mark;
  97. struct hlist_node *pos;
  98. assert_spin_locked(&mnt->mnt_root->d_lock);
  99. hlist_for_each_entry(mark, pos, &m->mnt_fsnotify_marks, m.m_list) {
  100. if (mark->group == group) {
  101. fsnotify_get_mark(mark);
  102. return mark;
  103. }
  104. }
  105. return NULL;
  106. }
  107. /*
  108. * given a group and vfsmount, find the mark associated with that combination.
  109. * if found take a reference to that mark and return it, else return NULL
  110. */
  111. struct fsnotify_mark *fsnotify_find_vfsmount_mark(struct fsnotify_group *group,
  112. struct vfsmount *mnt)
  113. {
  114. struct fsnotify_mark *mark;
  115. spin_lock(&mnt->mnt_root->d_lock);
  116. mark = fsnotify_find_vfsmount_mark_locked(group, mnt);
  117. spin_unlock(&mnt->mnt_root->d_lock);
  118. return mark;
  119. }
  120. /*
  121. * Attach an initialized mark to a given group and vfsmount.
  122. * These marks may be used for the fsnotify backend to determine which
  123. * event types should be delivered to which groups.
  124. */
  125. int fsnotify_add_vfsmount_mark(struct fsnotify_mark *mark,
  126. struct fsnotify_group *group, struct vfsmount *mnt,
  127. int allow_dups)
  128. {
  129. struct mount *m = real_mount(mnt);
  130. struct fsnotify_mark *lmark;
  131. struct hlist_node *node, *last = NULL;
  132. int ret = 0;
  133. mark->flags |= FSNOTIFY_MARK_FLAG_VFSMOUNT;
  134. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  135. assert_spin_locked(&mark->lock);
  136. spin_lock(&mnt->mnt_root->d_lock);
  137. mark->m.mnt = mnt;
  138. /* is mark the first mark? */
  139. if (hlist_empty(&m->mnt_fsnotify_marks)) {
  140. hlist_add_head_rcu(&mark->m.m_list, &m->mnt_fsnotify_marks);
  141. goto out;
  142. }
  143. /* should mark be in the middle of the current list? */
  144. hlist_for_each_entry(lmark, node, &m->mnt_fsnotify_marks, m.m_list) {
  145. last = node;
  146. if ((lmark->group == group) && !allow_dups) {
  147. ret = -EEXIST;
  148. goto out;
  149. }
  150. if (mark->group->priority < lmark->group->priority)
  151. continue;
  152. if ((mark->group->priority == lmark->group->priority) &&
  153. (mark->group < lmark->group))
  154. continue;
  155. hlist_add_before_rcu(&mark->m.m_list, &lmark->m.m_list);
  156. goto out;
  157. }
  158. BUG_ON(last == NULL);
  159. /* mark should be the last entry. last is the current last entry */
  160. hlist_add_after_rcu(last, &mark->m.m_list);
  161. out:
  162. fsnotify_recalc_vfsmount_mask_locked(mnt);
  163. spin_unlock(&mnt->mnt_root->d_lock);
  164. return ret;
  165. }