aboutsummaryrefslogtreecommitdiffstats
path: root/fs/notify/notification.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/notify/notification.c')
-rw-r--r--fs/notify/notification.c209
1 files changed, 126 insertions, 83 deletions
diff --git a/fs/notify/notification.c b/fs/notify/notification.c
index b8bf53b4c108..f39260f8f865 100644
--- a/fs/notify/notification.c
+++ b/fs/notify/notification.c
@@ -56,7 +56,7 @@ static struct kmem_cache *fsnotify_event_holder_cachep;
56 * it is needed. It's refcnt is set 1 at kernel init time and will never 56 * it is needed. It's refcnt is set 1 at kernel init time and will never
57 * get set to 0 so it will never get 'freed' 57 * get set to 0 so it will never get 'freed'
58 */ 58 */
59static struct fsnotify_event q_overflow_event; 59static struct fsnotify_event *q_overflow_event;
60static atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0); 60static atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0);
61 61
62/** 62/**
@@ -87,12 +87,15 @@ void fsnotify_put_event(struct fsnotify_event *event)
87 return; 87 return;
88 88
89 if (atomic_dec_and_test(&event->refcnt)) { 89 if (atomic_dec_and_test(&event->refcnt)) {
90 pr_debug("%s: event=%p\n", __func__, event);
91
90 if (event->data_type == FSNOTIFY_EVENT_PATH) 92 if (event->data_type == FSNOTIFY_EVENT_PATH)
91 path_put(&event->path); 93 path_put(&event->path);
92 94
93 BUG_ON(!list_empty(&event->private_data_list)); 95 BUG_ON(!list_empty(&event->private_data_list));
94 96
95 kfree(event->file_name); 97 kfree(event->file_name);
98 put_pid(event->tgid);
96 kmem_cache_free(fsnotify_event_cachep, event); 99 kmem_cache_free(fsnotify_event_cachep, event);
97 } 100 }
98} 101}
@@ -104,7 +107,8 @@ struct fsnotify_event_holder *fsnotify_alloc_event_holder(void)
104 107
105void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder) 108void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder)
106{ 109{
107 kmem_cache_free(fsnotify_event_holder_cachep, holder); 110 if (holder)
111 kmem_cache_free(fsnotify_event_holder_cachep, holder);
108} 112}
109 113
110/* 114/*
@@ -129,53 +133,20 @@ struct fsnotify_event_private_data *fsnotify_remove_priv_from_event(struct fsnot
129} 133}
130 134
131/* 135/*
132 * Check if 2 events contain the same information. We do not compare private data
133 * but at this moment that isn't a problem for any know fsnotify listeners.
134 */
135static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new)
136{
137 if ((old->mask == new->mask) &&
138 (old->to_tell == new->to_tell) &&
139 (old->data_type == new->data_type) &&
140 (old->name_len == new->name_len)) {
141 switch (old->data_type) {
142 case (FSNOTIFY_EVENT_INODE):
143 /* remember, after old was put on the wait_q we aren't
144 * allowed to look at the inode any more, only thing
145 * left to check was if the file_name is the same */
146 if (!old->name_len ||
147 !strcmp(old->file_name, new->file_name))
148 return true;
149 break;
150 case (FSNOTIFY_EVENT_PATH):
151 if ((old->path.mnt == new->path.mnt) &&
152 (old->path.dentry == new->path.dentry))
153 return true;
154 break;
155 case (FSNOTIFY_EVENT_NONE):
156 if (old->mask & FS_Q_OVERFLOW)
157 return true;
158 else if (old->mask & FS_IN_IGNORED)
159 return false;
160 return false;
161 };
162 }
163 return false;
164}
165
166/*
167 * Add an event to the group notification queue. The group can later pull this 136 * Add an event to the group notification queue. The group can later pull this
168 * event off the queue to deal with. If the event is successfully added to the 137 * event off the queue to deal with. If the event is successfully added to the
169 * group's notification queue, a reference is taken on event. 138 * group's notification queue, a reference is taken on event.
170 */ 139 */
171int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event, 140struct fsnotify_event *fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event,
172 struct fsnotify_event_private_data *priv) 141 struct fsnotify_event_private_data *priv,
142 struct fsnotify_event *(*merge)(struct list_head *,
143 struct fsnotify_event *))
173{ 144{
145 struct fsnotify_event *return_event = NULL;
174 struct fsnotify_event_holder *holder = NULL; 146 struct fsnotify_event_holder *holder = NULL;
175 struct list_head *list = &group->notification_list; 147 struct list_head *list = &group->notification_list;
176 struct fsnotify_event_holder *last_holder; 148
177 struct fsnotify_event *last_event; 149 pr_debug("%s: group=%p event=%p priv=%p\n", __func__, group, event, priv);
178 int ret = 0;
179 150
180 /* 151 /*
181 * There is one fsnotify_event_holder embedded inside each fsnotify_event. 152 * There is one fsnotify_event_holder embedded inside each fsnotify_event.
@@ -189,18 +160,40 @@ int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_even
189alloc_holder: 160alloc_holder:
190 holder = fsnotify_alloc_event_holder(); 161 holder = fsnotify_alloc_event_holder();
191 if (!holder) 162 if (!holder)
192 return -ENOMEM; 163 return ERR_PTR(-ENOMEM);
193 } 164 }
194 165
195 mutex_lock(&group->notification_mutex); 166 mutex_lock(&group->notification_mutex);
196 167
197 if (group->q_len >= group->max_events) { 168 if (group->q_len >= group->max_events) {
198 event = &q_overflow_event; 169 event = q_overflow_event;
199 ret = -EOVERFLOW; 170
171 /*
172 * we need to return the overflow event
173 * which means we need a ref
174 */
175 fsnotify_get_event(event);
176 return_event = event;
177
200 /* sorry, no private data on the overflow event */ 178 /* sorry, no private data on the overflow event */
201 priv = NULL; 179 priv = NULL;
202 } 180 }
203 181
182 if (!list_empty(list) && merge) {
183 struct fsnotify_event *tmp;
184
185 tmp = merge(list, event);
186 if (tmp) {
187 mutex_unlock(&group->notification_mutex);
188
189 if (return_event)
190 fsnotify_put_event(return_event);
191 if (holder != &event->holder)
192 fsnotify_destroy_event_holder(holder);
193 return tmp;
194 }
195 }
196
204 spin_lock(&event->lock); 197 spin_lock(&event->lock);
205 198
206 if (list_empty(&event->holder.event_list)) { 199 if (list_empty(&event->holder.event_list)) {
@@ -212,19 +205,13 @@ alloc_holder:
212 * event holder was used, go back and get a new one */ 205 * event holder was used, go back and get a new one */
213 spin_unlock(&event->lock); 206 spin_unlock(&event->lock);
214 mutex_unlock(&group->notification_mutex); 207 mutex_unlock(&group->notification_mutex);
215 goto alloc_holder;
216 }
217 208
218 if (!list_empty(list)) { 209 if (return_event) {
219 last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list); 210 fsnotify_put_event(return_event);
220 last_event = last_holder->event; 211 return_event = NULL;
221 if (event_compare(last_event, event)) {
222 spin_unlock(&event->lock);
223 mutex_unlock(&group->notification_mutex);
224 if (holder != &event->holder)
225 fsnotify_destroy_event_holder(holder);
226 return -EEXIST;
227 } 212 }
213
214 goto alloc_holder;
228 } 215 }
229 216
230 group->q_len++; 217 group->q_len++;
@@ -238,7 +225,7 @@ alloc_holder:
238 mutex_unlock(&group->notification_mutex); 225 mutex_unlock(&group->notification_mutex);
239 226
240 wake_up(&group->notification_waitq); 227 wake_up(&group->notification_waitq);
241 return ret; 228 return return_event;
242} 229}
243 230
244/* 231/*
@@ -253,6 +240,8 @@ struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group
253 240
254 BUG_ON(!mutex_is_locked(&group->notification_mutex)); 241 BUG_ON(!mutex_is_locked(&group->notification_mutex));
255 242
243 pr_debug("%s: group=%p\n", __func__, group);
244
256 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list); 245 holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
257 246
258 event = holder->event; 247 event = holder->event;
@@ -314,25 +303,82 @@ void fsnotify_flush_notify(struct fsnotify_group *group)
314 303
315static void initialize_event(struct fsnotify_event *event) 304static void initialize_event(struct fsnotify_event *event)
316{ 305{
317 event->holder.event = NULL;
318 INIT_LIST_HEAD(&event->holder.event_list); 306 INIT_LIST_HEAD(&event->holder.event_list);
319 atomic_set(&event->refcnt, 1); 307 atomic_set(&event->refcnt, 1);
320 308
321 spin_lock_init(&event->lock); 309 spin_lock_init(&event->lock);
322 310
323 event->path.dentry = NULL;
324 event->path.mnt = NULL;
325 event->inode = NULL;
326 event->data_type = FSNOTIFY_EVENT_NONE;
327
328 INIT_LIST_HEAD(&event->private_data_list); 311 INIT_LIST_HEAD(&event->private_data_list);
312}
313
314/*
315 * Caller damn well better be holding whatever mutex is protecting the
316 * old_holder->event_list and the new_event must be a clean event which
317 * cannot be found anywhere else in the kernel.
318 */
319int fsnotify_replace_event(struct fsnotify_event_holder *old_holder,
320 struct fsnotify_event *new_event)
321{
322 struct fsnotify_event *old_event = old_holder->event;
323 struct fsnotify_event_holder *new_holder = &new_event->holder;
329 324
330 event->to_tell = NULL; 325 enum event_spinlock_class {
326 SPINLOCK_OLD,
327 SPINLOCK_NEW,
328 };
331 329
332 event->file_name = NULL; 330 pr_debug("%s: old_event=%p new_event=%p\n", __func__, old_event, new_event);
333 event->name_len = 0;
334 331
335 event->sync_cookie = 0; 332 /*
333 * if the new_event's embedded holder is in use someone
334 * screwed up and didn't give us a clean new event.
335 */
336 BUG_ON(!list_empty(&new_holder->event_list));
337
338 spin_lock_nested(&old_event->lock, SPINLOCK_OLD);
339 spin_lock_nested(&new_event->lock, SPINLOCK_NEW);
340
341 new_holder->event = new_event;
342 list_replace_init(&old_holder->event_list, &new_holder->event_list);
343
344 spin_unlock(&new_event->lock);
345 spin_unlock(&old_event->lock);
346
347 /* event == holder means we are referenced through the in event holder */
348 if (old_holder != &old_event->holder)
349 fsnotify_destroy_event_holder(old_holder);
350
351 fsnotify_get_event(new_event); /* on the list take reference */
352 fsnotify_put_event(old_event); /* off the list, drop reference */
353
354 return 0;
355}
356
357struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event)
358{
359 struct fsnotify_event *event;
360
361 event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL);
362 if (!event)
363 return NULL;
364
365 pr_debug("%s: old_event=%p new_event=%p\n", __func__, old_event, event);
366
367 memcpy(event, old_event, sizeof(*event));
368 initialize_event(event);
369
370 if (event->name_len) {
371 event->file_name = kstrdup(old_event->file_name, GFP_KERNEL);
372 if (!event->file_name) {
373 kmem_cache_free(fsnotify_event_cachep, event);
374 return NULL;
375 }
376 }
377 event->tgid = get_pid(old_event->tgid);
378 if (event->data_type == FSNOTIFY_EVENT_PATH)
379 path_get(&event->path);
380
381 return event;
336} 382}
337 383
338/* 384/*
@@ -348,15 +394,18 @@ static void initialize_event(struct fsnotify_event *event)
348 * @name the filename, if available 394 * @name the filename, if available
349 */ 395 */
350struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data, 396struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
351 int data_type, const char *name, u32 cookie, 397 int data_type, const unsigned char *name,
352 gfp_t gfp) 398 u32 cookie, gfp_t gfp)
353{ 399{
354 struct fsnotify_event *event; 400 struct fsnotify_event *event;
355 401
356 event = kmem_cache_alloc(fsnotify_event_cachep, gfp); 402 event = kmem_cache_zalloc(fsnotify_event_cachep, gfp);
357 if (!event) 403 if (!event)
358 return NULL; 404 return NULL;
359 405
406 pr_debug("%s: event=%p to_tell=%p mask=%x data=%p data_type=%d\n",
407 __func__, event, to_tell, mask, data, data_type);
408
360 initialize_event(event); 409 initialize_event(event);
361 410
362 if (name) { 411 if (name) {
@@ -368,30 +417,21 @@ struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask,
368 event->name_len = strlen(event->file_name); 417 event->name_len = strlen(event->file_name);
369 } 418 }
370 419
420 event->tgid = get_pid(task_tgid(current));
371 event->sync_cookie = cookie; 421 event->sync_cookie = cookie;
372 event->to_tell = to_tell; 422 event->to_tell = to_tell;
423 event->data_type = data_type;
373 424
374 switch (data_type) { 425 switch (data_type) {
375 case FSNOTIFY_EVENT_FILE: {
376 struct file *file = data;
377 struct path *path = &file->f_path;
378 event->path.dentry = path->dentry;
379 event->path.mnt = path->mnt;
380 path_get(&event->path);
381 event->data_type = FSNOTIFY_EVENT_PATH;
382 break;
383 }
384 case FSNOTIFY_EVENT_PATH: { 426 case FSNOTIFY_EVENT_PATH: {
385 struct path *path = data; 427 struct path *path = data;
386 event->path.dentry = path->dentry; 428 event->path.dentry = path->dentry;
387 event->path.mnt = path->mnt; 429 event->path.mnt = path->mnt;
388 path_get(&event->path); 430 path_get(&event->path);
389 event->data_type = FSNOTIFY_EVENT_PATH;
390 break; 431 break;
391 } 432 }
392 case FSNOTIFY_EVENT_INODE: 433 case FSNOTIFY_EVENT_INODE:
393 event->inode = data; 434 event->inode = data;
394 event->data_type = FSNOTIFY_EVENT_INODE;
395 break; 435 break;
396 case FSNOTIFY_EVENT_NONE: 436 case FSNOTIFY_EVENT_NONE:
397 event->inode = NULL; 437 event->inode = NULL;
@@ -412,8 +452,11 @@ __init int fsnotify_notification_init(void)
412 fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC); 452 fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
413 fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC); 453 fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
414 454
415 initialize_event(&q_overflow_event); 455 q_overflow_event = fsnotify_create_event(NULL, FS_Q_OVERFLOW, NULL,
416 q_overflow_event.mask = FS_Q_OVERFLOW; 456 FSNOTIFY_EVENT_NONE, NULL, 0,
457 GFP_KERNEL);
458 if (!q_overflow_event)
459 panic("unable to allocate fsnotify q_overflow_event\n");
417 460
418 return 0; 461 return 0;
419} 462}