vfs: introduce FMODE_NONOTIFY
Subsystems (6)
PATCH 1/10 vfs: introduce FMODE_NONOTIFY
This is a new f_mode which can only be set by the kernel. It indicates that the fd was opened by fanotify and should not cause future fanotify events. This is needed to prevent fanotify livelock. An example of obvious livelock is from fanotify close events.
Process A closes file1
This creates a close event for file1.
fanotify opens file1 for Listener X
Listener X deals with the event and closes its fd for file1. This creates a close event for file1.
fanotify opens file1 for Listener X
Listener X deals with the event and closes its fd for file1. This creates a close event for file1.
fanotify opens file1 for Listener X
Listener X deals with the event and closes its fd for file1. notice a pattern?
The fix is to add the FMODE_NONOTIFY bit to the open filp done by the kernel for fanotify. Thus when that file is used it will not generate future events.
This patch simply defines the bit.
---
fs/open.c | 7 ++++--- include/asm-generic/fcntl.h | 8 ++++++++ include/linux/fs.h | 3 +++ include/linux/fsnotify.h | 24 ++++++++++++++++-------- 4 files changed, 31 insertions(+), 11 deletions(-)
fs/open.c +4 -3
@@ -831,9 +831,10 @@ static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,struct inode *inode;int error; - f->f_flags = flags; - f->f_mode = (__force fmode_t)((flags+1 ) &O_ACCMODE ) |FMODE_LSEEK | -FMODE_PREAD |FMODE_PWRITE ; + f->f_flags = (flags & ~(FMODE_EXEC |FMODE_NONOTIFY )); + f->f_mode = (__force fmode_t)((flags+1 ) &O_ACCMODE ) | (flags &FMODE_NONOTIFY ) | +FMODE_LSEEK |FMODE_PREAD |FMODE_PWRITE ; + inode = dentry->d_inode;if (f->f_mode &FMODE_WRITE ) { error =__get_file_write_access (inode, mnt);
include/asm-generic/fcntl.h +8 -0
@@ -3,6 +3,14 @@#include <linux/types.h> +/* +* FMODE_EXEC is 0x20 +* FMODE_NONOTIFY is 0x800000 +* These cannot be used by userspace O_* until internal and external open +* flags are split. +* -Eric Paris +*/ +#define O_ACCMODE 00000003#define O_RDONLY 00000000#define O_WRONLY 00000001
include/linux/fs.h +3 -0
@@ -87,6 +87,9 @@ struct inodes_stat_t {*/ #define FMODE_NOCMTIME ((__force fmode_t)2048) +/* File was opened by fanotify and shouldn't generate fanotify events */ +#define FMODE_NONOTIFY ((__force fmode_t)8388608) +/* * The below are the various read and write types that we support. Some of * them include behavioral modifiers that send information down to the
include/linux/fsnotify.h +16 -8
@@ -197,8 +197,10 @@ static inline void fsnotify_access(struct file *file)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL ); -fsnotify_parent (path,NULL , mask); -fsnotify (inode, mask, path,FSNOTIFY_EVENT_PATH ,NULL ,0 ); +if (!(file->f_mode &FMODE_NONOTIFY )) { +fsnotify_parent (path,NULL , mask); +fsnotify (inode, mask, path,FSNOTIFY_EVENT_PATH ,NULL ,0 ); + } }/*
@@ -215,8 +217,10 @@ static inline void fsnotify_modify(struct file *file)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL ); -fsnotify_parent (path,NULL , mask); -fsnotify (inode, mask, path,FSNOTIFY_EVENT_PATH ,NULL ,0 ); +if (!(file->f_mode &FMODE_NONOTIFY )) { +fsnotify_parent (path,NULL , mask); +fsnotify (inode, mask, path,FSNOTIFY_EVENT_PATH ,NULL ,0 ); + } }/*
@@ -233,8 +237,10 @@ static inline void fsnotify_open(struct file *file)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL ); -fsnotify_parent (path,NULL , mask); -fsnotify (inode, mask, path,FSNOTIFY_EVENT_PATH ,NULL ,0 ); +if (!(file->f_mode &FMODE_NONOTIFY )) { +fsnotify_parent (path,NULL , mask); +fsnotify (inode, mask, path,FSNOTIFY_EVENT_PATH ,NULL ,0 ); + } }/*
@@ -252,8 +258,10 @@ static inline void fsnotify_close(struct file *file)inotify_inode_queue_event (inode, mask,0 ,NULL ,NULL ); -fsnotify_parent (path,NULL , mask); -fsnotify (inode, mask, path,FSNOTIFY_EVENT_PATH ,NULL ,0 ); +if (!(file->f_mode &FMODE_NONOTIFY )) { +fsnotify_parent (path,NULL , mask); +fsnotify (inode, mask, path,FSNOTIFY_EVENT_PATH ,NULL ,0 ); + } }/*
PATCH 2/10 fanotify: fscking all notification system
fanotify is a novel file notification system which bases notification on giving userspace both an event type (open, close, read, write) and an open file descriptor to the object in question. This should address a number of races and problems with other notification systems like inotify and dnotify and should allow the future implementation of blocking or access controlled notification. These are useful for on access scanners or hierachical storage management schemes.
This patch just implements the basics of the fsnotify functions.
---
fs/notify/Kconfig | 1 + fs/notify/Makefile | 1 + fs/notify/fanotify/Kconfig | 11 ++++++ fs/notify/fanotify/Makefile | 1 + fs/notify/fanotify/fanotify.c | 78 +++++++++++++++++++++++++++++++++++++++++ fs/notify/fanotify/fanotify.h | 12 ++++++ include/linux/Kbuild | 1 + include/linux/fanotify.h | 40 +++++++++++++++++++++ 8 files changed, 145 insertions(+), 0 deletions(-) create mode 100644 fs/notify/fanotify/Kconfig create mode 100644 fs/notify/fanotify/Makefile create mode 100644 fs/notify/fanotify/fanotify.c create mode 100644 fs/notify/fanotify/fanotify.h create mode 100644 include/linux/fanotify.h
fs/notify/Kconfig +1 -0
@@ -3,3 +3,4 @@ config FSNOTIFY source "fs/notify/dnotify/Kconfig" source "fs/notify/inotify/Kconfig" +source "fs/notify/fanotify/Kconfig"
fs/notify/Makefile +1 -0
@@ -2,3 +2,4 @@ obj-$(CONFIG_FSNOTIFY) += fsnotify.o notification.o group.o inode_mark.o obj-y += dnotify/ obj-y += inotify/ +obj-y += fanotify/
fs/notify/fanotify/Kconfig new file +11 -0
@@ -0,0 +1,11 @@ +config FANOTIFY + bool "Filesystem wide access notification" + select FSNOTIFY + default y + ---help--- + Say Y here to enable fanotify suport. fanotify is a system wide + file access notification interface. Events are read from from a + socket and in doing so an fd is created in the reading process + which points to the same data as the one on which the event occured. + + If unsure, say Y.
fs/notify/fanotify/Makefile new file +1 -0
@@ -0,0 +1 @@ +obj-$(CONFIG_FANOTIFY) += fanotify.o
fs/notify/fanotify/fanotify.c new file +78 -0
78 lines not on this page · raw message
fs/notify/fanotify/fanotify.h new file +12 -0
@@ -0,0 +1,12 @@ +#include <linux/fanotify.h> +#include <linux/fsnotify_backend.h> +#include <linux/net.h> +#include <linux/kernel.h> +#include <linux/types.h> + +static inline bool fanotify_mask_valid (__u32 mask) +{ +if (mask & ~((__u32 )FAN_ALL_INCOMING_EVENTS )) +return false; +return true; +}
include/linux/Kbuild +1 -0
@@ -206,6 +206,7 @@ unifdef-y += ethtool.h unifdef-y += eventpoll.h unifdef-y += signalfd.h unifdef-y += ext2_fs.h +unifdef-y += fanotify.h unifdef-y += fb.h unifdef-y += fcntl.h unifdef-y += filter.h
include/linux/fanotify.h new file +40 -0
@@ -0,0 +1,40 @@ +#ifndef _LINUX_FANOTIFY_H +#define _LINUX_FANOTIFY_H + +#include <linux/types.h> + +/* the following events that user-space can register for */ +#define FAN_ACCESS 0x00000001/* File was accessed */ +#define FAN_MODIFY 0x00000002/* File was modified */ +#define FAN_CLOSE_WRITE 0x00000008/* Unwrittable file closed */ +#define FAN_CLOSE_NOWRITE 0x00000010/* Writtable file closed */ +#define FAN_OPEN 0x00000020/* File was opened */ + +#define FAN_EVENT_ON_CHILD 0x08000000/* interested in child events */ + +/* FIXME currently Q's have no limit.... */ +#define FAN_Q_OVERFLOW 0x00004000/* Event queued overflowed */ + +/* helper events */ +#define FAN_CLOSE (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE)/* close */ + +/* +* All of the events - we build the list by hand so that we can add flags in +* the future and not break backward compatibility. Apps will get only the +* events that they originally wanted. Be sure to add new events here! +*/ +#define FAN_ALL_EVENTS (FAN_ACCESS |\ + FAN_MODIFY |\ + FAN_CLOSE |\ + FAN_OPEN) + +/* +* All legal FAN bits userspace can request (although possibly not all +* at the same time. +*/ +#define FAN_ALL_INCOMING_EVENTS (FAN_ALL_EVENTS |\ + FAN_EVENT_ON_CHILD) +#ifdef __KERNEL__ + +#endif /* __KERNEL__ */ +#endif /* _LINUX_FANOTIFY_H */
Re: [PATCH 02/10] fanotify: fscking all notification system
Quoted 7 lines · Eric Paris
fanotify is a novel file notification system which bases notification on giving userspace both an event type (open, close, read, write) and an open file descriptor to the object in question. This should address a number of races and problems with other notification systems like inotify and dnotify and should allow the future implementation of blocking or access controlled notification. These are useful for on access scanners or hierachical storage management schemes.
I'm not hearing the negative feedback of the past and most everyone's questions and concerns have been addressed. Everyone ok if I drop these into -next?
-Eric
Re: [PATCH 02/10] fanotify: fscking all notification system
Quoted 3 lines · Eric Paris
I'm not hearing the negative feedback of the past and most everyone's questions and concerns have been addressed. Everyone ok if I drop these into -next?
Some of the comments are outdated and don't match the code anymore.
Could the man pages please be removed from the comments, updated, and submitted separately (i.e., not as part of the kernel patches)?
Thanks,
Andreas
PATCH 3/10 fanotify:drop notification if they exist in the outgoing queue
In reply to Eric Parisfanotify listeners get an open file descriptor to the object in question so the ordering of operations is not as important as in other notification systems. inotify will drop events if the last event in the event FIFO is the same as the current event. This patch will drop fanotify events if they are the same as another event anywhere in the event FIFO.
---
fs/notify/fanotify/fanotify.c | 45 +++++++++++++++++++++++++++++++++++++++-- 1 files changed, 43 insertions(+), 2 deletions(-)
fs/notify/fanotify/fanotify.c +43 -2
@@ -6,6 +6,45 @@#include "fanotify.h" +static bool should_merge (struct fsnotify_event *old,struct fsnotify_event *new) +{ +pr_debug ("%s: old=%p new=%p\n" , __func__, old, new); + +if ((old->mask == new->mask) && + (old->to_tell == new->to_tell) && + (old->data_type == new->data_type)) { +switch (old->data_type) { +case (FSNOTIFY_EVENT_PATH ): +if ((old->path.mnt == new->path.mnt) && + (old->path.dentry == new->path.dentry)) +return true; +case (FSNOTIFY_EVENT_NONE ): +return true; +default : +BUG (); + }; + } +return false; +} + +static int fanotify_merge (struct list_head *list,struct fsnotify_event *event) +{ +struct fsnotify_event_holder *holder; +struct fsnotify_event *test_event; + +pr_debug ("%s: list=%p event=%p\n" , __func__, list, event); + +/* and the list better be locked by something too! */ + +list_for_each_entry_reverse (holder, list, event_list) { + test_event = holder->event; +if (should_merge (test_event, event)) +return -EEXIST ; + } + +return 0 ; +} +static int fanotify_handle_event (struct fsnotify_group *group,struct fsnotify_event *event) {int ret;
@@ -21,8 +60,10 @@ static int fanotify_handle_event(struct fsnotify_group *group, struct fsnotify_epr_debug ("%s: group=%p event=%p\n" , __func__, group, event); - ret =fsnotify_add_notify_event (group, event,NULL ,NULL ); - + ret =fsnotify_add_notify_event (group, event,NULL , fanotify_merge); +/* -EEXIST means this event was merged with another, not that it was an error */ +if (ret == -EEXIST ) + ret =0 ;return ret; }
PATCH 4/10 fanotify: merge notification events with different masks
In reply to Eric ParisInstead of just merging fanotify events if they are exactly the same, merge notification events with different masks. To do this we have to clone the old event, update the mask in the new event with the new merged mask, and put the new event in place of the old event.
---
fs/notify/fanotify/fanotify.c | 39 ++++++++++++++++++++++++++++++--------- 1 files changed, 30 insertions(+), 9 deletions(-)
fs/notify/fanotify/fanotify.c +30 -9
@@ -10,8 +10,7 @@ static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new) {pr_debug ("%s: old=%p new=%p\n" , __func__, old, new); -if ((old->mask == new->mask) && - (old->to_tell == new->to_tell) && +if ((old->to_tell == new->to_tell) && (old->data_type == new->data_type)) {switch (old->data_type) {case (FSNOTIFY_EVENT_PATH ):
@@ -29,20 +28,42 @@ static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)static int fanotify_merge (struct list_head *list,struct fsnotify_event *event) { -struct fsnotify_event_holder *holder; +struct fsnotify_event_holder *test_holder;struct fsnotify_event *test_event; +struct fsnotify_event *new_event; +int ret =0 ;pr_debug ("%s: list=%p event=%p\n" , __func__, list, event);/* and the list better be locked by something too! */ -list_for_each_entry_reverse (holder, list, event_list) { - test_event = holder->event; -if (should_merge (test_event, event)) -return -EEXIST ; +list_for_each_entry_reverse (test_holder, list, event_list) { + test_event = test_holder->event; +if (should_merge (test_event, event)) { + ret = -EEXIST ; + +/* if they are exactly the same we are done */ +if (test_event->mask == event->mask) + goto out; + +/* can't allocate memory, merge was no possible */ + new_event =fsnotify_clone_event (test_event); +if (unlikely (!new_event)) { + ret =0 ; + goto out; + } + +/* build new event and replace it on the list */ + new_event->mask = (test_event->mask | event->mask); +fsnotify_replace_event (test_holder, new_event); +/* match ref from fsnotify_clone_event() */ +fsnotify_put_event (new_event); + +break ; + } } - -return 0 ; +out: +return ret; }static int fanotify_handle_event (struct fsnotify_group *group,struct fsnotify_event *event)
PATCH 5/10 fanotify: do not clone on merge unless needed
In reply to Eric ParisCurrently if 2 events are going to be merged on the notication queue with different masks the second event will be cloned and will replace the first event. However if this notification queue is the only place referencing the event in question there is no reason not to just update the event in place. We can tell this if the event->refcnt == 1. Since we hold a reference for each queue this event is on we know that when refcnt == 1 this is the only queue. The other concern is that it might be about to be added to a new queue, but this can't be the case since fsnotify holds a reference on the event until it is finished adding it to queues.
---
fs/notify/fanotify/fanotify.c | 10 ++++++++++ 1 files changed, 10 insertions(+), 0 deletions(-)
fs/notify/fanotify/fanotify.c +10 -0
@@ -46,6 +46,16 @@ static int fanotify_merge(struct list_head *list, struct fsnotify_event *event)if (test_event->mask == event->mask) goto out; +/* +* if the refcnt == 1 this is the only queue +* for this event and so we can update the mask +* in place. +*/ +if (atomic_read (&test_event->refcnt) ==1 ) { + test_event->mask |= event->mask; + goto out; + } +/* can't allocate memory, merge was no possible */ new_event =fsnotify_clone_event (test_event);if (unlikely (!new_event)) {
PATCH 6/10 fanotify: fanotify_init syscall declaration
In reply to Eric ParisThis patch defines a new syscall fanotify_init() of the form:
int sys_fanotify_init(unsigned int flags, unsigned int event_f_flags, int priority)
This syscall is used to create and fanotify group. This is very similar to the inotify_init() syscall.
---
arch/x86/ia32/ia32entry.S | 1 + arch/x86/include/asm/unistd_32.h | 3 ++- arch/x86/include/asm/unistd_64.h | 2 ++ arch/x86/kernel/syscall_table_32.S | 1 + fs/notify/fanotify/Makefile | 2 +- fs/notify/fanotify/fanotify_user.c | 13 +++++++++++++ include/linux/syscalls.h | 1 + 7 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 fs/notify/fanotify/fanotify_user.c
arch/x86/ia32/ia32entry.S +1 -0
@@ -842,4 +842,5 @@ ia32_sys_call_table: .quad compat_sys_rt_tgsigqueueinfo /* 335 */ .quad sys_perf_event_open .quad compat_sys_recvmmsg + .quad sys_fanotify_init ia32_syscall_end:
arch/x86/include/asm/unistd_32.h +2 -1
@@ -343,10 +343,11 @@#define __NR_rt_tgsigqueueinfo 335#define __NR_perf_event_open 336#define __NR_recvmmsg 337 +#define __NR_fanotify_init 338#ifdef __KERNEL__ -#define NR_syscalls 338 +#define NR_syscalls 339#define __ARCH_WANT_IPC_PARSE_VERSION#define __ARCH_WANT_OLD_READDIR
arch/x86/include/asm/unistd_64.h +2 -0
@@ -663,6 +663,8 @@ __SYSCALL(__NR_rt_tgsigqueueinfo, sys_rt_tgsigqueueinfo) __SYSCALL(__NR_perf_event_open, sys_perf_event_open)#define __NR_recvmmsg 299 __SYSCALL(__NR_recvmmsg, sys_recvmmsg) +#define __NR_fanotify_init 300 +__SYSCALL(__NR_fanotify_init, sys_fanotify_init)#ifndef __NO_STUBS#define __ARCH_WANT_OLD_READDIR
arch/x86/kernel/syscall_table_32.S +1 -0
@@ -337,3 +337,4 @@ ENTRY(sys_call_table) .long sys_rt_tgsigqueueinfo /* 335 */ .long sys_perf_event_open .long sys_recvmmsg + .long sys_fanotify_init
fs/notify/fanotify/Makefile +1 -1
@@ -1 +1 @@ -obj-$(CONFIG_FANOTIFY) += fanotify.o +obj-$(CONFIG_FANOTIFY) += fanotify.o fanotify_user.o
fs/notify/fanotify/fanotify_user.c new file +13 -0
@@ -0,0 +1,13 @@ +#include <linux/fcntl.h> +#include <linux/fs.h> +#include <linux/fsnotify_backend.h> +#include <linux/security.h> +#include <linux/syscalls.h> + +#include "fanotify.h" + +SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags, + int, priority) +{ +return -ENOSYS ; +}
include/linux/syscalls.h +1 -0
@@ -876,6 +876,7 @@ asmlinkage long sys_pselect6(int, fd_set __user *, fd_set __user *,asmlinkage long sys_ppoll (struct pollfd __user *,unsigned int ,struct timespec __user *,const sigset_t __user *,size_t ); +asmlinkage long sys_fanotify_init (unsigned int flags,unsigned int event_f_flags,int priority);int kernel_execve (const char *filename,char *const argv[],char *const envp[]);
PATCH 7/10 fanotify: fanotify_init syscall implementation
In reply to Eric ParisNAME
fanotify_init - initialize an fanotify group
SYNOPSIS
int fanotify_init(unsigned int flags, unsigned int event_f_flags, int priority);
DESCRIPTION
fanotify_init() initializes a new fanotify instance and returns a file descriptor associated with the new fanotify event queue.
The following values can be OR'd into the flags field:
FAN_NONBLOCK Set the O_NONBLOCK file status flag on the new open file description.
Using this flag saves extra calls to fcntl(2) to achieve the same result.
FAN_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor.
See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful.
The event_f_flags argument is unused and must be set to 0
The priority argument is unused and must be set to 0
RETURN VALUE
On success, this system call return a new file descriptor. On error, -1 is returned, and errno is set to indicate the error.
ERRORS
EINVAL An invalid value was specified in flags.
EINVAL A non-zero valid was passed in event_f_flags or in priority
ENFILE The system limit on the total number of file descriptors has been reached.
ENOMEM Insufficient kernel memory is available.
CONFORMING TO
These system calls are Linux-specific.
---
fs/notify/fanotify/fanotify.h | 2 + fs/notify/fanotify/fanotify_user.c | 61 +++++++++++++++++++++++++++++++++++- include/linux/fanotify.h | 4 ++ 3 files changed, 66 insertions(+), 1 deletions(-)
fs/notify/fanotify/fanotify.h +2 -0
@@ -4,6 +4,8 @@#include <linux/kernel.h> #include <linux/types.h> +extern const struct fsnotify_ops fanotify_fsnotify_ops; +static inline bool fanotify_mask_valid (__u32 mask) {if (mask & ~((__u32 )FAN_ALL_INCOMING_EVENTS ))
fs/notify/fanotify/fanotify_user.c +60 -1
73 lines not on this page · raw message
include/linux/fanotify.h +4 -0
@@ -18,6 +18,10 @@/* helper events */ #define FAN_CLOSE (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE)/* close */ +#define FAN_CLOEXEC 0x00000001 +#define FAN_NONBLOCK 0x00000002 + +#define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK)/* * All of the events - we build the list by hand so that we can add flags in * the future and not break backward compatibility. Apps will get only the
PATCH 8/10 fanotify: sys_fanotify_mark declartion
In reply to Eric ParisThis patch simply declares the new sys_fanotify_mark syscall
int fanotify_mark(int fanotify_fd, unsigned int flags, int dfd
const char *pathname, u64 mask, u64 ignored_mask)
---
arch/x86/ia32/ia32entry.S | 1 + arch/x86/include/asm/unistd_32.h | 3 ++- arch/x86/include/asm/unistd_64.h | 2 ++ arch/x86/kernel/syscall_table_32.S | 1 + fs/notify/fanotify/fanotify_user.c | 6 ++++++ include/linux/syscalls.h | 3 +++ 6 files changed, 15 insertions(+), 1 deletions(-)
arch/x86/ia32/ia32entry.S +1 -0
@@ -843,4 +843,5 @@ ia32_sys_call_table: .quad sys_perf_event_open .quad compat_sys_recvmmsg .quad sys_fanotify_init + .quad sys_fanotify_mark ia32_syscall_end:
arch/x86/include/asm/unistd_32.h +2 -1
@@ -344,10 +344,11 @@#define __NR_perf_event_open 336#define __NR_recvmmsg 337#define __NR_fanotify_init 338 +#define __NR_fanotify_mark 339#ifdef __KERNEL__ -#define NR_syscalls 339 +#define NR_syscalls 340#define __ARCH_WANT_IPC_PARSE_VERSION#define __ARCH_WANT_OLD_READDIR
arch/x86/include/asm/unistd_64.h +2 -0
@@ -665,6 +665,8 @@ __SYSCALL(__NR_perf_event_open, sys_perf_event_open) __SYSCALL(__NR_recvmmsg, sys_recvmmsg)#define __NR_fanotify_init 300 __SYSCALL(__NR_fanotify_init, sys_fanotify_init) +#define __NR_fanotify_mark 301 +__SYSCALL(__NR_fanotify_mark, sys_fanotify_mark)#ifndef __NO_STUBS#define __ARCH_WANT_OLD_READDIR
arch/x86/kernel/syscall_table_32.S +1 -0
@@ -338,3 +338,4 @@ ENTRY(sys_call_table) .long sys_perf_event_open .long sys_recvmmsg .long sys_fanotify_init + .long sys_fanotify_mark
fs/notify/fanotify/fanotify_user.c +6 -0
@@ -70,3 +70,9 @@ out_put_group:fsnotify_put_group (group);return fd; } + +SYSCALL_DEFINE5(fanotify_mark, int, fanotify_fd, unsigned int, flags, + int, dfd, const char __user *, pathname, __u64, mask) +{ +return -ENOSYS ; +}
include/linux/syscalls.h +3 -0
@@ -877,6 +877,9 @@ asmlinkage long sys_ppoll(struct pollfd __user *, unsigned int, struct timespec __user *, const sigset_t __user *, size_t);asmlinkage long sys_fanotify_init (unsigned int flags,unsigned int event_f_flags,int priority); +asmlinkage long sys_fanotify_mark (int fanotify_fd,unsigned int flags, +int fd,const char __user *pathname, +u64 mask);int kernel_execve (const char *filename,char *const argv[],char *const envp[]);
PATCH 9/10 fanotify: fanotify_mark syscall implementation
In reply to Eric ParisNAME
fanotify_mark - add, remove, or modify an fanotify mark on a
filesystem object
SYNOPSIS
int fanotify_mark(int fanotify_fd, unsigned int flags,
int dfd, const char *pathname, u64 mask, u64 ignored_mask)
DESCRIPTION
fanotify_mark() is used to add remove or modify a mark on a filesystem object. Marks are used to indicate that the fanotify group is interested in events which occur on that object. At this point in time marks may only be added to files and directories.
fanotify_fd must be a file descriptor returned by fanotify_init()
The flags field must contain exactly one of the following:
FAN_MARK_ADD - or the bits in mask and ignored mask into the mark FAN_MARK_REMOVE - bitwise remove the bits in mask and ignored mark
from the mark
The following values can be OR'd into the flags field:
FAN_MARK_DONT_FOLLOW - same meaning as O_NOFOLLOW as described in open(2) FAN_MARK_ONLYDIR - same meaning as O_DIRECTORY as described in open(2)
dfd may be any other the following:
AT_FDCWD: the object will be lookup up based on pathname similar
to open(2)
file descriptor of a directory: if pathname is not NULL the
object to modify will be lookup up similar to openat(2)
file descriptor of the final object: if pathname is NULL the
object to modify will be the object referenced by dfd
The mask is the bitwise OR of the set of events of interest such as:
FAN_ACCESS - object was accessed (read)
FAN_MODIFY - object was modified (write)
FAN_CLOSE_WRITE - object was writable and was closed
FAN_CLOSE_NOWRITE - object was read only and was closed
FAN_OPEN - object was opened
FAN_EVENT_ON_CHILD - interested in objected that happen to
children. Only relavent when the object is a directory
FAN_Q_OVERFLOW - event queue overflowed (not implemented)
The ignored mask is the opposite of the mask as if applied after the mask. If FAN_OPEN is specified in both the mask and the ignored_mask no event will be sent to userspace. This is not persently used but will be used when more objects may be marked. Assume you marked a mount point as something of interest. You could then add an ignored_mask entry on individual inodes to get notification on everything in the mount point except for a select few inodes.
RETURN VALUE
On success, this system call returns 0. On error, -1 is returned, and errno is set to indicate the error.
ERRORS
EINVAL An invalid value was specified in flags.
EINVAL An invalid value was specified in mask.
EINVAL An invalid value was specified in ignored_mask.
EINVAL fanotify_fd is not a file descriptor as returned by fanotify_init()
EBADF fanotify_fd is not a valid file descriptor
EBADF dfd is not a vlid file descriptor and path is NULL.
ENOTDIR dfd is not a directory and path is not NULL
EACCESS you do not have search permission on dfd
EACCESS no search permissions on some part of the path
ENENT file not found
ENOMEM Insufficient kernel memory is available.
CONFORMING TO
These system calls are Linux-specific.
---
fs/notify/fanotify/fanotify.h | 18 +++ fs/notify/fanotify/fanotify_user.c | 239 ++++++++++++++++++++++++++++++++++++ include/linux/fanotify.h | 13 ++ 3 files changed, 269 insertions(+), 1 deletions(-)
fs/notify/fanotify/fanotify.h +18 -0
@@ -6,6 +6,24 @@extern const struct fsnotify_ops fanotify_fsnotify_ops; +static inline bool fanotify_mark_flags_valid (unsigned int flags) +{ +/* must be either and add or a remove */ +if (!(flags & (FAN_MARK_ADD |FAN_MARK_REMOVE ))) +return false; + +/* cannot be both add and remove */ +if ((flags &FAN_MARK_ADD ) && + (flags &FAN_MARK_REMOVE )) +return false; + +/* cannot have more flags than we know about */ +if (flags & ~FAN_ALL_MARK_FLAGS ) +return false; + +return true; +} +static inline bool fanotify_mask_valid (__u32 mask) {if (mask & ~((__u32 )FAN_ALL_INCOMING_EVENTS ))
fs/notify/fanotify/fanotify_user.c +238 -1
261 lines not on this page · raw message
include/linux/fanotify.h +13 -0
@@ -18,10 +18,23 @@/* helper events */ #define FAN_CLOSE (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE)/* close */ +/* flags used for fanotify_init() */ #define FAN_CLOEXEC 0x00000001#define FAN_NONBLOCK 0x00000002#define FAN_ALL_INIT_FLAGS (FAN_CLOEXEC | FAN_NONBLOCK) + +/* flags used for fanotify_modify_mark() */ +#define FAN_MARK_ADD 0x00000001 +#define FAN_MARK_REMOVE 0x00000002 +#define FAN_MARK_DONT_FOLLOW 0x00000004 +#define FAN_MARK_ONLYDIR 0x00000008 + +#define FAN_ALL_MARK_FLAGS (FAN_MARK_ADD |\ + FAN_MARK_REMOVE |\ + FAN_MARK_DONT_FOLLOW |\ + FAN_MARK_ONLYDIR) +/* * All of the events - we build the list by hand so that we can add flags in * the future and not break backward compatibility. Apps will get only the
PATCH 10/10 send events using read
In reply to Eric Paris---
fs/notify/fanotify/fanotify.h | 5 + fs/notify/fanotify/fanotify_user.c | 225 +++++++++++++++++++++++++++++++++++- include/linux/fanotify.h | 24 ++++ 3 files changed, 250 insertions(+), 4 deletions(-)
fs/notify/fanotify/fanotify.h +5 -0
@@ -30,3 +30,8 @@ static inline bool fanotify_mask_valid(__u32 mask)return false;return true; } + +static inline __u32 fanotify_outgoing_mask (__u32 mask) +{ +return mask &FAN_ALL_OUTGOING_EVENTS ; +}
fs/notify/fanotify/fanotify_user.c +221 -4
250 lines not on this page · raw message
include/linux/fanotify.h +24 -0
@@ -51,6 +51,30 @@*/ #define FAN_ALL_INCOMING_EVENTS (FAN_ALL_EVENTS |\ FAN_EVENT_ON_CHILD) + +#define FAN_ALL_OUTGOING_EVENTS (FAN_ALL_EVENTS |\ + FAN_Q_OVERFLOW) + +#define FANOTIFY_METADATA_VERSION 1 + +struct fanotify_event_metadata { +__u32 event_len; +__u32 vers; +__s32 fd; +__u64 mask; +} __attribute__ ((packed)); + +/* Helper functions to deal with fanotify_event_metadata buffers */ +#define FAN_EVENT_METADATA_LEN (sizeof(struct fanotify_event_metadata)) + +#define FAN_EVENT_NEXT (meta, len) ((len) -= (meta)->event_len, \ + (struct fanotify_event_metadata*)(((char *)(meta)) + \ + (meta)->event_len)) + +#define FAN_EVENT_OK (meta, len) ((long)(len) >= (long)FAN_EVENT_METADATA_LEN && \ + (long)(meta)->event_len >= (long)FAN_EVENT_METADATA_LEN && \ + (long)(meta)->event_len <= (long)(len)) +#ifdef __KERNEL__#endif /* __KERNEL__ */
Re: [PATCH 10/10] send events using read
Hi, Eric,
This is not a full review, but I did notice a problem as I was trying to figure out the new API...
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +10
-0+static ssize_t fanotify_read (struct file *file,char __user *buf, +size_t count,loff_t *pos) +{ +struct fsnotify_group *group; +struct fsnotify_event *kevent; +char __user *start; +int ret; +DEFINE_WAIT (wait); + + start = buf;
fs/notify/fanotify/fanotify_user.c line 174
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +10
-0+ group = file->private_data; + +pr_debug ("%s: group=%p\n" , __func__, group); + +while (1 ) { +prepare_to_wait (&group->notification_waitq, &wait,TASK_INTERRUPTIBLE ); + +mutex_lock (&group->notification_mutex); + kevent =get_one_event (group, count); +mutex_unlock (&group->notification_mutex);
prepare_to_wait(), among other things, sets the task state. But then you go into various sleeping calls (mutex_lock(), for starters); that will undo what prepare_to_wait has done. You'll be back in TASK_RUNNING at this point, so you'll never sleep.
I've not looked at the code well enough to know how to fix it. My guess is that the sleeping on event availability should be done in get_one_event(), or in a small wrapper which goes immediately around it.
Quoted 13 lines
fs/notify/fanotify/fanotify_user.c +13
-0+ +if (kevent) { + ret =PTR_ERR (kevent); +if (IS_ERR (kevent)) +break ; + ret =copy_event_to_user (group, kevent, buf); +fsnotify_put_event (kevent); +if (ret <0 ) +break ; + buf += ret; + count -= ret; +continue ; + }
fs/notify/fanotify/fanotify_user.c line 197
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +10
-0+ + ret = -EAGAIN ; +if (file->f_flags &O_NONBLOCK ) +break ; + ret = -EINTR ; +if (signal_pending (current)) +break ; + +if (start != buf) +break ;
Alternatively, maybe you could do this here?
prepare_to_wait(...); if (fsnotify_notify_queue_is_empty(group)) schedule()
Review
Quoted 2 lines
(quoted diff) +2
-0+ + schedule();
You also need finish_wait() here, not outside the loop.
fs/notify/fanotify/fanotify_user.c line 206
Quoted 7 lines
fs/notify/fanotify/fanotify_user.c +7
-0+ } + +finish_wait (&group->notification_waitq, &wait); +if (start != buf && ret != -EFAULT ) + ret = buf - start; +return ret; +}
jon
Re: [PATCH 10/10] send events using read
Quoted 4 lines · Jonathan Corbet
Hi, Eric,
This is not a full review, but I did notice a problem as I was trying to figure out the new API...
This is a rip off of inotify which was introduced in commit 3632dee2f8b from Vegard Nossum back in January. I can't seem to find any discussion of this before it went into Linus' tree, so if someone knows how this patch got to Linus and what was said about it, I'd like to know. Thanks for unwittingly finding and inotify bug!
I looked back over it and it looks to me like it will work although there may be a race like situation if there are multiple things trying to read events. I can see how with perfect timing and precision Task A might try to get the mutex and it is being held by task B. Task B drops the mutex and Task A gets it, this causes Task A to be TASK_RUNNABLE. Lets assume Task B runs back around the loop and tries to get it while Task A still holds it. Task A will drop the mutex and Task B gets it, now B is runnable. Repeat until infinity with perfect timing! It's not really a live-lock, if either of them ever gets the mutex uncontested or if an event ever arrives they are going to sleep and/or break the cycle.
Certainly I'll take a look at it.
Quoted 25 lines
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +10
-0+static ssize_t fanotify_read (struct file *file,char __user *buf, +size_t count,loff_t *pos) +{ +struct fsnotify_group *group; +struct fsnotify_event *kevent; +char __user *start; +int ret; +DEFINE_WAIT (wait); + + start = buf;fs/notify/fanotify/fanotify_user.c line 174
Quoted 10 lines
fs/notify/fanotify/fanotify_user.c +10
-0+ group = file->private_data; + +pr_debug ("%s: group=%p\n" , __func__, group); + +while (1 ) { +prepare_to_wait (&group->notification_waitq, &wait,TASK_INTERRUPTIBLE ); + +mutex_lock (&group->notification_mutex); + kevent =get_one_event (group, count); +mutex_unlock (&group->notification_mutex);
prepare_to_wait(), among other things, sets the task state. But then you go into various sleeping calls (mutex_lock(), for starters); that will undo whatprepare_to_waithas done. You'll be back inTASK_RUNNINGat this point, so you'll never sleep.
Re: [PATCH 10/10] send events using read
2009/11/4 Eric Paris <[email protected]>:
Quoted 11 lines · Jonathan Corbet
Quoted 4 lines
Hi, Eric,
This is not a full review, but I did notice a problem as I was trying to figure out the new API...
This is a rip off of inotify which was introduced in commit
3632dee2f8bfrom Vegard Nossum back in January. I can't seem to find any discussion of this before it went into Linus' tree, so if someone knows how this patch got to Linus and what was said about it, I'd like to know. Thanks for unwittingly finding and inotify bug!
I don't have a git tree handy, but I think this is the locking imbalance fix. It went to the security list, so that's probably why you didn't find it. As far as I can tell, the code in question was wrong even before that patch, though.
Vegard