Skip to contentSkip to conversation

Connector: Filesystem Events Connector try 2

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

Subsystems (2)
lore
Share & tools
lore

Yi Yang

author
#0 lore raw

PATCH 1/1 Connector: Filesystem Events Connector try 2

This new patch is update for last patch, it removes spinlock and makes include/linux/fsnotify.h more clean when CONFIG_FS_EVENTS=n, it also reformats some too long lines so that they are less than 80 columns.

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.

Filesystem events connector is never a duplicate of inotify, inotify
just concerns change on file or directory, Beagle uses it to watch file changes in order to regenerate index for it, inotify can't tell us who did that change and what is its process name, but filesystem events connector can do these, moreover inotify's overhead is greater than filesystem events connector, inotify needs compare inode with
watched file or directories list to decide whether it should generate an
inotify_event, some locks also increase overhead, filesystem event
connector hasn't these overhead, it just generates a fsevent and send.

To be important, filesystem event connector doesn't add any new system call, the user space application can make use of it by netlink socket, but inotify added several system calls, many events mechanism in kernel
have used netlink as communication way with user space, for example,
KOBJECT_UEVENT, PROC_EVENTS, to use netlink will make it more possible
to unify events interface to netlink, the user space application can use
it very easy.

drivers/connector/Kconfig  |    8 +
drivers/connector/Makefile |    1
drivers/connector/cn_fs.c  |  200 +++++++++++++++++++++++++++++++++++++++++++++
include/linux/connector.h  |    2
include/linux/fsevent.h    |   86 +++++++++++++++++++
include/linux/fsnotify.h   |   36 ++++++++
6 files changed, 333 insertions(+)
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 +36 -0
@@ -16,6 +16,7 @@
 #include <linux/dnotify.h>
 #include <linux/inotify.h>
 #include <linux/audit.h>
+#include <linux/fsevent.h>
 
 /*
  * fsnotify_d_instantiate - instantiate a dentry for inode
@@ -67,6 +68,8 @@ static inline void fsnotify_move(struct 
 	}
 	audit_inode_child(old_name, source, old_dir->i_ino);
 	audit_inode_child(new_name, target, new_dir->i_ino);
+	raise_fsevent_move(old_dir, old_name, new_dir, new_name, 
+			   FSEVENT_MOVE | (isdir?FSEVENT_ISDIR:0));
 }
 
 /*
@@ -78,6 +81,8 @@ static inline void fsnotify_nameremove(s
 		isdir = IN_ISDIR;
 	dnotify_parent(dentry, DN_DELETE);
 	inotify_dentry_parent_queue_event(dentry, IN_DELETE|isdir, 0, dentry->d_name.name);
+	raise_fsevent(dentry, 
+		      FSEVENT_DELETE | (isdir?FSEVENT_ISDIR:0));
 }
 
 /*
@@ -97,6 +102,7 @@ static inline void fsnotify_create(struc
 	inode_dir_notify(inode, DN_CREATE);
 	inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name);
 	audit_inode_child(dentry->d_name.name, dentry->d_inode, inode->i_ino);
+	raise_fsevent_create(inode, dentry->d_name.name, FSEVENT_CREATE);
 }
 
 /*
@@ -108,6 +114,8 @@ static inline void fsnotify_mkdir(struct
 	inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0, 
 				  dentry->d_name.name);
 	audit_inode_child(dentry->d_name.name, dentry->d_inode, inode->i_ino);
+	raise_fsevent_create(inode, dentry->d_name.name, 
+			     FSEVENT_CREATE | FSEVENT_ISDIR);
 }
 
 /*
@@ -124,6 +132,8 @@ static inline void fsnotify_access(struc
 	dnotify_parent(dentry, DN_ACCESS);
 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
 	inotify_inode_queue_event(inode, mask, 0, NULL);
+	raise_fsevent(dentry, FSEVENT_ACCESS | 
+			      ((S_ISDIR(inode->i_mode))?FSEVENT_ISDIR:0));
 }
 
 /*
@@ -140,6 +150,8 @@ static inline void fsnotify_modify(struc
 	dnotify_parent(dentry, DN_MODIFY);
 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
 	inotify_inode_queue_event(inode, mask, 0, NULL);
+	raise_fsevent(dentry, FSEVENT_MODIFY |
+			      ((S_ISDIR(inode->i_mode))?FSEVENT_ISDIR:0));
 }
 
 /*
@@ -155,6 +167,8 @@ static inline void fsnotify_open(struct 
 
 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
 	inotify_inode_queue_event(inode, mask, 0, NULL);	
+	raise_fsevent(dentry, FSEVENT_OPEN |
+			      ((S_ISDIR(inode->i_mode))?FSEVENT_ISDIR:0));
 }
 
 /*
@@ -173,6 +187,8 @@ static inline void fsnotify_close(struct
 
 	inotify_dentry_parent_queue_event(dentry, mask, 0, name);
 	inotify_inode_queue_event(inode, mask, 0, NULL);
+	raise_fsevent(dentry, FSEVENT_CLOSE |
+			      ((S_ISDIR(inode->i_mode))?FSEVENT_ISDIR:0));
 }
 
 /*
@@ -188,6 +204,8 @@ static inline void fsnotify_xattr(struct
 
 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
 	inotify_inode_queue_event(inode, mask, 0, NULL);
+	raise_fsevent(dentry, FSEVENT_MODIFY_ATTRIB |
+			      ((S_ISDIR(inode->i_mode))?FSEVENT_ISDIR:0));
 }
 
 /*
@@ -238,6 +256,24 @@ static inline void fsnotify_change(struc
 		inotify_dentry_parent_queue_event(dentry, in_mask, 0,
 						  dentry->d_name.name);
 	}
+
+#ifdef CONFIG_FS_EVENTS
+	{
+	u32 fsevent_mask = 0;
+	if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
+		fsevent_mask |= FSEVENT_MODIFY_ATTRIB;
+	if ((ia_valid & ATTR_ATIME) && (ia_valid & ATTR_MTIME))
+		fsevent_mask |= FSEVENT_MODIFY_ATTRIB;
+	else if (ia_valid & ATTR_ATIME)
+		fsevent_mask |= FSEVENT_ACCESS;
+	else if (ia_valid & ATTR_MTIME)
+		fsevent_mask |= FSEVENT_MODIFY;
+	if (ia_valid & ATTR_SIZE)
+		fsevent_mask |= FSEVENT_MODIFY;
+	if (fsevent_mask)
+		raise_fsevent(dentry, fsevent_mask);
+	}
+#endif /* CONFIG_FS_EVENTS */
 }
 
 #ifdef CONFIG_INOTIFY	/* inotify helpers */
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 +86 -0

86 lines not on this page · raw message

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

200 lines not on this page · raw message

#1 lore raw
Quoted 6 lines · Yi Yang

This new patch is update for last patch, it removes spinlock and makes include/linux/fsnotify.h more clean when CONFIG_FS_EVENTS=n, it also reformats some too long lines so that they are less than 80 columns.

Quoted 8 lines

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.

That would seem to have some privacy implications...

I'd expect that all the info which is needed can be obtained via syscall auditing.

I don't recall having seen demand for this feature before. For what reason is it needed? What is the application?

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

This new patch is update for last patch, it removes spinlock and makes include/linux/fsnotify.h more clean when CONFIG_FS_EVENTS=n, it also reformats some too long lines so that they are less than 80 columns.

Quoted 9 lines

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.

Quoted 5 lines

That would seem to have some privacy implications...

I'd expect that all the info which is needed can be obtained via syscall auditing.

Yes, but if enabling syscall audit, all the syscalls will be audited, so every syscall will add overhead, moreover , it will not only send log to klog or system log, but also it will send netlink message.

Filesystem events connector is very simple functionally, it just focuses on filesystem activities. Process Events Connector(cn_proc) is a very typical case.

Quoted 3 lines

I don't recall having seen demand for this feature before. For what reason is it needed? What is the application?

Anti-virus software can use this feature to monitor malign software's activities, foe example, modify system configuration or critical share libraries. Some system administration applications can use to obtain filesystem activities of every user in order to diagnose some system troubles.