inode_mark.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 an inode, destroy all of the marks associated with that inode.
  57. */
  58. void fsnotify_clear_marks_by_inode(struct inode *inode)
  59. {
  60. struct fsnotify_mark *mark;
  61. struct hlist_node *n;
  62. LIST_HEAD(free_list);
  63. spin_lock(&inode->i_lock);
  64. hlist_for_each_entry_safe(mark, n, &inode->i_fsnotify_marks, obj_list) {
  65. list_add(&mark->free_list, &free_list);
  66. hlist_del_init_rcu(&mark->obj_list);
  67. fsnotify_get_mark(mark);
  68. }
  69. spin_unlock(&inode->i_lock);
  70. fsnotify_destroy_marks(&free_list);
  71. }
  72. /*
  73. * Given a group clear all of the inode marks associated with that group.
  74. */
  75. void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
  76. {
  77. fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_INODE);
  78. }
  79. /*
  80. * given a group and inode, find the mark associated with that combination.
  81. * if found take a reference to that mark and return it, else return NULL
  82. */
  83. struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group,
  84. struct inode *inode)
  85. {
  86. struct fsnotify_mark *mark;
  87. spin_lock(&inode->i_lock);
  88. mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
  89. spin_unlock(&inode->i_lock);
  90. return mark;
  91. }
  92. /*
  93. * If we are setting a mark mask on an inode mark we should pin the inode
  94. * in memory.
  95. */
  96. void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *mark,
  97. __u32 mask)
  98. {
  99. struct inode *inode;
  100. assert_spin_locked(&mark->lock);
  101. if (mask &&
  102. mark->inode &&
  103. !(mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) {
  104. mark->flags |= FSNOTIFY_MARK_FLAG_OBJECT_PINNED;
  105. inode = igrab(mark->inode);
  106. /*
  107. * we shouldn't be able to get here if the inode wasn't
  108. * already safely held in memory. But bug in case it
  109. * ever is wrong.
  110. */
  111. BUG_ON(!inode);
  112. }
  113. }
  114. /*
  115. * Attach an initialized mark to a given inode.
  116. * These marks may be used for the fsnotify backend to determine which
  117. * event types should be delivered to which group and for which inodes. These
  118. * marks are ordered according to priority, highest number first, and then by
  119. * the group's location in memory.
  120. */
  121. int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
  122. struct fsnotify_group *group, struct inode *inode,
  123. int allow_dups)
  124. {
  125. int ret;
  126. mark->flags |= FSNOTIFY_MARK_FLAG_INODE;
  127. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  128. assert_spin_locked(&mark->lock);
  129. spin_lock(&inode->i_lock);
  130. mark->inode = inode;
  131. ret = fsnotify_add_mark_list(&inode->i_fsnotify_marks, mark,
  132. allow_dups);
  133. inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks);
  134. spin_unlock(&inode->i_lock);
  135. return ret;
  136. }
  137. /**
  138. * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
  139. * @list: list of inodes being unmounted (sb->s_inodes)
  140. *
  141. * Called during unmount with no locks held, so needs to be safe against
  142. * concurrent modifiers. We temporarily drop inode_sb_list_lock and CAN block.
  143. */
  144. void fsnotify_unmount_inodes(struct list_head *list)
  145. {
  146. struct inode *inode, *next_i, *need_iput = NULL;
  147. spin_lock(&inode_sb_list_lock);
  148. list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
  149. struct inode *need_iput_tmp;
  150. /*
  151. * We cannot __iget() an inode in state I_FREEING,
  152. * I_WILL_FREE, or I_NEW which is fine because by that point
  153. * the inode cannot have any associated watches.
  154. */
  155. spin_lock(&inode->i_lock);
  156. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
  157. spin_unlock(&inode->i_lock);
  158. continue;
  159. }
  160. /*
  161. * If i_count is zero, the inode cannot have any watches and
  162. * doing an __iget/iput with MS_ACTIVE clear would actually
  163. * evict all inodes with zero i_count from icache which is
  164. * unnecessarily violent and may in fact be illegal to do.
  165. */
  166. if (!atomic_read(&inode->i_count)) {
  167. spin_unlock(&inode->i_lock);
  168. continue;
  169. }
  170. need_iput_tmp = need_iput;
  171. need_iput = NULL;
  172. /* In case fsnotify_inode_delete() drops a reference. */
  173. if (inode != need_iput_tmp)
  174. __iget(inode);
  175. else
  176. need_iput_tmp = NULL;
  177. spin_unlock(&inode->i_lock);
  178. /* In case the dropping of a reference would nuke next_i. */
  179. while (&next_i->i_sb_list != list) {
  180. spin_lock(&next_i->i_lock);
  181. if (!(next_i->i_state & (I_FREEING | I_WILL_FREE)) &&
  182. atomic_read(&next_i->i_count)) {
  183. __iget(next_i);
  184. need_iput = next_i;
  185. spin_unlock(&next_i->i_lock);
  186. break;
  187. }
  188. spin_unlock(&next_i->i_lock);
  189. next_i = list_entry(next_i->i_sb_list.next,
  190. struct inode, i_sb_list);
  191. }
  192. /*
  193. * We can safely drop inode_sb_list_lock here because either
  194. * we actually hold references on both inode and next_i or
  195. * end of list. Also no new inodes will be added since the
  196. * umount has begun.
  197. */
  198. spin_unlock(&inode_sb_list_lock);
  199. if (need_iput_tmp)
  200. iput(need_iput_tmp);
  201. /* for each watch, send FS_UNMOUNT and then remove it */
  202. fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  203. fsnotify_inode_delete(inode);
  204. iput(inode);
  205. spin_lock(&inode_sb_list_lock);
  206. }
  207. spin_unlock(&inode_sb_list_lock);
  208. }