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 | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 93 | kfree(event->file_name); |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 94 | kmem_cache_free(fsnotify_event_cachep, event); |
| 95 | } |
| 96 | } |
| 97 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 98 | struct fsnotify_event_holder *fsnotify_alloc_event_holder(void) |
| 99 | { |
| 100 | return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL); |
| 101 | } |
| 102 | |
| 103 | void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder) |
| 104 | { |
| 105 | kmem_cache_free(fsnotify_event_holder_cachep, holder); |
| 106 | } |
| 107 | |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 108 | /* |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 109 | * check if 2 events contain the same information. |
| 110 | */ |
| 111 | static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new) |
| 112 | { |
| 113 | if ((old->mask == new->mask) && |
| 114 | (old->to_tell == new->to_tell) && |
| 115 | (old->data_type == new->data_type)) { |
| 116 | switch (old->data_type) { |
| 117 | case (FSNOTIFY_EVENT_INODE): |
| 118 | if (old->inode == new->inode) |
| 119 | return true; |
| 120 | break; |
| 121 | case (FSNOTIFY_EVENT_PATH): |
| 122 | if ((old->path.mnt == new->path.mnt) && |
| 123 | (old->path.dentry == new->path.dentry)) |
| 124 | return true; |
| 125 | case (FSNOTIFY_EVENT_NONE): |
| 126 | return true; |
| 127 | }; |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Add an event to the group notification queue. The group can later pull this |
| 134 | * event off the queue to deal with. If the event is successfully added to the |
| 135 | * group's notification queue, a reference is taken on event. |
| 136 | */ |
| 137 | int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event) |
| 138 | { |
| 139 | struct fsnotify_event_holder *holder = NULL; |
| 140 | struct list_head *list = &group->notification_list; |
| 141 | struct fsnotify_event_holder *last_holder; |
| 142 | struct fsnotify_event *last_event; |
| 143 | |
| 144 | /* |
| 145 | * There is one fsnotify_event_holder embedded inside each fsnotify_event. |
| 146 | * Check if we expect to be able to use that holder. If not alloc a new |
| 147 | * holder. |
| 148 | * For the overflow event it's possible that something will use the in |
| 149 | * event holder before we get the lock so we may need to jump back and |
| 150 | * alloc a new holder, this can't happen for most events... |
| 151 | */ |
| 152 | if (!list_empty(&event->holder.event_list)) { |
| 153 | alloc_holder: |
| 154 | holder = fsnotify_alloc_event_holder(); |
| 155 | if (!holder) |
| 156 | return -ENOMEM; |
| 157 | } |
| 158 | |
| 159 | mutex_lock(&group->notification_mutex); |
| 160 | |
| 161 | if (group->q_len >= group->max_events) |
| 162 | event = &q_overflow_event; |
| 163 | |
| 164 | spin_lock(&event->lock); |
| 165 | |
| 166 | if (list_empty(&event->holder.event_list)) { |
| 167 | if (unlikely(holder)) |
| 168 | fsnotify_destroy_event_holder(holder); |
| 169 | holder = &event->holder; |
| 170 | } else if (unlikely(!holder)) { |
| 171 | /* between the time we checked above and got the lock the in |
| 172 | * event holder was used, go back and get a new one */ |
| 173 | spin_unlock(&event->lock); |
| 174 | mutex_unlock(&group->notification_mutex); |
| 175 | goto alloc_holder; |
| 176 | } |
| 177 | |
| 178 | if (!list_empty(list)) { |
| 179 | last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list); |
| 180 | last_event = last_holder->event; |
| 181 | if (event_compare(last_event, event)) { |
| 182 | spin_unlock(&event->lock); |
| 183 | mutex_unlock(&group->notification_mutex); |
| 184 | if (holder != &event->holder) |
| 185 | fsnotify_destroy_event_holder(holder); |
| 186 | return 0; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | group->q_len++; |
| 191 | holder->event = event; |
| 192 | |
| 193 | fsnotify_get_event(event); |
| 194 | list_add_tail(&holder->event_list, list); |
| 195 | spin_unlock(&event->lock); |
| 196 | mutex_unlock(&group->notification_mutex); |
| 197 | |
| 198 | wake_up(&group->notification_waitq); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Remove and return the first event from the notification list. There is a |
| 204 | * reference held on this event since it was on the list. It is the responsibility |
| 205 | * of the caller to drop this reference. |
| 206 | */ |
| 207 | struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group) |
| 208 | { |
| 209 | struct fsnotify_event *event; |
| 210 | struct fsnotify_event_holder *holder; |
| 211 | |
| 212 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
| 213 | |
| 214 | holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list); |
| 215 | |
| 216 | event = holder->event; |
| 217 | |
| 218 | spin_lock(&event->lock); |
| 219 | holder->event = NULL; |
| 220 | list_del_init(&holder->event_list); |
| 221 | spin_unlock(&event->lock); |
| 222 | |
| 223 | /* event == holder means we are referenced through the in event holder */ |
| 224 | if (holder != &event->holder) |
| 225 | fsnotify_destroy_event_holder(holder); |
| 226 | |
| 227 | group->q_len--; |
| 228 | |
| 229 | return event; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * This will not remove the event, that must be done with fsnotify_remove_notify_event() |
| 234 | */ |
| 235 | struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group) |
| 236 | { |
| 237 | struct fsnotify_event *event; |
| 238 | struct fsnotify_event_holder *holder; |
| 239 | |
| 240 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
| 241 | |
| 242 | holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list); |
| 243 | event = holder->event; |
| 244 | |
| 245 | return event; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Called when a group is being torn down to clean up any outstanding |
| 250 | * event notifications. |
| 251 | */ |
| 252 | void fsnotify_flush_notify(struct fsnotify_group *group) |
| 253 | { |
| 254 | struct fsnotify_event *event; |
| 255 | |
| 256 | mutex_lock(&group->notification_mutex); |
| 257 | while (!fsnotify_notify_queue_is_empty(group)) { |
| 258 | event = fsnotify_remove_notify_event(group); |
| 259 | fsnotify_put_event(event); /* matches fsnotify_add_notify_event */ |
| 260 | } |
| 261 | mutex_unlock(&group->notification_mutex); |
| 262 | } |
| 263 | |
| 264 | static void initialize_event(struct fsnotify_event *event) |
| 265 | { |
| 266 | event->holder.event = NULL; |
| 267 | INIT_LIST_HEAD(&event->holder.event_list); |
| 268 | atomic_set(&event->refcnt, 1); |
| 269 | |
| 270 | spin_lock_init(&event->lock); |
| 271 | |
| 272 | event->path.dentry = NULL; |
| 273 | event->path.mnt = NULL; |
| 274 | event->inode = NULL; |
| 275 | event->data_type = FSNOTIFY_EVENT_NONE; |
| 276 | |
| 277 | event->to_tell = NULL; |
Eric Paris | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 278 | |
| 279 | event->file_name = NULL; |
| 280 | event->name_len = 0; |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame^] | 281 | |
| 282 | event->sync_cookie = 0; |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* |
| 286 | * fsnotify_create_event - Allocate a new event which will be sent to each |
| 287 | * group's handle_event function if the group was interested in this |
| 288 | * particular event. |
| 289 | * |
| 290 | * @to_tell the inode which is supposed to receive the event (sometimes a |
| 291 | * parent of the inode to which the event happened. |
| 292 | * @mask what actually happened. |
| 293 | * @data pointer to the object which was actually affected |
| 294 | * @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] | 295 | * @name the filename, if available |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 296 | */ |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame^] | 297 | struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data, |
| 298 | int data_type, const char *name, u32 cookie) |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 299 | { |
| 300 | struct fsnotify_event *event; |
| 301 | |
| 302 | event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL); |
| 303 | if (!event) |
| 304 | return NULL; |
| 305 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 306 | initialize_event(event); |
Eric Paris | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 307 | |
| 308 | if (name) { |
| 309 | event->file_name = kstrdup(name, GFP_KERNEL); |
| 310 | if (!event->file_name) { |
| 311 | kmem_cache_free(fsnotify_event_cachep, event); |
| 312 | return NULL; |
| 313 | } |
| 314 | event->name_len = strlen(event->file_name); |
| 315 | } |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame^] | 316 | |
| 317 | event->sync_cookie = cookie; |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 318 | event->to_tell = to_tell; |
| 319 | |
| 320 | switch (data_type) { |
| 321 | case FSNOTIFY_EVENT_FILE: { |
| 322 | struct file *file = data; |
| 323 | struct path *path = &file->f_path; |
| 324 | event->path.dentry = path->dentry; |
| 325 | event->path.mnt = path->mnt; |
| 326 | path_get(&event->path); |
| 327 | event->data_type = FSNOTIFY_EVENT_PATH; |
| 328 | break; |
| 329 | } |
| 330 | case FSNOTIFY_EVENT_PATH: { |
| 331 | struct path *path = data; |
| 332 | event->path.dentry = path->dentry; |
| 333 | event->path.mnt = path->mnt; |
| 334 | path_get(&event->path); |
| 335 | event->data_type = FSNOTIFY_EVENT_PATH; |
| 336 | break; |
| 337 | } |
| 338 | case FSNOTIFY_EVENT_INODE: |
| 339 | event->inode = data; |
| 340 | event->data_type = FSNOTIFY_EVENT_INODE; |
| 341 | break; |
| 342 | case FSNOTIFY_EVENT_NONE: |
| 343 | event->inode = NULL; |
| 344 | event->path.dentry = NULL; |
| 345 | event->path.mnt = NULL; |
| 346 | break; |
| 347 | default: |
| 348 | BUG(); |
| 349 | } |
| 350 | |
| 351 | event->mask = mask; |
| 352 | |
| 353 | return event; |
| 354 | } |
| 355 | |
| 356 | __init int fsnotify_notification_init(void) |
| 357 | { |
| 358 | fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC); |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 359 | fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC); |
| 360 | |
| 361 | initialize_event(&q_overflow_event); |
| 362 | q_overflow_event.mask = FS_Q_OVERFLOW; |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | subsys_initcall(fsnotify_notification_init); |
| 367 | |