inotify.c 25.1 KB
Newer Older
R
Robert Love 已提交
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>
 *
8 9
 * Kernel API added by: Amy Griffis <amy.griffis@hp.com>
 *
R
Robert Love 已提交
10
 * Copyright (C) 2005 John McCutchan
11
 * Copyright 2006 Hewlett-Packard Development Company, L.P.
R
Robert Love 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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>
#include <linux/spinlock.h>
#include <linux/idr.h>
#include <linux/slab.h>
#include <linux/fs.h>
30
#include <linux/sched.h>
R
Robert Love 已提交
31 32 33 34 35 36 37 38 39 40 41
#include <linux/init.h>
#include <linux/list.h>
#include <linux/writeback.h>
#include <linux/inotify.h>

static atomic_t inotify_cookie;

/*
 * Lock ordering:
 *
 * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
I
Ingo Molnar 已提交
42
 * iprune_mutex (synchronize shrink_icache_memory())
R
Robert Love 已提交
43
 * 	inode_lock (protects the super_block->s_inodes list)
I
Ingo Molnar 已提交
44
 * 	inode->inotify_mutex (protects inode->inotify_watches and watches->i_list)
45 46 47 48 49 50
 * 		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.
R
Robert Love 已提交
51 52 53
 */

/*
54
 * Lifetimes of the three main data structures--inotify_handle, inode, and
R
Robert Love 已提交
55 56
 * inotify_watch--are managed by reference count.
 *
57 58 59
 * 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().
R
Robert Love 已提交
60
 *
61 62 63 64 65 66
 * 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.
R
Robert Love 已提交
67 68
 *
 * inode: Pinned so long as the inode is associated with a watch, from
69
 * inotify_add_watch() to the final put_inotify_watch().
R
Robert Love 已提交
70 71 72
 */

/*
73
 * struct inotify_handle - represents an inotify instance
R
Robert Love 已提交
74
 *
I
Ingo Molnar 已提交
75
 * This structure is protected by the mutex 'mutex'.
R
Robert Love 已提交
76
 */
77
struct inotify_handle {
R
Robert Love 已提交
78
	struct idr		idr;		/* idr mapping wd -> watch */
I
Ingo Molnar 已提交
79
	struct mutex		mutex;		/* protects this bad boy */
R
Robert Love 已提交
80 81
	struct list_head	watches;	/* list of watches */
	atomic_t		count;		/* reference count */
82
	u32			last_wd;	/* the last wd allocated */
83
	const struct inotify_operations *in_ops; /* inotify caller operations */
R
Robert Love 已提交
84 85
};

86
static inline void get_inotify_handle(struct inotify_handle *ih)
R
Robert Love 已提交
87
{
88
	atomic_inc(&ih->count);
R
Robert Love 已提交
89 90
}

91
static inline void put_inotify_handle(struct inotify_handle *ih)
R
Robert Love 已提交
92
{
93 94 95
	if (atomic_dec_and_test(&ih->count)) {
		idr_destroy(&ih->idr);
		kfree(ih);
R
Robert Love 已提交
96 97 98
	}
}

99 100 101 102 103
/**
 * get_inotify_watch - grab a reference to an inotify_watch
 * @watch: watch to grab
 */
void get_inotify_watch(struct inotify_watch *watch)
R
Robert Love 已提交
104 105 106
{
	atomic_inc(&watch->count);
}
107
EXPORT_SYMBOL_GPL(get_inotify_watch);
R
Robert Love 已提交
108

A
Al Viro 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122
int pin_inotify_watch(struct inotify_watch *watch)
{
	struct super_block *sb = watch->inode->i_sb;
	spin_lock(&sb_lock);
	if (sb->s_count >= S_BIAS) {
		atomic_inc(&sb->s_active);
		spin_unlock(&sb_lock);
		atomic_inc(&watch->count);
		return 1;
	}
	spin_unlock(&sb_lock);
	return 0;
}

123
/**
R
Robert Love 已提交
124
 * put_inotify_watch - decrements the ref count on a given watch.  cleans up
125 126 127
 * watch references if the count reaches zero.  inotify_watch is freed by
 * inotify callers via the destroy_watch() op.
 * @watch: watch to release
R
Robert Love 已提交
128
 */
129
void put_inotify_watch(struct inotify_watch *watch)
R
Robert Love 已提交
130 131
{
	if (atomic_dec_and_test(&watch->count)) {
132
		struct inotify_handle *ih = watch->ih;
R
Robert Love 已提交
133

134 135 136
		iput(watch->inode);
		ih->in_ops->destroy_watch(watch);
		put_inotify_handle(ih);
R
Robert Love 已提交
137 138
	}
}
139
EXPORT_SYMBOL_GPL(put_inotify_watch);
R
Robert Love 已提交
140

A
Al Viro 已提交
141 142 143 144 145 146 147
void unpin_inotify_watch(struct inotify_watch *watch)
{
	struct super_block *sb = watch->inode->i_sb;
	put_inotify_watch(watch);
	deactivate_super(sb);
}

R
Robert Love 已提交
148
/*
149
 * inotify_handle_get_wd - returns the next WD for use by the given handle
R
Robert Love 已提交
150
 *
151
 * Callers must hold ih->mutex.  This function can sleep.
R
Robert Love 已提交
152
 */
153 154
static int inotify_handle_get_wd(struct inotify_handle *ih,
				 struct inotify_watch *watch)
R
Robert Love 已提交
155 156 157 158
{
	int ret;

	do {
159
		if (unlikely(!idr_pre_get(&ih->idr, GFP_NOFS)))
R
Robert Love 已提交
160
			return -ENOSPC;
161
		ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd);
R
Robert Love 已提交
162 163
	} while (ret == -EAGAIN);

164 165
	if (likely(!ret))
		ih->last_wd = watch->wd;
R
Robert Love 已提交
166

167
	return ret;
R
Robert Love 已提交
168 169
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/*
 * 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) {
N
Nick Piggin 已提交
192
			if (!child->d_inode)
193
				continue;
N
Nick Piggin 已提交
194

195
			spin_lock(&child->d_lock);
N
Nick Piggin 已提交
196
			if (watched)
197
				child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
N
Nick Piggin 已提交
198 199
			else
				child->d_flags &=~DCACHE_INOTIFY_PARENT_WATCHED;
200 201 202 203 204 205
			spin_unlock(&child->d_lock);
		}
	}
	spin_unlock(&dcache_lock);
}

R
Robert Love 已提交
206
/*
207 208
 * inotify_find_handle - find the watch associated with the given inode and
 * handle
R
Robert Love 已提交
209
 *
I
Ingo Molnar 已提交
210
 * Callers must hold inode->inotify_mutex.
R
Robert Love 已提交
211
 */
212 213
static struct inotify_watch *inode_find_handle(struct inode *inode,
					       struct inotify_handle *ih)
R
Robert Love 已提交
214 215 216 217
{
	struct inotify_watch *watch;

	list_for_each_entry(watch, &inode->inotify_watches, i_list) {
218
		if (watch->ih == ih)
R
Robert Love 已提交
219 220 221 222 223 224 225
			return watch;
	}

	return NULL;
}

/*
226
 * remove_watch_no_event - remove watch without the IN_IGNORED event.
227 228
 *
 * Callers must hold both inode->inotify_mutex and ih->mutex.
R
Robert Love 已提交
229 230
 */
static void remove_watch_no_event(struct inotify_watch *watch,
231
				  struct inotify_handle *ih)
R
Robert Love 已提交
232 233
{
	list_del(&watch->i_list);
234
	list_del(&watch->h_list);
R
Robert Love 已提交
235

236 237 238
	if (!inotify_inode_watched(watch->inode))
		set_dentry_child_flags(watch->inode, 0);

239
	idr_remove(&ih->idr, watch->wd);
R
Robert Love 已提交
240 241
}

242 243 244 245 246 247
/**
 * 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
R
Robert Love 已提交
248
 *
249
 * Callers must hold both inode->inotify_mutex and ih->mutex.
R
Robert Love 已提交
250
 */
251 252
void inotify_remove_watch_locked(struct inotify_handle *ih,
				 struct inotify_watch *watch)
R
Robert Love 已提交
253
{
254
	remove_watch_no_event(watch, ih);
255
	ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL);
R
Robert Love 已提交
256
}
257
EXPORT_SYMBOL_GPL(inotify_remove_watch_locked);
R
Robert Love 已提交
258

259
/* Kernel API for producing events */
260

R
Robert Love 已提交
261
/*
262
 * inotify_d_instantiate - instantiate dcache entry for inode
R
Robert Love 已提交
263
 */
264
void inotify_d_instantiate(struct dentry *entry, struct inode *inode)
R
Robert Love 已提交
265
{
266 267 268 269 270 271 272
	struct dentry *parent;

	if (!inode)
		return;

	spin_lock(&entry->d_lock);
	parent = entry->d_parent;
273
	if (parent->d_inode && inotify_inode_watched(parent->d_inode))
274 275
		entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
	spin_unlock(&entry->d_lock);
R
Robert Love 已提交
276 277
}

278 279 280 281 282 283 284 285 286 287 288 289 290
/*
 * 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;
}
R
Robert Love 已提交
291 292 293 294 295 296 297

/**
 * 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
298
 * @n_inode: inode associated with name
R
Robert Love 已提交
299 300
 */
void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
301
			       const char *name, struct inode *n_inode)
R
Robert Love 已提交
302 303 304 305 306 307
{
	struct inotify_watch *watch, *next;

	if (!inotify_inode_watched(inode))
		return;

I
Ingo Molnar 已提交
308
	mutex_lock(&inode->inotify_mutex);
R
Robert Love 已提交
309 310 311
	list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
		u32 watch_mask = watch->mask;
		if (watch_mask & mask) {
312 313
			struct inotify_handle *ih= watch->ih;
			mutex_lock(&ih->mutex);
R
Robert Love 已提交
314
			if (watch_mask & IN_ONESHOT)
315
				remove_watch_no_event(watch, ih);
316 317
			ih->in_ops->handle_event(watch, watch->wd, mask, cookie,
						 name, n_inode);
318
			mutex_unlock(&ih->mutex);
R
Robert Love 已提交
319 320
		}
	}
I
Ingo Molnar 已提交
321
	mutex_unlock(&inode->inotify_mutex);
R
Robert Love 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
}
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;

338
	if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))
J
John McCutchan 已提交
339 340
		return;

R
Robert Love 已提交
341 342 343 344 345 346 347
	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);
348 349
		inotify_inode_queue_event(inode, mask, cookie, name,
					  dentry->d_inode);
R
Robert Love 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
		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
I
Ingo Molnar 已提交
370
 * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
R
Robert Love 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
 * 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;
401
		/* In case inotify_remove_watch_locked() drops a reference. */
R
Robert Love 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
		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,
I
Ingo Molnar 已提交
419
		 * iprune_mutex keeps shrink_icache_memory() away.
R
Robert Love 已提交
420 421 422 423 424 425 426
		 */
		spin_unlock(&inode_lock);

		if (need_iput_tmp)
			iput(need_iput_tmp);

		/* for each watch, send IN_UNMOUNT and then remove it */
I
Ingo Molnar 已提交
427
		mutex_lock(&inode->inotify_mutex);
R
Robert Love 已提交
428 429
		watches = &inode->inotify_watches;
		list_for_each_entry_safe(watch, next_w, watches, i_list) {
430
			struct inotify_handle *ih= watch->ih;
431
			get_inotify_watch(watch);
432 433
			mutex_lock(&ih->mutex);
			ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,
434
						 NULL, NULL);
435
			inotify_remove_watch_locked(ih, watch);
436
			mutex_unlock(&ih->mutex);
437
			put_inotify_watch(watch);
R
Robert Love 已提交
438
		}
I
Ingo Molnar 已提交
439
		mutex_unlock(&inode->inotify_mutex);
R
Robert Love 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
		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;

I
Ingo Molnar 已提交
455
	mutex_lock(&inode->inotify_mutex);
R
Robert Love 已提交
456
	list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
457 458
		struct inotify_handle *ih = watch->ih;
		mutex_lock(&ih->mutex);
459
		inotify_remove_watch_locked(ih, watch);
460
		mutex_unlock(&ih->mutex);
R
Robert Love 已提交
461
	}
I
Ingo Molnar 已提交
462
	mutex_unlock(&inode->inotify_mutex);
R
Robert Love 已提交
463 464 465
}
EXPORT_SYMBOL_GPL(inotify_inode_is_dead);

466
/* Kernel Consumer API */
R
Robert Love 已提交
467

468 469 470 471 472
/**
 * inotify_init - allocate and initialize an inotify instance
 * @ops: caller's inotify operations
 */
struct inotify_handle *inotify_init(const struct inotify_operations *ops)
R
Robert Love 已提交
473
{
474
	struct inotify_handle *ih;
R
Robert Love 已提交
475

476 477 478
	ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);
	if (unlikely(!ih))
		return ERR_PTR(-ENOMEM);
R
Robert Love 已提交
479

480 481 482 483 484 485 486
	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);
R
Robert Love 已提交
487

488
	return ih;
R
Robert Love 已提交
489
}
490
EXPORT_SYMBOL_GPL(inotify_init);
R
Robert Love 已提交
491

492 493 494 495 496 497 498 499 500 501 502 503 504
/**
 * 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);

A
Al Viro 已提交
505 506 507 508 509 510 511 512 513 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 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
/*
 * Watch removals suck violently.  To kick the watch out we need (in this
 * order) inode->inotify_mutex and ih->mutex.  That's fine if we have
 * a hold on inode; however, for all other cases we need to make damn sure
 * we don't race with umount.  We can *NOT* just grab a reference to a
 * watch - inotify_unmount_inodes() will happily sail past it and we'll end
 * with reference to inode potentially outliving its superblock.  Ideally
 * we just want to grab an active reference to superblock if we can; that
 * will make sure we won't go into inotify_umount_inodes() until we are
 * done.  Cleanup is just deactivate_super().  However, that leaves a messy
 * case - what if we *are* racing with umount() and active references to
 * superblock can't be acquired anymore?  We can bump ->s_count, grab
 * ->s_umount, which will almost certainly wait until the superblock is shut
 * down and the watch in question is pining for fjords.  That's fine, but
 * there is a problem - we might have hit the window between ->s_active
 * getting to 0 / ->s_count - below S_BIAS (i.e. the moment when superblock
 * is past the point of no return and is heading for shutdown) and the
 * moment when deactivate_super() acquires ->s_umount.  We could just do
 * drop_super() yield() and retry, but that's rather antisocial and this
 * stuff is luser-triggerable.  OTOH, having grabbed ->s_umount and having
 * found that we'd got there first (i.e. that ->s_root is non-NULL) we know
 * that we won't race with inotify_umount_inodes().  So we could grab a
 * reference to watch and do the rest as above, just with drop_super() instead
 * of deactivate_super(), right?  Wrong.  We had to drop ih->mutex before we
 * could grab ->s_umount.  So the watch could've been gone already.
 *
 * That still can be dealt with - we need to save watch->wd, do idr_find()
 * and compare its result with our pointer.  If they match, we either have
 * the damn thing still alive or we'd lost not one but two races at once,
 * the watch had been killed and a new one got created with the same ->wd
 * at the same address.  That couldn't have happened in inotify_destroy(),
 * but inotify_rm_wd() could run into that.  Still, "new one got created"
 * is not a problem - we have every right to kill it or leave it alone,
 * whatever's more convenient.
 *
 * So we can use idr_find(...) == watch && watch->inode->i_sb == sb as
 * "grab it and kill it" check.  If it's been our original watch, we are
 * fine, if it's a newcomer - nevermind, just pretend that we'd won the
 * race and kill the fscker anyway; we are safe since we know that its
 * superblock won't be going away.
 *
 * And yes, this is far beyond mere "not very pretty"; so's the entire
 * concept of inotify to start with.
 */

/**
 * pin_to_kill - pin the watch down for removal
 * @ih: inotify handle
 * @watch: watch to kill
 *
 * Called with ih->mutex held, drops it.  Possible return values:
 * 0 - nothing to do, it has died
 * 1 - remove it, drop the reference and deactivate_super()
 * 2 - remove it, drop the reference and drop_super(); we tried hard to avoid
 * that variant, since it involved a lot of PITA, but that's the best that
 * could've been done.
 */
static int pin_to_kill(struct inotify_handle *ih, struct inotify_watch *watch)
{
	struct super_block *sb = watch->inode->i_sb;
	s32 wd = watch->wd;

	spin_lock(&sb_lock);
	if (sb->s_count >= S_BIAS) {
		atomic_inc(&sb->s_active);
		spin_unlock(&sb_lock);
		get_inotify_watch(watch);
		mutex_unlock(&ih->mutex);
		return 1;	/* the best outcome */
	}
	sb->s_count++;
	spin_unlock(&sb_lock);
	mutex_unlock(&ih->mutex); /* can't grab ->s_umount under it */
	down_read(&sb->s_umount);
	if (likely(!sb->s_root)) {
		/* fs is already shut down; the watch is dead */
		drop_super(sb);
		return 0;
	}
	/* raced with the final deactivate_super() */
	mutex_lock(&ih->mutex);
	if (idr_find(&ih->idr, wd) != watch || watch->inode->i_sb != sb) {
		/* the watch is dead */
		mutex_unlock(&ih->mutex);
		drop_super(sb);
		return 0;
	}
	/* still alive or freed and reused with the same sb and wd; kill */
	get_inotify_watch(watch);
	mutex_unlock(&ih->mutex);
	return 2;
}

static void unpin_and_kill(struct inotify_watch *watch, int how)
{
	struct super_block *sb = watch->inode->i_sb;
	put_inotify_watch(watch);
	switch (how) {
	case 1:
		deactivate_super(sb);
		break;
	case 2:
		drop_super(sb);
	}
}

611 612 613 614 615
/**
 * inotify_destroy - clean up and destroy an inotify instance
 * @ih: inotify handle
 */
void inotify_destroy(struct inotify_handle *ih)
R
Robert Love 已提交
616 617
{
	/*
618
	 * Destroy all of the watches for this handle. Unfortunately, not very
R
Robert Love 已提交
619 620
	 * 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
621
	 * hold inode->inotify_mutex before ih->mutex.  The following works.
A
Al Viro 已提交
622 623
	 *
	 * AV: it had to become even uglier to start working ;-/
R
Robert Love 已提交
624 625 626 627
	 */
	while (1) {
		struct inotify_watch *watch;
		struct list_head *watches;
A
Al Viro 已提交
628
		struct super_block *sb;
R
Robert Love 已提交
629
		struct inode *inode;
A
Al Viro 已提交
630
		int how;
R
Robert Love 已提交
631

632 633
		mutex_lock(&ih->mutex);
		watches = &ih->watches;
R
Robert Love 已提交
634
		if (list_empty(watches)) {
635
			mutex_unlock(&ih->mutex);
R
Robert Love 已提交
636 637
			break;
		}
638
		watch = list_first_entry(watches, struct inotify_watch, h_list);
A
Al Viro 已提交
639 640 641 642
		sb = watch->inode->i_sb;
		how = pin_to_kill(ih, watch);
		if (!how)
			continue;
R
Robert Love 已提交
643 644

		inode = watch->inode;
I
Ingo Molnar 已提交
645
		mutex_lock(&inode->inotify_mutex);
646
		mutex_lock(&ih->mutex);
647 648

		/* make sure we didn't race with another list removal */
649 650 651 652
		if (likely(idr_find(&ih->idr, watch->wd))) {
			remove_watch_no_event(watch, ih);
			put_inotify_watch(watch);
		}
653

654
		mutex_unlock(&ih->mutex);
I
Ingo Molnar 已提交
655
		mutex_unlock(&inode->inotify_mutex);
A
Al Viro 已提交
656
		unpin_and_kill(watch, how);
R
Robert Love 已提交
657 658
	}

659 660
	/* free this handle: the put matching the get in inotify_init() */
	put_inotify_handle(ih);
R
Robert Love 已提交
661
}
662
EXPORT_SYMBOL_GPL(inotify_destroy);
R
Robert Love 已提交
663

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
/**
 * 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);

695 696 697 698 699
/**
 * 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
R
Robert Love 已提交
700
 *
701
 * Caller must pin given inode (via nameidata).
R
Robert Love 已提交
702
 */
703 704
s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,
			      u32 mask)
R
Robert Love 已提交
705
{
706 707 708
	struct inotify_watch *old;
	int mask_add = 0;
	int ret;
R
Robert Love 已提交
709

710 711 712 713 714 715
	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))
R
Robert Love 已提交
716 717
		return -EINVAL;

I
Ingo Molnar 已提交
718
	mutex_lock(&inode->inotify_mutex);
719
	mutex_lock(&ih->mutex);
R
Robert Love 已提交
720

721 722 723 724 725 726 727 728
	/*
	 * 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;
R
Robert Love 已提交
729 730
	}

731 732 733 734 735 736 737 738
	if (mask_add)
		old->mask |= mask;
	else
		old->mask = mask;
	ret = old->wd;
out:
	mutex_unlock(&ih->mutex);
	mutex_unlock(&inode->inotify_mutex);
R
Robert Love 已提交
739 740
	return ret;
}
741
EXPORT_SYMBOL_GPL(inotify_find_update_watch);
R
Robert Love 已提交
742

743 744 745 746 747 748 749 750 751 752 753 754 755
/**
 * 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)
R
Robert Love 已提交
756
{
757
	int ret = 0;
N
Nick Piggin 已提交
758
	int newly_watched;
R
Robert Love 已提交
759

760 761 762 763 764
	/* 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;
R
Robert Love 已提交
765

766 767
	mutex_lock(&inode->inotify_mutex);
	mutex_lock(&ih->mutex);
768

769 770
	/* Initialize a new watch */
	ret = inotify_handle_get_wd(ih, watch);
R
Robert Love 已提交
771
	if (unlikely(ret))
772 773
		goto out;
	ret = watch->wd;
R
Robert Love 已提交
774

775 776 777
	/* save a reference to handle and bump the count to make it official */
	get_inotify_handle(ih);
	watch->ih = ih;
R
Robert Love 已提交
778 779

	/*
780 781
	 * 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.
R
Robert Love 已提交
782
	 */
783
	watch->inode = igrab(inode);
R
Robert Love 已提交
784

785
	/* Add the watch to the handle's and the inode's list */
N
Nick Piggin 已提交
786
	newly_watched = !inotify_inode_watched(inode);
787
	list_add(&watch->h_list, &ih->watches);
R
Robert Love 已提交
788
	list_add(&watch->i_list, &inode->inotify_watches);
N
Nick Piggin 已提交
789 790 791 792 793 794 795 796
	/*
	 * 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);

R
Robert Love 已提交
797
out:
798
	mutex_unlock(&ih->mutex);
I
Ingo Molnar 已提交
799
	mutex_unlock(&inode->inotify_mutex);
R
Robert Love 已提交
800 801
	return ret;
}
802
EXPORT_SYMBOL_GPL(inotify_add_watch);
R
Robert Love 已提交
803

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
/**
 * 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;
}

839 840 841 842 843 844 845 846
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);
}

847 848 849 850 851 852 853 854
/**
 * 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)
R
Robert Love 已提交
855
{
856
	struct inotify_watch *watch;
A
Al Viro 已提交
857
	struct super_block *sb;
858
	struct inode *inode;
A
Al Viro 已提交
859
	int how;
R
Robert Love 已提交
860

861 862 863 864 865
	mutex_lock(&ih->mutex);
	watch = idr_find(&ih->idr, wd);
	if (unlikely(!watch)) {
		mutex_unlock(&ih->mutex);
		return -EINVAL;
R
Robert Love 已提交
866
	}
A
Al Viro 已提交
867 868 869 870 871
	sb = watch->inode->i_sb;
	how = pin_to_kill(ih, watch);
	if (!how)
		return 0;

872
	inode = watch->inode;
R
Robert Love 已提交
873

874 875
	mutex_lock(&inode->inotify_mutex);
	mutex_lock(&ih->mutex);
R
Robert Love 已提交
876

877 878
	/* make sure that we did not race */
	if (likely(idr_find(&ih->idr, wd) == watch))
879
		inotify_remove_watch_locked(ih, watch);
R
Robert Love 已提交
880

881 882
	mutex_unlock(&ih->mutex);
	mutex_unlock(&inode->inotify_mutex);
A
Al Viro 已提交
883
	unpin_and_kill(watch, how);
R
Robert Love 已提交
884

885 886 887
	return 0;
}
EXPORT_SYMBOL_GPL(inotify_rm_wd);
R
Robert Love 已提交
888

889 890 891 892 893 894 895 896 897 898 899 900 901 902
/**
 * 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);

R
Robert Love 已提交
903
/*
904
 * inotify_setup - core initialization function
R
Robert Love 已提交
905
 */
R
Robert Love 已提交
906
static int __init inotify_setup(void)
R
Robert Love 已提交
907 908 909 910 911 912
{
	atomic_set(&inotify_cookie, 0);

	return 0;
}

R
Robert Love 已提交
913
module_init(inotify_setup);