inode_mark.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. int cmp;
  167. mark->flags |= FSNOTIFY_MARK_FLAG_INODE;
  168. BUG_ON(!mutex_is_locked(&group->mark_mutex));
  169. assert_spin_locked(&mark->lock);
  170. spin_lock(&inode->i_lock);
  171. mark->i.inode = inode;
  172. /* is mark the first mark? */
  173. if (hlist_empty(&inode->i_fsnotify_marks)) {
  174. hlist_add_head_rcu(&mark->i.i_list, &inode->i_fsnotify_marks);
  175. goto out;
  176. }
  177. /* should mark be in the middle of the current list? */
  178. hlist_for_each_entry(lmark, &inode->i_fsnotify_marks, i.i_list) {
  179. last = lmark;
  180. if ((lmark->group == group) && !allow_dups) {
  181. ret = -EEXIST;
  182. goto out;
  183. }
  184. cmp = fsnotify_compare_groups(lmark->group, mark->group);
  185. if (cmp < 0)
  186. continue;
  187. hlist_add_before_rcu(&mark->i.i_list, &lmark->i.i_list);
  188. goto out;
  189. }
  190. BUG_ON(last == NULL);
  191. /* mark should be the last entry. last is the current last entry */
  192. hlist_add_behind_rcu(&mark->i.i_list, &last->i.i_list);
  193. out:
  194. fsnotify_recalc_inode_mask_locked(inode);
  195. spin_unlock(&inode->i_lock);
  196. return ret;
  197. }
  198. /**
  199. * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
  200. * @list: list of inodes being unmounted (sb->s_inodes)
  201. *
  202. * Called during unmount with no locks held, so needs to be safe against
  203. * concurrent modifiers. We temporarily drop inode_sb_list_lock and CAN block.
  204. */
  205. void fsnotify_unmount_inodes(struct list_head *list)
  206. {
  207. struct inode *inode, *next_i, *need_iput = NULL;
  208. spin_lock(&inode_sb_list_lock);
  209. list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
  210. struct inode *need_iput_tmp;
  211. /*
  212. * We cannot __iget() an inode in state I_FREEING,
  213. * I_WILL_FREE, or I_NEW which is fine because by that point
  214. * the inode cannot have any associated watches.
  215. */
  216. spin_lock(&inode->i_lock);
  217. if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
  218. spin_unlock(&inode->i_lock);
  219. continue;
  220. }
  221. /*
  222. * If i_count is zero, the inode cannot have any watches and
  223. * doing an __iget/iput with MS_ACTIVE clear would actually
  224. * evict all inodes with zero i_count from icache which is
  225. * unnecessarily violent and may in fact be illegal to do.
  226. */
  227. if (!atomic_read(&inode->i_count)) {
  228. spin_unlock(&inode->i_lock);
  229. continue;
  230. }
  231. need_iput_tmp = need_iput;
  232. need_iput = NULL;
  233. /* In case fsnotify_inode_delete() drops a reference. */
  234. if (inode != need_iput_tmp)
  235. __iget(inode);
  236. else
  237. need_iput_tmp = NULL;
  238. spin_unlock(&inode->i_lock);
  239. /* In case the dropping of a reference would nuke next_i. */
  240. while (&next_i->i_sb_list != list) {
  241. spin_lock(&next_i->i_lock);
  242. if (!(next_i->i_state & (I_FREEING | I_WILL_FREE)) &&
  243. atomic_read(&next_i->i_count)) {
  244. __iget(next_i);
  245. need_iput = next_i;
  246. spin_unlock(&next_i->i_lock);
  247. break;
  248. }
  249. spin_unlock(&next_i->i_lock);
  250. next_i = list_entry(next_i->i_sb_list.next,
  251. struct inode, i_sb_list);
  252. }
  253. /*
  254. * We can safely drop inode_sb_list_lock here because either
  255. * we actually hold references on both inode and next_i or
  256. * end of list. Also no new inodes will be added since the
  257. * umount has begun.
  258. */
  259. spin_unlock(&inode_sb_list_lock);
  260. if (need_iput_tmp)
  261. iput(need_iput_tmp);
  262. /* for each watch, send FS_UNMOUNT and then remove it */
  263. fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
  264. fsnotify_inode_delete(inode);
  265. iput(inode);
  266. spin_lock(&inode_sb_list_lock);
  267. }
  268. spin_unlock(&inode_sb_list_lock);
  269. }