162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci/* 762306a36Sopenharmony_ci * Basic idea behind the notification queue: An fsnotify group (like inotify) 862306a36Sopenharmony_ci * sends the userspace notification about events asynchronously some time after 962306a36Sopenharmony_ci * the event happened. When inotify gets an event it will need to add that 1062306a36Sopenharmony_ci * event to the group notify queue. Since a single event might need to be on 1162306a36Sopenharmony_ci * multiple group's notification queues we can't add the event directly to each 1262306a36Sopenharmony_ci * queue and instead add a small "event_holder" to each queue. This event_holder 1362306a36Sopenharmony_ci * has a pointer back to the original event. Since the majority of events are 1462306a36Sopenharmony_ci * going to end up on one, and only one, notification queue we embed one 1562306a36Sopenharmony_ci * event_holder into each event. This means we have a single allocation instead 1662306a36Sopenharmony_ci * of always needing two. If the embedded event_holder is already in use by 1762306a36Sopenharmony_ci * another group a new event_holder (from fsnotify_event_holder_cachep) will be 1862306a36Sopenharmony_ci * allocated and used. 1962306a36Sopenharmony_ci */ 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci#include <linux/fs.h> 2262306a36Sopenharmony_ci#include <linux/init.h> 2362306a36Sopenharmony_ci#include <linux/kernel.h> 2462306a36Sopenharmony_ci#include <linux/list.h> 2562306a36Sopenharmony_ci#include <linux/module.h> 2662306a36Sopenharmony_ci#include <linux/mount.h> 2762306a36Sopenharmony_ci#include <linux/mutex.h> 2862306a36Sopenharmony_ci#include <linux/namei.h> 2962306a36Sopenharmony_ci#include <linux/path.h> 3062306a36Sopenharmony_ci#include <linux/slab.h> 3162306a36Sopenharmony_ci#include <linux/spinlock.h> 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci#include <linux/atomic.h> 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci#include <linux/fsnotify_backend.h> 3662306a36Sopenharmony_ci#include "fsnotify.h" 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_cistatic atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0); 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci/** 4162306a36Sopenharmony_ci * fsnotify_get_cookie - return a unique cookie for use in synchronizing events. 4262306a36Sopenharmony_ci * Called from fsnotify_move, which is inlined into filesystem modules. 4362306a36Sopenharmony_ci */ 4462306a36Sopenharmony_ciu32 fsnotify_get_cookie(void) 4562306a36Sopenharmony_ci{ 4662306a36Sopenharmony_ci return atomic_inc_return(&fsnotify_sync_cookie); 4762306a36Sopenharmony_ci} 4862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(fsnotify_get_cookie); 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_civoid fsnotify_destroy_event(struct fsnotify_group *group, 5162306a36Sopenharmony_ci struct fsnotify_event *event) 5262306a36Sopenharmony_ci{ 5362306a36Sopenharmony_ci /* Overflow events are per-group and we don't want to free them */ 5462306a36Sopenharmony_ci if (!event || event == group->overflow_event) 5562306a36Sopenharmony_ci return; 5662306a36Sopenharmony_ci /* 5762306a36Sopenharmony_ci * If the event is still queued, we have a problem... Do an unreliable 5862306a36Sopenharmony_ci * lockless check first to avoid locking in the common case. The 5962306a36Sopenharmony_ci * locking may be necessary for permission events which got removed 6062306a36Sopenharmony_ci * from the list by a different CPU than the one freeing the event. 6162306a36Sopenharmony_ci */ 6262306a36Sopenharmony_ci if (!list_empty(&event->list)) { 6362306a36Sopenharmony_ci spin_lock(&group->notification_lock); 6462306a36Sopenharmony_ci WARN_ON(!list_empty(&event->list)); 6562306a36Sopenharmony_ci spin_unlock(&group->notification_lock); 6662306a36Sopenharmony_ci } 6762306a36Sopenharmony_ci group->ops->free_event(group, event); 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci/* 7162306a36Sopenharmony_ci * Try to add an event to the notification queue. 7262306a36Sopenharmony_ci * The group can later pull this event off the queue to deal with. 7362306a36Sopenharmony_ci * The group can use the @merge hook to merge the event with a queued event. 7462306a36Sopenharmony_ci * The group can use the @insert hook to insert the event into hash table. 7562306a36Sopenharmony_ci * The function returns: 7662306a36Sopenharmony_ci * 0 if the event was added to a queue 7762306a36Sopenharmony_ci * 1 if the event was merged with some other queued event 7862306a36Sopenharmony_ci * 2 if the event was not queued - either the queue of events has overflown 7962306a36Sopenharmony_ci * or the group is shutting down. 8062306a36Sopenharmony_ci */ 8162306a36Sopenharmony_ciint fsnotify_insert_event(struct fsnotify_group *group, 8262306a36Sopenharmony_ci struct fsnotify_event *event, 8362306a36Sopenharmony_ci int (*merge)(struct fsnotify_group *, 8462306a36Sopenharmony_ci struct fsnotify_event *), 8562306a36Sopenharmony_ci void (*insert)(struct fsnotify_group *, 8662306a36Sopenharmony_ci struct fsnotify_event *)) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci int ret = 0; 8962306a36Sopenharmony_ci struct list_head *list = &group->notification_list; 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci pr_debug("%s: group=%p event=%p\n", __func__, group, event); 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci spin_lock(&group->notification_lock); 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci if (group->shutdown) { 9662306a36Sopenharmony_ci spin_unlock(&group->notification_lock); 9762306a36Sopenharmony_ci return 2; 9862306a36Sopenharmony_ci } 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci if (event == group->overflow_event || 10162306a36Sopenharmony_ci group->q_len >= group->max_events) { 10262306a36Sopenharmony_ci ret = 2; 10362306a36Sopenharmony_ci /* Queue overflow event only if it isn't already queued */ 10462306a36Sopenharmony_ci if (!list_empty(&group->overflow_event->list)) { 10562306a36Sopenharmony_ci spin_unlock(&group->notification_lock); 10662306a36Sopenharmony_ci return ret; 10762306a36Sopenharmony_ci } 10862306a36Sopenharmony_ci event = group->overflow_event; 10962306a36Sopenharmony_ci goto queue; 11062306a36Sopenharmony_ci } 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci if (!list_empty(list) && merge) { 11362306a36Sopenharmony_ci ret = merge(group, event); 11462306a36Sopenharmony_ci if (ret) { 11562306a36Sopenharmony_ci spin_unlock(&group->notification_lock); 11662306a36Sopenharmony_ci return ret; 11762306a36Sopenharmony_ci } 11862306a36Sopenharmony_ci } 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ciqueue: 12162306a36Sopenharmony_ci group->q_len++; 12262306a36Sopenharmony_ci list_add_tail(&event->list, list); 12362306a36Sopenharmony_ci if (insert) 12462306a36Sopenharmony_ci insert(group, event); 12562306a36Sopenharmony_ci spin_unlock(&group->notification_lock); 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci wake_up(&group->notification_waitq); 12862306a36Sopenharmony_ci kill_fasync(&group->fsn_fa, SIGIO, POLL_IN); 12962306a36Sopenharmony_ci return ret; 13062306a36Sopenharmony_ci} 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_civoid fsnotify_remove_queued_event(struct fsnotify_group *group, 13362306a36Sopenharmony_ci struct fsnotify_event *event) 13462306a36Sopenharmony_ci{ 13562306a36Sopenharmony_ci assert_spin_locked(&group->notification_lock); 13662306a36Sopenharmony_ci /* 13762306a36Sopenharmony_ci * We need to init list head for the case of overflow event so that 13862306a36Sopenharmony_ci * check in fsnotify_add_event() works 13962306a36Sopenharmony_ci */ 14062306a36Sopenharmony_ci list_del_init(&event->list); 14162306a36Sopenharmony_ci group->q_len--; 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci/* 14562306a36Sopenharmony_ci * Return the first event on the notification list without removing it. 14662306a36Sopenharmony_ci * Returns NULL if the list is empty. 14762306a36Sopenharmony_ci */ 14862306a36Sopenharmony_cistruct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group) 14962306a36Sopenharmony_ci{ 15062306a36Sopenharmony_ci assert_spin_locked(&group->notification_lock); 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci if (fsnotify_notify_queue_is_empty(group)) 15362306a36Sopenharmony_ci return NULL; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci return list_first_entry(&group->notification_list, 15662306a36Sopenharmony_ci struct fsnotify_event, list); 15762306a36Sopenharmony_ci} 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci/* 16062306a36Sopenharmony_ci * Remove and return the first event from the notification list. It is the 16162306a36Sopenharmony_ci * responsibility of the caller to destroy the obtained event 16262306a36Sopenharmony_ci */ 16362306a36Sopenharmony_cistruct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group) 16462306a36Sopenharmony_ci{ 16562306a36Sopenharmony_ci struct fsnotify_event *event = fsnotify_peek_first_event(group); 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci if (!event) 16862306a36Sopenharmony_ci return NULL; 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci pr_debug("%s: group=%p event=%p\n", __func__, group, event); 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci fsnotify_remove_queued_event(group, event); 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci return event; 17562306a36Sopenharmony_ci} 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci/* 17862306a36Sopenharmony_ci * Called when a group is being torn down to clean up any outstanding 17962306a36Sopenharmony_ci * event notifications. 18062306a36Sopenharmony_ci */ 18162306a36Sopenharmony_civoid fsnotify_flush_notify(struct fsnotify_group *group) 18262306a36Sopenharmony_ci{ 18362306a36Sopenharmony_ci struct fsnotify_event *event; 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci spin_lock(&group->notification_lock); 18662306a36Sopenharmony_ci while (!fsnotify_notify_queue_is_empty(group)) { 18762306a36Sopenharmony_ci event = fsnotify_remove_first_event(group); 18862306a36Sopenharmony_ci spin_unlock(&group->notification_lock); 18962306a36Sopenharmony_ci fsnotify_destroy_event(group, event); 19062306a36Sopenharmony_ci spin_lock(&group->notification_lock); 19162306a36Sopenharmony_ci } 19262306a36Sopenharmony_ci spin_unlock(&group->notification_lock); 19362306a36Sopenharmony_ci} 194