Skip to contentSkip to conversation

fsnotify: unified filesystem notification backend

PATCH In review Eric Paris 2 participants 14 messages Feb 9, 2009 – Feb 17, 2009

Subsystems (5)
lore
Share & tools
lore

Eric Paris

author
#0 lore raw

PATCH 1/11 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.

---

 fs/notify/Kconfig                |   13 +++
 fs/notify/Makefile               |    2 
 fs/notify/fsnotify.c             |   78 +++++++++++++++++++
 fs/notify/fsnotify.h             |   19 +++++
 fs/notify/group.c                |  157 ++++++++++++++++++++++++++++++++++++++
 fs/notify/notification.c         |  133 ++++++++++++++++++++++++++++++++
 include/linux/fsnotify.h         |   55 +++++++++++--
 include/linux/fsnotify_backend.h |  148 ++++++++++++++++++++++++++++++++++++
 8 files changed, 597 insertions(+), 8 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
index 50914d7..31dac7e 100644
@@ -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
index 5a95b60..7cb285a 100644
@@ -1,2 +1,4 @@
 obj-y			+= dnotify/
 obj-y			+= inotify/
+
+obj-$(CONFIG_FSNOTIFY)		+= fsnotify.o notification.o group.o
fs/notify/fsnotify.c new file +78 -0

78 lines not on this page · raw message

fs/notify/fsnotify.h new file +19 -0
new file mode 100644
index 0000000..9c53600
@@ -0,0 +1,19 @@
+#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_struct;
+extern struct list_head fsnotify_groups;
+extern __u64 fsnotify_mask;
+
+extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is);
+#endif	/* _LINUX_FSNOTIFY_PRIVATE_H */
fs/notify/group.c new file +157 -0

157 lines not on this page · raw message

fs/notify/notification.c new file +133 -0

133 lines not on this page · raw message

include/linux/fsnotify.h +47 -8

182 lines not on this page · raw message

include/linux/fsnotify_backend.h new file +148 -0

148 lines not on this page · raw message

author
#1 lore raw

PATCH 2/11 fsnotify: add group priorities

In preperation for blocking fsnotify calls group priorities must be added. When multiple groups request the same event type the lowest priority group will receive the notification first.

---

 fs/notify/group.c                |   27 ++++++++++++++++++++++-----
 include/linux/fsnotify_backend.h |    3 ++-
 2 files changed, 24 insertions(+), 6 deletions(-)
fs/notify/group.c +22 -5
index a877929..d7cabe5 100644
@@ -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);
 }
 
 void fsnotify_get_group(struct fsnotify_group *group)
@@ -98,14 +111,16 @@ void fsnotify_put_group(struct fsnotify_group *group)
 	}
 }
 
-static struct fsnotify_group *fsnotify_find_group(unsigned int group_num, __u64 mask, const struct fsnotify_ops *ops)
+static struct fsnotify_group *fsnotify_find_group(unsigned int priority, unsigned int group_num,
+						  __u64 mask, const struct fsnotify_ops *ops)
 {
 	struct fsnotify_group *group_iter;
 	struct fsnotify_group *group = NULL;
 
 	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;
@@ -121,7 +136,8 @@ static struct fsnotify_group *fsnotify_find_group(unsigned int group_num, __u64
  * 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, __u64 mask, const struct fsnotify_ops *ops)
+struct fsnotify_group *fsnotify_obtain_group(unsigned int priority, unsigned int group_num,
+					     __u64 mask, const struct fsnotify_ops *ops)
 {
 	struct fsnotify_group *group, *tgroup;
 
@@ -131,13 +147,14 @@ struct fsnotify_group *fsnotify_obtain_group(unsigned int group_num, __u64 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);
 	/* we raced and something else inserted the same group */
 	if (tgroup) {
 		mutex_unlock(&fsnotify_grp_mutex);
include/linux/fsnotify_backend.h +2 -1
index 98ffcd6..e156df5 100644
@@ -86,6 +86,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 */
@@ -129,7 +130,7 @@ extern void fsnotify(struct inode *to_tell, __u64 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, __u64 mask, const struct fsnotify_ops *ops);
+extern struct fsnotify_group *fsnotify_obtain_group(unsigned int priority, unsigned int group_num, __u64 mask, const struct fsnotify_ops *ops);
 extern void fsnotify_put_group(struct fsnotify_group *group);
 extern void fsnotify_get_group(struct fsnotify_group *group);
 
author
#2 lore raw

PATCH 3/11 fsnotify: add in inode fsnotify markings

In reply to 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.

---

 Documentation/filesystems/fsnotify.txt |  192 ++++++++++++++++++++++++++
 fs/inode.c                             |    9 +
 fs/notify/Makefile                     |    2 
 fs/notify/fsnotify.c                   |   13 ++
 fs/notify/fsnotify.h                   |    3 
 fs/notify/group.c                      |   24 +++
 fs/notify/inode_mark.c                 |  238 ++++++++++++++++++++++++++++++++
 include/linux/fs.h                     |    5 +
 include/linux/fsnotify.h               |    9 +
 include/linux/fsnotify_backend.h       |   50 +++++++
 10 files changed, 543 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/filesystems/fsnotify.txt
 create mode 100644 fs/notify/inode_mark.c
Documentation/filesystems/fsnotify.txt new file +192 -0
new file mode 100644
index 0000000..6da6cc1
@@ -0,0 +1,192 @@
+fsnotify inode mark locking/lifetime/and refcnting
+
+struct fsnotify_mark_entry {
+	__u64 mask;			/* mask this mark entry is for */
+	atomic_t refcnt;		/* active things looking at this mark */
+	int freeme;			/* free when this is set and refcnt hits 0 */
+	struct inode *inode;		/* inode this entry is associated with */
+	struct fsnotify_group *group;	/* group this mark entry is for */
+	struct list_head 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_private)(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 0.  Any task that finds the fsnotify_mark_entry and either
+already has a reference or holds the inode->i_lock or group->mark_lock can
+take a reference.  The mark will survive until the reference hits 0 and the
+object has been marked for death (freeme)
+
+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 3 things, entry->group, entry->inode, and entry->freeme.
+
+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 and freeme has been set.  fsnotify_mark_entries normally live in the
+kernel with a refcnt==0 and only have positive refcnt when "something" is
+actively referencing its contents.  freeme is set when the fsnotify_mark is no
+longer on either an inode or a group list.  If it is off both lists we
+know the mark is unreachable by anything else and when the refcnt hits 0 we
+can free.
+
+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.  While
+walking the list we list_del_init the i_list, take a reference, and using
+free_i_list hook this into a private list we anchor on the stack.  At this
+point we can safely drop the i_lock and walk the private list we anchored on
+the stack (remember we hold a reference to everything on this list.)  For each
+entry we take the entry->lock and check if the ->group and ->inode pointers
+are set.  If they are, we take their respective locks and now hold all the
+interesting locks.  We can safely list_del_init the i_list and g_list and can
+set ->inode and ->group to NULL.  Once off both lists and everything is surely
+NULL we set freeme for cleanup.
+
+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 4 things
+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 meand and takes a reference.
+
+At this point.
+	refcnt == 2
+	i_list -> inode
+	inode -> inode
+	g_list -> group
+	group -> group
+	free_i_list -> NUL
+	free_g_list -> NUL
+	freeme = 0
+
+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 == 3
+	i_list -> NUL 
+	inode -> inode
+	g_list -> group
+	group -> group
+	free_i_list -> private list on stack
+	free_g_list -> NUL
+	freeme = 0
+
+refcnt == 3.  events is not on the i_list.
+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.
+
+At this point.
+	refcnt == 4
+	i_list -> NUL 
+	inode -> inode
+	g_list -> NUL
+	group -> group
+	free_i_list -> private list on stack
+	free_g_list -> private list on another stack
+	freeme = 0
+
+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
+	and set freeme = 1.  Unlock i_lock, mark_lock, and entry->lock.  Drop
+	reference.   (this is the end of A)
+
+At this point.
+	refcnt == 3
+	i_list -> NUL 
+	inode -> NULL
+	g_list -> NUL
+	group -> NULL
+	free_i_list -> private list on stack
+	free_g_list -> private list on another stack
+	freeme = 1
+
+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 even if off the
+	relevant lists)  D now pretends it has all the locks (even through it
+	only has mark->lock.)  so it calls list_del_init on i_list and g_list.
+	it sets ->inode and ->group to NULL (they already were) and it sets
+	freeme = 1 (it already way)  D unlocks all the locks it holds and
+	drops its reference
+
+At this point.
+	refcnt == 2
+	i_list -> NUL 
+	inode -> NULL
+	g_list -> NUL
+	group -> NULL
+	free_i_list -> private list on stack
+	free_g_list -> undefined
+	freeme = 1
+
+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
+	freeme = 1
+
+B is the only thing left with a reference and it will do something similar to
+A, only it will only be holding mark->lock (it won't get the inode or group
+lock since they are NULL in the event.)  It will also clear everything,
+setfreeme, unlock, and drop it's reference via fsnotify_put_mark.
+
+fsnotify_put_mark calls atomic_dec_and_test(mark->refcnt, mark->lock) and will
+then test if freeme is set.  It is set.  That means we are the last thing
+inside the kernel with a pointer to this fsnotify_mark_entry and it should be
+destroyed.  fsnotify_put_mark proceedes to clean up any private data and frees
+things up.  
fs/inode.c +9 -0
index 40e37c0..f6a51a1 100644
@@ -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_LIST_HEAD(&inode->i_fsnotify_mark_entries);
+#endif
 }
 
 EXPORT_SYMBOL(inode_init_once);
fs/notify/Makefile +1 -1
index 7cb285a..47b60f3 100644
@@ -1,4 +1,4 @@
 obj-y			+= dnotify/
 obj-y			+= inotify/
 
-obj-$(CONFIG_FSNOTIFY)		+= fsnotify.o notification.o group.o
+obj-$(CONFIG_FSNOTIFY)		+= fsnotify.o notification.o group.o inode_mark.o
fs/notify/fsnotify.c +13 -0
index b51f90a..e7e53f7 100644
@@ -25,6 +25,15 @@
 #include <linux/fsnotify_backend.h>
 #include "fsnotify.h"
 
+void __fsnotify_inode_delete(struct inode *inode, int flag)
+{
+	if (list_empty(&fsnotify_groups))
+		return;
+
+	fsnotify_clear_marks_by_inode(inode, flag);
+}
+EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);
+
 void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is)
 {
 	struct fsnotify_group *group;
@@ -37,6 +46,8 @@ void fsnotify(struct inode *to_tell, __u64 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 (assuming something is using fsnotify)  Not blocking while
@@ -52,6 +63,8 @@ void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is)
 	idx = srcu_read_lock(&fsnotify_grp_srcu_struct);
 	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, lets hope something else blows up */
fs/notify/fsnotify.h +3 -0
index 9c53600..bad4da9 100644
@@ -16,4 +16,7 @@ extern struct list_head fsnotify_groups;
 extern __u64 fsnotify_mask;
 
 extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is);
+
+extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group);
+extern void fsnotify_clear_marks_by_inode(struct inode *inode, unsigned int flags);
 #endif	/* _LINUX_FSNOTIFY_PRIVATE_H */
fs/notify/group.c +24 -0
index d7cabe5..bac7d8d 100644
@@ -47,6 +47,24 @@ void fsnotify_recalc_global_mask(void)
 	fsnotify_mask = mask;
 }
 
+void fsnotify_recalc_group_mask(struct fsnotify_group *group)
+{
+	__u64 mask = 0;
+	unsigned long 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 @@ 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);
 
@@ -151,6 +172,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 +238 -0

238 lines not on this page · raw message

include/linux/fs.h +5 -0
index 7c68aa9..c5ec88f 100644
@@ -690,6 +690,11 @@ struct inode {
 
 	__u32			i_generation;
 
+#ifdef CONFIG_FSNOTIFY
+	__u64			i_fsnotify_mask; /* all events this inode cares about */
+	struct list_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
index d1f7de2..6ae332f 100644
@@ -99,6 +99,14 @@ static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
 }
 
 /*
+ * 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_INODE_DESTROY);
+}
+
+/*
  * fsnotify_inoderemove - an inode is going away
  */
 static inline void fsnotify_inoderemove(struct inode *inode)
@@ -107,6 +115,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, FSNOTIFY_LAST_DENTRY);
 }
 
 /*
include/linux/fsnotify_backend.h +49 -1

98 lines not on this page · raw message

#3 lore raw

Re: [PATCH -v1 03/11] fsnotify: add in inode fsnotify markings

<picks a patch at random>

I don't have a good sense of what value all of this work brings to Linux. Why should I get excited about this? Why is it all worth the time/effort/risk/etc which would be involved in getting it integrated?

All rather unclear, but very important!

Quoted 1 line · On Mon, 09 Feb 2009 16:15:37 -0500 Eric Paris <[email protected]> wrote:

This patch creates in inode fsnotify markings.

I often come to a patch intending to review it, run into trouble and I instead end up reviewing the code's _reviewability_, rather than reviewing the code itself.

IOW, I often find the code to be too hard to understand. It's not that I _couldn't_ understand it, but I feel that the code requires too much time and work to understand, and that this effort could be reduced were the originator to go back and make the code more approachable.

I figure that if we do this, we end up with code which is more maintainable. Plus the review ends up being much more effective.

This patch is one of those patches.

Quoted 4 lines

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.

Please use checkpatch? The patchset introduces numerous trivial layout glitches which you did not intend to add.

Quoted 7 lines

...

Documentation/filesystems/fsnotify.txt new file +1 -0
@@ -0,0 +1,192 @@
+fsnotify inode mark locking/lifetime/and refcnting

Documentation/filesystems/fsnotify.txt line 11

Quoted 10 lines
Documentation/filesystems/fsnotify.txt new file +10 -0
+
+struct fsnotify_mark_entry {
+	__u64 mask;			/* mask this mark entry is for */
+	atomic_t refcnt;		/* active things looking at this mark */
+	int freeme;			/* free when this is set and refcnt hits 0 */
+	struct inode *inode;		/* inode this entry is associated with */
+	struct fsnotify_group *group;	/* group this mark entry is for */
+	struct list_head 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 */

s/killme/freeme/?

Documentation/filesystems/fsnotify.txt line 15

Quoted 4 lines
Documentation/filesystems/fsnotify.txt new file +4 -0
+	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_private)(struct fsnotify_mark_entry *entry); /* called on final put+free */
+};

Why does `freeme' exist at all? Normally the refcount is sufficient. What is different here?

Documentation/filesystems/fsnotify.txt line 23

Quoted 7 lines
Documentation/filesystems/fsnotify.txt new file +7 -0
+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 0.  Any task that finds the fsnotify_mark_entry and either
+already has a reference or holds the inode->i_lock or group->mark_lock can
+take a reference.  The mark will survive until the reference hits 0 and the
+object has been marked for death (freeme)

See, we'd more typically "mark it for death" by decrementing its refcount at that time. If that made it go to zero then the object gets destroyed immediately.

Quoted 18 lines
Documentation/filesystems/fsnotify.txt new file +18 -0
+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 3 things, entry->group, entry->inode, and entry->freeme.
+
+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)

Documentation/filesystems/fsnotify.txt line 52

Quoted 10 lines
Documentation/filesystems/fsnotify.txt new file +10 -0
+
+
+LIFETIME:
+Inode marks survive between when they are added to an inode and when their
+refcnt==0 and freeme has been set.  fsnotify_mark_entries normally live in the
+kernel with a refcnt==0 and only have positive refcnt when "something" is
+actively referencing its contents.  freeme is set when the fsnotify_mark is no
+longer on either an inode or a group list.  If it is off both lists we
+know the mark is unreachable by anything else and when the refcnt hits 0 we
+can free.

Incrementing the refcount by 1 for each time an object is present on a list is a fairly common technique.

Quoted 20 lines
Documentation/filesystems/fsnotify.txt new file +20 -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.  While
+walking the list we list_del_init the i_list, take a reference, and using
+free_i_list hook this into a private list we anchor on the stack.  At this
+point we can safely drop the i_lock and walk the private list we anchored on
+the stack (remember we hold a reference to everything on this list.)  For each
+entry we take the entry->lock and check if the ->group and ->inode pointers
+are set.  If they are, we take their respective locks and now hold all the
+interesting locks.  We can safely list_del_init the i_list and g_list and can
+set ->inode and ->group to NULL.  Once off both lists and everything is surely
+NULL we set freeme for cleanup.
+

Documentation/filesystems/fsnotify.txt line 83

Quoted 10 lines
Documentation/filesystems/fsnotify.txt new file +10 -0
+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 4 things
+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 meand and takes a reference.

"means"

Quoted 88 lines
Documentation/filesystems/fsnotify.txt new file +88 -0
+
+At this point.
+	refcnt == 2
+	i_list -> inode
+	inode -> inode
+	g_list -> group
+	group -> group
+	free_i_list -> NUL
+	free_g_list -> NUL
+	freeme = 0
+
+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 == 3
+	i_list -> NUL 
+	inode -> inode
+	g_list -> group
+	group -> group
+	free_i_list -> private list on stack
+	free_g_list -> NUL
+	freeme = 0
+
+refcnt == 3.  events is not on the i_list.
+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.
+
+At this point.
+	refcnt == 4
+	i_list -> NUL 
+	inode -> inode
+	g_list -> NUL
+	group -> group
+	free_i_list -> private list on stack
+	free_g_list -> private list on another stack
+	freeme = 0
+
+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
+	and set freeme = 1.  Unlock i_lock, mark_lock, and entry->lock.  Drop
+	reference.   (this is the end of A)
+
+At this point.
+	refcnt == 3
+	i_list -> NUL 
+	inode -> NULL
+	g_list -> NUL
+	group -> NULL
+	free_i_list -> private list on stack
+	free_g_list -> private list on another stack
+	freeme = 1
+
+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 even if off the
+	relevant lists)  D now pretends it has all the locks (even through it
+	only has mark->lock.)  so it calls list_del_init on i_list and g_list.
+	it sets ->inode and ->group to NULL (they already were) and it sets
+	freeme = 1 (it already way)  D unlocks all the locks it holds and
+	drops its reference
+
+At this point.
+	refcnt == 2
+	i_list -> NUL 
+	inode -> NULL
+	g_list -> NUL
+	group -> NULL
+	free_i_list -> private list on stack
+	free_g_list -> undefined
+	freeme = 1
+
+C does the same thing as B and the mark looks like:

Documentation/filesystems/fsnotify.txt line 181

Quoted 10 lines
Documentation/filesystems/fsnotify.txt new file +10 -0
+
+At this point.
+	refcnt == 1
+	i_list -> NUL 
+	inode -> NULL
+	g_list -> NUL
+	group -> NULL
+	free_i_list -> undefined
+	free_g_list -> undefined
+	freeme = 1

Holy cow.

Does the benefit of this new filesystem really justify the new complexity and risk?

Quoted 23 lines
Documentation/filesystems/fsnotify.txt new file +10 -0
+B is the only thing left with a reference and it will do something similar to
+A, only it will only be holding mark->lock (it won't get the inode or group
+lock since they are NULL in the event.)  It will also clear everything,
+setfreeme, unlock, and drop it's reference via fsnotify_put_mark.
+
+fsnotify_put_mark calls atomic_dec_and_test(mark->refcnt, mark->lock) and will
+then test if freeme is set.  It is set.  That means we are the last thing
+inside the kernel with a pointer to this fsnotify_mark_entry and it should be
+destroyed.  fsnotify_put_mark proceedes to clean up any private data and frees
+things up.  
fs/inode.c +1 -0
index 40e37c0..f6a51a1 100644
@@ -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>
 

fs/inode.c struct inode *inode_init_always(struct super_block *sb, struct inode *inode) · line 195

Quoted 6 lines
fs/inode.c +3 -0
@@ -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

That is the nineteenth open-coded instance of

	inode->foo = 0;

in this function.

Maybe it's time to just memset the whole thing? We were going to do that a while back but for some reason it didn't happen.

Quoted 37 lines
fs/inode.c +4 -0
 	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_LIST_HEAD(&inode->i_fsnotify_mark_entries);
+#endif
 }
 
 EXPORT_SYMBOL(inode_init_once);
fs/notify/Makefile +1 -1
index 7cb285a..47b60f3 100644
@@ -1,4 +1,4 @@
 obj-y			+= dnotify/
 obj-y			+= inotify/
 
-obj-$(CONFIG_FSNOTIFY)		+= fsnotify.o notification.o group.o
+obj-$(CONFIG_FSNOTIFY)		+= fsnotify.o notification.o group.o inode_mark.o
fs/notify/fsnotify.c +0 -0
index b51f90a..e7e53f7 100644
@@ -25,6 +25,15 @@
 #include <linux/fsnotify_backend.h>

fs/notify/fsnotify.c line 35

Quoted 10 lines
fs/notify/fsnotify.c +8 -0
 #include "fsnotify.h"
 
+void __fsnotify_inode_delete(struct inode *inode, int flag)
+{
+	if (list_empty(&fsnotify_groups))
+		return;
+
+	fsnotify_clear_marks_by_inode(inode, flag);
+}
+EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);

What a strange function.

- it tests some global list

- it has no locking for the access to that list

- it is wholly unobvious why this is safe - suppose that someone added something to fsnotify_groups one nanosecond after the list_empty() test failed?

It needs comments explaining all of this, at least?

- it has an argument called "flag". This name is only slightly less bad than the infamous "tmp". Please think up an identifier which better communicates the information which this scalar contains, then use that identifier consistently across the patchset.

Quoted 21 lines
fs/notify/fsnotify.c +2 -0
 void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is)
 {
 	struct fsnotify_group *group;
@@ -37,6 +46,8 @@ void fsnotify(struct inode *to_tell, __u64 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 (assuming something is using fsnotify)  Not blocking while
 
...
fs/notify/group.c +0 -0
@@ -47,6 +47,24 @@ void fsnotify_recalc_global_mask(void)
 	fsnotify_mask = mask;
 }
 

fs/notify/group.c void fsnotify_recalc_global_mask(void) · line 59

Quoted 10 lines
fs/notify/group.c +10 -0
+void fsnotify_recalc_group_mask(struct fsnotify_group *group)
+{
+	__u64 mask = 0;
+	unsigned long 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;
+	}

Unneeded braces. (There are multiple instances of this in the patchset)

fs/notify/group.c void fsnotify_recalc_global_mask(void) · line 66

Quoted 7 lines
fs/notify/group.c +7 -0
+	spin_unlock(&group->mark_lock);
+
+	group->mask = mask;
+
+	if (old_mask != mask)
+		fsnotify_recalc_global_mask();
+}

The function appears to be confused about the width of `mask'. Is it 64-bit or 32? Seems to be 64-bit, and that this is a bug.

A nice comment explaining this function's role in the world would be good.

Quoted 17 lines
fs/notify/group.c +4 -0
 static void fsnotify_add_group(struct fsnotify_group *group)
 {
 	int priority = group->priority;
@@ -73,6 +91,9 @@ 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);
 
 
...
 
+static void fsnotify_destroy_mark(struct fsnotify_mark_entry *entry)

fs/notify/group.c void fsnotify_get_group(struct fsnotify_group *group)

Quoted 10 lines
fs/notify/group.c +10 -0
+{
+	entry->group = NULL;
+	entry->inode = NULL;
+	entry->mask = 0;
+	INIT_LIST_HEAD(&entry->i_list);
+	INIT_LIST_HEAD(&entry->g_list);
+	INIT_LIST_HEAD(&entry->free_i_list);
+	INIT_LIST_HEAD(&entry->free_g_list);
+	entry->free_private(entry);
+}

Why does a `destroy' function initialise the object? Seems strange.

Perhaps because it is using a slab cache constructor somewhere? If so, perhaps it should stop doing that and switch to plain old initialise-at-allocation-time.

Quoted 10 lines
fs/notify/group.c +9 -0
+void fsnotify_get_mark(struct fsnotify_mark_entry *entry)
+{
+	atomic_inc(&entry->refcnt);
+}
 
+void fsnotify_put_mark(struct fsnotify_mark_entry *entry)
+{
+	if (atomic_dec_and_lock(&entry->refcnt, &entry->lock)) {
+		/*
+		 * if refcnt hit 0 and freeme was set when it hit zero it's

fs/notify/group.c

Quoted 10 lines
fs/notify/group.c +10 -0
+		 * time to clean up!
+		 */
+		if (entry->freeme) {
+			spin_unlock(&entry->lock);
+			fsnotify_destroy_mark(entry);
+			return;
+		}
+		spin_unlock(&entry->lock);
+	}
+}

This is strange in two ways.

a) the possibly-unneeded `freeme', mentioned above.

b) it takes a lock which is _internal_ to the object which is being discarded. A much more common pattern is to take a higher-level lock when the object's refcount falls to zero. eg:

                if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
                        iput_final(inode);

So something unusual is happening here. I think the challenge for you is to

- convince yourself (and others) that this design is appropriate and optimal.

- find a way of communicating the design to code-readers and to code-reviewers. So that others have a hope of maintaining it.

And personally, I don't find ../../Documentation/foo.txt to be terribly useful. I just don't trust those files to be complete and to be maintained. It is better if the code can be understood by reading the C file.

fs/notify/group.c

Quoted 10 lines
fs/notify/group.c +10 -0
+void fsnotify_recalc_inode_mask_locked(struct inode *inode)
+{
+	struct fsnotify_mark_entry *entry;
+	__u64 new_mask = 0;
+
+	list_for_each_entry(entry, &inode->i_fsnotify_mark_entries, i_list) {
+		new_mask |= entry->mask;
+	}
+	inode->i_fsnotify_mask = new_mask;
+}

The code should document which lock is needed. assert_spin_locked() is one way. Or a comment.

fs/notify/group.c

Quoted 6 lines
fs/notify/group.c +6 -0
+void fsnotify_recalc_inode_mask(struct inode *inode)
+{
+	spin_lock(&inode->i_lock);
+	fsnotify_recalc_inode_mask_locked(inode);
+	spin_unlock(&inode->i_lock);
+}

"Reader" is now wondering under what circumstances this function is called. Why does the kernel need to "recalculate" an inode mask? As a result of some syscall which did comething to an inode, perhaps? It's pretty hard to find this out, isn't it? Perhaps "Writer" can find a way to help "Reader" in this quest ;)

Quoted 28 lines
fs/notify/group.c +28 -0
+void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
+{
+	struct fsnotify_mark_entry *lentry, *entry;
+	struct inode *inode;
+	LIST_HEAD(free_list);
+
+	spin_lock(&group->mark_lock);
+	list_for_each_entry_safe(entry, lentry, &group->mark_entries, g_list) {
+		list_del_init(&entry->g_list);
+		list_add(&entry->free_g_list, &free_list);
+		fsnotify_get_mark(entry);
+	}
+	spin_unlock(&group->mark_lock);
+
+	list_for_each_entry_safe(entry, lentry, &free_list, free_g_list) {
+		spin_lock(&entry->lock);
+		inode = entry->inode;
+		if (!inode) {
+			entry->group = NULL;
+			spin_unlock(&entry->lock);
+			fsnotify_put_mark(entry);
+			continue;
+		}
+		spin_lock(&inode->i_lock);
+
+		list_del_init(&entry->i_list);
+		entry->inode = NULL;
+		list_del_init(&entry->g_list);

fs/notify/group.c

Quoted 10 lines
fs/notify/group.c +10 -0
+		entry->group = NULL;
+		entry->freeme = 1;
+
+		fsnotify_recalc_inode_mask_locked(inode);
+		spin_unlock(&inode->i_lock);
+		spin_unlock(&entry->lock);
+
+		fsnotify_put_mark(entry);
+	}
+}

OK, so we have the concept of a "mark". fsnotify_mark_entry is fairly nicely described, at its definition site.

We also have a concept of a "group". I wonder what that is. Logically, I go to the definition site of `struct fsnotify_group' and learn nothing!

Quoted 20 lines
fs/notify/group.c +20 -0
+void fsnotify_destroy_mark_by_entry(struct fsnotify_mark_entry *entry)
+{
+	struct fsnotify_group *group;
+	struct inode *inode;
+
+	spin_lock(&entry->lock);
+
+	group = entry->group;
+	if (group)
+		spin_lock(&group->mark_lock);
+
+	inode = entry->inode;
+	if (inode)
+		spin_lock(&inode->i_lock);
+
+	list_del_init(&entry->i_list);
+	entry->inode = NULL;
+	list_del_init(&entry->g_list);
+	entry->group = NULL;
+	entry->freeme = 1;

fs/notify/group.c

Quoted 10 lines
fs/notify/group.c +10 -0
+
+	if (inode) {
+		fsnotify_recalc_inode_mask_locked(inode);
+		spin_unlock(&inode->i_lock);
+	}
+	if (group)
+		spin_unlock(&group->mark_lock);
+
+	spin_unlock(&entry->lock);
+}

I find that if one understands the data structures, and the relationship between them then comprehending the implementation is fairly straightforward. That is particularly the case in this code. `struct fsnotify_mark_entry' is undocumented, which didn't help.

otoh, to an unusually high level, a large part of the complexity in this code revolves around the dynamic behaviour - what can race with what, which locks protect which data from which activities, etc. That part of it _has_ been documented. It made my head spin, and not understanding the data didn't help.

Quoted 10 lines
fs/notify/group.c +10 -0
+void fsnotify_clear_marks_by_inode(struct inode *inode, unsigned int flags)
+{
+	struct fsnotify_mark_entry *lentry, *entry;
+	LIST_HEAD(free_list);
+
+	spin_lock(&inode->i_lock);
+	list_for_each_entry_safe(entry, lentry, &inode->i_fsnotify_mark_entries, i_list) {
+		list_del_init(&entry->i_list);
+		list_add(&entry->free_i_list, &free_list);
+		fsnotify_get_mark(entry);

fs/notify/group.c

Quoted 10 lines
fs/notify/group.c +10 -0
+	}
+	spin_unlock(&inode->i_lock);
+
+	/*
+	 * at this point destroy_by_* might race.
+	 *
+	 * we used list_del_init() so it can be list_del_init'd again, no harm.
+	 * we were called from an inode function so we know that other user can
+	 * try to grab entry->inode->i_lock without a problem.
+	 */

Is this hacky?

This reader is wondering "well, why didn't we just take the lock, to prevent the race?". AFACIT there is no way for the reader to determine this from the code, which isn't a good situation.

fs/notify/group.c

Quoted 9 lines
fs/notify/group.c +9 -0
+	list_for_each_entry_safe(entry, lentry, &free_list, free_i_list) {
+		entry->group->ops->mark_clear_inode(entry, inode, flags);
+		fsnotify_put_mark(entry);
+	}
+
+	fsnotify_recalc_inode_mask(inode);
+}
+
+/* caller must hold inode->i_lock */

w00t!

I kinda like the use of assert_spin_locked() for this.

Quoted 58 lines
fs/notify/group.c +57 -0
+struct fsnotify_mark_entry *fsnotify_find_mark_entry(struct fsnotify_group *group, struct inode *inode)
+{
+	struct fsnotify_mark_entry *entry;
+
+	list_for_each_entry(entry, &inode->i_fsnotify_mark_entries, i_list) {
+		if (entry->group == group) {
+			fsnotify_get_mark(entry);
+			return entry;
+		}
+	}
+	return NULL;
+}
+
+void fsnotify_init_mark(struct fsnotify_mark_entry *entry, struct fsnotify_group *group, struct inode *inode, __u64 mask)
+{
+	spin_lock_init(&entry->lock);
+	atomic_set(&entry->refcnt, 1);
+	entry->group = group;
+	entry->mask = mask;
+	entry->inode = inode;
+	entry->freeme = 0;
+	entry->free_private = group->ops->free_mark;
+}
+
+int fsnotify_add_mark(struct fsnotify_mark_entry *entry)
+{
+	struct fsnotify_mark_entry *lentry;
+	struct fsnotify_group *group = entry->group;
+	struct inode *inode = entry->inode;
+	int ret = 0;
+
+	/*
+	 * LOCKING ORDER!!!!
+	 * entry->lock
+	 * group->mark_lock
+	 * inode->i_lock
+	 */
+	spin_lock(&group->mark_lock);
+	spin_lock(&inode->i_lock);
+
+	lentry = fsnotify_find_mark_entry(group, inode);
+	if (!lentry) {
+		list_add(&entry->i_list, &inode->i_fsnotify_mark_entries);
+		list_add(&entry->g_list, &group->mark_entries);
+		fsnotify_recalc_inode_mask_locked(inode);
+	}
+
+	spin_unlock(&inode->i_lock);
+	spin_unlock(&group->mark_lock);
+
+	if (lentry) {
+		ret = -EEXIST;
+		fsnotify_put_mark(lentry);
+	}
+
+	return ret;
+}
Quoted 1 line

From the name I'd guess that the function adds a mark to, umm,

something. Actually it adds it to the inode and to a list which is in an object with is pointed to by the to-be-added object. IOW I just discovered that we have bidirectional references happening here.

All very interesting.

include/linux/fs.h struct inode { · line 694

Quoted 10 lines
include/linux/fs.h +2 -0
index 7c68aa9..c5ec88f 100644
@@ -690,6 +690,11 @@ struct inode {
 
 	__u32			i_generation;
 
+#ifdef CONFIG_FSNOTIFY
+	__u64			i_fsnotify_mask; /* all events this inode cares about */

OK. Where are these events enumerated?

include/linux/fs.h struct inode { · line 695

Quoted 1 line
include/linux/fs.h +1 -0
+	struct list_head	i_fsnotify_mark_entries; /* fsnotify mark entries */

Reader wonders:

- what type of objects are strung onto this list?

- what is the locking protocol for this list?

Quoted 14 lines
include/linux/fs.h +2 -0
+#endif
+
 #ifdef CONFIG_DNOTIFY
 	unsigned long		i_dnotify_mask; /* Directory notify events */
 	struct dnotify_struct	*i_dnotify; /* for directory notifications */
include/linux/fsnotify.h +0 -0
index d1f7de2..6ae332f 100644
@@ -99,6 +99,14 @@ static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
 }
 
...
 

include/linux/fsnotify.h static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)

Quoted 10 lines
include/linux/fsnotify.h +10 -0
+/*
+ * a mark is simply an entry attached to an in core inode which allows an
+ * fsnotify listener to indicate they are either no longer interested in events
+ * of a type matching mask or only interested in those events.
+ *
+ * these are flushed when an inode is evicted from core and may be flushed
+ * when the inode is modified (as seen by fsnotify_access).  Some fsnotify users
+ * (such as dnotify) will flush these when the open fd is closed and not at
+ * inode eviction or modification.
+ */

That's a good comment!

I'm surprised that a caller adds a mark to opt out of notifications rather than to opt in. What's the thinking there?

include/linux/fsnotify.h

Quoted 9 lines
include/linux/fsnotify.h +9 -0
+struct fsnotify_mark_entry {
+	__u64 mask;			/* mask this mark entry is for */
+	atomic_t refcnt;		/* active things looking at this mark */
+	int freeme;			/* free when this is set and refcnt hits 0 */
+	struct inode *inode;		/* inode this entry is associated with */
+	struct fsnotify_group *group;	/* group this mark entry is for */
+	struct list_head 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 */

s/killme/freeme/

Quoted 5 lines
include/linux/fsnotify.h +5 -0
+	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_private)(struct fsnotify_mark_entry *entry); /* called on final put+free */
+};
+
author
#4 lore raw

Re: [PATCH -v1 03/11] fsnotify: add in inode fsnotify markings

Quoted 7 lines · Andrew Morton

<picks a patch at random>

I don't have a good sense of what value all of this work brings to Linux. Why should I get excited about this? Why is it all worth the time/effort/risk/etc which would be involved in getting it integrated?

All rather unclear, but very important!

I have largely taken your comments and rewritten this patch (and a couple others) to be easier to understand and review.

current patches are at http://people.redhat.com/~eparis/fsnotify

I received some offline comments which I intend to address and will be reposting later this week.

thank you!

author
#5 lore raw

PATCH 4/11 fsnotify: parent event notification

In reply to Eric Paris

inotify 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           |    4 +
 include/linux/dcache.h           |    3 +
 include/linux/fsnotify.h         |  111 +++++++++++++++++++++++++++++++++++++-
 include/linux/fsnotify_backend.h |    5 ++
 4 files changed, 119 insertions(+), 4 deletions(-)
fs/notify/inode_mark.c +4 -0
index 9a65fbc..840bd91 100644
@@ -78,6 +78,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);
 }
 
 void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
@@ -232,6 +234,8 @@ int fsnotify_add_mark(struct fsnotify_mark_entry *entry)
 	if (lentry) {
 		ret = -EEXIST;
 		fsnotify_put_mark(lentry);
+	} else {
+		fsnotify_update_dentry_child_flags(inode);
 	}
 
 	return ret;
include/linux/dcache.h +2 -1
index c66d224..2b935f2 100644
@@ -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 +108 -3

170 lines not on this page · raw message

include/linux/fsnotify_backend.h +5 -0
index a9508c5..ad7294f 100644
@@ -63,6 +63,11 @@
 #define FS_DN_RENAME		0x1000000000000000ull	/* file renamed */
 #define FS_DN_MULTISHOT		0x2000000000000000ull	/* dnotify multishot */
 
+#define FS_EVENTS_WITH_CHILD	(FS_ACCESS | FS_MODIFY | FS_ATTRIB |\
+				 FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | FS_OPEN |\
+				 FS_MOVED_FROM | FS_MOVED_TO | FS_CREATE |\
+				 FS_DELETE)
+
 /* when calling fsnotify tell it if the data is a path or inode */
 #define FSNOTIFY_EVENT_PATH	1
 #define FSNOTIFY_EVENT_INODE	2
author
#6 lore raw

PATCH 5/11 dnotify: reimplement dnotify using fsnotify

In reply to Eric Paris

Reimplement dnotify using fsnotify.

---

 MAINTAINERS                      |    2 
 fs/notify/dnotify/Kconfig        |    1 
 fs/notify/dnotify/dnotify.c      |  449 ++++++++++++++++++++++++++++++--------
 include/linux/dnotify.h          |   29 +-
 include/linux/fs.h               |    5 
 include/linux/fsnotify.h         |   70 ++----
 include/linux/fsnotify_backend.h |    3 
 7 files changed, 386 insertions(+), 173 deletions(-)
MAINTAINERS +2 -0
index e8bbc45..daba68f 100644
@@ -1413,6 +1413,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
index 26adf5d..904ff8d 100644
@@ -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 +350 -99

525 lines not on this page · raw message

include/linux/dnotify.h +8 -21
index 102a902..e8c4256 100644
@@ -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
index c5ec88f..1eed69c 100644
@@ -695,11 +695,6 @@ struct inode {
 	struct list_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
index ad7294f..2bada0f 100644
@@ -77,6 +77,9 @@
 #define FSNOTIFY_LAST_DENTRY	1
 #define FSNOTIFY_INODE_DESTROY	2
 
+/* 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;
author
#7 lore raw

PATCH 6/11 fsnotify: generic notification queue and waitq

In reply to Eric Paris

inotify 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             |    3 +
 fs/notify/group.c                |    9 ++
 fs/notify/notification.c         |  181 +++++++++++++++++++++++++++++++++++++-
 include/linux/fsnotify_backend.h |   34 +++++++
 4 files changed, 223 insertions(+), 4 deletions(-)
fs/notify/fsnotify.h +3 -0
index bad4da9..6d6942c 100644
@@ -15,8 +15,11 @@ extern struct srcu_struct fsnotify_grp_srcu_struct;
 extern struct list_head fsnotify_groups;
 extern __u64 fsnotify_mask;
 
+extern void fsnotify_flush_notif(struct fsnotify_group *group);
+
 extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is);
 
 extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group);
 extern void fsnotify_clear_marks_by_inode(struct inode *inode, unsigned int flags);
+
 #endif	/* _LINUX_FSNOTIFY_PRIVATE_H */
fs/notify/group.c +9 -0
index bac7d8d..9168e81 100644
@@ -91,6 +91,9 @@ void fsnotify_get_group(struct fsnotify_group *group)
 
 static void fsnotify_destroy_group(struct fsnotify_group *group)
 {
+	/* clear the notification queue of all events */
+	fsnotify_flush_notif(group);
+
 	/* clear all inode mark entries for this group */
 	fsnotify_clear_marks_by_group(group);
 
@@ -172,6 +175,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 +177 -4

218 lines not on this page · raw message

include/linux/fsnotify_backend.h +34 -0
index 2bada0f..6223efa 100644
@@ -69,6 +69,7 @@
 				 FS_DELETE)
 
 /* 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
@@ -101,6 +102,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 */
@@ -113,6 +121,21 @@ 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;
+};
+
 struct fsnotify_event_private_data {
 	struct fsnotify_group *group;
 	struct list_head event_list;
@@ -125,6 +148,12 @@ struct fsnotify_event_private_data {
  * 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;
 	/*
@@ -183,6 +212,11 @@ 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, 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 void fsnotify_recalc_inode_mask(struct inode *inode);
 extern void fsnotify_init_mark(struct fsnotify_mark_entry *entry, struct fsnotify_group *group, struct inode *inode, __u64 mask);
 extern struct fsnotify_mark_entry *fsnotify_find_mark_entry(struct fsnotify_group *group, struct inode *inode);
author
#8 lore raw

PATCH 7/11 fsnotify: include pathnames with entries when possible

In reply to Eric Paris

When 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             |    5 ++++-
 fs/notify/notification.c         |   18 +++++++++++++++++-
 include/linux/fsnotify.h         |   32 ++++++++++++++++----------------
 include/linux/fsnotify_backend.h |    7 +++++--
 5 files changed, 44 insertions(+), 22 deletions(-)
fs/notify/fsnotify.c +2 -2
index e7e53f7..2aa437b 100644
@@ -34,7 +34,7 @@ void __fsnotify_inode_delete(struct inode *inode, int flag)
 }
 EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);
 
-void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is)
+void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *file_name)
 {
 	struct fsnotify_group *group;
 	struct fsnotify_event *event = NULL;
@@ -66,7 +66,7 @@ void fsnotify(struct inode *to_tell, __u64 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, lets hope something else blows up */
 				if (!event)
 					break;
fs/notify/fsnotify.h +4 -1
index 6d6942c..72e163e 100644
@@ -17,9 +17,12 @@ extern __u64 fsnotify_mask;
 
 extern void fsnotify_flush_notif(struct fsnotify_group *group);
 
-extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is);
+extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name);
 
 extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group);
 extern void fsnotify_clear_marks_by_inode(struct inode *inode, unsigned int flags);
 
+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 +17 -1
index 3884879..21a8b03 100644
@@ -62,6 +62,7 @@ void fsnotify_put_event(struct fsnotify_event *event)
 		event->mask = 0;
 
 		BUG_ON(!list_empty(&event->private_data_list));
+		kfree(event->file_name);
 		kmem_cache_free(event_kmem_cache, event);
 	}
 }
@@ -248,9 +249,12 @@ static void initialize_event(struct fsnotify_event *event)
 	INIT_LIST_HEAD(&event->private_data_list);
 
 	event->to_tell = NULL;
+
+	event->file_name = NULL;
+	event->name_len = 0;
 }
 
-struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is)
+struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name)
 {
 	struct fsnotify_event *event;
 
@@ -259,6 +263,15 @@ struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 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_is) {
@@ -284,6 +297,9 @@ struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask,
 		event->flag = FSNOTIFY_EVENT_INODE;
 		break;
 	default:
+		event->path.dentry = NULL;
+		event->path.mnt = NULL;
+		event->inode = NULL;
 		BUG();
 	};
 
include/linux/fsnotify.h +16 -16
index f5ce85b..3081d86 100644
@@ -107,7 +107,7 @@ static inline void fsnotify_parent(struct dentry *dentry, __u64 orig_mask)
 	if (p_inode && (p_inode->i_fsnotify_mask & mask)) {
 		dget(parent);
 		spin_unlock(&dentry->d_lock);
-		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);
 	} else {
 		spin_unlock(&dentry->d_lock);
@@ -161,18 +161,18 @@ 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) {
 		inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
 		inotify_inode_is_dead(target);
-		fsnotify(target, FS_DELETE, target, FSNOTIFY_EVENT_INODE);
+		fsnotify(target, FS_DELETE, target, FSNOTIFY_EVENT_INODE, NULL);
 	}
 
 	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);
 }
@@ -205,7 +205,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, FSNOTIFY_LAST_DENTRY);
 }
 
@@ -216,7 +216,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);
 }
 
 /*
@@ -228,7 +228,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);
 }
 
 /*
@@ -243,7 +243,7 @@ static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct
 	fsnotify_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);
 }
 
 /*
@@ -255,7 +255,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);
 }
 
 /*
@@ -273,7 +273,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);
 }
 
 /*
@@ -291,7 +291,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);
 }
 
 /*
@@ -309,7 +309,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);
 }
 
 /*
@@ -330,7 +330,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);
 }
 
 /*
@@ -348,7 +348,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);
 }
 
 /*
@@ -386,7 +386,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 +5 -2
index 6223efa..b86b4ef 100644
@@ -168,6 +168,9 @@ struct fsnotify_event {
 	atomic_t refcnt;	/* how many groups still are using/need to send this event */
 	__u64 mask;		/* the type of access */
 
+	char *file_name;
+	size_t name_len;
+
 	struct list_head private_data_list;
 };
 
@@ -198,7 +201,7 @@ struct fsnotify_mark_entry {
 #ifdef CONFIG_FSNOTIFY
 
 /* called from the vfs to signal fs events */
-extern void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is);
+extern void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name);
 extern void __fsnotify_inode_delete(struct inode *inode, int flag);
 
 /* called from fsnotify interfaces, such as fanotify or dnotify */
@@ -226,7 +229,7 @@ extern void fsnotify_get_mark(struct fsnotify_mark_entry *entry);
 extern void fsnotify_put_mark(struct fsnotify_mark_entry *entry);
 #else
 
-static inline void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is);
+static inline void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name);
 {}
 
 static inline void __fsnotify_inode_delete(struct inode *inode, int flag)
author
#9 lore raw

PATCH 8/11 fsnotify: add correlations between events

In reply to Eric Paris

inotify 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/fsnotify.h             |    2 +-
 fs/notify/notification.c         |   14 +++++++++++++-
 include/linux/fsnotify.h         |   39 +++++++++++++++++++-------------------
 include/linux/fsnotify_backend.h |   11 +++++++++--
 5 files changed, 45 insertions(+), 25 deletions(-)
fs/notify/fsnotify.c +2 -2
index 2aa437b..8b816c0 100644
@@ -34,7 +34,7 @@ void __fsnotify_inode_delete(struct inode *inode, int flag)
 }
 EXPORT_SYMBOL_GPL(__fsnotify_inode_delete);
 
-void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *file_name)
+void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *file_name, u32 cookie)
 {
 	struct fsnotify_group *group;
 	struct fsnotify_event *event = NULL;
@@ -66,7 +66,7 @@ void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const
 			if (!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, lets hope something else blows up */
 				if (!event)
 					break;
fs/notify/fsnotify.h +1 -1
index 72e163e..b384fa8 100644
@@ -17,7 +17,7 @@ extern __u64 fsnotify_mask;
 
 extern void fsnotify_flush_notif(struct fsnotify_group *group);
 
-extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name);
+extern struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name, u32 cookie);
 
 extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group);
 extern void fsnotify_clear_marks_by_inode(struct inode *inode, unsigned int flags);
fs/notify/notification.c +13 -1
index 21a8b03..a636a1f 100644
@@ -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>
@@ -35,6 +36,13 @@
 static struct kmem_cache *event_kmem_cache;
 static struct kmem_cache *event_holder_kmem_cache;
 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)
@@ -252,9 +260,11 @@ static void initialize_event(struct fsnotify_event *event)
 
 	event->file_name = NULL;
 	event->name_len = 0;
+
+	event->sync_cookie = 0;
 }
 
-struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name)
+struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name, u32 cookie)
 {
 	struct fsnotify_event *event;
 
@@ -272,6 +282,8 @@ struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u64 mask,
 		}
 		event->name_len = strlen(event->file_name);
 	}
+
+	event->sync_cookie = cookie;
 	event->to_tell = to_tell;
 
 	switch (data_is) {
include/linux/fsnotify.h +20 -19
index 3081d86..bdbc897 100644
@@ -107,7 +107,7 @@ static inline void fsnotify_parent(struct dentry *dentry, __u64 orig_mask)
 	if (p_inode && (p_inode->i_fsnotify_mask & mask)) {
 		dget(parent);
 		spin_unlock(&dentry->d_lock);
-		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);
 	} else {
 		spin_unlock(&dentry->d_lock);
@@ -139,7 +139,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();
 	__u64 old_dir_mask = 0;
 	__u64 new_dir_mask = 0;
 
@@ -156,23 +157,23 @@ 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) {
 		inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
 		inotify_inode_is_dead(target);
-		fsnotify(target, FS_DELETE, target, FSNOTIFY_EVENT_INODE, NULL);
+		fsnotify(target, FS_DELETE, target, FSNOTIFY_EVENT_INODE, NULL, 0);
 	}
 
 	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);
 }
@@ -205,7 +206,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, FSNOTIFY_LAST_DENTRY);
 }
 
@@ -216,7 +217,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);
 }
 
 /*
@@ -228,7 +229,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);
 }
 
 /*
@@ -243,7 +244,7 @@ static inline void fsnotify_link(struct inode *dir, struct inode *inode, struct
 	fsnotify_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);
 }
 
 /*
@@ -255,7 +256,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);
 }
 
 /*
@@ -273,7 +274,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);
 }
 
 /*
@@ -291,7 +292,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);
 }
 
 /*
@@ -309,7 +310,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);
 }
 
 /*
@@ -330,7 +331,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);
 }
 
 /*
@@ -348,7 +349,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);
 }
 
 /*
@@ -386,7 +387,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 +9 -2
index b86b4ef..c86795d 100644
@@ -168,6 +168,7 @@ struct fsnotify_event {
 	atomic_t refcnt;	/* how many groups still are using/need to send this event */
 	__u64 mask;		/* the type of access */
 
+	u32 sync_cookie;	/* used to corrolate events, namely inotify mv events */
 	char *file_name;
 	size_t name_len;
 
@@ -201,8 +202,9 @@ struct fsnotify_mark_entry {
 #ifdef CONFIG_FSNOTIFY
 
 /* called from the vfs to signal fs events */
-extern void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name);
+extern void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name, u32 cookie);
 extern void __fsnotify_inode_delete(struct inode *inode, int flag);
+extern u32 fsnotify_get_cookie(void);
 
 /* called from fsnotify interfaces, such as fanotify or dnotify */
 extern void fsnotify_recalc_global_mask(void);
@@ -229,12 +231,17 @@ extern void fsnotify_get_mark(struct fsnotify_mark_entry *entry);
 extern void fsnotify_put_mark(struct fsnotify_mark_entry *entry);
 #else
 
-static inline void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name);
+static inline void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name, u32 cookie);
 {}
 
 static inline void __fsnotify_inode_delete(struct inode *inode, int flag)
 {}
 
+static inline u32 fsnotify_get_cookie(void)
+{
+	return 0;
+}
+
 #endif	/* CONFIG_FSNOTIFY */
 
 #endif	/* __KERNEL __ */
author
#10 lore raw

PATCH 9/11 fsnotify: fsnotify marks on inodes pin them in core

In reply to Eric Paris

This 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 |   25 ++++++++++++++++++++-----
 1 files changed, 20 insertions(+), 5 deletions(-)
fs/notify/inode_mark.c +20 -5
index 840bd91..ff65e62 100644
@@ -108,13 +108,16 @@ void fsnotify_clear_marks_by_group(struct fsnotify_group *group)
 		spin_lock(&inode->i_lock);
 
 		list_del_init(&entry->i_list);
-		entry->inode = NULL;
 		list_del_init(&entry->g_list);
-		entry->group = NULL;
-		entry->freeme = 1;
 
 		fsnotify_recalc_inode_mask_locked(inode);
 		spin_unlock(&inode->i_lock);
+
+		entry->group = NULL;
+		entry->freeme = 1;
+		entry->inode = NULL;
+		iput(inode);
+
 		spin_unlock(&entry->lock);
 
 		fsnotify_put_mark(entry);
@@ -137,14 +140,17 @@ void fsnotify_destroy_mark_by_entry(struct fsnotify_mark_entry *entry)
 		spin_lock(&inode->i_lock);
 
 	list_del_init(&entry->i_list);
-	entry->inode = NULL;
 	list_del_init(&entry->g_list);
+
+	entry->inode = NULL;
 	entry->group = NULL;
 	entry->freeme = 1;
 
 	if (inode) {
 		fsnotify_recalc_inode_mask_locked(inode);
 		spin_unlock(&inode->i_lock);
+
+		iput(inode);
 	}
 	if (group)
 		spin_unlock(&group->mark_lock);
@@ -173,6 +179,11 @@ void fsnotify_clear_marks_by_inode(struct inode *inode, unsigned int flags)
 	 * try to grab entry->inode->i_lock without a problem.
 	 */
 	list_for_each_entry_safe(entry, lentry, &free_list, free_i_list) {
+		spin_lock(&entry->lock);
+		if (entry->inode)
+			iput(entry->inode);
+		entry->inode = NULL;
+		spin_unlock(&entry->lock);
 		entry->group->ops->mark_clear_inode(entry, inode, flags);
 		fsnotify_put_mark(entry);
 	}
@@ -209,9 +220,13 @@ int fsnotify_add_mark(struct fsnotify_mark_entry *entry)
 {
 	struct fsnotify_mark_entry *lentry;
 	struct fsnotify_group *group = entry->group;
-	struct inode *inode = entry->inode;
+	struct inode *inode;
 	int ret = 0;
 
+	inode = igrab(entry->inode);
+	if (unlikely(!inode))
+		return -EINVAL;
+
 	/*
 	 * LOCKING ORDER!!!!
 	 * entry->lock
author
#11 lore raw

PATCH 10/11 fsnotify: handle filesystem unmounts with fsnotify marks

In reply to Eric Paris

When 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 |    5 +++
 3 files changed, 79 insertions(+), 0 deletions(-)
fs/inode.c +1 -0
index f6a51a1..bec51e3 100644
@@ -405,6 +405,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 +5 -0
index c86795d..203842c 100644
@@ -229,6 +229,8 @@ extern int fsnotify_add_mark(struct fsnotify_mark_entry *entry);
 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);
 #else
 
 static inline void fsnotify(struct inode *to_tell, __u64 mask, void *data, int data_is, const char *name, u32 cookie);
@@ -242,6 +244,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 __ */
author
#12 lore raw

PATCH 11/11 inotify: reimplement inotify using fsnotify

In reply to Eric Paris

Yes, holy shit, I'm trying to reimplement inotify as fsnotify...

---

 MAINTAINERS                          |    2 
 fs/notify/inotify/Kconfig            |   20 +
 fs/notify/inotify/Makefile           |    2 
 fs/notify/inotify/inotify.h          |  108 ++++++
 fs/notify/inotify/inotify_fsnotify.c |  176 ++++++++++
 fs/notify/inotify/inotify_kernel.c   |  236 +++++++++++++
 fs/notify/inotify/inotify_user.c     |  598 +++++++++-------------------------
 fs/notify/notification.c             |   30 +-
 include/linux/fsnotify.h             |   39 +-
 include/linux/fsnotify_backend.h     |   11 +
 10 files changed, 744 insertions(+), 478 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
index daba68f..0bc3f24 100644
@@ -2243,6 +2243,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/inotify/Kconfig +12 -8
index 4467928..5356884 100644
@@ -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
index e290f3b..aff7f68 100644
@@ -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 +108 -0

108 lines not on this page · raw message

fs/notify/inotify/inotify_fsnotify.c new file +176 -0

176 lines not on this page · raw message

fs/notify/inotify/inotify_kernel.c new file +236 -0

236 lines not on this page · raw message

fs/notify/inotify/inotify_user.c +157 -441

794 lines not on this page · raw message

fs/notify/notification.c +22 -8
index a636a1f..14af58d 100644
@@ -123,7 +123,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;
@@ -141,21 +141,35 @@ int fsnotify_add_notif_event(struct fsnotify_group *group, struct fsnotify_event
 	 * the holder spinlock.  If we see it blank we know that once we
 	 * get that lock the in event 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);
 
-	if (group->q_len >= group->max_events)
+	if (group->q_len >= group->max_events) 
 		event = &q_overflow_event;
 
 	spin_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 +19 -20
index bdbc897..e60c943 100644
@@ -265,10 +265,10 @@ 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;
-	__u64 mask = IN_ACCESS;
+	__u64 mask = FS_ACCESS;
 
 	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);
@@ -283,10 +283,10 @@ static inline void fsnotify_access(struct dentry *dentry)
 static inline void fsnotify_modify(struct dentry *dentry)
 {
 	struct inode *inode = dentry->d_inode;
-	__u64 mask = IN_MODIFY;
+	__u64 mask = FS_MODIFY;
 
 	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);
@@ -301,10 +301,10 @@ static inline void fsnotify_modify(struct dentry *dentry)
 static inline void fsnotify_open(struct dentry *dentry)
 {
 	struct inode *inode = dentry->d_inode;
-	__u64 mask = IN_OPEN;
+	__u64 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);
@@ -320,14 +320,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;
-	__u64 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
+	__u64 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_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
 	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
 
 	fsnotify_parent(dentry, mask);
@@ -340,10 +339,10 @@ static inline void fsnotify_close(struct file *file)
 static inline void fsnotify_xattr(struct dentry *dentry)
 {
 	struct inode *inode = dentry->d_inode;
-	__u64 mask = IN_ATTRIB;
+	__u64 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);
@@ -362,26 +361,26 @@ static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
 	__u64 mask = 0;
 
 	if (ia_valid & ATTR_UID)
-		mask |= IN_ATTRIB;
+		mask |= FS_ATTRIB;
 	if (ia_valid & ATTR_GID)
-		mask |= IN_ATTRIB;
+		mask |= FS_ATTRIB;
 	if (ia_valid & ATTR_SIZE)
-		mask |= IN_MODIFY;
+		mask |= FS_MODIFY;
 
 	/* both times implies a utime(s) call */
 	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
-		mask |= IN_ATTRIB;
+		mask |= FS_ATTRIB;
 	else if (ia_valid & ATTR_ATIME)
-		mask |= IN_ACCESS;
+		mask |= FS_ACCESS;
 	else if (ia_valid & ATTR_MTIME)
-		mask |= IN_MODIFY;
+		mask |= FS_MODIFY;
 
 	if (ia_valid & ATTR_MODE)
-		mask |= IN_ATTRIB;
+		mask |= FS_ATTRIB;
 
 	if (mask) {
 		if (S_ISDIR(inode->i_mode))
-			mask |= IN_ISDIR;
+			mask |= FS_IN_ISDIR;
 		inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
 		inotify_dentry_parent_queue_event(dentry, mask, 0,
 						  dentry->d_name.name);
include/linux/fsnotify_backend.h +11 -0
index 203842c..0070cc3 100644
@@ -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 */
@@ -80,6 +81,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;
@@ -118,6 +120,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
 	};
 };
 
author
#13 lore raw

cover fsnotify: unified filesystem notification backend

In reply to Eric Paris

Whoops, forgot the cover note. So proceeding is an implementation of fsnotify, a novel unified filesystem notification backend. This patch set implements the backend and converts both dnotify and inotify to this backend. This patch set reduced the size of struct inode if one uses both dnotify and inotify.

I have patches which move the audit functionality which uses inotify to fsnotify and I have patches which implement fanotify, my new on access notification and access decisions to this backend, but just to keep the series small I'm only sending fsnotify, dnotify and inotify changes.

I would love the most careful and scrutinizing eyes on patches 3, 9, 10 which implement the changes to inodes and introduce by far the most complex locking, refcnt, and object lifetimes.

dnotify changes are in patch 5
inotify changes are in patch 11

 Documentation/filesystems/fsnotify.txt |  192 ++++++++++
 MAINTAINERS                            |    4 
 fs/inode.c                             |   10 
 fs/notify/Kconfig                      |   13 
 fs/notify/Makefile                     |    2 
 fs/notify/dnotify/Kconfig              |    1 
 fs/notify/dnotify/dnotify.c            |  449 +++++++++++++++++++-----
 fs/notify/fsnotify.c                   |   91 +++++
 fs/notify/fsnotify.h                   |   28 +
 fs/notify/group.c                      |  207 +++++++++++
 fs/notify/inode_mark.c                 |  330 ++++++++++++++++++
 fs/notify/inotify/Kconfig              |   20 -
 fs/notify/inotify/Makefile             |    2 
 fs/notify/inotify/inotify.h            |  108 +++++
 fs/notify/inotify/inotify_fsnotify.c   |  176 +++++++++
 fs/notify/inotify/inotify_kernel.c     |  236 +++++++++++++
 fs/notify/inotify/inotify_user.c       |  598 ++++++++-------------------------
 fs/notify/notification.c               |  348 +++++++++++++++++++
 include/linux/dcache.h                 |    3 
 include/linux/dnotify.h                |   29 -
 include/linux/fs.h                     |    6 
 include/linux/fsnotify.h               |  255 ++++++++++----
 include/linux/fsnotify_backend.h       |  265 ++++++++++++++
 23 files changed, 2735 insertions(+), 638 deletions(-)