Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
0004  */
0005 
0006 /*
0007  * Basic idea behind the notification queue: An fsnotify group (like inotify)
0008  * sends the userspace notification about events asynchronously some time after
0009  * the event happened.  When inotify gets an event it will need to add that
0010  * event to the group notify queue.  Since a single event might need to be on
0011  * multiple group's notification queues we can't add the event directly to each
0012  * queue and instead add a small "event_holder" to each queue.  This event_holder
0013  * has a pointer back to the original event.  Since the majority of events are
0014  * going to end up on one, and only one, notification queue we embed one
0015  * event_holder into each event.  This means we have a single allocation instead
0016  * of always needing two.  If the embedded event_holder is already in use by
0017  * another group a new event_holder (from fsnotify_event_holder_cachep) will be
0018  * allocated and used.
0019  */
0020 
0021 #include <linux/fs.h>
0022 #include <linux/init.h>
0023 #include <linux/kernel.h>
0024 #include <linux/list.h>
0025 #include <linux/module.h>
0026 #include <linux/mount.h>
0027 #include <linux/mutex.h>
0028 #include <linux/namei.h>
0029 #include <linux/path.h>
0030 #include <linux/slab.h>
0031 #include <linux/spinlock.h>
0032 
0033 #include <linux/atomic.h>
0034 
0035 #include <linux/fsnotify_backend.h>
0036 #include "fsnotify.h"
0037 
0038 static atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0);
0039 
0040 /**
0041  * fsnotify_get_cookie - return a unique cookie for use in synchronizing events.
0042  * Called from fsnotify_move, which is inlined into filesystem modules.
0043  */
0044 u32 fsnotify_get_cookie(void)
0045 {
0046     return atomic_inc_return(&fsnotify_sync_cookie);
0047 }
0048 EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
0049 
0050 void fsnotify_destroy_event(struct fsnotify_group *group,
0051                 struct fsnotify_event *event)
0052 {
0053     /* Overflow events are per-group and we don't want to free them */
0054     if (!event || event == group->overflow_event)
0055         return;
0056     /*
0057      * If the event is still queued, we have a problem... Do an unreliable
0058      * lockless check first to avoid locking in the common case. The
0059      * locking may be necessary for permission events which got removed
0060      * from the list by a different CPU than the one freeing the event.
0061      */
0062     if (!list_empty(&event->list)) {
0063         spin_lock(&group->notification_lock);
0064         WARN_ON(!list_empty(&event->list));
0065         spin_unlock(&group->notification_lock);
0066     }
0067     group->ops->free_event(group, event);
0068 }
0069 
0070 /*
0071  * Try to add an event to the notification queue.
0072  * The group can later pull this event off the queue to deal with.
0073  * The group can use the @merge hook to merge the event with a queued event.
0074  * The group can use the @insert hook to insert the event into hash table.
0075  * The function returns:
0076  * 0 if the event was added to a queue
0077  * 1 if the event was merged with some other queued event
0078  * 2 if the event was not queued - either the queue of events has overflown
0079  *   or the group is shutting down.
0080  */
0081 int fsnotify_insert_event(struct fsnotify_group *group,
0082               struct fsnotify_event *event,
0083               int (*merge)(struct fsnotify_group *,
0084                        struct fsnotify_event *),
0085               void (*insert)(struct fsnotify_group *,
0086                      struct fsnotify_event *))
0087 {
0088     int ret = 0;
0089     struct list_head *list = &group->notification_list;
0090 
0091     pr_debug("%s: group=%p event=%p\n", __func__, group, event);
0092 
0093     spin_lock(&group->notification_lock);
0094 
0095     if (group->shutdown) {
0096         spin_unlock(&group->notification_lock);
0097         return 2;
0098     }
0099 
0100     if (event == group->overflow_event ||
0101         group->q_len >= group->max_events) {
0102         ret = 2;
0103         /* Queue overflow event only if it isn't already queued */
0104         if (!list_empty(&group->overflow_event->list)) {
0105             spin_unlock(&group->notification_lock);
0106             return ret;
0107         }
0108         event = group->overflow_event;
0109         goto queue;
0110     }
0111 
0112     if (!list_empty(list) && merge) {
0113         ret = merge(group, event);
0114         if (ret) {
0115             spin_unlock(&group->notification_lock);
0116             return ret;
0117         }
0118     }
0119 
0120 queue:
0121     group->q_len++;
0122     list_add_tail(&event->list, list);
0123     if (insert)
0124         insert(group, event);
0125     spin_unlock(&group->notification_lock);
0126 
0127     wake_up(&group->notification_waitq);
0128     kill_fasync(&group->fsn_fa, SIGIO, POLL_IN);
0129     return ret;
0130 }
0131 
0132 void fsnotify_remove_queued_event(struct fsnotify_group *group,
0133                   struct fsnotify_event *event)
0134 {
0135     assert_spin_locked(&group->notification_lock);
0136     /*
0137      * We need to init list head for the case of overflow event so that
0138      * check in fsnotify_add_event() works
0139      */
0140     list_del_init(&event->list);
0141     group->q_len--;
0142 }
0143 
0144 /*
0145  * Return the first event on the notification list without removing it.
0146  * Returns NULL if the list is empty.
0147  */
0148 struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group)
0149 {
0150     assert_spin_locked(&group->notification_lock);
0151 
0152     if (fsnotify_notify_queue_is_empty(group))
0153         return NULL;
0154 
0155     return list_first_entry(&group->notification_list,
0156                 struct fsnotify_event, list);
0157 }
0158 
0159 /*
0160  * Remove and return the first event from the notification list.  It is the
0161  * responsibility of the caller to destroy the obtained event
0162  */
0163 struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
0164 {
0165     struct fsnotify_event *event = fsnotify_peek_first_event(group);
0166 
0167     if (!event)
0168         return NULL;
0169 
0170     pr_debug("%s: group=%p event=%p\n", __func__, group, event);
0171 
0172     fsnotify_remove_queued_event(group, event);
0173 
0174     return event;
0175 }
0176 
0177 /*
0178  * Called when a group is being torn down to clean up any outstanding
0179  * event notifications.
0180  */
0181 void fsnotify_flush_notify(struct fsnotify_group *group)
0182 {
0183     struct fsnotify_event *event;
0184 
0185     spin_lock(&group->notification_lock);
0186     while (!fsnotify_notify_queue_is_empty(group)) {
0187         event = fsnotify_remove_first_event(group);
0188         spin_unlock(&group->notification_lock);
0189         fsnotify_destroy_event(group, event);
0190         spin_lock(&group->notification_lock);
0191     }
0192     spin_unlock(&group->notification_lock);
0193 }