Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 19 | /* |
| 20 | * Basic idea behind the notification queue: An fsnotify group (like inotify) |
| 21 | * sends the userspace notification about events asyncronously some time after |
| 22 | * the event happened. When inotify gets an event it will need to add that |
| 23 | * event to the group notify queue. Since a single event might need to be on |
| 24 | * multiple group's notification queues we can't add the event directly to each |
| 25 | * queue and instead add a small "event_holder" to each queue. This event_holder |
| 26 | * has a pointer back to the original event. Since the majority of events are |
| 27 | * going to end up on one, and only one, notification queue we embed one |
| 28 | * event_holder into each event. This means we have a single allocation instead |
| 29 | * of always needing two. If the embedded event_holder is already in use by |
| 30 | * another group a new event_holder (from fsnotify_event_holder_cachep) will be |
| 31 | * allocated and used. |
| 32 | */ |
| 33 | |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 34 | #include <linux/fs.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/kernel.h> |
| 37 | #include <linux/list.h> |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame] | 38 | #include <linux/module.h> |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 39 | #include <linux/mount.h> |
| 40 | #include <linux/mutex.h> |
| 41 | #include <linux/namei.h> |
| 42 | #include <linux/path.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/spinlock.h> |
| 45 | |
| 46 | #include <asm/atomic.h> |
| 47 | |
| 48 | #include <linux/fsnotify_backend.h> |
| 49 | #include "fsnotify.h" |
| 50 | |
| 51 | static struct kmem_cache *fsnotify_event_cachep; |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 52 | static struct kmem_cache *fsnotify_event_holder_cachep; |
| 53 | /* |
| 54 | * This is a magic event we send when the q is too full. Since it doesn't |
| 55 | * hold real event information we just keep one system wide and use it any time |
| 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' |
| 58 | */ |
| 59 | static struct fsnotify_event q_overflow_event; |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame] | 60 | static atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0); |
| 61 | |
| 62 | /** |
| 63 | * fsnotify_get_cookie - return a unique cookie for use in synchronizing events. |
| 64 | * Called from fsnotify_move, which is inlined into filesystem modules. |
| 65 | */ |
| 66 | u32 fsnotify_get_cookie(void) |
| 67 | { |
| 68 | return atomic_inc_return(&fsnotify_sync_cookie); |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(fsnotify_get_cookie); |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 71 | |
| 72 | /* return true if the notify queue is empty, false otherwise */ |
| 73 | bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group) |
| 74 | { |
| 75 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
| 76 | return list_empty(&group->notification_list) ? true : false; |
| 77 | } |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 78 | |
| 79 | void fsnotify_get_event(struct fsnotify_event *event) |
| 80 | { |
| 81 | atomic_inc(&event->refcnt); |
| 82 | } |
| 83 | |
| 84 | void fsnotify_put_event(struct fsnotify_event *event) |
| 85 | { |
| 86 | if (!event) |
| 87 | return; |
| 88 | |
| 89 | if (atomic_dec_and_test(&event->refcnt)) { |
| 90 | if (event->data_type == FSNOTIFY_EVENT_PATH) |
| 91 | path_put(&event->path); |
| 92 | |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 93 | BUG_ON(!list_empty(&event->private_data_list)); |
| 94 | |
Eric Paris | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 95 | kfree(event->file_name); |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 96 | kmem_cache_free(fsnotify_event_cachep, event); |
| 97 | } |
| 98 | } |
| 99 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 100 | struct fsnotify_event_holder *fsnotify_alloc_event_holder(void) |
| 101 | { |
| 102 | return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL); |
| 103 | } |
| 104 | |
| 105 | void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder) |
| 106 | { |
| 107 | kmem_cache_free(fsnotify_event_holder_cachep, holder); |
| 108 | } |
| 109 | |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 110 | /* |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 111 | * Find the private data that the group previously attached to this event when |
| 112 | * the group added the event to the notification queue (fsnotify_add_notify_event) |
| 113 | */ |
| 114 | struct fsnotify_event_private_data *fsnotify_remove_priv_from_event(struct fsnotify_group *group, struct fsnotify_event *event) |
| 115 | { |
| 116 | struct fsnotify_event_private_data *lpriv; |
| 117 | struct fsnotify_event_private_data *priv = NULL; |
| 118 | |
| 119 | assert_spin_locked(&event->lock); |
| 120 | |
| 121 | list_for_each_entry(lpriv, &event->private_data_list, event_list) { |
| 122 | if (lpriv->group == group) { |
| 123 | priv = lpriv; |
| 124 | list_del(&priv->event_list); |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | return priv; |
| 129 | } |
| 130 | |
| 131 | /* |
| 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. |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 134 | */ |
| 135 | static 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) && |
Eric Paris | 4a148ba | 2009-07-13 15:56:55 -0400 | [diff] [blame] | 139 | (old->data_type == new->data_type) && |
| 140 | (old->name_len == new->name_len)) { |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 141 | switch (old->data_type) { |
| 142 | case (FSNOTIFY_EVENT_INODE): |
Eric Paris | 4a148ba | 2009-07-13 15:56:55 -0400 | [diff] [blame] | 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)) |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 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; |
Eric Paris | c05594b | 2009-07-13 15:56:55 -0400 | [diff] [blame] | 154 | break; |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 155 | case (FSNOTIFY_EVENT_NONE): |
Eric Paris | f44aebc | 2009-07-15 15:49:52 -0400 | [diff] [blame] | 156 | return false; |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 157 | }; |
| 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Add an event to the group notification queue. The group can later pull this |
| 164 | * event off the queue to deal with. If the event is successfully added to the |
| 165 | * group's notification queue, a reference is taken on event. |
| 166 | */ |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 167 | int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event, |
| 168 | struct fsnotify_event_private_data *priv) |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 169 | { |
| 170 | struct fsnotify_event_holder *holder = NULL; |
| 171 | struct list_head *list = &group->notification_list; |
| 172 | struct fsnotify_event_holder *last_holder; |
| 173 | struct fsnotify_event *last_event; |
| 174 | |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 175 | /* easy to tell if priv was attached to the event */ |
| 176 | INIT_LIST_HEAD(&priv->event_list); |
| 177 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 178 | /* |
| 179 | * There is one fsnotify_event_holder embedded inside each fsnotify_event. |
| 180 | * Check if we expect to be able to use that holder. If not alloc a new |
| 181 | * holder. |
| 182 | * For the overflow event it's possible that something will use the in |
| 183 | * event holder before we get the lock so we may need to jump back and |
| 184 | * alloc a new holder, this can't happen for most events... |
| 185 | */ |
| 186 | if (!list_empty(&event->holder.event_list)) { |
| 187 | alloc_holder: |
| 188 | holder = fsnotify_alloc_event_holder(); |
| 189 | if (!holder) |
| 190 | return -ENOMEM; |
| 191 | } |
| 192 | |
| 193 | mutex_lock(&group->notification_mutex); |
| 194 | |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 195 | if (group->q_len >= group->max_events) { |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 196 | event = &q_overflow_event; |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 197 | /* sorry, no private data on the overflow event */ |
| 198 | priv = NULL; |
| 199 | } |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 200 | |
| 201 | spin_lock(&event->lock); |
| 202 | |
| 203 | if (list_empty(&event->holder.event_list)) { |
| 204 | if (unlikely(holder)) |
| 205 | fsnotify_destroy_event_holder(holder); |
| 206 | holder = &event->holder; |
| 207 | } else if (unlikely(!holder)) { |
| 208 | /* between the time we checked above and got the lock the in |
| 209 | * event holder was used, go back and get a new one */ |
| 210 | spin_unlock(&event->lock); |
| 211 | mutex_unlock(&group->notification_mutex); |
| 212 | goto alloc_holder; |
| 213 | } |
| 214 | |
| 215 | if (!list_empty(list)) { |
| 216 | last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list); |
| 217 | last_event = last_holder->event; |
| 218 | if (event_compare(last_event, event)) { |
| 219 | spin_unlock(&event->lock); |
| 220 | mutex_unlock(&group->notification_mutex); |
| 221 | if (holder != &event->holder) |
| 222 | fsnotify_destroy_event_holder(holder); |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 223 | return -EEXIST; |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | group->q_len++; |
| 228 | holder->event = event; |
| 229 | |
| 230 | fsnotify_get_event(event); |
| 231 | list_add_tail(&holder->event_list, list); |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 232 | if (priv) |
| 233 | list_add_tail(&priv->event_list, &event->private_data_list); |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 234 | spin_unlock(&event->lock); |
| 235 | mutex_unlock(&group->notification_mutex); |
| 236 | |
| 237 | wake_up(&group->notification_waitq); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Remove and return the first event from the notification list. There is a |
| 243 | * reference held on this event since it was on the list. It is the responsibility |
| 244 | * of the caller to drop this reference. |
| 245 | */ |
| 246 | struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group) |
| 247 | { |
| 248 | struct fsnotify_event *event; |
| 249 | struct fsnotify_event_holder *holder; |
| 250 | |
| 251 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
| 252 | |
| 253 | holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list); |
| 254 | |
| 255 | event = holder->event; |
| 256 | |
| 257 | spin_lock(&event->lock); |
| 258 | holder->event = NULL; |
| 259 | list_del_init(&holder->event_list); |
| 260 | spin_unlock(&event->lock); |
| 261 | |
| 262 | /* event == holder means we are referenced through the in event holder */ |
| 263 | if (holder != &event->holder) |
| 264 | fsnotify_destroy_event_holder(holder); |
| 265 | |
| 266 | group->q_len--; |
| 267 | |
| 268 | return event; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * This will not remove the event, that must be done with fsnotify_remove_notify_event() |
| 273 | */ |
| 274 | struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group) |
| 275 | { |
| 276 | struct fsnotify_event *event; |
| 277 | struct fsnotify_event_holder *holder; |
| 278 | |
| 279 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
| 280 | |
| 281 | holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list); |
| 282 | event = holder->event; |
| 283 | |
| 284 | return event; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Called when a group is being torn down to clean up any outstanding |
| 289 | * event notifications. |
| 290 | */ |
| 291 | void fsnotify_flush_notify(struct fsnotify_group *group) |
| 292 | { |
| 293 | struct fsnotify_event *event; |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 294 | struct fsnotify_event_private_data *priv; |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 295 | |
| 296 | mutex_lock(&group->notification_mutex); |
| 297 | while (!fsnotify_notify_queue_is_empty(group)) { |
| 298 | event = fsnotify_remove_notify_event(group); |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 299 | /* if they don't implement free_event_priv they better not have attached any */ |
| 300 | if (group->ops->free_event_priv) { |
| 301 | spin_lock(&event->lock); |
| 302 | priv = fsnotify_remove_priv_from_event(group, event); |
| 303 | spin_unlock(&event->lock); |
| 304 | if (priv) |
| 305 | group->ops->free_event_priv(priv); |
| 306 | } |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 307 | fsnotify_put_event(event); /* matches fsnotify_add_notify_event */ |
| 308 | } |
| 309 | mutex_unlock(&group->notification_mutex); |
| 310 | } |
| 311 | |
| 312 | static void initialize_event(struct fsnotify_event *event) |
| 313 | { |
| 314 | event->holder.event = NULL; |
| 315 | INIT_LIST_HEAD(&event->holder.event_list); |
| 316 | atomic_set(&event->refcnt, 1); |
| 317 | |
| 318 | spin_lock_init(&event->lock); |
| 319 | |
| 320 | event->path.dentry = NULL; |
| 321 | event->path.mnt = NULL; |
| 322 | event->inode = NULL; |
| 323 | event->data_type = FSNOTIFY_EVENT_NONE; |
| 324 | |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 325 | INIT_LIST_HEAD(&event->private_data_list); |
| 326 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 327 | event->to_tell = NULL; |
Eric Paris | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 328 | |
| 329 | event->file_name = NULL; |
| 330 | event->name_len = 0; |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame] | 331 | |
| 332 | event->sync_cookie = 0; |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* |
| 336 | * fsnotify_create_event - Allocate a new event which will be sent to each |
| 337 | * group's handle_event function if the group was interested in this |
| 338 | * particular event. |
| 339 | * |
| 340 | * @to_tell the inode which is supposed to receive the event (sometimes a |
| 341 | * parent of the inode to which the event happened. |
| 342 | * @mask what actually happened. |
| 343 | * @data pointer to the object which was actually affected |
| 344 | * @data_type flag indication if the data is a file, path, inode, nothing... |
Eric Paris | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 345 | * @name the filename, if available |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 346 | */ |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame] | 347 | struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data, |
Eric Paris | f44aebc | 2009-07-15 15:49:52 -0400 | [diff] [blame] | 348 | int data_type, const char *name, u32 cookie, |
| 349 | gfp_t gfp) |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 350 | { |
| 351 | struct fsnotify_event *event; |
| 352 | |
Eric Paris | f44aebc | 2009-07-15 15:49:52 -0400 | [diff] [blame] | 353 | event = kmem_cache_alloc(fsnotify_event_cachep, gfp); |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 354 | if (!event) |
| 355 | return NULL; |
| 356 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 357 | initialize_event(event); |
Eric Paris | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 358 | |
| 359 | if (name) { |
Eric Paris | f44aebc | 2009-07-15 15:49:52 -0400 | [diff] [blame] | 360 | event->file_name = kstrdup(name, gfp); |
Eric Paris | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 361 | if (!event->file_name) { |
| 362 | kmem_cache_free(fsnotify_event_cachep, event); |
| 363 | return NULL; |
| 364 | } |
| 365 | event->name_len = strlen(event->file_name); |
| 366 | } |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame] | 367 | |
| 368 | event->sync_cookie = cookie; |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 369 | event->to_tell = to_tell; |
| 370 | |
| 371 | switch (data_type) { |
| 372 | case FSNOTIFY_EVENT_FILE: { |
| 373 | struct file *file = data; |
| 374 | struct path *path = &file->f_path; |
| 375 | event->path.dentry = path->dentry; |
| 376 | event->path.mnt = path->mnt; |
| 377 | path_get(&event->path); |
| 378 | event->data_type = FSNOTIFY_EVENT_PATH; |
| 379 | break; |
| 380 | } |
| 381 | case FSNOTIFY_EVENT_PATH: { |
| 382 | struct path *path = data; |
| 383 | event->path.dentry = path->dentry; |
| 384 | event->path.mnt = path->mnt; |
| 385 | path_get(&event->path); |
| 386 | event->data_type = FSNOTIFY_EVENT_PATH; |
| 387 | break; |
| 388 | } |
| 389 | case FSNOTIFY_EVENT_INODE: |
| 390 | event->inode = data; |
| 391 | event->data_type = FSNOTIFY_EVENT_INODE; |
| 392 | break; |
| 393 | case FSNOTIFY_EVENT_NONE: |
| 394 | event->inode = NULL; |
| 395 | event->path.dentry = NULL; |
| 396 | event->path.mnt = NULL; |
| 397 | break; |
| 398 | default: |
| 399 | BUG(); |
| 400 | } |
| 401 | |
| 402 | event->mask = mask; |
| 403 | |
| 404 | return event; |
| 405 | } |
| 406 | |
| 407 | __init int fsnotify_notification_init(void) |
| 408 | { |
| 409 | fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC); |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 410 | fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC); |
| 411 | |
| 412 | initialize_event(&q_overflow_event); |
| 413 | q_overflow_event.mask = FS_Q_OVERFLOW; |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | subsys_initcall(fsnotify_notification_init); |
| 418 | |