[PATCH 1/3] fsnotify: add support for ignoring events from self

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Base support to allow inotify and fanotify instances to ignore
self generated events.  This allows user mode file servers to monitor
changes to their exported shares by other servers or other activities
on their server.  With FS_IGNORE_ME enabled, any f/s event triggered by
my pid is not queued into my watch/notify group.

Signed-off-by: Jim Lieb <jlieb@xxxxxxxxxxx>
---
 fs/notify/fsnotify.c             | 35 ++++++++++++++++++++++++-----------
 fs/notify/mark.c                 | 16 ++++++++++++++++
 include/linux/fsnotify_backend.h |  6 +++++-
 3 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index 4bb21d6..88770da 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -134,6 +134,7 @@ static int send_to_group(struct inode *to_tell,
 	struct fsnotify_group *group = NULL;
 	__u32 inode_test_mask = 0;
 	__u32 vfsmount_test_mask = 0;
+	struct pid *my_pid;
 
 	if (unlikely(!inode_mark && !vfsmount_mark)) {
 		BUG();
@@ -150,23 +151,35 @@ static int send_to_group(struct inode *to_tell,
 			vfsmount_mark->ignored_mask = 0;
 	}
 
+	my_pid = get_pid(task_tgid(current));
 	/* does the inode mark tell us to do something? */
 	if (inode_mark) {
-		group = inode_mark->group;
-		inode_test_mask = (mask & ~FS_EVENT_ON_CHILD);
-		inode_test_mask &= inode_mark->mask;
-		inode_test_mask &= ~inode_mark->ignored_mask;
+		if (inode_mark->mask & FS_IGNORE_ME &&
+		    my_pid == inode_mark->ignore_pid) {
+			inode_test_mask = 0;
+		} else {
+			group = inode_mark->group;
+			inode_test_mask = (mask & ~FS_EVENT_ON_CHILD);
+			inode_test_mask &= inode_mark->mask;
+			inode_test_mask &= ~inode_mark->ignored_mask;
+		}
 	}
 
 	/* does the vfsmount_mark tell us to do something? */
 	if (vfsmount_mark) {
-		vfsmount_test_mask = (mask & ~FS_EVENT_ON_CHILD);
-		group = vfsmount_mark->group;
-		vfsmount_test_mask &= vfsmount_mark->mask;
-		vfsmount_test_mask &= ~vfsmount_mark->ignored_mask;
-		if (inode_mark)
-			vfsmount_test_mask &= ~inode_mark->ignored_mask;
+		if (vfsmount_mark->mask & FS_IGNORE_ME &&
+		    my_pid == vfsmount_mark->ignore_pid) {
+			vfsmount_test_mask = 0;
+		} else {
+			vfsmount_test_mask = (mask & ~FS_EVENT_ON_CHILD);
+			group = vfsmount_mark->group;
+			vfsmount_test_mask &= vfsmount_mark->mask;
+			vfsmount_test_mask &= ~vfsmount_mark->ignored_mask;
+			if (inode_mark)
+				vfsmount_test_mask &= ~inode_mark->ignored_mask;
+		}
 	}
+	put_pid(my_pid);
 
 	pr_debug("%s: group=%p to_tell=%p mask=%x inode_mark=%p"
 		 " inode_test_mask=%x vfsmount_mark=%p vfsmount_test_mask=%x"
@@ -300,7 +313,7 @@ static __init int fsnotify_init(void)
 {
 	int ret;
 
-	BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 23);
+	BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 24);
 
 	ret = init_srcu_struct(&fsnotify_mark_srcu);
 	if (ret)
diff --git a/fs/notify/mark.c b/fs/notify/mark.c
index 923fe4a..ec08c48 100644
--- a/fs/notify/mark.c
+++ b/fs/notify/mark.c
@@ -132,6 +132,11 @@ void fsnotify_destroy_mark_locked(struct fsnotify_mark *mark,
 
 	mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
 
+	if (mark->ignore_pid != NULL) {
+		put_pid(mark->ignore_pid);
+		mark->ignore_pid = NULL;
+	}
+
 	if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
 		inode = mark->i.inode;
 		fsnotify_destroy_inode_mark(mark);
@@ -198,6 +203,17 @@ void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
 
 	mark->mask = mask;
 
+	if (mask & FS_IGNORE_ME) {
+		if (mark->ignore_pid != NULL)
+			put_pid(mark->ignore_pid);
+		mark->ignore_pid = get_pid(task_tgid(current));
+	} else {
+		if (mark->ignore_pid != NULL) {
+			put_pid(mark->ignore_pid);
+			mark->ignore_pid = NULL;
+		}
+	}
+
 	if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
 		fsnotify_set_inode_mark_mask_locked(mark, mask);
 }
diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h
index 4b2ee8d..4cc2d4a 100644
--- a/include/linux/fsnotify_backend.h
+++ b/include/linux/fsnotify_backend.h
@@ -44,6 +44,8 @@
 #define FS_OPEN_PERM		0x00010000	/* open event in an permission hook */
 #define FS_ACCESS_PERM		0x00020000	/* access event in a permissions hook */
 
+#define FS_IGNORE_ME		0x00800000	/* do not events I caused */
+
 #define FS_EXCL_UNLINK		0x04000000	/* do not send events if object is unlinked */
 #define FS_ISDIR		0x40000000	/* event occurred against dir */
 #define FS_IN_ONESHOT		0x80000000	/* only send event once */
@@ -71,7 +73,8 @@
 			     FS_MOVED_FROM | FS_MOVED_TO | FS_CREATE | \
 			     FS_DELETE | FS_DELETE_SELF | FS_MOVE_SELF | \
 			     FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | \
-			     FS_OPEN_PERM | FS_ACCESS_PERM | FS_EXCL_UNLINK | \
+			     FS_OPEN_PERM | FS_ACCESS_PERM | FS_IGNORE_ME | \
+			     FS_EXCL_UNLINK |				\
 			     FS_ISDIR | FS_IN_ONESHOT | FS_DN_RENAME | \
 			     FS_DN_MULTISHOT | FS_EVENT_ON_CHILD)
 
@@ -281,6 +284,7 @@ struct fsnotify_mark {
 	/* we hold ref for each i_list and g_list.  also one ref for each 'thing'
 	 * in kernel that found and may be using this mark. */
 	atomic_t refcnt;		/* active things looking at this mark */
+	struct pid *ignore_pid;		/* ignore events by this thread group */
 	struct fsnotify_group *group;	/* group this mark is for */
 	struct list_head g_list;	/* list of marks by group->i_fsnotify_marks */
 	spinlock_t lock;		/* protect group and inode */
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [Samba]     [Device Mapper]     [CEPH Development]
  Powered by Linux