Skip to contentSkip to conversation

inotify kernel API

PATCH In review Amy Griffis 6 participants 7 messages May 26, 2006 – May 30, 2006 1 message not in the archive

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 inotify kernel API

This patch introduces a kernel API for inotify, making it possible for kernel modules to benefit from inotify's mechanism for watching inodes. With this patch, inotify will maintain for each caller a list of watches (via an embedded struct inotify_watch), where each inotify_watch is associated with a corresponding struct inode. The caller registers an event handler and specifies for which filesystem events their event handler should be called per inotify_watch.

I am particularly interested in a kernel API for inotify because it allows the audit subsystem to provide a limited form of path-based rules, where a rule's inode data is updated based on changes to its immediate parent's directory entries. Using an inotify API allows audit to take advantage of inotify's hooks, list management, and inode removal handling, instead of duplicating that functionality.

I posted an earlier version of this patch here:

    http://lkml.org/lkml/2006/4/6/96

After stress testing and completing audit patches to use this API, I've made the following changes:

(*) Allow callers to share the refcounting for an inotify_watch.
If the caller has embedded the inotify_watch in one of its own structs, both inotify and the caller may need to use refcounts for that data. Since the caller is ultimately responsible for freeing the inotify_watch data, they must register a destroy function to be called on the last put_inotify_watch. Also provide inotify_init_watch() to enable a caller to use refcounts before calling inotify_add_watch().

(*) Have inotify_find_watch() grab a reference before returning to
caller; caller puts the reference when ready. Since inotify watches are always subject to auto-removal based on filesystem events, this solves the problem of inotify finding an existing watch, only to have it disappear when inotify drops its locks before returning to caller.

In my last post, I was concerned that the locking hierarchy requirement -- locks taken during a caller's event handler cannot be held during other inotify calls -- could cause synchronization problems for kernel consumers. However, the above changes to refcounting seem to alleviate those issues.

(*) Allow callers to remove watches from their event handler.
Audit uses this feature to remove a watch after an IN_MOVE_SELF event. Another similar use could be to have functionality similar to IN_ONESHOT, but have it apply to a subset of events in the mask.

(*) Fixed a deadlock in inotify_dev_queue_event().

(*) Fixed memleaks in inotify_destroy() and with IN_ONESHOT masks.

(*) Re-ordered calls to event handler with IN_IGNORED events.
Since caller may do final put here, this must be the last thing inotify does with an inotify_watch.

I did some stress tests and performance comparisons on inotify with and without this patch. The tests I used and some results are posted here:

    http://free.linux.hp.com/~amg/inotify/

There is more detailed documentation on the inotify kernel API in Documentation/filesystems/inotify.txt as part of this patch and in comments at the top of inotify.c.

--

 Documentation/filesystems/inotify.txt |  130 ++++
 fs/Kconfig                            |   24 
 fs/Makefile                           |    1 
 fs/inotify.c                          |  991 +++++++++-------------------------
 fs/inotify_user.c                     |  719 ++++++++++++++++++++++++
 include/linux/fsnotify.h              |   29 
 include/linux/inotify.h               |  111 +++
 include/linux/sched.h                 |    2 
 kernel/sysctl.c                       |    4 
 kernel/user.c                         |    2 
 10 files changed, 1275 insertions(+), 738 deletions(-)
Documentation/filesystems/inotify.txt +124 -6
index 6d50190..59a919f 100644
@@ -69,17 +69,135 @@ Prototypes:
 	int inotify_rm_watch (int fd, __u32 mask);
 
 
-(iii) Internal Kernel Implementation
+(iii) Kernel Interface
 
-Each inotify instance is associated with an inotify_device structure.
+Inotify's kernel API consists a set of functions for managing watches and an
+event callback.
+
+To use the kernel API, you must first initialize an inotify instance with a set
+of inotify_operations.  You are given an opaque inotify_handle, which you use
+for any further calls to inotify.
+
+    struct inotify_handle *ih = inotify_init(my_event_handler);
+
+You must provide a function for processing events and a function for destroying
+the inotify watch.
+
+    void handle_event(struct inotify_watch *watch, u32 wd, u32 mask,
+    	              u32 cookie, const char *name, struct inode *inode)
+
+	watch - the pointer to the inotify_watch that triggered this call
+	wd - the watch descriptor
+	mask - describes the event that occurred
+	cookie - an identifier for synchronizing events
+	name - the dentry name for affected files in a directory-based event
+	inode - the affected inode in a directory-based event
+
+    void destroy_watch(struct inotify_watch *watch)
+
+You may add watches by providing a pre-allocated and initialized inotify_watch
+structure and specifying the inode to watch along with an inotify event mask.
+You must pin the inode during the call.  You will likely wish to embed the
+inotify_watch structure in a structure of your own which contains other
+information about the watch.  Once you add an inotify watch, it is immediately
+subject to removal depending on filesystem events.  You must grab a reference if
+you depend on the watch hanging around after the call.
+
+    inotify_init_watch(&my_watch->iwatch);
+    inotify_get_watch(&my_watch->iwatch);	// optional
+    s32 wd = inotify_add_watch(ih, &my_watch->iwatch, inode, mask);
+    inotify_put_watch(&my_watch->iwatch);	// optional
+
+You may use the watch descriptor (wd) or the address of the inotify_watch for
+other inotify operations.  You must not directly read or manipulate data in the
+inotify_watch.  Additionally, you must not call inotify_add_watch() more than
+once for a given inotify_watch structure, unless you have first called either
+inotify_rm_watch() or inotify_rm_wd().
+
+To determine if you have already registered a watch for a given inode, you may
+call inotify_find_watch(), which gives you both the wd and the watch pointer for
+the inotify_watch, or an error if the watch does not exist.
+
+    wd = inotify_find_watch(ih, inode, &watchp);
+
+You may use container_of() on the watch pointer to access your own data
+associated with a given watch.  When an existing watch is found,
+inotify_find_watch() bumps the refcount before releasing its locks.  You must
+put that reference with:
+
+    put_inotify_watch(watchp);
+
+Call inotify_find_update_watch() to update the event mask for an existing watch.
+inotify_find_update_watch() returns the wd of the updated watch, or an error if
+the watch does not exist.
+
+    wd = inotify_find_update_watch(ih, inode, mask);
+
+An existing watch may be removed by calling either inotify_rm_watch() or
+inotify_rm_wd().
+
+    int ret = inotify_rm_watch(ih, &my_watch->iwatch);
+    int ret = inotify_rm_wd(ih, wd);
+
+A watch may be removed while executing your event handler with the following:
+
+    inotify_remove_watch_locked(ih, iwatch);
+
+Call inotify_destroy() to remove all watches from your inotify instance and
+release it.  If there are no outstanding references, inotify_destroy() will call
+your destroy_watch op for each watch.
+
+    inotify_destroy(ih);
+
+When inotify removes a watch, it sends an IN_IGNORED event to your callback.
+You may use this event as an indication to free the watch memory.  Note that
+inotify may remove a watch due to filesystem events, as well as by your request.
+If you use IN_ONESHOT, inotify will remove the watch after the first event, at
+which point you may call the final inotify_put_watch.
+
+(iv) Kernel Interface Prototypes
+
+	struct inotify_handle *inotify_init(struct inotify_operations *ops);
+
+	inotify_init_watch(struct inotify_watch *watch);
+
+	s32 inotify_add_watch(struct inotify_handle *ih,
+		              struct inotify_watch *watch,
+			      struct inode *inode, u32 mask);
+
+	s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode,
+			       struct inotify_watch **watchp);
+
+	s32 inotify_find_update_watch(struct inotify_handle *ih,
+				      struct inode *inode, u32 mask);
+
+	int inotify_rm_wd(struct inotify_handle *ih, u32 wd);
+
+	int inotify_rm_watch(struct inotify_handle *ih,
+			     struct inotify_watch *watch);
+
+	void inotify_remove_watch_locked(struct inotify_handle *ih,
+					 struct inotify_watch *watch);
+
+	void inotify_destroy(struct inotify_handle *ih);
+
+	void get_inotify_watch(struct inotify_watch *watch);
+	void put_inotify_watch(struct inotify_watch *watch);
+
+
+(v) Internal Kernel Implementation
+
+Each inotify instance is represented by an inotify_handle structure.
+Inotify's userspace consumers also have an inotify_device which is
+associated with the inotify_handle, and on which events are queued.
 
 Each watch is associated with an inotify_watch structure.  Watches are chained
-off of each associated device and each associated inode.
+off of each associated inotify_handle and each associated inode.
 
-See fs/inotify.c for the locking and lifetime rules.
+See fs/inotify.c and fs/inotify_user.c for the locking and lifetime rules.
 
 
-(iv) Rationale
+(vi) Rationale
 
 Q: What is the design decision behind not tying the watch to the open fd of
    the watched object?
@@ -145,7 +263,7 @@ A: The poor user-space interface is the 
    file descriptor-based one that allows basic file I/O and poll/select.
    Obtaining the fd and managing the watches could have been done either via a
    device file or a family of new system calls.  We decided to implement a
-   family of system calls because that is the preffered approach for new kernel
+   family of system calls because that is the preferred approach for new kernel
    interfaces.  The only real difference was whether we wanted to use open(2)
    and ioctl(2) or a couple of new system calls.  System calls beat ioctls.
 
fs/Kconfig +18 -6
index 67d5568..5a8329d 100644
@@ -406,18 +406,30 @@ config INOTIFY
 	bool "Inotify file change notification support"
 	default y
 	---help---
-	  Say Y here to enable inotify support and the associated system
-	  calls.  Inotify is a file change notification system and a
-	  replacement for dnotify.  Inotify fixes numerous shortcomings in
-	  dnotify and introduces several new features.  It allows monitoring
-	  of both files and directories via a single open fd.  Other features
-	  include multiple file events, one-shot support, and unmount
+	  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.
 
 	  For more information, see Documentation/filesystems/inotify.txt
 
 	  If unsure, say Y.
 
+config INOTIFY_USER
+	bool "Inotify support for userspace"
+	depends on INOTIFY
+	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.
+
+	  For more information, see Documentation/filesystems/inotify.txt
+
+	  If unsure, say Y.
+
 config QUOTA
 	bool "Quota support"
 	help
fs/Makefile +1 -0
index 7652ccb..69d4658 100644
@@ -13,6 +13,7 @@ obj-y :=	open.o read_write.o file_table.
 		ioprio.o pnode.o drop_caches.o splice.o sync.o
 
 obj-$(CONFIG_INOTIFY)		+= inotify.o
+obj-$(CONFIG_INOTIFY_USER)	+= inotify_user.o
 obj-$(CONFIG_EPOLL)		+= eventpoll.o
 obj-$(CONFIG_COMPAT)		+= compat.o compat_ioctl.o
 
fs/inotify.c +285 -706

1,241 lines not on this page · raw message

fs/inotify_user.c new file +719 -0

719 lines not on this page · raw message

include/linux/fsnotify.h +16 -13
index 11438ef..a9d3044 100644
@@ -54,16 +54,18 @@ static inline void fsnotify_move(struct 
 
 	if (isdir)
 		isdir = IN_ISDIR;
-	inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name);
-	inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name);
+	inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name,
+				  source);
+	inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name,
+				  source);
 
 	if (target) {
-		inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL);
+		inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL, NULL);
 		inotify_inode_is_dead(target);
 	}
 
 	if (source) {
-		inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL);
+		inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL, NULL);
 	}
 	audit_inode_child(old_name, source, old_dir->i_ino);
 	audit_inode_child(new_name, target, new_dir->i_ino);
@@ -85,7 +87,7 @@ static inline void fsnotify_nameremove(s
  */
 static inline void fsnotify_inoderemove(struct inode *inode)
 {
-	inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL);
+	inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL, NULL);
 	inotify_inode_is_dead(inode);
 }
 
@@ -95,7 +97,8 @@ static inline void fsnotify_inoderemove(
 static inline void fsnotify_create(struct inode *inode, struct dentry *dentry)
 {
 	inode_dir_notify(inode, DN_CREATE);
-	inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name);
+	inotify_inode_queue_event(inode, IN_CREATE, 0, dentry->d_name.name,
+				  dentry->d_inode);
 	audit_inode_child(dentry->d_name.name, dentry->d_inode, inode->i_ino);
 }
 
@@ -106,7 +109,7 @@ static inline void fsnotify_mkdir(struct
 {
 	inode_dir_notify(inode, DN_CREATE);
 	inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0, 
-				  dentry->d_name.name);
+				  dentry->d_name.name, dentry->d_inode);
 	audit_inode_child(dentry->d_name.name, dentry->d_inode, inode->i_ino);
 }
 
@@ -123,7 +126,7 @@ 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);
+	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
 }
 
 /*
@@ -139,7 +142,7 @@ 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);
+	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
 }
 
 /*
@@ -154,7 +157,7 @@ static inline void fsnotify_open(struct 
 		mask |= IN_ISDIR;
 
 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
-	inotify_inode_queue_event(inode, mask, 0, NULL);	
+	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
 }
 
 /*
@@ -172,7 +175,7 @@ static inline void fsnotify_close(struct
 		mask |= IN_ISDIR;
 
 	inotify_dentry_parent_queue_event(dentry, mask, 0, name);
-	inotify_inode_queue_event(inode, mask, 0, NULL);
+	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
 }
 
 /*
@@ -187,7 +190,7 @@ static inline void fsnotify_xattr(struct
 		mask |= IN_ISDIR;
 
 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
-	inotify_inode_queue_event(inode, mask, 0, NULL);
+	inotify_inode_queue_event(inode, mask, 0, NULL, NULL);
 }
 
 /*
@@ -234,7 +237,7 @@ static inline void fsnotify_change(struc
 	if (in_mask) {
 		if (S_ISDIR(inode->i_mode))
 			in_mask |= IN_ISDIR;
-		inotify_inode_queue_event(inode, in_mask, 0, NULL);
+		inotify_inode_queue_event(inode, in_mask, 0, NULL, NULL);
 		inotify_dentry_parent_queue_event(dentry, in_mask, 0,
 						  dentry->d_name.name);
 	}
include/linux/inotify.h +108 -3

146 lines not on this page · raw message

include/linux/sched.h +1 -1
index 14a56ad..8e0122f 100644
@@ -500,7 +500,7 @@ struct user_struct {
 	atomic_t processes;	/* How many processes does this user have? */
 	atomic_t files;		/* How many open files does this user have? */
 	atomic_t sigpending;	/* How many pending signals does this user have? */
-#ifdef CONFIG_INOTIFY
+#ifdef CONFIG_INOTIFY_USER
 	atomic_t inotify_watches; /* How many inotify watches does this user have? */
 	atomic_t inotify_devs;	/* How many inotify devs does this user have opened? */
 #endif
kernel/sysctl.c +2 -2
index 71463e7..ec1a1a3 100644
@@ -159,7 +159,7 @@ extern ctl_table random_table[];
 #ifdef CONFIG_UNIX98_PTYS
 extern ctl_table pty_table[];
 #endif
-#ifdef CONFIG_INOTIFY
+#ifdef CONFIG_INOTIFY_USER
 extern ctl_table inotify_table[];
 #endif
 
@@ -1100,7 +1100,7 @@ #ifdef CONFIG_MMU
 		.mode		= 0644,
 		.proc_handler	= &proc_doulongvec_minmax,
 	},
-#ifdef CONFIG_INOTIFY
+#ifdef CONFIG_INOTIFY_USER
 	{
 		.ctl_name	= FS_INOTIFY,
 		.procname	= "inotify",
kernel/user.c +1 -1
index 2116642..4b1eb74 100644
@@ -140,7 +140,7 @@ struct user_struct * alloc_uid(uid_t uid
 		atomic_set(&new->processes, 0);
 		atomic_set(&new->files, 0);
 		atomic_set(&new->sigpending, 0);
-#ifdef CONFIG_INOTIFY
+#ifdef CONFIG_INOTIFY_USER
 		atomic_set(&new->inotify_watches, 0);
 		atomic_set(&new->inotify_devs, 0);
 #endif
#1 lore raw
Quoted 12 lines · Amy Griffis

After stress testing and completing audit patches to use this API, I've made the following changes:

(*) Allow callers to share the refcounting for an inotify_watch.
If the caller has embedded the inotify_watch in one of its own structs, both inotify and the caller may need to use refcounts for that data. Since the caller is ultimately responsible for freeing the inotify_watch data, they must register a destroy function to be called on the last put_inotify_watch. Also provide inotify_init_watch() to enable a caller to use refcounts before calling inotify_add_watch().

Seems sane

Quoted 10 lines

(*) Allow callers to remove watches from their event handler.
Audit uses this feature to remove a watch after an IN_MOVE_SELF event. Another similar use could be to have functionality similar to IN_ONESHOT, but have it apply to a subset of events in the mask.
(*) Fixed a deadlock in inotify_dev_queue_event().

(*) Fixed memleaks in inotify_destroy() and with IN_ONESHOT masks.

Quoted 7 lines

(*) Re-ordered calls to event handler with IN_IGNORED events.
Since caller may do final put here, this must be the last thing inotify does with an inotify_watch.

I did some stress tests and performance comparisons on inotify with and without this patch. The tests I used and some results are posted here:

Having only glanced at your latest code, all of your changes and bug fixes look good. Thanks very much for putting the effort into auditing and testing inotify.

Signature · 1 line
John McCutchan <[email protected]>
#3 lore raw
In reply to a message not in the archive

John McCutchan wrote: [Fri May 26 2006, 12:12:26PM EDT]

Quoted 3 lines

Having only glanced at your latest code, all of your changes and bug fixes look good. Thanks very much for putting the effort into auditing and testing inotify.

Thanks, that's great to hear.

Andrew, could this patch be included in the next -mm kernel?

Thanks,
Amy

#4 lore raw
Quoted 6 lines · Amy Griffis

John McCutchan wrote: [Fri May 26 2006, 12:12:26PM EDT]

Quoted 3 lines

Having only glanced at your latest code, all of your changes and bug fixes look good. Thanks very much for putting the effort into auditing and testing inotify.

Thanks, that's great to hear.

btw is the user of this in-kernel stuff also included?

#5 lore raw
Quoted 9 lines · Arjan van de Ven
Quoted 6 lines · Amy Griffis

John McCutchan wrote: [Fri May 26 2006, 12:12:26PM EDT]

Quoted 3 lines

Having only glanced at your latest code, all of your changes and bug fixes look good. Thanks very much for putting the effort into auditing and testing inotify.

Thanks, that's great to hear.

btw is the user of this in-kernel stuff also included?

It will be - that's used by pending kernel/audit* stuff.

#6 lore raw
In reply to Amy Griffis
Quoted 2 lines · On Thu, 25 May 2006 22:10:30 -0400 Amy Griffis <[email protected]> wrote:
 fs/inotify.c                          |  991 +++++++++-------------------------
 fs/inotify_user.c                     |  719 ++++++++++++++++++++++++

This patch would be much easier to review if the move-code-around part was delivered in a separate patch from the add-new-functionality part.

#7 lore raw
Quoted 8 lines · Andrew Morton
Quoted 2 lines · On Thu, 25 May 2006 22:10:30 -0400 Amy Griffis <[email protected]> wrote:
 fs/inotify.c                          |  991 +++++++++-------------------------
 fs/inotify_user.c                     |  719 ++++++++++++++++++++++++

This patch would be much easier to review if the move-code-around part was delivered in a separate patch from the add-new-functionality part.

Makes sense. I'll split it up and repost.