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 | */ |
Eric Paris | b4277d3 | 2009-12-17 20:12:06 -0500 | [diff] [blame] | 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)) { |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame^] | 90 | pr_debug("%s: event=%p\n", __func__, event); |
| 91 | |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 92 | if (event->data_type == FSNOTIFY_EVENT_PATH) |
| 93 | path_put(&event->path); |
| 94 | |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 95 | BUG_ON(!list_empty(&event->private_data_list)); |
| 96 | |
Eric Paris | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 97 | kfree(event->file_name); |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 98 | put_pid(event->tgid); |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 99 | kmem_cache_free(fsnotify_event_cachep, event); |
| 100 | } |
| 101 | } |
| 102 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 103 | struct fsnotify_event_holder *fsnotify_alloc_event_holder(void) |
| 104 | { |
| 105 | return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL); |
| 106 | } |
| 107 | |
| 108 | void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder) |
| 109 | { |
Eric Paris | 74766bb | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 110 | if (holder) |
| 111 | kmem_cache_free(fsnotify_event_holder_cachep, holder); |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 112 | } |
| 113 | |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 114 | /* |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 115 | * Find the private data that the group previously attached to this event when |
| 116 | * the group added the event to the notification queue (fsnotify_add_notify_event) |
| 117 | */ |
| 118 | struct fsnotify_event_private_data *fsnotify_remove_priv_from_event(struct fsnotify_group *group, struct fsnotify_event *event) |
| 119 | { |
| 120 | struct fsnotify_event_private_data *lpriv; |
| 121 | struct fsnotify_event_private_data *priv = NULL; |
| 122 | |
| 123 | assert_spin_locked(&event->lock); |
| 124 | |
| 125 | list_for_each_entry(lpriv, &event->private_data_list, event_list) { |
| 126 | if (lpriv->group == group) { |
| 127 | priv = lpriv; |
| 128 | list_del(&priv->event_list); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | return priv; |
| 133 | } |
| 134 | |
| 135 | /* |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 136 | * Add an event to the group notification queue. The group can later pull this |
| 137 | * event off the queue to deal with. If the event is successfully added to the |
| 138 | * group's notification queue, a reference is taken on event. |
| 139 | */ |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 140 | int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event, |
Eric Paris | 74766bb | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 141 | struct fsnotify_event_private_data *priv, |
Eric Paris | 6e5f77b | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 142 | int (*merge)(struct list_head *, |
| 143 | struct fsnotify_event *, |
| 144 | void **arg), |
| 145 | void **arg) |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 146 | { |
| 147 | struct fsnotify_event_holder *holder = NULL; |
| 148 | struct list_head *list = &group->notification_list; |
Eric Paris | 74766bb | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 149 | int rc = 0; |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 150 | |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame^] | 151 | pr_debug("%s: group=%p event=%p priv=%p\n", __func__, group, event, priv); |
| 152 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 153 | /* |
| 154 | * There is one fsnotify_event_holder embedded inside each fsnotify_event. |
| 155 | * Check if we expect to be able to use that holder. If not alloc a new |
| 156 | * holder. |
| 157 | * For the overflow event it's possible that something will use the in |
| 158 | * event holder before we get the lock so we may need to jump back and |
| 159 | * alloc a new holder, this can't happen for most events... |
| 160 | */ |
| 161 | if (!list_empty(&event->holder.event_list)) { |
| 162 | alloc_holder: |
| 163 | holder = fsnotify_alloc_event_holder(); |
| 164 | if (!holder) |
| 165 | return -ENOMEM; |
| 166 | } |
| 167 | |
| 168 | mutex_lock(&group->notification_mutex); |
| 169 | |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 170 | if (group->q_len >= group->max_events) { |
Eric Paris | b4277d3 | 2009-12-17 20:12:06 -0500 | [diff] [blame] | 171 | event = q_overflow_event; |
Eric Paris | 74766bb | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 172 | rc = -EOVERFLOW; |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 173 | /* sorry, no private data on the overflow event */ |
| 174 | priv = NULL; |
| 175 | } |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 176 | |
Eric Paris | 74766bb | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 177 | if (!list_empty(list) && merge) { |
| 178 | int ret; |
| 179 | |
Eric Paris | 6e5f77b | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 180 | ret = merge(list, event, arg); |
Eric Paris | 74766bb | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 181 | if (ret) { |
| 182 | mutex_unlock(&group->notification_mutex); |
| 183 | if (holder != &event->holder) |
| 184 | fsnotify_destroy_event_holder(holder); |
| 185 | return ret; |
| 186 | } |
| 187 | } |
| 188 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 189 | spin_lock(&event->lock); |
| 190 | |
| 191 | if (list_empty(&event->holder.event_list)) { |
| 192 | if (unlikely(holder)) |
| 193 | fsnotify_destroy_event_holder(holder); |
| 194 | holder = &event->holder; |
| 195 | } else if (unlikely(!holder)) { |
| 196 | /* between the time we checked above and got the lock the in |
| 197 | * event holder was used, go back and get a new one */ |
| 198 | spin_unlock(&event->lock); |
| 199 | mutex_unlock(&group->notification_mutex); |
| 200 | goto alloc_holder; |
| 201 | } |
| 202 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 203 | group->q_len++; |
| 204 | holder->event = event; |
| 205 | |
| 206 | fsnotify_get_event(event); |
| 207 | list_add_tail(&holder->event_list, list); |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 208 | if (priv) |
| 209 | list_add_tail(&priv->event_list, &event->private_data_list); |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 210 | spin_unlock(&event->lock); |
| 211 | mutex_unlock(&group->notification_mutex); |
| 212 | |
| 213 | wake_up(&group->notification_waitq); |
Eric Paris | 74766bb | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 214 | return rc; |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Remove and return the first event from the notification list. There is a |
| 219 | * reference held on this event since it was on the list. It is the responsibility |
| 220 | * of the caller to drop this reference. |
| 221 | */ |
| 222 | struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group) |
| 223 | { |
| 224 | struct fsnotify_event *event; |
| 225 | struct fsnotify_event_holder *holder; |
| 226 | |
| 227 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
| 228 | |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame^] | 229 | pr_debug("%s: group=%p\n", __func__, group); |
| 230 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 231 | holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list); |
| 232 | |
| 233 | event = holder->event; |
| 234 | |
| 235 | spin_lock(&event->lock); |
| 236 | holder->event = NULL; |
| 237 | list_del_init(&holder->event_list); |
| 238 | spin_unlock(&event->lock); |
| 239 | |
| 240 | /* event == holder means we are referenced through the in event holder */ |
| 241 | if (holder != &event->holder) |
| 242 | fsnotify_destroy_event_holder(holder); |
| 243 | |
| 244 | group->q_len--; |
| 245 | |
| 246 | return event; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * This will not remove the event, that must be done with fsnotify_remove_notify_event() |
| 251 | */ |
| 252 | struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group) |
| 253 | { |
| 254 | struct fsnotify_event *event; |
| 255 | struct fsnotify_event_holder *holder; |
| 256 | |
| 257 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
| 258 | |
| 259 | holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list); |
| 260 | event = holder->event; |
| 261 | |
| 262 | return event; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Called when a group is being torn down to clean up any outstanding |
| 267 | * event notifications. |
| 268 | */ |
| 269 | void fsnotify_flush_notify(struct fsnotify_group *group) |
| 270 | { |
| 271 | struct fsnotify_event *event; |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 272 | struct fsnotify_event_private_data *priv; |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 273 | |
| 274 | mutex_lock(&group->notification_mutex); |
| 275 | while (!fsnotify_notify_queue_is_empty(group)) { |
| 276 | event = fsnotify_remove_notify_event(group); |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 277 | /* if they don't implement free_event_priv they better not have attached any */ |
| 278 | if (group->ops->free_event_priv) { |
| 279 | spin_lock(&event->lock); |
| 280 | priv = fsnotify_remove_priv_from_event(group, event); |
| 281 | spin_unlock(&event->lock); |
| 282 | if (priv) |
| 283 | group->ops->free_event_priv(priv); |
| 284 | } |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 285 | fsnotify_put_event(event); /* matches fsnotify_add_notify_event */ |
| 286 | } |
| 287 | mutex_unlock(&group->notification_mutex); |
| 288 | } |
| 289 | |
| 290 | static void initialize_event(struct fsnotify_event *event) |
| 291 | { |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 292 | INIT_LIST_HEAD(&event->holder.event_list); |
| 293 | atomic_set(&event->refcnt, 1); |
| 294 | |
| 295 | spin_lock_init(&event->lock); |
| 296 | |
Eric Paris | e4aff11 | 2009-05-21 17:01:50 -0400 | [diff] [blame] | 297 | INIT_LIST_HEAD(&event->private_data_list); |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Eric Paris | 1201a53 | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 300 | /* |
| 301 | * Caller damn well better be holding whatever mutex is protecting the |
Eric Paris | cac69da | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 302 | * old_holder->event_list and the new_event must be a clean event which |
| 303 | * cannot be found anywhere else in the kernel. |
Eric Paris | 1201a53 | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 304 | */ |
| 305 | int fsnotify_replace_event(struct fsnotify_event_holder *old_holder, |
| 306 | struct fsnotify_event *new_event) |
| 307 | { |
| 308 | struct fsnotify_event *old_event = old_holder->event; |
Eric Paris | cac69da | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 309 | struct fsnotify_event_holder *new_holder = &new_event->holder; |
| 310 | |
| 311 | enum event_spinlock_class { |
| 312 | SPINLOCK_OLD, |
| 313 | SPINLOCK_NEW, |
| 314 | }; |
Eric Paris | 1201a53 | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 315 | |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame^] | 316 | pr_debug("%s: old_event=%p new_event=%p\n", __func__, old_event, new_event); |
| 317 | |
Eric Paris | 1201a53 | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 318 | /* |
Eric Paris | cac69da | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 319 | * if the new_event's embedded holder is in use someone |
| 320 | * screwed up and didn't give us a clean new event. |
Eric Paris | 1201a53 | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 321 | */ |
Eric Paris | cac69da | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 322 | BUG_ON(!list_empty(&new_holder->event_list)); |
Eric Paris | 1201a53 | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 323 | |
Eric Paris | cac69da | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 324 | spin_lock_nested(&old_event->lock, SPINLOCK_OLD); |
| 325 | spin_lock_nested(&new_event->lock, SPINLOCK_NEW); |
Eric Paris | 1201a53 | 2009-12-17 21:24:22 -0500 | [diff] [blame] | 326 | |
| 327 | new_holder->event = new_event; |
| 328 | list_replace_init(&old_holder->event_list, &new_holder->event_list); |
| 329 | |
| 330 | spin_unlock(&new_event->lock); |
| 331 | spin_unlock(&old_event->lock); |
| 332 | |
| 333 | /* event == holder means we are referenced through the in event holder */ |
| 334 | if (old_holder != &old_event->holder) |
| 335 | fsnotify_destroy_event_holder(old_holder); |
| 336 | |
| 337 | fsnotify_get_event(new_event); /* on the list take reference */ |
| 338 | fsnotify_put_event(old_event); /* off the list, drop reference */ |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Eric Paris | b4e4e14 | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 343 | struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event) |
| 344 | { |
| 345 | struct fsnotify_event *event; |
| 346 | |
| 347 | event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL); |
| 348 | if (!event) |
| 349 | return NULL; |
| 350 | |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame^] | 351 | pr_debug("%s: old_event=%p new_event=%p\n", __func__, old_event, event); |
| 352 | |
Eric Paris | b4e4e14 | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 353 | memcpy(event, old_event, sizeof(*event)); |
| 354 | initialize_event(event); |
| 355 | |
| 356 | if (event->name_len) { |
| 357 | event->file_name = kstrdup(old_event->file_name, GFP_KERNEL); |
| 358 | if (!event->file_name) { |
| 359 | kmem_cache_free(fsnotify_event_cachep, event); |
| 360 | return NULL; |
| 361 | } |
| 362 | } |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 363 | event->tgid = get_pid(old_event->tgid); |
Eric Paris | b4e4e14 | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 364 | if (event->data_type == FSNOTIFY_EVENT_PATH) |
| 365 | path_get(&event->path); |
| 366 | |
| 367 | return event; |
| 368 | } |
| 369 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 370 | /* |
| 371 | * fsnotify_create_event - Allocate a new event which will be sent to each |
| 372 | * group's handle_event function if the group was interested in this |
| 373 | * particular event. |
| 374 | * |
| 375 | * @to_tell the inode which is supposed to receive the event (sometimes a |
| 376 | * parent of the inode to which the event happened. |
| 377 | * @mask what actually happened. |
| 378 | * @data pointer to the object which was actually affected |
| 379 | * @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] | 380 | * @name the filename, if available |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 381 | */ |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame] | 382 | struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data, |
Eric Paris | 59b0df2 | 2010-02-08 12:53:52 -0500 | [diff] [blame] | 383 | int data_type, const unsigned char *name, |
| 384 | u32 cookie, gfp_t gfp) |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 385 | { |
| 386 | struct fsnotify_event *event; |
| 387 | |
Eric Paris | 6f3a539 | 2009-12-17 20:12:07 -0500 | [diff] [blame] | 388 | event = kmem_cache_zalloc(fsnotify_event_cachep, gfp); |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 389 | if (!event) |
| 390 | return NULL; |
| 391 | |
Eric Paris | 5ba08e2 | 2010-07-28 10:18:37 -0400 | [diff] [blame^] | 392 | pr_debug("%s: event=%p to_tell=%p mask=%x data=%p data_type=%d\n", |
| 393 | __func__, event, to_tell, mask, data, data_type); |
| 394 | |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 395 | initialize_event(event); |
Eric Paris | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 396 | |
| 397 | if (name) { |
Eric Paris | f44aebc | 2009-07-15 15:49:52 -0400 | [diff] [blame] | 398 | event->file_name = kstrdup(name, gfp); |
Eric Paris | 62ffe5d | 2009-05-21 17:01:43 -0400 | [diff] [blame] | 399 | if (!event->file_name) { |
| 400 | kmem_cache_free(fsnotify_event_cachep, event); |
| 401 | return NULL; |
| 402 | } |
| 403 | event->name_len = strlen(event->file_name); |
| 404 | } |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame] | 405 | |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 406 | event->tgid = get_pid(task_tgid(current)); |
Eric Paris | 47882c6 | 2009-05-21 17:01:47 -0400 | [diff] [blame] | 407 | event->sync_cookie = cookie; |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 408 | event->to_tell = to_tell; |
Eric Paris | b4e4e14 | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 409 | event->data_type = data_type; |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 410 | |
| 411 | switch (data_type) { |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 412 | case FSNOTIFY_EVENT_PATH: { |
| 413 | struct path *path = data; |
| 414 | event->path.dentry = path->dentry; |
| 415 | event->path.mnt = path->mnt; |
| 416 | path_get(&event->path); |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 417 | break; |
| 418 | } |
| 419 | case FSNOTIFY_EVENT_INODE: |
| 420 | event->inode = data; |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 421 | break; |
| 422 | case FSNOTIFY_EVENT_NONE: |
| 423 | event->inode = NULL; |
| 424 | event->path.dentry = NULL; |
| 425 | event->path.mnt = NULL; |
| 426 | break; |
| 427 | default: |
| 428 | BUG(); |
| 429 | } |
| 430 | |
| 431 | event->mask = mask; |
| 432 | |
| 433 | return event; |
| 434 | } |
| 435 | |
| 436 | __init int fsnotify_notification_init(void) |
| 437 | { |
| 438 | fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC); |
Eric Paris | a2d8bc6 | 2009-05-21 17:01:37 -0400 | [diff] [blame] | 439 | fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC); |
| 440 | |
Eric Paris | b4277d3 | 2009-12-17 20:12:06 -0500 | [diff] [blame] | 441 | q_overflow_event = fsnotify_create_event(NULL, FS_Q_OVERFLOW, NULL, |
| 442 | FSNOTIFY_EVENT_NONE, NULL, 0, |
| 443 | GFP_KERNEL); |
| 444 | if (!q_overflow_event) |
| 445 | panic("unable to allocate fsnotify q_overflow_event\n"); |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | subsys_initcall(fsnotify_notification_init); |
| 450 | |