blob: cf6b0429a25730f34c38e9b11dd1dcb39992d11e [file] [log] [blame]
Robert Love0eeca282005-07-12 17:06:03 -04001/*
2 * fs/inotify.c - inode-based file event notifications
3 *
4 * Authors:
5 * John McCutchan <ttb@tentacle.dhs.org>
6 * Robert Love <rml@novell.com>
7 *
Amy Griffis2d9048e2006-06-01 13:10:59 -07008 * Kernel API added by: Amy Griffis <amy.griffis@hp.com>
9 *
Robert Love0eeca282005-07-12 17:06:03 -040010 * Copyright (C) 2005 John McCutchan
Amy Griffis2d9048e2006-06-01 13:10:59 -070011 * Copyright 2006 Hewlett-Packard Development Company, L.P.
Robert Love0eeca282005-07-12 17:06:03 -040012 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2, or (at your option) any
16 * later version.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 */
23
24#include <linux/module.h>
25#include <linux/kernel.h>
Robert Love0eeca282005-07-12 17:06:03 -040026#include <linux/spinlock.h>
27#include <linux/idr.h>
28#include <linux/slab.h>
29#include <linux/fs.h>
Al Viro914e2632006-10-18 13:55:46 -040030#include <linux/sched.h>
Robert Love0eeca282005-07-12 17:06:03 -040031#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/writeback.h>
34#include <linux/inotify.h>
Eric Paris90586522009-05-21 17:01:20 -040035#include <linux/fsnotify_backend.h>
Robert Love0eeca282005-07-12 17:06:03 -040036
37static atomic_t inotify_cookie;
38
Robert Love0eeca282005-07-12 17:06:03 -040039/*
40 * Lock ordering:
41 *
42 * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
Ingo Molnarf24075b2006-03-23 03:00:34 -080043 * iprune_mutex (synchronize shrink_icache_memory())
Robert Love0eeca282005-07-12 17:06:03 -040044 * inode_lock (protects the super_block->s_inodes list)
Ingo Molnard4f9af92006-03-23 03:00:30 -080045 * inode->inotify_mutex (protects inode->inotify_watches and watches->i_list)
Amy Griffis2d9048e2006-06-01 13:10:59 -070046 * inotify_handle->mutex (protects inotify_handle and watches->h_list)
47 *
48 * The inode->inotify_mutex and inotify_handle->mutex and held during execution
49 * of a caller's event handler. Thus, the caller must not hold any locks
50 * taken in their event handler while calling any of the published inotify
51 * interfaces.
Robert Love0eeca282005-07-12 17:06:03 -040052 */
53
54/*
Amy Griffis2d9048e2006-06-01 13:10:59 -070055 * Lifetimes of the three main data structures--inotify_handle, inode, and
Robert Love0eeca282005-07-12 17:06:03 -040056 * inotify_watch--are managed by reference count.
57 *
Amy Griffis2d9048e2006-06-01 13:10:59 -070058 * inotify_handle: Lifetime is from inotify_init() to inotify_destroy().
59 * Additional references can bump the count via get_inotify_handle() and drop
60 * the count via put_inotify_handle().
Robert Love0eeca282005-07-12 17:06:03 -040061 *
Amy Griffis2d9048e2006-06-01 13:10:59 -070062 * inotify_watch: for inotify's purposes, lifetime is from inotify_add_watch()
63 * to remove_watch_no_event(). Additional references can bump the count via
64 * get_inotify_watch() and drop the count via put_inotify_watch(). The caller
65 * is reponsible for the final put after receiving IN_IGNORED, or when using
66 * IN_ONESHOT after receiving the first event. Inotify does the final put if
67 * inotify_destroy() is called.
Robert Love0eeca282005-07-12 17:06:03 -040068 *
69 * inode: Pinned so long as the inode is associated with a watch, from
Amy Griffis2d9048e2006-06-01 13:10:59 -070070 * inotify_add_watch() to the final put_inotify_watch().
Robert Love0eeca282005-07-12 17:06:03 -040071 */
72
73/*
Amy Griffis2d9048e2006-06-01 13:10:59 -070074 * struct inotify_handle - represents an inotify instance
Robert Love0eeca282005-07-12 17:06:03 -040075 *
Ingo Molnard4f9af92006-03-23 03:00:30 -080076 * This structure is protected by the mutex 'mutex'.
Robert Love0eeca282005-07-12 17:06:03 -040077 */
Amy Griffis2d9048e2006-06-01 13:10:59 -070078struct inotify_handle {
Robert Love0eeca282005-07-12 17:06:03 -040079 struct idr idr; /* idr mapping wd -> watch */
Ingo Molnard4f9af92006-03-23 03:00:30 -080080 struct mutex mutex; /* protects this bad boy */
Robert Love0eeca282005-07-12 17:06:03 -040081 struct list_head watches; /* list of watches */
82 atomic_t count; /* reference count */
John McCutchanb9c55d22005-08-01 11:00:45 -040083 u32 last_wd; /* the last wd allocated */
Amy Griffis2d9048e2006-06-01 13:10:59 -070084 const struct inotify_operations *in_ops; /* inotify caller operations */
Robert Love0eeca282005-07-12 17:06:03 -040085};
86
Amy Griffis2d9048e2006-06-01 13:10:59 -070087static inline void get_inotify_handle(struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -040088{
Amy Griffis2d9048e2006-06-01 13:10:59 -070089 atomic_inc(&ih->count);
Robert Love0eeca282005-07-12 17:06:03 -040090}
91
Amy Griffis2d9048e2006-06-01 13:10:59 -070092static inline void put_inotify_handle(struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -040093{
Amy Griffis2d9048e2006-06-01 13:10:59 -070094 if (atomic_dec_and_test(&ih->count)) {
95 idr_destroy(&ih->idr);
96 kfree(ih);
Robert Love0eeca282005-07-12 17:06:03 -040097 }
98}
99
Amy Griffis2d9048e2006-06-01 13:10:59 -0700100/**
101 * get_inotify_watch - grab a reference to an inotify_watch
102 * @watch: watch to grab
103 */
104void get_inotify_watch(struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400105{
106 atomic_inc(&watch->count);
107}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700108EXPORT_SYMBOL_GPL(get_inotify_watch);
Robert Love0eeca282005-07-12 17:06:03 -0400109
Al Viro8f7b0ba2008-11-15 01:15:43 +0000110int pin_inotify_watch(struct inotify_watch *watch)
111{
112 struct super_block *sb = watch->inode->i_sb;
Al Virob20bd1a2010-03-22 08:53:19 -0400113 if (atomic_inc_not_zero(&sb->s_active)) {
Al Viro8f7b0ba2008-11-15 01:15:43 +0000114 atomic_inc(&watch->count);
115 return 1;
116 }
Al Viro8f7b0ba2008-11-15 01:15:43 +0000117 return 0;
118}
119
Amy Griffis2d9048e2006-06-01 13:10:59 -0700120/**
Robert Love0eeca282005-07-12 17:06:03 -0400121 * put_inotify_watch - decrements the ref count on a given watch. cleans up
Amy Griffis2d9048e2006-06-01 13:10:59 -0700122 * watch references if the count reaches zero. inotify_watch is freed by
123 * inotify callers via the destroy_watch() op.
124 * @watch: watch to release
Robert Love0eeca282005-07-12 17:06:03 -0400125 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700126void put_inotify_watch(struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400127{
128 if (atomic_dec_and_test(&watch->count)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700129 struct inotify_handle *ih = watch->ih;
130
Robert Love0eeca282005-07-12 17:06:03 -0400131 iput(watch->inode);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700132 ih->in_ops->destroy_watch(watch);
133 put_inotify_handle(ih);
Robert Love0eeca282005-07-12 17:06:03 -0400134 }
135}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700136EXPORT_SYMBOL_GPL(put_inotify_watch);
Robert Love0eeca282005-07-12 17:06:03 -0400137
Al Viro8f7b0ba2008-11-15 01:15:43 +0000138void unpin_inotify_watch(struct inotify_watch *watch)
139{
140 struct super_block *sb = watch->inode->i_sb;
141 put_inotify_watch(watch);
142 deactivate_super(sb);
143}
144
Robert Love0eeca282005-07-12 17:06:03 -0400145/*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700146 * inotify_handle_get_wd - returns the next WD for use by the given handle
Robert Love0eeca282005-07-12 17:06:03 -0400147 *
Amy Griffis2d9048e2006-06-01 13:10:59 -0700148 * Callers must hold ih->mutex. This function can sleep.
Robert Love0eeca282005-07-12 17:06:03 -0400149 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700150static int inotify_handle_get_wd(struct inotify_handle *ih,
151 struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400152{
153 int ret;
154
155 do {
Ingo Molnarf04b30d2009-02-18 14:48:43 -0800156 if (unlikely(!idr_pre_get(&ih->idr, GFP_NOFS)))
Robert Love0eeca282005-07-12 17:06:03 -0400157 return -ENOSPC;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700158 ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd);
Robert Love0eeca282005-07-12 17:06:03 -0400159 } while (ret == -EAGAIN);
160
Amy Griffis2d9048e2006-06-01 13:10:59 -0700161 if (likely(!ret))
162 ih->last_wd = watch->wd;
163
Robert Love0eeca282005-07-12 17:06:03 -0400164 return ret;
165}
166
167/*
Nick Pigginc32ccd82006-03-25 03:07:09 -0800168 * inotify_inode_watched - returns nonzero if there are watches on this inode
169 * and zero otherwise. We call this lockless, we do not care if we race.
170 */
171static inline int inotify_inode_watched(struct inode *inode)
172{
173 return !list_empty(&inode->inotify_watches);
174}
175
176/*
177 * Get child dentry flag into synch with parent inode.
178 * Flag should always be clear for negative dentrys.
179 */
180static void set_dentry_child_flags(struct inode *inode, int watched)
181{
182 struct dentry *alias;
183
184 spin_lock(&dcache_lock);
185 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
186 struct dentry *child;
187
188 list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {
Nick Piggin0d71bd52008-02-06 01:37:29 -0800189 if (!child->d_inode)
Nick Pigginc32ccd82006-03-25 03:07:09 -0800190 continue;
Nick Piggin0d71bd52008-02-06 01:37:29 -0800191
Nick Pigginc32ccd82006-03-25 03:07:09 -0800192 spin_lock(&child->d_lock);
Nick Piggin0d71bd52008-02-06 01:37:29 -0800193 if (watched)
Nick Pigginc32ccd82006-03-25 03:07:09 -0800194 child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
Nick Piggin0d71bd52008-02-06 01:37:29 -0800195 else
196 child->d_flags &=~DCACHE_INOTIFY_PARENT_WATCHED;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800197 spin_unlock(&child->d_lock);
198 }
199 }
200 spin_unlock(&dcache_lock);
201}
202
203/*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700204 * inotify_find_handle - find the watch associated with the given inode and
205 * handle
Robert Love0eeca282005-07-12 17:06:03 -0400206 *
Ingo Molnard4f9af92006-03-23 03:00:30 -0800207 * Callers must hold inode->inotify_mutex.
Robert Love0eeca282005-07-12 17:06:03 -0400208 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700209static struct inotify_watch *inode_find_handle(struct inode *inode,
210 struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -0400211{
212 struct inotify_watch *watch;
213
214 list_for_each_entry(watch, &inode->inotify_watches, i_list) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700215 if (watch->ih == ih)
Robert Love0eeca282005-07-12 17:06:03 -0400216 return watch;
217 }
218
219 return NULL;
220}
221
222/*
Amy Griffis3ca10062006-06-01 13:11:05 -0700223 * remove_watch_no_event - remove watch without the IN_IGNORED event.
Amy Griffis2d9048e2006-06-01 13:10:59 -0700224 *
225 * Callers must hold both inode->inotify_mutex and ih->mutex.
Robert Love0eeca282005-07-12 17:06:03 -0400226 */
227static void remove_watch_no_event(struct inotify_watch *watch,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700228 struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -0400229{
230 list_del(&watch->i_list);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700231 list_del(&watch->h_list);
Robert Love0eeca282005-07-12 17:06:03 -0400232
Nick Pigginc32ccd82006-03-25 03:07:09 -0800233 if (!inotify_inode_watched(watch->inode))
234 set_dentry_child_flags(watch->inode, 0);
235
Amy Griffis2d9048e2006-06-01 13:10:59 -0700236 idr_remove(&ih->idr, watch->wd);
Robert Love0eeca282005-07-12 17:06:03 -0400237}
238
Amy Griffis3ca10062006-06-01 13:11:05 -0700239/**
240 * inotify_remove_watch_locked - Remove a watch from both the handle and the
241 * inode. Sends the IN_IGNORED event signifying that the inode is no longer
242 * watched. May be invoked from a caller's event handler.
243 * @ih: inotify handle associated with watch
244 * @watch: watch to remove
Robert Love0eeca282005-07-12 17:06:03 -0400245 *
Amy Griffis2d9048e2006-06-01 13:10:59 -0700246 * Callers must hold both inode->inotify_mutex and ih->mutex.
Robert Love0eeca282005-07-12 17:06:03 -0400247 */
Amy Griffis3ca10062006-06-01 13:11:05 -0700248void inotify_remove_watch_locked(struct inotify_handle *ih,
249 struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400250{
Amy Griffis2d9048e2006-06-01 13:10:59 -0700251 remove_watch_no_event(watch, ih);
Amy Griffis7c297722006-06-01 13:11:01 -0700252 ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400253}
Amy Griffis3ca10062006-06-01 13:11:05 -0700254EXPORT_SYMBOL_GPL(inotify_remove_watch_locked);
Robert Love0eeca282005-07-12 17:06:03 -0400255
Amy Griffis2d9048e2006-06-01 13:10:59 -0700256/* Kernel API for producing events */
Nick Pigginc32ccd82006-03-25 03:07:09 -0800257
Robert Love0eeca282005-07-12 17:06:03 -0400258/*
Nick Pigginc32ccd82006-03-25 03:07:09 -0800259 * inotify_d_instantiate - instantiate dcache entry for inode
Robert Love0eeca282005-07-12 17:06:03 -0400260 */
Nick Pigginc32ccd82006-03-25 03:07:09 -0800261void inotify_d_instantiate(struct dentry *entry, struct inode *inode)
Robert Love0eeca282005-07-12 17:06:03 -0400262{
Nick Pigginc32ccd82006-03-25 03:07:09 -0800263 struct dentry *parent;
264
265 if (!inode)
266 return;
267
Nick Pigginc32ccd82006-03-25 03:07:09 -0800268 spin_lock(&entry->d_lock);
269 parent = entry->d_parent;
Arnd Bergmann091e8812006-04-10 22:54:31 -0700270 if (parent->d_inode && inotify_inode_watched(parent->d_inode))
Nick Pigginc32ccd82006-03-25 03:07:09 -0800271 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
272 spin_unlock(&entry->d_lock);
Robert Love0eeca282005-07-12 17:06:03 -0400273}
274
Nick Pigginc32ccd82006-03-25 03:07:09 -0800275/*
276 * inotify_d_move - dcache entry has been moved
277 */
278void inotify_d_move(struct dentry *entry)
279{
280 struct dentry *parent;
281
282 parent = entry->d_parent;
283 if (inotify_inode_watched(parent->d_inode))
284 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
285 else
286 entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
287}
Robert Love0eeca282005-07-12 17:06:03 -0400288
289/**
290 * inotify_inode_queue_event - queue an event to all watches on this inode
291 * @inode: inode event is originating from
292 * @mask: event mask describing this event
293 * @cookie: cookie for synchronization, or zero
294 * @name: filename, if any
Amy Griffis7c297722006-06-01 13:11:01 -0700295 * @n_inode: inode associated with name
Robert Love0eeca282005-07-12 17:06:03 -0400296 */
297void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
Amy Griffis7c297722006-06-01 13:11:01 -0700298 const char *name, struct inode *n_inode)
Robert Love0eeca282005-07-12 17:06:03 -0400299{
300 struct inotify_watch *watch, *next;
301
302 if (!inotify_inode_watched(inode))
303 return;
304
Ingo Molnard4f9af92006-03-23 03:00:30 -0800305 mutex_lock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400306 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
307 u32 watch_mask = watch->mask;
308 if (watch_mask & mask) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700309 struct inotify_handle *ih= watch->ih;
310 mutex_lock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400311 if (watch_mask & IN_ONESHOT)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700312 remove_watch_no_event(watch, ih);
Amy Griffis7c297722006-06-01 13:11:01 -0700313 ih->in_ops->handle_event(watch, watch->wd, mask, cookie,
314 name, n_inode);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700315 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400316 }
317 }
Ingo Molnard4f9af92006-03-23 03:00:30 -0800318 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400319}
320EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
321
322/**
323 * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
324 * @dentry: the dentry in question, we queue against this dentry's parent
325 * @mask: event mask describing this event
326 * @cookie: cookie for synchronization, or zero
327 * @name: filename, if any
328 */
329void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
330 u32 cookie, const char *name)
331{
332 struct dentry *parent;
333 struct inode *inode;
334
Nick Pigginc32ccd82006-03-25 03:07:09 -0800335 if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))
John McCutchan820249b2005-09-06 15:16:38 -0700336 return;
337
Robert Love0eeca282005-07-12 17:06:03 -0400338 spin_lock(&dentry->d_lock);
339 parent = dentry->d_parent;
340 inode = parent->d_inode;
341
342 if (inotify_inode_watched(inode)) {
343 dget(parent);
344 spin_unlock(&dentry->d_lock);
Amy Griffis7c297722006-06-01 13:11:01 -0700345 inotify_inode_queue_event(inode, mask, cookie, name,
346 dentry->d_inode);
Robert Love0eeca282005-07-12 17:06:03 -0400347 dput(parent);
348 } else
349 spin_unlock(&dentry->d_lock);
350}
351EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
352
353/**
354 * inotify_get_cookie - return a unique cookie for use in synchronizing events.
355 */
356u32 inotify_get_cookie(void)
357{
358 return atomic_inc_return(&inotify_cookie);
359}
360EXPORT_SYMBOL_GPL(inotify_get_cookie);
361
362/**
363 * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
364 * @list: list of inodes being unmounted (sb->s_inodes)
365 *
366 * Called with inode_lock held, protecting the unmounting super block's list
Ingo Molnarf24075b2006-03-23 03:00:34 -0800367 * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
Robert Love0eeca282005-07-12 17:06:03 -0400368 * We temporarily drop inode_lock, however, and CAN block.
369 */
370void inotify_unmount_inodes(struct list_head *list)
371{
372 struct inode *inode, *next_i, *need_iput = NULL;
373
374 list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
375 struct inotify_watch *watch, *next_w;
376 struct inode *need_iput_tmp;
377 struct list_head *watches;
378
379 /*
Al Viroa4ffdde2010-06-02 17:38:30 -0400380 * We cannot __iget() an inode in state I_FREEING,
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700381 * I_WILL_FREE, or I_NEW which is fine because by that point
382 * the inode cannot have any associated watches.
383 */
Al Viroa4ffdde2010-06-02 17:38:30 -0400384 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW))
Nick Pigginaabb8fd2009-03-11 13:17:36 -0700385 continue;
386
387 /*
Robert Love0eeca282005-07-12 17:06:03 -0400388 * If i_count is zero, the inode cannot have any watches and
389 * doing an __iget/iput with MS_ACTIVE clear would actually
390 * evict all inodes with zero i_count from icache which is
391 * unnecessarily violent and may in fact be illegal to do.
392 */
393 if (!atomic_read(&inode->i_count))
394 continue;
395
Robert Love0eeca282005-07-12 17:06:03 -0400396 need_iput_tmp = need_iput;
397 need_iput = NULL;
Amy Griffis3ca10062006-06-01 13:11:05 -0700398 /* In case inotify_remove_watch_locked() drops a reference. */
Robert Love0eeca282005-07-12 17:06:03 -0400399 if (inode != need_iput_tmp)
400 __iget(inode);
401 else
402 need_iput_tmp = NULL;
403 /* In case the dropping of a reference would nuke next_i. */
404 if ((&next_i->i_sb_list != list) &&
405 atomic_read(&next_i->i_count) &&
Al Viroa4ffdde2010-06-02 17:38:30 -0400406 !(next_i->i_state & (I_FREEING|I_WILL_FREE))) {
Robert Love0eeca282005-07-12 17:06:03 -0400407 __iget(next_i);
408 need_iput = next_i;
409 }
410
411 /*
412 * We can safely drop inode_lock here because we hold
413 * references on both inode and next_i. Also no new inodes
414 * will be added since the umount has begun. Finally,
Ingo Molnarf24075b2006-03-23 03:00:34 -0800415 * iprune_mutex keeps shrink_icache_memory() away.
Robert Love0eeca282005-07-12 17:06:03 -0400416 */
417 spin_unlock(&inode_lock);
418
419 if (need_iput_tmp)
420 iput(need_iput_tmp);
421
422 /* for each watch, send IN_UNMOUNT and then remove it */
Ingo Molnard4f9af92006-03-23 03:00:30 -0800423 mutex_lock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400424 watches = &inode->inotify_watches;
425 list_for_each_entry_safe(watch, next_w, watches, i_list) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700426 struct inotify_handle *ih= watch->ih;
Dmitri Monakhov6ee5a392008-12-09 13:14:26 -0800427 get_inotify_watch(watch);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700428 mutex_lock(&ih->mutex);
429 ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,
Amy Griffis7c297722006-06-01 13:11:01 -0700430 NULL, NULL);
Amy Griffis3ca10062006-06-01 13:11:05 -0700431 inotify_remove_watch_locked(ih, watch);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700432 mutex_unlock(&ih->mutex);
Dmitri Monakhov6ee5a392008-12-09 13:14:26 -0800433 put_inotify_watch(watch);
Robert Love0eeca282005-07-12 17:06:03 -0400434 }
Ingo Molnard4f9af92006-03-23 03:00:30 -0800435 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400436 iput(inode);
437
438 spin_lock(&inode_lock);
439 }
440}
441EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
442
443/**
444 * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
445 * @inode: inode that is about to be removed
446 */
447void inotify_inode_is_dead(struct inode *inode)
448{
449 struct inotify_watch *watch, *next;
450
Ingo Molnard4f9af92006-03-23 03:00:30 -0800451 mutex_lock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400452 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700453 struct inotify_handle *ih = watch->ih;
454 mutex_lock(&ih->mutex);
Amy Griffis3ca10062006-06-01 13:11:05 -0700455 inotify_remove_watch_locked(ih, watch);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700456 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400457 }
Ingo Molnard4f9af92006-03-23 03:00:30 -0800458 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400459}
460EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
461
Amy Griffis2d9048e2006-06-01 13:10:59 -0700462/* Kernel Consumer API */
Robert Love0eeca282005-07-12 17:06:03 -0400463
Amy Griffis2d9048e2006-06-01 13:10:59 -0700464/**
465 * inotify_init - allocate and initialize an inotify instance
466 * @ops: caller's inotify operations
467 */
468struct inotify_handle *inotify_init(const struct inotify_operations *ops)
Robert Love0eeca282005-07-12 17:06:03 -0400469{
Amy Griffis2d9048e2006-06-01 13:10:59 -0700470 struct inotify_handle *ih;
Robert Love0eeca282005-07-12 17:06:03 -0400471
Amy Griffis2d9048e2006-06-01 13:10:59 -0700472 ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);
473 if (unlikely(!ih))
474 return ERR_PTR(-ENOMEM);
Robert Love0eeca282005-07-12 17:06:03 -0400475
Amy Griffis2d9048e2006-06-01 13:10:59 -0700476 idr_init(&ih->idr);
477 INIT_LIST_HEAD(&ih->watches);
478 mutex_init(&ih->mutex);
479 ih->last_wd = 0;
480 ih->in_ops = ops;
481 atomic_set(&ih->count, 0);
482 get_inotify_handle(ih);
483
484 return ih;
Robert Love0eeca282005-07-12 17:06:03 -0400485}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700486EXPORT_SYMBOL_GPL(inotify_init);
Robert Love0eeca282005-07-12 17:06:03 -0400487
Amy Griffis2d9048e2006-06-01 13:10:59 -0700488/**
Amy Griffisa9dc9712006-06-01 13:11:03 -0700489 * inotify_init_watch - initialize an inotify watch
490 * @watch: watch to initialize
491 */
492void inotify_init_watch(struct inotify_watch *watch)
493{
494 INIT_LIST_HEAD(&watch->h_list);
495 INIT_LIST_HEAD(&watch->i_list);
496 atomic_set(&watch->count, 0);
497 get_inotify_watch(watch); /* initial get */
498}
499EXPORT_SYMBOL_GPL(inotify_init_watch);
500
Al Viro8f7b0ba2008-11-15 01:15:43 +0000501/*
502 * Watch removals suck violently. To kick the watch out we need (in this
503 * order) inode->inotify_mutex and ih->mutex. That's fine if we have
504 * a hold on inode; however, for all other cases we need to make damn sure
505 * we don't race with umount. We can *NOT* just grab a reference to a
506 * watch - inotify_unmount_inodes() will happily sail past it and we'll end
507 * with reference to inode potentially outliving its superblock. Ideally
508 * we just want to grab an active reference to superblock if we can; that
509 * will make sure we won't go into inotify_umount_inodes() until we are
510 * done. Cleanup is just deactivate_super(). However, that leaves a messy
511 * case - what if we *are* racing with umount() and active references to
512 * superblock can't be acquired anymore? We can bump ->s_count, grab
Al Viro1712ac82010-03-22 15:22:31 -0400513 * ->s_umount, which will wait until the superblock is shut down and the
514 * watch in question is pining for fjords.
Al Viro8f7b0ba2008-11-15 01:15:43 +0000515 *
516 * And yes, this is far beyond mere "not very pretty"; so's the entire
517 * concept of inotify to start with.
518 */
519
520/**
521 * pin_to_kill - pin the watch down for removal
522 * @ih: inotify handle
523 * @watch: watch to kill
524 *
525 * Called with ih->mutex held, drops it. Possible return values:
526 * 0 - nothing to do, it has died
527 * 1 - remove it, drop the reference and deactivate_super()
Al Viro8f7b0ba2008-11-15 01:15:43 +0000528 */
529static int pin_to_kill(struct inotify_handle *ih, struct inotify_watch *watch)
530{
531 struct super_block *sb = watch->inode->i_sb;
Al Viro8f7b0ba2008-11-15 01:15:43 +0000532
Al Virob20bd1a2010-03-22 08:53:19 -0400533 if (atomic_inc_not_zero(&sb->s_active)) {
Al Viro8f7b0ba2008-11-15 01:15:43 +0000534 get_inotify_watch(watch);
535 mutex_unlock(&ih->mutex);
536 return 1; /* the best outcome */
537 }
Al Virob20bd1a2010-03-22 08:53:19 -0400538 spin_lock(&sb_lock);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000539 sb->s_count++;
540 spin_unlock(&sb_lock);
541 mutex_unlock(&ih->mutex); /* can't grab ->s_umount under it */
542 down_read(&sb->s_umount);
Al Viro1712ac82010-03-22 15:22:31 -0400543 /* fs is already shut down; the watch is dead */
544 drop_super(sb);
545 return 0;
Al Viro8f7b0ba2008-11-15 01:15:43 +0000546}
547
Al Viro1712ac82010-03-22 15:22:31 -0400548static void unpin_and_kill(struct inotify_watch *watch)
Al Viro8f7b0ba2008-11-15 01:15:43 +0000549{
550 struct super_block *sb = watch->inode->i_sb;
551 put_inotify_watch(watch);
Al Viro1712ac82010-03-22 15:22:31 -0400552 deactivate_super(sb);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000553}
554
Amy Griffisa9dc9712006-06-01 13:11:03 -0700555/**
Amy Griffis2d9048e2006-06-01 13:10:59 -0700556 * inotify_destroy - clean up and destroy an inotify instance
557 * @ih: inotify handle
558 */
559void inotify_destroy(struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -0400560{
Robert Love0eeca282005-07-12 17:06:03 -0400561 /*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700562 * Destroy all of the watches for this handle. Unfortunately, not very
Robert Love0eeca282005-07-12 17:06:03 -0400563 * pretty. We cannot do a simple iteration over the list, because we
564 * do not know the inode until we iterate to the watch. But we need to
Amy Griffis2d9048e2006-06-01 13:10:59 -0700565 * hold inode->inotify_mutex before ih->mutex. The following works.
Al Viro8f7b0ba2008-11-15 01:15:43 +0000566 *
567 * AV: it had to become even uglier to start working ;-/
Robert Love0eeca282005-07-12 17:06:03 -0400568 */
569 while (1) {
570 struct inotify_watch *watch;
571 struct list_head *watches;
Al Viro8f7b0ba2008-11-15 01:15:43 +0000572 struct super_block *sb;
Robert Love0eeca282005-07-12 17:06:03 -0400573 struct inode *inode;
574
Amy Griffis2d9048e2006-06-01 13:10:59 -0700575 mutex_lock(&ih->mutex);
576 watches = &ih->watches;
Robert Love0eeca282005-07-12 17:06:03 -0400577 if (list_empty(watches)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700578 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400579 break;
580 }
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700581 watch = list_first_entry(watches, struct inotify_watch, h_list);
Al Viro8f7b0ba2008-11-15 01:15:43 +0000582 sb = watch->inode->i_sb;
Al Viro1712ac82010-03-22 15:22:31 -0400583 if (!pin_to_kill(ih, watch))
Al Viro8f7b0ba2008-11-15 01:15:43 +0000584 continue;
Robert Love0eeca282005-07-12 17:06:03 -0400585
586 inode = watch->inode;
Ingo Molnard4f9af92006-03-23 03:00:30 -0800587 mutex_lock(&inode->inotify_mutex);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700588 mutex_lock(&ih->mutex);
Amy Griffis66055a42006-05-20 15:00:06 -0700589
590 /* make sure we didn't race with another list removal */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700591 if (likely(idr_find(&ih->idr, watch->wd))) {
592 remove_watch_no_event(watch, ih);
593 put_inotify_watch(watch);
594 }
Amy Griffis66055a42006-05-20 15:00:06 -0700595
Amy Griffis2d9048e2006-06-01 13:10:59 -0700596 mutex_unlock(&ih->mutex);
Ingo Molnard4f9af92006-03-23 03:00:30 -0800597 mutex_unlock(&inode->inotify_mutex);
Al Viro1712ac82010-03-22 15:22:31 -0400598 unpin_and_kill(watch);
Robert Love0eeca282005-07-12 17:06:03 -0400599 }
600
Amy Griffis2d9048e2006-06-01 13:10:59 -0700601 /* free this handle: the put matching the get in inotify_init() */
602 put_inotify_handle(ih);
Robert Love0eeca282005-07-12 17:06:03 -0400603}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700604EXPORT_SYMBOL_GPL(inotify_destroy);
Robert Love0eeca282005-07-12 17:06:03 -0400605
Amy Griffis2d9048e2006-06-01 13:10:59 -0700606/**
Amy Griffisa9dc9712006-06-01 13:11:03 -0700607 * inotify_find_watch - find an existing watch for an (ih,inode) pair
608 * @ih: inotify handle
609 * @inode: inode to watch
610 * @watchp: pointer to existing inotify_watch
611 *
612 * Caller must pin given inode (via nameidata).
613 */
614s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode,
615 struct inotify_watch **watchp)
616{
617 struct inotify_watch *old;
618 int ret = -ENOENT;
619
620 mutex_lock(&inode->inotify_mutex);
621 mutex_lock(&ih->mutex);
622
623 old = inode_find_handle(inode, ih);
624 if (unlikely(old)) {
625 get_inotify_watch(old); /* caller must put watch */
626 *watchp = old;
627 ret = old->wd;
628 }
629
630 mutex_unlock(&ih->mutex);
631 mutex_unlock(&inode->inotify_mutex);
632
633 return ret;
634}
635EXPORT_SYMBOL_GPL(inotify_find_watch);
636
637/**
Amy Griffis2d9048e2006-06-01 13:10:59 -0700638 * inotify_find_update_watch - find and update the mask of an existing watch
639 * @ih: inotify handle
640 * @inode: inode's watch to update
641 * @mask: mask of events to watch
642 *
643 * Caller must pin given inode (via nameidata).
644 */
645s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,
646 u32 mask)
647{
648 struct inotify_watch *old;
649 int mask_add = 0;
650 int ret;
651
652 if (mask & IN_MASK_ADD)
653 mask_add = 1;
654
655 /* don't allow invalid bits: we don't want flags set */
656 mask &= IN_ALL_EVENTS | IN_ONESHOT;
657 if (unlikely(!mask))
658 return -EINVAL;
659
660 mutex_lock(&inode->inotify_mutex);
661 mutex_lock(&ih->mutex);
662
663 /*
664 * Handle the case of re-adding a watch on an (inode,ih) pair that we
665 * are already watching. We just update the mask and return its wd.
666 */
667 old = inode_find_handle(inode, ih);
668 if (unlikely(!old)) {
669 ret = -ENOENT;
670 goto out;
671 }
672
673 if (mask_add)
674 old->mask |= mask;
675 else
676 old->mask = mask;
677 ret = old->wd;
678out:
679 mutex_unlock(&ih->mutex);
680 mutex_unlock(&inode->inotify_mutex);
681 return ret;
682}
683EXPORT_SYMBOL_GPL(inotify_find_update_watch);
684
685/**
686 * inotify_add_watch - add a watch to an inotify instance
687 * @ih: inotify handle
688 * @watch: caller allocated watch structure
689 * @inode: inode to watch
690 * @mask: mask of events to watch
691 *
692 * Caller must pin given inode (via nameidata).
693 * Caller must ensure it only calls inotify_add_watch() once per watch.
694 * Calls inotify_handle_get_wd() so may sleep.
695 */
696s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch,
697 struct inode *inode, u32 mask)
698{
699 int ret = 0;
Nick Piggind599e362008-02-06 01:37:28 -0800700 int newly_watched;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700701
702 /* don't allow invalid bits: we don't want flags set */
703 mask &= IN_ALL_EVENTS | IN_ONESHOT;
704 if (unlikely(!mask))
705 return -EINVAL;
706 watch->mask = mask;
707
708 mutex_lock(&inode->inotify_mutex);
709 mutex_lock(&ih->mutex);
710
711 /* Initialize a new watch */
712 ret = inotify_handle_get_wd(ih, watch);
713 if (unlikely(ret))
714 goto out;
715 ret = watch->wd;
716
Amy Griffis2d9048e2006-06-01 13:10:59 -0700717 /* save a reference to handle and bump the count to make it official */
718 get_inotify_handle(ih);
719 watch->ih = ih;
720
721 /*
722 * Save a reference to the inode and bump the ref count to make it
723 * official. We hold a reference to nameidata, which makes this safe.
724 */
725 watch->inode = igrab(inode);
726
Amy Griffis2d9048e2006-06-01 13:10:59 -0700727 /* Add the watch to the handle's and the inode's list */
Nick Piggind599e362008-02-06 01:37:28 -0800728 newly_watched = !inotify_inode_watched(inode);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700729 list_add(&watch->h_list, &ih->watches);
730 list_add(&watch->i_list, &inode->inotify_watches);
Nick Piggind599e362008-02-06 01:37:28 -0800731 /*
732 * Set child flags _after_ adding the watch, so there is no race
733 * windows where newly instantiated children could miss their parent's
734 * watched flag.
735 */
736 if (newly_watched)
737 set_dentry_child_flags(inode, 1);
738
Amy Griffis2d9048e2006-06-01 13:10:59 -0700739out:
740 mutex_unlock(&ih->mutex);
741 mutex_unlock(&inode->inotify_mutex);
742 return ret;
743}
744EXPORT_SYMBOL_GPL(inotify_add_watch);
745
746/**
Al Virob9efe8a2007-06-07 12:21:44 -0400747 * inotify_clone_watch - put the watch next to existing one
748 * @old: already installed watch
749 * @new: new watch
750 *
751 * Caller must hold the inotify_mutex of inode we are dealing with;
752 * it is expected to remove the old watch before unlocking the inode.
753 */
754s32 inotify_clone_watch(struct inotify_watch *old, struct inotify_watch *new)
755{
756 struct inotify_handle *ih = old->ih;
757 int ret = 0;
758
759 new->mask = old->mask;
760 new->ih = ih;
761
762 mutex_lock(&ih->mutex);
763
764 /* Initialize a new watch */
765 ret = inotify_handle_get_wd(ih, new);
766 if (unlikely(ret))
767 goto out;
768 ret = new->wd;
769
770 get_inotify_handle(ih);
771
772 new->inode = igrab(old->inode);
773
774 list_add(&new->h_list, &ih->watches);
775 list_add(&new->i_list, &old->inode->inotify_watches);
776out:
777 mutex_unlock(&ih->mutex);
778 return ret;
779}
780
Al Viro455434d2007-06-07 12:22:59 -0400781void inotify_evict_watch(struct inotify_watch *watch)
782{
783 get_inotify_watch(watch);
784 mutex_lock(&watch->ih->mutex);
785 inotify_remove_watch_locked(watch->ih, watch);
786 mutex_unlock(&watch->ih->mutex);
787}
788
Al Virob9efe8a2007-06-07 12:21:44 -0400789/**
Amy Griffis2d9048e2006-06-01 13:10:59 -0700790 * inotify_rm_wd - remove a watch from an inotify instance
791 * @ih: inotify handle
792 * @wd: watch descriptor to remove
Robert Love0eeca282005-07-12 17:06:03 -0400793 *
794 * Can sleep.
795 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700796int inotify_rm_wd(struct inotify_handle *ih, u32 wd)
Robert Love0eeca282005-07-12 17:06:03 -0400797{
798 struct inotify_watch *watch;
Al Viro8f7b0ba2008-11-15 01:15:43 +0000799 struct super_block *sb;
Robert Love0eeca282005-07-12 17:06:03 -0400800 struct inode *inode;
801
Amy Griffis2d9048e2006-06-01 13:10:59 -0700802 mutex_lock(&ih->mutex);
803 watch = idr_find(&ih->idr, wd);
Robert Love0eeca282005-07-12 17:06:03 -0400804 if (unlikely(!watch)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700805 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400806 return -EINVAL;
807 }
Al Viro8f7b0ba2008-11-15 01:15:43 +0000808 sb = watch->inode->i_sb;
Al Viro1712ac82010-03-22 15:22:31 -0400809 if (!pin_to_kill(ih, watch))
Al Viro8f7b0ba2008-11-15 01:15:43 +0000810 return 0;
811
Robert Love0eeca282005-07-12 17:06:03 -0400812 inode = watch->inode;
Robert Love0eeca282005-07-12 17:06:03 -0400813
Ingo Molnard4f9af92006-03-23 03:00:30 -0800814 mutex_lock(&inode->inotify_mutex);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700815 mutex_lock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400816
817 /* make sure that we did not race */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700818 if (likely(idr_find(&ih->idr, wd) == watch))
Amy Griffis3ca10062006-06-01 13:11:05 -0700819 inotify_remove_watch_locked(ih, watch);
Robert Love0eeca282005-07-12 17:06:03 -0400820
Amy Griffis2d9048e2006-06-01 13:10:59 -0700821 mutex_unlock(&ih->mutex);
Ingo Molnard4f9af92006-03-23 03:00:30 -0800822 mutex_unlock(&inode->inotify_mutex);
Al Viro1712ac82010-03-22 15:22:31 -0400823 unpin_and_kill(watch);
Robert Love0eeca282005-07-12 17:06:03 -0400824
825 return 0;
826}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700827EXPORT_SYMBOL_GPL(inotify_rm_wd);
Robert Love0eeca282005-07-12 17:06:03 -0400828
Amy Griffisa9dc9712006-06-01 13:11:03 -0700829/**
830 * inotify_rm_watch - remove a watch from an inotify instance
831 * @ih: inotify handle
832 * @watch: watch to remove
833 *
834 * Can sleep.
835 */
836int inotify_rm_watch(struct inotify_handle *ih,
837 struct inotify_watch *watch)
838{
839 return inotify_rm_wd(ih, watch->wd);
840}
841EXPORT_SYMBOL_GPL(inotify_rm_watch);
842
Robert Love0eeca282005-07-12 17:06:03 -0400843/*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700844 * inotify_setup - core initialization function
Robert Love0eeca282005-07-12 17:06:03 -0400845 */
Robert Loveb6807162005-07-25 15:07:13 -0400846static int __init inotify_setup(void)
Robert Love0eeca282005-07-12 17:06:03 -0400847{
Eric Paris90586522009-05-21 17:01:20 -0400848 BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
849 BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
850 BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
851 BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
852 BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
853 BUILD_BUG_ON(IN_OPEN != FS_OPEN);
854 BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
855 BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
856 BUILD_BUG_ON(IN_CREATE != FS_CREATE);
857 BUILD_BUG_ON(IN_DELETE != FS_DELETE);
858 BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
859 BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
860 BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
861
862 BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
863 BUILD_BUG_ON(IN_ISDIR != FS_IN_ISDIR);
864 BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
865 BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
866
Robert Love0eeca282005-07-12 17:06:03 -0400867 atomic_set(&inotify_cookie, 0);
868
Robert Love0eeca282005-07-12 17:06:03 -0400869 return 0;
870}
871
Robert Loveb6807162005-07-25 15:07:13 -0400872module_init(inotify_setup);