1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
290586523SEric Paris /*
390586523SEric Paris * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
490586523SEric Paris */
590586523SEric Paris
6a2d8bc6cSEric Paris /*
7a2d8bc6cSEric Paris * Basic idea behind the notification queue: An fsnotify group (like inotify)
8b3834be5SAdam Buchbinder * sends the userspace notification about events asynchronously some time after
9a2d8bc6cSEric Paris * the event happened. When inotify gets an event it will need to add that
10a2d8bc6cSEric Paris * event to the group notify queue. Since a single event might need to be on
11a2d8bc6cSEric Paris * multiple group's notification queues we can't add the event directly to each
12a2d8bc6cSEric Paris * queue and instead add a small "event_holder" to each queue. This event_holder
13a2d8bc6cSEric Paris * has a pointer back to the original event. Since the majority of events are
14a2d8bc6cSEric Paris * going to end up on one, and only one, notification queue we embed one
15a2d8bc6cSEric Paris * event_holder into each event. This means we have a single allocation instead
16a2d8bc6cSEric Paris * of always needing two. If the embedded event_holder is already in use by
17a2d8bc6cSEric Paris * another group a new event_holder (from fsnotify_event_holder_cachep) will be
18a2d8bc6cSEric Paris * allocated and used.
19a2d8bc6cSEric Paris */
20a2d8bc6cSEric Paris
2190586523SEric Paris #include <linux/fs.h>
2290586523SEric Paris #include <linux/init.h>
2390586523SEric Paris #include <linux/kernel.h>
2490586523SEric Paris #include <linux/list.h>
2547882c6fSEric Paris #include <linux/module.h>
2690586523SEric Paris #include <linux/mount.h>
2790586523SEric Paris #include <linux/mutex.h>
2890586523SEric Paris #include <linux/namei.h>
2990586523SEric Paris #include <linux/path.h>
3090586523SEric Paris #include <linux/slab.h>
3190586523SEric Paris #include <linux/spinlock.h>
3290586523SEric Paris
3360063497SArun Sharma #include <linux/atomic.h>
3490586523SEric Paris
3590586523SEric Paris #include <linux/fsnotify_backend.h>
3690586523SEric Paris #include "fsnotify.h"
3790586523SEric Paris
3847882c6fSEric Paris static atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0);
3947882c6fSEric Paris
4047882c6fSEric Paris /**
4147882c6fSEric Paris * fsnotify_get_cookie - return a unique cookie for use in synchronizing events.
4247882c6fSEric Paris * Called from fsnotify_move, which is inlined into filesystem modules.
4347882c6fSEric Paris */
fsnotify_get_cookie(void)4447882c6fSEric Paris u32 fsnotify_get_cookie(void)
4547882c6fSEric Paris {
4647882c6fSEric Paris return atomic_inc_return(&fsnotify_sync_cookie);
4747882c6fSEric Paris }
4847882c6fSEric Paris EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
49a2d8bc6cSEric Paris
fsnotify_destroy_event(struct fsnotify_group * group,struct fsnotify_event * event)507053aee2SJan Kara void fsnotify_destroy_event(struct fsnotify_group *group,
517053aee2SJan Kara struct fsnotify_event *event)
5290586523SEric Paris {
537053aee2SJan Kara /* Overflow events are per-group and we don't want to free them */
54a0a92d26SAmir Goldstein if (!event || event == group->overflow_event)
5590586523SEric Paris return;
560b1b8652SJan Kara /*
570b1b8652SJan Kara * If the event is still queued, we have a problem... Do an unreliable
580b1b8652SJan Kara * lockless check first to avoid locking in the common case. The
590b1b8652SJan Kara * locking may be necessary for permission events which got removed
600b1b8652SJan Kara * from the list by a different CPU than the one freeing the event.
610b1b8652SJan Kara */
620b1b8652SJan Kara if (!list_empty(&event->list)) {
630b1b8652SJan Kara spin_lock(&group->notification_lock);
645838d444SJan Kara WARN_ON(!list_empty(&event->list));
650b1b8652SJan Kara spin_unlock(&group->notification_lock);
660b1b8652SJan Kara }
67*330ae77dSGabriel Krisman Bertazi group->ops->free_event(group, event);
68e4aff117SEric Paris }
69e4aff117SEric Paris
70e4aff117SEric Paris /*
7194e00d28SAmir Goldstein * Try to add an event to the notification queue.
7294e00d28SAmir Goldstein * The group can later pull this event off the queue to deal with.
7394e00d28SAmir Goldstein * The group can use the @merge hook to merge the event with a queued event.
7494e00d28SAmir Goldstein * The group can use the @insert hook to insert the event into hash table.
7594e00d28SAmir Goldstein * The function returns:
7694e00d28SAmir Goldstein * 0 if the event was added to a queue
7794e00d28SAmir Goldstein * 1 if the event was merged with some other queued event
7812703dbfSJan Kara * 2 if the event was not queued - either the queue of events has overflown
7912703dbfSJan Kara * or the group is shutting down.
80a2d8bc6cSEric Paris */
fsnotify_insert_event(struct fsnotify_group * group,struct fsnotify_event * event,int (* merge)(struct fsnotify_group *,struct fsnotify_event *),void (* insert)(struct fsnotify_group *,struct fsnotify_event *))811ad03c3aSGabriel Krisman Bertazi int fsnotify_insert_event(struct fsnotify_group *group,
827053aee2SJan Kara struct fsnotify_event *event,
8394e00d28SAmir Goldstein int (*merge)(struct fsnotify_group *,
8494e00d28SAmir Goldstein struct fsnotify_event *),
8594e00d28SAmir Goldstein void (*insert)(struct fsnotify_group *,
86f70ab54cSEric Paris struct fsnotify_event *))
87a2d8bc6cSEric Paris {
8883c0e1b4SJan Kara int ret = 0;
89a2d8bc6cSEric Paris struct list_head *list = &group->notification_list;
90e4aff117SEric Paris
917053aee2SJan Kara pr_debug("%s: group=%p event=%p\n", __func__, group, event);
92a2d8bc6cSEric Paris
93c21dbe20SJan Kara spin_lock(&group->notification_lock);
94a2d8bc6cSEric Paris
9512703dbfSJan Kara if (group->shutdown) {
96c21dbe20SJan Kara spin_unlock(&group->notification_lock);
9712703dbfSJan Kara return 2;
9812703dbfSJan Kara }
9912703dbfSJan Kara
1007b1f6417SJan Kara if (event == group->overflow_event ||
1017b1f6417SJan Kara group->q_len >= group->max_events) {
102482ef06cSJan Kara ret = 2;
1037053aee2SJan Kara /* Queue overflow event only if it isn't already queued */
104ff57cd58SJan Kara if (!list_empty(&group->overflow_event->list)) {
105c21dbe20SJan Kara spin_unlock(&group->notification_lock);
106482ef06cSJan Kara return ret;
107482ef06cSJan Kara }
108ff57cd58SJan Kara event = group->overflow_event;
109482ef06cSJan Kara goto queue;
110e4aff117SEric Paris }
111a2d8bc6cSEric Paris
11274766bbfSEric Paris if (!list_empty(list) && merge) {
11394e00d28SAmir Goldstein ret = merge(group, event);
11483c0e1b4SJan Kara if (ret) {
115c21dbe20SJan Kara spin_unlock(&group->notification_lock);
11683c0e1b4SJan Kara return ret;
117f70ab54cSEric Paris }
118a2d8bc6cSEric Paris }
119a2d8bc6cSEric Paris
120482ef06cSJan Kara queue:
121a2d8bc6cSEric Paris group->q_len++;
1227053aee2SJan Kara list_add_tail(&event->list, list);
12394e00d28SAmir Goldstein if (insert)
12494e00d28SAmir Goldstein insert(group, event);
125c21dbe20SJan Kara spin_unlock(&group->notification_lock);
126a2d8bc6cSEric Paris
127a2d8bc6cSEric Paris wake_up(&group->notification_waitq);
1280a6b6bd5SEric Paris kill_fasync(&group->fsn_fa, SIGIO, POLL_IN);
12983c0e1b4SJan Kara return ret;
130a2d8bc6cSEric Paris }
131a2d8bc6cSEric Paris
fsnotify_remove_queued_event(struct fsnotify_group * group,struct fsnotify_event * event)132f7db89acSJan Kara void fsnotify_remove_queued_event(struct fsnotify_group *group,
133f7db89acSJan Kara struct fsnotify_event *event)
134f7db89acSJan Kara {
135f7db89acSJan Kara assert_spin_locked(&group->notification_lock);
136f7db89acSJan Kara /*
137f7db89acSJan Kara * We need to init list head for the case of overflow event so that
138f7db89acSJan Kara * check in fsnotify_add_event() works
139f7db89acSJan Kara */
140f7db89acSJan Kara list_del_init(&event->list);
141f7db89acSJan Kara group->q_len--;
142f7db89acSJan Kara }
143f7db89acSJan Kara
144a2d8bc6cSEric Paris /*
1456f73171eSAmir Goldstein * Return the first event on the notification list without removing it.
1466f73171eSAmir Goldstein * Returns NULL if the list is empty.
147a2d8bc6cSEric Paris */
fsnotify_peek_first_event(struct fsnotify_group * group)1488ba8fa91SJan Kara struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group)
149a2d8bc6cSEric Paris {
150ed272640SJan Kara assert_spin_locked(&group->notification_lock);
151a2d8bc6cSEric Paris
1526f73171eSAmir Goldstein if (fsnotify_notify_queue_is_empty(group))
1536f73171eSAmir Goldstein return NULL;
1546f73171eSAmir Goldstein
1557053aee2SJan Kara return list_first_entry(&group->notification_list,
1567053aee2SJan Kara struct fsnotify_event, list);
157a2d8bc6cSEric Paris }
158a2d8bc6cSEric Paris
159a2d8bc6cSEric Paris /*
1606f73171eSAmir Goldstein * Remove and return the first event from the notification list. It is the
1616f73171eSAmir Goldstein * responsibility of the caller to destroy the obtained event
1626f73171eSAmir Goldstein */
fsnotify_remove_first_event(struct fsnotify_group * group)1636f73171eSAmir Goldstein struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
1646f73171eSAmir Goldstein {
1656f73171eSAmir Goldstein struct fsnotify_event *event = fsnotify_peek_first_event(group);
1666f73171eSAmir Goldstein
1676f73171eSAmir Goldstein if (!event)
1686f73171eSAmir Goldstein return NULL;
1696f73171eSAmir Goldstein
1706f73171eSAmir Goldstein pr_debug("%s: group=%p event=%p\n", __func__, group, event);
1716f73171eSAmir Goldstein
1726f73171eSAmir Goldstein fsnotify_remove_queued_event(group, event);
1736f73171eSAmir Goldstein
1746f73171eSAmir Goldstein return event;
1756f73171eSAmir Goldstein }
1766f73171eSAmir Goldstein
1776f73171eSAmir Goldstein /*
178a2d8bc6cSEric Paris * Called when a group is being torn down to clean up any outstanding
179a2d8bc6cSEric Paris * event notifications.
180a2d8bc6cSEric Paris */
fsnotify_flush_notify(struct fsnotify_group * group)181a2d8bc6cSEric Paris void fsnotify_flush_notify(struct fsnotify_group *group)
182a2d8bc6cSEric Paris {
183a2d8bc6cSEric Paris struct fsnotify_event *event;
184a2d8bc6cSEric Paris
185c21dbe20SJan Kara spin_lock(&group->notification_lock);
186a2d8bc6cSEric Paris while (!fsnotify_notify_queue_is_empty(group)) {
1878ba8fa91SJan Kara event = fsnotify_remove_first_event(group);
188c21dbe20SJan Kara spin_unlock(&group->notification_lock);
1897053aee2SJan Kara fsnotify_destroy_event(group, event);
190c21dbe20SJan Kara spin_lock(&group->notification_lock);
191a2d8bc6cSEric Paris }
192c21dbe20SJan Kara spin_unlock(&group->notification_lock);
193a2d8bc6cSEric Paris }
194