diff options
author | Eric Paris <eparis@redhat.com> | 2009-05-21 17:01:20 -0400 |
---|---|---|
committer | Eric Paris <eparis@redhat.com> | 2009-06-11 14:57:52 -0400 |
commit | 90586523eb4b349806887c62ee70685a49415124 (patch) | |
tree | 2ba6da637f897bbb2309b141b81801e4151d87b0 /fs/notify/notification.c | |
parent | c9059598ea8981d02356eead3188bf7fa4d717b8 (diff) |
fsnotify: unified filesystem notification backend
fsnotify is a backend for filesystem notification. fsnotify does
not provide any userspace interface but does provide the basis
needed for other notification schemes such as dnotify. fsnotify
can be extended to be the backend for inotify or the upcoming
fanotify. fsnotify provides a mechanism for "groups" to register for
some set of filesystem events and to then deliver those events to
those groups for processing.
fsnotify has a number of benefits, the first being actually shrinking the size
of an inode. Before fsnotify to support both dnotify and inotify an inode had
unsigned long i_dnotify_mask; /* Directory notify events */
struct dnotify_struct *i_dnotify; /* for directory notifications */
struct list_head inotify_watches; /* watches on this inode */
struct mutex inotify_mutex; /* protects the watches list
But with fsnotify this same functionallity (and more) is done with just
__u32 i_fsnotify_mask; /* all events for this inode */
struct hlist_head i_fsnotify_mark_entries; /* marks on this inode */
That's right, inotify, dnotify, and fanotify all in 64 bits. We used that
much space just in inotify_watches alone, before this patch set.
fsnotify object lifetime and locking is MUCH better than what we have today.
inotify locking is incredibly complex. See 8f7b0ba1c8539 as an example of
what's been busted since inception. inotify needs to know internal semantics
of superblock destruction and unmounting to function. The inode pinning and
vfs contortions are horrible.
no fsnotify implementers do allocation under locks. This means things like
f04b30de3 which (due to an overabundance of caution) changes GFP_KERNEL to
GFP_NOFS can be reverted. There are no longer any allocation rules when using
or implementing your own fsnotify listener.
fsnotify paves the way for fanotify. In brief fanotify is a notification
mechanism that delivers the lisener both an 'event' and an open file descriptor
to the object in question. This means that fanotify is pathname agnostic.
Some on lkml may not care for the original companies or users that pushed for
TALPA, but fanotify was designed with flexibility and input for other users in
mind. The readahead group expressed interest in fanotify as it could be used
to profile disk access on boot without breaking the audit system. The desktop
search groups have also expressed interest in fanotify as it solves a number
of the race conditions and problems present with managing inotify when more
than a limited number of specific files are of interest. fanotify can provide
for a userspace access control system which makes it a clean interface for AV
vendors to hook without trying to do binary patching on the syscall table,
LSM, and everywhere else they do their things today. With this patch series
fanotify can be implemented in less than 1200 lines of easy to review code.
Almost all of which is the socket based user interface.
This patch series builds fsnotify to the point that it can implement
dnotify and inotify_user. Patches exist and will be sent soon after
acceptance to finish the in kernel inotify conversion (audit) and implement
fanotify.
Signed-off-by: Eric Paris <eparis@redhat.com>
Acked-by: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christoph Hellwig <hch@lst.de>
Diffstat (limited to 'fs/notify/notification.c')
-rw-r--r-- | fs/notify/notification.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/fs/notify/notification.c b/fs/notify/notification.c new file mode 100644 index 000000000000..b8e9a87f8f58 --- /dev/null +++ b/fs/notify/notification.c | |||
@@ -0,0 +1,121 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2, or (at your option) | ||
7 | * any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; see the file COPYING. If not, write to | ||
16 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | ||
17 | */ | ||
18 | |||
19 | #include <linux/fs.h> | ||
20 | #include <linux/init.h> | ||
21 | #include <linux/kernel.h> | ||
22 | #include <linux/list.h> | ||
23 | #include <linux/mount.h> | ||
24 | #include <linux/mutex.h> | ||
25 | #include <linux/namei.h> | ||
26 | #include <linux/path.h> | ||
27 | #include <linux/slab.h> | ||
28 | #include <linux/spinlock.h> | ||
29 | |||
30 | #include <asm/atomic.h> | ||
31 | |||
32 | #include <linux/fsnotify_backend.h> | ||
33 | #include "fsnotify.h" | ||
34 | |||
35 | static struct kmem_cache *fsnotify_event_cachep; | ||
36 | |||
37 | void fsnotify_get_event(struct fsnotify_event *event) | ||
38 | { | ||
39 | atomic_inc(&event->refcnt); | ||
40 | } | ||
41 | |||
42 | void fsnotify_put_event(struct fsnotify_event *event) | ||
43 | { | ||
44 | if (!event) | ||
45 | return; | ||
46 | |||
47 | if (atomic_dec_and_test(&event->refcnt)) { | ||
48 | if (event->data_type == FSNOTIFY_EVENT_PATH) | ||
49 | path_put(&event->path); | ||
50 | |||
51 | kmem_cache_free(fsnotify_event_cachep, event); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | /* | ||
56 | * Allocate a new event which will be sent to each group's handle_event function | ||
57 | * if the group was interested in this particular event. | ||
58 | */ | ||
59 | struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, | ||
60 | void *data, int data_type) | ||
61 | { | ||
62 | struct fsnotify_event *event; | ||
63 | |||
64 | event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL); | ||
65 | if (!event) | ||
66 | return NULL; | ||
67 | |||
68 | atomic_set(&event->refcnt, 1); | ||
69 | |||
70 | spin_lock_init(&event->lock); | ||
71 | |||
72 | event->path.dentry = NULL; | ||
73 | event->path.mnt = NULL; | ||
74 | event->inode = NULL; | ||
75 | |||
76 | event->to_tell = to_tell; | ||
77 | |||
78 | switch (data_type) { | ||
79 | case FSNOTIFY_EVENT_FILE: { | ||
80 | struct file *file = data; | ||
81 | struct path *path = &file->f_path; | ||
82 | event->path.dentry = path->dentry; | ||
83 | event->path.mnt = path->mnt; | ||
84 | path_get(&event->path); | ||
85 | event->data_type = FSNOTIFY_EVENT_PATH; | ||
86 | break; | ||
87 | } | ||
88 | case FSNOTIFY_EVENT_PATH: { | ||
89 | struct path *path = data; | ||
90 | event->path.dentry = path->dentry; | ||
91 | event->path.mnt = path->mnt; | ||
92 | path_get(&event->path); | ||
93 | event->data_type = FSNOTIFY_EVENT_PATH; | ||
94 | break; | ||
95 | } | ||
96 | case FSNOTIFY_EVENT_INODE: | ||
97 | event->inode = data; | ||
98 | event->data_type = FSNOTIFY_EVENT_INODE; | ||
99 | break; | ||
100 | case FSNOTIFY_EVENT_NONE: | ||
101 | event->inode = NULL; | ||
102 | event->path.dentry = NULL; | ||
103 | event->path.mnt = NULL; | ||
104 | break; | ||
105 | default: | ||
106 | BUG(); | ||
107 | } | ||
108 | |||
109 | event->mask = mask; | ||
110 | |||
111 | return event; | ||
112 | } | ||
113 | |||
114 | __init int fsnotify_notification_init(void) | ||
115 | { | ||
116 | fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC); | ||
117 | |||
118 | return 0; | ||
119 | } | ||
120 | subsys_initcall(fsnotify_notification_init); | ||
121 | |||