inode_mark.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/mutex.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/atomic.h>
  25. #include <linux/fsnotify_backend.h>
  26. #include "fsnotify.h"
  27. #include "../internal.h"
  28. /*
  29. * Recalculate the inode->i_fsnotify_mask, or the mask of all FS_* event types
  30. * any notifier is interested in hearing for this inode.
  31. */
  32. void fsnotify_recalc_inode_mask(struct inode *inode)
  33. {
  34. spin_lock(&inode->i_lock);
  35. inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks);
  36. spin_unlock(&inode->i_lock);
  37. __fsnotify_update_child_dentry_flags(inode);
  38. }
  39. void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark)
  40. {
  41. struct inode *inode = mark->inode;
  42. BUG_ON(!mutex_is_locked(&mark->group->mark_mutex));
  43. assert_spin_locked(&mark->lock);
  44. spin_lock(&inode->i_lock);
  45. hlist_del_init_rcu(&mark->obj_list);
  46. mark->inode = NULL;
  47. /*
  48. * this mark is now off the inode->i_fsnotify_marks list and we
  49. * hold the inode->i_lock, so this is the perfect time to update the
  50. * inode->i_fsnotify_mask
  51. */
  52. inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks);
  53. spin_unlock(&inode->i_lock);
  54. }
  55. /*
  56. * Given a group clear all of the inode marks associated with that group.
  57. */
  58. void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
  59. {
  60. fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_INODE);
  61. }
  62. /*
  63. * given a group and inode, find the mark associated with that combination.
  64. * if found take a reference to that mark and return it, else return NULL
  65. */
  66. struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group,
  67. struct inode *inode)
  68. {
  69. struct fsnotify_mark *mark;
  70. spin_lock(&inode->i_lock);
  71. mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
  72. spin_unlock(&inode->i_lock);
  73. return mark;
  74. }
  75. /*
  76. * If we are setting a mark mask on an inode mark we should pin the inode
  77. * in memory.
  78. */
  79. void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *mark,
  80. __u32 mask)
  81. {
  82. struct inode *inode;
  83. assert_spin_locked(&mark->lock);
  84. if (mask &&
  85. mark->inode &&
  86. !(mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) {
  87. mark->flags |= FSNOTIFY_MARK_FLAG_OBJECT_PINNED;
  88. inode = igrab(mark->inode);
  89. /*
  90. * we shouldn't be able to get here if the inode wasn't
  91. * already safely held in memory. But bug in case it
  92. * ever is wrong.
  93. */
  94. BUG_ON(!inode);
  95. }
  96. }
  97. /*
  98. * Attach an initialized mark to a given inode.
  99. * These marks may be used for the fsnotify backend to determine which
  100. * event types should be delivered to which group and for which inodes. These
  101. * marks are ordered according to priority, highest number first, and then by
  102. * the group's location in memory.
  103. */
  104. int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
  105. struct fsnotify_group *group, struct inode *inode,
  106. int allow_dups)
  107. {
  108. int ret;
  109. mark->flags |= FSNOTIFY_MARK_FLAG_INODE;
  110. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  111. assert_spin_locked(&mark->lock);
  112. spin_lock(&inode->i_lock);
  113. mark->inode = inode;
  114. ret = fsnotify_add_mark_list(&inode->i_fsnotify_marks, mark,
  115. allow_dups);
  116. inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks);
  117. spin_unlock(&inode->i_lock);
  118. return ret;
  119. }
  120. /**
  121. * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
  122. * @sb: superblock being unmounted.
  123. *
  124. * Called during unmount with no locks held, so needs to be safe against
  125. * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
  126. */
  127. void fsnotify_unmount_inodes(struct super_block *sb)
  128. {
  129. struct inode *inode, *iput_inode = NULL;
  130. spin_lock(&sb->s_inode_list_lock);
  131. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  132. /*
  133. * We cannot __iget() an inode in state I_FREEING,
  134. * I_WILL_FREE, or I_NEW which is fine because by that point
  135. * the inode cannot have any associated watches.
  136. */
  137. spin_lock(&inode->i_lock);
  138. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
  139. spin_unlock(&inode->i_lock);
  140. continue;
  141. }
  142. /*
  143. * If i_count is zero, the inode cannot have any watches and
  144. * doing an __iget/iput with MS_ACTIVE clear would actually
  145. * evict all inodes with zero i_count from icache which is
  146. * unnecessarily violent and may in fact be illegal to do.
  147. */
  148. if (!atomic_read(&inode->i_count)) {
  149. spin_unlock(&inode->i_lock);
  150. continue;
  151. }
  152. __iget(inode);
  153. spin_unlock(&inode->i_lock);
  154. spin_unlock(&sb->s_inode_list_lock);
  155. if (iput_inode)
  156. iput(iput_inode);
  157. /* for each watch, send FS_UNMOUNT and then remove it */
  158. fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  159. fsnotify_inode_delete(inode);
  160. iput_inode = inode;
  161. spin_lock(&sb->s_inode_list_lock);
  162. }
  163. spin_unlock(&sb->s_inode_list_lock);
  164. if (iput_inode)
  165. iput(iput_inode);
  166. }