mutex: add atomic_dec_and_mutex_lock
Subsystems (6)
PATCH 1/13 mutex: add atomic_dec_and_mutex_lock
Much like the atomic_dec_and_lock() function in which we take an hold a spin_lock if we drop the atomic to 0 this function takes and holds the mutex if we dec the atomic to 0.
---
include/linux/mutex.h | 23 +++++++++++++++++++++++ 1 files changed, 23 insertions(+), 0 deletions(-)
include/linux/mutex.h +23 -0
@@ -151,4 +151,27 @@ extern int __must_check mutex_lock_killable(struct mutex *lock);extern int mutex_trylock (struct mutex *lock);extern void mutex_unlock (struct mutex *lock); +/** +* atomic_dec_and_mutex_lock - return holding mutex if we dec to 0 +* @cnt: the atomic which we are to dec +* @lock: the mutex to return holding if we dec to 0 +* +* return true and hold lock if we dec to 0, return false otherwise +*/ +static inline int atomic_dec_and_mutex_lock (atomic_t *cnt,struct mutex *lock) +{ +/* dec if we can't possibly hit 0 */ +if (atomic_add_unless (cnt,-1 ,1 )) +return 0 ; +/* we might hit 0, so take the lock */ +mutex_lock (lock); +if (!atomic_dec_and_test (cnt)) { +/* when we actually did the dec, we didn't hit 0 */ +mutex_unlock (lock); +return 0 ; + } +/* we hit 0, and we hold the lock */ +return 1 ; +} +#endif
PATCH 2/13 fsnotify: unified filesystem notification backend
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. fsnotify can be extended to be the backend for inotify or the upcoming fanotify. fsnotify provides a mechanism for "groups" to register for some set of filesystem events and to then deliver those events to those groups for processing.
fsnotify has a number of benefits, the first being actually shrinking the size of an inode. Before fsnotify to support both dnotify and inotify an inode had
unsigned long i_dnotify_mask; /* Directory notify events */
struct dnotify_struct *i_dnotify; /* for directory notifications */
struct list_head inotify_watches; /* watches on this inode */
struct mutex inotify_mutex; /* protects the watches listBut with fsnotify this same functionallity (and more) is done with just
__u32 i_fsnotify_mask; /* all events for this inode */
struct hlist_head i_fsnotify_mark_entries; /* marks on this inode */
That's right, inotify, dnotify, and fanotify all in 64 bits. We used that much space just in inotify_watches alone, before this patch set.
fsnotify object lifetime and locking is MUCH better than what we have today. inotify locking is incredibly complex. See 8f7b0ba1c8539 as an example of what's been busted since inception. inotify needs to know internal semantics of superblock destruction and unmounting to function. The inode pinning and vfs contortions are horrible.
no fsnotify implementers do allocation under locks. This means things like f04b30de3 which (due to an overabundance of caution) changes GFP_KERNEL to GFP_NOFS can be reverted. There are no longer any allocation rules when using or implementing your own fsnotify listener.
fsnotify paves the way for fanotify. people may not care for the original companies that pushed for TALPA, but fanotify was designed with flexibility in mind. A first group that wants fanotify like interfaces is the readahead group. So they can be profiling at boot time in order to dynamicly tune readahead to help with boot speed. I've got other ideas how to use fanotify to speed up boot when dealing with encrypted mounts, but I'm not ready to say it yet since I don't know if my idea will work.
This patch series just builds fsnotify to the point that it can implement dnotify and inotify_user. Patches exist and will be sent soon after acceptance to finish the in kernel inotify conversion (audit) and implement fanotify.
---
fs/notify/Kconfig | 13 +++ fs/notify/Makefile | 2 fs/notify/fsnotify.c | 79 ++++++++++++++++++ fs/notify/fsnotify.h | 17 ++++ fs/notify/group.c | 168 ++++++++++++++++++++++++++++++++++++++ fs/notify/notification.c | 116 ++++++++++++++++++++++++++ include/linux/fsnotify.h | 129 +++++++++++++++++++++-------- include/linux/fsnotify_backend.h | 135 +++++++++++++++++++++++++++++++ 8 files changed, 622 insertions(+), 37 deletions(-) create mode 100644 fs/notify/fsnotify.c create mode 100644 fs/notify/fsnotify.h create mode 100644 fs/notify/group.c create mode 100644 fs/notify/notification.c create mode 100644 include/linux/fsnotify_backend.h
fs/notify/Kconfig +13 -0
@@ -1,2 +1,15 @@ +config FSNOTIFY + bool "Filesystem notification backend" + 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/inotify/Kconfig"
fs/notify/Makefile +2 -0
@@ -1,2 +1,4 @@ +obj-$(CONFIG_FSNOTIFY) += fsnotify.o notification.o group.o + obj-y += dnotify/ obj-y += inotify/
fs/notify/fsnotify.c new file +79 -0
79 lines not on this page · raw message
fs/notify/fsnotify.h new file +17 -0
@@ -0,0 +1,17 @@ +#ifndef _LINUX_FSNOTIFY_PRIVATE_H +#define _LINUX_FSNOTIFY_PRIVATE_H + +#include <linux/dcache.h> +#include <linux/list.h> +#include <linux/fs.h> +#include <linux/path.h> +#include <linux/spinlock.h> + +#include <linux/fsnotify.h> + +#include <asm/atomic.h> + +extern struct srcu_struct fsnotify_grp_srcu; +extern struct list_head fsnotify_groups; +extern __u32 fsnotify_mask; +#endif /* _LINUX_FSNOTIFY_PRIVATE_H */
fs/notify/group.c new file +168 -0
168 lines not on this page · raw message
fs/notify/notification.c new file +116 -0
116 lines not on this page · raw message
include/linux/fsnotify.h +92 -37
301 lines not on this page · raw message
include/linux/fsnotify_backend.h new file +135 -0
135 lines not on this page · raw message
Re: [PATCH 02/13] fsnotify: unified filesystem notification backend
Quoted 43 lines · Eric Paris
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. fsnotify can be extended to be the backend for inotify or the upcoming fanotify. fsnotify provides a mechanism for "groups" to register for some set of filesystem events and to then deliver those events to those groups for processing.
fsnotify has a number of benefits, the first being actually shrinking the size of an inode. Before fsnotify to support both dnotify and inotify an inode had
unsigned long i_dnotify_mask; /* Directory notify events */ struct dnotify_struct *i_dnotify; /* for directory notifications */ struct list_head inotify_watches; /* watches on this inode */ struct mutex inotify_mutex; /* protects the watches listBut with fsnotify this same functionallity (and more) is done with just
__u32i_fsnotify_mask; /* all events for this inode */
structhlist_headi_fsnotify_mark_entries; /* marks on this inode */That's right, inotify, dnotify, and fanotify all in 64 bits. We used that much space just in
inotify_watchesalone, before this patch set.fsnotify object lifetime and locking is MUCH better than what we have today. inotify locking is incredibly complex. See
8f7b0ba1c8539as an example of what's been busted since inception. inotify needs to know internal semantics of superblock destruction and unmounting to function. The inode pinning and vfs contortions are horrible.no fsnotify implementers do allocation under locks. This means things like
f04b30de3which (due to an overabundance of caution) changesGFP_KERNELtoGFP_NOFScan be reverted. There are no longer any allocation rules when using or implementing your own fsnotify listener.fsnotify paves the way for fanotify. people may not care for the original companies that pushed for TALPA, but fanotify was designed with flexibility in mind. A first group that wants fanotify like interfaces is the readahead group. So they can be profiling at boot time in order to dynamicly tune readahead to help with boot speed. I've got other ideas how to use fanotify to speed up boot when dealing with encrypted mounts, but I'm not ready to say it yet since I don't know if my idea will work.
Quoted 4 lines
This patch series just builds fsnotify to the point that it can implement dnotify and
inotify_user. Patches exist and will be sent soon after acceptance to finish the in kernel inotify conversion (audit) and implement fanotify.
Looks good from an SRCU viewpoint. The only comment I have (other than running commentary interspersed) is why no
BUG_ON(!mutex_is_locked(&fsnotify_grp_mutex));
in fsnotify_add_group().
You have a global lock fsnotify_grp_mutex covering all of the updates I can see, use fsnotify_grp_srcu consistently, and have all traversals covered either by srcu_read_lock() or fsnotify_grp_mutex.
Thanx, Paul
Quoted 94 lines
Signed-off-by Eric Paris <[email protected]>---
fs/notify/Kconfig | 13 +++ fs/notify/Makefile | 2 fs/notify/fsnotify.c | 79 ++++++++++++++++++ fs/notify/fsnotify.h | 17 ++++ fs/notify/group.c | 168 ++++++++++++++++++++++++++++++++++++++ fs/notify/notification.c | 116 ++++++++++++++++++++++++++ include/linux/fsnotify.h | 129 +++++++++++++++++++++-------- include/linux/fsnotify_backend.h | 135 +++++++++++++++++++++++++++++++ 8 files changed, 622 insertions(+), 37 deletions(-) create mode 100644 fs/notify/fsnotify.c create mode 100644 fs/notify/fsnotify.h create mode 100644 fs/notify/group.c create mode 100644 fs/notify/notification.c create mode 100644 include/linux/fsnotify_backend.hfs/notify/Kconfig +13
-0@@ -1,2 +1,15 @@ +config FSNOTIFY + bool "Filesystem notification backend" + 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/inotify/Kconfig"fs/notify/Makefile +2
-0@@ -1,2 +1,4 @@ +obj-$(CONFIG_FSNOTIFY) += fsnotify.o notification.o group.o + obj-y += dnotify/ obj-y += inotify/fs/notify/fsnotify.c new file +41
-0@@ -0,0 +1,79 @@ +/* +* Copyright (C) 2008 Red Hat, Inc., Eric Paris <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2, or (at your option) +* any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; see the file COPYING. If not, write to +* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include <linux/dcache.h> +#include <linux/fs.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/srcu.h> + +#include <linux/fsnotify_backend.h> +#include "fsnotify.h" + +/* +* This is the main call to fsnotify. The VFS calls into hook specific functions +* in linux/fsnotify.h. Those functions then in turn call here. Here will call +* out to all of the registered fsnotify_group. Those groups can then use the +* notification event in whatever means they feel necessary. +*/ +void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is) +{ +struct fsnotify_group *group; +struct fsnotify_event *event =NULL ; +int idx; + +if (list_empty (&fsnotify_groups)) +return ;
fs/notify/fsnotify.c line 51
Quoted 10 lines
fs/notify/fsnotify.c new file +10
-0+ +if (!(mask & fsnotify_mask)) +return ; + +/* +* SRCU!! the groups list is very very much read only and the path is +* very hot. The VAST majority of events are not going to need to do +* anything other than walk the list so it's crazy to pre-allocate. +*/ + idx =srcu_read_lock (&fsnotify_grp_srcu);
All the srcu operations use the same srcu_struct, as required.
fs/notify/fsnotify.c line 52
Quoted 1 line
fs/notify/fsnotify.c new file +1
-0+list_for_each_entry_rcu (group, &fsnotify_groups, group_list) {
Under srcu_read_lock(), as required.
Quoted 89 lines
fs/notify/fsnotify.c new file +27
-0+if (mask & group->mask) { +if (!event) { + event =fsnotify_create_event (to_tell, mask, data, data_is); +/* shit, we OOM'd and now we can't tell, maybe +* someday someone else will want to do something +* here */ +if (!event) +break ; + } + group->ops->handle_event (group, event); + } + } +srcu_read_unlock (&fsnotify_grp_srcu, idx); +/* +* fsnotify_create_event() took a reference so the event can't be cleaned +* up while we are still trying to add it to lists, drop that one. +*/ +if (event) +fsnotify_put_event (event); +} +EXPORT_SYMBOL_GPL (fsnotify); + +static __init int fsnotify_init (void ) +{ +return init_srcu_struct (&fsnotify_grp_srcu); +} +subsys_initcall (fsnotify_init);fs/notify/fsnotify.h new file +17
-0@@ -0,0 +1,17 @@ +#ifndef _LINUX_FSNOTIFY_PRIVATE_H +#define _LINUX_FSNOTIFY_PRIVATE_H + +#include <linux/dcache.h> +#include <linux/list.h> +#include <linux/fs.h> +#include <linux/path.h> +#include <linux/spinlock.h> + +#include <linux/fsnotify.h> + +#include <asm/atomic.h> + +extern struct srcu_struct fsnotify_grp_srcu; +extern struct list_head fsnotify_groups; +extern __u32 fsnotify_mask; +#endif /* _LINUX_FSNOTIFY_PRIVATE_H */ fs/notify/group.c new file +33
-0@@ -0,0 +1,168 @@ +/* +* Copyright (C) 2008 Red Hat, Inc., Eric Paris <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2, or (at your option) +* any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; see the file COPYING. If not, write to +* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include <linux/list.h> +#include <linux/mutex.h> +#include <linux/slab.h> +#include <linux/srcu.h> +#include <linux/rculist.h> +#include <linux/wait.h> + +#include <linux/fsnotify_backend.h> +#include "fsnotify.h" + +#include <asm/atomic.h> + +DEFINE_MUTEX (fsnotify_grp_mutex); +struct srcu_struct fsnotify_grp_srcu; +LIST_HEAD (fsnotify_groups);
fs/notify/group.c line 43
Quoted 10 lines
fs/notify/group.c new file +10
-0+__u32 fsnotify_mask; + +void fsnotify_recalc_global_mask (void ) +{ +struct fsnotify_group *group; +__u32 mask =0 ; +int idx; + + idx =srcu_read_lock (&fsnotify_grp_srcu); +list_for_each_entry_rcu (group, &fsnotify_groups, group_list) {
Under srcu_read_lock(), as required.
fs/notify/group.c line 52
Quoted 9 lines
fs/notify/group.c new file +9
-0+ mask |= group->mask; + } +srcu_read_unlock (&fsnotify_grp_srcu, idx); + fsnotify_mask = mask; +} + +static void fsnotify_add_group (struct fsnotify_group *group) +{ +list_add_rcu (&group->group_list, &fsnotify_groups);
OK, protected by fsnotify_grp_mutex.
Can't resist asking why no "BUG_ON(!mutex_is_locked(&fsnotify_grp_mutex));", though. ;-)
Quoted 12 lines
fs/notify/group.c new file +12
-0+ group->evicted =0 ; +} + +static void fsnotify_get_group (struct fsnotify_group *group) +{ +atomic_inc (&group->refcnt); +} + +static void fsnotify_destroy_group (struct fsnotify_group *group) +{ +if (group->ops->free_group_priv) + group->ops->free_group_priv (group);
fs/notify/group.c line 74
Quoted 10 lines
fs/notify/group.c new file +10
-0+ +kfree (group); +} + +static void __fsnotify_evict_group (struct fsnotify_group *group) +{ +BUG_ON (!mutex_is_locked (&fsnotify_grp_mutex)); + +if (!group->evicted) +list_del_rcu (&group->group_list);
OK, protected by fsnotify_grp_mutex or the BUG_ON trips. ;-)
Quoted 28 lines
fs/notify/group.c new file +28
-0+ group->evicted =1 ; +} + +void fsnotify_evict_group (struct fsnotify_group *group) +{ +mutex_lock (&fsnotify_grp_mutex); +__fsnotify_evict_group (group); +mutex_unlock (&fsnotify_grp_mutex); +} + +void fsnotify_put_group (struct fsnotify_group *group) +{ +if (!atomic_dec_and_mutex_lock (&group->refcnt, &fsnotify_grp_mutex)) +return ; + +/* OK, now we know that there's no other users *and* we hold mutex, +* so no new references will appear */ +__fsnotify_evict_group (group); + +/* now it's off the list, so the only thing we might care about is +* srcu acces.... */ +mutex_unlock (&fsnotify_grp_mutex); +synchronize_srcu (&fsnotify_grp_srcu); + +/* and now it is really dead. _Nothing_ could be seeing it */ +fsnotify_recalc_global_mask (); +fsnotify_destroy_group (group); +}
fs/notify/group.c line 112
Quoted 10 lines
fs/notify/group.c new file +10
-0+ +static struct fsnotify_group *fsnotify_find_group (unsigned int group_num,__u32 mask, +const struct fsnotify_ops *ops) +{ +struct fsnotify_group *group_iter; +struct fsnotify_group *group =NULL ; + +BUG_ON (!mutex_is_locked (&fsnotify_grp_mutex)); + +list_for_each_entry_rcu (group_iter, &fsnotify_groups, group_list) {
Given that you hold fsnotify_grp_mutex, you in theory don't need the _rcu form, but not a problem. Just wanted you to know that I was paying attention. ;-)
Quoted 646 lines
fs/notify/group.c new file +56
-0+if (group_iter->group_num == group_num) { +if ((group_iter->mask == mask) && + (group_iter->ops == ops)) { +fsnotify_get_group (group_iter); + group = group_iter; + }else + group =ERR_PTR (-EEXIST ); + } + } +return group; +} + +/* +* Either finds an existing group which matches the group_num, mask, and ops or +* creates a new group and adds it to the global group list. In either case we +* take a reference for the group returned. +* +* low use function, could be faster to check if the group is there before we do +* the allocation and the initialization, but this is only called when notification +* systems make changes, so why make it more complex? +*/ +struct fsnotify_group *fsnotify_obtain_group (unsigned int group_num,__u32 mask, +const struct fsnotify_ops *ops) +{ +struct fsnotify_group *group, *tgroup; + + group =kmalloc (sizeof (struct fsnotify_group ),GFP_KERNEL ); +if (!group) +return ERR_PTR (-ENOMEM ); + +atomic_set (&group->refcnt,1 ); + + group->group_num = group_num; + group->mask = mask; + + group->ops = ops; + +mutex_lock (&fsnotify_grp_mutex); + tgroup =fsnotify_find_group (group_num, mask, ops); +if (tgroup) { +/* group already exists */ +mutex_unlock (&fsnotify_grp_mutex); +/* destroy the new one we made */ +fsnotify_put_group (group); +return tgroup; + } + +/* group not found, add a new one */ +fsnotify_add_group (group); +mutex_unlock (&fsnotify_grp_mutex); + +if (mask) +fsnotify_recalc_global_mask (); + +return group; +}fs/notify/notification.c new file +116
-0@@ -0,0 +1,116 @@ +/* +* Copyright (C) 2008 Red Hat, Inc., Eric Paris <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2, or (at your option) +* any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; see the file COPYING. If not, write to +* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include <linux/fs.h> +#include <linux/init.h> +#include <linux/kernel.h> +#include <linux/list.h> +#include <linux/mount.h> +#include <linux/mutex.h> +#include <linux/namei.h> +#include <linux/path.h> +#include <linux/slab.h> +#include <linux/spinlock.h> + +#include <asm/atomic.h> + +#include <linux/fsnotify_backend.h> +#include "fsnotify.h" + +static struct kmem_cache *event_kmem_cache; + +void fsnotify_get_event (struct fsnotify_event *event) +{ +atomic_inc (&event->refcnt); +} + +void fsnotify_put_event (struct fsnotify_event *event) +{ +if (!event) +return ; + +if (atomic_dec_and_test (&event->refcnt)) { +if (event->data_type ==FSNOTIFY_EVENT_PATH ) { +path_put (&event->path); + event->path.dentry =NULL ; + event->path.mnt =NULL ; + } + + event->mask =0 ; + +kmem_cache_free (event_kmem_cache, event); + } +} + +struct fsnotify_event *fsnotify_create_event (struct inode *to_tell,__u32 mask,void *data,int data_type) +{ +struct fsnotify_event *event; + + event =kmem_cache_alloc (event_kmem_cache,GFP_KERNEL ); +if (!event) +return NULL ; + +atomic_set (&event->refcnt,1 ); + +spin_lock_init (&event->lock); + + event->path.dentry =NULL ; + event->path.mnt =NULL ; + event->inode =NULL ; + + event->to_tell = to_tell; + +switch (data_type) { +case FSNOTIFY_EVENT_FILE : { +struct file *file = data; +struct path *path = &file->f_path; + event->path.dentry = path->dentry; + event->path.mnt = path->mnt; +path_get (&event->path); + event->data_type =FSNOTIFY_EVENT_PATH ; +break ; + } +case FSNOTIFY_EVENT_PATH : { +struct path *path = data; + event->path.dentry = path->dentry; + event->path.mnt = path->mnt; +path_get (&event->path); + event->data_type =FSNOTIFY_EVENT_PATH ; +break ; + } +case FSNOTIFY_EVENT_INODE : + event->inode = data; + event->data_type =FSNOTIFY_EVENT_INODE ; +break ; +default : +BUG (); + }; + + event->mask = mask; + +return event; +} + +__init int fsnotify_notification_init (void ) +{ + event_kmem_cache =kmem_cache_create ("fsnotify_event" ,sizeof (struct fsnotify_event ),0 ,SLAB_PANIC ,NULL ); + +return 0 ; +} +subsys_initcall (fsnotify_notification_init); +include/linux/fsnotify.h +92
-37@@ -13,6 +13,7 @@#include <linux/dnotify.h> #include <linux/inotify.h> +#include <linux/fsnotify_backend.h> #include <linux/audit.h> /* @@ -35,6 +36,17 @@ static inline void fsnotify_d_move(struct dentry *entry) }/* +* fsnotify_inoderemove - an inode is going away +*/ +static inline void fsnotify_inoderemove (struct inode *inode) +{ +inotify_inode_queue_event (inode,IN_DELETE_SELF ,0 ,NULL ,NULL ); +inotify_inode_is_dead (inode); + +fsnotify (inode,FS_DELETE_SELF , inode,FSNOTIFY_EVENT_INODE ); +} + +/* * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir */ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,@@ -43,28 +55,42 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, {struct inode *source = moved->d_inode;u32 cookie =inotify_get_cookie (); +__u32 old_dir_mask =0 ; +__u32 new_dir_mask =0 ; -if (old_dir == new_dir) +if (old_dir == new_dir) {inode_dir_notify (old_dir,DN_RENAME ); -else { + old_dir_mask =FS_DN_RENAME ; + }else {inode_dir_notify (old_dir,DN_DELETE ); + old_dir_mask =FS_DELETE ;inode_dir_notify (new_dir,DN_CREATE ); + new_dir_mask =FS_CREATE ; } -if (isdir) +if (isdir) { isdir =IN_ISDIR ; + old_dir_mask |=FS_IN_ISDIR ; + new_dir_mask |=FS_IN_ISDIR ; + } + + old_dir_mask |=FS_MOVED_FROM ; + new_dir_mask |=FS_MOVED_TO ; +inotify_inode_queue_event (old_dir,IN_MOVED_FROM |isdir,cookie,old_name, source);inotify_inode_queue_event (new_dir,IN_MOVED_TO |isdir, cookie, new_name, source); -if (target) { -inotify_inode_queue_event (target,IN_DELETE_SELF ,0 ,NULL ,NULL ); -inotify_inode_is_dead (target); - } +fsnotify (old_dir, old_dir_mask, old_dir,FSNOTIFY_EVENT_INODE ); +fsnotify (new_dir, new_dir_mask, new_dir,FSNOTIFY_EVENT_INODE ); + +if (target) +fsnotify_inoderemove (target);if (source) {inotify_inode_queue_event (source,IN_MOVE_SELF ,0 ,NULL ,NULL ); +fsnotify (source,FS_MOVE_SELF , moved->d_inode,FSNOTIFY_EVENT_INODE ); }audit_inode_child (new_name, moved, new_dir); }@@ -75,26 +101,19 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,static inline void fsnotify_nameremove (struct dentry *dentry,int isdir) {if (isdir) - isdir =IN_ISDIR ; + isdir =FS_IN_ISDIR ;dnotify_parent (dentry,DN_DELETE );inotify_dentry_parent_queue_event (dentry,IN_DELETE |isdir,0 , dentry->d_name.name); }/* -* fsnotify_inoderemove - an inode is going away -*/ -static inline void fsnotify_inoderemove (struct inode *inode) -{ -inotify_inode_queue_event (inode,IN_DELETE_SELF ,0 ,NULL ,NULL ); -inotify_inode_is_dead (inode); -} - -/* * fsnotify_link_count - inode's link count changed */ static inline void fsnotify_link_count (struct inode *inode) {inotify_inode_queue_event (inode,IN_ATTRIB ,0 ,NULL ,NULL ); + +fsnotify (inode,FS_ATTRIB , inode,FSNOTIFY_EVENT_INODE ); }/* @@ -106,6 +125,8 @@ static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)inotify_inode_queue_event (inode,IN_CREATE ,0 , dentry->d_name.name, dentry->d_inode);audit_inode_child (dentry->d_name.name, dentry, inode); + +fsnotify (inode,FS_CREATE , dentry->d_inode,FSNOTIFY_EVENT_INODE ); }/* @@ -120,6 +141,8 @@ static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct inode);fsnotify_link_count (inode);audit_inode_child (new_dentry->d_name.name, new_dentry, dir); + +fsnotify (dir,FS_CREATE , inode,FSNOTIFY_EVENT_INODE ); }/* @@ -131,6 +154,8 @@ static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)inotify_inode_queue_event (inode,IN_CREATE |IN_ISDIR ,0 , dentry->d_name.name, dentry->d_inode);audit_inode_child (dentry->d_name.name, dentry, inode); + +fsnotify (inode,FS_CREATE |FS_IN_ISDIR , dentry->d_inode,FSNOTIFY_EVENT_INODE ); }/* @@ -139,14 +164,16 @@ static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)static inline void fsnotify_access (struct dentry *dentry) {struct inode *inode = dentry->d_inode; -u32 mask =IN_ACCESS ; +__u32 mask =FS_ACCESS ;if (S_ISDIR (inode->i_mode)) - mask |=IN_ISDIR ; + mask |=FS_IN_ISDIR ;dnotify_parent (dentry,DN_ACCESS );inotify_dentry_parent_queue_event (dentry, mask,0 , dentry->d_name.name);inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL ); + +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ); }/* @@ -155,14 +182,16 @@ static inline void fsnotify_access(struct dentry *dentry)static inline void fsnotify_modify (struct dentry *dentry) {struct inode *inode = dentry->d_inode; -u32 mask =IN_MODIFY ; +__u32 mask =FS_MODIFY ;if (S_ISDIR (inode->i_mode)) - mask |=IN_ISDIR ; + mask |=FS_IN_ISDIR ;dnotify_parent (dentry,DN_MODIFY );inotify_dentry_parent_queue_event (dentry, mask,0 , dentry->d_name.name);inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL ); + +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ); }/* @@ -171,13 +200,15 @@ static inline void fsnotify_modify(struct dentry *dentry)static inline void fsnotify_open (struct dentry *dentry) {struct inode *inode = dentry->d_inode; -u32 mask =IN_OPEN ; +__u32 mask =FS_OPEN ;if (S_ISDIR (inode->i_mode)) - mask |=IN_ISDIR ; + mask |=FS_IN_ISDIR ;inotify_dentry_parent_queue_event (dentry, mask,0 , dentry->d_name.name);inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL ); + +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ); }/* @@ -189,13 +220,15 @@ static inline void fsnotify_close(struct file *file)struct inode *inode = dentry->d_inode;const char *name = dentry->d_name.name;fmode_t mode = file->f_mode; -u32 mask = (mode &FMODE_WRITE ) ?IN_CLOSE_WRITE :IN_CLOSE_NOWRITE ; +__u32 mask = (mode &FMODE_WRITE ) ?FS_CLOSE_WRITE :FS_CLOSE_NOWRITE ;if (S_ISDIR (inode->i_mode)) - mask |=IN_ISDIR ; + mask |=FS_IN_ISDIR ;inotify_dentry_parent_queue_event (dentry, mask,0 , name);inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL ); + +fsnotify (inode, mask, file,FSNOTIFY_EVENT_FILE ); }/* @@ -204,13 +237,15 @@ static inline void fsnotify_close(struct file *file)static inline void fsnotify_xattr (struct dentry *dentry) {struct inode *inode = dentry->d_inode; -u32 mask =IN_ATTRIB ; +__u32 mask =FS_ATTRIB ;if (S_ISDIR (inode->i_mode)) - mask |=IN_ISDIR ; + mask |=FS_IN_ISDIR ;inotify_dentry_parent_queue_event (dentry, mask,0 , dentry->d_name.name);inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL ); + +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ); }/* @@ -224,31 +259,31 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)u32 in_mask =0 ;if (ia_valid &ATTR_UID ) { - in_mask |=IN_ATTRIB ; + in_mask |=FS_ATTRIB ; dn_mask |=DN_ATTRIB ; }if (ia_valid &ATTR_GID ) { - in_mask |=IN_ATTRIB ; + in_mask |=FS_ATTRIB ; dn_mask |=DN_ATTRIB ; }if (ia_valid &ATTR_SIZE ) { - in_mask |=IN_MODIFY ; + in_mask |=FS_MODIFY ; dn_mask |=DN_MODIFY ; }/* both times implies a utime(s) call */ if ((ia_valid & (ATTR_ATIME |ATTR_MTIME )) == (ATTR_ATIME |ATTR_MTIME )) { - in_mask |=IN_ATTRIB ; + in_mask |=FS_ATTRIB ; dn_mask |=DN_ATTRIB ; }else if (ia_valid &ATTR_ATIME ) { - in_mask |=IN_ACCESS ; + in_mask |=FS_ACCESS ; dn_mask |=DN_ACCESS ; }else if (ia_valid &ATTR_MTIME ) { - in_mask |=IN_MODIFY ; + in_mask |=FS_MODIFY ; dn_mask |=DN_MODIFY ; }if (ia_valid &ATTR_MODE ) { - in_mask |=IN_ATTRIB ; + in_mask |=FS_ATTRIB ; dn_mask |=DN_ATTRIB ; }@@ -256,20 +291,40 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)dnotify_parent (dentry, dn_mask);if (in_mask) {if (S_ISDIR (inode->i_mode)) - in_mask |=IN_ISDIR ; + in_mask |=FS_IN_ISDIR ;inotify_inode_queue_event (inode, in_mask,0 ,NULL ,NULL );inotify_dentry_parent_queue_event (dentry, in_mask,0 , dentry->d_name.name); +fsnotify (inode, in_mask, inode,FSNOTIFY_EVENT_INODE ); } } -#ifdef CONFIG_INOTIFY /* inotify helpers */ +#if defined(CONFIG_INOTIFY ) || defined(CONFIG_FSNOTIFY )/* notify helpers */ /* * fsnotify_oldname_init - save off the old filename before we change it */ static inline const char *fsnotify_oldname_init (const char *name) { +BUILD_BUG_ON (IN_ACCESS !=FS_ACCESS ); +BUILD_BUG_ON (IN_MODIFY !=FS_MODIFY ); +BUILD_BUG_ON (IN_ATTRIB !=FS_ATTRIB ); +BUILD_BUG_ON (IN_CLOSE_WRITE !=FS_CLOSE_WRITE ); +BUILD_BUG_ON (IN_CLOSE_NOWRITE !=FS_CLOSE_NOWRITE ); +BUILD_BUG_ON (IN_OPEN !=FS_OPEN ); +BUILD_BUG_ON (IN_MOVED_FROM !=FS_MOVED_FROM ); +BUILD_BUG_ON (IN_MOVED_TO !=FS_MOVED_TO ); +BUILD_BUG_ON (IN_CREATE !=FS_CREATE ); +BUILD_BUG_ON (IN_DELETE !=FS_DELETE ); +BUILD_BUG_ON (IN_DELETE_SELF !=FS_DELETE_SELF ); +BUILD_BUG_ON (IN_MOVE_SELF !=FS_MOVE_SELF ); +BUILD_BUG_ON (IN_Q_OVERFLOW !=FS_Q_OVERFLOW ); + +BUILD_BUG_ON (IN_UNMOUNT !=FS_UNMOUNT ); +BUILD_BUG_ON (IN_ISDIR !=FS_IN_ISDIR ); +BUILD_BUG_ON (IN_IGNORED !=FS_IN_IGNORED ); +BUILD_BUG_ON (IN_ONESHOT !=FS_IN_ONESHOT ); +return kstrdup (name,GFP_KERNEL ); }@@ -281,7 +336,7 @@ static inline void fsnotify_oldname_free(const char *old_name) kfree(old_name); } -#else /* CONFIG_INOTIFY */ +#else /* CONFIG_INOTIFY || CONFIG_FSNOTIFY */ static inline const char *fsnotify_oldname_init (const char *name) {include/linux/fsnotify_backend.h new file +135
-1@@ -0,0 +1,135 @@ +/* +* Filesystem access notification for Linux +* +* Copyright (C) 2008 Red Hat, Inc., Eric Paris <[email protected]> +*/ + +#ifndef _LINUX_FSNOTIFY_BACKEND_H +#define _LINUX_FSNOTIFY_BACKEND_H + +#ifdef __KERNEL__ + +#include <linux/fs.h> /* struct inode */ +#include <linux/list.h> +#include <linux/path.h> /* struct path */ +#include <linux/spinlock.h> +#include <linux/wait.h> + +#include <asm/atomic.h> + +/* +* IN_* from inotfy.h lines up EXACTLY with FS_*, this is so we can easily +* convert between them. dnotify only needs conversion at watch creation +* so no perf loss there. fanotify isn't defined yet, so it can use the +* wholes if it needs more events. +*/ +#define FS_ACCESS 0x00000001ul/* File was accessed */ +#define FS_MODIFY 0x00000002ul/* File was modified */ +#define FS_ATTRIB 0x00000004ul/* Metadata changed */ +#define FS_CLOSE_WRITE 0x00000008ul/* Writtable file was closed */ +#define FS_CLOSE_NOWRITE 0x00000010ul/* Unwrittable file closed */ +#define FS_OPEN 0x00000020ul/* File was opened */ +#define FS_MOVED_FROM 0x00000040ul/* File was moved from X */ +#define FS_MOVED_TO 0x00000080ul/* File was moved to Y */ +#define FS_CREATE 0x00000100ul/* Subfile was created */ +#define FS_DELETE 0x00000200ul/* Subfile was deleted */ +#define FS_DELETE_SELF 0x00000400ul/* Self was deleted */ +#define FS_MOVE_SELF 0x00000800ul/* Self was moved */ + +#define FS_UNMOUNT 0x00002000ul/* inode on umount fs */ +#define FS_Q_OVERFLOW 0x00004000ul/* Event queued overflowed */ +#define FS_IN_IGNORED 0x00008000ul/* last inotify event here */ + +#define FS_IN_ISDIR 0x40000000ul/* event occurred against dir */ +#define FS_IN_ONESHOT 0x80000000ul/* only send event once */ + +#define FS_DN_RENAME 0x10000000ul/* file renamed */ +#define FS_DN_MULTISHOT 0x20000000ul/* dnotify multishot */ + +#define FS_EVENT_ON_CHILD 0x08000000ul + +struct fsnotify_group ; +struct fsnotify_event ; + +/* +* Each group much define these ops. +* +* handle_event - main call for a group to handle an fs event +* free_group_priv - called when a group refcnt hits 0 to clean up the private union +*/ +struct fsnotify_ops { +int (*handle_event)(struct fsnotify_group *group,struct fsnotify_event *event); +void (*free_group_priv)(struct fsnotify_group *group); +}; + +/* +* A group is a "thing" that wants to receive notification about filesystem +* events. The mask holds the subset of event types this group cares about. +* refcnt on a group is up to the implementor and at any moment if it goes 0 +* everything will be cleaned up. +*/ +struct fsnotify_group { +struct list_head group_list;/* list of all groups on the system */ +__u32 mask;/* mask of events this group cares about */ +atomic_t refcnt;/* num of processes with a special file open */ +unsigned int group_num;/* the 'name' of the event */ + +const struct fsnotify_ops *ops;/* how this group handles things */ + +unsigned int evicted:1 ;/* has this group been evicted? */ + +/* groups can define private fields here */ +union { + }; +}; + +/* +* all of the information about the original object we want to now send to +* a group. If you want to carry more info from the accessing task to the +* listener this structure is where you need to be adding fields. +*/ +struct fsnotify_event { +spinlock_t lock;/* protection for the associated event_holder and private_list */ +struct inode *to_tell; +/* +* depending on the event type we should have either a path or inode +* we should never have more than one.... +*/ +union { +struct path path; +struct inode *inode; + }; +/* when calling fsnotify tell it if the data is a path or inode */ +#define FSNOTIFY_EVENT_PATH 1 +#define FSNOTIFY_EVENT_INODE 2 +#define FSNOTIFY_EVENT_FILE 3 +int data_type;/* which of the above union we have */ +atomic_t refcnt;/* how many groups still are using/need to send this event */ +__u32 mask;/* the type of access */ +}; + +#ifdef CONFIG_FSNOTIFY + +/* called from the vfs to signal fs events */ +extern void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is); + +/* called from fsnotify interfaces, such as fanotify or dnotify */ +extern void fsnotify_recalc_global_mask (void ); +extern struct fsnotify_group *fsnotify_obtain_group (unsigned int group_num,__u32 mask,const struct fsnotify_ops *ops); +extern void fsnotify_put_group (struct fsnotify_group *group); + +extern void fsnotify_get_event (struct fsnotify_event *event); +extern void fsnotify_put_event (struct fsnotify_event *event); +extern struct fsnotify_event_private_data *fsnotify_get_priv_from_event (struct fsnotify_group *group,struct fsnotify_event *event); + +/* put here because inotify does some weird stuff when destroying watches */ +extern struct fsnotify_event *fsnotify_create_event (struct inode *to_tell,__u32 mask,void *data,int data_is); +#else + +static inline void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is); +{} +#endif /* CONFIG_FSNOTIFY */ + +#endif /* __KERNEL __ */ + +#endif /* _LINUX_FSNOTIFY_BACKEND_H */ --To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
PATCH 3/13 fsnotify: add group priorities
In reply to Eric ParisThis introduces an ordering to fnotify groups. It's most interesting because an HSM would need to run before a typical access notifier. And an access control system would need to run between the two.
---
fs/notify/group.c | 29 ++++++++++++++++++++++------- include/linux/fsnotify_backend.h | 4 +++- 2 files changed, 25 insertions(+), 8 deletions(-)
fs/notify/group.c +22 -7
@@ -49,8 +49,21 @@ void fsnotify_recalc_global_mask(void)static void fsnotify_add_group (struct fsnotify_group *group) { -list_add_rcu (&group->group_list, &fsnotify_groups); +int priority = group->priority; +struct fsnotify_group *group_iter; + group->evicted =0 ; +list_for_each_entry (group_iter, &fsnotify_groups, group_list) { +/* insert in front of this one? */ +if (priority < group_iter->priority) { +/* I used list_add_tail() to insert in front of group_iter... */ +list_add_tail_rcu (&group->group_list, &group_iter->group_list); +return ; + } + } + +/* apparently we need to be the last entry */ +list_add_tail_rcu (&group->group_list, &fsnotify_groups); }static void fsnotify_get_group (struct fsnotify_group *group)
@@ -101,8 +114,8 @@ void fsnotify_put_group(struct fsnotify_group *group)fsnotify_destroy_group (group); } -static struct fsnotify_group *fsnotify_find_group (unsigned int group_num,__u32 mask, -const struct fsnotify_ops *ops) +static struct fsnotify_group *fsnotify_find_group (unsigned int priority,unsigned int group_num, +__u32 mask,const struct fsnotify_ops *ops) {struct fsnotify_group *group_iter;struct fsnotify_group *group =NULL ;
@@ -110,8 +123,9 @@ static struct fsnotify_group *fsnotify_find_group(unsigned int group_num, __u32BUG_ON (!mutex_is_locked (&fsnotify_grp_mutex));list_for_each_entry_rcu (group_iter, &fsnotify_groups, group_list) { -if (group_iter->group_num == group_num) { +if (group_iter->priority == priority) {if ((group_iter->mask == mask) && + (group_iter->group_num == group_num) && (group_iter->ops == ops)) {fsnotify_get_group (group_iter); group = group_iter;
@@ -131,8 +145,8 @@ static struct fsnotify_group *fsnotify_find_group(unsigned int group_num, __u32* the allocation and the initialization, but this is only called when notification * systems make changes, so why make it more complex? */ -struct fsnotify_group *fsnotify_obtain_group (unsigned int group_num,__u32 mask, -const struct fsnotify_ops *ops) +struct fsnotify_group *fsnotify_obtain_group (unsigned int priority,unsigned int group_num, +__u32 mask,const struct fsnotify_ops *ops) {struct fsnotify_group *group, *tgroup;
@@ -142,13 +156,14 @@ struct fsnotify_group *fsnotify_obtain_group(unsigned int group_num, __u32 mask,atomic_set (&group->refcnt,1 ); + group->priority = priority; group->group_num = group_num; group->mask = mask; group->ops = ops;mutex_lock (&fsnotify_grp_mutex); - tgroup =fsnotify_find_group (group_num, mask, ops); + tgroup =fsnotify_find_group (priority, group_num, mask, ops);if (tgroup) {/* group already exists */ mutex_unlock (&fsnotify_grp_mutex);
include/linux/fsnotify_backend.h +3 -1
@@ -76,6 +76,7 @@ struct fsnotify_group {const struct fsnotify_ops *ops;/* how this group handles things */ +unsigned int priority;/* order this group should receive msgs. low first */ unsigned int evicted:1; /* has this group been evicted? *//* groups can define private fields here */
@@ -115,7 +116,8 @@ extern void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is)/* called from fsnotify interfaces, such as fanotify or dnotify */ extern void fsnotify_recalc_global_mask (void ); -extern struct fsnotify_group *fsnotify_obtain_group (unsigned int group_num,__u32 mask,const struct fsnotify_ops *ops); +extern struct fsnotify_group *fsnotify_obtain_group (unsigned int priority,unsigned int group_num, +__u32 mask,const struct fsnotify_ops *ops);extern void fsnotify_put_group (struct fsnotify_group *group);extern void fsnotify_get_event (struct fsnotify_event *event);
PATCH 4/13 fsnotify: add in inode fsnotify markings
In reply to Eric ParisThis patch creates in inode fsnotify markings. dnotify will make use of in inode markings to mark which inodes it wishes to send events for. fanotify will use this to mark which inodes it does not wish to send events for.
---
Documentation/filesystems/fsnotify.txt | 180 ++++++++++++++++++++++++++ fs/inode.c | 9 + fs/notify/Makefile | 2 fs/notify/fsnotify.c | 10 + fs/notify/fsnotify.h | 3 fs/notify/group.c | 24 +++ fs/notify/inode_mark.c | 224 ++++++++++++++++++++++++++++++++ include/linux/fs.h | 5 + include/linux/fsnotify.h | 9 + include/linux/fsnotify_backend.h | 54 ++++++++ 10 files changed, 518 insertions(+), 2 deletions(-) create mode 100644 Documentation/filesystems/fsnotify.txt create mode 100644 fs/notify/inode_mark.c
Documentation/filesystems/fsnotify.txt new file +180 -0
@@ -0,0 +1,180 @@ +fsnotify inode mark locking/lifetime/and refcnting + +struct fsnotify_mark_entry { + __u32 mask; /* mask this mark entry is for */ + /* we hold ref for each i_list and g_list. also one ref for each 'thing' + * in kernel that found and may be using this mark. */ + atomic_t refcnt; /* active things looking at this mark */ + struct inode *inode; /* inode this entry is associated with */ + struct fsnotify_group *group; /* group this mark entry is for */ + struct hlist_node i_list; /* list of mark_entries by inode->i_fsnotify_mark_entries */ + struct list_head g_list; /* list of mark_entries by group->i_fsnotify_mark_entries */ + spinlock_t lock; /* protect group, inode, and killme */ + struct list_head free_i_list; /* tmp list used when freeing this mark */ + struct list_head free_g_list; /* tmp list used when freeing this mark */ + void (*free_mark)(struct fsnotify_mark_entry *entry); /* called on final put+free */ +}; + +REFCNT: +The mark->refcnt tells how many "things" in the kernel currectly are +referencing this object. The object typically will live inside the kernel +with a refcnt of 2, one for each list it is on (i_list, g_list). Any task +which can find this object holding the appropriete locks, can take a reference +and the object itself is guarenteed to survive until the reference is dropped. + +LOCKING: +There are 3 spinlocks involved with fsnotify inode marks and they MUST +be taking in order as follows: + +entry->lock +group->mark_lock +inode->i_lock + +entry->lock protects 2 things, entry->group and entry->inode. You must hold +that lock to dereference either of these things (they could be NULL even with +the lock) + +group->mark_lock protects the mark_entries list anchored inside a given group +and each entry is hooked via the g_list. It also sorta protects the +free_g_list, which when used is anchored by a private list on the stack of the +task which held the group->mark_lock. + +inode->i_lock protects the i_fsnotify_mark_entries list anchored inside a +given inode and each entry is hooked via the i_list. (and sorta the +free_i_list) + + +LIFETIME: +Inode marks survive between when they are added to an inode and when their +refcnt==0. + +The inode mark can be cleared for a number of different reasons including: +- The inode is unlinked for the last time. (fsnotify_inoderemove) +- The inode is being evicted from cache. (fsnotify_inode_delete) +- The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes) +- Something explicitly requests that it be removed. (fsnotify_destroy_mark_by_entry) +- The fsnotify_group associated with the mark is going away and all such marks + need to be cleaned up. (fsnotify_clear_marks_by_group) + +Worst case we are given an inode and need to clean up all the marks on that +inode. We take i_lock and walk the i_fsnotify_mark_entries safely. For each +mark on the list we take a reference (so the mark can't disappear under us). +We remove that mark form the inode's list of marks and we add this mark to a +private list anchored on the stack using i_free_list; At this point we no +longer fear anything finding the mark using the inode's list of marks. + +We can safely and locklessly run the private list on the stack of everything +we just unattached from the original inode. For each mark on the private list +we grab the mark-> and can thus dereference mark->group and mark->inode. If +we see the group and inode are not NULL we take those locks. Now holding all +3 locks we can completely remove the mark from other tasks finding it in the +future. Remember, 10 things might already be referencing this mark, but they +better be holding a ref. We drop our reference we took before we unhooked it +from the inode. When the ref hits 0 we can free the mark. + +Very similarly for freeing by group, except we use free_g_list. + +This has the very interesting property of being able to run concurrently with +any (or all) other directions. Lets walk through what happens with every +combination trying to simultaneously mark this entry for destruction. + +(A) finds this event by some means and takes a reference. (this could be any +means including in the case of inotify through an idr, which is known to be +safe since the idr entry itself holds a reference) +(B) finds this event by some means and takes a reference. + +At this point. + refcnt == 4 + i_list -> inode + inode -> inode + g_list -> group + group -> group + free_i_list -> NULL + free_g_list -> NULL + +(C) comes in and tries to free all of the fsnotify_mark attached to an inode. +---- C will take the i_lock and walk the i_fsnotify_mark entries list calling + list_del_init() on i_list, adding the entry to it's private list via + free_i_list, and taking a reference. C releases the i_lock. Start + walking the private list and block on the entry->lock (held by A + below) + +At this point. + refcnt == 5 + i_list -> NULL + inode -> inode + g_list -> group + group -> group + free_i_list -> private list on (C) stack + free_g_list -> NULL + +(D) comes in and tries to free all of the marks attached to the same inode. +---- D will take the i_lock and won't find this entry on the list and does + nothing. (this is the end of D) + +(E) comes along and wants to free all of the marks in the group. +---- E take the group->mark_lock walk the group->mark_entry. grab a + reference to the mark, list_del_init the g_list. Add the mark to the + free_g_list. Release the group->mark_lock. Now start walking the new + private list and block in entry->lock. + +**This is actually the point where the kernel cannot find this mark ** + +At this point. + refcnt == 6 + i_list -> NULL + inode -> inode + g_list -> NULL + group -> group + free_i_list -> private list on (C) stack + free_g_list -> private list on (E) stack + +(A) finally decides it wants to kill this entry for some reason. +---- A will take the entry->lock. It will check if mark->group is non-NULL + and if so takes mark->group->mark_lock (it may have blocked here on D + above). Check the ->inode and if set take mark->inode->i_lock (again + we may have been blocking on C). We now own all the locks. So + list_del_init on i_list and g_list. set ->inode and ->group = NULL + drop those refs. Unlock i_lock, mark_lock, and entry->lock. Drop our + reference. (this is the end of A) + +**Diff sequence of events this could be the point where the object is no +longer able to be found** + +At this point. + refcnt == 3 + i_list -> NUL + inode -> NULL + g_list -> NUL + group -> NULL + free_i_list -> private list on (C) stack + free_g_list -> private list on (E) stack + +(D) happens to be the one to win the entry->lock. +---- D sees that ->inode and ->group and NULL so it just doesn't bother to + grab those locks (if they are NULL we know this mark if off the + relevant lists). So D doesn't do anything. It sees that the mark is + off the lists so all it need to do is drop it's reference. + +At this point. + refcnt == 2 + i_list -> NUL + inode -> NULL + g_list -> NUL + group -> NULL + free_i_list -> private list on (C) stack + free_g_list -> undefined + +(C) does the same thing as B and the mark looks like: + +At this point. + refcnt == 1 + i_list -> NUL + inode -> NULL + g_list -> NUL + group -> NULL + free_i_list -> undefined + free_g_list -> undefined + +(B) is the only thing left with a reference when it drops that reference the +object will get freed.
fs/inode.c +9 -0
@@ -22,6 +22,7 @@#include <linux/cdev.h> #include <linux/bootmem.h> #include <linux/inotify.h> +#include <linux/fsnotify.h> #include <linux/mount.h> #include <linux/async.h>
@@ -189,6 +190,10 @@ struct inode *inode_init_always(struct super_block *sb, struct inode *inode) inode->i_private =NULL ; inode->i_mapping = mapping; +#ifdef CONFIG_FSNOTIFY + inode->i_fsnotify_mask =0 ; +#endif +return inode; out_free_security:
@@ -220,6 +225,7 @@ void destroy_inode(struct inode *inode) {BUG_ON (inode_has_buffers (inode));security_inode_free (inode); +fsnotify_inode_delete (inode);if (inode->i_sb->s_op->destroy_inode) inode->i_sb->s_op->destroy_inode (inode);else
@@ -251,6 +257,9 @@ void inode_init_once(struct inode *inode)INIT_LIST_HEAD (&inode->inotify_watches);mutex_init (&inode->inotify_mutex);#endif +#ifdef CONFIG_FSNOTIFY +INIT_HLIST_HEAD (&inode->i_fsnotify_mark_entries); +#endif }EXPORT_SYMBOL (inode_init_once);
fs/notify/Makefile +1 -1
@@ -1,4 +1,4 @@ -obj-$(CONFIG_FSNOTIFY) += fsnotify.o notification.o group.o +obj-$(CONFIG_FSNOTIFY) += fsnotify.o notification.o group.o inode_mark.o obj-y += dnotify/ obj-y += inotify/
fs/notify/fsnotify.c +10 -0
@@ -25,6 +25,12 @@#include <linux/fsnotify_backend.h> #include "fsnotify.h" +void __fsnotify_inode_delete (struct inode *inode) +{ +fsnotify_clear_marks_by_inode (inode); +} +EXPORT_SYMBOL_GPL (__fsnotify_inode_delete); +/* * This is the main call to fsnotify. The VFS calls into hook specific functions * in linux/fsnotify.h. Those functions then in turn call here. Here will call
@@ -43,6 +49,8 @@ void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is)if (!(mask & fsnotify_mask))return ; +if (!(mask & to_tell->i_fsnotify_mask)) +return ;/* * SRCU!! the groups list is very very much read only and the path is * very hot. The VAST majority of events are not going to need to do
@@ -51,6 +59,8 @@ void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is) idx =srcu_read_lock (&fsnotify_grp_srcu);list_for_each_entry_rcu (group, &fsnotify_groups, group_list) {if (mask & group->mask) { +if (!group->ops->should_send_event (group, to_tell, mask)) +continue ;if (!event) { event =fsnotify_create_event (to_tell, mask, data, data_is);/* shit, we OOM'd and now we can't tell, maybe
fs/notify/fsnotify.h +3 -0
@@ -14,4 +14,7 @@extern struct srcu_struct fsnotify_grp_srcu;extern struct list_head fsnotify_groups;extern __u32 fsnotify_mask; + +extern void fsnotify_clear_marks_by_group (struct fsnotify_group *group); +extern void fsnotify_clear_marks_by_inode (struct inode *inode);#endif /* _LINUX_FSNOTIFY_PRIVATE_H */
fs/notify/group.c +24 -0
@@ -47,6 +47,24 @@ void fsnotify_recalc_global_mask(void) fsnotify_mask = mask; } +void fsnotify_recalc_group_mask (struct fsnotify_group *group) +{ +__u32 mask =0 ; +__u32 old_mask = group->mask; +struct fsnotify_mark_entry *entry; + +spin_lock (&group->mark_lock); +list_for_each_entry (entry, &group->mark_entries, g_list) { + mask |= entry->mask; + } +spin_unlock (&group->mark_lock); + + group->mask = mask; + +if (old_mask != mask) +fsnotify_recalc_global_mask (); +} +static void fsnotify_add_group (struct fsnotify_group *group) {int priority = group->priority;
@@ -73,6 +91,9 @@ static void fsnotify_get_group(struct fsnotify_group *group)static void fsnotify_destroy_group (struct fsnotify_group *group) { +/* clear all inode mark entries for this group */ +fsnotify_clear_marks_by_group (group); +if (group->ops->free_group_priv) group->ops->free_group_priv (group);
@@ -160,6 +181,9 @@ struct fsnotify_group *fsnotify_obtain_group(unsigned int priority, unsigned int group->group_num = group_num; group->mask = mask; +spin_lock_init (&group->mark_lock); +INIT_LIST_HEAD (&group->mark_entries); + group->ops = ops;mutex_lock (&fsnotify_grp_mutex);
fs/notify/inode_mark.c new file +224 -0
224 lines not on this page · raw message
include/linux/fs.h +5 -0
@@ -696,6 +696,11 @@ struct inode {__u32 i_generation; +#ifdef CONFIG_FSNOTIFY +__u32 i_fsnotify_mask;/* all events this inode cares about */ +struct hlist_head i_fsnotify_mark_entries;/* fsnotify mark entries */ +#endif +#ifdef CONFIG_DNOTIFY unsigned long i_dnotify_mask;/* Directory notify events */ struct dnotify_struct *i_dnotify;/* for directory notifications */
include/linux/fsnotify.h +9 -0
@@ -36,6 +36,14 @@ static inline void fsnotify_d_move(struct dentry *entry) }/* +* fsnotify_inode_delete - and inode is being evicted from cache, clean up is needed +*/ +static inline void fsnotify_inode_delete (struct inode *inode) +{ +__fsnotify_inode_delete (inode); +} + +/* * fsnotify_inoderemove - an inode is going away */ static inline void fsnotify_inoderemove (struct inode *inode)
@@ -44,6 +52,7 @@ static inline void fsnotify_inoderemove(struct inode *inode)inotify_inode_is_dead (inode);fsnotify (inode,FS_DELETE_SELF , inode,FSNOTIFY_EVENT_INODE ); +__fsnotify_inode_delete (inode); }/*
include/linux/fsnotify_backend.h +53 -1
107 lines not on this page · raw message
Al Viro
maintainerRe: [PATCH 04/13] fsnotify: add in inode fsnotify markings
Quoted 3 lines · Eric Paris
This patch creates in inode fsnotify markings. dnotify will make use of in inode markings to mark which inodes it wishes to send events for. fanotify will use this to mark which inodes it does not wish to send events for.
Hrm... What happens if fsnotify_clear_marks_by_inode() is called, gets the only mark we had for a group, removes it from all lists, drops all locks and races with the final fsnotify_put_group()? Said put_group sees nothing whatsoever on the per-group mark list (the only mark we used to have there is already evicted) and the group is cheerfully freed. Then fsnotify_destroy_mark_by_entry() calls
group->ops->freeing_mark(entry, group)
with group already freed... Gets really interesting in case of inotify - there ->freeing_mark() leads to blocking allocations and really touches group in non-trivial ways.
Note that for inotify ->freeing_mark() will happily add notification to group's queue and that's another race of the same nature - even if group itself isn't yet freed, that final put_group() might bloody well get past flushing the queue.
I really don't like the idea of having marks contributing to group refcount - that would have solved this one, but we'd get potential leaks from hell ;-/ Maybe a separate counter controlling only actual freeing of group?
Al Viro
maintainerRe: [PATCH 04/13] fsnotify: add in inode fsnotify markings
Quoted 8 lines · Al Viro
Note that for inotify ->freeing_mark() will happily add notification to group's queue and that's another race of the same nature - even if group itself isn't yet freed, that final
put_group()might bloody well get past flushing the queue.I really don't like the idea of having marks contributing to group refcount - that would have solved this one, but we'd get potential leaks from hell ;-/ Maybe a separate counter controlling only actual freeing of group?
Actually, let's make it
number of entries with this ->group + 1 if group is not past the eviction of marks in destroy_group
Then destroy_group would evict marks, decrement that sucker and if it's 0 - call rest_of_destroy_group(). And destroy_mark_by_entry() would do the same decrement-and-call-if-0. With everything starting at queue flush done in rest_of_destroy_group().
AFAICS, that would work without creating leaks. Comments?
PATCH 5/13 fsnotify: parent event notification
In reply to Eric Parisinotify and dnotify both use a similar parent notification mechanism. We add a generic parent notification mechanism to fsnotify for both of these to use. This new machanism also adds the dentry flag optimization which exists for inotify to dnotify.
---
fs/notify/inode_mark.c | 48 +++++++++++++++++++++++++++++++++ include/linux/dcache.h | 3 +- include/linux/fsnotify.h | 56 ++++++++++++++++++++++++++++++++++++-- include/linux/fsnotify_backend.h | 43 +++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 4 deletions(-)
fs/notify/inode_mark.c +48 -0
@@ -41,6 +41,48 @@ void fsnotify_put_mark(struct fsnotify_mark_entry *entry) }/* +* Given an inode, first check if we care what happens to out children. Inotify +* and dnotify both tell their parents about events. If we care about any event +* on a child we run all of our children and set a dentry flag saying that the +* parent cares. Thus when an event happens on a child it can quickly tell if +* if there is a need to find a parent and send the event to the parent. +*/ +static inline void fsnotify_update_dentry_child_flags (struct inode *inode) +{ +struct dentry *alias; +int watched; + +if (!S_ISDIR (inode->i_mode)) +return ; + +/* determine if the children should tell inode about their events */ + watched =fsnotify_inode_watches_children (inode); + +spin_lock (&dcache_lock); +/* run all of the dentries associated with this inode. Since this is a +* directory, there damn well better only be one item on this list */ +list_for_each_entry (alias, &inode->i_dentry, d_alias) { +struct dentry *child; + +/* run all of the children of the original inode and fix their +* d_flags to indicate parental interest (their parent is the +* original inode) */ +list_for_each_entry (child, &alias->d_subdirs, d_u.d_child) { +if (!child->d_inode) +continue ; + +spin_lock (&child->d_lock); +if (watched) + child->d_flags |=DCACHE_FSNOTIFY_PARENT_WATCHED ; +else + child->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED ; +spin_unlock (&child->d_lock); + } + } +spin_unlock (&dcache_lock); +} + +/* * recalculate the mask of events relevant to a given inode locked. */ static void fsnotify_recalc_inode_mask_locked (struct inode *inode)
@@ -65,6 +107,8 @@ void fsnotify_recalc_inode_mask(struct inode *inode)spin_lock (&inode->i_lock);fsnotify_recalc_inode_mask_locked (inode);spin_unlock (&inode->i_lock); + +fsnotify_update_dentry_child_flags (inode); }/*
@@ -113,6 +157,8 @@ void fsnotify_destroy_mark_by_entry(struct fsnotify_mark_entry *entry)spin_unlock (&entry->lock); group->ops->freeing_mark (entry, group); + +fsnotify_update_dentry_child_flags (inode); }void fsnotify_clear_marks_by_group (struct fsnotify_group *group)
@@ -218,6 +264,8 @@ int fsnotify_add_mark(struct fsnotify_mark_entry *entry, struct fsnotify_group *if (lentry) { ret = -EEXIST ;fsnotify_put_mark (lentry); + }else { +fsnotify_update_dentry_child_flags (inode); }return ret;
include/linux/dcache.h +2 -1
@@ -180,7 +180,8 @@ d_iput: no no no yes#define DCACHE_REFERENCED 0x0008/* Recently used, don't discard. */ #define DCACHE_UNHASHED 0x0010 -#define DCACHE_INOTIFY_PARENT_WATCHED 0x0020/* Parent inode is watched */ +#define DCACHE_INOTIFY_PARENT_WATCHED 0x0020/* Parent inode is watched by inotify */ +#define DCACHE_FSNOTIFY_PARENT_WATCHED 0x0040/* Parent inode is watched by some fsnotify listener */ #define DCACHE_COOKIE 0x0040/* For use by dcookie subsystem */
include/linux/fsnotify.h +53 -3
111 lines not on this page · raw message
include/linux/fsnotify_backend.h +43 -0
63 lines not on this page · raw message
PATCH 6/13 dnotify: reimplement dnotify using fsnotify
In reply to Eric ParisReimplement dnotify using fsnotify.
---
MAINTAINERS | 2 fs/notify/dnotify/Kconfig | 1 fs/notify/dnotify/dnotify.c | 405 +++++++++++++++++++++++++++++--------- include/linux/dnotify.h | 29 +-- include/linux/fs.h | 5 include/linux/fsnotify.h | 70 ++----- include/linux/fsnotify_backend.h | 3 7 files changed, 340 insertions(+), 175 deletions(-)
MAINTAINERS +2 -0
@@ -1424,6 +1424,8 @@ S: Orphan DIRECTORY NOTIFICATION (DNOTIFY) P: Stephen Rothwell M: [email protected] +P: Eric Paris +M: [email protected] L: [email protected] S: Supported
fs/notify/dnotify/Kconfig +1 -0
@@ -1,5 +1,6 @@ config DNOTIFY bool "Dnotify support" + depends on FSNOTIFY default y help Dnotify is a directory-based per-fd file change notification system
fs/notify/dnotify/dnotify.c +304 -101
479 lines not on this page · raw message
include/linux/dnotify.h +8 -21
@@ -10,7 +10,7 @@struct dnotify_struct {struct dnotify_struct * dn_next; -unsigned long dn_mask; +__u64 dn_mask;int dn_fd;struct file * dn_filp;fl_owner_t dn_owner;
@@ -21,23 +21,18 @@ struct dnotify_struct {#ifdef CONFIG_DNOTIFY -extern void __inode_dir_notify (struct inode *,unsigned long ); +#define ALL_DNOTIFY_EVENTS (FS_DELETE | FS_DELETE_CHILD |\ + FS_MODIFY | FS_MODIFY_CHILD |\ + FS_ACCESS | FS_ACCESS_CHILD |\ + FS_ATTRIB | FS_ATTRIB_CHILD |\ + FS_CREATE | FS_DN_RENAME |\ + FS_MOVED_FROM | FS_MOVED_TO) +extern void dnotify_flush (struct file *,fl_owner_t );extern int fcntl_dirnotify (int ,struct file *,unsigned long ); -extern void dnotify_parent (struct dentry *,unsigned long ); - -static inline void inode_dir_notify (struct inode *inode,unsigned long event) -{ -if (inode->i_dnotify_mask & (event)) -__inode_dir_notify (inode, event); -}#else -static inline void __inode_dir_notify (struct inode *inode,unsigned long event) -{ -} -static inline void dnotify_flush (struct file *filp,fl_owner_t id) { }
@@ -47,14 +42,6 @@ static inline int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)return -EINVAL ; } -static inline void dnotify_parent (struct dentry *dentry,unsigned long event) -{ -} - -static inline void inode_dir_notify (struct inode *inode,unsigned long event) -{ -} -#endif /* CONFIG_DNOTIFY */ #endif /* __KERNEL __ */
include/linux/fs.h +0 -5
@@ -701,11 +701,6 @@ struct inode {struct hlist_head i_fsnotify_mark_entries;/* fsnotify mark entries */ #endif -#ifdef CONFIG_DNOTIFY -unsigned long i_dnotify_mask;/* Directory notify events */ -struct dnotify_struct *i_dnotify;/* for directory notifications */ -#endif -#ifdef CONFIG_INOTIFY struct list_head inotify_watches;/* watches on this inode */ struct mutex inotify_mutex;/* protects the watches list */
include/linux/fsnotify.h +22 -48
126 lines not on this page · raw message
include/linux/fsnotify_backend.h +3 -0
@@ -56,6 +56,9 @@ FS_MOVED_FROM | FS_MOVED_TO | FS_CREATE |\ FS_DELETE) +/* listeners that hard code group numbers near the top */ +#define DNOTIFY_GROUP_NUM UINT_MAX +struct fsnotify_group ;struct fsnotify_event ;struct fsnotify_mark_entry ;
PATCH 7/13 fsnotify: generic notification queue and waitq
In reply to Eric Parisinotify needs to do asyc notification in which event information is stored on a queue until the listener is ready to receive it. This patch implements a generic notification queue for inotify (and later fanotify) to store events to be sent at a later time.
---
fs/notify/fsnotify.h | 2 fs/notify/group.c | 9 ++ fs/notify/notification.c | 209 ++++++++++++++++++++++++++++++++++++-- include/linux/fsnotify_backend.h | 35 ++++++ 4 files changed, 244 insertions(+), 11 deletions(-)
fs/notify/fsnotify.h +2 -0
@@ -15,6 +15,8 @@ extern struct srcu_struct fsnotify_grp_srcu;extern struct list_head fsnotify_groups;extern __u32 fsnotify_mask; +extern void fsnotify_flush_notif (struct fsnotify_group *group); +extern void fsnotify_clear_marks_by_group (struct fsnotify_group *group);extern void fsnotify_clear_marks_by_inode (struct inode *inode);#endif /* _LINUX_FSNOTIFY_PRIVATE_H */
fs/notify/group.c +9 -0
@@ -94,6 +94,9 @@ static void fsnotify_destroy_group(struct fsnotify_group *group)/* clear all inode mark entries for this group */ fsnotify_clear_marks_by_group (group); +/* clear the notification queue of all events */ +fsnotify_flush_notif (group); +if (group->ops->free_group_priv) group->ops->free_group_priv (group);
@@ -181,6 +184,12 @@ struct fsnotify_group *fsnotify_obtain_group(unsigned int priority, unsigned int group->group_num = group_num; group->mask = mask; +mutex_init (&group->notification_mutex); +INIT_LIST_HEAD (&group->notification_list); +init_waitqueue_head (&group->notification_waitq); + group->q_len =0 ; + group->max_events =UINT_MAX ; +spin_lock_init (&group->mark_lock);INIT_LIST_HEAD (&group->mark_entries);
fs/notify/notification.c +199 -10
244 lines not on this page · raw message
include/linux/fsnotify_backend.h +34 -1
@@ -95,6 +95,13 @@ struct fsnotify_group {const struct fsnotify_ops *ops;/* how this group handles things */ +/* needed to send notification to userspace */ +struct mutex notification_mutex;/* protect the notification_list */ +struct list_head notification_list;/* list of event_holder this group needs to send to userspace */ +wait_queue_head_t notification_waitq;/* read() on the notification file blocks on this waitq */ +unsigned int q_len;/* events on the queue */ +unsigned int max_events;/* maximum events allowed on the list */ +/* stores all fastapth entries assoc with this group so they can be cleaned on unregister */ spinlock_t mark_lock;/* protect mark_entries list */ struct list_head mark_entries;/* all inode mark entries for this group */
@@ -108,11 +115,32 @@ struct fsnotify_group { };/* +* A single event can be queued in multiple group->notification_lists. +* +* each group->notification_list will point to an event_holder which in turns points +* to the actual event that needs to be sent to userspace. +* +* Seemed cheaper to create a refcnt'd event and a small holder for every group +* than create a different event for every group +* +*/ +struct fsnotify_event_holder { +struct fsnotify_event *event; +struct list_head event_list; +}; + +/* * all of the information about the original object we want to now send to * a group. If you want to carry more info from the accessing task to the * listener this structure is where you need to be adding fields. */ struct fsnotify_event { +/* +* If we create an event we are also likely going to need a holder +* to link to a group. So embed one holder in the event. Means only +* one allocation for the common case where we only have one group +*/ +struct fsnotify_event_holder holder;spinlock_t lock;/* protection for the associated event_holder and private_list */ struct inode *to_tell;/*
@@ -124,6 +152,7 @@ struct fsnotify_event {struct inode *inode; };/* when calling fsnotify tell it if the data is a path or inode */ +#define FSNOTIFY_EVENT_NONE 0#define FSNOTIFY_EVENT_PATH 1#define FSNOTIFY_EVENT_INODE 2#define FSNOTIFY_EVENT_FILE 3
@@ -203,7 +232,11 @@ extern void fsnotify_put_group(struct fsnotify_group *group);extern void fsnotify_get_event (struct fsnotify_event *event);extern void fsnotify_put_event (struct fsnotify_event *event); -extern struct fsnotify_event_private_data *fsnotify_get_priv_from_event (struct fsnotify_group *group,struct fsnotify_event *event); + +extern int fsnotify_add_notif_event (struct fsnotify_group *group,struct fsnotify_event *event); +extern int fsnotify_check_notif_queue (struct fsnotify_group *group); +extern struct fsnotify_event *fsnotify_peek_notif_event (struct fsnotify_group *group); +extern struct fsnotify_event *fsnotify_remove_notif_event (struct fsnotify_group *group);/* functions used to manipulate the marks attached to inodes */ extern void fsnotify_recalc_inode_mask (struct inode *inode);
PATCH 8/13 fsnotify: include pathnames with entries when possible
In reply to Eric ParisWhen inotify wants to send events to a directory about a child it includes the name of the original file. This patch collects that filename and makes it available for notification.
---
fs/notify/fsnotify.c | 4 ++-- fs/notify/fsnotify.h | 4 ++++ fs/notify/notification.c | 19 ++++++++++++++++++- include/linux/fsnotify.h | 30 +++++++++++++++--------------- include/linux/fsnotify_backend.h | 9 ++++++--- 5 files changed, 45 insertions(+), 21 deletions(-)
fs/notify/fsnotify.c +2 -2
@@ -37,7 +37,7 @@ EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);* out to all of the registered fsnotify_group. Those groups can then use the * notification event in whatever means they feel necessary. */ -void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is) +void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is,const char *file_name) {struct fsnotify_group *group;struct fsnotify_event *event =NULL ;
@@ -62,7 +62,7 @@ void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is)if (!group->ops->should_send_event (group, to_tell, mask))continue ;if (!event) { - event =fsnotify_create_event (to_tell, mask, data, data_is); + event =fsnotify_create_event (to_tell, mask, data, data_is, file_name);/* shit, we OOM'd and now we can't tell, maybe * someday someone else will want to do something * here */
fs/notify/fsnotify.h +4 -0
@@ -19,4 +19,8 @@ extern void fsnotify_flush_notif(struct fsnotify_group *group);extern void fsnotify_clear_marks_by_group (struct fsnotify_group *group);extern void fsnotify_clear_marks_by_inode (struct inode *inode); + +extern struct fsnotify_event_holder *fsnotify_alloc_event_holder (void ); +extern void fsnotify_destroy_event_holder (struct fsnotify_event_holder *holder); +#endif /* _LINUX_FSNOTIFY_PRIVATE_H */
fs/notify/notification.c +18 -1
@@ -62,6 +62,8 @@ void fsnotify_put_event(struct fsnotify_event *event)if (atomic_dec_and_test (&event->refcnt)) {if (event->data_type ==FSNOTIFY_EVENT_PATH )path_put (&event->path); + +kfree (event->file_name);kmem_cache_free (event_kmem_cache, event); } }
@@ -239,6 +241,9 @@ static void initialize_event(struct fsnotify_event *event) event->data_type =FSNOTIFY_EVENT_NONE ; event->to_tell =NULL ; + + event->file_name =NULL ; + event->name_len =0 ; }/*
@@ -249,7 +254,7 @@ static void initialize_event(struct fsnotify_event *event)* @data pointer to the object which was actually affected * @data_type flag indication if the data is a file, path, inode, nothing... */ -struct fsnotify_event *fsnotify_create_event (struct inode *to_tell,__u32 mask,void *data,int data_type) +struct fsnotify_event *fsnotify_create_event (struct inode *to_tell,__u32 mask,void *data,int data_type,const char *name) {struct fsnotify_event *event;
@@ -258,6 +263,15 @@ struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask,return NULL ;initialize_event (event); + +if (name) { + event->file_name =kstrdup (name,GFP_KERNEL ); +if (!event->file_name) { +kmem_cache_free (event_kmem_cache, event); +return NULL ; + } + event->name_len =strlen (event->file_name); + } event->to_tell = to_tell;switch (data_type) {
@@ -283,6 +297,9 @@ struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, event->data_type =FSNOTIFY_EVENT_INODE ;break ;default : + event->path.dentry =NULL ; + event->path.mnt =NULL ; + event->inode =NULL ;BUG (); };
include/linux/fsnotify.h +15 -15
@@ -54,7 +54,7 @@ static inline void fsnotify_parent(struct dentry *dentry, __u32 mask)spin_unlock (&dentry->d_lock);if (send) { -fsnotify (p_inode, mask, dentry->d_inode,FSNOTIFY_EVENT_INODE ); +fsnotify (p_inode, mask, dentry->d_inode,FSNOTIFY_EVENT_INODE , dentry->d_name.name);dput (parent); } }
@@ -92,7 +92,7 @@ static inline void fsnotify_inoderemove(struct inode *inode)inotify_inode_queue_event (inode,IN_DELETE_SELF ,0 ,NULL ,NULL );inotify_inode_is_dead (inode); -fsnotify (inode,FS_DELETE_SELF , inode,FSNOTIFY_EVENT_INODE ); +fsnotify (inode,FS_DELETE_SELF , inode,FSNOTIFY_EVENT_INODE ,NULL );__fsnotify_inode_delete (inode); }
@@ -126,15 +126,15 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,inotify_inode_queue_event (new_dir,IN_MOVED_TO |isdir, cookie, new_name, source); -fsnotify (old_dir, old_dir_mask, old_dir,FSNOTIFY_EVENT_INODE ); -fsnotify (new_dir, new_dir_mask, new_dir,FSNOTIFY_EVENT_INODE ); +fsnotify (old_dir, old_dir_mask, old_dir,FSNOTIFY_EVENT_INODE , old_name); +fsnotify (new_dir, new_dir_mask, new_dir,FSNOTIFY_EVENT_INODE , new_name);if (target)fsnotify_inoderemove (target);if (source) {inotify_inode_queue_event (source,IN_MOVE_SELF ,0 ,NULL ,NULL ); -fsnotify (source,FS_MOVE_SELF , moved->d_inode,FSNOTIFY_EVENT_INODE ); +fsnotify (source,FS_MOVE_SELF , moved->d_inode,FSNOTIFY_EVENT_INODE ,NULL ); }audit_inode_child (new_name, moved, new_dir); }
@@ -158,7 +158,7 @@ static inline void fsnotify_link_count(struct inode *inode) {inotify_inode_queue_event (inode,IN_ATTRIB ,0 ,NULL ,NULL ); -fsnotify (inode,FS_ATTRIB , inode,FSNOTIFY_EVENT_INODE ); +fsnotify (inode,FS_ATTRIB , inode,FSNOTIFY_EVENT_INODE ,NULL ); }/*
@@ -170,7 +170,7 @@ static inline void fsnotify_create(struct inode *inode, struct dentry *dentry) dentry->d_inode);audit_inode_child (dentry->d_name.name, dentry, inode); -fsnotify (inode,FS_CREATE , dentry->d_inode,FSNOTIFY_EVENT_INODE ); +fsnotify (inode,FS_CREATE , dentry->d_inode,FSNOTIFY_EVENT_INODE , dentry->d_name.name); }/*
@@ -185,7 +185,7 @@ static inline void fsnotify_link(struct inode *dir, struct inode *inode, structfsnotify_link_count (inode);audit_inode_child (new_dentry->d_name.name, new_dentry, dir); -fsnotify (dir,FS_CREATE , inode,FSNOTIFY_EVENT_INODE ); +fsnotify (dir,FS_CREATE , inode,FSNOTIFY_EVENT_INODE , new_dentry->d_name.name); }/*
@@ -197,7 +197,7 @@ static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry) dentry->d_name.name, dentry->d_inode);audit_inode_child (dentry->d_name.name, dentry, inode); -fsnotify (inode,FS_CREATE |FS_IN_ISDIR , dentry->d_inode,FSNOTIFY_EVENT_INODE ); +fsnotify (inode,FS_CREATE |FS_IN_ISDIR , dentry->d_inode,FSNOTIFY_EVENT_INODE , dentry->d_name.name); }/*
@@ -215,7 +215,7 @@ static inline void fsnotify_access(struct dentry *dentry)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ); +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ); }/*
@@ -233,7 +233,7 @@ static inline void fsnotify_modify(struct dentry *dentry)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ); +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ); }/*
@@ -251,7 +251,7 @@ static inline void fsnotify_open(struct dentry *dentry)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ); +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ); }/*
@@ -272,7 +272,7 @@ static inline void fsnotify_close(struct file *file)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask); -fsnotify (inode, mask, file,FSNOTIFY_EVENT_FILE ); +fsnotify (inode, mask, file,FSNOTIFY_EVENT_FILE ,NULL ); }/*
@@ -290,7 +290,7 @@ static inline void fsnotify_xattr(struct dentry *dentry)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ); +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ); }/*
@@ -328,7 +328,7 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid) dentry->d_name.name);fsnotify_parent (dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ); +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ); } }
include/linux/fsnotify_backend.h +6 -3
@@ -159,6 +159,9 @@ struct fsnotify_event {int data_type;/* which of the above union we have */ atomic_t refcnt;/* how many groups still are using/need to send this event */ __u32 mask;/* the type of access */ + +char *file_name; +size_t name_len; };/*
@@ -189,7 +192,7 @@ struct fsnotify_mark_entry {#ifdef CONFIG_FSNOTIFY /* called from the vfs to signal fs events */ -extern void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is); +extern void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is,const char *name);extern void __fsnotify_inode_delete (struct inode *inode);static inline int fsnotify_inode_watches_children (struct inode *inode)
@@ -248,11 +251,11 @@ extern void fsnotify_get_mark(struct fsnotify_mark_entry *entry);extern void fsnotify_put_mark (struct fsnotify_mark_entry *entry);/* put here because inotify does some weird stuff when destroying watches */ -extern struct fsnotify_event *fsnotify_create_event (struct inode *to_tell,__u32 mask,void *data,int data_is); +extern struct fsnotify_event *fsnotify_create_event (struct inode *to_tell,__u32 mask,void *data,int data_is,const char *name);#else -static inline void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is); +static inline void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is,const char *name); {}static inline void __fsnotify_inode_delete (struct inode *inode)
PATCH 9/13 fsnotify: add correlations between events
In reply to Eric Parisinotify sends userspace a correlation between events when they are related (aka when dentries are moved). This adds that same support for all fsnotify events.
---
fs/notify/fsnotify.c | 4 ++-- fs/notify/notification.c | 15 ++++++++++++++- include/linux/fsnotify.h | 37 +++++++++++++++++++------------------ include/linux/fsnotify_backend.h | 15 ++++++++++++--- 4 files changed, 47 insertions(+), 24 deletions(-)
fs/notify/fsnotify.c +2 -2
@@ -37,7 +37,7 @@ EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);* out to all of the registered fsnotify_group. Those groups can then use the * notification event in whatever means they feel necessary. */ -void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is,const char *file_name) +void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is,const char *file_name,u32 cookie) {struct fsnotify_group *group;struct fsnotify_event *event =NULL ;
@@ -62,7 +62,7 @@ void fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is, constif (!group->ops->should_send_event (group, to_tell, mask))continue ;if (!event) { - event =fsnotify_create_event (to_tell, mask, data, data_is, file_name); + event =fsnotify_create_event (to_tell, mask, data, data_is, file_name, cookie);/* shit, we OOM'd and now we can't tell, maybe * someday someone else will want to do something * here */
fs/notify/notification.c +14 -1
@@ -20,6 +20,7 @@#include <linux/init.h> #include <linux/kernel.h> #include <linux/list.h> +#include <linux/module.h> #include <linux/mount.h> #include <linux/mutex.h> #include <linux/namei.h>
@@ -41,6 +42,13 @@ static struct kmem_cache *event_holder_kmem_cache;* get set to 0 so it will never get 'freed' */ static struct fsnotify_event q_overflow_event; +static atomic_t fsnotify_sync_cookie =ATOMIC_INIT (0 ); + +u32 fsnotify_get_cookie (void ) +{ +return atomic_inc_return (&fsnotify_sync_cookie); +} +EXPORT_SYMBOL_GPL (fsnotify_get_cookie);/* return 1 if something is available, return 0 otherwise */ int fsnotify_check_notif_queue (struct fsnotify_group *group)
@@ -244,6 +252,8 @@ static void initialize_event(struct fsnotify_event *event) event->file_name =NULL ; event->name_len =0 ; + + event->sync_cookie =0 ; }/*
@@ -254,7 +264,8 @@ static void initialize_event(struct fsnotify_event *event)* @data pointer to the object which was actually affected * @data_type flag indication if the data is a file, path, inode, nothing... */ -struct fsnotify_event *fsnotify_create_event (struct inode *to_tell,__u32 mask,void *data,int data_type,const char *name) +struct fsnotify_event *fsnotify_create_event (struct inode *to_tell,__u32 mask,void *data, +int data_type,const char *name,u32 cookie) {struct fsnotify_event *event;
@@ -272,6 +283,8 @@ struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, } event->name_len =strlen (event->file_name); } + + event->sync_cookie = cookie; event->to_tell = to_tell;switch (data_type) {
include/linux/fsnotify.h +19 -18
@@ -54,7 +54,7 @@ static inline void fsnotify_parent(struct dentry *dentry, __u32 mask)spin_unlock (&dentry->d_lock);if (send) { -fsnotify (p_inode, mask, dentry->d_inode,FSNOTIFY_EVENT_INODE , dentry->d_name.name); +fsnotify (p_inode, mask, dentry->d_inode,FSNOTIFY_EVENT_INODE , dentry->d_name.name,0 );dput (parent); } }
@@ -92,7 +92,7 @@ static inline void fsnotify_inoderemove(struct inode *inode)inotify_inode_queue_event (inode,IN_DELETE_SELF ,0 ,NULL ,NULL );inotify_inode_is_dead (inode); -fsnotify (inode,FS_DELETE_SELF , inode,FSNOTIFY_EVENT_INODE ,NULL ); +fsnotify (inode,FS_DELETE_SELF , inode,FSNOTIFY_EVENT_INODE ,NULL ,0 );__fsnotify_inode_delete (inode); }
@@ -104,7 +104,8 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, int isdir, struct inode *target, struct dentry *moved) { struct inode *source = moved->d_inode; -u32 cookie =inotify_get_cookie (); +u32 in_cookie =inotify_get_cookie (); +u32 fs_cookie =fsnotify_get_cookie ();__u32 old_dir_mask =0 ;__u32 new_dir_mask =0 ;
@@ -121,20 +122,20 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, old_dir_mask |=FS_MOVED_FROM ; new_dir_mask |=FS_MOVED_TO ; -inotify_inode_queue_event (old_dir,IN_MOVED_FROM |isdir,cookie,old_name, +inotify_inode_queue_event (old_dir,IN_MOVED_FROM |isdir, in_cookie, old_name, source); -inotify_inode_queue_event (new_dir,IN_MOVED_TO |isdir, cookie, new_name, +inotify_inode_queue_event (new_dir,IN_MOVED_TO |isdir, in_cookie, new_name, source); -fsnotify (old_dir, old_dir_mask, old_dir,FSNOTIFY_EVENT_INODE , old_name); -fsnotify (new_dir, new_dir_mask, new_dir,FSNOTIFY_EVENT_INODE , new_name); +fsnotify (old_dir, old_dir_mask, old_dir,FSNOTIFY_EVENT_INODE , old_name, fs_cookie); +fsnotify (new_dir, new_dir_mask, new_dir,FSNOTIFY_EVENT_INODE , new_name, fs_cookie);if (target)fsnotify_inoderemove (target);if (source) {inotify_inode_queue_event (source,IN_MOVE_SELF ,0 ,NULL ,NULL ); -fsnotify (source,FS_MOVE_SELF , moved->d_inode,FSNOTIFY_EVENT_INODE ,NULL ); +fsnotify (source,FS_MOVE_SELF , moved->d_inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); }audit_inode_child (new_name, moved, new_dir); }
@@ -158,7 +159,7 @@ static inline void fsnotify_link_count(struct inode *inode) {inotify_inode_queue_event (inode,IN_ATTRIB ,0 ,NULL ,NULL ); -fsnotify (inode,FS_ATTRIB , inode,FSNOTIFY_EVENT_INODE ,NULL ); +fsnotify (inode,FS_ATTRIB , inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); }/*
@@ -170,7 +171,7 @@ static inline void fsnotify_create(struct inode *inode, struct dentry *dentry) dentry->d_inode);audit_inode_child (dentry->d_name.name, dentry, inode); -fsnotify (inode,FS_CREATE , dentry->d_inode,FSNOTIFY_EVENT_INODE , dentry->d_name.name); +fsnotify (inode,FS_CREATE , dentry->d_inode,FSNOTIFY_EVENT_INODE , dentry->d_name.name,0 ); }/*
@@ -185,7 +186,7 @@ static inline void fsnotify_link(struct inode *dir, struct inode *inode, structfsnotify_link_count (inode);audit_inode_child (new_dentry->d_name.name, new_dentry, dir); -fsnotify (dir,FS_CREATE , inode,FSNOTIFY_EVENT_INODE , new_dentry->d_name.name); +fsnotify (dir,FS_CREATE , inode,FSNOTIFY_EVENT_INODE , new_dentry->d_name.name,0 ); }/*
@@ -197,7 +198,7 @@ static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry) dentry->d_name.name, dentry->d_inode);audit_inode_child (dentry->d_name.name, dentry, inode); -fsnotify (inode,FS_CREATE |FS_IN_ISDIR , dentry->d_inode,FSNOTIFY_EVENT_INODE , dentry->d_name.name); +fsnotify (inode,FS_CREATE |FS_IN_ISDIR , dentry->d_inode,FSNOTIFY_EVENT_INODE , dentry->d_name.name,0 ); }/*
@@ -215,7 +216,7 @@ static inline void fsnotify_access(struct dentry *dentry)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ); +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); }/*
@@ -233,7 +234,7 @@ static inline void fsnotify_modify(struct dentry *dentry)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ); +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); }/*
@@ -251,7 +252,7 @@ static inline void fsnotify_open(struct dentry *dentry)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ); +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); }/*
@@ -272,7 +273,7 @@ static inline void fsnotify_close(struct file *file)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask); -fsnotify (inode, mask, file,FSNOTIFY_EVENT_FILE ,NULL ); +fsnotify (inode, mask, file,FSNOTIFY_EVENT_FILE ,NULL ,0 ); }/*
@@ -290,7 +291,7 @@ static inline void fsnotify_xattr(struct dentry *dentry)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ); +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); }/*
@@ -328,7 +329,7 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid) dentry->d_name.name);fsnotify_parent (dentry, mask); -fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ); +fsnotify (inode, mask, inode,FSNOTIFY_EVENT_INODE ,NULL ,0 ); } }
include/linux/fsnotify_backend.h +12 -3
@@ -160,6 +160,7 @@ struct fsnotify_event {atomic_t refcnt;/* how many groups still are using/need to send this event */ __u32 mask;/* the type of access */ +u32 sync_cookie;/* used to corrolate events, namely inotify mv events */ char *file_name;size_t name_len; };
@@ -192,8 +193,9 @@ struct fsnotify_mark_entry {#ifdef CONFIG_FSNOTIFY /* called from the vfs to signal fs events */ -extern void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is,const char *name); +extern void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is,const char *name,u32 cookie);extern void __fsnotify_inode_delete (struct inode *inode); +extern u32 fsnotify_get_cookie (void );static inline int fsnotify_inode_watches_children (struct inode *inode) {
@@ -251,11 +253,13 @@ extern void fsnotify_get_mark(struct fsnotify_mark_entry *entry);extern void fsnotify_put_mark (struct fsnotify_mark_entry *entry);/* put here because inotify does some weird stuff when destroying watches */ -extern struct fsnotify_event *fsnotify_create_event (struct inode *to_tell,__u32 mask,void *data,int data_is,const char *name); +extern struct fsnotify_event *fsnotify_create_event (struct inode *to_tell,__u32 mask,void *data, +int data_is,const char *name,__u32 cookie);#else -static inline void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is,const char *name); +static inline void fsnotify (struct inode *to_tell,__u32 mask,void *data,int data_is, +const char *name,u32 cookie); {}static inline void __fsnotify_inode_delete (struct inode *inode)
@@ -264,6 +268,11 @@ static inline void __fsnotify_inode_delete(struct inode *inode)static inline void __fsnotify_d_instantiate (struct dentry *dentry,struct inode *inode) {} +static inline u32 fsnotify_get_cookie (void ) +{ +return 0 ; +} +#endif /* CONFIG_FSNOTIFY */ #endif /* __KERNEL __ */
PATCH 10/13 fsnotify: allow groups to add private data to events
In reply to Eric Parisinotify needs per group information attached to events. This patch allows groups to attach private information and implements a callback so that information can be freed when an event is being destroyed.
---
fs/notify/dnotify/dnotify.c | 1 + fs/notify/notification.c | 31 +++++++++++++++++++++++++++---- include/linux/fsnotify_backend.h | 15 ++++++++++++++- 3 files changed, 42 insertions(+), 5 deletions(-)
fs/notify/dnotify/dnotify.c +1 -0
@@ -152,6 +152,7 @@ static struct fsnotify_ops dnotify_fsnotify_ops = { .should_send_event = dnotify_should_send_event, .free_group_priv = NULL, .freeing_mark = dnotify_freeing_mark, + .free_event_priv = NULL, };void dnotify_flush (struct file *filp,fl_owner_t id)
fs/notify/notification.c +27 -4
@@ -71,6 +71,7 @@ void fsnotify_put_event(struct fsnotify_event *event)if (event->data_type ==FSNOTIFY_EVENT_PATH )path_put (&event->path); +BUG_ON (!list_empty (&event->private_data_list));kfree (event->file_name);kmem_cache_free (event_kmem_cache, event); }
@@ -86,8 +87,23 @@ void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder)kmem_cache_free (event_holder_kmem_cache, holder); } +struct fsnotify_event_private_data *fsnotify_get_priv_from_event (struct fsnotify_group *group,struct fsnotify_event *event) +{ +struct fsnotify_event_private_data *lpriv; +struct fsnotify_event_private_data *priv =NULL ; + +list_for_each_entry (lpriv, &event->private_data_list, event_list) { +if (lpriv->group == group) { + priv = lpriv; +break ; + } + } +return priv; +} +/* -* check if 2 events contain the same information. +* check if 2 events contain the same information. we do not compare private data +* but at this moment that isn't a problem. */ static inline int event_compare (struct fsnotify_event *old,struct fsnotify_event *new) {
@@ -111,10 +127,11 @@ static inline int event_compare(struct fsnotify_event *old, struct fsnotify_even }/* -* Add an event to the group notification queue. The group can later pull this -* event off the queue to deal with. +* Add events to the generic group notification queue. We test if the event +* is the same as the last event in the queue, and if so, we do not add it. +* Events added to this queue must be removed with fsnotify_remove_notif_event. */ -int fsnotify_add_notif_event (struct fsnotify_group *group,struct fsnotify_event *event) +int fsnotify_add_notif_event (struct fsnotify_group *group,struct fsnotify_event *event,struct fsnotify_event_private_data *priv) {struct fsnotify_event_holder *holder;struct list_head *list = &group->notification_list;
@@ -166,6 +183,8 @@ int fsnotify_add_notif_event(struct fsnotify_group *group, struct fsnotify_eventfsnotify_get_event (event);list_add_tail (&holder->event_list, list); +if (priv) +list_add_tail (&priv->event_list, &event->private_data_list);spin_unlock (&event->lock);mutex_unlock (&group->notification_mutex);
@@ -230,6 +249,8 @@ void fsnotify_flush_notif(struct fsnotify_group *group)mutex_lock (&group->notification_mutex);while (fsnotify_check_notif_queue (group)) { event =fsnotify_remove_notif_event (group); +if (group->ops->free_event_priv) + group->ops->free_event_priv (group, event);fsnotify_put_event (event); }mutex_unlock (&group->notification_mutex);
@@ -248,6 +269,8 @@ static void initialize_event(struct fsnotify_event *event) event->inode =NULL ; event->data_type =FSNOTIFY_EVENT_NONE ; +INIT_LIST_HEAD (&event->private_data_list); + event->to_tell =NULL ; event->file_name =NULL ;
include/linux/fsnotify_backend.h +14 -1
@@ -79,6 +79,7 @@ struct fsnotify_ops {int (*handle_event)(struct fsnotify_group *group,struct fsnotify_event *event);void (*free_group_priv)(struct fsnotify_group *group);void (*freeing_mark)(struct fsnotify_mark_entry *entry,struct fsnotify_group *group); +void (*free_event_priv)(struct fsnotify_group *group,struct fsnotify_event *event); };/*
@@ -130,6 +131,15 @@ struct fsnotify_event_holder { };/* +* Inotify needs to tack data onto an event. This struct lets us later find the +* correct private data of the correct group. +*/ +struct fsnotify_event_private_data { +struct fsnotify_group *group; +struct list_head event_list; +}; + +/* * all of the information about the original object we want to now send to * a group. If you want to carry more info from the accessing task to the * listener this structure is where you need to be adding fields.
@@ -163,6 +173,8 @@ struct fsnotify_event {u32 sync_cookie;/* used to corrolate events, namely inotify mv events */ char *file_name;size_t name_len; + +struct list_head private_data_list;/* groups can store private data here */ };/*
@@ -238,10 +250,11 @@ extern void fsnotify_put_group(struct fsnotify_group *group);extern void fsnotify_get_event (struct fsnotify_event *event);extern void fsnotify_put_event (struct fsnotify_event *event); -extern int fsnotify_add_notif_event (struct fsnotify_group *group,struct fsnotify_event *event); +extern int fsnotify_add_notif_event (struct fsnotify_group *group,struct fsnotify_event *event,struct fsnotify_event_private_data *priv);extern int fsnotify_check_notif_queue (struct fsnotify_group *group);extern struct fsnotify_event *fsnotify_peek_notif_event (struct fsnotify_group *group);extern struct fsnotify_event *fsnotify_remove_notif_event (struct fsnotify_group *group); +extern struct fsnotify_event_private_data *fsnotify_get_priv_from_event (struct fsnotify_group *group,struct fsnotify_event *event);/* functions used to manipulate the marks attached to inodes */ extern void fsnotify_recalc_inode_mask (struct inode *inode);
Al Viro
maintainerRe: [PATCH 10/13] fsnotify: allow groups to add private data to events
fs/notify/notification.c int fsnotify_add_notif_event(struct fsnotify_group *group, struct fsnotify_event · line 187
Quoted 11 lines · Eric Paris
fs/notify/notification.c +3
-1-int fsnotify_add_notif_event (struct fsnotify_group *group,struct fsnotify_event *event) +int fsnotify_add_notif_event (struct fsnotify_group *group,struct fsnotify_event *event,struct fsnotify_event_private_data *priv) {struct fsnotify_event_holder *holder;struct list_head *list = &group->notification_list;@@ -166,6 +183,8 @@ int fsnotify_add_notif_event(struct fsnotify_group *group, struct fsnotify_eventfsnotify_get_event (event);list_add_tail (&holder->event_list, list); +if (priv) +list_add_tail (&priv->event_list, &event->private_data_list);
Won't that leak priv if we bugger off earlier? On OOM or on finding last_event we are OK with...
PATCH 11/13 fsnotify: fsnotify marks on inodes pin them in core
In reply to Eric ParisThis patch pins any inodes with an fsnotify mark in core. The idea is that as soon as the mark is removed from the inode->fsnotify_mark_entries list the inode will be iput. In reality is doesn't quite work exactly this way. The igrab will happen when the mark is added to an inode, but the iput will happen when the inode pointer is NULL'd inside the mark.
It's possible that 2 racing things will try to remove the mark from different directions. One may try to remove the mark because of an explicit request and one might try to remove it because the inode was deleted. It's possible that the removal because of inode deletion will remove the mark from the inode's list, but the removal by explicit request will actually set entry->inode == NULL; and call the iput. This is safe.
---
fs/notify/inode_mark.c | 9 ++++++++- 1 files changed, 8 insertions(+), 1 deletions(-)
fs/notify/inode_mark.c +8 -1
@@ -159,6 +159,8 @@ void fsnotify_destroy_mark_by_entry(struct fsnotify_mark_entry *entry) group->ops->freeing_mark (entry, group);fsnotify_update_dentry_child_flags (inode); + +iput (inode); }void fsnotify_clear_marks_by_group (struct fsnotify_group *group)
@@ -228,11 +230,16 @@ void fsnotify_init_mark(struct fsnotify_mark_entry *entry, void (*free_mark)(str entry->free_mark = free_mark; } -int fsnotify_add_mark (struct fsnotify_mark_entry *entry,struct fsnotify_group *group,struct inode *inode) +int fsnotify_add_mark (struct fsnotify_mark_entry *entry,struct fsnotify_group *group,struct inode *in_inode) {struct fsnotify_mark_entry *lentry; +struct inode *inode;int ret =0 ; + inode =igrab (in_inode); +if (unlikely (!inode)) +return -EINVAL ; +/* * LOCKING ORDER!!!! * entry->lock
PATCH 12/13 fsnotify: handle filesystem unmounts with fsnotify marks
In reply to Eric ParisWhen an fs is unmounted with an fsnotify mark entry attached to one of its inodes we need to destroy that mark entry and we also (like inotify) send an unmount event.
---
fs/inode.c | 1 + fs/notify/inode_mark.c | 73 ++++++++++++++++++++++++++++++++++++++ include/linux/fsnotify_backend.h | 4 ++ 3 files changed, 78 insertions(+), 0 deletions(-)
fs/inode.c +1 -0
@@ -406,6 +406,7 @@ int invalidate_inodes(struct super_block * sb)mutex_lock (&iprune_mutex);spin_lock (&inode_lock);inotify_unmount_inodes (&sb->s_inodes); +fsnotify_unmount_inodes (&sb->s_inodes); busy =invalidate_list (&sb->s_inodes, &throw_away);spin_unlock (&inode_lock);
fs/notify/inode_mark.c +73 -0
82 lines not on this page · raw message
include/linux/fsnotify_backend.h +4 -0
@@ -264,6 +264,7 @@ extern int fsnotify_add_mark(struct fsnotify_mark_entry *entry, struct fsnotify_extern void fsnotify_destroy_mark_by_entry (struct fsnotify_mark_entry *entry);extern void fsnotify_get_mark (struct fsnotify_mark_entry *entry);extern void fsnotify_put_mark (struct fsnotify_mark_entry *entry); +extern void fsnotify_unmount_inodes (struct list_head *list);/* put here because inotify does some weird stuff when destroying watches */ extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
@@ -286,6 +287,9 @@ static inline u32 fsnotify_get_cookie(void)return 0 ; } +static inline void fsnotify_unmount_inodes (struct list_head *list) +{} +#endif /* CONFIG_FSNOTIFY */ #endif /* __KERNEL __ */
PATCH 13/13 inotify: reimplement inotify using fsnotify
In reply to Eric ParisReimplement inotify_user using fsnotify. This should be feature for feature exactly the same as the original inotify_user. This does not make any changes to the in kernel inotify feature used by audit. Those patches (and the eventual removal of in kernel inotify) will come after the new inotify_user proves to be working correctly.
---
MAINTAINERS | 2 fs/notify/fsnotify.h | 1 fs/notify/inotify/Kconfig | 20 + fs/notify/inotify/Makefile | 2 fs/notify/inotify/inotify.h | 107 ++++++ fs/notify/inotify/inotify_fsnotify.c | 146 +++++++++ fs/notify/inotify/inotify_kernel.c | 273 ++++++++++++++++ fs/notify/inotify/inotify_user.c | 584 ++++++++-------------------------- fs/notify/notification.c | 28 +- include/linux/fsnotify.h | 3 include/linux/fsnotify_backend.h | 12 + 11 files changed, 717 insertions(+), 461 deletions(-) create mode 100644 fs/notify/inotify/inotify.h create mode 100644 fs/notify/inotify/inotify_fsnotify.c create mode 100644 fs/notify/inotify/inotify_kernel.c
MAINTAINERS +2 -0
@@ -2252,6 +2252,8 @@ P: John McCutchan M: [email protected] P: Robert Love M: [email protected] +P: Eric Paris +M: [email protected] L: [email protected] S: Maintained
fs/notify/fsnotify.h +0 -1
@@ -17,7 +17,6 @@ extern __u32 fsnotify_mask;extern void fsnotify_flush_notif (struct fsnotify_group *group); -extern void fsnotify_clear_marks_by_group (struct fsnotify_group *group);extern void fsnotify_clear_marks_by_inode (struct inode *inode);extern struct fsnotify_event_holder *fsnotify_alloc_event_holder (void );
fs/notify/inotify/Kconfig +12 -8
@@ -1,26 +1,30 @@ config INOTIFY bool "Inotify file change notification support" - default y + default n ---help--- - Say Y here to enable inotify support. Inotify is a file change - notification system and a replacement for dnotify. Inotify fixes - numerous shortcomings in dnotify and introduces several new features - including multiple file events, one-shot support, and unmount - notification. + Say Y here to enable legacy in kernel inotify support. Inotify is a + file change notification system. It is a replacement for dnotify. + This option only provides the legacy inotify in kernel API. There + are no in tree kernel users of this interface since it is deprecated. + You only need this if you are loading an out of tree kernel module + that uses inotify. For more information, see <file:Documentation/filesystems/inotify.txt> - If unsure, say Y. + If unsure, say N. config INOTIFY_USER bool "Inotify support for userspace" - depends on INOTIFY + depends on FSNOTIFY default y ---help--- Say Y here to enable inotify support for userspace, including the associated system calls. Inotify allows monitoring of both files and directories via a single open fd. Events are read from the file descriptor, which is also select()- and poll()-able. + Inotify fixes numerous shortcomings in dnotify and introduces several + new features including multiple file events, one-shot support, and + unmount notification. For more information, see <file:Documentation/filesystems/inotify.txt>
fs/notify/inotify/Makefile +1 -1
@@ -1,2 +1,2 @@ obj-$(CONFIG_INOTIFY) += inotify.o -obj-$(CONFIG_INOTIFY_USER) += inotify_user.o +obj-$(CONFIG_INOTIFY_USER) += inotify_fsnotify.o inotify_kernel.o inotify_user.o
fs/notify/inotify/inotify.h new file +107 -0
107 lines not on this page · raw message
fs/notify/inotify/inotify_fsnotify.c new file +146 -0
146 lines not on this page · raw message
fs/notify/inotify/inotify_kernel.c new file +273 -0
273 lines not on this page · raw message
fs/notify/inotify/inotify_user.c +142 -442
779 lines not on this page · raw message
fs/notify/notification.c +21 -7
@@ -133,7 +133,7 @@ static inline int event_compare(struct fsnotify_event *old, struct fsnotify_even*/ int fsnotify_add_notif_event (struct fsnotify_group *group,struct fsnotify_event *event,struct fsnotify_event_private_data *priv) { -struct fsnotify_event_holder *holder; +struct fsnotify_event_holder *holder =NULL ;struct list_head *list = &group->notification_list;struct fsnotify_event_holder *last_holder;struct fsnotify_event *last_event;
@@ -151,13 +151,11 @@ int fsnotify_add_notif_event(struct fsnotify_group *group, struct fsnotify_event* the event->lock. If we see it empty we know that once we * get that lock the embedded holder will be ok for us to (re)use */ -if (list_empty (&event->holder.event_list)) - holder = (struct fsnotify_event_holder *)event; -else +if (!list_empty (&event->holder.event_list)) { holder =alloc_event_holder (); - -if (!holder) -return -ENOMEM ; +if (!holder) +return -ENOMEM ; + }mutex_lock (&group->notification_mutex);
@@ -166,6 +164,22 @@ int fsnotify_add_notif_event(struct fsnotify_group *group, struct fsnotify_eventspin_lock (&event->lock); +if (list_empty (&event->holder.event_list)) { +if (unlikely (holder)) +fsnotify_destroy_event_holder (holder); + holder = &event->holder; + }else if (unlikely (!holder)) { +/* this only happens if we had room in the original event holder +* but we switched to the overflow event and that in event holder +* was in use */ + holder =alloc_event_holder (); +if (!holder) { +spin_unlock (&event->lock); +mutex_unlock (&group->notification_mutex); +return -ENOMEM ; + } + } +if (!list_empty (list)) { last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list); last_event = last_holder->event;
include/linux/fsnotify.h +1 -2
@@ -262,14 +262,13 @@ static inline void fsnotify_close(struct file *file) {struct dentry *dentry = file->f_path.dentry;struct inode *inode = dentry->d_inode; -const char *name = dentry->d_name.name;fmode_t mode = file->f_mode;__u32 mask = (mode &FMODE_WRITE ) ?FS_CLOSE_WRITE :FS_CLOSE_NOWRITE ;if (S_ISDIR (inode->i_mode)) mask |=FS_IN_ISDIR ; -inotify_dentry_parent_queue_event (dentry, mask,0 , name); +inotify_dentry_parent_queue_event (dentry, mask,0 , dentry->d_name.name);inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL );fsnotify_parent (dentry, mask);
include/linux/fsnotify_backend.h +12 -0
@@ -9,6 +9,7 @@#ifdef __KERNEL__ +#include <linux/idr.h> /* inotify uses this */ #include <linux/fs.h> /* struct inode */ #include <linux/list.h> #include <linux/path.h> /* struct path */
@@ -58,6 +59,7 @@/* listeners that hard code group numbers near the top */ #define DNOTIFY_GROUP_NUM UINT_MAX +#define INOTIFY_GROUP_NUM (DNOTIFY_GROUP_NUM-1)struct fsnotify_group ;struct fsnotify_event ;
@@ -112,6 +114,15 @@ struct fsnotify_group {/* groups can define private fields here */ union { +#ifdef CONFIG_INOTIFY_USER +struct inotify_group_private_data { +spinlock_t idr_lock; +struct idr idr; +u32 last_wd; +struct fasync_struct *fa;/* async notification */ +struct user_struct *user; + } inotify_data; +#endif }; };
@@ -262,6 +273,7 @@ extern void fsnotify_init_mark(struct fsnotify_mark_entry *entry, void (*free_maextern struct fsnotify_mark_entry *fsnotify_find_mark_entry (struct fsnotify_group *group,struct inode *inode);extern int fsnotify_add_mark (struct fsnotify_mark_entry *entry,struct fsnotify_group *group,struct inode *inode);extern void fsnotify_destroy_mark_by_entry (struct fsnotify_mark_entry *entry); +extern void fsnotify_clear_marks_by_group (struct fsnotify_group *group);extern void fsnotify_get_mark (struct fsnotify_mark_entry *entry);extern void fsnotify_put_mark (struct fsnotify_mark_entry *entry);extern void fsnotify_unmount_inodes (struct list_head *list);
Al Viro
maintainerRe: [PATCH 13/13] inotify: reimplement inotify using fsnotify
fs/notify/notification.c int fsnotify_add_notif_event(struct fsnotify_group *group, struct fsnotify_event · line 177
Quoted 3 lines · Eric Paris
fs/notify/notification.c +3
-0+ holder =alloc_event_holder (); +if (!holder) { +spin_unlock (&event->lock);
Um. Your alloc_event_holder() is using GFP_KERNEL...