blob: 8477c4fbecb45ff7a181c05ac835e8238c086dcd [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>
Robert Love0eeca282005-07-12 17:06:03 -040030#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/writeback.h>
33#include <linux/inotify.h>
Robert Love0eeca282005-07-12 17:06:03 -040034
35static atomic_t inotify_cookie;
36
Robert Love0eeca282005-07-12 17:06:03 -040037/*
38 * Lock ordering:
39 *
40 * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
Ingo Molnarf24075b2006-03-23 03:00:34 -080041 * iprune_mutex (synchronize shrink_icache_memory())
Robert Love0eeca282005-07-12 17:06:03 -040042 * inode_lock (protects the super_block->s_inodes list)
Ingo Molnard4f9af92006-03-23 03:00:30 -080043 * inode->inotify_mutex (protects inode->inotify_watches and watches->i_list)
Amy Griffis2d9048e2006-06-01 13:10:59 -070044 * inotify_handle->mutex (protects inotify_handle and watches->h_list)
45 *
46 * The inode->inotify_mutex and inotify_handle->mutex and held during execution
47 * of a caller's event handler. Thus, the caller must not hold any locks
48 * taken in their event handler while calling any of the published inotify
49 * interfaces.
Robert Love0eeca282005-07-12 17:06:03 -040050 */
51
52/*
Amy Griffis2d9048e2006-06-01 13:10:59 -070053 * Lifetimes of the three main data structures--inotify_handle, inode, and
Robert Love0eeca282005-07-12 17:06:03 -040054 * inotify_watch--are managed by reference count.
55 *
Amy Griffis2d9048e2006-06-01 13:10:59 -070056 * inotify_handle: Lifetime is from inotify_init() to inotify_destroy().
57 * Additional references can bump the count via get_inotify_handle() and drop
58 * the count via put_inotify_handle().
Robert Love0eeca282005-07-12 17:06:03 -040059 *
Amy Griffis2d9048e2006-06-01 13:10:59 -070060 * inotify_watch: for inotify's purposes, lifetime is from inotify_add_watch()
61 * to remove_watch_no_event(). Additional references can bump the count via
62 * get_inotify_watch() and drop the count via put_inotify_watch(). The caller
63 * is reponsible for the final put after receiving IN_IGNORED, or when using
64 * IN_ONESHOT after receiving the first event. Inotify does the final put if
65 * inotify_destroy() is called.
Robert Love0eeca282005-07-12 17:06:03 -040066 *
67 * inode: Pinned so long as the inode is associated with a watch, from
Amy Griffis2d9048e2006-06-01 13:10:59 -070068 * inotify_add_watch() to the final put_inotify_watch().
Robert Love0eeca282005-07-12 17:06:03 -040069 */
70
71/*
Amy Griffis2d9048e2006-06-01 13:10:59 -070072 * struct inotify_handle - represents an inotify instance
Robert Love0eeca282005-07-12 17:06:03 -040073 *
Ingo Molnard4f9af92006-03-23 03:00:30 -080074 * This structure is protected by the mutex 'mutex'.
Robert Love0eeca282005-07-12 17:06:03 -040075 */
Amy Griffis2d9048e2006-06-01 13:10:59 -070076struct inotify_handle {
Robert Love0eeca282005-07-12 17:06:03 -040077 struct idr idr; /* idr mapping wd -> watch */
Ingo Molnard4f9af92006-03-23 03:00:30 -080078 struct mutex mutex; /* protects this bad boy */
Robert Love0eeca282005-07-12 17:06:03 -040079 struct list_head watches; /* list of watches */
80 atomic_t count; /* reference count */
John McCutchanb9c55d22005-08-01 11:00:45 -040081 u32 last_wd; /* the last wd allocated */
Amy Griffis2d9048e2006-06-01 13:10:59 -070082 const struct inotify_operations *in_ops; /* inotify caller operations */
Robert Love0eeca282005-07-12 17:06:03 -040083};
84
Amy Griffis2d9048e2006-06-01 13:10:59 -070085static inline void get_inotify_handle(struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -040086{
Amy Griffis2d9048e2006-06-01 13:10:59 -070087 atomic_inc(&ih->count);
Robert Love0eeca282005-07-12 17:06:03 -040088}
89
Amy Griffis2d9048e2006-06-01 13:10:59 -070090static inline void put_inotify_handle(struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -040091{
Amy Griffis2d9048e2006-06-01 13:10:59 -070092 if (atomic_dec_and_test(&ih->count)) {
93 idr_destroy(&ih->idr);
94 kfree(ih);
Robert Love0eeca282005-07-12 17:06:03 -040095 }
96}
97
Amy Griffis2d9048e2006-06-01 13:10:59 -070098/**
99 * get_inotify_watch - grab a reference to an inotify_watch
100 * @watch: watch to grab
101 */
102void get_inotify_watch(struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400103{
104 atomic_inc(&watch->count);
105}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700106EXPORT_SYMBOL_GPL(get_inotify_watch);
Robert Love0eeca282005-07-12 17:06:03 -0400107
Amy Griffis2d9048e2006-06-01 13:10:59 -0700108/**
Robert Love0eeca282005-07-12 17:06:03 -0400109 * put_inotify_watch - decrements the ref count on a given watch. cleans up
Amy Griffis2d9048e2006-06-01 13:10:59 -0700110 * watch references if the count reaches zero. inotify_watch is freed by
111 * inotify callers via the destroy_watch() op.
112 * @watch: watch to release
Robert Love0eeca282005-07-12 17:06:03 -0400113 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700114void put_inotify_watch(struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400115{
116 if (atomic_dec_and_test(&watch->count)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700117 struct inotify_handle *ih = watch->ih;
118
Robert Love0eeca282005-07-12 17:06:03 -0400119 iput(watch->inode);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700120 ih->in_ops->destroy_watch(watch);
121 put_inotify_handle(ih);
Robert Love0eeca282005-07-12 17:06:03 -0400122 }
123}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700124EXPORT_SYMBOL_GPL(put_inotify_watch);
Robert Love0eeca282005-07-12 17:06:03 -0400125
126/*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700127 * inotify_handle_get_wd - returns the next WD for use by the given handle
Robert Love0eeca282005-07-12 17:06:03 -0400128 *
Amy Griffis2d9048e2006-06-01 13:10:59 -0700129 * Callers must hold ih->mutex. This function can sleep.
Robert Love0eeca282005-07-12 17:06:03 -0400130 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700131static int inotify_handle_get_wd(struct inotify_handle *ih,
132 struct inotify_watch *watch)
Robert Love0eeca282005-07-12 17:06:03 -0400133{
134 int ret;
135
136 do {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700137 if (unlikely(!idr_pre_get(&ih->idr, GFP_KERNEL)))
Robert Love0eeca282005-07-12 17:06:03 -0400138 return -ENOSPC;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700139 ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd);
Robert Love0eeca282005-07-12 17:06:03 -0400140 } while (ret == -EAGAIN);
141
Amy Griffis2d9048e2006-06-01 13:10:59 -0700142 if (likely(!ret))
143 ih->last_wd = watch->wd;
144
Robert Love0eeca282005-07-12 17:06:03 -0400145 return ret;
146}
147
148/*
Nick Pigginc32ccd82006-03-25 03:07:09 -0800149 * inotify_inode_watched - returns nonzero if there are watches on this inode
150 * and zero otherwise. We call this lockless, we do not care if we race.
151 */
152static inline int inotify_inode_watched(struct inode *inode)
153{
154 return !list_empty(&inode->inotify_watches);
155}
156
157/*
158 * Get child dentry flag into synch with parent inode.
159 * Flag should always be clear for negative dentrys.
160 */
161static void set_dentry_child_flags(struct inode *inode, int watched)
162{
163 struct dentry *alias;
164
165 spin_lock(&dcache_lock);
166 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
167 struct dentry *child;
168
169 list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {
170 if (!child->d_inode) {
171 WARN_ON(child->d_flags & DCACHE_INOTIFY_PARENT_WATCHED);
172 continue;
173 }
174 spin_lock(&child->d_lock);
175 if (watched) {
176 WARN_ON(child->d_flags &
177 DCACHE_INOTIFY_PARENT_WATCHED);
178 child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
179 } else {
180 WARN_ON(!(child->d_flags &
181 DCACHE_INOTIFY_PARENT_WATCHED));
182 child->d_flags&=~DCACHE_INOTIFY_PARENT_WATCHED;
183 }
184 spin_unlock(&child->d_lock);
185 }
186 }
187 spin_unlock(&dcache_lock);
188}
189
190/*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700191 * inotify_find_handle - find the watch associated with the given inode and
192 * handle
Robert Love0eeca282005-07-12 17:06:03 -0400193 *
Ingo Molnard4f9af92006-03-23 03:00:30 -0800194 * Callers must hold inode->inotify_mutex.
Robert Love0eeca282005-07-12 17:06:03 -0400195 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700196static struct inotify_watch *inode_find_handle(struct inode *inode,
197 struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -0400198{
199 struct inotify_watch *watch;
200
201 list_for_each_entry(watch, &inode->inotify_watches, i_list) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700202 if (watch->ih == ih)
Robert Love0eeca282005-07-12 17:06:03 -0400203 return watch;
204 }
205
206 return NULL;
207}
208
209/*
210 * remove_watch_no_event - remove_watch() without the IN_IGNORED event.
Amy Griffis2d9048e2006-06-01 13:10:59 -0700211 *
212 * Callers must hold both inode->inotify_mutex and ih->mutex.
Robert Love0eeca282005-07-12 17:06:03 -0400213 */
214static void remove_watch_no_event(struct inotify_watch *watch,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700215 struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -0400216{
217 list_del(&watch->i_list);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700218 list_del(&watch->h_list);
Robert Love0eeca282005-07-12 17:06:03 -0400219
Nick Pigginc32ccd82006-03-25 03:07:09 -0800220 if (!inotify_inode_watched(watch->inode))
221 set_dentry_child_flags(watch->inode, 0);
222
Amy Griffis2d9048e2006-06-01 13:10:59 -0700223 idr_remove(&ih->idr, watch->wd);
Robert Love0eeca282005-07-12 17:06:03 -0400224}
225
226/*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700227 * remove_watch - Remove a watch from both the handle and the inode. Sends
228 * the IN_IGNORED event signifying that the inode is no longer watched.
Robert Love0eeca282005-07-12 17:06:03 -0400229 *
Amy Griffis2d9048e2006-06-01 13:10:59 -0700230 * Callers must hold both inode->inotify_mutex and ih->mutex.
Robert Love0eeca282005-07-12 17:06:03 -0400231 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700232static void remove_watch(struct inotify_watch *watch, struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -0400233{
Amy Griffis2d9048e2006-06-01 13:10:59 -0700234 remove_watch_no_event(watch, ih);
Amy Griffis7c297722006-06-01 13:11:01 -0700235 ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL);
Robert Love0eeca282005-07-12 17:06:03 -0400236}
237
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
250 WARN_ON(entry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED);
251 spin_lock(&entry->d_lock);
252 parent = entry->d_parent;
Arnd Bergmann091e8812006-04-10 22:54:31 -0700253 if (parent->d_inode && inotify_inode_watched(parent->d_inode))
Nick Pigginc32ccd82006-03-25 03:07:09 -0800254 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
255 spin_unlock(&entry->d_lock);
Robert Love0eeca282005-07-12 17:06:03 -0400256}
257
Nick Pigginc32ccd82006-03-25 03:07:09 -0800258/*
259 * inotify_d_move - dcache entry has been moved
260 */
261void inotify_d_move(struct dentry *entry)
262{
263 struct dentry *parent;
264
265 parent = entry->d_parent;
266 if (inotify_inode_watched(parent->d_inode))
267 entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
268 else
269 entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
270}
Robert Love0eeca282005-07-12 17:06:03 -0400271
272/**
273 * inotify_inode_queue_event - queue an event to all watches on this inode
274 * @inode: inode event is originating from
275 * @mask: event mask describing this event
276 * @cookie: cookie for synchronization, or zero
277 * @name: filename, if any
Amy Griffis7c297722006-06-01 13:11:01 -0700278 * @n_inode: inode associated with name
Robert Love0eeca282005-07-12 17:06:03 -0400279 */
280void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
Amy Griffis7c297722006-06-01 13:11:01 -0700281 const char *name, struct inode *n_inode)
Robert Love0eeca282005-07-12 17:06:03 -0400282{
283 struct inotify_watch *watch, *next;
284
285 if (!inotify_inode_watched(inode))
286 return;
287
Ingo Molnard4f9af92006-03-23 03:00:30 -0800288 mutex_lock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400289 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
290 u32 watch_mask = watch->mask;
291 if (watch_mask & mask) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700292 struct inotify_handle *ih= watch->ih;
293 mutex_lock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400294 if (watch_mask & IN_ONESHOT)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700295 remove_watch_no_event(watch, ih);
Amy Griffis7c297722006-06-01 13:11:01 -0700296 ih->in_ops->handle_event(watch, watch->wd, mask, cookie,
297 name, n_inode);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700298 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400299 }
300 }
Ingo Molnard4f9af92006-03-23 03:00:30 -0800301 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400302}
303EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
304
305/**
306 * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
307 * @dentry: the dentry in question, we queue against this dentry's parent
308 * @mask: event mask describing this event
309 * @cookie: cookie for synchronization, or zero
310 * @name: filename, if any
311 */
312void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
313 u32 cookie, const char *name)
314{
315 struct dentry *parent;
316 struct inode *inode;
317
Nick Pigginc32ccd82006-03-25 03:07:09 -0800318 if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))
John McCutchan820249b2005-09-06 15:16:38 -0700319 return;
320
Robert Love0eeca282005-07-12 17:06:03 -0400321 spin_lock(&dentry->d_lock);
322 parent = dentry->d_parent;
323 inode = parent->d_inode;
324
325 if (inotify_inode_watched(inode)) {
326 dget(parent);
327 spin_unlock(&dentry->d_lock);
Amy Griffis7c297722006-06-01 13:11:01 -0700328 inotify_inode_queue_event(inode, mask, cookie, name,
329 dentry->d_inode);
Robert Love0eeca282005-07-12 17:06:03 -0400330 dput(parent);
331 } else
332 spin_unlock(&dentry->d_lock);
333}
334EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
335
336/**
337 * inotify_get_cookie - return a unique cookie for use in synchronizing events.
338 */
339u32 inotify_get_cookie(void)
340{
341 return atomic_inc_return(&inotify_cookie);
342}
343EXPORT_SYMBOL_GPL(inotify_get_cookie);
344
345/**
346 * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
347 * @list: list of inodes being unmounted (sb->s_inodes)
348 *
349 * Called with inode_lock held, protecting the unmounting super block's list
Ingo Molnarf24075b2006-03-23 03:00:34 -0800350 * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
Robert Love0eeca282005-07-12 17:06:03 -0400351 * We temporarily drop inode_lock, however, and CAN block.
352 */
353void inotify_unmount_inodes(struct list_head *list)
354{
355 struct inode *inode, *next_i, *need_iput = NULL;
356
357 list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
358 struct inotify_watch *watch, *next_w;
359 struct inode *need_iput_tmp;
360 struct list_head *watches;
361
362 /*
363 * If i_count is zero, the inode cannot have any watches and
364 * doing an __iget/iput with MS_ACTIVE clear would actually
365 * evict all inodes with zero i_count from icache which is
366 * unnecessarily violent and may in fact be illegal to do.
367 */
368 if (!atomic_read(&inode->i_count))
369 continue;
370
371 /*
372 * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or
373 * I_WILL_FREE which is fine because by that point the inode
374 * cannot have any associated watches.
375 */
376 if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE))
377 continue;
378
379 need_iput_tmp = need_iput;
380 need_iput = NULL;
381 /* In case the remove_watch() drops a reference. */
382 if (inode != need_iput_tmp)
383 __iget(inode);
384 else
385 need_iput_tmp = NULL;
386 /* In case the dropping of a reference would nuke next_i. */
387 if ((&next_i->i_sb_list != list) &&
388 atomic_read(&next_i->i_count) &&
389 !(next_i->i_state & (I_CLEAR | I_FREEING |
390 I_WILL_FREE))) {
391 __iget(next_i);
392 need_iput = next_i;
393 }
394
395 /*
396 * We can safely drop inode_lock here because we hold
397 * references on both inode and next_i. Also no new inodes
398 * will be added since the umount has begun. Finally,
Ingo Molnarf24075b2006-03-23 03:00:34 -0800399 * iprune_mutex keeps shrink_icache_memory() away.
Robert Love0eeca282005-07-12 17:06:03 -0400400 */
401 spin_unlock(&inode_lock);
402
403 if (need_iput_tmp)
404 iput(need_iput_tmp);
405
406 /* for each watch, send IN_UNMOUNT and then remove it */
Ingo Molnard4f9af92006-03-23 03:00:30 -0800407 mutex_lock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400408 watches = &inode->inotify_watches;
409 list_for_each_entry_safe(watch, next_w, watches, i_list) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700410 struct inotify_handle *ih= watch->ih;
411 mutex_lock(&ih->mutex);
412 ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,
Amy Griffis7c297722006-06-01 13:11:01 -0700413 NULL, NULL);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700414 remove_watch(watch, ih);
415 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400416 }
Ingo Molnard4f9af92006-03-23 03:00:30 -0800417 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400418 iput(inode);
419
420 spin_lock(&inode_lock);
421 }
422}
423EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
424
425/**
426 * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
427 * @inode: inode that is about to be removed
428 */
429void inotify_inode_is_dead(struct inode *inode)
430{
431 struct inotify_watch *watch, *next;
432
Ingo Molnard4f9af92006-03-23 03:00:30 -0800433 mutex_lock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400434 list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700435 struct inotify_handle *ih = watch->ih;
436 mutex_lock(&ih->mutex);
437 remove_watch(watch, ih);
438 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400439 }
Ingo Molnard4f9af92006-03-23 03:00:30 -0800440 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400441}
442EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
443
Amy Griffis2d9048e2006-06-01 13:10:59 -0700444/* Kernel Consumer API */
Robert Love0eeca282005-07-12 17:06:03 -0400445
Amy Griffis2d9048e2006-06-01 13:10:59 -0700446/**
447 * inotify_init - allocate and initialize an inotify instance
448 * @ops: caller's inotify operations
449 */
450struct inotify_handle *inotify_init(const struct inotify_operations *ops)
Robert Love0eeca282005-07-12 17:06:03 -0400451{
Amy Griffis2d9048e2006-06-01 13:10:59 -0700452 struct inotify_handle *ih;
Robert Love0eeca282005-07-12 17:06:03 -0400453
Amy Griffis2d9048e2006-06-01 13:10:59 -0700454 ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);
455 if (unlikely(!ih))
456 return ERR_PTR(-ENOMEM);
Robert Love0eeca282005-07-12 17:06:03 -0400457
Amy Griffis2d9048e2006-06-01 13:10:59 -0700458 idr_init(&ih->idr);
459 INIT_LIST_HEAD(&ih->watches);
460 mutex_init(&ih->mutex);
461 ih->last_wd = 0;
462 ih->in_ops = ops;
463 atomic_set(&ih->count, 0);
464 get_inotify_handle(ih);
465
466 return ih;
Robert Love0eeca282005-07-12 17:06:03 -0400467}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700468EXPORT_SYMBOL_GPL(inotify_init);
Robert Love0eeca282005-07-12 17:06:03 -0400469
Amy Griffis2d9048e2006-06-01 13:10:59 -0700470/**
Amy Griffisa9dc9712006-06-01 13:11:03 -0700471 * inotify_init_watch - initialize an inotify watch
472 * @watch: watch to initialize
473 */
474void inotify_init_watch(struct inotify_watch *watch)
475{
476 INIT_LIST_HEAD(&watch->h_list);
477 INIT_LIST_HEAD(&watch->i_list);
478 atomic_set(&watch->count, 0);
479 get_inotify_watch(watch); /* initial get */
480}
481EXPORT_SYMBOL_GPL(inotify_init_watch);
482
483/**
Amy Griffis2d9048e2006-06-01 13:10:59 -0700484 * inotify_destroy - clean up and destroy an inotify instance
485 * @ih: inotify handle
486 */
487void inotify_destroy(struct inotify_handle *ih)
Robert Love0eeca282005-07-12 17:06:03 -0400488{
Robert Love0eeca282005-07-12 17:06:03 -0400489 /*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700490 * Destroy all of the watches for this handle. Unfortunately, not very
Robert Love0eeca282005-07-12 17:06:03 -0400491 * pretty. We cannot do a simple iteration over the list, because we
492 * do not know the inode until we iterate to the watch. But we need to
Amy Griffis2d9048e2006-06-01 13:10:59 -0700493 * hold inode->inotify_mutex before ih->mutex. The following works.
Robert Love0eeca282005-07-12 17:06:03 -0400494 */
495 while (1) {
496 struct inotify_watch *watch;
497 struct list_head *watches;
498 struct inode *inode;
499
Amy Griffis2d9048e2006-06-01 13:10:59 -0700500 mutex_lock(&ih->mutex);
501 watches = &ih->watches;
Robert Love0eeca282005-07-12 17:06:03 -0400502 if (list_empty(watches)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700503 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400504 break;
505 }
Amy Griffis2d9048e2006-06-01 13:10:59 -0700506 watch = list_entry(watches->next, struct inotify_watch, h_list);
Robert Love0eeca282005-07-12 17:06:03 -0400507 get_inotify_watch(watch);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700508 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400509
510 inode = watch->inode;
Ingo Molnard4f9af92006-03-23 03:00:30 -0800511 mutex_lock(&inode->inotify_mutex);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700512 mutex_lock(&ih->mutex);
Amy Griffis66055a42006-05-20 15:00:06 -0700513
514 /* make sure we didn't race with another list removal */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700515 if (likely(idr_find(&ih->idr, watch->wd))) {
516 remove_watch_no_event(watch, ih);
517 put_inotify_watch(watch);
518 }
Amy Griffis66055a42006-05-20 15:00:06 -0700519
Amy Griffis2d9048e2006-06-01 13:10:59 -0700520 mutex_unlock(&ih->mutex);
Ingo Molnard4f9af92006-03-23 03:00:30 -0800521 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400522 put_inotify_watch(watch);
523 }
524
Amy Griffis2d9048e2006-06-01 13:10:59 -0700525 /* free this handle: the put matching the get in inotify_init() */
526 put_inotify_handle(ih);
Robert Love0eeca282005-07-12 17:06:03 -0400527}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700528EXPORT_SYMBOL_GPL(inotify_destroy);
Robert Love0eeca282005-07-12 17:06:03 -0400529
Amy Griffis2d9048e2006-06-01 13:10:59 -0700530/**
Amy Griffisa9dc9712006-06-01 13:11:03 -0700531 * inotify_find_watch - find an existing watch for an (ih,inode) pair
532 * @ih: inotify handle
533 * @inode: inode to watch
534 * @watchp: pointer to existing inotify_watch
535 *
536 * Caller must pin given inode (via nameidata).
537 */
538s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode,
539 struct inotify_watch **watchp)
540{
541 struct inotify_watch *old;
542 int ret = -ENOENT;
543
544 mutex_lock(&inode->inotify_mutex);
545 mutex_lock(&ih->mutex);
546
547 old = inode_find_handle(inode, ih);
548 if (unlikely(old)) {
549 get_inotify_watch(old); /* caller must put watch */
550 *watchp = old;
551 ret = old->wd;
552 }
553
554 mutex_unlock(&ih->mutex);
555 mutex_unlock(&inode->inotify_mutex);
556
557 return ret;
558}
559EXPORT_SYMBOL_GPL(inotify_find_watch);
560
561/**
Amy Griffis2d9048e2006-06-01 13:10:59 -0700562 * inotify_find_update_watch - find and update the mask of an existing watch
563 * @ih: inotify handle
564 * @inode: inode's watch to update
565 * @mask: mask of events to watch
566 *
567 * Caller must pin given inode (via nameidata).
568 */
569s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,
570 u32 mask)
571{
572 struct inotify_watch *old;
573 int mask_add = 0;
574 int ret;
575
576 if (mask & IN_MASK_ADD)
577 mask_add = 1;
578
579 /* don't allow invalid bits: we don't want flags set */
580 mask &= IN_ALL_EVENTS | IN_ONESHOT;
581 if (unlikely(!mask))
582 return -EINVAL;
583
584 mutex_lock(&inode->inotify_mutex);
585 mutex_lock(&ih->mutex);
586
587 /*
588 * Handle the case of re-adding a watch on an (inode,ih) pair that we
589 * are already watching. We just update the mask and return its wd.
590 */
591 old = inode_find_handle(inode, ih);
592 if (unlikely(!old)) {
593 ret = -ENOENT;
594 goto out;
595 }
596
597 if (mask_add)
598 old->mask |= mask;
599 else
600 old->mask = mask;
601 ret = old->wd;
602out:
603 mutex_unlock(&ih->mutex);
604 mutex_unlock(&inode->inotify_mutex);
605 return ret;
606}
607EXPORT_SYMBOL_GPL(inotify_find_update_watch);
608
609/**
610 * inotify_add_watch - add a watch to an inotify instance
611 * @ih: inotify handle
612 * @watch: caller allocated watch structure
613 * @inode: inode to watch
614 * @mask: mask of events to watch
615 *
616 * Caller must pin given inode (via nameidata).
617 * Caller must ensure it only calls inotify_add_watch() once per watch.
618 * Calls inotify_handle_get_wd() so may sleep.
619 */
620s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch,
621 struct inode *inode, u32 mask)
622{
623 int ret = 0;
624
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 if (!inotify_inode_watched(inode))
651 set_dentry_child_flags(inode, 1);
652
653 /* Add the watch to the handle's and the inode's list */
654 list_add(&watch->h_list, &ih->watches);
655 list_add(&watch->i_list, &inode->inotify_watches);
656out:
657 mutex_unlock(&ih->mutex);
658 mutex_unlock(&inode->inotify_mutex);
659 return ret;
660}
661EXPORT_SYMBOL_GPL(inotify_add_watch);
662
663/**
664 * inotify_rm_wd - remove a watch from an inotify instance
665 * @ih: inotify handle
666 * @wd: watch descriptor to remove
Robert Love0eeca282005-07-12 17:06:03 -0400667 *
668 * Can sleep.
669 */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700670int inotify_rm_wd(struct inotify_handle *ih, u32 wd)
Robert Love0eeca282005-07-12 17:06:03 -0400671{
672 struct inotify_watch *watch;
673 struct inode *inode;
674
Amy Griffis2d9048e2006-06-01 13:10:59 -0700675 mutex_lock(&ih->mutex);
676 watch = idr_find(&ih->idr, wd);
Robert Love0eeca282005-07-12 17:06:03 -0400677 if (unlikely(!watch)) {
Amy Griffis2d9048e2006-06-01 13:10:59 -0700678 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400679 return -EINVAL;
680 }
681 get_inotify_watch(watch);
682 inode = watch->inode;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700683 mutex_unlock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400684
Ingo Molnard4f9af92006-03-23 03:00:30 -0800685 mutex_lock(&inode->inotify_mutex);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700686 mutex_lock(&ih->mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400687
688 /* make sure that we did not race */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700689 if (likely(idr_find(&ih->idr, wd) == watch))
690 remove_watch(watch, ih);
Robert Love0eeca282005-07-12 17:06:03 -0400691
Amy Griffis2d9048e2006-06-01 13:10:59 -0700692 mutex_unlock(&ih->mutex);
Ingo Molnard4f9af92006-03-23 03:00:30 -0800693 mutex_unlock(&inode->inotify_mutex);
Robert Love0eeca282005-07-12 17:06:03 -0400694 put_inotify_watch(watch);
695
696 return 0;
697}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700698EXPORT_SYMBOL_GPL(inotify_rm_wd);
Robert Love0eeca282005-07-12 17:06:03 -0400699
Amy Griffisa9dc9712006-06-01 13:11:03 -0700700/**
701 * inotify_rm_watch - remove a watch from an inotify instance
702 * @ih: inotify handle
703 * @watch: watch to remove
704 *
705 * Can sleep.
706 */
707int inotify_rm_watch(struct inotify_handle *ih,
708 struct inotify_watch *watch)
709{
710 return inotify_rm_wd(ih, watch->wd);
711}
712EXPORT_SYMBOL_GPL(inotify_rm_watch);
713
Robert Love0eeca282005-07-12 17:06:03 -0400714/*
Amy Griffis2d9048e2006-06-01 13:10:59 -0700715 * inotify_setup - core initialization function
Robert Love0eeca282005-07-12 17:06:03 -0400716 */
Robert Loveb6807162005-07-25 15:07:13 -0400717static int __init inotify_setup(void)
Robert Love0eeca282005-07-12 17:06:03 -0400718{
Robert Love0eeca282005-07-12 17:06:03 -0400719 atomic_set(&inotify_cookie, 0);
720
Robert Love0eeca282005-07-12 17:06:03 -0400721 return 0;
722}
723
Robert Loveb6807162005-07-25 15:07:13 -0400724module_init(inotify_setup);