| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 1 | /* | 
 | 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 Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 8 |  * Kernel API added by: Amy Griffis <amy.griffis@hp.com> | 
 | 9 |  * | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 10 |  * Copyright (C) 2005 John McCutchan | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 11 |  * Copyright 2006 Hewlett-Packard Development Company, L.P. | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 12 |  * | 
 | 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 Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 26 | #include <linux/spinlock.h> | 
 | 27 | #include <linux/idr.h> | 
 | 28 | #include <linux/slab.h> | 
 | 29 | #include <linux/fs.h> | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 30 | #include <linux/init.h> | 
 | 31 | #include <linux/list.h> | 
 | 32 | #include <linux/writeback.h> | 
 | 33 | #include <linux/inotify.h> | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 34 |  | 
 | 35 | static atomic_t inotify_cookie; | 
 | 36 |  | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 37 | /* | 
 | 38 |  * Lock ordering: | 
 | 39 |  * | 
 | 40 |  * dentry->d_lock (used to keep d_move() away from dentry->d_parent) | 
| Ingo Molnar | f24075b | 2006-03-23 03:00:34 -0800 | [diff] [blame] | 41 |  * iprune_mutex (synchronize shrink_icache_memory()) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 42 |  * 	inode_lock (protects the super_block->s_inodes list) | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 43 |  * 	inode->inotify_mutex (protects inode->inotify_watches and watches->i_list) | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 44 |  * 		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 Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 50 |  */ | 
 | 51 |  | 
 | 52 | /* | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 53 |  * Lifetimes of the three main data structures--inotify_handle, inode, and | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 54 |  * inotify_watch--are managed by reference count. | 
 | 55 |  * | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 56 |  * 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 Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 59 |  * | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 60 |  * 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 Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 66 |  * | 
 | 67 |  * inode: Pinned so long as the inode is associated with a watch, from | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 68 |  * inotify_add_watch() to the final put_inotify_watch(). | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 69 |  */ | 
 | 70 |  | 
 | 71 | /* | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 72 |  * struct inotify_handle - represents an inotify instance | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 73 |  * | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 74 |  * This structure is protected by the mutex 'mutex'. | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 75 |  */ | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 76 | struct inotify_handle { | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 77 | 	struct idr		idr;		/* idr mapping wd -> watch */ | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 78 | 	struct mutex		mutex;		/* protects this bad boy */ | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 79 | 	struct list_head	watches;	/* list of watches */ | 
 | 80 | 	atomic_t		count;		/* reference count */ | 
| John McCutchan | b9c55d2 | 2005-08-01 11:00:45 -0400 | [diff] [blame] | 81 | 	u32			last_wd;	/* the last wd allocated */ | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 82 | 	const struct inotify_operations *in_ops; /* inotify caller operations */ | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 83 | }; | 
 | 84 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 85 | static inline void get_inotify_handle(struct inotify_handle *ih) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 86 | { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 87 | 	atomic_inc(&ih->count); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 88 | } | 
 | 89 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 90 | static inline void put_inotify_handle(struct inotify_handle *ih) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 91 | { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 92 | 	if (atomic_dec_and_test(&ih->count)) { | 
 | 93 | 		idr_destroy(&ih->idr); | 
 | 94 | 		kfree(ih); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 95 | 	} | 
 | 96 | } | 
 | 97 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 98 | /** | 
 | 99 |  * get_inotify_watch - grab a reference to an inotify_watch | 
 | 100 |  * @watch: watch to grab | 
 | 101 |  */ | 
 | 102 | void get_inotify_watch(struct inotify_watch *watch) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 103 | { | 
 | 104 | 	atomic_inc(&watch->count); | 
 | 105 | } | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 106 | EXPORT_SYMBOL_GPL(get_inotify_watch); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 107 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 108 | /** | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 109 |  * put_inotify_watch - decrements the ref count on a given watch.  cleans up | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 110 |  * 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 Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 113 |  */ | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 114 | void put_inotify_watch(struct inotify_watch *watch) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 115 | { | 
 | 116 | 	if (atomic_dec_and_test(&watch->count)) { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 117 | 		struct inotify_handle *ih = watch->ih; | 
 | 118 |  | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 119 | 		iput(watch->inode); | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 120 | 		ih->in_ops->destroy_watch(watch); | 
 | 121 | 		put_inotify_handle(ih); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 122 | 	} | 
 | 123 | } | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 124 | EXPORT_SYMBOL_GPL(put_inotify_watch); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 125 |  | 
 | 126 | /* | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 127 |  * inotify_handle_get_wd - returns the next WD for use by the given handle | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 128 |  * | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 129 |  * Callers must hold ih->mutex.  This function can sleep. | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 130 |  */ | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 131 | static int inotify_handle_get_wd(struct inotify_handle *ih, | 
 | 132 | 				 struct inotify_watch *watch) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 133 | { | 
 | 134 | 	int ret; | 
 | 135 |  | 
 | 136 | 	do { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 137 | 		if (unlikely(!idr_pre_get(&ih->idr, GFP_KERNEL))) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 138 | 			return -ENOSPC; | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 139 | 		ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 140 | 	} while (ret == -EAGAIN); | 
 | 141 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 142 | 	if (likely(!ret)) | 
 | 143 | 		ih->last_wd = watch->wd; | 
 | 144 |  | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 145 | 	return ret; | 
 | 146 | } | 
 | 147 |  | 
 | 148 | /* | 
| Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 149 |  * 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 |  */ | 
 | 152 | static 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 |  */ | 
 | 161 | static 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 Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 191 |  * inotify_find_handle - find the watch associated with the given inode and | 
 | 192 |  * handle | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 193 |  * | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 194 |  * Callers must hold inode->inotify_mutex. | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 195 |  */ | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 196 | static struct inotify_watch *inode_find_handle(struct inode *inode, | 
 | 197 | 					       struct inotify_handle *ih) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 198 | { | 
 | 199 | 	struct inotify_watch *watch; | 
 | 200 |  | 
 | 201 | 	list_for_each_entry(watch, &inode->inotify_watches, i_list) { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 202 | 		if (watch->ih == ih) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 203 | 			return watch; | 
 | 204 | 	} | 
 | 205 |  | 
 | 206 | 	return NULL; | 
 | 207 | } | 
 | 208 |  | 
 | 209 | /* | 
| Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 210 |  * remove_watch_no_event - remove watch without the IN_IGNORED event. | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 211 |  * | 
 | 212 |  * Callers must hold both inode->inotify_mutex and ih->mutex. | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 213 |  */ | 
 | 214 | static void remove_watch_no_event(struct inotify_watch *watch, | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 215 | 				  struct inotify_handle *ih) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 216 | { | 
 | 217 | 	list_del(&watch->i_list); | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 218 | 	list_del(&watch->h_list); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 219 |  | 
| Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 220 | 	if (!inotify_inode_watched(watch->inode)) | 
 | 221 | 		set_dentry_child_flags(watch->inode, 0); | 
 | 222 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 223 | 	idr_remove(&ih->idr, watch->wd); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 224 | } | 
 | 225 |  | 
| Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 226 | /** | 
 | 227 |  * inotify_remove_watch_locked - Remove a watch from both the handle and the | 
 | 228 |  * inode.  Sends the IN_IGNORED event signifying that the inode is no longer | 
 | 229 |  * watched.  May be invoked from a caller's event handler. | 
 | 230 |  * @ih: inotify handle associated with watch | 
 | 231 |  * @watch: watch to remove | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 232 |  * | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 233 |  * Callers must hold both inode->inotify_mutex and ih->mutex. | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 234 |  */ | 
| Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 235 | void inotify_remove_watch_locked(struct inotify_handle *ih, | 
 | 236 | 				 struct inotify_watch *watch) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 237 | { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 238 | 	remove_watch_no_event(watch, ih); | 
| Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 239 | 	ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 240 | } | 
| Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 241 | EXPORT_SYMBOL_GPL(inotify_remove_watch_locked); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 242 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 243 | /* Kernel API for producing events */ | 
| Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 244 |  | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 245 | /* | 
| Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 246 |  * inotify_d_instantiate - instantiate dcache entry for inode | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 247 |  */ | 
| Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 248 | void inotify_d_instantiate(struct dentry *entry, struct inode *inode) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 249 | { | 
| Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 250 | 	struct dentry *parent; | 
 | 251 |  | 
 | 252 | 	if (!inode) | 
 | 253 | 		return; | 
 | 254 |  | 
 | 255 | 	WARN_ON(entry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED); | 
 | 256 | 	spin_lock(&entry->d_lock); | 
 | 257 | 	parent = entry->d_parent; | 
| Arnd Bergmann | 091e881 | 2006-04-10 22:54:31 -0700 | [diff] [blame] | 258 | 	if (parent->d_inode && inotify_inode_watched(parent->d_inode)) | 
| Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 259 | 		entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED; | 
 | 260 | 	spin_unlock(&entry->d_lock); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 261 | } | 
 | 262 |  | 
| Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 263 | /* | 
 | 264 |  * inotify_d_move - dcache entry has been moved | 
 | 265 |  */ | 
 | 266 | void inotify_d_move(struct dentry *entry) | 
 | 267 | { | 
 | 268 | 	struct dentry *parent; | 
 | 269 |  | 
 | 270 | 	parent = entry->d_parent; | 
 | 271 | 	if (inotify_inode_watched(parent->d_inode)) | 
 | 272 | 		entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED; | 
 | 273 | 	else | 
 | 274 | 		entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED; | 
 | 275 | } | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 276 |  | 
 | 277 | /** | 
 | 278 |  * inotify_inode_queue_event - queue an event to all watches on this inode | 
 | 279 |  * @inode: inode event is originating from | 
 | 280 |  * @mask: event mask describing this event | 
 | 281 |  * @cookie: cookie for synchronization, or zero | 
 | 282 |  * @name: filename, if any | 
| Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 283 |  * @n_inode: inode associated with name | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 284 |  */ | 
 | 285 | void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie, | 
| Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 286 | 			       const char *name, struct inode *n_inode) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 287 | { | 
 | 288 | 	struct inotify_watch *watch, *next; | 
 | 289 |  | 
 | 290 | 	if (!inotify_inode_watched(inode)) | 
 | 291 | 		return; | 
 | 292 |  | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 293 | 	mutex_lock(&inode->inotify_mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 294 | 	list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) { | 
 | 295 | 		u32 watch_mask = watch->mask; | 
 | 296 | 		if (watch_mask & mask) { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 297 | 			struct inotify_handle *ih= watch->ih; | 
 | 298 | 			mutex_lock(&ih->mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 299 | 			if (watch_mask & IN_ONESHOT) | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 300 | 				remove_watch_no_event(watch, ih); | 
| Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 301 | 			ih->in_ops->handle_event(watch, watch->wd, mask, cookie, | 
 | 302 | 						 name, n_inode); | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 303 | 			mutex_unlock(&ih->mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 304 | 		} | 
 | 305 | 	} | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 306 | 	mutex_unlock(&inode->inotify_mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 307 | } | 
 | 308 | EXPORT_SYMBOL_GPL(inotify_inode_queue_event); | 
 | 309 |  | 
 | 310 | /** | 
 | 311 |  * inotify_dentry_parent_queue_event - queue an event to a dentry's parent | 
 | 312 |  * @dentry: the dentry in question, we queue against this dentry's parent | 
 | 313 |  * @mask: event mask describing this event | 
 | 314 |  * @cookie: cookie for synchronization, or zero | 
 | 315 |  * @name: filename, if any | 
 | 316 |  */ | 
 | 317 | void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask, | 
 | 318 | 				       u32 cookie, const char *name) | 
 | 319 | { | 
 | 320 | 	struct dentry *parent; | 
 | 321 | 	struct inode *inode; | 
 | 322 |  | 
| Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 323 | 	if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED)) | 
| John McCutchan | 820249b | 2005-09-06 15:16:38 -0700 | [diff] [blame] | 324 | 		return; | 
 | 325 |  | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 326 | 	spin_lock(&dentry->d_lock); | 
 | 327 | 	parent = dentry->d_parent; | 
 | 328 | 	inode = parent->d_inode; | 
 | 329 |  | 
 | 330 | 	if (inotify_inode_watched(inode)) { | 
 | 331 | 		dget(parent); | 
 | 332 | 		spin_unlock(&dentry->d_lock); | 
| Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 333 | 		inotify_inode_queue_event(inode, mask, cookie, name, | 
 | 334 | 					  dentry->d_inode); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 335 | 		dput(parent); | 
 | 336 | 	} else | 
 | 337 | 		spin_unlock(&dentry->d_lock); | 
 | 338 | } | 
 | 339 | EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event); | 
 | 340 |  | 
 | 341 | /** | 
 | 342 |  * inotify_get_cookie - return a unique cookie for use in synchronizing events. | 
 | 343 |  */ | 
 | 344 | u32 inotify_get_cookie(void) | 
 | 345 | { | 
 | 346 | 	return atomic_inc_return(&inotify_cookie); | 
 | 347 | } | 
 | 348 | EXPORT_SYMBOL_GPL(inotify_get_cookie); | 
 | 349 |  | 
 | 350 | /** | 
 | 351 |  * inotify_unmount_inodes - an sb is unmounting.  handle any watched inodes. | 
 | 352 |  * @list: list of inodes being unmounted (sb->s_inodes) | 
 | 353 |  * | 
 | 354 |  * Called with inode_lock held, protecting the unmounting super block's list | 
| Ingo Molnar | f24075b | 2006-03-23 03:00:34 -0800 | [diff] [blame] | 355 |  * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay. | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 356 |  * We temporarily drop inode_lock, however, and CAN block. | 
 | 357 |  */ | 
 | 358 | void inotify_unmount_inodes(struct list_head *list) | 
 | 359 | { | 
 | 360 | 	struct inode *inode, *next_i, *need_iput = NULL; | 
 | 361 |  | 
 | 362 | 	list_for_each_entry_safe(inode, next_i, list, i_sb_list) { | 
 | 363 | 		struct inotify_watch *watch, *next_w; | 
 | 364 | 		struct inode *need_iput_tmp; | 
 | 365 | 		struct list_head *watches; | 
 | 366 |  | 
 | 367 | 		/* | 
 | 368 | 		 * If i_count is zero, the inode cannot have any watches and | 
 | 369 | 		 * doing an __iget/iput with MS_ACTIVE clear would actually | 
 | 370 | 		 * evict all inodes with zero i_count from icache which is | 
 | 371 | 		 * unnecessarily violent and may in fact be illegal to do. | 
 | 372 | 		 */ | 
 | 373 | 		if (!atomic_read(&inode->i_count)) | 
 | 374 | 			continue; | 
 | 375 |  | 
 | 376 | 		/* | 
 | 377 | 		 * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or | 
 | 378 | 		 * I_WILL_FREE which is fine because by that point the inode | 
 | 379 | 		 * cannot have any associated watches. | 
 | 380 | 		 */ | 
 | 381 | 		if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE)) | 
 | 382 | 			continue; | 
 | 383 |  | 
 | 384 | 		need_iput_tmp = need_iput; | 
 | 385 | 		need_iput = NULL; | 
| Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 386 | 		/* In case inotify_remove_watch_locked() drops a reference. */ | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 387 | 		if (inode != need_iput_tmp) | 
 | 388 | 			__iget(inode); | 
 | 389 | 		else | 
 | 390 | 			need_iput_tmp = NULL; | 
 | 391 | 		/* In case the dropping of a reference would nuke next_i. */ | 
 | 392 | 		if ((&next_i->i_sb_list != list) && | 
 | 393 | 				atomic_read(&next_i->i_count) && | 
 | 394 | 				!(next_i->i_state & (I_CLEAR | I_FREEING | | 
 | 395 | 					I_WILL_FREE))) { | 
 | 396 | 			__iget(next_i); | 
 | 397 | 			need_iput = next_i; | 
 | 398 | 		} | 
 | 399 |  | 
 | 400 | 		/* | 
 | 401 | 		 * We can safely drop inode_lock here because we hold | 
 | 402 | 		 * references on both inode and next_i.  Also no new inodes | 
 | 403 | 		 * will be added since the umount has begun.  Finally, | 
| Ingo Molnar | f24075b | 2006-03-23 03:00:34 -0800 | [diff] [blame] | 404 | 		 * iprune_mutex keeps shrink_icache_memory() away. | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 405 | 		 */ | 
 | 406 | 		spin_unlock(&inode_lock); | 
 | 407 |  | 
 | 408 | 		if (need_iput_tmp) | 
 | 409 | 			iput(need_iput_tmp); | 
 | 410 |  | 
 | 411 | 		/* for each watch, send IN_UNMOUNT and then remove it */ | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 412 | 		mutex_lock(&inode->inotify_mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 413 | 		watches = &inode->inotify_watches; | 
 | 414 | 		list_for_each_entry_safe(watch, next_w, watches, i_list) { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 415 | 			struct inotify_handle *ih= watch->ih; | 
 | 416 | 			mutex_lock(&ih->mutex); | 
 | 417 | 			ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0, | 
| Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 418 | 						 NULL, NULL); | 
| Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 419 | 			inotify_remove_watch_locked(ih, watch); | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 420 | 			mutex_unlock(&ih->mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 421 | 		} | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 422 | 		mutex_unlock(&inode->inotify_mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 423 | 		iput(inode);		 | 
 | 424 |  | 
 | 425 | 		spin_lock(&inode_lock); | 
 | 426 | 	} | 
 | 427 | } | 
 | 428 | EXPORT_SYMBOL_GPL(inotify_unmount_inodes); | 
 | 429 |  | 
 | 430 | /** | 
 | 431 |  * inotify_inode_is_dead - an inode has been deleted, cleanup any watches | 
 | 432 |  * @inode: inode that is about to be removed | 
 | 433 |  */ | 
 | 434 | void inotify_inode_is_dead(struct inode *inode) | 
 | 435 | { | 
 | 436 | 	struct inotify_watch *watch, *next; | 
 | 437 |  | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 438 | 	mutex_lock(&inode->inotify_mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 439 | 	list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 440 | 		struct inotify_handle *ih = watch->ih; | 
 | 441 | 		mutex_lock(&ih->mutex); | 
| Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 442 | 		inotify_remove_watch_locked(ih, watch); | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 443 | 		mutex_unlock(&ih->mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 444 | 	} | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 445 | 	mutex_unlock(&inode->inotify_mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 446 | } | 
 | 447 | EXPORT_SYMBOL_GPL(inotify_inode_is_dead); | 
 | 448 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 449 | /* Kernel Consumer API */ | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 450 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 451 | /** | 
 | 452 |  * inotify_init - allocate and initialize an inotify instance | 
 | 453 |  * @ops: caller's inotify operations | 
 | 454 |  */ | 
 | 455 | struct inotify_handle *inotify_init(const struct inotify_operations *ops) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 456 | { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 457 | 	struct inotify_handle *ih; | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 458 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 459 | 	ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL); | 
 | 460 | 	if (unlikely(!ih)) | 
 | 461 | 		return ERR_PTR(-ENOMEM); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 462 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 463 | 	idr_init(&ih->idr); | 
 | 464 | 	INIT_LIST_HEAD(&ih->watches); | 
 | 465 | 	mutex_init(&ih->mutex); | 
 | 466 | 	ih->last_wd = 0; | 
 | 467 | 	ih->in_ops = ops; | 
 | 468 | 	atomic_set(&ih->count, 0); | 
 | 469 | 	get_inotify_handle(ih); | 
 | 470 |  | 
 | 471 | 	return ih; | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 472 | } | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 473 | EXPORT_SYMBOL_GPL(inotify_init); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 474 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 475 | /** | 
| Amy Griffis | a9dc971 | 2006-06-01 13:11:03 -0700 | [diff] [blame] | 476 |  * inotify_init_watch - initialize an inotify watch | 
 | 477 |  * @watch: watch to initialize | 
 | 478 |  */ | 
 | 479 | void inotify_init_watch(struct inotify_watch *watch) | 
 | 480 | { | 
 | 481 | 	INIT_LIST_HEAD(&watch->h_list); | 
 | 482 | 	INIT_LIST_HEAD(&watch->i_list); | 
 | 483 | 	atomic_set(&watch->count, 0); | 
 | 484 | 	get_inotify_watch(watch); /* initial get */ | 
 | 485 | } | 
 | 486 | EXPORT_SYMBOL_GPL(inotify_init_watch); | 
 | 487 |  | 
 | 488 | /** | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 489 |  * inotify_destroy - clean up and destroy an inotify instance | 
 | 490 |  * @ih: inotify handle | 
 | 491 |  */ | 
 | 492 | void inotify_destroy(struct inotify_handle *ih) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 493 | { | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 494 | 	/* | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 495 | 	 * Destroy all of the watches for this handle. Unfortunately, not very | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 496 | 	 * pretty.  We cannot do a simple iteration over the list, because we | 
 | 497 | 	 * do not know the inode until we iterate to the watch.  But we need to | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 498 | 	 * hold inode->inotify_mutex before ih->mutex.  The following works. | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 499 | 	 */ | 
 | 500 | 	while (1) { | 
 | 501 | 		struct inotify_watch *watch; | 
 | 502 | 		struct list_head *watches; | 
 | 503 | 		struct inode *inode; | 
 | 504 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 505 | 		mutex_lock(&ih->mutex); | 
 | 506 | 		watches = &ih->watches; | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 507 | 		if (list_empty(watches)) { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 508 | 			mutex_unlock(&ih->mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 509 | 			break; | 
 | 510 | 		} | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 511 | 		watch = list_entry(watches->next, struct inotify_watch, h_list); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 512 | 		get_inotify_watch(watch); | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 513 | 		mutex_unlock(&ih->mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 514 |  | 
 | 515 | 		inode = watch->inode; | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 516 | 		mutex_lock(&inode->inotify_mutex); | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 517 | 		mutex_lock(&ih->mutex); | 
| Amy Griffis | 66055a4 | 2006-05-20 15:00:06 -0700 | [diff] [blame] | 518 |  | 
 | 519 | 		/* make sure we didn't race with another list removal */ | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 520 | 		if (likely(idr_find(&ih->idr, watch->wd))) { | 
 | 521 | 			remove_watch_no_event(watch, ih); | 
 | 522 | 			put_inotify_watch(watch); | 
 | 523 | 		} | 
| Amy Griffis | 66055a4 | 2006-05-20 15:00:06 -0700 | [diff] [blame] | 524 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 525 | 		mutex_unlock(&ih->mutex); | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 526 | 		mutex_unlock(&inode->inotify_mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 527 | 		put_inotify_watch(watch); | 
 | 528 | 	} | 
 | 529 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 530 | 	/* free this handle: the put matching the get in inotify_init() */ | 
 | 531 | 	put_inotify_handle(ih); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 532 | } | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 533 | EXPORT_SYMBOL_GPL(inotify_destroy); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 534 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 535 | /** | 
| Amy Griffis | a9dc971 | 2006-06-01 13:11:03 -0700 | [diff] [blame] | 536 |  * inotify_find_watch - find an existing watch for an (ih,inode) pair | 
 | 537 |  * @ih: inotify handle | 
 | 538 |  * @inode: inode to watch | 
 | 539 |  * @watchp: pointer to existing inotify_watch | 
 | 540 |  * | 
 | 541 |  * Caller must pin given inode (via nameidata). | 
 | 542 |  */ | 
 | 543 | s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode, | 
 | 544 | 		       struct inotify_watch **watchp) | 
 | 545 | { | 
 | 546 | 	struct inotify_watch *old; | 
 | 547 | 	int ret = -ENOENT; | 
 | 548 |  | 
 | 549 | 	mutex_lock(&inode->inotify_mutex); | 
 | 550 | 	mutex_lock(&ih->mutex); | 
 | 551 |  | 
 | 552 | 	old = inode_find_handle(inode, ih); | 
 | 553 | 	if (unlikely(old)) { | 
 | 554 | 		get_inotify_watch(old); /* caller must put watch */ | 
 | 555 | 		*watchp = old; | 
 | 556 | 		ret = old->wd; | 
 | 557 | 	} | 
 | 558 |  | 
 | 559 | 	mutex_unlock(&ih->mutex); | 
 | 560 | 	mutex_unlock(&inode->inotify_mutex); | 
 | 561 |  | 
 | 562 | 	return ret; | 
 | 563 | } | 
 | 564 | EXPORT_SYMBOL_GPL(inotify_find_watch); | 
 | 565 |  | 
 | 566 | /** | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 567 |  * inotify_find_update_watch - find and update the mask of an existing watch | 
 | 568 |  * @ih: inotify handle | 
 | 569 |  * @inode: inode's watch to update | 
 | 570 |  * @mask: mask of events to watch | 
 | 571 |  * | 
 | 572 |  * Caller must pin given inode (via nameidata). | 
 | 573 |  */ | 
 | 574 | s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode, | 
 | 575 | 			      u32 mask) | 
 | 576 | { | 
 | 577 | 	struct inotify_watch *old; | 
 | 578 | 	int mask_add = 0; | 
 | 579 | 	int ret; | 
 | 580 |  | 
 | 581 | 	if (mask & IN_MASK_ADD) | 
 | 582 | 		mask_add = 1; | 
 | 583 |  | 
 | 584 | 	/* don't allow invalid bits: we don't want flags set */ | 
 | 585 | 	mask &= IN_ALL_EVENTS | IN_ONESHOT; | 
 | 586 | 	if (unlikely(!mask)) | 
 | 587 | 		return -EINVAL; | 
 | 588 |  | 
 | 589 | 	mutex_lock(&inode->inotify_mutex); | 
 | 590 | 	mutex_lock(&ih->mutex); | 
 | 591 |  | 
 | 592 | 	/* | 
 | 593 | 	 * Handle the case of re-adding a watch on an (inode,ih) pair that we | 
 | 594 | 	 * are already watching.  We just update the mask and return its wd. | 
 | 595 | 	 */ | 
 | 596 | 	old = inode_find_handle(inode, ih); | 
 | 597 | 	if (unlikely(!old)) { | 
 | 598 | 		ret = -ENOENT; | 
 | 599 | 		goto out; | 
 | 600 | 	} | 
 | 601 |  | 
 | 602 | 	if (mask_add) | 
 | 603 | 		old->mask |= mask; | 
 | 604 | 	else | 
 | 605 | 		old->mask = mask; | 
 | 606 | 	ret = old->wd; | 
 | 607 | out: | 
 | 608 | 	mutex_unlock(&ih->mutex); | 
 | 609 | 	mutex_unlock(&inode->inotify_mutex); | 
 | 610 | 	return ret; | 
 | 611 | } | 
 | 612 | EXPORT_SYMBOL_GPL(inotify_find_update_watch); | 
 | 613 |  | 
 | 614 | /** | 
 | 615 |  * inotify_add_watch - add a watch to an inotify instance | 
 | 616 |  * @ih: inotify handle | 
 | 617 |  * @watch: caller allocated watch structure | 
 | 618 |  * @inode: inode to watch | 
 | 619 |  * @mask: mask of events to watch | 
 | 620 |  * | 
 | 621 |  * Caller must pin given inode (via nameidata). | 
 | 622 |  * Caller must ensure it only calls inotify_add_watch() once per watch. | 
 | 623 |  * Calls inotify_handle_get_wd() so may sleep. | 
 | 624 |  */ | 
 | 625 | s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch, | 
 | 626 | 		      struct inode *inode, u32 mask) | 
 | 627 | { | 
 | 628 | 	int ret = 0; | 
 | 629 |  | 
 | 630 | 	/* don't allow invalid bits: we don't want flags set */ | 
 | 631 | 	mask &= IN_ALL_EVENTS | IN_ONESHOT; | 
 | 632 | 	if (unlikely(!mask)) | 
 | 633 | 		return -EINVAL; | 
 | 634 | 	watch->mask = mask; | 
 | 635 |  | 
 | 636 | 	mutex_lock(&inode->inotify_mutex); | 
 | 637 | 	mutex_lock(&ih->mutex); | 
 | 638 |  | 
 | 639 | 	/* Initialize a new watch */ | 
 | 640 | 	ret = inotify_handle_get_wd(ih, watch); | 
 | 641 | 	if (unlikely(ret)) | 
 | 642 | 		goto out; | 
 | 643 | 	ret = watch->wd; | 
 | 644 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 645 | 	/* save a reference to handle and bump the count to make it official */ | 
 | 646 | 	get_inotify_handle(ih); | 
 | 647 | 	watch->ih = ih; | 
 | 648 |  | 
 | 649 | 	/* | 
 | 650 | 	 * Save a reference to the inode and bump the ref count to make it | 
 | 651 | 	 * official.  We hold a reference to nameidata, which makes this safe. | 
 | 652 | 	 */ | 
 | 653 | 	watch->inode = igrab(inode); | 
 | 654 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 655 | 	if (!inotify_inode_watched(inode)) | 
 | 656 | 		set_dentry_child_flags(inode, 1); | 
 | 657 |  | 
 | 658 | 	/* Add the watch to the handle's and the inode's list */ | 
 | 659 | 	list_add(&watch->h_list, &ih->watches); | 
 | 660 | 	list_add(&watch->i_list, &inode->inotify_watches); | 
 | 661 | out: | 
 | 662 | 	mutex_unlock(&ih->mutex); | 
 | 663 | 	mutex_unlock(&inode->inotify_mutex); | 
 | 664 | 	return ret; | 
 | 665 | } | 
 | 666 | EXPORT_SYMBOL_GPL(inotify_add_watch); | 
 | 667 |  | 
 | 668 | /** | 
 | 669 |  * inotify_rm_wd - remove a watch from an inotify instance | 
 | 670 |  * @ih: inotify handle | 
 | 671 |  * @wd: watch descriptor to remove | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 672 |  * | 
 | 673 |  * Can sleep. | 
 | 674 |  */ | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 675 | int inotify_rm_wd(struct inotify_handle *ih, u32 wd) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 676 | { | 
 | 677 | 	struct inotify_watch *watch; | 
 | 678 | 	struct inode *inode; | 
 | 679 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 680 | 	mutex_lock(&ih->mutex); | 
 | 681 | 	watch = idr_find(&ih->idr, wd); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 682 | 	if (unlikely(!watch)) { | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 683 | 		mutex_unlock(&ih->mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 684 | 		return -EINVAL; | 
 | 685 | 	} | 
 | 686 | 	get_inotify_watch(watch); | 
 | 687 | 	inode = watch->inode; | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 688 | 	mutex_unlock(&ih->mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 689 |  | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 690 | 	mutex_lock(&inode->inotify_mutex); | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 691 | 	mutex_lock(&ih->mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 692 |  | 
 | 693 | 	/* make sure that we did not race */ | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 694 | 	if (likely(idr_find(&ih->idr, wd) == watch)) | 
| Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 695 | 		inotify_remove_watch_locked(ih, watch); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 696 |  | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 697 | 	mutex_unlock(&ih->mutex); | 
| Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 698 | 	mutex_unlock(&inode->inotify_mutex); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 699 | 	put_inotify_watch(watch); | 
 | 700 |  | 
 | 701 | 	return 0; | 
 | 702 | } | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 703 | EXPORT_SYMBOL_GPL(inotify_rm_wd); | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 704 |  | 
| Amy Griffis | a9dc971 | 2006-06-01 13:11:03 -0700 | [diff] [blame] | 705 | /** | 
 | 706 |  * inotify_rm_watch - remove a watch from an inotify instance | 
 | 707 |  * @ih: inotify handle | 
 | 708 |  * @watch: watch to remove | 
 | 709 |  * | 
 | 710 |  * Can sleep. | 
 | 711 |  */ | 
 | 712 | int inotify_rm_watch(struct inotify_handle *ih, | 
 | 713 | 		     struct inotify_watch *watch) | 
 | 714 | { | 
 | 715 | 	return inotify_rm_wd(ih, watch->wd); | 
 | 716 | } | 
 | 717 | EXPORT_SYMBOL_GPL(inotify_rm_watch); | 
 | 718 |  | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 719 | /* | 
| Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 720 |  * inotify_setup - core initialization function | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 721 |  */ | 
| Robert Love | b680716 | 2005-07-25 15:07:13 -0400 | [diff] [blame] | 722 | static int __init inotify_setup(void) | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 723 | { | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 724 | 	atomic_set(&inotify_cookie, 0); | 
 | 725 |  | 
| Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 726 | 	return 0; | 
 | 727 | } | 
 | 728 |  | 
| Robert Love | b680716 | 2005-07-25 15:07:13 -0400 | [diff] [blame] | 729 | module_init(inotify_setup); |