Blame view

fs/inotify.c 20.2 KB
0eeca2830   Robert Love   [PATCH] inotify
1
2
3
4
5
6
7
  /*
   * fs/inotify.c - inode-based file event notifications
   *
   * Authors:
   *	John McCutchan	<ttb@tentacle.dhs.org>
   *	Robert Love	<rml@novell.com>
   *
2d9048e20   Amy Griffis   [PATCH] inotify (...
8
9
   * Kernel API added by: Amy Griffis <amy.griffis@hp.com>
   *
0eeca2830   Robert Love   [PATCH] inotify
10
   * Copyright (C) 2005 John McCutchan
2d9048e20   Amy Griffis   [PATCH] inotify (...
11
   * Copyright 2006 Hewlett-Packard Development Company, L.P.
0eeca2830   Robert Love   [PATCH] inotify
12
13
14
15
16
17
18
19
20
21
22
23
24
25
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the
   * Free Software Foundation; either version 2, or (at your option) any
   * later version.
   *
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   */
  
  #include <linux/module.h>
  #include <linux/kernel.h>
0eeca2830   Robert Love   [PATCH] inotify
26
27
28
29
  #include <linux/spinlock.h>
  #include <linux/idr.h>
  #include <linux/slab.h>
  #include <linux/fs.h>
914e26379   Al Viro   [PATCH] severing ...
30
  #include <linux/sched.h>
0eeca2830   Robert Love   [PATCH] inotify
31
32
33
34
  #include <linux/init.h>
  #include <linux/list.h>
  #include <linux/writeback.h>
  #include <linux/inotify.h>
0eeca2830   Robert Love   [PATCH] inotify
35
36
  
  static atomic_t inotify_cookie;
0eeca2830   Robert Love   [PATCH] inotify
37
38
39
40
  /*
   * Lock ordering:
   *
   * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
f24075bd0   Ingo Molnar   [PATCH] sem2mutex...
41
   * iprune_mutex (synchronize shrink_icache_memory())
0eeca2830   Robert Love   [PATCH] inotify
42
   * 	inode_lock (protects the super_block->s_inodes list)
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
43
   * 	inode->inotify_mutex (protects inode->inotify_watches and watches->i_list)
2d9048e20   Amy Griffis   [PATCH] inotify (...
44
45
46
47
48
49
   * 		inotify_handle->mutex (protects inotify_handle and watches->h_list)
   *
   * The inode->inotify_mutex and inotify_handle->mutex and held during execution
   * of a caller's event handler.  Thus, the caller must not hold any locks
   * taken in their event handler while calling any of the published inotify
   * interfaces.
0eeca2830   Robert Love   [PATCH] inotify
50
51
52
   */
  
  /*
2d9048e20   Amy Griffis   [PATCH] inotify (...
53
   * Lifetimes of the three main data structures--inotify_handle, inode, and
0eeca2830   Robert Love   [PATCH] inotify
54
55
   * inotify_watch--are managed by reference count.
   *
2d9048e20   Amy Griffis   [PATCH] inotify (...
56
57
58
   * inotify_handle: Lifetime is from inotify_init() to inotify_destroy().
   * Additional references can bump the count via get_inotify_handle() and drop
   * the count via put_inotify_handle().
0eeca2830   Robert Love   [PATCH] inotify
59
   *
2d9048e20   Amy Griffis   [PATCH] inotify (...
60
61
62
63
64
65
   * inotify_watch: for inotify's purposes, lifetime is from inotify_add_watch()
   * to remove_watch_no_event().  Additional references can bump the count via
   * get_inotify_watch() and drop the count via put_inotify_watch().  The caller
   * is reponsible for the final put after receiving IN_IGNORED, or when using
   * IN_ONESHOT after receiving the first event.  Inotify does the final put if
   * inotify_destroy() is called.
0eeca2830   Robert Love   [PATCH] inotify
66
67
   *
   * inode: Pinned so long as the inode is associated with a watch, from
2d9048e20   Amy Griffis   [PATCH] inotify (...
68
   * inotify_add_watch() to the final put_inotify_watch().
0eeca2830   Robert Love   [PATCH] inotify
69
70
71
   */
  
  /*
2d9048e20   Amy Griffis   [PATCH] inotify (...
72
   * struct inotify_handle - represents an inotify instance
0eeca2830   Robert Love   [PATCH] inotify
73
   *
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
74
   * This structure is protected by the mutex 'mutex'.
0eeca2830   Robert Love   [PATCH] inotify
75
   */
2d9048e20   Amy Griffis   [PATCH] inotify (...
76
  struct inotify_handle {
0eeca2830   Robert Love   [PATCH] inotify
77
  	struct idr		idr;		/* idr mapping wd -> watch */
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
78
  	struct mutex		mutex;		/* protects this bad boy */
0eeca2830   Robert Love   [PATCH] inotify
79
80
  	struct list_head	watches;	/* list of watches */
  	atomic_t		count;		/* reference count */
b9c55d29e   John McCutchan   [PATCH] inotify: ...
81
  	u32			last_wd;	/* the last wd allocated */
2d9048e20   Amy Griffis   [PATCH] inotify (...
82
  	const struct inotify_operations *in_ops; /* inotify caller operations */
0eeca2830   Robert Love   [PATCH] inotify
83
  };
2d9048e20   Amy Griffis   [PATCH] inotify (...
84
  static inline void get_inotify_handle(struct inotify_handle *ih)
0eeca2830   Robert Love   [PATCH] inotify
85
  {
2d9048e20   Amy Griffis   [PATCH] inotify (...
86
  	atomic_inc(&ih->count);
0eeca2830   Robert Love   [PATCH] inotify
87
  }
2d9048e20   Amy Griffis   [PATCH] inotify (...
88
  static inline void put_inotify_handle(struct inotify_handle *ih)
0eeca2830   Robert Love   [PATCH] inotify
89
  {
2d9048e20   Amy Griffis   [PATCH] inotify (...
90
91
92
  	if (atomic_dec_and_test(&ih->count)) {
  		idr_destroy(&ih->idr);
  		kfree(ih);
0eeca2830   Robert Love   [PATCH] inotify
93
94
  	}
  }
2d9048e20   Amy Griffis   [PATCH] inotify (...
95
96
97
98
99
  /**
   * get_inotify_watch - grab a reference to an inotify_watch
   * @watch: watch to grab
   */
  void get_inotify_watch(struct inotify_watch *watch)
0eeca2830   Robert Love   [PATCH] inotify
100
101
102
  {
  	atomic_inc(&watch->count);
  }
2d9048e20   Amy Griffis   [PATCH] inotify (...
103
  EXPORT_SYMBOL_GPL(get_inotify_watch);
0eeca2830   Robert Love   [PATCH] inotify
104

2d9048e20   Amy Griffis   [PATCH] inotify (...
105
  /**
0eeca2830   Robert Love   [PATCH] inotify
106
   * put_inotify_watch - decrements the ref count on a given watch.  cleans up
2d9048e20   Amy Griffis   [PATCH] inotify (...
107
108
109
   * watch references if the count reaches zero.  inotify_watch is freed by
   * inotify callers via the destroy_watch() op.
   * @watch: watch to release
0eeca2830   Robert Love   [PATCH] inotify
110
   */
2d9048e20   Amy Griffis   [PATCH] inotify (...
111
  void put_inotify_watch(struct inotify_watch *watch)
0eeca2830   Robert Love   [PATCH] inotify
112
113
  {
  	if (atomic_dec_and_test(&watch->count)) {
2d9048e20   Amy Griffis   [PATCH] inotify (...
114
  		struct inotify_handle *ih = watch->ih;
0eeca2830   Robert Love   [PATCH] inotify
115

2d9048e20   Amy Griffis   [PATCH] inotify (...
116
117
118
  		iput(watch->inode);
  		ih->in_ops->destroy_watch(watch);
  		put_inotify_handle(ih);
0eeca2830   Robert Love   [PATCH] inotify
119
120
  	}
  }
2d9048e20   Amy Griffis   [PATCH] inotify (...
121
  EXPORT_SYMBOL_GPL(put_inotify_watch);
0eeca2830   Robert Love   [PATCH] inotify
122
123
  
  /*
2d9048e20   Amy Griffis   [PATCH] inotify (...
124
   * inotify_handle_get_wd - returns the next WD for use by the given handle
0eeca2830   Robert Love   [PATCH] inotify
125
   *
2d9048e20   Amy Griffis   [PATCH] inotify (...
126
   * Callers must hold ih->mutex.  This function can sleep.
0eeca2830   Robert Love   [PATCH] inotify
127
   */
2d9048e20   Amy Griffis   [PATCH] inotify (...
128
129
  static int inotify_handle_get_wd(struct inotify_handle *ih,
  				 struct inotify_watch *watch)
0eeca2830   Robert Love   [PATCH] inotify
130
131
132
133
  {
  	int ret;
  
  	do {
2d9048e20   Amy Griffis   [PATCH] inotify (...
134
  		if (unlikely(!idr_pre_get(&ih->idr, GFP_KERNEL)))
0eeca2830   Robert Love   [PATCH] inotify
135
  			return -ENOSPC;
2d9048e20   Amy Griffis   [PATCH] inotify (...
136
  		ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd);
0eeca2830   Robert Love   [PATCH] inotify
137
  	} while (ret == -EAGAIN);
2d9048e20   Amy Griffis   [PATCH] inotify (...
138
139
  	if (likely(!ret))
  		ih->last_wd = watch->wd;
0eeca2830   Robert Love   [PATCH] inotify
140

2d9048e20   Amy Griffis   [PATCH] inotify (...
141
  	return ret;
0eeca2830   Robert Love   [PATCH] inotify
142
143
144
  }
  
  /*
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
   * inotify_inode_watched - returns nonzero if there are watches on this inode
   * and zero otherwise.  We call this lockless, we do not care if we race.
   */
  static inline int inotify_inode_watched(struct inode *inode)
  {
  	return !list_empty(&inode->inotify_watches);
  }
  
  /*
   * Get child dentry flag into synch with parent inode.
   * Flag should always be clear for negative dentrys.
   */
  static void set_dentry_child_flags(struct inode *inode, int watched)
  {
  	struct dentry *alias;
  
  	spin_lock(&dcache_lock);
  	list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  		struct dentry *child;
  
  		list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {
0d71bd599   Nick Piggin   inotify: remove d...
166
  			if (!child->d_inode)
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
167
  				continue;
0d71bd599   Nick Piggin   inotify: remove d...
168

c32ccd87b   Nick Piggin   [PATCH] inotify: ...
169
  			spin_lock(&child->d_lock);
0d71bd599   Nick Piggin   inotify: remove d...
170
  			if (watched)
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
171
  				child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
0d71bd599   Nick Piggin   inotify: remove d...
172
173
  			else
  				child->d_flags &=~DCACHE_INOTIFY_PARENT_WATCHED;
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
174
175
176
177
178
179
180
  			spin_unlock(&child->d_lock);
  		}
  	}
  	spin_unlock(&dcache_lock);
  }
  
  /*
2d9048e20   Amy Griffis   [PATCH] inotify (...
181
182
   * inotify_find_handle - find the watch associated with the given inode and
   * handle
0eeca2830   Robert Love   [PATCH] inotify
183
   *
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
184
   * Callers must hold inode->inotify_mutex.
0eeca2830   Robert Love   [PATCH] inotify
185
   */
2d9048e20   Amy Griffis   [PATCH] inotify (...
186
187
  static struct inotify_watch *inode_find_handle(struct inode *inode,
  					       struct inotify_handle *ih)
0eeca2830   Robert Love   [PATCH] inotify
188
189
190
191
  {
  	struct inotify_watch *watch;
  
  	list_for_each_entry(watch, &inode->inotify_watches, i_list) {
2d9048e20   Amy Griffis   [PATCH] inotify (...
192
  		if (watch->ih == ih)
0eeca2830   Robert Love   [PATCH] inotify
193
194
195
196
197
198
199
  			return watch;
  	}
  
  	return NULL;
  }
  
  /*
3ca10067f   Amy Griffis   [PATCH] inotify (...
200
   * remove_watch_no_event - remove watch without the IN_IGNORED event.
2d9048e20   Amy Griffis   [PATCH] inotify (...
201
202
   *
   * Callers must hold both inode->inotify_mutex and ih->mutex.
0eeca2830   Robert Love   [PATCH] inotify
203
204
   */
  static void remove_watch_no_event(struct inotify_watch *watch,
2d9048e20   Amy Griffis   [PATCH] inotify (...
205
  				  struct inotify_handle *ih)
0eeca2830   Robert Love   [PATCH] inotify
206
207
  {
  	list_del(&watch->i_list);
2d9048e20   Amy Griffis   [PATCH] inotify (...
208
  	list_del(&watch->h_list);
0eeca2830   Robert Love   [PATCH] inotify
209

c32ccd87b   Nick Piggin   [PATCH] inotify: ...
210
211
  	if (!inotify_inode_watched(watch->inode))
  		set_dentry_child_flags(watch->inode, 0);
2d9048e20   Amy Griffis   [PATCH] inotify (...
212
  	idr_remove(&ih->idr, watch->wd);
0eeca2830   Robert Love   [PATCH] inotify
213
  }
3ca10067f   Amy Griffis   [PATCH] inotify (...
214
215
216
217
218
219
  /**
   * inotify_remove_watch_locked - Remove a watch from both the handle and the
   * inode.  Sends the IN_IGNORED event signifying that the inode is no longer
   * watched.  May be invoked from a caller's event handler.
   * @ih: inotify handle associated with watch
   * @watch: watch to remove
0eeca2830   Robert Love   [PATCH] inotify
220
   *
2d9048e20   Amy Griffis   [PATCH] inotify (...
221
   * Callers must hold both inode->inotify_mutex and ih->mutex.
0eeca2830   Robert Love   [PATCH] inotify
222
   */
3ca10067f   Amy Griffis   [PATCH] inotify (...
223
224
  void inotify_remove_watch_locked(struct inotify_handle *ih,
  				 struct inotify_watch *watch)
0eeca2830   Robert Love   [PATCH] inotify
225
  {
2d9048e20   Amy Griffis   [PATCH] inotify (...
226
  	remove_watch_no_event(watch, ih);
7c2977228   Amy Griffis   [PATCH] inotify (...
227
  	ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL);
0eeca2830   Robert Love   [PATCH] inotify
228
  }
3ca10067f   Amy Griffis   [PATCH] inotify (...
229
  EXPORT_SYMBOL_GPL(inotify_remove_watch_locked);
0eeca2830   Robert Love   [PATCH] inotify
230

2d9048e20   Amy Griffis   [PATCH] inotify (...
231
  /* Kernel API for producing events */
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
232

0eeca2830   Robert Love   [PATCH] inotify
233
  /*
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
234
   * inotify_d_instantiate - instantiate dcache entry for inode
0eeca2830   Robert Love   [PATCH] inotify
235
   */
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
236
  void inotify_d_instantiate(struct dentry *entry, struct inode *inode)
0eeca2830   Robert Love   [PATCH] inotify
237
  {
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
238
239
240
241
  	struct dentry *parent;
  
  	if (!inode)
  		return;
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
242
243
  	spin_lock(&entry->d_lock);
  	parent = entry->d_parent;
091e881d0   Arnd Bergmann   [PATCH] inotify: ...
244
  	if (parent->d_inode && inotify_inode_watched(parent->d_inode))
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
245
246
  		entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
  	spin_unlock(&entry->d_lock);
0eeca2830   Robert Love   [PATCH] inotify
247
  }
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
248
249
250
251
252
253
254
255
256
257
258
259
260
  /*
   * inotify_d_move - dcache entry has been moved
   */
  void inotify_d_move(struct dentry *entry)
  {
  	struct dentry *parent;
  
  	parent = entry->d_parent;
  	if (inotify_inode_watched(parent->d_inode))
  		entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
  	else
  		entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
  }
0eeca2830   Robert Love   [PATCH] inotify
261
262
263
264
265
266
267
  
  /**
   * inotify_inode_queue_event - queue an event to all watches on this inode
   * @inode: inode event is originating from
   * @mask: event mask describing this event
   * @cookie: cookie for synchronization, or zero
   * @name: filename, if any
7c2977228   Amy Griffis   [PATCH] inotify (...
268
   * @n_inode: inode associated with name
0eeca2830   Robert Love   [PATCH] inotify
269
270
   */
  void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
7c2977228   Amy Griffis   [PATCH] inotify (...
271
  			       const char *name, struct inode *n_inode)
0eeca2830   Robert Love   [PATCH] inotify
272
273
274
275
276
  {
  	struct inotify_watch *watch, *next;
  
  	if (!inotify_inode_watched(inode))
  		return;
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
277
  	mutex_lock(&inode->inotify_mutex);
0eeca2830   Robert Love   [PATCH] inotify
278
279
280
  	list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
  		u32 watch_mask = watch->mask;
  		if (watch_mask & mask) {
2d9048e20   Amy Griffis   [PATCH] inotify (...
281
282
  			struct inotify_handle *ih= watch->ih;
  			mutex_lock(&ih->mutex);
0eeca2830   Robert Love   [PATCH] inotify
283
  			if (watch_mask & IN_ONESHOT)
2d9048e20   Amy Griffis   [PATCH] inotify (...
284
  				remove_watch_no_event(watch, ih);
7c2977228   Amy Griffis   [PATCH] inotify (...
285
286
  			ih->in_ops->handle_event(watch, watch->wd, mask, cookie,
  						 name, n_inode);
2d9048e20   Amy Griffis   [PATCH] inotify (...
287
  			mutex_unlock(&ih->mutex);
0eeca2830   Robert Love   [PATCH] inotify
288
289
  		}
  	}
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
290
  	mutex_unlock(&inode->inotify_mutex);
0eeca2830   Robert Love   [PATCH] inotify
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  }
  EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
  
  /**
   * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
   * @dentry: the dentry in question, we queue against this dentry's parent
   * @mask: event mask describing this event
   * @cookie: cookie for synchronization, or zero
   * @name: filename, if any
   */
  void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
  				       u32 cookie, const char *name)
  {
  	struct dentry *parent;
  	struct inode *inode;
c32ccd87b   Nick Piggin   [PATCH] inotify: ...
306
  	if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))
820249baf   John McCutchan   [PATCH] inotify s...
307
  		return;
0eeca2830   Robert Love   [PATCH] inotify
308
309
310
311
312
313
314
  	spin_lock(&dentry->d_lock);
  	parent = dentry->d_parent;
  	inode = parent->d_inode;
  
  	if (inotify_inode_watched(inode)) {
  		dget(parent);
  		spin_unlock(&dentry->d_lock);
7c2977228   Amy Griffis   [PATCH] inotify (...
315
316
  		inotify_inode_queue_event(inode, mask, cookie, name,
  					  dentry->d_inode);
0eeca2830   Robert Love   [PATCH] inotify
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
  		dput(parent);
  	} else
  		spin_unlock(&dentry->d_lock);
  }
  EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
  
  /**
   * inotify_get_cookie - return a unique cookie for use in synchronizing events.
   */
  u32 inotify_get_cookie(void)
  {
  	return atomic_inc_return(&inotify_cookie);
  }
  EXPORT_SYMBOL_GPL(inotify_get_cookie);
  
  /**
   * inotify_unmount_inodes - an sb is unmounting.  handle any watched inodes.
   * @list: list of inodes being unmounted (sb->s_inodes)
   *
   * Called with inode_lock held, protecting the unmounting super block's list
f24075bd0   Ingo Molnar   [PATCH] sem2mutex...
337
   * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
0eeca2830   Robert Love   [PATCH] inotify
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
   * We temporarily drop inode_lock, however, and CAN block.
   */
  void inotify_unmount_inodes(struct list_head *list)
  {
  	struct inode *inode, *next_i, *need_iput = NULL;
  
  	list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
  		struct inotify_watch *watch, *next_w;
  		struct inode *need_iput_tmp;
  		struct list_head *watches;
  
  		/*
  		 * If i_count is zero, the inode cannot have any watches and
  		 * doing an __iget/iput with MS_ACTIVE clear would actually
  		 * evict all inodes with zero i_count from icache which is
  		 * unnecessarily violent and may in fact be illegal to do.
  		 */
  		if (!atomic_read(&inode->i_count))
  			continue;
  
  		/*
  		 * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or
  		 * I_WILL_FREE which is fine because by that point the inode
  		 * cannot have any associated watches.
  		 */
  		if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE))
  			continue;
  
  		need_iput_tmp = need_iput;
  		need_iput = NULL;
3ca10067f   Amy Griffis   [PATCH] inotify (...
368
  		/* In case inotify_remove_watch_locked() drops a reference. */
0eeca2830   Robert Love   [PATCH] inotify
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
  		if (inode != need_iput_tmp)
  			__iget(inode);
  		else
  			need_iput_tmp = NULL;
  		/* In case the dropping of a reference would nuke next_i. */
  		if ((&next_i->i_sb_list != list) &&
  				atomic_read(&next_i->i_count) &&
  				!(next_i->i_state & (I_CLEAR | I_FREEING |
  					I_WILL_FREE))) {
  			__iget(next_i);
  			need_iput = next_i;
  		}
  
  		/*
  		 * We can safely drop inode_lock here because we hold
  		 * references on both inode and next_i.  Also no new inodes
  		 * will be added since the umount has begun.  Finally,
f24075bd0   Ingo Molnar   [PATCH] sem2mutex...
386
  		 * iprune_mutex keeps shrink_icache_memory() away.
0eeca2830   Robert Love   [PATCH] inotify
387
388
389
390
391
392
393
  		 */
  		spin_unlock(&inode_lock);
  
  		if (need_iput_tmp)
  			iput(need_iput_tmp);
  
  		/* for each watch, send IN_UNMOUNT and then remove it */
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
394
  		mutex_lock(&inode->inotify_mutex);
0eeca2830   Robert Love   [PATCH] inotify
395
396
  		watches = &inode->inotify_watches;
  		list_for_each_entry_safe(watch, next_w, watches, i_list) {
2d9048e20   Amy Griffis   [PATCH] inotify (...
397
398
399
  			struct inotify_handle *ih= watch->ih;
  			mutex_lock(&ih->mutex);
  			ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,
7c2977228   Amy Griffis   [PATCH] inotify (...
400
  						 NULL, NULL);
3ca10067f   Amy Griffis   [PATCH] inotify (...
401
  			inotify_remove_watch_locked(ih, watch);
2d9048e20   Amy Griffis   [PATCH] inotify (...
402
  			mutex_unlock(&ih->mutex);
0eeca2830   Robert Love   [PATCH] inotify
403
  		}
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
404
  		mutex_unlock(&inode->inotify_mutex);
0eeca2830   Robert Love   [PATCH] inotify
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  		iput(inode);		
  
  		spin_lock(&inode_lock);
  	}
  }
  EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
  
  /**
   * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
   * @inode: inode that is about to be removed
   */
  void inotify_inode_is_dead(struct inode *inode)
  {
  	struct inotify_watch *watch, *next;
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
419
  	mutex_lock(&inode->inotify_mutex);
0eeca2830   Robert Love   [PATCH] inotify
420
  	list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
2d9048e20   Amy Griffis   [PATCH] inotify (...
421
422
  		struct inotify_handle *ih = watch->ih;
  		mutex_lock(&ih->mutex);
3ca10067f   Amy Griffis   [PATCH] inotify (...
423
  		inotify_remove_watch_locked(ih, watch);
2d9048e20   Amy Griffis   [PATCH] inotify (...
424
  		mutex_unlock(&ih->mutex);
0eeca2830   Robert Love   [PATCH] inotify
425
  	}
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
426
  	mutex_unlock(&inode->inotify_mutex);
0eeca2830   Robert Love   [PATCH] inotify
427
428
  }
  EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
2d9048e20   Amy Griffis   [PATCH] inotify (...
429
  /* Kernel Consumer API */
0eeca2830   Robert Love   [PATCH] inotify
430

2d9048e20   Amy Griffis   [PATCH] inotify (...
431
432
433
434
435
  /**
   * inotify_init - allocate and initialize an inotify instance
   * @ops: caller's inotify operations
   */
  struct inotify_handle *inotify_init(const struct inotify_operations *ops)
0eeca2830   Robert Love   [PATCH] inotify
436
  {
2d9048e20   Amy Griffis   [PATCH] inotify (...
437
  	struct inotify_handle *ih;
0eeca2830   Robert Love   [PATCH] inotify
438

2d9048e20   Amy Griffis   [PATCH] inotify (...
439
440
441
  	ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);
  	if (unlikely(!ih))
  		return ERR_PTR(-ENOMEM);
0eeca2830   Robert Love   [PATCH] inotify
442

2d9048e20   Amy Griffis   [PATCH] inotify (...
443
444
445
446
447
448
449
  	idr_init(&ih->idr);
  	INIT_LIST_HEAD(&ih->watches);
  	mutex_init(&ih->mutex);
  	ih->last_wd = 0;
  	ih->in_ops = ops;
  	atomic_set(&ih->count, 0);
  	get_inotify_handle(ih);
0eeca2830   Robert Love   [PATCH] inotify
450

2d9048e20   Amy Griffis   [PATCH] inotify (...
451
  	return ih;
0eeca2830   Robert Love   [PATCH] inotify
452
  }
2d9048e20   Amy Griffis   [PATCH] inotify (...
453
  EXPORT_SYMBOL_GPL(inotify_init);
0eeca2830   Robert Love   [PATCH] inotify
454

2d9048e20   Amy Griffis   [PATCH] inotify (...
455
  /**
a9dc971d3   Amy Griffis   [PATCH] inotify (...
456
457
458
459
460
461
462
463
464
465
466
467
468
   * inotify_init_watch - initialize an inotify watch
   * @watch: watch to initialize
   */
  void inotify_init_watch(struct inotify_watch *watch)
  {
  	INIT_LIST_HEAD(&watch->h_list);
  	INIT_LIST_HEAD(&watch->i_list);
  	atomic_set(&watch->count, 0);
  	get_inotify_watch(watch); /* initial get */
  }
  EXPORT_SYMBOL_GPL(inotify_init_watch);
  
  /**
2d9048e20   Amy Griffis   [PATCH] inotify (...
469
470
471
472
   * inotify_destroy - clean up and destroy an inotify instance
   * @ih: inotify handle
   */
  void inotify_destroy(struct inotify_handle *ih)
0eeca2830   Robert Love   [PATCH] inotify
473
  {
0eeca2830   Robert Love   [PATCH] inotify
474
  	/*
2d9048e20   Amy Griffis   [PATCH] inotify (...
475
  	 * Destroy all of the watches for this handle. Unfortunately, not very
0eeca2830   Robert Love   [PATCH] inotify
476
477
  	 * pretty.  We cannot do a simple iteration over the list, because we
  	 * do not know the inode until we iterate to the watch.  But we need to
2d9048e20   Amy Griffis   [PATCH] inotify (...
478
  	 * hold inode->inotify_mutex before ih->mutex.  The following works.
0eeca2830   Robert Love   [PATCH] inotify
479
480
481
482
483
  	 */
  	while (1) {
  		struct inotify_watch *watch;
  		struct list_head *watches;
  		struct inode *inode;
2d9048e20   Amy Griffis   [PATCH] inotify (...
484
485
  		mutex_lock(&ih->mutex);
  		watches = &ih->watches;
0eeca2830   Robert Love   [PATCH] inotify
486
  		if (list_empty(watches)) {
2d9048e20   Amy Griffis   [PATCH] inotify (...
487
  			mutex_unlock(&ih->mutex);
0eeca2830   Robert Love   [PATCH] inotify
488
489
  			break;
  		}
b5e618181   Pavel Emelianov   Introduce a handy...
490
  		watch = list_first_entry(watches, struct inotify_watch, h_list);
0eeca2830   Robert Love   [PATCH] inotify
491
  		get_inotify_watch(watch);
2d9048e20   Amy Griffis   [PATCH] inotify (...
492
  		mutex_unlock(&ih->mutex);
0eeca2830   Robert Love   [PATCH] inotify
493
494
  
  		inode = watch->inode;
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
495
  		mutex_lock(&inode->inotify_mutex);
2d9048e20   Amy Griffis   [PATCH] inotify (...
496
  		mutex_lock(&ih->mutex);
66055a4e7   Amy Griffis   [PATCH] fix race ...
497
498
  
  		/* make sure we didn't race with another list removal */
2d9048e20   Amy Griffis   [PATCH] inotify (...
499
500
501
502
  		if (likely(idr_find(&ih->idr, watch->wd))) {
  			remove_watch_no_event(watch, ih);
  			put_inotify_watch(watch);
  		}
66055a4e7   Amy Griffis   [PATCH] fix race ...
503

2d9048e20   Amy Griffis   [PATCH] inotify (...
504
  		mutex_unlock(&ih->mutex);
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
505
  		mutex_unlock(&inode->inotify_mutex);
0eeca2830   Robert Love   [PATCH] inotify
506
507
  		put_inotify_watch(watch);
  	}
2d9048e20   Amy Griffis   [PATCH] inotify (...
508
509
  	/* free this handle: the put matching the get in inotify_init() */
  	put_inotify_handle(ih);
0eeca2830   Robert Love   [PATCH] inotify
510
  }
2d9048e20   Amy Griffis   [PATCH] inotify (...
511
  EXPORT_SYMBOL_GPL(inotify_destroy);
0eeca2830   Robert Love   [PATCH] inotify
512

2d9048e20   Amy Griffis   [PATCH] inotify (...
513
  /**
a9dc971d3   Amy Griffis   [PATCH] inotify (...
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
   * inotify_find_watch - find an existing watch for an (ih,inode) pair
   * @ih: inotify handle
   * @inode: inode to watch
   * @watchp: pointer to existing inotify_watch
   *
   * Caller must pin given inode (via nameidata).
   */
  s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode,
  		       struct inotify_watch **watchp)
  {
  	struct inotify_watch *old;
  	int ret = -ENOENT;
  
  	mutex_lock(&inode->inotify_mutex);
  	mutex_lock(&ih->mutex);
  
  	old = inode_find_handle(inode, ih);
  	if (unlikely(old)) {
  		get_inotify_watch(old); /* caller must put watch */
  		*watchp = old;
  		ret = old->wd;
  	}
  
  	mutex_unlock(&ih->mutex);
  	mutex_unlock(&inode->inotify_mutex);
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(inotify_find_watch);
  
  /**
2d9048e20   Amy Griffis   [PATCH] inotify (...
545
546
547
548
   * inotify_find_update_watch - find and update the mask of an existing watch
   * @ih: inotify handle
   * @inode: inode's watch to update
   * @mask: mask of events to watch
0eeca2830   Robert Love   [PATCH] inotify
549
   *
2d9048e20   Amy Griffis   [PATCH] inotify (...
550
   * Caller must pin given inode (via nameidata).
0eeca2830   Robert Love   [PATCH] inotify
551
   */
2d9048e20   Amy Griffis   [PATCH] inotify (...
552
553
  s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,
  			      u32 mask)
0eeca2830   Robert Love   [PATCH] inotify
554
  {
2d9048e20   Amy Griffis   [PATCH] inotify (...
555
556
557
  	struct inotify_watch *old;
  	int mask_add = 0;
  	int ret;
0eeca2830   Robert Love   [PATCH] inotify
558

2d9048e20   Amy Griffis   [PATCH] inotify (...
559
560
561
562
563
564
  	if (mask & IN_MASK_ADD)
  		mask_add = 1;
  
  	/* don't allow invalid bits: we don't want flags set */
  	mask &= IN_ALL_EVENTS | IN_ONESHOT;
  	if (unlikely(!mask))
0eeca2830   Robert Love   [PATCH] inotify
565
  		return -EINVAL;
0eeca2830   Robert Love   [PATCH] inotify
566

d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
567
  	mutex_lock(&inode->inotify_mutex);
2d9048e20   Amy Griffis   [PATCH] inotify (...
568
  	mutex_lock(&ih->mutex);
0eeca2830   Robert Love   [PATCH] inotify
569

2d9048e20   Amy Griffis   [PATCH] inotify (...
570
571
572
573
574
575
576
577
  	/*
  	 * Handle the case of re-adding a watch on an (inode,ih) pair that we
  	 * are already watching.  We just update the mask and return its wd.
  	 */
  	old = inode_find_handle(inode, ih);
  	if (unlikely(!old)) {
  		ret = -ENOENT;
  		goto out;
0eeca2830   Robert Love   [PATCH] inotify
578
  	}
2d9048e20   Amy Griffis   [PATCH] inotify (...
579
580
581
582
583
584
585
586
  	if (mask_add)
  		old->mask |= mask;
  	else
  		old->mask = mask;
  	ret = old->wd;
  out:
  	mutex_unlock(&ih->mutex);
  	mutex_unlock(&inode->inotify_mutex);
0eeca2830   Robert Love   [PATCH] inotify
587
588
  	return ret;
  }
2d9048e20   Amy Griffis   [PATCH] inotify (...
589
  EXPORT_SYMBOL_GPL(inotify_find_update_watch);
0eeca2830   Robert Love   [PATCH] inotify
590

2d9048e20   Amy Griffis   [PATCH] inotify (...
591
592
593
594
595
596
597
598
599
600
601
602
603
  /**
   * inotify_add_watch - add a watch to an inotify instance
   * @ih: inotify handle
   * @watch: caller allocated watch structure
   * @inode: inode to watch
   * @mask: mask of events to watch
   *
   * Caller must pin given inode (via nameidata).
   * Caller must ensure it only calls inotify_add_watch() once per watch.
   * Calls inotify_handle_get_wd() so may sleep.
   */
  s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch,
  		      struct inode *inode, u32 mask)
0eeca2830   Robert Love   [PATCH] inotify
604
  {
2d9048e20   Amy Griffis   [PATCH] inotify (...
605
  	int ret = 0;
d599e36a9   Nick Piggin   inotify: fix race
606
  	int newly_watched;
0eeca2830   Robert Love   [PATCH] inotify
607

2d9048e20   Amy Griffis   [PATCH] inotify (...
608
609
610
611
612
  	/* don't allow invalid bits: we don't want flags set */
  	mask &= IN_ALL_EVENTS | IN_ONESHOT;
  	if (unlikely(!mask))
  		return -EINVAL;
  	watch->mask = mask;
783bc29bb   Robert Love   [PATCH] inotify: ...
613

2d9048e20   Amy Griffis   [PATCH] inotify (...
614
615
  	mutex_lock(&inode->inotify_mutex);
  	mutex_lock(&ih->mutex);
8140a5005   John McCutchan   [PATCH] inotify: ...
616

2d9048e20   Amy Griffis   [PATCH] inotify (...
617
618
  	/* Initialize a new watch */
  	ret = inotify_handle_get_wd(ih, watch);
b680716ed   Robert Love   [PATCH] inotify: ...
619
  	if (unlikely(ret))
2d9048e20   Amy Griffis   [PATCH] inotify (...
620
621
  		goto out;
  	ret = watch->wd;
0eeca2830   Robert Love   [PATCH] inotify
622

2d9048e20   Amy Griffis   [PATCH] inotify (...
623
624
625
  	/* save a reference to handle and bump the count to make it official */
  	get_inotify_handle(ih);
  	watch->ih = ih;
0eeca2830   Robert Love   [PATCH] inotify
626
627
  
  	/*
2d9048e20   Amy Griffis   [PATCH] inotify (...
628
629
  	 * Save a reference to the inode and bump the ref count to make it
  	 * official.  We hold a reference to nameidata, which makes this safe.
0eeca2830   Robert Love   [PATCH] inotify
630
  	 */
2d9048e20   Amy Griffis   [PATCH] inotify (...
631
  	watch->inode = igrab(inode);
0eeca2830   Robert Love   [PATCH] inotify
632

2d9048e20   Amy Griffis   [PATCH] inotify (...
633
  	/* Add the watch to the handle's and the inode's list */
d599e36a9   Nick Piggin   inotify: fix race
634
  	newly_watched = !inotify_inode_watched(inode);
2d9048e20   Amy Griffis   [PATCH] inotify (...
635
  	list_add(&watch->h_list, &ih->watches);
0eeca2830   Robert Love   [PATCH] inotify
636
  	list_add(&watch->i_list, &inode->inotify_watches);
d599e36a9   Nick Piggin   inotify: fix race
637
638
639
640
641
642
643
  	/*
  	 * Set child flags _after_ adding the watch, so there is no race
  	 * windows where newly instantiated children could miss their parent's
  	 * watched flag.
  	 */
  	if (newly_watched)
  		set_dentry_child_flags(inode, 1);
0eeca2830   Robert Love   [PATCH] inotify
644
  out:
2d9048e20   Amy Griffis   [PATCH] inotify (...
645
  	mutex_unlock(&ih->mutex);
d4f9af9da   Ingo Molnar   [PATCH] sem2mutex...
646
  	mutex_unlock(&inode->inotify_mutex);
0eeca2830   Robert Love   [PATCH] inotify
647
648
  	return ret;
  }
2d9048e20   Amy Griffis   [PATCH] inotify (...
649
  EXPORT_SYMBOL_GPL(inotify_add_watch);
0eeca2830   Robert Love   [PATCH] inotify
650

2d9048e20   Amy Griffis   [PATCH] inotify (...
651
  /**
b9efe8a23   Al Viro   [PATCH] new helpe...
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
   * inotify_clone_watch - put the watch next to existing one
   * @old: already installed watch
   * @new: new watch
   *
   * Caller must hold the inotify_mutex of inode we are dealing with;
   * it is expected to remove the old watch before unlocking the inode.
   */
  s32 inotify_clone_watch(struct inotify_watch *old, struct inotify_watch *new)
  {
  	struct inotify_handle *ih = old->ih;
  	int ret = 0;
  
  	new->mask = old->mask;
  	new->ih = ih;
  
  	mutex_lock(&ih->mutex);
  
  	/* Initialize a new watch */
  	ret = inotify_handle_get_wd(ih, new);
  	if (unlikely(ret))
  		goto out;
  	ret = new->wd;
  
  	get_inotify_handle(ih);
  
  	new->inode = igrab(old->inode);
  
  	list_add(&new->h_list, &ih->watches);
  	list_add(&new->i_list, &old->inode->inotify_watches);
  out:
  	mutex_unlock(&ih->mutex);
  	return ret;
  }
455434d45   Al Viro   [PATCH] new helpe...
685
686
687
688
689
690
691
  void inotify_evict_watch(struct inotify_watch *watch)
  {
  	get_inotify_watch(watch);
  	mutex_lock(&watch->ih->mutex);
  	inotify_remove_watch_locked(watch->ih, watch);
  	mutex_unlock(&watch->ih->mutex);
  }
b9efe8a23   Al Viro   [PATCH] new helpe...
692
  /**
2d9048e20   Amy Griffis   [PATCH] inotify (...
693
694
695
696
697
698
699
   * inotify_rm_wd - remove a watch from an inotify instance
   * @ih: inotify handle
   * @wd: watch descriptor to remove
   *
   * Can sleep.
   */
  int inotify_rm_wd(struct inotify_handle *ih, u32 wd)
0eeca2830   Robert Love   [PATCH] inotify
700
  {
2d9048e20   Amy Griffis   [PATCH] inotify (...
701
702
  	struct inotify_watch *watch;
  	struct inode *inode;
783bc29bb   Robert Love   [PATCH] inotify: ...
703

2d9048e20   Amy Griffis   [PATCH] inotify (...
704
705
706
707
708
  	mutex_lock(&ih->mutex);
  	watch = idr_find(&ih->idr, wd);
  	if (unlikely(!watch)) {
  		mutex_unlock(&ih->mutex);
  		return -EINVAL;
783bc29bb   Robert Love   [PATCH] inotify: ...
709
  	}
2d9048e20   Amy Griffis   [PATCH] inotify (...
710
711
712
  	get_inotify_watch(watch);
  	inode = watch->inode;
  	mutex_unlock(&ih->mutex);
783bc29bb   Robert Love   [PATCH] inotify: ...
713

2d9048e20   Amy Griffis   [PATCH] inotify (...
714
715
  	mutex_lock(&inode->inotify_mutex);
  	mutex_lock(&ih->mutex);
9a556e890   Robert Love   [PATCH] inotify: ...
716

2d9048e20   Amy Griffis   [PATCH] inotify (...
717
718
  	/* make sure that we did not race */
  	if (likely(idr_find(&ih->idr, wd) == watch))
3ca10067f   Amy Griffis   [PATCH] inotify (...
719
  		inotify_remove_watch_locked(ih, watch);
0eeca2830   Robert Love   [PATCH] inotify
720

2d9048e20   Amy Griffis   [PATCH] inotify (...
721
722
723
  	mutex_unlock(&ih->mutex);
  	mutex_unlock(&inode->inotify_mutex);
  	put_inotify_watch(watch);
0eeca2830   Robert Love   [PATCH] inotify
724

2d9048e20   Amy Griffis   [PATCH] inotify (...
725
726
727
  	return 0;
  }
  EXPORT_SYMBOL_GPL(inotify_rm_wd);
0eeca2830   Robert Love   [PATCH] inotify
728

a9dc971d3   Amy Griffis   [PATCH] inotify (...
729
730
731
732
733
734
735
736
737
738
739
740
741
  /**
   * inotify_rm_watch - remove a watch from an inotify instance
   * @ih: inotify handle
   * @watch: watch to remove
   *
   * Can sleep.
   */
  int inotify_rm_watch(struct inotify_handle *ih,
  		     struct inotify_watch *watch)
  {
  	return inotify_rm_wd(ih, watch->wd);
  }
  EXPORT_SYMBOL_GPL(inotify_rm_watch);
0eeca2830   Robert Love   [PATCH] inotify
742
  /*
2d9048e20   Amy Griffis   [PATCH] inotify (...
743
   * inotify_setup - core initialization function
0eeca2830   Robert Love   [PATCH] inotify
744
   */
b680716ed   Robert Love   [PATCH] inotify: ...
745
  static int __init inotify_setup(void)
0eeca2830   Robert Love   [PATCH] inotify
746
  {
0eeca2830   Robert Love   [PATCH] inotify
747
  	atomic_set(&inotify_cookie, 0);
0eeca2830   Robert Love   [PATCH] inotify
748
749
  	return 0;
  }
b680716ed   Robert Love   [PATCH] inotify: ...
750
  module_init(inotify_setup);