080b6d8b9973d59fdbcecc60a06a9bab6975585f
[linux-2.6-block.git] / fs / notify / inode_mark.c
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
19 #include <linux/fs.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/spinlock.h>
25
26 #include <linux/atomic.h>
27
28 #include <linux/fsnotify_backend.h>
29 #include "fsnotify.h"
30
31 #include "../internal.h"
32
33 void fsnotify_recalc_inode_mask(struct inode *inode)
34 {
35         fsnotify_recalc_mask(inode->i_fsnotify_marks);
36 }
37
38 /*
39  * Given a group clear all of the inode marks associated with that group.
40  */
41 void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
42 {
43         fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_OBJ_TYPE_INODE);
44 }
45
46 /*
47  * given a group and inode, find the mark associated with that combination.
48  * if found take a reference to that mark and return it, else return NULL
49  */
50 struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group,
51                                                struct inode *inode)
52 {
53         return fsnotify_find_mark(inode->i_fsnotify_marks, group);
54 }
55
56 /**
57  * fsnotify_unmount_inodes - an sb is unmounting.  handle any watched inodes.
58  * @sb: superblock being unmounted.
59  *
60  * Called during unmount with no locks held, so needs to be safe against
61  * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
62  */
63 void fsnotify_unmount_inodes(struct super_block *sb)
64 {
65         struct inode *inode, *iput_inode = NULL;
66
67         spin_lock(&sb->s_inode_list_lock);
68         list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
69                 /*
70                  * We cannot __iget() an inode in state I_FREEING,
71                  * I_WILL_FREE, or I_NEW which is fine because by that point
72                  * the inode cannot have any associated watches.
73                  */
74                 spin_lock(&inode->i_lock);
75                 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
76                         spin_unlock(&inode->i_lock);
77                         continue;
78                 }
79
80                 /*
81                  * If i_count is zero, the inode cannot have any watches and
82                  * doing an __iget/iput with MS_ACTIVE clear would actually
83                  * evict all inodes with zero i_count from icache which is
84                  * unnecessarily violent and may in fact be illegal to do.
85                  */
86                 if (!atomic_read(&inode->i_count)) {
87                         spin_unlock(&inode->i_lock);
88                         continue;
89                 }
90
91                 __iget(inode);
92                 spin_unlock(&inode->i_lock);
93                 spin_unlock(&sb->s_inode_list_lock);
94
95                 if (iput_inode)
96                         iput(iput_inode);
97
98                 /* for each watch, send FS_UNMOUNT and then remove it */
99                 fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
100
101                 fsnotify_inode_delete(inode);
102
103                 iput_inode = inode;
104
105                 spin_lock(&sb->s_inode_list_lock);
106         }
107         spin_unlock(&sb->s_inode_list_lock);
108
109         if (iput_inode)
110                 iput(iput_inode);
111 }