Commit fc013a58 authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'for-linus' of git://git.infradead.org/users/eparis/notify

* 'for-linus' of git://git.infradead.org/users/eparis/notify:
  inotify: use GFP_NOFS under potential memory pressure
  fsnotify: fix inotify tail drop check with path entries
  inotify: check filename before dropping repeat events
  fsnotify: use def_bool in kconfig instead of letting the user choose
  inotify: fix error paths in inotify_update_watch
  inotify: do not leak inode marks in inotify_add_watch
  inotify: drop user watch count when a watch is removed
parents 3a542974 f44aebcc
config FSNOTIFY config FSNOTIFY
bool "Filesystem notification backend" def_bool n
default y
---help---
fsnotify is a backend for filesystem notification. fsnotify does
not provide any userspace interface but does provide the basis
needed for other notification schemes such as dnotify, inotify,
and fanotify.
Say Y here to enable fsnotify suport.
If unsure, say Y.
source "fs/notify/dnotify/Kconfig" source "fs/notify/dnotify/Kconfig"
source "fs/notify/inotify/Kconfig" source "fs/notify/inotify/Kconfig"
config DNOTIFY config DNOTIFY
bool "Dnotify support" bool "Dnotify support"
depends on FSNOTIFY select FSNOTIFY
default y default y
help help
Dnotify is a directory-based per-fd file change notification system Dnotify is a directory-based per-fd file change notification system
......
...@@ -159,7 +159,9 @@ void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is, const ...@@ -159,7 +159,9 @@ void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is, const
if (!group->ops->should_send_event(group, to_tell, mask)) if (!group->ops->should_send_event(group, to_tell, mask))
continue; continue;
if (!event) { if (!event) {
event = fsnotify_create_event(to_tell, mask, data, data_is, file_name, cookie); event = fsnotify_create_event(to_tell, mask, data,
data_is, file_name, cookie,
GFP_KERNEL);
/* shit, we OOM'd and now we can't tell, maybe /* shit, we OOM'd and now we can't tell, maybe
* someday someone else will want to do something * someday someone else will want to do something
* here */ * here */
......
...@@ -15,7 +15,7 @@ config INOTIFY ...@@ -15,7 +15,7 @@ config INOTIFY
config INOTIFY_USER config INOTIFY_USER
bool "Inotify support for userspace" bool "Inotify support for userspace"
depends on FSNOTIFY select FSNOTIFY
default y default y
---help--- ---help---
Say Y here to enable inotify support for userspace, including the Say Y here to enable inotify support for userspace, including the
......
...@@ -57,7 +57,6 @@ int inotify_max_user_watches __read_mostly; ...@@ -57,7 +57,6 @@ int inotify_max_user_watches __read_mostly;
static struct kmem_cache *inotify_inode_mark_cachep __read_mostly; static struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
struct kmem_cache *event_priv_cachep __read_mostly; struct kmem_cache *event_priv_cachep __read_mostly;
static struct fsnotify_event *inotify_ignored_event;
/* /*
* When inotify registers a new group it increments this and uses that * When inotify registers a new group it increments this and uses that
...@@ -365,6 +364,17 @@ static int inotify_find_inode(const char __user *dirname, struct path *path, uns ...@@ -365,6 +364,17 @@ static int inotify_find_inode(const char __user *dirname, struct path *path, uns
return error; return error;
} }
static void inotify_remove_from_idr(struct fsnotify_group *group,
struct inotify_inode_mark_entry *ientry)
{
struct idr *idr;
spin_lock(&group->inotify_data.idr_lock);
idr = &group->inotify_data.idr;
idr_remove(idr, ientry->wd);
spin_unlock(&group->inotify_data.idr_lock);
ientry->wd = -1;
}
/* /*
* Send IN_IGNORED for this wd, remove this wd from the idr, and drop the * Send IN_IGNORED for this wd, remove this wd from the idr, and drop the
* internal reference help on the mark because it is in the idr. * internal reference help on the mark because it is in the idr.
...@@ -373,13 +383,19 @@ void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry, ...@@ -373,13 +383,19 @@ void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry,
struct fsnotify_group *group) struct fsnotify_group *group)
{ {
struct inotify_inode_mark_entry *ientry; struct inotify_inode_mark_entry *ientry;
struct fsnotify_event *ignored_event;
struct inotify_event_private_data *event_priv; struct inotify_event_private_data *event_priv;
struct fsnotify_event_private_data *fsn_event_priv; struct fsnotify_event_private_data *fsn_event_priv;
struct idr *idr;
ignored_event = fsnotify_create_event(NULL, FS_IN_IGNORED, NULL,
FSNOTIFY_EVENT_NONE, NULL, 0,
GFP_NOFS);
if (!ignored_event)
return;
ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry); ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
event_priv = kmem_cache_alloc(event_priv_cachep, GFP_KERNEL); event_priv = kmem_cache_alloc(event_priv_cachep, GFP_NOFS);
if (unlikely(!event_priv)) if (unlikely(!event_priv))
goto skip_send_ignore; goto skip_send_ignore;
...@@ -388,7 +404,7 @@ void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry, ...@@ -388,7 +404,7 @@ void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry,
fsn_event_priv->group = group; fsn_event_priv->group = group;
event_priv->wd = ientry->wd; event_priv->wd = ientry->wd;
fsnotify_add_notify_event(group, inotify_ignored_event, fsn_event_priv); fsnotify_add_notify_event(group, ignored_event, fsn_event_priv);
/* did the private data get added? */ /* did the private data get added? */
if (list_empty(&fsn_event_priv->event_list)) if (list_empty(&fsn_event_priv->event_list))
...@@ -396,14 +412,16 @@ void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry, ...@@ -396,14 +412,16 @@ void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry,
skip_send_ignore: skip_send_ignore:
/* matches the reference taken when the event was created */
fsnotify_put_event(ignored_event);
/* remove this entry from the idr */ /* remove this entry from the idr */
spin_lock(&group->inotify_data.idr_lock); inotify_remove_from_idr(group, ientry);
idr = &group->inotify_data.idr;
idr_remove(idr, ientry->wd);
spin_unlock(&group->inotify_data.idr_lock);
/* removed from idr, drop that reference */ /* removed from idr, drop that reference */
fsnotify_put_mark(entry); fsnotify_put_mark(entry);
atomic_dec(&group->inotify_data.user->inotify_watches);
} }
/* ding dong the mark is dead */ /* ding dong the mark is dead */
...@@ -418,6 +436,7 @@ static int inotify_update_watch(struct fsnotify_group *group, struct inode *inod ...@@ -418,6 +436,7 @@ static int inotify_update_watch(struct fsnotify_group *group, struct inode *inod
{ {
struct fsnotify_mark_entry *entry = NULL; struct fsnotify_mark_entry *entry = NULL;
struct inotify_inode_mark_entry *ientry; struct inotify_inode_mark_entry *ientry;
struct inotify_inode_mark_entry *tmp_ientry;
int ret = 0; int ret = 0;
int add = (arg & IN_MASK_ADD); int add = (arg & IN_MASK_ADD);
__u32 mask; __u32 mask;
...@@ -428,54 +447,66 @@ static int inotify_update_watch(struct fsnotify_group *group, struct inode *inod ...@@ -428,54 +447,66 @@ static int inotify_update_watch(struct fsnotify_group *group, struct inode *inod
if (unlikely(!mask)) if (unlikely(!mask))
return -EINVAL; return -EINVAL;
ientry = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL); tmp_ientry = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
if (unlikely(!ientry)) if (unlikely(!tmp_ientry))
return -ENOMEM; return -ENOMEM;
/* we set the mask at the end after attaching it */ /* we set the mask at the end after attaching it */
fsnotify_init_mark(&ientry->fsn_entry, inotify_free_mark); fsnotify_init_mark(&tmp_ientry->fsn_entry, inotify_free_mark);
ientry->wd = 0; tmp_ientry->wd = -1;
find_entry: find_entry:
spin_lock(&inode->i_lock); spin_lock(&inode->i_lock);
entry = fsnotify_find_mark_entry(group, inode); entry = fsnotify_find_mark_entry(group, inode);
spin_unlock(&inode->i_lock); spin_unlock(&inode->i_lock);
if (entry) { if (entry) {
kmem_cache_free(inotify_inode_mark_cachep, ientry);
ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry); ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
} else { } else {
if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches) { ret = -ENOSPC;
ret = -ENOSPC; if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches)
goto out_err;
}
ret = fsnotify_add_mark(&ientry->fsn_entry, group, inode);
if (ret == -EEXIST)
goto find_entry;
else if (ret)
goto out_err; goto out_err;
entry = &ientry->fsn_entry;
retry: retry:
ret = -ENOMEM; ret = -ENOMEM;
if (unlikely(!idr_pre_get(&group->inotify_data.idr, GFP_KERNEL))) if (unlikely(!idr_pre_get(&group->inotify_data.idr, GFP_KERNEL)))
goto out_err; goto out_err;
spin_lock(&group->inotify_data.idr_lock); spin_lock(&group->inotify_data.idr_lock);
/* if entry is added to the idr we keep the reference obtained ret = idr_get_new_above(&group->inotify_data.idr, &tmp_ientry->fsn_entry,
* through fsnotify_mark_add. remember to drop this reference group->inotify_data.last_wd,
* when entry is removed from idr */ &tmp_ientry->wd);
ret = idr_get_new_above(&group->inotify_data.idr, entry,
++group->inotify_data.last_wd,
&ientry->wd);
spin_unlock(&group->inotify_data.idr_lock); spin_unlock(&group->inotify_data.idr_lock);
if (ret) { if (ret) {
if (ret == -EAGAIN) if (ret == -EAGAIN)
goto retry; goto retry;
goto out_err; goto out_err;
} }
ret = fsnotify_add_mark(&tmp_ientry->fsn_entry, group, inode);
if (ret) {
inotify_remove_from_idr(group, tmp_ientry);
if (ret == -EEXIST)
goto find_entry;
goto out_err;
}
/* tmp_ientry has been added to the inode, so we are all set up.
* now we just need to make sure tmp_ientry doesn't get freed and
* we need to set up entry and ientry so the generic code can
* do its thing. */
ientry = tmp_ientry;
entry = &ientry->fsn_entry;
tmp_ientry = NULL;
atomic_inc(&group->inotify_data.user->inotify_watches); atomic_inc(&group->inotify_data.user->inotify_watches);
/* update the idr hint */
group->inotify_data.last_wd = ientry->wd;
/* we put the mark on the idr, take a reference */
fsnotify_get_mark(entry);
} }
ret = ientry->wd;
spin_lock(&entry->lock); spin_lock(&entry->lock);
old_mask = entry->mask; old_mask = entry->mask;
...@@ -506,14 +537,19 @@ static int inotify_update_watch(struct fsnotify_group *group, struct inode *inod ...@@ -506,14 +537,19 @@ static int inotify_update_watch(struct fsnotify_group *group, struct inode *inod
fsnotify_recalc_group_mask(group); fsnotify_recalc_group_mask(group);
} }
return ientry->wd; /* this either matches fsnotify_find_mark_entry, or init_mark_entry
* depending on which path we took... */
fsnotify_put_mark(entry);
out_err: out_err:
/* see this isn't supposed to happen, just kill the watch */ /* could be an error, could be that we found an existing mark */
if (entry) { if (tmp_ientry) {
fsnotify_destroy_mark_by_entry(entry); /* on the idr but didn't make it on the inode */
fsnotify_put_mark(entry); if (tmp_ientry->wd != -1)
inotify_remove_from_idr(group, tmp_ientry);
kmem_cache_free(inotify_inode_mark_cachep, tmp_ientry);
} }
return ret; return ret;
} }
...@@ -721,9 +757,6 @@ static int __init inotify_user_setup(void) ...@@ -721,9 +757,6 @@ static int __init inotify_user_setup(void)
inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark_entry, SLAB_PANIC); inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark_entry, SLAB_PANIC);
event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC); event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC);
inotify_ignored_event = fsnotify_create_event(NULL, FS_IN_IGNORED, NULL, FSNOTIFY_EVENT_NONE, NULL, 0);
if (!inotify_ignored_event)
panic("unable to allocate the inotify ignored event\n");
inotify_max_queued_events = 16384; inotify_max_queued_events = 16384;
inotify_max_user_instances = 128; inotify_max_user_instances = 128;
......
...@@ -136,18 +136,24 @@ static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new ...@@ -136,18 +136,24 @@ static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new
{ {
if ((old->mask == new->mask) && if ((old->mask == new->mask) &&
(old->to_tell == new->to_tell) && (old->to_tell == new->to_tell) &&
(old->data_type == new->data_type)) { (old->data_type == new->data_type) &&
(old->name_len == new->name_len)) {
switch (old->data_type) { switch (old->data_type) {
case (FSNOTIFY_EVENT_INODE): case (FSNOTIFY_EVENT_INODE):
if (old->inode == new->inode) /* remember, after old was put on the wait_q we aren't
* allowed to look at the inode any more, only thing
* left to check was if the file_name is the same */
if (old->name_len &&
!strcmp(old->file_name, new->file_name))
return true; return true;
break; break;
case (FSNOTIFY_EVENT_PATH): case (FSNOTIFY_EVENT_PATH):
if ((old->path.mnt == new->path.mnt) && if ((old->path.mnt == new->path.mnt) &&
(old->path.dentry == new->path.dentry)) (old->path.dentry == new->path.dentry))
return true; return true;
break;
case (FSNOTIFY_EVENT_NONE): case (FSNOTIFY_EVENT_NONE):
return true; return false;
}; };
} }
return false; return false;
...@@ -339,18 +345,19 @@ static void initialize_event(struct fsnotify_event *event) ...@@ -339,18 +345,19 @@ static void initialize_event(struct fsnotify_event *event)
* @name the filename, if available * @name the filename, if available
*/ */
struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data, struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
int data_type, const char *name, u32 cookie) int data_type, const char *name, u32 cookie,
gfp_t gfp)
{ {
struct fsnotify_event *event; struct fsnotify_event *event;
event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL); event = kmem_cache_alloc(fsnotify_event_cachep, gfp);
if (!event) if (!event)
return NULL; return NULL;
initialize_event(event); initialize_event(event);
if (name) { if (name) {
event->file_name = kstrdup(name, GFP_KERNEL); event->file_name = kstrdup(name, gfp);
if (!event->file_name) { if (!event->file_name) {
kmem_cache_free(fsnotify_event_cachep, event); kmem_cache_free(fsnotify_event_cachep, event);
return NULL; return NULL;
......
...@@ -352,7 +352,7 @@ extern void fsnotify_unmount_inodes(struct list_head *list); ...@@ -352,7 +352,7 @@ extern void fsnotify_unmount_inodes(struct list_head *list);
/* put here because inotify does some weird stuff when destroying watches */ /* put here because inotify does some weird stuff when destroying watches */
extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask,
void *data, int data_is, const char *name, void *data, int data_is, const char *name,
u32 cookie); u32 cookie, gfp_t gfp);
#else #else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment