vfsmount_mark.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/writeback.h> /* for inode_lock */
  27. #include <asm/atomic.h>
  28. #include <linux/fsnotify_backend.h>
  29. #include "fsnotify.h"
  30. void fsnotify_clear_marks_by_mount(struct vfsmount *mnt)
  31. {
  32. struct fsnotify_mark *mark, *lmark;
  33. struct hlist_node *pos, *n;
  34. LIST_HEAD(free_list);
  35. spin_lock(&mnt->mnt_root->d_lock);
  36. hlist_for_each_entry_safe(mark, pos, n, &mnt->mnt_fsnotify_marks, m.m_list) {
  37. list_add(&mark->m.free_m_list, &free_list);
  38. hlist_del_init(&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. fsnotify_destroy_mark(mark);
  44. fsnotify_put_mark(mark);
  45. }
  46. }
  47. /*
  48. * Recalculate the mask of events relevant to a given vfsmount locked.
  49. */
  50. static void fsnotify_recalc_vfsmount_mask_locked(struct vfsmount *mnt)
  51. {
  52. struct fsnotify_mark *mark;
  53. struct hlist_node *pos;
  54. __u32 new_mask = 0;
  55. assert_spin_locked(&mnt->mnt_root->d_lock);
  56. hlist_for_each_entry(mark, pos, &mnt->mnt_fsnotify_marks, m.m_list)
  57. new_mask |= mark->mask;
  58. mnt->mnt_fsnotify_mask = new_mask;
  59. }
  60. /*
  61. * Recalculate the mnt->mnt_fsnotify_mask, or the mask of all FS_* event types
  62. * any notifier is interested in hearing for this mount point
  63. */
  64. void fsnotify_recalc_vfsmount_mask(struct vfsmount *mnt)
  65. {
  66. spin_lock(&mnt->mnt_root->d_lock);
  67. fsnotify_recalc_vfsmount_mask_locked(mnt);
  68. spin_unlock(&mnt->mnt_root->d_lock);
  69. }
  70. void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark *mark)
  71. {
  72. struct vfsmount *mnt = mark->m.mnt;
  73. assert_spin_locked(&mark->lock);
  74. assert_spin_locked(&mark->group->mark_lock);
  75. spin_lock(&mnt->mnt_root->d_lock);
  76. hlist_del_init(&mark->m.m_list);
  77. mark->m.mnt = NULL;
  78. fsnotify_recalc_vfsmount_mask_locked(mnt);
  79. spin_unlock(&mnt->mnt_root->d_lock);
  80. }
  81. static struct fsnotify_mark *fsnotify_find_vfsmount_mark_locked(struct fsnotify_group *group,
  82. struct vfsmount *mnt)
  83. {
  84. struct fsnotify_mark *mark;
  85. struct hlist_node *pos;
  86. assert_spin_locked(&mnt->mnt_root->d_lock);
  87. hlist_for_each_entry(mark, pos, &mnt->mnt_fsnotify_marks, m.m_list) {
  88. if (mark->group == group) {
  89. fsnotify_get_mark(mark);
  90. return mark;
  91. }
  92. }
  93. return NULL;
  94. }
  95. /*
  96. * given a group and vfsmount, find the mark associated with that combination.
  97. * if found take a reference to that mark and return it, else return NULL
  98. */
  99. struct fsnotify_mark *fsnotify_find_vfsmount_mark(struct fsnotify_group *group,
  100. struct vfsmount *mnt)
  101. {
  102. struct fsnotify_mark *mark;
  103. spin_lock(&mnt->mnt_root->d_lock);
  104. mark = fsnotify_find_vfsmount_mark_locked(group, mnt);
  105. spin_unlock(&mnt->mnt_root->d_lock);
  106. return mark;
  107. }
  108. /*
  109. * Attach an initialized mark to a given group and vfsmount.
  110. * These marks may be used for the fsnotify backend to determine which
  111. * event types should be delivered to which groups.
  112. */
  113. int fsnotify_add_vfsmount_mark(struct fsnotify_mark *mark,
  114. struct fsnotify_group *group, struct vfsmount *mnt,
  115. int allow_dups)
  116. {
  117. struct fsnotify_mark *lmark = NULL;
  118. int ret = 0;
  119. mark->flags = FSNOTIFY_MARK_FLAG_VFSMOUNT;
  120. /*
  121. * LOCKING ORDER!!!!
  122. * mark->lock
  123. * group->mark_lock
  124. * mnt->mnt_root->d_lock
  125. */
  126. assert_spin_locked(&mark->lock);
  127. assert_spin_locked(&group->mark_lock);
  128. spin_lock(&mnt->mnt_root->d_lock);
  129. if (!allow_dups)
  130. lmark = fsnotify_find_vfsmount_mark_locked(group, mnt);
  131. if (!lmark) {
  132. mark->m.mnt = mnt;
  133. hlist_add_head(&mark->m.m_list, &mnt->mnt_fsnotify_marks);
  134. fsnotify_recalc_vfsmount_mask_locked(mnt);
  135. } else {
  136. ret = -EEXIST;
  137. }
  138. spin_unlock(&mnt->mnt_root->d_lock);
  139. return ret;
  140. }