aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorRobert Love <rml@novell.com>2005-07-12 17:06:03 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2005-07-12 23:38:38 -0400
commit0eeca28300df110bd6ed54b31193c83b87921443 (patch)
tree7db42d8a18d80eca538f5b7d25e0532b8fa38b85 /include
parentbd4c625c061c2a38568d0add3478f59172455159 (diff)
[PATCH] inotify
inotify is intended to correct the deficiencies of dnotify, particularly its inability to scale and its terrible user interface: * dnotify requires the opening of one fd per each directory that you intend to watch. This quickly results in too many open files and pins removable media, preventing unmount. * dnotify is directory-based. You only learn about changes to directories. Sure, a change to a file in a directory affects the directory, but you are then forced to keep a cache of stat structures. * dnotify's interface to user-space is awful. Signals? inotify provides a more usable, simple, powerful solution to file change notification: * inotify's interface is a system call that returns a fd, not SIGIO. You get a single fd, which is select()-able. * inotify has an event that says "the filesystem that the item you were watching is on was unmounted." * inotify can watch directories or files. Inotify is currently used by Beagle (a desktop search infrastructure), Gamin (a FAM replacement), and other projects. See Documentation/filesystems/inotify.txt. Signed-off-by: Robert Love <rml@novell.com> Cc: John McCutchan <ttb@tentacle.dhs.org> Cc: Christoph Hellwig <hch@lst.de> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'include')
-rw-r--r--include/asm-i386/unistd.h5
-rw-r--r--include/linux/fs.h6
-rw-r--r--include/linux/fsnotify.h248
-rw-r--r--include/linux/inotify.h108
-rw-r--r--include/linux/sched.h4
-rw-r--r--include/linux/sysctl.h11
6 files changed, 379 insertions, 3 deletions
diff --git a/include/asm-i386/unistd.h b/include/asm-i386/unistd.h
index e25e4c71a879..a7cb377745bf 100644
--- a/include/asm-i386/unistd.h
+++ b/include/asm-i386/unistd.h
@@ -296,8 +296,11 @@
296#define __NR_keyctl 288 296#define __NR_keyctl 288
297#define __NR_ioprio_set 289 297#define __NR_ioprio_set 289
298#define __NR_ioprio_get 290 298#define __NR_ioprio_get 290
299#define __NR_inotify_init 291
300#define __NR_inotify_add_watch 292
301#define __NR_inotify_rm_watch 293
299 302
300#define NR_syscalls 291 303#define NR_syscalls 294
301 304
302/* 305/*
303 * user-visible error numbers are in the range -1 - -128: see 306 * user-visible error numbers are in the range -1 - -128: see
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 302ec20838ca..c9bf3746a9fb 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -474,6 +474,11 @@ struct inode {
474 struct dnotify_struct *i_dnotify; /* for directory notifications */ 474 struct dnotify_struct *i_dnotify; /* for directory notifications */
475#endif 475#endif
476 476
477#ifdef CONFIG_INOTIFY
478 struct list_head inotify_watches; /* watches on this inode */
479 struct semaphore inotify_sem; /* protects the watches list */
480#endif
481
477 unsigned long i_state; 482 unsigned long i_state;
478 unsigned long dirtied_when; /* jiffies of first dirtying */ 483 unsigned long dirtied_when; /* jiffies of first dirtying */
479 484
@@ -1393,7 +1398,6 @@ extern void emergency_remount(void);
1393extern int do_remount_sb(struct super_block *sb, int flags, 1398extern int do_remount_sb(struct super_block *sb, int flags,
1394 void *data, int force); 1399 void *data, int force);
1395extern sector_t bmap(struct inode *, sector_t); 1400extern sector_t bmap(struct inode *, sector_t);
1396extern int setattr_mask(unsigned int);
1397extern int notify_change(struct dentry *, struct iattr *); 1401extern int notify_change(struct dentry *, struct iattr *);
1398extern int permission(struct inode *, int, struct nameidata *); 1402extern int permission(struct inode *, int, struct nameidata *);
1399extern int generic_permission(struct inode *, int, 1403extern int generic_permission(struct inode *, int,
diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h
new file mode 100644
index 000000000000..eb581b6cfca9
--- /dev/null
+++ b/include/linux/fsnotify.h
@@ -0,0 +1,248 @@
1#ifndef _LINUX_FS_NOTIFY_H
2#define _LINUX_FS_NOTIFY_H
3
4/*
5 * include/linux/fsnotify.h - generic hooks for filesystem notification, to
6 * reduce in-source duplication from both dnotify and inotify.
7 *
8 * We don't compile any of this away in some complicated menagerie of ifdefs.
9 * Instead, we rely on the code inside to optimize away as needed.
10 *
11 * (C) Copyright 2005 Robert Love
12 */
13
14#ifdef __KERNEL__
15
16#include <linux/dnotify.h>
17#include <linux/inotify.h>
18
19/*
20 * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
21 */
22static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
23 const char *old_name, const char *new_name,
24 int isdir)
25{
26 u32 cookie = inotify_get_cookie();
27
28 if (old_dir == new_dir)
29 inode_dir_notify(old_dir, DN_RENAME);
30 else {
31 inode_dir_notify(old_dir, DN_DELETE);
32 inode_dir_notify(new_dir, DN_CREATE);
33 }
34
35 if (isdir)
36 isdir = IN_ISDIR;
37 inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name);
38 inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name);
39}
40
41/*
42 * fsnotify_unlink - file was unlinked
43 */
44static inline void fsnotify_unlink(struct dentry *dentry, struct inode *dir)
45{
46 struct inode *inode = dentry->d_inode;
47
48 inode_dir_notify(dir, DN_DELETE);
49 inotify_inode_queue_event(dir, IN_DELETE, 0, dentry->d_name.name);
50 inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL);
51
52 inotify_inode_is_dead(inode);
53}
54
55/*
56 * fsnotify_rmdir - directory was removed
57 */
58static inline void fsnotify_rmdir(struct dentry *dentry, struct inode *inode,
59 struct inode *dir)
60{
61 inode_dir_notify(dir, DN_DELETE);
62 inotify_inode_queue_event(dir,IN_DELETE|IN_ISDIR,0,dentry->d_name.name);
63 inotify_inode_queue_event(inode, IN_DELETE_SELF | IN_ISDIR, 0, NULL);
64 inotify_inode_is_dead(inode);
65}
66
67/*
68 * fsnotify_create - 'name' was linked in
69 */
70static inline void fsnotify_create(struct inode *inode, const char *name)
71{
72 inode_dir_notify(inode, DN_CREATE);
73 inotify_inode_queue_event(inode, IN_CREATE, 0, name);
74}
75
76/*
77 * fsnotify_mkdir - directory 'name' was created
78 */
79static inline void fsnotify_mkdir(struct inode *inode, const char *name)
80{
81 inode_dir_notify(inode, DN_CREATE);
82 inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0, name);
83}
84
85/*
86 * fsnotify_access - file was read
87 */
88static inline void fsnotify_access(struct dentry *dentry)
89{
90 struct inode *inode = dentry->d_inode;
91 u32 mask = IN_ACCESS;
92
93 if (S_ISDIR(inode->i_mode))
94 mask |= IN_ISDIR;
95
96 dnotify_parent(dentry, DN_ACCESS);
97 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
98 inotify_inode_queue_event(inode, mask, 0, NULL);
99}
100
101/*
102 * fsnotify_modify - file was modified
103 */
104static inline void fsnotify_modify(struct dentry *dentry)
105{
106 struct inode *inode = dentry->d_inode;
107 u32 mask = IN_MODIFY;
108
109 if (S_ISDIR(inode->i_mode))
110 mask |= IN_ISDIR;
111
112 dnotify_parent(dentry, DN_MODIFY);
113 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
114 inotify_inode_queue_event(inode, mask, 0, NULL);
115}
116
117/*
118 * fsnotify_open - file was opened
119 */
120static inline void fsnotify_open(struct dentry *dentry)
121{
122 struct inode *inode = dentry->d_inode;
123 u32 mask = IN_OPEN;
124
125 if (S_ISDIR(inode->i_mode))
126 mask |= IN_ISDIR;
127
128 inotify_inode_queue_event(inode, mask, 0, NULL);
129 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
130}
131
132/*
133 * fsnotify_close - file was closed
134 */
135static inline void fsnotify_close(struct file *file)
136{
137 struct dentry *dentry = file->f_dentry;
138 struct inode *inode = dentry->d_inode;
139 const char *name = dentry->d_name.name;
140 mode_t mode = file->f_mode;
141 u32 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
142
143 if (S_ISDIR(inode->i_mode))
144 mask |= IN_ISDIR;
145
146 inotify_dentry_parent_queue_event(dentry, mask, 0, name);
147 inotify_inode_queue_event(inode, mask, 0, NULL);
148}
149
150/*
151 * fsnotify_xattr - extended attributes were changed
152 */
153static inline void fsnotify_xattr(struct dentry *dentry)
154{
155 struct inode *inode = dentry->d_inode;
156 u32 mask = IN_ATTRIB;
157
158 if (S_ISDIR(inode->i_mode))
159 mask |= IN_ISDIR;
160
161 inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
162 inotify_inode_queue_event(inode, mask, 0, NULL);
163}
164
165/*
166 * fsnotify_change - notify_change event. file was modified and/or metadata
167 * was changed.
168 */
169static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
170{
171 struct inode *inode = dentry->d_inode;
172 int dn_mask = 0;
173 u32 in_mask = 0;
174
175 if (ia_valid & ATTR_UID) {
176 in_mask |= IN_ATTRIB;
177 dn_mask |= DN_ATTRIB;
178 }
179 if (ia_valid & ATTR_GID) {
180 in_mask |= IN_ATTRIB;
181 dn_mask |= DN_ATTRIB;
182 }
183 if (ia_valid & ATTR_SIZE) {
184 in_mask |= IN_MODIFY;
185 dn_mask |= DN_MODIFY;
186 }
187 /* both times implies a utime(s) call */
188 if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
189 {
190 in_mask |= IN_ATTRIB;
191 dn_mask |= DN_ATTRIB;
192 } else if (ia_valid & ATTR_ATIME) {
193 in_mask |= IN_ACCESS;
194 dn_mask |= DN_ACCESS;
195 } else if (ia_valid & ATTR_MTIME) {
196 in_mask |= IN_MODIFY;
197 dn_mask |= DN_MODIFY;
198 }
199 if (ia_valid & ATTR_MODE) {
200 in_mask |= IN_ATTRIB;
201 dn_mask |= DN_ATTRIB;
202 }
203
204 if (dn_mask)
205 dnotify_parent(dentry, dn_mask);
206 if (in_mask) {
207 if (S_ISDIR(inode->i_mode))
208 in_mask |= IN_ISDIR;
209 inotify_inode_queue_event(inode, in_mask, 0, NULL);
210 inotify_dentry_parent_queue_event(dentry, in_mask, 0,
211 dentry->d_name.name);
212 }
213}
214
215#ifdef CONFIG_INOTIFY /* inotify helpers */
216
217/*
218 * fsnotify_oldname_init - save off the old filename before we change it
219 */
220static inline const char *fsnotify_oldname_init(const char *name)
221{
222 return kstrdup(name, GFP_KERNEL);
223}
224
225/*
226 * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
227 */
228static inline void fsnotify_oldname_free(const char *old_name)
229{
230 kfree(old_name);
231}
232
233#else /* CONFIG_INOTIFY */
234
235static inline const char *fsnotify_oldname_init(const char *name)
236{
237 return NULL;
238}
239
240static inline void fsnotify_oldname_free(const char *old_name)
241{
242}
243
244#endif /* ! CONFIG_INOTIFY */
245
246#endif /* __KERNEL__ */
247
248#endif /* _LINUX_FS_NOTIFY_H */
diff --git a/include/linux/inotify.h b/include/linux/inotify.h
new file mode 100644
index 000000000000..a40c2bf0408e
--- /dev/null
+++ b/include/linux/inotify.h
@@ -0,0 +1,108 @@
1/*
2 * Inode based directory notification for Linux
3 *
4 * Copyright (C) 2005 John McCutchan
5 */
6
7#ifndef _LINUX_INOTIFY_H
8#define _LINUX_INOTIFY_H
9
10#include <linux/types.h>
11
12/*
13 * struct inotify_event - structure read from the inotify device for each event
14 *
15 * When you are watching a directory, you will receive the filename for events
16 * such as IN_CREATE, IN_DELETE, IN_OPEN, IN_CLOSE, ..., relative to the wd.
17 */
18struct inotify_event {
19 __s32 wd; /* watch descriptor */
20 __u32 mask; /* watch mask */
21 __u32 cookie; /* cookie to synchronize two events */
22 __u32 len; /* length (including nulls) of name */
23 char name[0]; /* stub for possible name */
24};
25
26/* the following are legal, implemented events that user-space can watch for */
27#define IN_ACCESS 0x00000001 /* File was accessed */
28#define IN_MODIFY 0x00000002 /* File was modified */
29#define IN_ATTRIB 0x00000004 /* Metadata changed */
30#define IN_CLOSE_WRITE 0x00000008 /* Writtable file was closed */
31#define IN_CLOSE_NOWRITE 0x00000010 /* Unwrittable file closed */
32#define IN_OPEN 0x00000020 /* File was opened */
33#define IN_MOVED_FROM 0x00000040 /* File was moved from X */
34#define IN_MOVED_TO 0x00000080 /* File was moved to Y */
35#define IN_CREATE 0x00000100 /* Subfile was created */
36#define IN_DELETE 0x00000200 /* Subfile was deleted */
37#define IN_DELETE_SELF 0x00000400 /* Self was deleted */
38
39/* the following are legal events. they are sent as needed to any watch */
40#define IN_UNMOUNT 0x00002000 /* Backing fs was unmounted */
41#define IN_Q_OVERFLOW 0x00004000 /* Event queued overflowed */
42#define IN_IGNORED 0x00008000 /* File was ignored */
43
44/* helper events */
45#define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* close */
46#define IN_MOVE (IN_MOVED_FROM | IN_MOVED_TO) /* moves */
47
48/* special flags */
49#define IN_ISDIR 0x40000000 /* event occurred against dir */
50#define IN_ONESHOT 0x80000000 /* only send event once */
51
52/*
53 * All of the events - we build the list by hand so that we can add flags in
54 * the future and not break backward compatibility. Apps will get only the
55 * events that they originally wanted. Be sure to add new events here!
56 */
57#define IN_ALL_EVENTS (IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE | \
58 IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM | \
59 IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF)
60
61#ifdef __KERNEL__
62
63#include <linux/dcache.h>
64#include <linux/fs.h>
65#include <linux/config.h>
66
67#ifdef CONFIG_INOTIFY
68
69extern void inotify_inode_queue_event(struct inode *, __u32, __u32,
70 const char *);
71extern void inotify_dentry_parent_queue_event(struct dentry *, __u32, __u32,
72 const char *);
73extern void inotify_unmount_inodes(struct list_head *);
74extern void inotify_inode_is_dead(struct inode *);
75extern u32 inotify_get_cookie(void);
76
77#else
78
79static inline void inotify_inode_queue_event(struct inode *inode,
80 __u32 mask, __u32 cookie,
81 const char *filename)
82{
83}
84
85static inline void inotify_dentry_parent_queue_event(struct dentry *dentry,
86 __u32 mask, __u32 cookie,
87 const char *filename)
88{
89}
90
91static inline void inotify_unmount_inodes(struct list_head *list)
92{
93}
94
95static inline void inotify_inode_is_dead(struct inode *inode)
96{
97}
98
99static inline u32 inotify_get_cookie(void)
100{
101 return 0;
102}
103
104#endif /* CONFIG_INOTIFY */
105
106#endif /* __KERNEL __ */
107
108#endif /* _LINUX_INOTIFY_H */
diff --git a/include/linux/sched.h b/include/linux/sched.h
index ff48815bd3a2..dec5827c7742 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -410,6 +410,10 @@ struct user_struct {
410 atomic_t processes; /* How many processes does this user have? */ 410 atomic_t processes; /* How many processes does this user have? */
411 atomic_t files; /* How many open files does this user have? */ 411 atomic_t files; /* How many open files does this user have? */
412 atomic_t sigpending; /* How many pending signals does this user have? */ 412 atomic_t sigpending; /* How many pending signals does this user have? */
413#ifdef CONFIG_INOTIFY
414 atomic_t inotify_watches; /* How many inotify watches does this user have? */
415 atomic_t inotify_devs; /* How many inotify devs does this user have opened? */
416#endif
413 /* protected by mq_lock */ 417 /* protected by mq_lock */
414 unsigned long mq_bytes; /* How many bytes can be allocated to mqueue? */ 418 unsigned long mq_bytes; /* How many bytes can be allocated to mqueue? */
415 unsigned long locked_shm; /* How many pages of mlocked shm ? */ 419 unsigned long locked_shm; /* How many pages of mlocked shm ? */
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index 5b5f434ac9a0..ce19a2aa0b21 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -61,7 +61,8 @@ enum
61 CTL_DEV=7, /* Devices */ 61 CTL_DEV=7, /* Devices */
62 CTL_BUS=8, /* Busses */ 62 CTL_BUS=8, /* Busses */
63 CTL_ABI=9, /* Binary emulation */ 63 CTL_ABI=9, /* Binary emulation */
64 CTL_CPU=10 /* CPU stuff (speed scaling, etc) */ 64 CTL_CPU=10, /* CPU stuff (speed scaling, etc) */
65 CTL_INOTIFY=11 /* Inotify */
65}; 66};
66 67
67/* CTL_BUS names: */ 68/* CTL_BUS names: */
@@ -70,6 +71,14 @@ enum
70 CTL_BUS_ISA=1 /* ISA */ 71 CTL_BUS_ISA=1 /* ISA */
71}; 72};
72 73
74/* CTL_INOTIFY names: */
75enum
76{
77 INOTIFY_MAX_USER_DEVICES=1, /* max number of inotify device instances per user */
78 INOTIFY_MAX_USER_WATCHES=2, /* max number of inotify watches per user */
79 INOTIFY_MAX_QUEUED_EVENTS=3 /* Max number of queued events per inotify device instance */
80};
81
73/* CTL_KERN names: */ 82/* CTL_KERN names: */
74enum 83enum
75{ 84{