blob: b9370316727e18342f8ea34b7ebd5c8d907e67b3 [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 -040033void fsnotify_recalc_inode_mask(struct inode *inode)
34{
Jan Karaa2426772017-03-15 09:16:27 +010035 fsnotify_recalc_mask(inode->i_fsnotify_marks);
Eric Paris3be25f42009-05-21 17:01:26 -040036}
37
Eric Paris3be25f42009-05-21 17:01:26 -040038/*
Eric Paris4d926042009-12-17 21:24:34 -050039 * Given a group clear all of the inode marks associated with that group.
40 */
41void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
42{
Jan Kara86ffe242017-03-14 14:29:35 +010043 fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_OBJ_TYPE_INODE);
Eric Paris4d926042009-12-17 21:24:34 -050044}
45
46/*
Eric Paris3be25f42009-05-21 17:01:26 -040047 * 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 */
Eric Paris5444e292009-12-17 21:24:27 -050050struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group,
51 struct inode *inode)
Eric Paris3be25f42009-05-21 17:01:26 -040052{
Jan Kara08991e82017-02-01 09:21:58 +010053 return fsnotify_find_mark(&inode->i_fsnotify_marks, group);
Eric Paris3be25f42009-05-21 17:01:26 -040054}
55
Eric Paris164bc612009-05-21 17:01:58 -040056/**
57 * fsnotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
Dave Chinner74278da2015-03-04 12:37:22 -050058 * @sb: superblock being unmounted.
Eric Paris164bc612009-05-21 17:01:58 -040059 *
Dave Chinner55fa6092011-03-22 22:23:40 +110060 * Called during unmount with no locks held, so needs to be safe against
Dave Chinner74278da2015-03-04 12:37:22 -050061 * concurrent modifiers. We temporarily drop sb->s_inode_list_lock and CAN block.
Eric Paris164bc612009-05-21 17:01:58 -040062 */
Dave Chinner74278da2015-03-04 12:37:22 -050063void fsnotify_unmount_inodes(struct super_block *sb)
Eric Paris164bc612009-05-21 17:01:58 -040064{
Jan Kara57168632016-12-12 16:08:41 +010065 struct inode *inode, *iput_inode = NULL;
Eric Paris164bc612009-05-21 17:01:58 -040066
Dave Chinner74278da2015-03-04 12:37:22 -050067 spin_lock(&sb->s_inode_list_lock);
Jan Kara57168632016-12-12 16:08:41 +010068 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
Eric Paris164bc612009-05-21 17:01:58 -040069 /*
Al Viroa4ffdde2010-06-02 17:38:30 -040070 * We cannot __iget() an inode in state I_FREEING,
Eric Paris164bc612009-05-21 17:01:58 -040071 * I_WILL_FREE, or I_NEW which is fine because by that point
72 * the inode cannot have any associated watches.
73 */
Dave Chinner250df6e2011-03-22 22:23:36 +110074 spin_lock(&inode->i_lock);
75 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) {
76 spin_unlock(&inode->i_lock);
Eric Paris164bc612009-05-21 17:01:58 -040077 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +110078 }
Eric Paris164bc612009-05-21 17:01:58 -040079
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 */
Dave Chinner250df6e2011-03-22 22:23:36 +110086 if (!atomic_read(&inode->i_count)) {
87 spin_unlock(&inode->i_lock);
Eric Paris164bc612009-05-21 17:01:58 -040088 continue;
Dave Chinner250df6e2011-03-22 22:23:36 +110089 }
Eric Paris164bc612009-05-21 17:01:58 -040090
Jan Kara57168632016-12-12 16:08:41 +010091 __iget(inode);
Dave Chinner250df6e2011-03-22 22:23:36 +110092 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -050093 spin_unlock(&sb->s_inode_list_lock);
Eric Paris164bc612009-05-21 17:01:58 -040094
Jan Kara57168632016-12-12 16:08:41 +010095 if (iput_inode)
96 iput(iput_inode);
Eric Paris164bc612009-05-21 17:01:58 -040097
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
Jan Kara57168632016-12-12 16:08:41 +0100103 iput_inode = inode;
Eric Paris164bc612009-05-21 17:01:58 -0400104
Dave Chinner74278da2015-03-04 12:37:22 -0500105 spin_lock(&sb->s_inode_list_lock);
Eric Paris164bc612009-05-21 17:01:58 -0400106 }
Dave Chinner74278da2015-03-04 12:37:22 -0500107 spin_unlock(&sb->s_inode_list_lock);
Jan Kara57168632016-12-12 16:08:41 +0100108
109 if (iput_inode)
110 iput(iput_inode);
Eric Paris164bc612009-05-21 17:01:58 -0400111}