blob: a3645249f7ecfa4cbdae8434bb87b47ffd02dbb1 [file] [log] [blame]
Eric Paris3be25f42009-05-21 17:01:26 -04001/*
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
Eric Paris3be25f42009-05-21 17:01:26 -040019#include <linux/fs.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/mutex.h>
Eric Paris3be25f42009-05-21 17:01:26 -040024#include <linux/spinlock.h>
25
Arun Sharma600634972011-07-26 16:09:06 -070026#include <linux/atomic.h>
Eric Paris3be25f42009-05-21 17:01:26 -040027
28#include <linux/fsnotify_backend.h>
29#include "fsnotify.h"
30
Dave Chinner55fa6092011-03-22 22:23:40 +110031#include "../internal.h"
32
Eric Paris3be25f42009-05-21 17:01:26 -040033/*
Eric Paris3be25f42009-05-21 17:01:26 -040034 * Recalculate the inode->i_fsnotify_mask, or the mask of all FS_* event types
35 * any notifier is interested in hearing for this inode.
36 */
37void fsnotify_recalc_inode_mask(struct inode *inode)
38{
39 spin_lock(&inode->i_lock);
Jan Kara0809ab62014-12-12 16:58:36 -080040 inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -040041 spin_unlock(&inode->i_lock);
Eric Parisc28f7e52009-05-21 17:01:29 -040042
43 __fsnotify_update_child_dentry_flags(inode);
Eric Paris3be25f42009-05-21 17:01:26 -040044}
45
Eric Paris5444e292009-12-17 21:24:27 -050046void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark)
Eric Paris3be25f42009-05-21 17:01:26 -040047{
Jan Kara0809ab62014-12-12 16:58:36 -080048 struct inode *inode = mark->inode;
Eric Paris3be25f42009-05-21 17:01:26 -040049
Lino Sanfilippo986ab092011-06-14 17:29:50 +020050 BUG_ON(!mutex_is_locked(&mark->group->mark_mutex));
Eric Paris5444e292009-12-17 21:24:27 -050051 assert_spin_locked(&mark->lock);
Eric Paris3be25f42009-05-21 17:01:26 -040052
Eric Paris3be25f42009-05-21 17:01:26 -040053 spin_lock(&inode->i_lock);
54
Jan Kara0809ab62014-12-12 16:58:36 -080055 hlist_del_init_rcu(&mark->obj_list);
56 mark->inode = NULL;
Eric Paris3be25f42009-05-21 17:01:26 -040057
58 /*
Eric Parise61ce862009-12-17 21:24:24 -050059 * this mark is now off the inode->i_fsnotify_marks list and we
Eric Paris3be25f42009-05-21 17:01:26 -040060 * hold the inode->i_lock, so this is the perfect time to update the
61 * inode->i_fsnotify_mask
62 */
Jan Kara0809ab62014-12-12 16:58:36 -080063 inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -040064 spin_unlock(&inode->i_lock);
Eric Paris3be25f42009-05-21 17:01:26 -040065}
66
67/*
Eric Paris4d926042009-12-17 21:24:34 -050068 * Given a group clear all of the inode marks associated with that group.
69 */
70void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
71{
72 fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_INODE);
73}
74
75/*
Eric Paris3be25f42009-05-21 17:01:26 -040076 * given a group and inode, find the mark associated with that combination.
77 * if found take a reference to that mark and return it, else return NULL
78 */
Eric Paris5444e292009-12-17 21:24:27 -050079struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group,
80 struct inode *inode)
Eric Paris3be25f42009-05-21 17:01:26 -040081{
Andreas Gruenbacher35566082009-12-17 21:24:25 -050082 struct fsnotify_mark *mark;
83
84 spin_lock(&inode->i_lock);
Jan Kara0809ab62014-12-12 16:58:36 -080085 mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Andreas Gruenbacher35566082009-12-17 21:24:25 -050086 spin_unlock(&inode->i_lock);
87
88 return mark;
Eric Paris3be25f42009-05-21 17:01:26 -040089}
90
91/*
Eric Paris90b1e7a2009-12-17 21:24:33 -050092 * If we are setting a mark mask on an inode mark we should pin the inode
93 * in memory.
Eric Paris3be25f42009-05-21 17:01:26 -040094 */
Eric Paris90b1e7a2009-12-17 21:24:33 -050095void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *mark,
96 __u32 mask)
Eric Paris3be25f42009-05-21 17:01:26 -040097{
Eric Paris90b1e7a2009-12-17 21:24:33 -050098 struct inode *inode;
99
100 assert_spin_locked(&mark->lock);
101
102 if (mask &&
Jan Kara0809ab62014-12-12 16:58:36 -0800103 mark->inode &&
Eric Paris90b1e7a2009-12-17 21:24:33 -0500104 !(mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) {
105 mark->flags |= FSNOTIFY_MARK_FLAG_OBJECT_PINNED;
Jan Kara0809ab62014-12-12 16:58:36 -0800106 inode = igrab(mark->inode);
Eric Paris90b1e7a2009-12-17 21:24:33 -0500107 /*
108 * we shouldn't be able to get here if the inode wasn't
109 * already safely held in memory. But bug in case it
110 * ever is wrong.
111 */
112 BUG_ON(!inode);
113 }
114}
115
116/*
Eric Paris0c6532e2010-07-28 10:18:38 -0400117 * Attach an initialized mark to a given inode.
Eric Paris3be25f42009-05-21 17:01:26 -0400118 * These marks may be used for the fsnotify backend to determine which
Eric Paris0c6532e2010-07-28 10:18:38 -0400119 * event types should be delivered to which group and for which inodes. These
Eric Paris6ad2d4e2010-10-28 17:21:56 -0400120 * marks are ordered according to priority, highest number first, and then by
121 * the group's location in memory.
Eric Paris3be25f42009-05-21 17:01:26 -0400122 */
Eric Paris5444e292009-12-17 21:24:27 -0500123int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
124 struct fsnotify_group *group, struct inode *inode,
125 int allow_dups)
Eric Paris3be25f42009-05-21 17:01:26 -0400126{
Jan Kara0809ab62014-12-12 16:58:36 -0800127 int ret;
Eric Paris3be25f42009-05-21 17:01:26 -0400128
Eric Paris700307a2010-07-28 10:18:38 -0400129 mark->flags |= FSNOTIFY_MARK_FLAG_INODE;
Eric Paris1ef5f132009-05-21 17:01:54 -0400130
Lino Sanfilippo986ab092011-06-14 17:29:50 +0200131 BUG_ON(!mutex_is_locked(&group->mark_mutex));
Eric Paris5444e292009-12-17 21:24:27 -0500132 assert_spin_locked(&mark->lock);
Eric Paris71314852009-12-17 21:24:23 -0500133
Eric Paris3be25f42009-05-21 17:01:26 -0400134 spin_lock(&inode->i_lock);
Jan Kara0809ab62014-12-12 16:58:36 -0800135 mark->inode = inode;
136 ret = fsnotify_add_mark_list(&inode->i_fsnotify_marks, mark,
137 allow_dups);
138 inode->i_fsnotify_mask = fsnotify_recalc_mask(&inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -0400139 spin_unlock(&inode->i_lock);
Eric Paris3be25f42009-05-21 17:01:26 -0400140
141 return ret;
142}
Eric Paris164bc612009-05-21 17:01:58 -0400143
144/**
145 * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
Dave Chinner74278da2015-03-04 12:37:22 -0500146 * @sb: superblock being unmounted.
Eric Paris164bc612009-05-21 17:01:58 -0400147 *
Dave Chinner55fa6092011-03-22 22:23:40 +1100148 * Called during unmount with no locks held, so needs to be safe against
Dave Chinner74278da2015-03-04 12:37:22 -0500149 * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
Eric Paris164bc612009-05-21 17:01:58 -0400150 */
Dave Chinner74278da2015-03-04 12:37:22 -0500151void fsnotify_unmount_inodes(struct super_block *sb)
Eric Paris164bc612009-05-21 17:01:58 -0400152{
Jan Kara57168632016-12-12 16:08:41 +0100153 struct inode *inode, *iput_inode = NULL;
Eric Paris164bc612009-05-21 17:01:58 -0400154
Dave Chinner74278da2015-03-04 12:37:22 -0500155 spin_lock(&sb->s_inode_list_lock);
Jan Kara57168632016-12-12 16:08:41 +0100156 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Eric Paris164bc612009-05-21 17:01:58 -0400157 /*
Al Viroa4ffdde2010-06-02 17:38:30 -0400158 * We cannot __iget() an inode in state I_FREEING,
Eric Paris164bc612009-05-21 17:01:58 -0400159 * I_WILL_FREE, or I_NEW which is fine because by that point
160 * the inode cannot have any associated watches.
161 */
Dave Chinner250df6e2011-03-22 22:23:36 +1100162 spin_lock(&inode->i_lock);
163 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
164 spin_unlock(&inode->i_lock);
Eric Paris164bc612009-05-21 17:01:58 -0400165 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100166 }
Eric Paris164bc612009-05-21 17:01:58 -0400167
168 /*
169 * If i_count is zero, the inode cannot have any watches and
170 * doing an __iget/iput with MS_ACTIVE clear would actually
171 * evict all inodes with zero i_count from icache which is
172 * unnecessarily violent and may in fact be illegal to do.
173 */
Dave Chinner250df6e2011-03-22 22:23:36 +1100174 if (!atomic_read(&inode->i_count)) {
175 spin_unlock(&inode->i_lock);
Eric Paris164bc612009-05-21 17:01:58 -0400176 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +1100177 }
Eric Paris164bc612009-05-21 17:01:58 -0400178
Jan Kara57168632016-12-12 16:08:41 +0100179 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +1100180 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -0500181 spin_unlock(&sb->s_inode_list_lock);
Eric Paris164bc612009-05-21 17:01:58 -0400182
Jan Kara57168632016-12-12 16:08:41 +0100183 if (iput_inode)
184 iput(iput_inode);
Eric Paris164bc612009-05-21 17:01:58 -0400185
186 /* for each watch, send FS_UNMOUNT and then remove it */
187 fsnotify(inode, FS_UNMOUNT, inode, FSNOTIFY_EVENT_INODE, NULL, 0);
188
189 fsnotify_inode_delete(inode);
190
Jan Kara57168632016-12-12 16:08:41 +0100191 iput_inode = inode;
Eric Paris164bc612009-05-21 17:01:58 -0400192
Dave Chinner74278da2015-03-04 12:37:22 -0500193 spin_lock(&sb->s_inode_list_lock);
Eric Paris164bc612009-05-21 17:01:58 -0400194 }
Dave Chinner74278da2015-03-04 12:37:22 -0500195 spin_unlock(&sb->s_inode_list_lock);
Jan Kara57168632016-12-12 16:08:41 +0100196
197 if (iput_inode)
198 iput(iput_inode);
Eric Paris164bc612009-05-21 17:01:58 -0400199}