blob: 690e72595e6e6048addba645cf179cb418208f50 [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>
Robert Love0eeca282005-07-12 17:06:03 -040035
36static atomic_t inotify_cookie;
37
Robert Love0eeca282005-07-12 17:06:03 -040038/*
39 * Lock ordering:
40 *
41 * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
Ingo Molnarf24075b2006-03-23 03:00:34 -080042 * iprune_mutex (synchronize shrink_icache_memory())
Robert Love0eeca282005-07-12 17:06:03 -040043 * inode_lock (protects the super_block->s_inodes list)
Ingo Molnard4f9af92006-03-23 03:00:30 -080044 * inode->inotify_mutex (protects inode->inotify_watches and watches->i_list)
Amy Griffis2d9048e2006-06-01 13:10:59 -070045 * inotify_handle->mutex (protects inotify_handle and watches->h_list)
46 *
47 * The inode->inotify_mutex and inotify_handle->mutex and held during execution
48 * of a caller's event handler. Thus, the caller must not hold any locks
49 * taken in their event handler while calling any of the published inotify
50 * interfaces.
Robert Love0eeca282005-07-12 17:06:03 -040051 */
52
53/*
Amy Griffis2d9048e2006-06-01 13:10:59 -070054 * Lifetimes of the three main data structures--inotify_handle, inode, and
Robert Love0eeca282005-07-12 17:06:03 -040055 * inotify_watch--are managed by reference count.
56 *
Amy Griffis2d9048e2006-06-01 13:10:59 -070057 * inotify_handle: Lifetime is from inotify_init() to inotify_destroy().
58 * Additional references can bump the count via get_inotify_handle() and drop
59 * the count via put_inotify_handle().
Robert Love0eeca282005-07-12 17:06:03 -040060 *
Amy Griffis2d9048e2006-06-01 13:10:59 -070061 * inotify_watch: for inotify's purposes, lifetime is from inotify_add_watch()
62 * to remove_watch_no_event(). Additional references can bump the count via
63 * get_inotify_watch() and drop the count via put_inotify_watch(). The caller
64 * is reponsible for the final put after receiving IN_IGNORED, or when using
65 * IN_ONESHOT after receiving the first event. Inotify does the final put if
66 * inotify_destroy() is called.
Robert Love0eeca282005-07-12 17:06:03 -040067 *
68 * inode: Pinned so long as the inode is associated with a watch, from
Amy Griffis2d9048e2006-06-01 13:10:59 -070069 * inotify_add_watch() to the final put_inotify_watch().
Robert Love0eeca282005-07-12 17:06:03 -040070 */
71
72/*
Amy Griffis2d9048e2006-06-01 13:10:59 -070073 * struct inotify_handle - represents an inotify instance
Robert Love0eeca282005-07-12 17:06:03 -040074 *
Ingo Molnard4f9af92006-03-23 03:00:30 -080075 * This structure is protected by the mutex 'mutex'.
Robert Love0eeca282005-07-12 17:06:03 -040076 */
Amy Griffis2d9048e2006-06-01 13:10:59 -070077struct inotify_handle {
Robert Love0eeca282005-07-12 17:06:03 -040078 struct idr idr; /* idr mapping wd -> watch */
Ingo Molnard4f9af92006-03-23 03:00:30 -080079 struct mutex mutex; /* protects this bad boy */
Robert Love0eeca282005-07-12 17:06:03 -040080 struct list_head watches; /* list of watches */
81 atomic_t count; /* reference count */
John McCutchanb9c55d22005-08-01 11:00:45 -040082 u32 last_wd; /* the last wd allocated */
Amy Griffis2d9048e2006-06-01 13:10:59 -070083 const struct inotify_operations *in_ops; /* inotify caller operations */
Robert Love0eeca282005-07-12 17:06:03 -040084};
85
Amy Griffis2d9048e2006-06-01 13:10:59 -070086static inline void get_inotify_handle(struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -040087{
Amy Griffis2d9048e2006-06-01 13:10:59 -070088 atomic_inc(&ih->count);
Robert Love0eeca282005-07-12 17:06:03 -040089}
90
Amy Griffis2d9048e2006-06-01 13:10:59 -070091static inline void put_inotify_handle(struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -040092{
Amy Griffis2d9048e2006-06-01 13:10:59 -070093 if (atomic_dec_and_test(&ih->count)) {
94 idr_destroy(&ih->idr);
95 kfree(ih);
Robert Love0eeca282005-07-12 17:06:03 -040096 }
97}
98
Amy Griffis2d9048e2006-06-01 13:10:59 -070099/**
100 * get_inotify_watch - grab a reference to an inotify_watch
101 * @watch: watch to grab
102 */
103void get_inotify_watch(struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400104{
105 atomic_inc(&watch->count);
106}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700107EXPORT_SYMBOL_GPL(get_inotify_watch);
Robert Love0eeca282005-07-12 17:06:03 -0400108
Amy Griffis2d9048e2006-06-01 13:10:59 -0700109/**
Robert Love0eeca282005-07-12 17:06:03 -0400110 * put_inotify_watch - decrements the ref count on a given watch. cleans up
Amy Griffis2d9048e2006-06-01 13:10:59 -0700111 * watch references if the count reaches zero. inotify_watch is freed by
112 * inotify callers via the destroy_watch() op.
113 * @watch: watch to release
Robert Love0eeca282005-07-12 17:06:03 -0400114 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700115void put_inotify_watch(struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400116{
117 if (atomic_dec_and_test(&watch->count)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700118 struct inotify_handle *ih = watch->ih;
119
Robert Love0eeca282005-07-12 17:06:03 -0400120 iput(watch->inode);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700121 ih->in_ops->destroy_watch(watch);
122 put_inotify_handle(ih);
Robert Love0eeca282005-07-12 17:06:03 -0400123 }
124}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700125EXPORT_SYMBOL_GPL(put_inotify_watch);
Robert Love0eeca282005-07-12 17:06:03 -0400126
127/*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700128 * inotify_handle_get_wd - returns the next WD for use by the given handle
Robert Love0eeca282005-07-12 17:06:03 -0400129 *
Amy Griffis2d9048e2006-06-01 13:10:59 -0700130 * Callers must hold ih->mutex. This function can sleep.
Robert Love0eeca282005-07-12 17:06:03 -0400131 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700132static int inotify_handle_get_wd(struct inotify_handle *ih,
133 struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400134{
135 int ret;
136
137 do {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700138 if (unlikely(!idr_pre_get(&ih->idr, GFP_KERNEL)))
Robert Love0eeca282005-07-12 17:06:03 -0400139 return -ENOSPC;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700140 ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd);
Robert Love0eeca282005-07-12 17:06:03 -0400141 } while (ret == -EAGAIN);
142
Amy Griffis2d9048e2006-06-01 13:10:59 -0700143 if (likely(!ret))
144 ih->last_wd = watch->wd;
145
Robert Love0eeca282005-07-12 17:06:03 -0400146 return ret;
147}
148
149/*
Nick Pigginc32ccd82006-03-25 03:07:09 -0800150 * inotify_inode_watched - returns nonzero if there are watches on this inode
151 * and zero otherwise. We call this lockless, we do not care if we race.
152 */
153static inline int inotify_inode_watched(struct inode *inode)
154{
155 return !list_empty(&inode->inotify_watches);
156}
157
158/*
159 * Get child dentry flag into synch with parent inode.
160 * Flag should always be clear for negative dentrys.
161 */
162static void set_dentry_child_flags(struct inode *inode, int watched)
163{
164 struct dentry *alias;
165
166 spin_lock(&dcache_lock);
167 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
168 struct dentry *child;
169
170 list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {
Nick Piggin0d71bd52008-02-06 01:37:29 -0800171 if (!child->d_inode)
Nick Pigginc32ccd82006-03-25 03:07:09 -0800172 continue;
Nick Piggin0d71bd52008-02-06 01:37:29 -0800173
Nick Pigginc32ccd82006-03-25 03:07:09 -0800174 spin_lock(&child->d_lock);
Nick Piggin0d71bd52008-02-06 01:37:29 -0800175 if (watched)
Nick Pigginc32ccd82006-03-25 03:07:09 -0800176 child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
Nick Piggin0d71bd52008-02-06 01:37:29 -0800177 else
178 child->d_flags &=~DCACHE_INOTIFY_PARENT_WATCHED;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800179 spin_unlock(&child->d_lock);
180 }
181 }
182 spin_unlock(&dcache_lock);
183}
184
185/*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700186 * inotify_find_handle - find the watch associated with the given inode and
187 * handle
Robert Love0eeca282005-07-12 17:06:03 -0400188 *
Ingo Molnard4f9af92006-03-23 03:00:30 -0800189 * Callers must hold inode->inotify_mutex.
Robert Love0eeca282005-07-12 17:06:03 -0400190 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700191static struct inotify_watch *inode_find_handle(struct inode *inode,
192 struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -0400193{
194 struct inotify_watch *watch;
195
196 list_for_each_entry(watch, &inode->inotify_watches, i_list) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700197 if (watch->ih == ih)
Robert Love0eeca282005-07-12 17:06:03 -0400198 return watch;
199 }
200
201 return NULL;
202}
203
204/*
Amy Griffis3ca10062006-06-01 13:11:05 -0700205 * remove_watch_no_event - remove watch without the IN_IGNORED event.
Amy Griffis2d9048e2006-06-01 13:10:59 -0700206 *
207 * Callers must hold both inode->inotify_mutex and ih->mutex.
Robert Love0eeca282005-07-12 17:06:03 -0400208 */
209static void remove_watch_no_event(struct inotify_watch *watch,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700210 struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -0400211{
212 list_del(&watch->i_list);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700213 list_del(&watch->h_list);
Robert Love0eeca282005-07-12 17:06:03 -0400214
Nick Pigginc32ccd82006-03-25 03:07:09 -0800215 if (!inotify_inode_watched(watch->inode))
216 set_dentry_child_flags(watch->inode, 0);
217
Amy Griffis2d9048e2006-06-01 13:10:59 -0700218 idr_remove(&ih->idr, watch->wd);
Robert Love0eeca282005-07-12 17:06:03 -0400219}
220
Amy Griffis3ca10062006-06-01 13:11:05 -0700221/**
222 * inotify_remove_watch_locked - Remove a watch from both the handle and the
223 * inode. Sends the IN_IGNORED event signifying that the inode is no longer
224 * watched. May be invoked from a caller's event handler.
225 * @ih: inotify handle associated with watch
226 * @watch: watch to remove
Robert Love0eeca282005-07-12 17:06:03 -0400227 *
Amy Griffis2d9048e2006-06-01 13:10:59 -0700228 * Callers must hold both inode->inotify_mutex and ih->mutex.
Robert Love0eeca282005-07-12 17:06:03 -0400229 */
Amy Griffis3ca10062006-06-01 13:11:05 -0700230void inotify_remove_watch_locked(struct inotify_handle *ih,
231 struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400232{
Amy Griffis2d9048e2006-06-01 13:10:59 -0700233 remove_watch_no_event(watch, ih);
Amy Griffis7c297722006-06-01 13:11:01 -0700234 ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400235}
Amy Griffis3ca10062006-06-01 13:11:05 -0700236EXPORT_SYMBOL_GPL(inotify_remove_watch_locked);
Robert Love0eeca282005-07-12 17:06:03 -0400237
Amy Griffis2d9048e2006-06-01 13:10:59 -0700238/* Kernel API for producing events */
Nick Pigginc32ccd82006-03-25 03:07:09 -0800239
Robert Love0eeca282005-07-12 17:06:03 -0400240/*
Nick Pigginc32ccd82006-03-25 03:07:09 -0800241 * inotify_d_instantiate - instantiate dcache entry for inode
Robert Love0eeca282005-07-12 17:06:03 -0400242 */
Nick Pigginc32ccd82006-03-25 03:07:09 -0800243void inotify_d_instantiate(struct dentry *entry, struct inode *inode)
Robert Love0eeca282005-07-12 17:06:03 -0400244{
Nick Pigginc32ccd82006-03-25 03:07:09 -0800245 struct dentry *parent;
246
247 if (!inode)
248 return;
249
Nick Pigginc32ccd82006-03-25 03:07:09 -0800250 spin_lock(&entry->d_lock);
251 parent = entry->d_parent;
Arnd Bergmann091e8812006-04-10 22:54:31 -0700252 if (parent->d_inode && inotify_inode_watched(parent->d_inode))
Nick Pigginc32ccd82006-03-25 03:07:09 -0800253 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
254 spin_unlock(&entry->d_lock);
Robert Love0eeca282005-07-12 17:06:03 -0400255}
256
Nick Pigginc32ccd82006-03-25 03:07:09 -0800257/*
258 * inotify_d_move - dcache entry has been moved
259 */
260void inotify_d_move(struct dentry *entry)
261{
262 struct dentry *parent;
263
264 parent = entry->d_parent;
265 if (inotify_inode_watched(parent->d_inode))
266 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
267 else
268 entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
269}
Robert Love0eeca282005-07-12 17:06:03 -0400270
271/**
272 * inotify_inode_queue_event - queue an event to all watches on this inode
273 * @inode: inode event is originating from
274 * @mask: event mask describing this event
275 * @cookie: cookie for synchronization, or zero
276 * @name: filename, if any
Amy Griffis7c297722006-06-01 13:11:01 -0700277 * @n_inode: inode associated with name
Robert Love0eeca282005-07-12 17:06:03 -0400278 */
279void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
Amy Griffis7c297722006-06-01 13:11:01 -0700280 const char *name, struct inode *n_inode)
Robert Love0eeca282005-07-12 17:06:03 -0400281{
282 struct inotify_watch *watch, *next;
283
284 if (!inotify_inode_watched(inode))
285 return;
286
Ingo Molnard4f9af92006-03-23 03:00:30 -0800287 mutex_lock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400288 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
289 u32 watch_mask = watch->mask;
290 if (watch_mask & mask) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700291 struct inotify_handle *ih= watch->ih;
292 mutex_lock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400293 if (watch_mask & IN_ONESHOT)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700294 remove_watch_no_event(watch, ih);
Amy Griffis7c297722006-06-01 13:11:01 -0700295 ih->in_ops->handle_event(watch, watch->wd, mask, cookie,
296 name, n_inode);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700297 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400298 }
299 }
Ingo Molnard4f9af92006-03-23 03:00:30 -0800300 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400301}
302EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
303
304/**
305 * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
306 * @dentry: the dentry in question, we queue against this dentry's parent
307 * @mask: event mask describing this event
308 * @cookie: cookie for synchronization, or zero
309 * @name: filename, if any
310 */
311void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
312 u32 cookie, const char *name)
313{
314 struct dentry *parent;
315 struct inode *inode;
316
Nick Pigginc32ccd82006-03-25 03:07:09 -0800317 if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))
John McCutchan820249b2005-09-06 15:16:38 -0700318 return;
319
Robert Love0eeca282005-07-12 17:06:03 -0400320 spin_lock(&dentry->d_lock);
321 parent = dentry->d_parent;
322 inode = parent->d_inode;
323
324 if (inotify_inode_watched(inode)) {
325 dget(parent);
326 spin_unlock(&dentry->d_lock);
Amy Griffis7c297722006-06-01 13:11:01 -0700327 inotify_inode_queue_event(inode, mask, cookie, name,
328 dentry->d_inode);
Robert Love0eeca282005-07-12 17:06:03 -0400329 dput(parent);
330 } else
331 spin_unlock(&dentry->d_lock);
332}
333EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
334
335/**
336 * inotify_get_cookie - return a unique cookie for use in synchronizing events.
337 */
338u32 inotify_get_cookie(void)
339{
340 return atomic_inc_return(&inotify_cookie);
341}
342EXPORT_SYMBOL_GPL(inotify_get_cookie);
343
344/**
345 * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
346 * @list: list of inodes being unmounted (sb->s_inodes)
347 *
348 * Called with inode_lock held, protecting the unmounting super block's list
Ingo Molnarf24075b2006-03-23 03:00:34 -0800349 * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
Robert Love0eeca282005-07-12 17:06:03 -0400350 * We temporarily drop inode_lock, however, and CAN block.
351 */
352void inotify_unmount_inodes(struct list_head *list)
353{
354 struct inode *inode, *next_i, *need_iput = NULL;
355
356 list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
357 struct inotify_watch *watch, *next_w;
358 struct inode *need_iput_tmp;
359 struct list_head *watches;
360
361 /*
362 * If i_count is zero, the inode cannot have any watches and
363 * doing an __iget/iput with MS_ACTIVE clear would actually
364 * evict all inodes with zero i_count from icache which is
365 * unnecessarily violent and may in fact be illegal to do.
366 */
367 if (!atomic_read(&inode->i_count))
368 continue;
369
370 /*
371 * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or
372 * I_WILL_FREE which is fine because by that point the inode
373 * cannot have any associated watches.
374 */
375 if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE))
376 continue;
377
378 need_iput_tmp = need_iput;
379 need_iput = NULL;
Amy Griffis3ca10062006-06-01 13:11:05 -0700380 /* In case inotify_remove_watch_locked() drops a reference. */
Robert Love0eeca282005-07-12 17:06:03 -0400381 if (inode != need_iput_tmp)
382 __iget(inode);
383 else
384 need_iput_tmp = NULL;
385 /* In case the dropping of a reference would nuke next_i. */
386 if ((&next_i->i_sb_list != list) &&
387 atomic_read(&next_i->i_count) &&
388 !(next_i->i_state & (I_CLEAR | I_FREEING |
389 I_WILL_FREE))) {
390 __iget(next_i);
391 need_iput = next_i;
392 }
393
394 /*
395 * We can safely drop inode_lock here because we hold
396 * references on both inode and next_i. Also no new inodes
397 * will be added since the umount has begun. Finally,
Ingo Molnarf24075b2006-03-23 03:00:34 -0800398 * iprune_mutex keeps shrink_icache_memory() away.
Robert Love0eeca282005-07-12 17:06:03 -0400399 */
400 spin_unlock(&inode_lock);
401
402 if (need_iput_tmp)
403 iput(need_iput_tmp);
404
405 /* for each watch, send IN_UNMOUNT and then remove it */
Ingo Molnard4f9af92006-03-23 03:00:30 -0800406 mutex_lock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400407 watches = &inode->inotify_watches;
408 list_for_each_entry_safe(watch, next_w, watches, i_list) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700409 struct inotify_handle *ih= watch->ih;
410 mutex_lock(&ih->mutex);
411 ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,
Amy Griffis7c297722006-06-01 13:11:01 -0700412 NULL, NULL);
Amy Griffis3ca10062006-06-01 13:11:05 -0700413 inotify_remove_watch_locked(ih, watch);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700414 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400415 }
Ingo Molnard4f9af92006-03-23 03:00:30 -0800416 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400417 iput(inode);
418
419 spin_lock(&inode_lock);
420 }
421}
422EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
423
424/**
425 * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
426 * @inode: inode that is about to be removed
427 */
428void inotify_inode_is_dead(struct inode *inode)
429{
430 struct inotify_watch *watch, *next;
431
Ingo Molnard4f9af92006-03-23 03:00:30 -0800432 mutex_lock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400433 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700434 struct inotify_handle *ih = watch->ih;
435 mutex_lock(&ih->mutex);
Amy Griffis3ca10062006-06-01 13:11:05 -0700436 inotify_remove_watch_locked(ih, watch);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700437 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400438 }
Ingo Molnard4f9af92006-03-23 03:00:30 -0800439 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400440}
441EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
442
Amy Griffis2d9048e2006-06-01 13:10:59 -0700443/* Kernel Consumer API */
Robert Love0eeca282005-07-12 17:06:03 -0400444
Amy Griffis2d9048e2006-06-01 13:10:59 -0700445/**
446 * inotify_init - allocate and initialize an inotify instance
447 * @ops: caller's inotify operations
448 */
449struct inotify_handle *inotify_init(const struct inotify_operations *ops)
Robert Love0eeca282005-07-12 17:06:03 -0400450{
Amy Griffis2d9048e2006-06-01 13:10:59 -0700451 struct inotify_handle *ih;
Robert Love0eeca282005-07-12 17:06:03 -0400452
Amy Griffis2d9048e2006-06-01 13:10:59 -0700453 ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);
454 if (unlikely(!ih))
455 return ERR_PTR(-ENOMEM);
Robert Love0eeca282005-07-12 17:06:03 -0400456
Amy Griffis2d9048e2006-06-01 13:10:59 -0700457 idr_init(&ih->idr);
458 INIT_LIST_HEAD(&ih->watches);
459 mutex_init(&ih->mutex);
460 ih->last_wd = 0;
461 ih->in_ops = ops;
462 atomic_set(&ih->count, 0);
463 get_inotify_handle(ih);
464
465 return ih;
Robert Love0eeca282005-07-12 17:06:03 -0400466}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700467EXPORT_SYMBOL_GPL(inotify_init);
Robert Love0eeca282005-07-12 17:06:03 -0400468
Amy Griffis2d9048e2006-06-01 13:10:59 -0700469/**
Amy Griffisa9dc9712006-06-01 13:11:03 -0700470 * inotify_init_watch - initialize an inotify watch
471 * @watch: watch to initialize
472 */
473void inotify_init_watch(struct inotify_watch *watch)
474{
475 INIT_LIST_HEAD(&watch->h_list);
476 INIT_LIST_HEAD(&watch->i_list);
477 atomic_set(&watch->count, 0);
478 get_inotify_watch(watch); /* initial get */
479}
480EXPORT_SYMBOL_GPL(inotify_init_watch);
481
482/**
Amy Griffis2d9048e2006-06-01 13:10:59 -0700483 * inotify_destroy - clean up and destroy an inotify instance
484 * @ih: inotify handle
485 */
486void inotify_destroy(struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -0400487{
Robert Love0eeca282005-07-12 17:06:03 -0400488 /*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700489 * Destroy all of the watches for this handle. Unfortunately, not very
Robert Love0eeca282005-07-12 17:06:03 -0400490 * pretty. We cannot do a simple iteration over the list, because we
491 * do not know the inode until we iterate to the watch. But we need to
Amy Griffis2d9048e2006-06-01 13:10:59 -0700492 * hold inode->inotify_mutex before ih->mutex. The following works.
Robert Love0eeca282005-07-12 17:06:03 -0400493 */
494 while (1) {
495 struct inotify_watch *watch;
496 struct list_head *watches;
497 struct inode *inode;
498
Amy Griffis2d9048e2006-06-01 13:10:59 -0700499 mutex_lock(&ih->mutex);
500 watches = &ih->watches;
Robert Love0eeca282005-07-12 17:06:03 -0400501 if (list_empty(watches)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700502 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400503 break;
504 }
Pavel Emelianovb5e61812007-05-08 00:30:19 -0700505 watch = list_first_entry(watches, struct inotify_watch, h_list);
Robert Love0eeca282005-07-12 17:06:03 -0400506 get_inotify_watch(watch);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700507 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400508
509 inode = watch->inode;
Ingo Molnard4f9af92006-03-23 03:00:30 -0800510 mutex_lock(&inode->inotify_mutex);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700511 mutex_lock(&ih->mutex);
Amy Griffis66055a42006-05-20 15:00:06 -0700512
513 /* make sure we didn't race with another list removal */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700514 if (likely(idr_find(&ih->idr, watch->wd))) {
515 remove_watch_no_event(watch, ih);
516 put_inotify_watch(watch);
517 }
Amy Griffis66055a42006-05-20 15:00:06 -0700518
Amy Griffis2d9048e2006-06-01 13:10:59 -0700519 mutex_unlock(&ih->mutex);
Ingo Molnard4f9af92006-03-23 03:00:30 -0800520 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400521 put_inotify_watch(watch);
522 }
523
Amy Griffis2d9048e2006-06-01 13:10:59 -0700524 /* free this handle: the put matching the get in inotify_init() */
525 put_inotify_handle(ih);
Robert Love0eeca282005-07-12 17:06:03 -0400526}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700527EXPORT_SYMBOL_GPL(inotify_destroy);
Robert Love0eeca282005-07-12 17:06:03 -0400528
Amy Griffis2d9048e2006-06-01 13:10:59 -0700529/**
Amy Griffisa9dc9712006-06-01 13:11:03 -0700530 * inotify_find_watch - find an existing watch for an (ih,inode) pair
531 * @ih: inotify handle
532 * @inode: inode to watch
533 * @watchp: pointer to existing inotify_watch
534 *
535 * Caller must pin given inode (via nameidata).
536 */
537s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode,
538 struct inotify_watch **watchp)
539{
540 struct inotify_watch *old;
541 int ret = -ENOENT;
542
543 mutex_lock(&inode->inotify_mutex);
544 mutex_lock(&ih->mutex);
545
546 old = inode_find_handle(inode, ih);
547 if (unlikely(old)) {
548 get_inotify_watch(old); /* caller must put watch */
549 *watchp = old;
550 ret = old->wd;
551 }
552
553 mutex_unlock(&ih->mutex);
554 mutex_unlock(&inode->inotify_mutex);
555
556 return ret;
557}
558EXPORT_SYMBOL_GPL(inotify_find_watch);
559
560/**
Amy Griffis2d9048e2006-06-01 13:10:59 -0700561 * inotify_find_update_watch - find and update the mask of an existing watch
562 * @ih: inotify handle
563 * @inode: inode's watch to update
564 * @mask: mask of events to watch
565 *
566 * Caller must pin given inode (via nameidata).
567 */
568s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,
569 u32 mask)
570{
571 struct inotify_watch *old;
572 int mask_add = 0;
573 int ret;
574
575 if (mask & IN_MASK_ADD)
576 mask_add = 1;
577
578 /* don't allow invalid bits: we don't want flags set */
579 mask &= IN_ALL_EVENTS | IN_ONESHOT;
580 if (unlikely(!mask))
581 return -EINVAL;
582
583 mutex_lock(&inode->inotify_mutex);
584 mutex_lock(&ih->mutex);
585
586 /*
587 * Handle the case of re-adding a watch on an (inode,ih) pair that we
588 * are already watching. We just update the mask and return its wd.
589 */
590 old = inode_find_handle(inode, ih);
591 if (unlikely(!old)) {
592 ret = -ENOENT;
593 goto out;
594 }
595
596 if (mask_add)
597 old->mask |= mask;
598 else
599 old->mask = mask;
600 ret = old->wd;
601out:
602 mutex_unlock(&ih->mutex);
603 mutex_unlock(&inode->inotify_mutex);
604 return ret;
605}
606EXPORT_SYMBOL_GPL(inotify_find_update_watch);
607
608/**
609 * inotify_add_watch - add a watch to an inotify instance
610 * @ih: inotify handle
611 * @watch: caller allocated watch structure
612 * @inode: inode to watch
613 * @mask: mask of events to watch
614 *
615 * Caller must pin given inode (via nameidata).
616 * Caller must ensure it only calls inotify_add_watch() once per watch.
617 * Calls inotify_handle_get_wd() so may sleep.
618 */
619s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch,
620 struct inode *inode, u32 mask)
621{
622 int ret = 0;
Nick Piggind599e362008-02-06 01:37:28 -0800623 int newly_watched;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700624
625 /* don't allow invalid bits: we don't want flags set */
626 mask &= IN_ALL_EVENTS | IN_ONESHOT;
627 if (unlikely(!mask))
628 return -EINVAL;
629 watch->mask = mask;
630
631 mutex_lock(&inode->inotify_mutex);
632 mutex_lock(&ih->mutex);
633
634 /* Initialize a new watch */
635 ret = inotify_handle_get_wd(ih, watch);
636 if (unlikely(ret))
637 goto out;
638 ret = watch->wd;
639
Amy Griffis2d9048e2006-06-01 13:10:59 -0700640 /* save a reference to handle and bump the count to make it official */
641 get_inotify_handle(ih);
642 watch->ih = ih;
643
644 /*
645 * Save a reference to the inode and bump the ref count to make it
646 * official. We hold a reference to nameidata, which makes this safe.
647 */
648 watch->inode = igrab(inode);
649
Amy Griffis2d9048e2006-06-01 13:10:59 -0700650 /* Add the watch to the handle's and the inode's list */
Nick Piggind599e362008-02-06 01:37:28 -0800651 newly_watched = !inotify_inode_watched(inode);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700652 list_add(&watch->h_list, &ih->watches);
653 list_add(&watch->i_list, &inode->inotify_watches);
Nick Piggind599e362008-02-06 01:37:28 -0800654 /*
655 * Set child flags _after_ adding the watch, so there is no race
656 * windows where newly instantiated children could miss their parent's
657 * watched flag.
658 */
659 if (newly_watched)
660 set_dentry_child_flags(inode, 1);
661
Amy Griffis2d9048e2006-06-01 13:10:59 -0700662out:
663 mutex_unlock(&ih->mutex);
664 mutex_unlock(&inode->inotify_mutex);
665 return ret;
666}
667EXPORT_SYMBOL_GPL(inotify_add_watch);
668
669/**
Al Virob9efe8a2007-06-07 12:21:44 -0400670 * inotify_clone_watch - put the watch next to existing one
671 * @old: already installed watch
672 * @new: new watch
673 *
674 * Caller must hold the inotify_mutex of inode we are dealing with;
675 * it is expected to remove the old watch before unlocking the inode.
676 */
677s32 inotify_clone_watch(struct inotify_watch *old, struct inotify_watch *new)
678{
679 struct inotify_handle *ih = old->ih;
680 int ret = 0;
681
682 new->mask = old->mask;
683 new->ih = ih;
684
685 mutex_lock(&ih->mutex);
686
687 /* Initialize a new watch */
688 ret = inotify_handle_get_wd(ih, new);
689 if (unlikely(ret))
690 goto out;
691 ret = new->wd;
692
693 get_inotify_handle(ih);
694
695 new->inode = igrab(old->inode);
696
697 list_add(&new->h_list, &ih->watches);
698 list_add(&new->i_list, &old->inode->inotify_watches);
699out:
700 mutex_unlock(&ih->mutex);
701 return ret;
702}
703
Al Viro455434d2007-06-07 12:22:59 -0400704void inotify_evict_watch(struct inotify_watch *watch)
705{
706 get_inotify_watch(watch);
707 mutex_lock(&watch->ih->mutex);
708 inotify_remove_watch_locked(watch->ih, watch);
709 mutex_unlock(&watch->ih->mutex);
710}
711
Al Virob9efe8a2007-06-07 12:21:44 -0400712/**
Amy Griffis2d9048e2006-06-01 13:10:59 -0700713 * inotify_rm_wd - remove a watch from an inotify instance
714 * @ih: inotify handle
715 * @wd: watch descriptor to remove
Robert Love0eeca282005-07-12 17:06:03 -0400716 *
717 * Can sleep.
718 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700719int inotify_rm_wd(struct inotify_handle *ih, u32 wd)
Robert Love0eeca282005-07-12 17:06:03 -0400720{
721 struct inotify_watch *watch;
722 struct inode *inode;
723
Amy Griffis2d9048e2006-06-01 13:10:59 -0700724 mutex_lock(&ih->mutex);
725 watch = idr_find(&ih->idr, wd);
Robert Love0eeca282005-07-12 17:06:03 -0400726 if (unlikely(!watch)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700727 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400728 return -EINVAL;
729 }
730 get_inotify_watch(watch);
731 inode = watch->inode;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700732 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400733
Ingo Molnard4f9af92006-03-23 03:00:30 -0800734 mutex_lock(&inode->inotify_mutex);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700735 mutex_lock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400736
737 /* make sure that we did not race */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700738 if (likely(idr_find(&ih->idr, wd) == watch))
Amy Griffis3ca10062006-06-01 13:11:05 -0700739 inotify_remove_watch_locked(ih, watch);
Robert Love0eeca282005-07-12 17:06:03 -0400740
Amy Griffis2d9048e2006-06-01 13:10:59 -0700741 mutex_unlock(&ih->mutex);
Ingo Molnard4f9af92006-03-23 03:00:30 -0800742 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400743 put_inotify_watch(watch);
744
745 return 0;
746}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700747EXPORT_SYMBOL_GPL(inotify_rm_wd);
Robert Love0eeca282005-07-12 17:06:03 -0400748
Amy Griffisa9dc9712006-06-01 13:11:03 -0700749/**
750 * inotify_rm_watch - remove a watch from an inotify instance
751 * @ih: inotify handle
752 * @watch: watch to remove
753 *
754 * Can sleep.
755 */
756int inotify_rm_watch(struct inotify_handle *ih,
757 struct inotify_watch *watch)
758{
759 return inotify_rm_wd(ih, watch->wd);
760}
761EXPORT_SYMBOL_GPL(inotify_rm_watch);
762
Robert Love0eeca282005-07-12 17:06:03 -0400763/*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700764 * inotify_setup - core initialization function
Robert Love0eeca282005-07-12 17:06:03 -0400765 */
Robert Loveb6807162005-07-25 15:07:13 -0400766static int __init inotify_setup(void)
Robert Love0eeca282005-07-12 17:06:03 -0400767{
Robert Love0eeca282005-07-12 17:06:03 -0400768 atomic_set(&inotify_cookie, 0);
769
Robert Love0eeca282005-07-12 17:06:03 -0400770 return 0;
771}
772
Robert Loveb6807162005-07-25 15:07:13 -0400773module_init(inotify_setup);