Skip to contentSkip to conversation

Connector: Filesystem Events Connector

PATCH In review Yi Yang 2 participants 3 messages Mar 15, 2006 – Mar 16, 2006

Subsystems (2)
lore
Share & tools
lore

Yi Yang

author
#0 lore raw

PATCH 1/1 Connector: Filesystem Events Connector

This patch implements a new connector, Filesystem Event Connector,
the user can monitor filesystem activities via it, currently, it can monitor access, attribute change, open, create, modify, delete, move and close of any file or directory.

Every filesystem event will include tgid, uid and gid of the process
which triggered this event, process name, file or directory name
operated by it.

 drivers/connector/Kconfig  |    8 +
 drivers/connector/Makefile |    1
 drivers/connector/cn_fs.c  |  204+++++++++++++++++++++++++++++++++++++++++++++ 
 include/linux/connector.h  |    2
 include/linux/fsevent.h    |   82 ++++++++++++++++++
 include/linux/fsnotify.h   |   56 +++++++++++-
 6 files changed, 348 insertions(+), 5 deletions(-)
include/linux/connector.h renamed from include/linux/connector.h.orig +2 -0
@@ -34,6 +34,8 @@
 #define CN_VAL_PROC			0x1
 #define CN_IDX_CIFS			0x2
 #define CN_VAL_CIFS                     0x1
+#define CN_IDX_FS			0x3
+#define CN_VAL_FS			0x1
 
 #define CN_NETLINK_USERS		1
 
include/linux/fsnotify.h renamed from include/linux/fsnotify.h.orig +51 -5

189 lines not on this page · raw message

drivers/connector/Makefile renamed from drivers/connector/Makefile.orig +1 -0
@@ -1,4 +1,5 @@
 obj-$(CONFIG_CONNECTOR)		+= cn.o
 obj-$(CONFIG_PROC_EVENTS)	+= cn_proc.o
+obj-$(CONFIG_FS_EVENTS)	+= cn_fs.o
 
 cn-y				+= cn_queue.o connector.o
drivers/connector/Kconfig renamed from drivers/connector/Kconfig.orig +8 -0
@@ -18,4 +18,12 @@ config PROC_EVENTS
 	  Provide a connector that reports process events to userspace. Send
 	  events such as fork, exec, id change (uid, gid, suid, etc), and exit.
 
+config FS_EVENTS
+	boolean "Report filesystem events to userspace"
+	depends on CONNECTOR=y
+	default y
+	---help---
+	  Provide a connector that reports filesystem events to userspace. The
+	  reported event include open, read, write.
+
 endmenu
include/linux/fsevent.h new file +82 -0

82 lines not on this page · raw message

drivers/connector/cn_fs.c new file +204 -0

204 lines not on this page · raw message

#1 lore raw
Quoted 9 lines · Yi Yang

This patch implements a new connector, Filesystem Event Connector,
the user can monitor filesystem activities via it, currently, it can monitor access, attribute change, open, create, modify, delete, move and close of any file or directory.

Every filesystem event will include tgid, uid and gid of the process
which triggered this event, process name, file or directory name
operated by it.

Why does Linux need this feature?

It seems quite duplicative of the fsnotify stuff.

Quoted 23 lines
include/linux/fsnotify.h renamed from include/linux/fsnotify.h.orig +4 -0
 	/* both times implies a utime(s) call */
 	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
 	{
 		in_mask |= IN_ATTRIB;
 		dn_mask |= DN_ATTRIB;
+		fsevent_mask |= FSEVENT_MODIFY_ATTRIB;
 	} else if (ia_valid & ATTR_ATIME) {
 		in_mask |= IN_ACCESS;
 		dn_mask |= DN_ACCESS;
+		fsevent_mask |= FSEVENT_ACCESS;
 	} else if (ia_valid & ATTR_MTIME) {
 		in_mask |= IN_MODIFY;
 		dn_mask |= DN_MODIFY;
+		fsevent_mask |= FSEVENT_MODIFY;
 	}
 	if (ia_valid & ATTR_MODE) {
 		in_mask |= IN_ATTRIB;
 		dn_mask |= DN_ATTRIB;
+		fsevent_mask |= FSEVENT_MODIFY_ATTRIB;
 	}
 
 	if (dn_mask)

include/linux/fsnotify.h static inline void fsnotify_change(struc · line 287

Quoted 9 lines
include/linux/fsnotify.h renamed from include/linux/fsnotify.h.orig +5 -0
@@ -238,6 +279,11 @@ static inline void fsnotify_change(struc
 		inotify_dentry_parent_queue_event(dentry, in_mask, 0,
 						  dentry->d_name.name);
 	}
+	if (fsevent_mask) {
+		if (S_ISDIR(inode->i_mode))
+			fsevent_mask |= FSEVENT_ISDIR;
+		raise_fsevent(dentry, fsevent_mask);
+	}
 }

hmm, I wonder if the compiler is smart enough to recognise that fsevent_mask isn't used and to eliminate all this new code when CONFIG_FS_EVENTS=n

include/linux/fsevent.h line 69

Quoted 1 line
include/linux/fsevent.h new file +1 -0
+extern void raise_fsevent_move(struct inode * olddir, const char * oldname, struct inode * newdir, const char * newname, u32 mask);

Try to keep code looking nice in an 80-col display please.

Quoted 4 lines
drivers/connector/cn_fs.c new file +3 -0
+int __raise_fsevent(const char * oldname, const char * newname, u32 mask)
+{
+	struct cn_msg *msg;

drivers/connector/cn_fs.c line 59

Quoted 10 lines
drivers/connector/cn_fs.c new file +10 -0
+	struct fsevent *event;
+	__u8 * buffer;
+	int namelen = 0;
+	static unsigned long last;
+	static int fsevent_sum = 0;
+
+	if (atomic_read(&cn_fs_event_listeners) < 1)
+		return 0;
+
+	spin_lock(&fsevent_ratelimit_lock);

yipes. We just went through considerable pain fixing performance problems with inotify, and this patch brings them back, only much much worse.

See ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.16-rc6/2.6.16-rc6-mm1/broken-out/inotify-lock-avoidance-with-parent-watch-status-in-dentry.patch

author
#2 lore raw
Quoted 7 lines · Andrew Morton
Quoted 5 lines · Yi Yang

This patch implements a new connector, Filesystem Event Connector,
the user can monitor filesystem activities via it, currently, it can monitor access, attribute change, open, create, modify, delete, move and close of any file or directory.

Quoted 9 lines
Quoted 4 lines

Every filesystem event will include tgid, uid and gid of the process
which triggered this event, process name, file or directory name
operated by it.

Why does Linux need this feature?

It seems quite duplicative of the fsnotify stuff.

inotify just focuses on changes in filesystem, it can't tell us who is doing, moreover, it addes several system calls, I think its API is hard to use, but filesystem connector focuses on who is doing and what is done, it uses netlink API, so for user application, it is easy to use, to be most important, many event mechanisms in kernel are implemented by netlink, for example, KOBJECT_UEVENT, PROC_EVENTS , if all the event mechanisms use netlink, it is very easy to unify user space APIs.

But current situation is that inotify used a new mechanism, although that can be done by netlink.

Quoted 22 lines
Quoted 22 lines
include/linux/fsnotify.h renamed from include/linux/fsnotify.h.orig +4 -0
 	/* both times implies a utime(s) call */
 	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
 	{
 		in_mask |= IN_ATTRIB;
 		dn_mask |= DN_ATTRIB;
+		fsevent_mask |= FSEVENT_MODIFY_ATTRIB;
 	} else if (ia_valid & ATTR_ATIME) {
 		in_mask |= IN_ACCESS;
 		dn_mask |= DN_ACCESS;
+		fsevent_mask |= FSEVENT_ACCESS;
 	} else if (ia_valid & ATTR_MTIME) {
 		in_mask |= IN_MODIFY;
 		dn_mask |= DN_MODIFY;
+		fsevent_mask |= FSEVENT_MODIFY;
 	}
 	if (ia_valid & ATTR_MODE) {
 		in_mask |= IN_ATTRIB;
 		dn_mask |= DN_ATTRIB;
+		fsevent_mask |= FSEVENT_MODIFY_ATTRIB;
 	}
Quoted 17 lines

include/linux/fsnotify.h static inline void fsnotify_change(struc · line 288

Quoted 12 lines
include/linux/fsnotify.h renamed from include/linux/fsnotify.h.orig +5 -0
 	if (dn_mask)
@@ -238,6 +279,11 @@ static inline void fsnotify_change(struc
 		inotify_dentry_parent_queue_event(dentry, in_mask, 0,
 						  dentry->d_name.name);
 	}
+	if (fsevent_mask) {
+		if (S_ISDIR(inode->i_mode))
+			fsevent_mask |= FSEVENT_ISDIR;
+		raise_fsevent(dentry, fsevent_mask);
+	}
 }
 

hmm, I wonder if the compiler is smart enough to recognise that fsevent_mask isn't used and to eliminate all this new code when CONFIG_FS_EVENTS=n

Sorry, this is my mistake, I'll upload a new version.

Quoted 6 lines

include/linux/fsevent.h line 69

Quoted 3 lines
include/linux/fsevent.h new file +1 -0
+extern void raise_fsevent_move(struct inode * olddir, const char * oldname, struct inode * newdir, const char * newname, u32 mask);

Try to keep code looking nice in an 80-col display please.

OK.

Quoted 12 lines
Quoted 12 lines
drivers/connector/cn_fs.c new file +10 -0
+int __raise_fsevent(const char * oldname, const char * newname, u32 mask)
+{
+	struct cn_msg *msg;
+	struct fsevent *event;
+	__u8 * buffer;
+	int namelen = 0;
+	static unsigned long last;
+	static int fsevent_sum = 0;
+
+	if (atomic_read(&cn_fs_event_listeners) < 1)
Quoted 9 lines

drivers/connector/cn_fs.c line 59

Quoted 5 lines
drivers/connector/cn_fs.c new file +3 -0
+		return 0;
+
+	spin_lock(&fsevent_ratelimit_lock);

yipes. We just went through considerable pain fixing performance problems with inotify, and this patch brings them back, only much much worse.

fsevent will have lower overhead than inotify, because it don't need any comparison to decide whether this inode is watched, and it don't need generate a event for this inode's parent. I will consider to how to remove lock.

Quoted 3 lines

See ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.16-rc6/2.6.16-rc6-mm1/broken-out/inotify-lock-avoidance-with-parent-watch-status-in-dentry.patch