inode_mark.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 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. __u32 new_mask = 0;
  35. assert_spin_locked(&inode->i_lock);
  36. hlist_for_each_entry(mark, &inode->i_fsnotify_marks, i.i_list)
  37. new_mask |= mark->mask;
  38. inode->i_fsnotify_mask = new_mask;
  39. }
  40. /*
  41. * Recalculate the inode->i_fsnotify_mask, or the mask of all FS_* event types
  42. * any notifier is interested in hearing for this inode.
  43. */
  44. void fsnotify_recalc_inode_mask(struct inode *inode)
  45. {
  46. spin_lock(&inode->i_lock);
  47. fsnotify_recalc_inode_mask_locked(inode);
  48. spin_unlock(&inode->i_lock);
  49. __fsnotify_update_child_dentry_flags(inode);
  50. }
  51. void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark)
  52. {
  53. struct inode *inode = mark->i.inode;
  54. BUG_ON(!mutex_is_locked(&mark->group->mark_mutex));
  55. assert_spin_locked(&mark->lock);
  56. spin_lock(&inode->i_lock);
  57. hlist_del_init_rcu(&mark->i.i_list);
  58. mark->i.inode = NULL;
  59. /*
  60. * this mark is now off the inode->i_fsnotify_marks list and we
  61. * hold the inode->i_lock, so this is the perfect time to update the
  62. * inode->i_fsnotify_mask
  63. */
  64. fsnotify_recalc_inode_mask_locked(inode);
  65. spin_unlock(&inode->i_lock);
  66. }
  67. /*
  68. * Given an inode, destroy all of the marks associated with that inode.
  69. */
  70. void fsnotify_clear_marks_by_inode(struct inode *inode)
  71. {
  72. struct fsnotify_mark *mark, *lmark;
  73. struct hlist_node *n;
  74. LIST_HEAD(free_list);
  75. spin_lock(&inode->i_lock);
  76. hlist_for_each_entry_safe(mark, n, &inode->i_fsnotify_marks, i.i_list) {
  77. list_add(&mark->i.free_i_list, &free_list);
  78. hlist_del_init_rcu(&mark->i.i_list);
  79. fsnotify_get_mark(mark);
  80. }
  81. spin_unlock(&inode->i_lock);
  82. list_for_each_entry_safe(mark, lmark, &free_list, i.free_i_list) {
  83. struct fsnotify_group *group;
  84. spin_lock(&mark->lock);
  85. fsnotify_get_group(mark->group);
  86. group = mark->group;
  87. spin_unlock(&mark->lock);
  88. fsnotify_destroy_mark(mark, group);
  89. fsnotify_put_mark(mark);
  90. fsnotify_put_group(group);
  91. }
  92. }
  93. /*
  94. * Given a group clear all of the inode marks associated with that group.
  95. */
  96. void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
  97. {
  98. fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_INODE);
  99. }
  100. /*
  101. * given a group and inode, find the mark associated with that combination.
  102. * if found take a reference to that mark and return it, else return NULL
  103. */
  104. static struct fsnotify_mark *fsnotify_find_inode_mark_locked(
  105. struct fsnotify_group *group,
  106. struct inode *inode)
  107. {
  108. struct fsnotify_mark *mark;
  109. assert_spin_locked(&inode->i_lock);
  110. hlist_for_each_entry(mark, &inode->i_fsnotify_marks, i.i_list) {
  111. if (mark->group == group) {
  112. fsnotify_get_mark(mark);
  113. return mark;
  114. }
  115. }
  116. return NULL;
  117. }
  118. /*
  119. * given a group and inode, find the mark associated with that combination.
  120. * if found take a reference to that mark and return it, else return NULL
  121. */
  122. struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group,
  123. struct inode *inode)
  124. {
  125. struct fsnotify_mark *mark;
  126. spin_lock(&inode->i_lock);
  127. mark = fsnotify_find_inode_mark_locked(group, inode);
  128. spin_unlock(&inode->i_lock);
  129. return mark;
  130. }
  131. /*
  132. * If we are setting a mark mask on an inode mark we should pin the inode
  133. * in memory.
  134. */
  135. void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *mark,
  136. __u32 mask)
  137. {
  138. struct inode *inode;
  139. assert_spin_locked(&mark->lock);
  140. if (mask &&
  141. mark->i.inode &&
  142. !(mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) {
  143. mark->flags |= FSNOTIFY_MARK_FLAG_OBJECT_PINNED;
  144. inode = igrab(mark->i.inode);
  145. /*
  146. * we shouldn't be able to get here if the inode wasn't
  147. * already safely held in memory. But bug in case it
  148. * ever is wrong.
  149. */
  150. BUG_ON(!inode);
  151. }
  152. }
  153. /*
  154. * Attach an initialized mark to a given inode.
  155. * These marks may be used for the fsnotify backend to determine which
  156. * event types should be delivered to which group and for which inodes. These
  157. * marks are ordered according to priority, highest number first, and then by
  158. * the group's location in memory.
  159. */
  160. int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
  161. struct fsnotify_group *group, struct inode *inode,
  162. int allow_dups)
  163. {
  164. struct fsnotify_mark *lmark, *last = NULL;
  165. int ret = 0;
  166. mark->flags |= FSNOTIFY_MARK_FLAG_INODE;
  167. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  168. assert_spin_locked(&mark->lock);
  169. spin_lock(&inode->i_lock);
  170. mark->i.inode = inode;
  171. /* is mark the first mark? */
  172. if (hlist_empty(&inode->i_fsnotify_marks)) {
  173. hlist_add_head_rcu(&mark->i.i_list, &inode->i_fsnotify_marks);
  174. goto out;
  175. }
  176. /* should mark be in the middle of the current list? */
  177. hlist_for_each_entry(lmark, &inode->i_fsnotify_marks, i.i_list) {
  178. last = lmark;
  179. if ((lmark->group == group) && !allow_dups) {
  180. ret = -EEXIST;
  181. goto out;
  182. }
  183. if (mark->group->priority < lmark->group->priority)
  184. continue;
  185. if ((mark->group->priority == lmark->group->priority) &&
  186. (mark->group < lmark->group))
  187. continue;
  188. hlist_add_before_rcu(&mark->i.i_list, &lmark->i.i_list);
  189. goto out;
  190. }
  191. BUG_ON(last == NULL);
  192. /* mark should be the last entry. last is the current last entry */
  193. hlist_add_after_rcu(&last->i.i_list, &mark->i.i_list);
  194. out:
  195. fsnotify_recalc_inode_mask_locked(inode);
  196. spin_unlock(&inode->i_lock);
  197. return ret;
  198. }
  199. /**
  200. * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
  201. * @list: list of inodes being unmounted (sb->s_inodes)
  202. *
  203. * Called during unmount with no locks held, so needs to be safe against
  204. * concurrent modifiers. We temporarily drop inode_sb_list_lock and CAN block.
  205. */
  206. void fsnotify_unmount_inodes(struct list_head *list)
  207. {
  208. struct inode *inode, *next_i, *need_iput = NULL;
  209. spin_lock(&inode_sb_list_lock);
  210. list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
  211. struct inode *need_iput_tmp;
  212. /*
  213. * We cannot __iget() an inode in state I_FREEING,
  214. * I_WILL_FREE, or I_NEW which is fine because by that point
  215. * the inode cannot have any associated watches.
  216. */
  217. spin_lock(&inode->i_lock);
  218. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
  219. spin_unlock(&inode->i_lock);
  220. continue;
  221. }
  222. /*
  223. * If i_count is zero, the inode cannot have any watches and
  224. * doing an __iget/iput with MS_ACTIVE clear would actually
  225. * evict all inodes with zero i_count from icache which is
  226. * unnecessarily violent and may in fact be illegal to do.
  227. */
  228. if (!atomic_read(&inode->i_count)) {
  229. spin_unlock(&inode->i_lock);
  230. continue;
  231. }
  232. need_iput_tmp = need_iput;
  233. need_iput = NULL;
  234. /* In case fsnotify_inode_delete() drops a reference. */
  235. if (inode != need_iput_tmp)
  236. __iget(inode);
  237. else
  238. need_iput_tmp = NULL;
  239. spin_unlock(&inode->i_lock);
  240. /* In case the dropping of a reference would nuke next_i. */
  241. if ((&next_i->i_sb_list != list) &&
  242. atomic_read(&next_i->i_count)) {
  243. spin_lock(&next_i->i_lock);
  244. if (!(next_i->i_state & (I_FREEING | I_WILL_FREE))) {
  245. __iget(next_i);
  246. need_iput = next_i;
  247. }
  248. spin_unlock(&next_i->i_lock);
  249. }
  250. /*
  251. * We can safely drop inode_sb_list_lock here because we hold
  252. * references on both inode and next_i. Also no new inodes
  253. * will be added since the umount has begun.
  254. */
  255. spin_unlock(&inode_sb_list_lock);
  256. if (need_iput_tmp)
  257. iput(need_iput_tmp);
  258. /* for each watch, send FS_UNMOUNT and then remove it */
  259. fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  260. fsnotify_inode_delete(inode);
  261. iput(inode);
  262. spin_lock(&inode_sb_list_lock);
  263. }
  264. spin_unlock(&inode_sb_list_lock);
  265. }