inode_mark.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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, *next_i, *need_iput = NULL;
  130. spin_lock(&sb->s_inode_list_lock);
  131. list_for_each_entry_safe(inode, next_i, &sb->s_inodes, i_sb_list) {
  132. struct inode *need_iput_tmp;
  133. /*
  134. * We cannot __iget() an inode in state I_FREEING,
  135. * I_WILL_FREE, or I_NEW which is fine because by that point
  136. * the inode cannot have any associated watches.
  137. */
  138. spin_lock(&inode->i_lock);
  139. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
  140. spin_unlock(&inode->i_lock);
  141. continue;
  142. }
  143. /*
  144. * If i_count is zero, the inode cannot have any watches and
  145. * doing an __iget/iput with MS_ACTIVE clear would actually
  146. * evict all inodes with zero i_count from icache which is
  147. * unnecessarily violent and may in fact be illegal to do.
  148. */
  149. if (!atomic_read(&inode->i_count)) {
  150. spin_unlock(&inode->i_lock);
  151. continue;
  152. }
  153. need_iput_tmp = need_iput;
  154. need_iput = NULL;
  155. /* In case fsnotify_inode_delete() drops a reference. */
  156. if (inode != need_iput_tmp)
  157. __iget(inode);
  158. else
  159. need_iput_tmp = NULL;
  160. spin_unlock(&inode->i_lock);
  161. /* In case the dropping of a reference would nuke next_i. */
  162. while (&next_i->i_sb_list != &sb->s_inodes) {
  163. spin_lock(&next_i->i_lock);
  164. if (!(next_i->i_state & (I_FREEING | I_WILL_FREE)) &&
  165. atomic_read(&next_i->i_count)) {
  166. __iget(next_i);
  167. need_iput = next_i;
  168. spin_unlock(&next_i->i_lock);
  169. break;
  170. }
  171. spin_unlock(&next_i->i_lock);
  172. next_i = list_entry(next_i->i_sb_list.next,
  173. struct inode, i_sb_list);
  174. }
  175. /*
  176. * We can safely drop s_inode_list_lock here because either
  177. * we actually hold references on both inode and next_i or
  178. * end of list. Also no new inodes will be added since the
  179. * umount has begun.
  180. */
  181. spin_unlock(&sb->s_inode_list_lock);
  182. if (need_iput_tmp)
  183. iput(need_iput_tmp);
  184. /* for each watch, send FS_UNMOUNT and then remove it */
  185. fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  186. fsnotify_inode_delete(inode);
  187. iput(inode);
  188. spin_lock(&sb->s_inode_list_lock);
  189. }
  190. spin_unlock(&sb->s_inode_list_lock);
  191. }