VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
c82ee6d3beaa4 (Thomas Gleixner 2019-05-19 15:51:48 +0200   1) // SPDX-License-Identifier: GPL-2.0-or-later
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400   2) /*
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400   3)  *  Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400   4)  */
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400   5) 
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400   6) /*
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400   7)  * Basic idea behind the notification queue: An fsnotify group (like inotify)
b3834be5c42a5 (Adam Buchbinder 2012-09-19 21:48:02 -0400   8)  * sends the userspace notification about events asynchronously some time after
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400   9)  * the event happened.  When inotify gets an event it will need to add that
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  10)  * event to the group notify queue.  Since a single event might need to be on
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  11)  * multiple group's notification queues we can't add the event directly to each
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  12)  * queue and instead add a small "event_holder" to each queue.  This event_holder
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  13)  * has a pointer back to the original event.  Since the majority of events are
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  14)  * going to end up on one, and only one, notification queue we embed one
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  15)  * event_holder into each event.  This means we have a single allocation instead
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  16)  * of always needing two.  If the embedded event_holder is already in use by
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  17)  * another group a new event_holder (from fsnotify_event_holder_cachep) will be
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  18)  * allocated and used.
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  19)  */
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  20) 
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  21) #include <linux/fs.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  22) #include <linux/init.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  23) #include <linux/kernel.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  24) #include <linux/list.h>
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  25) #include <linux/module.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  26) #include <linux/mount.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  27) #include <linux/mutex.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  28) #include <linux/namei.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  29) #include <linux/path.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  30) #include <linux/slab.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  31) #include <linux/spinlock.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  32) 
60063497a95e7 (Arun Sharma     2011-07-26 16:09:06 -0700  33) #include <linux/atomic.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  34) 
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  35) #include <linux/fsnotify_backend.h>
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  36) #include "fsnotify.h"
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  37) 
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  38) static atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0);
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  39) 
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  40) /**
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  41)  * fsnotify_get_cookie - return a unique cookie for use in synchronizing events.
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  42)  * Called from fsnotify_move, which is inlined into filesystem modules.
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  43)  */
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  44) u32 fsnotify_get_cookie(void)
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  45) {
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  46) 	return atomic_inc_return(&fsnotify_sync_cookie);
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  47) }
47882c6f51e8e (Eric Paris      2009-05-21 17:01:47 -0400  48) EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  49) 
7053aee26a354 (Jan Kara        2014-01-21 15:48:14 -0800  50) void fsnotify_destroy_event(struct fsnotify_group *group,
7053aee26a354 (Jan Kara        2014-01-21 15:48:14 -0800  51) 			    struct fsnotify_event *event)
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  52) {
7053aee26a354 (Jan Kara        2014-01-21 15:48:14 -0800  53) 	/* Overflow events are per-group and we don't want to free them */
a0a92d261f292 (Amir Goldstein  2019-01-10 19:04:31 +0200  54) 	if (!event || event == group->overflow_event)
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  55) 		return;
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  56) 	/*
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  57) 	 * If the event is still queued, we have a problem... Do an unreliable
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  58) 	 * lockless check first to avoid locking in the common case. The
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  59) 	 * locking may be necessary for permission events which got removed
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  60) 	 * from the list by a different CPU than the one freeing the event.
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  61) 	 */
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  62) 	if (!list_empty(&event->list)) {
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  63) 		spin_lock(&group->notification_lock);
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  64) 		WARN_ON(!list_empty(&event->list));
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  65) 		spin_unlock(&group->notification_lock);
0b1b86527df4b (Jan Kara        2016-10-07 16:56:58 -0700  66) 	}
7053aee26a354 (Jan Kara        2014-01-21 15:48:14 -0800  67) 	group->ops->free_event(event);
e4aff117368cf (Eric Paris      2009-05-21 17:01:50 -0400  68) }
e4aff117368cf (Eric Paris      2009-05-21 17:01:50 -0400  69) 
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  70) /*
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  71)  * Try to add an event to the notification queue.
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  72)  * The group can later pull this event off the queue to deal with.
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  73)  * The group can use the @merge hook to merge the event with a queued event.
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  74)  * The group can use the @insert hook to insert the event into hash table.
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  75)  * The function returns:
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  76)  * 0 if the event was added to a queue
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  77)  * 1 if the event was merged with some other queued event
12703dbfeb154 (Jan Kara        2016-09-19 14:44:27 -0700  78)  * 2 if the event was not queued - either the queue of events has overflown
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  79)  *   or the group is shutting down.
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400  80)  */
8ba8fa9170935 (Jan Kara        2014-08-06 16:03:26 -0700  81) int fsnotify_add_event(struct fsnotify_group *group,
8ba8fa9170935 (Jan Kara        2014-08-06 16:03:26 -0700  82) 		       struct fsnotify_event *event,
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  83) 		       int (*merge)(struct fsnotify_group *,
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  84) 				    struct fsnotify_event *),
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  85) 		       void (*insert)(struct fsnotify_group *,
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200  86) 				      struct fsnotify_event *))
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  87) {
83c0e1b442b48 (Jan Kara        2014-01-28 18:53:22 +0100  88) 	int ret = 0;
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  89) 	struct list_head *list = &group->notification_list;
e4aff117368cf (Eric Paris      2009-05-21 17:01:50 -0400  90) 
7053aee26a354 (Jan Kara        2014-01-21 15:48:14 -0800  91) 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  92) 
c21dbe20f6062 (Jan Kara        2016-10-07 16:56:52 -0700  93) 	spin_lock(&group->notification_lock);
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400  94) 
12703dbfeb154 (Jan Kara        2016-09-19 14:44:27 -0700  95) 	if (group->shutdown) {
c21dbe20f6062 (Jan Kara        2016-10-07 16:56:52 -0700  96) 		spin_unlock(&group->notification_lock);
12703dbfeb154 (Jan Kara        2016-09-19 14:44:27 -0700  97) 		return 2;
12703dbfeb154 (Jan Kara        2016-09-19 14:44:27 -0700  98) 	}
12703dbfeb154 (Jan Kara        2016-09-19 14:44:27 -0700  99) 
7b1f641776e0c (Jan Kara        2018-02-21 15:07:52 +0100 100) 	if (event == group->overflow_event ||
7b1f641776e0c (Jan Kara        2018-02-21 15:07:52 +0100 101) 	    group->q_len >= group->max_events) {
482ef06c5e946 (Jan Kara        2014-02-21 19:07:54 +0100 102) 		ret = 2;
7053aee26a354 (Jan Kara        2014-01-21 15:48:14 -0800 103) 		/* Queue overflow event only if it isn't already queued */
ff57cd5863cf3 (Jan Kara        2014-02-21 19:14:11 +0100 104) 		if (!list_empty(&group->overflow_event->list)) {
c21dbe20f6062 (Jan Kara        2016-10-07 16:56:52 -0700 105) 			spin_unlock(&group->notification_lock);
482ef06c5e946 (Jan Kara        2014-02-21 19:07:54 +0100 106) 			return ret;
482ef06c5e946 (Jan Kara        2014-02-21 19:07:54 +0100 107) 		}
ff57cd5863cf3 (Jan Kara        2014-02-21 19:14:11 +0100 108) 		event = group->overflow_event;
482ef06c5e946 (Jan Kara        2014-02-21 19:07:54 +0100 109) 		goto queue;
e4aff117368cf (Eric Paris      2009-05-21 17:01:50 -0400 110) 	}
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 111) 
74766bbfa99ad (Eric Paris      2009-12-17 21:24:21 -0500 112) 	if (!list_empty(list) && merge) {
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200 113) 		ret = merge(group, event);
83c0e1b442b48 (Jan Kara        2014-01-28 18:53:22 +0100 114) 		if (ret) {
c21dbe20f6062 (Jan Kara        2016-10-07 16:56:52 -0700 115) 			spin_unlock(&group->notification_lock);
83c0e1b442b48 (Jan Kara        2014-01-28 18:53:22 +0100 116) 			return ret;
f70ab54cc6c39 (Eric Paris      2010-07-28 10:18:37 -0400 117) 		}
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 118) 	}
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 119) 
482ef06c5e946 (Jan Kara        2014-02-21 19:07:54 +0100 120) queue:
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 121) 	group->q_len++;
7053aee26a354 (Jan Kara        2014-01-21 15:48:14 -0800 122) 	list_add_tail(&event->list, list);
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200 123) 	if (insert)
94e00d28a680d (Amir Goldstein  2021-03-04 12:48:25 +0200 124) 		insert(group, event);
c21dbe20f6062 (Jan Kara        2016-10-07 16:56:52 -0700 125) 	spin_unlock(&group->notification_lock);
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 126) 
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 127) 	wake_up(&group->notification_waitq);
0a6b6bd5919a6 (Eric Paris      2011-10-14 17:43:39 -0400 128) 	kill_fasync(&group->fsn_fa, SIGIO, POLL_IN);
83c0e1b442b48 (Jan Kara        2014-01-28 18:53:22 +0100 129) 	return ret;
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 130) }
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 131) 
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 132) void fsnotify_remove_queued_event(struct fsnotify_group *group,
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 133) 				  struct fsnotify_event *event)
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 134) {
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 135) 	assert_spin_locked(&group->notification_lock);
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 136) 	/*
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 137) 	 * We need to init list head for the case of overflow event so that
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 138) 	 * check in fsnotify_add_event() works
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 139) 	 */
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 140) 	list_del_init(&event->list);
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 141) 	group->q_len--;
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 142) }
f7db89accc9c5 (Jan Kara        2019-01-09 13:15:23 +0100 143) 
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 144) /*
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 145)  * Return the first event on the notification list without removing it.
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 146)  * Returns NULL if the list is empty.
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 147)  */
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 148) struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group)
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400 149) {
ed2726406c6a7 (Jan Kara        2016-10-07 16:57:01 -0700 150) 	assert_spin_locked(&group->notification_lock);
90586523eb4b3 (Eric Paris      2009-05-21 17:01:20 -0400 151) 
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 152) 	if (fsnotify_notify_queue_is_empty(group))
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 153) 		return NULL;
5ba08e2eeb063 (Eric Paris      2010-07-28 10:18:37 -0400 154) 
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 155) 	return list_first_entry(&group->notification_list,
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 156) 				struct fsnotify_event, list);
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 157) }
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 158) 
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 159) /*
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 160)  * Remove and return the first event from the notification list.  It is the
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 161)  * responsibility of the caller to destroy the obtained event
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 162)  */
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 163) struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group)
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 164) {
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 165) 	struct fsnotify_event *event = fsnotify_peek_first_event(group);
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 166) 
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 167) 	if (!event)
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 168) 		return NULL;
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 169) 
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 170) 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 171) 
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 172) 	fsnotify_remove_queued_event(group, event);
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 173) 
6f73171e19236 (Amir Goldstein  2021-03-04 12:48:22 +0200 174) 	return event;
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 175) }
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 176) 
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 177) /*
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 178)  * Called when a group is being torn down to clean up any outstanding
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 179)  * event notifications.
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 180)  */
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 181) void fsnotify_flush_notify(struct fsnotify_group *group)
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 182) {
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 183) 	struct fsnotify_event *event;
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 184) 
c21dbe20f6062 (Jan Kara        2016-10-07 16:56:52 -0700 185) 	spin_lock(&group->notification_lock);
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 186) 	while (!fsnotify_notify_queue_is_empty(group)) {
8ba8fa9170935 (Jan Kara        2014-08-06 16:03:26 -0700 187) 		event = fsnotify_remove_first_event(group);
c21dbe20f6062 (Jan Kara        2016-10-07 16:56:52 -0700 188) 		spin_unlock(&group->notification_lock);
7053aee26a354 (Jan Kara        2014-01-21 15:48:14 -0800 189) 		fsnotify_destroy_event(group, event);
c21dbe20f6062 (Jan Kara        2016-10-07 16:56:52 -0700 190) 		spin_lock(&group->notification_lock);
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 191) 	}
c21dbe20f6062 (Jan Kara        2016-10-07 16:56:52 -0700 192) 	spin_unlock(&group->notification_lock);
a2d8bc6cb4a30 (Eric Paris      2009-05-21 17:01:37 -0400 193) }