22
22
* General Public License for more details.
25
#include <linux/dcache.h> /* d_unlinked */
25
26
#include <linux/fs.h> /* struct inode */
26
27
#include <linux/fsnotify_backend.h>
27
28
#include <linux/inotify.h>
28
29
#include <linux/path.h> /* struct path */
29
30
#include <linux/slab.h> /* kmem_* */
30
31
#include <linux/types.h>
32
#include <linux/sched.h>
32
34
#include "inotify.h"
34
static int inotify_handle_event(struct fsnotify_group *group, struct fsnotify_event *event)
36
struct fsnotify_mark_entry *entry;
37
struct inotify_inode_mark_entry *ientry;
37
* Check if 2 events contain the same information. We do not compare private data
38
* but at this moment that isn't a problem for any know fsnotify listeners.
40
static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new)
42
if ((old->mask == new->mask) &&
43
(old->to_tell == new->to_tell) &&
44
(old->data_type == new->data_type) &&
45
(old->name_len == new->name_len)) {
46
switch (old->data_type) {
47
case (FSNOTIFY_EVENT_INODE):
48
/* remember, after old was put on the wait_q we aren't
49
* allowed to look at the inode any more, only thing
50
* left to check was if the file_name is the same */
52
!strcmp(old->file_name, new->file_name))
55
case (FSNOTIFY_EVENT_PATH):
56
if ((old->path.mnt == new->path.mnt) &&
57
(old->path.dentry == new->path.dentry))
60
case (FSNOTIFY_EVENT_NONE):
61
if (old->mask & FS_Q_OVERFLOW)
63
else if (old->mask & FS_IN_IGNORED)
71
static struct fsnotify_event *inotify_merge(struct list_head *list,
72
struct fsnotify_event *event)
74
struct fsnotify_event_holder *last_holder;
75
struct fsnotify_event *last_event;
77
/* and the list better be locked by something too */
78
spin_lock(&event->lock);
80
last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list);
81
last_event = last_holder->event;
82
if (event_compare(last_event, event))
83
fsnotify_get_event(last_event);
87
spin_unlock(&event->lock);
92
static int inotify_handle_event(struct fsnotify_group *group,
93
struct fsnotify_mark *inode_mark,
94
struct fsnotify_mark *vfsmount_mark,
95
struct fsnotify_event *event)
97
struct inotify_inode_mark *i_mark;
38
98
struct inode *to_tell;
39
99
struct inotify_event_private_data *event_priv;
40
100
struct fsnotify_event_private_data *fsn_event_priv;
101
struct fsnotify_event *added_event;
104
BUG_ON(vfsmount_mark);
106
pr_debug("%s: group=%p event=%p to_tell=%p mask=%x\n", __func__, group,
107
event, event->to_tell, event->mask);
43
109
to_tell = event->to_tell;
45
spin_lock(&to_tell->i_lock);
46
entry = fsnotify_find_mark_entry(group, to_tell);
47
spin_unlock(&to_tell->i_lock);
48
/* race with watch removal? We already passes should_send */
51
ientry = container_of(entry, struct inotify_inode_mark_entry,
111
i_mark = container_of(inode_mark, struct inotify_inode_mark,
55
115
event_priv = kmem_cache_alloc(event_priv_cachep, GFP_KERNEL);
56
116
if (unlikely(!event_priv))
61
121
fsn_event_priv->group = group;
62
122
event_priv->wd = wd;
64
ret = fsnotify_add_notify_event(group, event, fsn_event_priv);
124
added_event = fsnotify_add_notify_event(group, event, fsn_event_priv, inotify_merge);
66
126
inotify_free_event_priv(fsn_event_priv);
67
/* EEXIST says we tail matched, EOVERFLOW isn't something
68
* to report up the stack. */
69
if ((ret == -EEXIST) ||
127
if (!IS_ERR(added_event))
128
fsnotify_put_event(added_event);
130
ret = PTR_ERR(added_event);
75
* If we hold the entry until after the event is on the queue
76
* IN_IGNORED won't be able to pass this event in the queue
78
fsnotify_put_mark(entry);
133
if (inode_mark->mask & IN_ONESHOT)
134
fsnotify_destroy_mark(inode_mark);
83
static void inotify_freeing_mark(struct fsnotify_mark_entry *entry, struct fsnotify_group *group)
139
static void inotify_freeing_mark(struct fsnotify_mark *fsn_mark, struct fsnotify_group *group)
85
inotify_ignored_and_remove_idr(entry, group);
141
inotify_ignored_and_remove_idr(fsn_mark, group);
88
static bool inotify_should_send_event(struct fsnotify_group *group, struct inode *inode, __u32 mask)
144
static bool inotify_should_send_event(struct fsnotify_group *group, struct inode *inode,
145
struct fsnotify_mark *inode_mark,
146
struct fsnotify_mark *vfsmount_mark,
147
__u32 mask, void *data, int data_type)
90
struct fsnotify_mark_entry *entry;
93
spin_lock(&inode->i_lock);
94
entry = fsnotify_find_mark_entry(group, inode);
95
spin_unlock(&inode->i_lock);
99
mask = (mask & ~FS_EVENT_ON_CHILD);
100
send = (entry->mask & mask);
102
/* find took a reference */
103
fsnotify_put_mark(entry);
149
if ((inode_mark->mask & FS_EXCL_UNLINK) &&
150
(data_type == FSNOTIFY_EVENT_PATH)) {
151
struct path *path = data;
153
if (d_unlinked(path->dentry))
115
167
static int idr_callback(int id, void *p, void *data)
117
struct fsnotify_mark_entry *entry;
118
struct inotify_inode_mark_entry *ientry;
169
struct fsnotify_mark *fsn_mark;
170
struct inotify_inode_mark *i_mark;
119
171
static bool warned = false;
126
ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
178
i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
128
WARN(1, "inotify closing but id=%d for entry=%p in group=%p still in "
180
WARN(1, "inotify closing but id=%d for fsn_mark=%p in group=%p still in "
129
181
"idr. Probably leaking memory\n", id, p, data);