inode_mark.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/writeback.h> /* for inode_lock */
  25. #include <asm/atomic.h>
  26. #include <linux/fsnotify_backend.h>
  27. #include "fsnotify.h"
  28. /*
  29. * Recalculate the mask of events relevant to a given inode locked.
  30. */
  31. static void fsnotify_recalc_inode_mask_locked(struct inode *inode)
  32. {
  33. struct fsnotify_mark *mark;
  34. struct hlist_node *pos;
  35. __u32 new_mask = 0;
  36. assert_spin_locked(&inode->i_lock);
  37. hlist_for_each_entry(mark, pos, &inode->i_fsnotify_marks, i.i_list)
  38. new_mask |= mark->mask;
  39. inode->i_fsnotify_mask = new_mask;
  40. }
  41. /*
  42. * Recalculate the inode->i_fsnotify_mask, or the mask of all FS_* event types
  43. * any notifier is interested in hearing for this inode.
  44. */
  45. void fsnotify_recalc_inode_mask(struct inode *inode)
  46. {
  47. spin_lock(&inode->i_lock);
  48. fsnotify_recalc_inode_mask_locked(inode);
  49. spin_unlock(&inode->i_lock);
  50. __fsnotify_update_child_dentry_flags(inode);
  51. }
  52. void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark)
  53. {
  54. struct inode *inode = mark->i.inode;
  55. assert_spin_locked(&mark->lock);
  56. assert_spin_locked(&mark->group->mark_lock);
  57. spin_lock(&inode->i_lock);
  58. hlist_del_init(&mark->i.i_list);
  59. mark->i.inode = NULL;
  60. /*
  61. * this mark is now off the inode->i_fsnotify_marks list and we
  62. * hold the inode->i_lock, so this is the perfect time to update the
  63. * inode->i_fsnotify_mask
  64. */
  65. fsnotify_recalc_inode_mask_locked(inode);
  66. spin_unlock(&inode->i_lock);
  67. }
  68. /*
  69. * Given an inode, destroy all of the marks associated with that inode.
  70. */
  71. void fsnotify_clear_marks_by_inode(struct inode *inode)
  72. {
  73. struct fsnotify_mark *mark, *lmark;
  74. struct hlist_node *pos, *n;
  75. LIST_HEAD(free_list);
  76. spin_lock(&inode->i_lock);
  77. hlist_for_each_entry_safe(mark, pos, n, &inode->i_fsnotify_marks, i.i_list) {
  78. list_add(&mark->i.free_i_list, &free_list);
  79. hlist_del_init(&mark->i.i_list);
  80. fsnotify_get_mark(mark);
  81. }
  82. spin_unlock(&inode->i_lock);
  83. list_for_each_entry_safe(mark, lmark, &free_list, i.free_i_list) {
  84. fsnotify_destroy_mark(mark);
  85. fsnotify_put_mark(mark);
  86. }
  87. }
  88. /*
  89. * given a group and inode, find the mark associated with that combination.
  90. * if found take a reference to that mark and return it, else return NULL
  91. */
  92. struct fsnotify_mark *fsnotify_find_inode_mark_locked(struct fsnotify_group *group,
  93. struct inode *inode)
  94. {
  95. struct fsnotify_mark *mark;
  96. struct hlist_node *pos;
  97. assert_spin_locked(&inode->i_lock);
  98. hlist_for_each_entry(mark, pos, &inode->i_fsnotify_marks, i.i_list) {
  99. if (mark->group == group) {
  100. fsnotify_get_mark(mark);
  101. return mark;
  102. }
  103. }
  104. return NULL;
  105. }
  106. /*
  107. * given a group and inode, find the mark associated with that combination.
  108. * if found take a reference to that mark and return it, else return NULL
  109. */
  110. struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group,
  111. struct inode *inode)
  112. {
  113. struct fsnotify_mark *mark;
  114. spin_lock(&inode->i_lock);
  115. mark = fsnotify_find_inode_mark_locked(group, inode);
  116. spin_unlock(&inode->i_lock);
  117. return mark;
  118. }
  119. /*
  120. * If we are setting a mark mask on an inode mark we should pin the inode
  121. * in memory.
  122. */
  123. void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *mark,
  124. __u32 mask)
  125. {
  126. struct inode *inode;
  127. assert_spin_locked(&mark->lock);
  128. if (mask &&
  129. mark->i.inode &&
  130. !(mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) {
  131. mark->flags |= FSNOTIFY_MARK_FLAG_OBJECT_PINNED;
  132. inode = igrab(mark->i.inode);
  133. /*
  134. * we shouldn't be able to get here if the inode wasn't
  135. * already safely held in memory. But bug in case it
  136. * ever is wrong.
  137. */
  138. BUG_ON(!inode);
  139. }
  140. }
  141. /*
  142. * Attach an initialized mark to a given group and inode.
  143. * These marks may be used for the fsnotify backend to determine which
  144. * event types should be delivered to which group and for which inodes.
  145. */
  146. int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
  147. struct fsnotify_group *group, struct inode *inode,
  148. int allow_dups)
  149. {
  150. struct fsnotify_mark *lmark = NULL;
  151. int ret = 0;
  152. mark->flags = FSNOTIFY_MARK_FLAG_INODE;
  153. assert_spin_locked(&mark->lock);
  154. assert_spin_locked(&group->mark_lock);
  155. spin_lock(&inode->i_lock);
  156. if (!allow_dups)
  157. lmark = fsnotify_find_inode_mark_locked(group, inode);
  158. if (!lmark) {
  159. mark->i.inode = inode;
  160. hlist_add_head(&mark->i.i_list, &inode->i_fsnotify_marks);
  161. fsnotify_recalc_inode_mask_locked(inode);
  162. }
  163. spin_unlock(&inode->i_lock);
  164. if (lmark)
  165. ret = -EEXIST;
  166. return ret;
  167. }
  168. /**
  169. * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
  170. * @list: list of inodes being unmounted (sb->s_inodes)
  171. *
  172. * Called with inode_lock held, protecting the unmounting super block's list
  173. * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
  174. * We temporarily drop inode_lock, however, and CAN block.
  175. */
  176. void fsnotify_unmount_inodes(struct list_head *list)
  177. {
  178. struct inode *inode, *next_i, *need_iput = NULL;
  179. list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
  180. struct inode *need_iput_tmp;
  181. /*
  182. * We cannot __iget() an inode in state I_CLEAR, I_FREEING,
  183. * I_WILL_FREE, or I_NEW which is fine because by that point
  184. * the inode cannot have any associated watches.
  185. */
  186. if (inode->i_state & (I_CLEAR|I_FREEING|I_WILL_FREE|I_NEW))
  187. continue;
  188. /*
  189. * If i_count is zero, the inode cannot have any watches and
  190. * doing an __iget/iput with MS_ACTIVE clear would actually
  191. * evict all inodes with zero i_count from icache which is
  192. * unnecessarily violent and may in fact be illegal to do.
  193. */
  194. if (!atomic_read(&inode->i_count))
  195. continue;
  196. need_iput_tmp = need_iput;
  197. need_iput = NULL;
  198. /* In case fsnotify_inode_delete() drops a reference. */
  199. if (inode != need_iput_tmp)
  200. __iget(inode);
  201. else
  202. need_iput_tmp = NULL;
  203. /* In case the dropping of a reference would nuke next_i. */
  204. if ((&next_i->i_sb_list != list) &&
  205. atomic_read(&next_i->i_count) &&
  206. !(next_i->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE))) {
  207. __iget(next_i);
  208. need_iput = next_i;
  209. }
  210. /*
  211. * We can safely drop inode_lock here because we hold
  212. * references on both inode and next_i. Also no new inodes
  213. * will be added since the umount has begun. Finally,
  214. * iprune_mutex keeps shrink_icache_memory() away.
  215. */
  216. spin_unlock(&inode_lock);
  217. if (need_iput_tmp)
  218. iput(need_iput_tmp);
  219. /* for each watch, send FS_UNMOUNT and then remove it */
  220. fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  221. fsnotify_inode_delete(inode);
  222. iput(inode);
  223. spin_lock(&inode_lock);
  224. }
  225. }