/[linux-patches]/genpatches-2.6/trunk/2.6.12-pre/4800_inotify-0.22-3.patch
Gentoo

Contents of /genpatches-2.6/trunk/2.6.12-pre/4800_inotify-0.22-3.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations) (download)
Sat Jun 11 23:20:41 2005 UTC (12 years, 6 months ago) by dsd
File size: 53128 byte(s)
Create 2.6.12 branch
1 inotify!
2
3 inotify is intended to correct the deficiencies of dnotify, particularly
4 its inability to scale and its terrible user interface:
5
6 * dnotify requires the opening of one fd per each directory
7 that you intend to watch. This quickly results in too many
8 open files and pins removable media, preventing unmount.
9 * dnotify is directory-based. You only learn about changes to
10 directories. Sure, a change to a file in a directory affects
11 the directory, but you are then forced to keep a cache of
12 stat structures.
13 * dnotify's interface to user-space is awful. Signals?
14
15 inotify provides a more usable, simple, powerful solution to file change
16 notification:
17
18 * inotify's interface is a device node, not SIGIO. You open a
19 single fd to the device node, which is select()-able.
20 * inotify has an event that says "the filesystem that the item
21 you were watching is on was unmounted."
22 * inotify can watch directories or files.
23
24 Inotify is currently used by Beagle (a desktop search infrastructure),
25 Gamin (a FAM replacement), and other projects.
26
27
28 Signed-off-by: Robert Love <rml@novell.com>
29
30 Documentation/filesystems/inotify.txt | 81 ++
31 fs/Kconfig | 13
32 fs/Makefile | 1
33 fs/attr.c | 33 -
34 fs/compat.c | 12
35 fs/file_table.c | 3
36 fs/inode.c | 6
37 fs/inotify.c | 961 ++++++++++++++++++++++++++++++++++
38 fs/namei.c | 30 -
39 fs/open.c | 4
40 fs/read_write.c | 15
41 fs/xattr.c | 5
42 include/linux/fs.h | 6
43 include/linux/fsnotify.h | 230 ++++++++
44 include/linux/inotify.h | 111 +++
45 include/linux/sched.h | 4
46 kernel/user.c | 4
47 17 files changed, 1463 insertions(+), 56 deletions(-)
48
49 diff -urN linux-2.6.12-rc3/Documentation/filesystems/inotify.txt linux/Documentation/filesystems/inotify.txt
50 --- linux-2.6.12-rc3/Documentation/filesystems/inotify.txt 1969-12-31 19:00:00.000000000 -0500
51 +++ linux/Documentation/filesystems/inotify.txt 2005-04-21 00:56:28.000000000 -0400
52 @@ -0,0 +1,81 @@
53 + inotify
54 + a powerful yet simple file change notification system
55 +
56 +
57 +
58 +Document started 15 Mar 2005 by Robert Love <rml@novell.com>
59 +
60 +(i) User Interface
61 +
62 +Inotify is controlled by a device node, /dev/inotify. If you do not use udev,
63 +this device may need to be created manually. First step, open it
64 +
65 + int dev_fd = open ("/dev/inotify", O_RDONLY);
66 +
67 +Change events are managed by "watches". A watch is an (object,mask) pair where
68 +the object is a file or directory and the mask is a bitmask of one or more
69 +inotify events that the application wishes to receive. See <linux/inotify.h>
70 +for valid events. A watch is referenced by a watch descriptor, or wd.
71 +
72 +Watches are added via a file descriptor.
73 +
74 +Watches on a directory will return events on any files inside of the directory.
75 +
76 +Adding a watch is simple,
77 +
78 + /* 'wd' represents the watch on fd with mask */
79 + struct inotify_request req = { fd, mask };
80 + int wd = ioctl (dev_fd, INOTIFY_WATCH, &req);
81 +
82 +You can add a large number of files via something like
83 +
84 + for each file to watch {
85 + struct inotify_request req;
86 + int file_fd;
87 +
88 + file_fd = open (file, O_RDONLY);
89 + if (fd < 0) {
90 + perror ("open");
91 + break;
92 + }
93 +
94 + req.fd = file_fd;
95 + req.mask = mask;
96 +
97 + wd = ioctl (dev_fd, INOTIFY_WATCH, &req);
98 +
99 + close (fd);
100 + }
101 +
102 +You can update an existing watch in the same manner, by passing in a new mask.
103 +
104 +An existing watch is removed via the INOTIFY_IGNORE ioctl, for example
105 +
106 + ioctl (dev_fd, INOTIFY_IGNORE, wd);
107 +
108 +Events are provided in the form of an inotify_event structure that is read(2)
109 +from /dev/inotify. The filename is of dynamic length and follows the struct.
110 +It is of size len. The filename is padded with null bytes to ensure proper
111 +alignment. This padding is reflected in len.
112 +
113 +You can slurp multiple events by passing a large buffer, for example
114 +
115 + size_t len = read (fd, buf, BUF_LEN);
116 +
117 +Will return as many events as are available and fit in BUF_LEN.
118 +
119 +/dev/inotify is also select() and poll() able.
120 +
121 +You can find the size of the current event queue via the FIONREAD ioctl.
122 +
123 +All watches are destroyed and cleaned up on close.
124 +
125 +
126 +(ii) Internal Kernel Implementation
127 +
128 +Each open inotify device is associated with an inotify_device structure.
129 +
130 +Each watch is associated with an inotify_watch structure. Watches are chained
131 +off of each associated device and each associated inode.
132 +
133 +See fs/inotify.c for the locking and lifetime rules.
134 diff -urN linux-2.6.12-rc3/fs/attr.c linux/fs/attr.c
135 --- linux-2.6.12-rc3/fs/attr.c 2005-04-20 22:47:05.000000000 -0400
136 +++ linux/fs/attr.c 2005-04-21 00:56:28.000000000 -0400
137 @@ -10,7 +10,7 @@
138 #include <linux/mm.h>
139 #include <linux/string.h>
140 #include <linux/smp_lock.h>
141 -#include <linux/dnotify.h>
142 +#include <linux/fsnotify.h>
143 #include <linux/fcntl.h>
144 #include <linux/quotaops.h>
145 #include <linux/security.h>
146 @@ -107,31 +107,8 @@
147 out:
148 return error;
149 }
150 -
151 EXPORT_SYMBOL(inode_setattr);
152
153 -int setattr_mask(unsigned int ia_valid)
154 -{
155 - unsigned long dn_mask = 0;
156 -
157 - if (ia_valid & ATTR_UID)
158 - dn_mask |= DN_ATTRIB;
159 - if (ia_valid & ATTR_GID)
160 - dn_mask |= DN_ATTRIB;
161 - if (ia_valid & ATTR_SIZE)
162 - dn_mask |= DN_MODIFY;
163 - /* both times implies a utime(s) call */
164 - if ((ia_valid & (ATTR_ATIME|ATTR_MTIME)) == (ATTR_ATIME|ATTR_MTIME))
165 - dn_mask |= DN_ATTRIB;
166 - else if (ia_valid & ATTR_ATIME)
167 - dn_mask |= DN_ACCESS;
168 - else if (ia_valid & ATTR_MTIME)
169 - dn_mask |= DN_MODIFY;
170 - if (ia_valid & ATTR_MODE)
171 - dn_mask |= DN_ATTRIB;
172 - return dn_mask;
173 -}
174 -
175 int notify_change(struct dentry * dentry, struct iattr * attr)
176 {
177 struct inode *inode = dentry->d_inode;
178 @@ -197,11 +174,9 @@
179 if (ia_valid & ATTR_SIZE)
180 up_write(&dentry->d_inode->i_alloc_sem);
181
182 - if (!error) {
183 - unsigned long dn_mask = setattr_mask(ia_valid);
184 - if (dn_mask)
185 - dnotify_parent(dentry, dn_mask);
186 - }
187 + if (!error)
188 + fsnotify_change(dentry, ia_valid);
189 +
190 return error;
191 }
192
193 diff -urN linux-2.6.12-rc3/fs/compat.c linux/fs/compat.c
194 --- linux-2.6.12-rc3/fs/compat.c 2005-04-20 22:47:06.000000000 -0400
195 +++ linux/fs/compat.c 2005-04-21 00:56:28.000000000 -0400
196 @@ -37,7 +37,7 @@
197 #include <linux/ctype.h>
198 #include <linux/module.h>
199 #include <linux/dirent.h>
200 -#include <linux/dnotify.h>
201 +#include <linux/fsnotify.h>
202 #include <linux/highuid.h>
203 #include <linux/sunrpc/svc.h>
204 #include <linux/nfsd/nfsd.h>
205 @@ -1307,9 +1307,13 @@
206 out:
207 if (iov != iovstack)
208 kfree(iov);
209 - if ((ret + (type == READ)) > 0)
210 - dnotify_parent(file->f_dentry,
211 - (type == READ) ? DN_ACCESS : DN_MODIFY);
212 + if ((ret + (type == READ)) > 0) {
213 + struct dentry *dentry = file->f_dentry;
214 + if (type == READ)
215 + fsnotify_access(dentry);
216 + else
217 + fsnotify_modify(dentry);
218 + }
219 return ret;
220 }
221
222 diff -urN linux-2.6.12-rc3/fs/file_table.c linux/fs/file_table.c
223 --- linux-2.6.12-rc3/fs/file_table.c 2005-03-02 02:37:47.000000000 -0500
224 +++ linux/fs/file_table.c 2005-04-21 00:56:28.000000000 -0400
225 @@ -16,6 +16,7 @@
226 #include <linux/eventpoll.h>
227 #include <linux/mount.h>
228 #include <linux/cdev.h>
229 +#include <linux/fsnotify.h>
230
231 /* sysctl tunables... */
232 struct files_stat_struct files_stat = {
233 @@ -123,6 +124,8 @@
234 struct inode *inode = dentry->d_inode;
235
236 might_sleep();
237 +
238 + fsnotify_close(file);
239 /*
240 * The function eventpoll_release() should be the first called
241 * in the file cleanup chain.
242 diff -urN linux-2.6.12-rc3/fs/inode.c linux/fs/inode.c
243 --- linux-2.6.12-rc3/fs/inode.c 2005-04-20 22:47:06.000000000 -0400
244 +++ linux/fs/inode.c 2005-04-21 00:56:28.000000000 -0400
245 @@ -21,6 +21,7 @@
246 #include <linux/pagemap.h>
247 #include <linux/cdev.h>
248 #include <linux/bootmem.h>
249 +#include <linux/inotify.h>
250
251 /*
252 * This is needed for the following functions:
253 @@ -129,6 +130,10 @@
254 #ifdef CONFIG_QUOTA
255 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
256 #endif
257 +#ifdef CONFIG_INOTIFY
258 + INIT_LIST_HEAD(&inode->inotify_watches);
259 + sema_init(&inode->inotify_sem, 1);
260 +#endif
261 inode->i_pipe = NULL;
262 inode->i_bdev = NULL;
263 inode->i_cdev = NULL;
264 @@ -355,6 +360,7 @@
265
266 down(&iprune_sem);
267 spin_lock(&inode_lock);
268 + inotify_unmount_inodes(&sb->s_inodes);
269 busy = invalidate_list(&sb->s_inodes, &throw_away);
270 spin_unlock(&inode_lock);
271
272 diff -urN linux-2.6.12-rc3/fs/inotify.c linux/fs/inotify.c
273 --- linux-2.6.12-rc3/fs/inotify.c 1969-12-31 19:00:00.000000000 -0500
274 +++ linux/fs/inotify.c 2005-04-21 02:42:40.000000000 -0400
275 @@ -0,0 +1,961 @@
276 +/*
277 + * fs/inotify.c - inode-based file event notifications
278 + *
279 + * Authors:
280 + * John McCutchan <ttb@tentacle.dhs.org>
281 + * Robert Love <rml@novell.com>
282 + *
283 + * Copyright (C) 2005 John McCutchan
284 + *
285 + * This program is free software; you can redistribute it and/or modify it
286 + * under the terms of the GNU General Public License as published by the
287 + * Free Software Foundation; either version 2, or (at your option) any
288 + * later version.
289 + *
290 + * This program is distributed in the hope that it will be useful, but
291 + * WITHOUT ANY WARRANTY; without even the implied warranty of
292 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
293 + * General Public License for more details.
294 + */
295 +
296 +#include <linux/module.h>
297 +#include <linux/kernel.h>
298 +#include <linux/sched.h>
299 +#include <linux/spinlock.h>
300 +#include <linux/idr.h>
301 +#include <linux/slab.h>
302 +#include <linux/fs.h>
303 +#include <linux/file.h>
304 +#include <linux/namei.h>
305 +#include <linux/poll.h>
306 +#include <linux/device.h>
307 +#include <linux/miscdevice.h>
308 +#include <linux/init.h>
309 +#include <linux/list.h>
310 +#include <linux/writeback.h>
311 +#include <linux/inotify.h>
312 +
313 +#include <asm/ioctls.h>
314 +
315 +static atomic_t inotify_cookie;
316 +
317 +static kmem_cache_t *watch_cachep;
318 +static kmem_cache_t *event_cachep;
319 +
320 +static int max_user_devices;
321 +static int max_user_watches;
322 +static unsigned int max_queued_events;
323 +
324 +/*
325 + * Lock ordering:
326 + *
327 + * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
328 + * iprune_sem (synchronize versus shrink_icache_memory())
329 + * inode_lock (protects the super_block->s_inodes list)
330 + * inode->inotify_sem (protects inode->inotify_watches and watches->i_list)
331 + * inotify_dev->sem (protects inotify_device and watches->d_list)
332 + */
333 +
334 +/*
335 + * Lifetimes of the three main data structures--inotify_device, inode, and
336 + * inotify_watch--are managed by reference count.
337 + *
338 + * inotify_device: Lifetime is from open until release. Additional references
339 + * can bump the count via get_inotify_dev() and drop the count via
340 + * put_inotify_dev().
341 + *
342 + * inotify_watch: Lifetime is from create_watch() to destory_watch().
343 + * Additional references can bump the count via get_inotify_watch() and drop
344 + * the count via put_inotify_watch().
345 + *
346 + * inode: Pinned so long as the inode is associated with a watch, from
347 + * create_watch() to put_inotify_watch().
348 + */
349 +
350 +/*
351 + * struct inotify_device - represents an open instance of an inotify device
352 + *
353 + * This structure is protected by the semaphore 'sem'.
354 + */
355 +struct inotify_device {
356 + wait_queue_head_t wq; /* wait queue for i/o */
357 + struct idr idr; /* idr mapping wd -> watch */
358 + struct semaphore sem; /* protects this bad boy */
359 + struct list_head events; /* list of queued events */
360 + struct list_head watches; /* list of watches */
361 + atomic_t count; /* reference count */
362 + struct user_struct *user; /* user who opened this dev */
363 + unsigned int queue_size; /* size of the queue (bytes) */
364 + unsigned int event_count; /* number of pending events */
365 + unsigned int max_events; /* maximum number of events */
366 +};
367 +
368 +/*
369 + * struct inotify_kernel_event - An intofiy event, originating from a watch and
370 + * queued for user-space. A list of these is attached to each instance of the
371 + * device. In read(), this list is walked and all events that can fit in the
372 + * buffer are returned.
373 + *
374 + * Protected by dev->sem of the device in which we are queued.
375 + */
376 +struct inotify_kernel_event {
377 + struct inotify_event event; /* the user-space event */
378 + struct list_head list; /* entry in inotify_device's list */
379 + char *name; /* filename, if any */
380 +};
381 +
382 +/*
383 + * struct inotify_watch - represents a watch request on a specific inode
384 + *
385 + * d_list is protected by dev->sem of the associated watch->dev.
386 + * i_list and mask are protected by inode->inotify_sem of the associated inode.
387 + * dev, inode, and wd are never written to once the watch is created.
388 + */
389 +struct inotify_watch {
390 + struct list_head d_list; /* entry in inotify_device's list */
391 + struct list_head i_list; /* entry in inode's list */
392 + atomic_t count; /* reference count */
393 + struct inotify_device *dev; /* associated device */
394 + struct inode *inode; /* associated inode */
395 + s32 wd; /* watch descriptor */
396 + u32 mask; /* event mask for this watch */
397 +};
398 +
399 +static ssize_t show_max_queued_events(struct class_device *class, char *buf)
400 +{
401 + return sprintf(buf, "%d\n", max_queued_events);
402 +}
403 +
404 +static ssize_t store_max_queued_events(struct class_device *class,
405 + const char *buf, size_t count)
406 +{
407 + unsigned int max;
408 +
409 + if (sscanf(buf, "%u", &max) > 0 && max > 0) {
410 + max_queued_events = max;
411 + return strlen(buf);
412 + }
413 + return -EINVAL;
414 +}
415 +
416 +static ssize_t show_max_user_devices(struct class_device *class, char *buf)
417 +{
418 + return sprintf(buf, "%d\n", max_user_devices);
419 +}
420 +
421 +static ssize_t store_max_user_devices(struct class_device *class,
422 + const char *buf, size_t count)
423 +{
424 + int max;
425 +
426 + if (sscanf(buf, "%d", &max) > 0 && max > 0) {
427 + max_user_devices = max;
428 + return strlen(buf);
429 + }
430 + return -EINVAL;
431 +}
432 +
433 +static ssize_t show_max_user_watches(struct class_device *class, char *buf)
434 +{
435 + return sprintf(buf, "%d\n", max_user_watches);
436 +}
437 +
438 +static ssize_t store_max_user_watches(struct class_device *class,
439 + const char *buf, size_t count)
440 +{
441 + int max;
442 +
443 + if (sscanf(buf, "%d", &max) > 0 && max > 0) {
444 + max_user_watches = max;
445 + return strlen(buf);
446 + }
447 + return -EINVAL;
448 +}
449 +
450 +static CLASS_DEVICE_ATTR(max_queued_events, S_IRUGO | S_IWUSR,
451 + show_max_queued_events, store_max_queued_events);
452 +static CLASS_DEVICE_ATTR(max_user_devices, S_IRUGO | S_IWUSR,
453 + show_max_user_devices, store_max_user_devices);
454 +static CLASS_DEVICE_ATTR(max_user_watches, S_IRUGO | S_IWUSR,
455 + show_max_user_watches, store_max_user_watches);
456 +
457 +static inline void get_inotify_dev(struct inotify_device *dev)
458 +{
459 + atomic_inc(&dev->count);
460 +}
461 +
462 +static inline void put_inotify_dev(struct inotify_device *dev)
463 +{
464 + if (atomic_dec_and_test(&dev->count)) {
465 + atomic_dec(&dev->user->inotify_devs);
466 + free_uid(dev->user);
467 + kfree(dev);
468 + }
469 +}
470 +
471 +static inline void get_inotify_watch(struct inotify_watch *watch)
472 +{
473 + atomic_inc(&watch->count);
474 +}
475 +
476 +/*
477 + * put_inotify_watch - decrements the ref count on a given watch. cleans up
478 + * the watch and its references if the count reaches zero.
479 + */
480 +static inline void put_inotify_watch(struct inotify_watch *watch)
481 +{
482 + if (atomic_dec_and_test(&watch->count)) {
483 + put_inotify_dev(watch->dev);
484 + iput(watch->inode);
485 + kmem_cache_free(watch_cachep, watch);
486 + }
487 +}
488 +
489 +/*
490 + * kernel_event - create a new kernel event with the given parameters
491 + *
492 + * This function can sleep.
493 + */
494 +static struct inotify_kernel_event * kernel_event(s32 wd, u32 mask, u32 cookie,
495 + const char *name)
496 +{
497 + struct inotify_kernel_event *kevent;
498 +
499 + kevent = kmem_cache_alloc(event_cachep, GFP_KERNEL);
500 + if (unlikely(!kevent))
501 + return NULL;
502 +
503 + /* we hand this out to user-space, so zero it just in case */
504 + memset(&kevent->event, 0, sizeof(struct inotify_event));
505 +
506 + kevent->event.wd = wd;
507 + kevent->event.mask = mask;
508 + kevent->event.cookie = cookie;
509 +
510 + INIT_LIST_HEAD(&kevent->list);
511 +
512 + if (name) {
513 + size_t len, rem, event_size = sizeof(struct inotify_event);
514 +
515 + /*
516 + * We need to pad the filename so as to properly align an
517 + * array of inotify_event structures. Because the structure is
518 + * small and the common case is a small filename, we just round
519 + * up to the next multiple of the structure's sizeof. This is
520 + * simple and safe for all architectures.
521 + */
522 + len = strlen(name) + 1;
523 + rem = event_size - len;
524 + if (len > event_size) {
525 + rem = event_size - (len % event_size);
526 + if (len % event_size == 0)
527 + rem = 0;
528 + }
529 + len += rem;
530 +
531 + kevent->name = kmalloc(len, GFP_KERNEL);
532 + if (unlikely(!kevent->name)) {
533 + kmem_cache_free(event_cachep, kevent);
534 + return NULL;
535 + }
536 + memset(kevent->name, 0, len);
537 + strncpy(kevent->name, name, strlen(name));
538 + kevent->event.len = len;
539 + } else {
540 + kevent->event.len = 0;
541 + kevent->name = NULL;
542 + }
543 +
544 + return kevent;
545 +}
546 +
547 +/*
548 + * inotify_dev_get_event - return the next event in the given dev's queue
549 + *
550 + * Caller must hold dev->sem.
551 + */
552 +static inline struct inotify_kernel_event *
553 +inotify_dev_get_event(struct inotify_device *dev)
554 +{
555 + return list_entry(dev->events.next, struct inotify_kernel_event, list);
556 +}
557 +
558 +/*
559 + * inotify_dev_queue_event - add a new event to the given device
560 + *
561 + * Caller must hold dev->sem. Can sleep (calls kernel_event()).
562 + */
563 +static void inotify_dev_queue_event(struct inotify_device *dev,
564 + struct inotify_watch *watch, u32 mask,
565 + u32 cookie, const char *name)
566 +{
567 + struct inotify_kernel_event *kevent, *last;
568 +
569 + /* coalescing: drop this event if it is a dupe of the previous */
570 + last = inotify_dev_get_event(dev);
571 + if (dev->event_count && last->event.mask == mask &&
572 + last->event.cookie == cookie &&
573 + last->event.wd == watch->wd) {
574 + const char *lastname = last->name;
575 +
576 + if (!name && !lastname)
577 + return;
578 + if (name && lastname && !strcmp(lastname, name))
579 + return;
580 + }
581 +
582 + /* the queue overflowed and we already sent the Q_OVERFLOW event */
583 + if (unlikely(dev->event_count > dev->max_events))
584 + return;
585 +
586 + /* if the queue overflows, we need to notify user space */
587 + if (unlikely(dev->event_count == dev->max_events))
588 + kevent = kernel_event(-1, IN_Q_OVERFLOW, cookie, NULL);
589 + else
590 + kevent = kernel_event(watch->wd, mask, cookie, name);
591 +
592 + if (unlikely(!kevent))
593 + return;
594 +
595 + /* queue the event and wake up anyone waiting */
596 + dev->event_count++;
597 + dev->queue_size += sizeof(struct inotify_event) + kevent->event.len;
598 + list_add_tail(&kevent->list, &dev->events);
599 + wake_up_interruptible(&dev->wq);
600 +}
601 +
602 +/*
603 + * remove_kevent - cleans up and ultimately frees the given kevent
604 + *
605 + * Caller must hold dev->sem.
606 + */
607 +static void remove_kevent(struct inotify_device *dev,
608 + struct inotify_kernel_event *kevent)
609 +{
610 + list_del(&kevent->list);
611 +
612 + dev->event_count--;
613 + dev->queue_size -= sizeof(struct inotify_event) + kevent->event.len;
614 +
615 + kfree(kevent->name);
616 + kmem_cache_free(event_cachep, kevent);
617 +}
618 +
619 +/*
620 + * inotify_dev_event_dequeue - destroy an event on the given device
621 + *
622 + * Caller must hold dev->sem.
623 + */
624 +static void inotify_dev_event_dequeue(struct inotify_device *dev)
625 +{
626 + if (!list_empty(&dev->events)) {
627 + struct inotify_kernel_event *kevent;
628 + kevent = inotify_dev_get_event(dev);
629 + remove_kevent(dev, kevent);
630 + }
631 +}
632 +
633 +/*
634 + * inotify_dev_get_wd - returns the next WD for use by the given dev
635 + *
636 + * Callers must hold dev->sem. This function can sleep.
637 + */
638 +static int inotify_dev_get_wd(struct inotify_device *dev,
639 + struct inotify_watch *watch)
640 +{
641 + int ret;
642 +
643 + do {
644 + if (unlikely(!idr_pre_get(&dev->idr, GFP_KERNEL)))
645 + return -ENOSPC;
646 + ret = idr_get_new(&dev->idr, watch, &watch->wd);
647 + } while (ret == -EAGAIN);
648 +
649 + return ret;
650 +}
651 +
652 +/*
653 + * create_watch - creates a watch on the given device.
654 + *
655 + * Callers must hold dev->sem. Calls inotify_dev_get_wd() so may sleep.
656 + * Both 'dev' and 'inode' (by way of nameidata) need to be pinned.
657 + */
658 +static struct inotify_watch *create_watch(struct inotify_device *dev,
659 + u32 mask, struct inode *inode)
660 +{
661 + struct inotify_watch *watch;
662 + int ret;
663 +
664 + if (atomic_read(&dev->user->inotify_watches) >= max_user_watches)
665 + return ERR_PTR(-ENOSPC);
666 +
667 + watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL);
668 + if (unlikely(!watch))
669 + return ERR_PTR(-ENOMEM);
670 +
671 + ret = inotify_dev_get_wd(dev, watch);
672 + if (unlikely(ret)) {
673 + kmem_cache_free(watch_cachep, watch);
674 + return ERR_PTR(ret);
675 + }
676 +
677 + watch->mask = mask;
678 + atomic_set(&watch->count, 0);
679 + INIT_LIST_HEAD(&watch->d_list);
680 + INIT_LIST_HEAD(&watch->i_list);
681 +
682 + /* save a reference to device and bump the count to make it official */
683 + get_inotify_dev(dev);
684 + watch->dev = dev;
685 +
686 + /*
687 + * Save a reference to the inode and bump the ref count to make it
688 + * official. We hold a reference to nameidata, which makes this safe.
689 + */
690 + watch->inode = igrab(inode);
691 +
692 + /* bump our own count, corresponding to our entry in dev->watches */
693 + get_inotify_watch(watch);
694 +
695 + atomic_inc(&dev->user->inotify_watches);
696 +
697 + return watch;
698 +}
699 +
700 +/*
701 + * inotify_find_dev - find the watch associated with the given inode and dev
702 + *
703 + * Callers must hold inode->inotify_sem.
704 + */
705 +static struct inotify_watch *inode_find_dev(struct inode *inode,
706 + struct inotify_device *dev)
707 +{
708 + struct inotify_watch *watch;
709 +
710 + list_for_each_entry(watch, &inode->inotify_watches, i_list) {
711 + if (watch->dev == dev)
712 + return watch;
713 + }
714 +
715 + return NULL;
716 +}
717 +
718 +/*
719 + * inotify_dev_is_watching_inode - is this device watching this inode?
720 + *
721 + * Callers must hold dev->sem.
722 + */
723 +static inline int inotify_dev_is_watching_inode(struct inotify_device *dev,
724 + struct inode *inode)
725 +{
726 + struct inotify_watch *watch;
727 +
728 + list_for_each_entry(watch, &dev->watches, d_list) {
729 + if (watch->inode == inode)
730 + return 1;
731 + }
732 +
733 + return 0;
734 +}
735 +
736 +/*
737 + * remove_watch_no_event - remove_watch() without the IN_IGNORED event.
738 + */
739 +static void remove_watch_no_event(struct inotify_watch *watch,
740 + struct inotify_device *dev)
741 +{
742 + list_del(&watch->i_list);
743 + list_del(&watch->d_list);
744 +
745 + atomic_dec(&dev->user->inotify_watches);
746 + idr_remove(&dev->idr, watch->wd);
747 + put_inotify_watch(watch);
748 +}
749 +
750 +/*
751 + * remove_watch - Remove a watch from both the device and the inode. Sends
752 + * the IN_IGNORED event to the given device signifying that the inode is no
753 + * longer watched.
754 + *
755 + * Callers must hold both inode->inotify_sem and dev->sem. We drop a
756 + * reference to the inode before returning.
757 + *
758 + * The inode is not iput() so as to remain atomic. If the inode needs to be
759 + * iput(), the call returns one. Otherwise, it returns zero.
760 + */
761 +static void remove_watch(struct inotify_watch *watch,struct inotify_device *dev)
762 +{
763 + inotify_dev_queue_event(dev, watch, IN_IGNORED, 0, NULL);
764 + remove_watch_no_event(watch, dev);
765 +}
766 +
767 +/* Kernel API */
768 +
769 +/**
770 + * inotify_inode_queue_event - queue an event to all watches on this inode
771 + * @inode: inode event is originating from
772 + * @mask: event mask describing this event
773 + * @cookie: cookie for synchronization, or zero
774 + * @name: filename, if any
775 + */
776 +void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
777 + const char *name)
778 +{
779 + struct inotify_watch *watch;
780 +
781 + down(&inode->inotify_sem);
782 + list_for_each_entry(watch, &inode->inotify_watches, i_list) {
783 + if (watch->mask & mask) {
784 + struct inotify_device *dev = watch->dev;
785 + down(&dev->sem);
786 + inotify_dev_queue_event(dev, watch, mask, cookie, name);
787 + up(&dev->sem);
788 + }
789 + }
790 + up(&inode->inotify_sem);
791 +}
792 +EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
793 +
794 +/**
795 + * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
796 + * @dentry: the dentry in question, we queue against this dentry's parent
797 + * @mask: event mask describing this event
798 + * @cookie: cookie for synchronization, or zero
799 + * @name: filename, if any
800 + */
801 +void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
802 + u32 cookie, const char *name)
803 +{
804 + struct dentry *parent;
805 + struct inode *inode;
806 +
807 + spin_lock(&dentry->d_lock);
808 + parent = dentry->d_parent;
809 + inode = parent->d_inode;
810 +
811 + /* We intentially do the list_empty() lockless. The race is fine. */
812 + if (!list_empty(&inode->inotify_watches)) {
813 + dget(parent);
814 + spin_unlock(&dentry->d_lock);
815 + inotify_inode_queue_event(inode, mask, cookie, name);
816 + dput(parent);
817 + } else
818 + spin_unlock(&dentry->d_lock);
819 +}
820 +EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
821 +
822 +/**
823 + * inotify_get_cookie - return a unique cookie for use in synchronizing events.
824 + */
825 +u32 inotify_get_cookie(void)
826 +{
827 + return atomic_inc_return(&inotify_cookie);
828 +}
829 +EXPORT_SYMBOL_GPL(inotify_get_cookie);
830 +
831 +/**
832 + * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
833 + * @list: list of inodes being unmounted (sb->s_inodes)
834 + *
835 + * Called with inode_lock held, protecting the unmounting super block's list
836 + * of inodes, and with iprune_sem held, keeping shrink_icache_memory() at bay.
837 + * We temporarily drop inode_lock, however, and CAN block.
838 + */
839 +void inotify_unmount_inodes(struct list_head *list)
840 +{
841 + struct inode *inode, *next_i;
842 +
843 + list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
844 + struct inotify_watch *watch, *next_w;
845 + struct list_head *watches;
846 +
847 + /*
848 + * We can safely drop inode_lock here because the per-sb list
849 + * of inodes must not change during unmount and iprune_sem
850 + * keeps shrink_icache_memory() away.
851 + */
852 + spin_unlock(&inode_lock);
853 +
854 + /* for each watch, send IN_UNMOUNT and then remove it */
855 + down(&inode->inotify_sem);
856 + watches = &inode->inotify_watches;
857 + list_for_each_entry_safe(watch, next_w, watches, i_list) {
858 + struct inotify_device *dev = watch->dev;
859 + down(&dev->sem);
860 + inotify_dev_queue_event(dev, watch, IN_UNMOUNT,0,NULL);
861 + remove_watch(watch, dev);
862 + up(&dev->sem);
863 + }
864 + up(&inode->inotify_sem);
865 +
866 + spin_lock(&inode_lock);
867 + }
868 +}
869 +EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
870 +
871 +/**
872 + * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
873 + * @inode: inode that is about to be removed
874 + */
875 +void inotify_inode_is_dead(struct inode *inode)
876 +{
877 + struct inotify_watch *watch, *next;
878 +
879 + down(&inode->inotify_sem);
880 + list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
881 + struct inotify_device *dev = watch->dev;
882 + down(&dev->sem);
883 + remove_watch(watch, dev);
884 + up(&dev->sem);
885 + }
886 + up(&inode->inotify_sem);
887 +}
888 +EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
889 +
890 +/* Device Interface */
891 +
892 +static unsigned int inotify_poll(struct file *file, poll_table *wait)
893 +{
894 + struct inotify_device *dev = file->private_data;
895 + int ret = 0;
896 +
897 + poll_wait(file, &dev->wq, wait);
898 + down(&dev->sem);
899 + if (!list_empty(&dev->events))
900 + ret = POLLIN | POLLRDNORM;
901 + up(&dev->sem);
902 +
903 + return ret;
904 +}
905 +
906 +static ssize_t inotify_read(struct file *file, char __user *buf,
907 + size_t count, loff_t *pos)
908 +{
909 + size_t event_size = sizeof (struct inotify_event);
910 + struct inotify_device *dev;
911 + char __user *start;
912 + int ret;
913 + DEFINE_WAIT(wait);
914 +
915 + start = buf;
916 + dev = file->private_data;
917 +
918 + while (1) {
919 + int events;
920 +
921 + prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE);
922 +
923 + down(&dev->sem);
924 + events = !list_empty(&dev->events);
925 + up(&dev->sem);
926 + if (events) {
927 + ret = 0;
928 + break;
929 + }
930 +
931 + if (file->f_flags & O_NONBLOCK) {
932 + ret = -EAGAIN;
933 + break;
934 + }
935 +
936 + if (signal_pending(current)) {
937 + ret = -EINTR;
938 + break;
939 + }
940 +
941 + schedule();
942 + }
943 +
944 + finish_wait(&dev->wq, &wait);
945 + if (ret)
946 + return ret;
947 +
948 + down(&dev->sem);
949 + while (1) {
950 + struct inotify_kernel_event *kevent;
951 +
952 + ret = buf - start;
953 + if (list_empty(&dev->events))
954 + break;
955 +
956 + kevent = inotify_dev_get_event(dev);
957 + if (event_size + kevent->event.len > count)
958 + break;
959 +
960 + if (copy_to_user(buf, &kevent->event, event_size)) {
961 + ret = -EFAULT;
962 + break;
963 + }
964 + buf += event_size;
965 + count -= event_size;
966 +
967 + if (kevent->name) {
968 + if (copy_to_user(buf, kevent->name, kevent->event.len)){
969 + ret = -EFAULT;
970 + break;
971 + }
972 + buf += kevent->event.len;
973 + count -= kevent->event.len;
974 + }
975 +
976 + remove_kevent(dev, kevent);
977 + }
978 + up(&dev->sem);
979 +
980 + return ret;
981 +}
982 +
983 +static int inotify_open(struct inode *inode, struct file *file)
984 +{
985 + struct inotify_device *dev;
986 + struct user_struct *user;
987 + int ret;
988 +
989 + user = get_uid(current->user);
990 +
991 + if (unlikely(atomic_read(&user->inotify_devs) >= max_user_devices)) {
992 + ret = -EMFILE;
993 + goto out_err;
994 + }
995 +
996 + dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL);
997 + if (unlikely(!dev)) {
998 + ret = -ENOMEM;
999 + goto out_err;
1000 + }
1001 +
1002 + idr_init(&dev->idr);
1003 + INIT_LIST_HEAD(&dev->events);
1004 + INIT_LIST_HEAD(&dev->watches);
1005 + init_waitqueue_head(&dev->wq);
1006 + sema_init(&dev->sem, 1);
1007 +
1008 + dev->event_count = 0;
1009 + dev->queue_size = 0;
1010 + dev->max_events = max_queued_events;
1011 + dev->user = user;
1012 + atomic_set(&dev->count, 0);
1013 +
1014 + get_inotify_dev(dev);
1015 + atomic_inc(&current->user->inotify_devs);
1016 +
1017 + file->private_data = dev;
1018 +
1019 + return 0;
1020 +out_err:
1021 + free_uid(user);
1022 + return ret;
1023 +}
1024 +
1025 +static int inotify_release(struct inode *ignored, struct file *file)
1026 +{
1027 + struct inotify_device *dev = file->private_data;
1028 +
1029 + /*
1030 + * Destroy all of the watches on this device. Unfortunately, not very
1031 + * pretty. We cannot do a simple iteration over the list, because we
1032 + * do not know the inode until we iterate to the watch. But we need to
1033 + * hold inode->inotify_sem before dev->sem. The following works.
1034 + */
1035 + while (1) {
1036 + struct inotify_watch *watch;
1037 + struct list_head *watches;
1038 + struct inode *inode;
1039 +
1040 + down(&dev->sem);
1041 + watches = &dev->watches;
1042 + if (list_empty(watches)) {
1043 + up(&dev->sem);
1044 + break;
1045 + }
1046 + watch = list_entry(watches->next, struct inotify_watch, d_list);
1047 + get_inotify_watch(watch);
1048 + up(&dev->sem);
1049 +
1050 + inode = watch->inode;
1051 + down(&inode->inotify_sem);
1052 + down(&dev->sem);
1053 + remove_watch_no_event(watch, dev);
1054 + up(&dev->sem);
1055 + up(&inode->inotify_sem);
1056 + put_inotify_watch(watch);
1057 + }
1058 +
1059 + /* destroy all of the events on this device */
1060 + down(&dev->sem);
1061 + while (!list_empty(&dev->events))
1062 + inotify_dev_event_dequeue(dev);
1063 + up(&dev->sem);
1064 +
1065 + /* free this device: the put matching the get in inotify_open() */
1066 + put_inotify_dev(dev);
1067 +
1068 + return 0;
1069 +}
1070 +
1071 +static int inotify_add_watch(struct inotify_device *dev,
1072 + int fd, u32 mask)
1073 +{
1074 + struct inotify_watch *watch, *old;
1075 + struct inode *inode;
1076 + struct file *filp;
1077 + int ret;
1078 +
1079 + filp = fget(fd);
1080 + if (!filp)
1081 + return -EBADF;
1082 + inode = filp->f_dentry->d_inode;
1083 +
1084 + down(&inode->inotify_sem);
1085 + down(&dev->sem);
1086 +
1087 + /*
1088 + * Handle the case of re-adding a watch on an (inode,dev) pair that we
1089 + * are already watching. We just update the mask and return its wd.
1090 + */
1091 + old = inode_find_dev(inode, dev);
1092 + if (unlikely(old)) {
1093 + old->mask = mask;
1094 + ret = old->wd;
1095 + goto out;
1096 + }
1097 +
1098 + watch = create_watch(dev, mask, inode);
1099 + if (unlikely(IS_ERR(watch))) {
1100 + ret = PTR_ERR(watch);
1101 + goto out;
1102 + }
1103 +
1104 + /* Add the watch to the device's and the inode's list */
1105 + list_add(&watch->d_list, &dev->watches);
1106 + list_add(&watch->i_list, &inode->inotify_watches);
1107 + ret = watch->wd;
1108 +
1109 +out:
1110 + up(&dev->sem);
1111 + up(&inode->inotify_sem);
1112 + fput(filp);
1113 +
1114 + return ret;
1115 +}
1116 +
1117 +/*
1118 + * inotify_ignore - handle the INOTIFY_IGNORE ioctl, asking that a given wd be
1119 + * removed from the device.
1120 + *
1121 + * Can sleep.
1122 + */
1123 +static int inotify_ignore(struct inotify_device *dev, s32 wd)
1124 +{
1125 + struct inotify_watch *watch;
1126 + struct inode *inode;
1127 +
1128 + down(&dev->sem);
1129 + watch = idr_find(&dev->idr, wd);
1130 + if (unlikely(!watch)) {
1131 + up(&dev->sem);
1132 + return -EINVAL;
1133 + }
1134 + get_inotify_watch(watch);
1135 + up(&dev->sem);
1136 +
1137 + inode = watch->inode;
1138 + down(&inode->inotify_sem);
1139 + down(&dev->sem);
1140 + remove_watch(watch, dev);
1141 + up(&dev->sem);
1142 + up(&inode->inotify_sem);
1143 + put_inotify_watch(watch);
1144 +
1145 + return 0;
1146 +}
1147 +
1148 +static long inotify_ioctl(struct file *file, unsigned int cmd,
1149 + unsigned long arg)
1150 +{
1151 + struct inotify_device *dev;
1152 + struct inotify_watch_request request;
1153 + void __user *p;
1154 + int ret = -ENOTTY;
1155 + s32 wd;
1156 +
1157 + dev = file->private_data;
1158 + p = (void __user *) arg;
1159 +
1160 + switch (cmd) {
1161 + case INOTIFY_WATCH:
1162 + if (unlikely(copy_from_user(&request, p, sizeof (request)))) {
1163 + ret = -EFAULT;
1164 + break;
1165 + }
1166 + ret = inotify_add_watch(dev, request.fd, request.mask);
1167 + break;
1168 + case INOTIFY_IGNORE:
1169 + if (unlikely(get_user(wd, (int __user *) p))) {
1170 + ret = -EFAULT;
1171 + break;
1172 + }
1173 + ret = inotify_ignore(dev, wd);
1174 + break;
1175 + case FIONREAD:
1176 + ret = put_user(dev->queue_size, (int __user *) p);
1177 + break;
1178 + }
1179 +
1180 + return ret;
1181 +}
1182 +
1183 +static struct file_operations inotify_fops = {
1184 + .owner = THIS_MODULE,
1185 + .poll = inotify_poll,
1186 + .read = inotify_read,
1187 + .open = inotify_open,
1188 + .release = inotify_release,
1189 + .unlocked_ioctl = inotify_ioctl,
1190 + .compat_ioctl = inotify_ioctl,
1191 +};
1192 +
1193 +static struct miscdevice inotify_device = {
1194 + .minor = MISC_DYNAMIC_MINOR,
1195 + .name = "inotify",
1196 + .fops = &inotify_fops,
1197 +};
1198 +
1199 +/*
1200 + * inotify_init - Our initialization function. Note that we cannnot return
1201 + * error because we have compiled-in VFS hooks. So an (unlikely) failure here
1202 + * must result in panic().
1203 + */
1204 +static int __init inotify_init(void)
1205 +{
1206 + struct class_device *class;
1207 + int ret;
1208 +
1209 + ret = misc_register(&inotify_device);
1210 + if (unlikely(ret))
1211 + panic("inotify: misc_register returned %d\n", ret);
1212 +
1213 + max_queued_events = 512;
1214 + max_user_devices = 64;
1215 + max_user_watches = 16384;
1216 +
1217 + class = inotify_device.class;
1218 + class_device_create_file(class, &class_device_attr_max_queued_events);
1219 + class_device_create_file(class, &class_device_attr_max_user_devices);
1220 + class_device_create_file(class, &class_device_attr_max_user_watches);
1221 +
1222 + atomic_set(&inotify_cookie, 0);
1223 +
1224 + watch_cachep = kmem_cache_create("inotify_watch_cache",
1225 + sizeof(struct inotify_watch),
1226 + 0, SLAB_PANIC, NULL, NULL);
1227 + event_cachep = kmem_cache_create("inotify_event_cache",
1228 + sizeof(struct inotify_kernel_event),
1229 + 0, SLAB_PANIC, NULL, NULL);
1230 +
1231 + printk(KERN_INFO "inotify device minor=%d\n", inotify_device.minor);
1232 +
1233 + return 0;
1234 +}
1235 +
1236 +module_init(inotify_init);
1237 diff -urN linux-2.6.12-rc3/fs/Kconfig linux/fs/Kconfig
1238 --- linux-2.6.12-rc3/fs/Kconfig 2005-04-20 22:47:06.000000000 -0400
1239 +++ linux/fs/Kconfig 2005-04-21 00:56:28.000000000 -0400
1240 @@ -339,6 +339,19 @@
1241 If you don't know whether you need it, then you don't need it:
1242 answer N.
1243
1244 +config INOTIFY
1245 + bool "Inotify file change notification support"
1246 + default y
1247 + ---help---
1248 + Say Y here to enable inotify support and the /dev/inotify character
1249 + device. Inotify is a file change notification system and a
1250 + replacement for dnotify. Inotify fixes numerous shortcomings in
1251 + dnotify and introduces several new features. It allows monitoring
1252 + of both files and directories via a single open fd. Multiple file
1253 + events are supported.
1254 +
1255 + If unsure, say Y.
1256 +
1257 config QUOTA
1258 bool "Quota support"
1259 help
1260 diff -urN linux-2.6.12-rc3/fs/Makefile linux/fs/Makefile
1261 --- linux-2.6.12-rc3/fs/Makefile 2005-03-02 02:38:10.000000000 -0500
1262 +++ linux/fs/Makefile 2005-04-21 00:56:28.000000000 -0400
1263 @@ -11,6 +11,7 @@
1264 attr.o bad_inode.o file.o filesystems.o namespace.o aio.o \
1265 seq_file.o xattr.o libfs.o fs-writeback.o mpage.o direct-io.o \
1266
1267 +obj-$(CONFIG_INOTIFY) += inotify.o
1268 obj-$(CONFIG_EPOLL) += eventpoll.o
1269 obj-$(CONFIG_COMPAT) += compat.o
1270
1271 diff -urN linux-2.6.12-rc3/fs/namei.c linux/fs/namei.c
1272 --- linux-2.6.12-rc3/fs/namei.c 2005-04-20 22:47:06.000000000 -0400
1273 +++ linux/fs/namei.c 2005-04-21 00:56:28.000000000 -0400
1274 @@ -21,7 +21,7 @@
1275 #include <linux/namei.h>
1276 #include <linux/quotaops.h>
1277 #include <linux/pagemap.h>
1278 -#include <linux/dnotify.h>
1279 +#include <linux/fsnotify.h>
1280 #include <linux/smp_lock.h>
1281 #include <linux/personality.h>
1282 #include <linux/security.h>
1283 @@ -1292,7 +1292,7 @@
1284 DQUOT_INIT(dir);
1285 error = dir->i_op->create(dir, dentry, mode, nd);
1286 if (!error) {
1287 - inode_dir_notify(dir, DN_CREATE);
1288 + fsnotify_create(dir, dentry->d_name.name);
1289 security_inode_post_create(dir, dentry, mode);
1290 }
1291 return error;
1292 @@ -1597,7 +1597,7 @@
1293 DQUOT_INIT(dir);
1294 error = dir->i_op->mknod(dir, dentry, mode, dev);
1295 if (!error) {
1296 - inode_dir_notify(dir, DN_CREATE);
1297 + fsnotify_create(dir, dentry->d_name.name);
1298 security_inode_post_mknod(dir, dentry, mode, dev);
1299 }
1300 return error;
1301 @@ -1670,7 +1670,7 @@
1302 DQUOT_INIT(dir);
1303 error = dir->i_op->mkdir(dir, dentry, mode);
1304 if (!error) {
1305 - inode_dir_notify(dir, DN_CREATE);
1306 + fsnotify_mkdir(dir, dentry->d_name.name);
1307 security_inode_post_mkdir(dir,dentry, mode);
1308 }
1309 return error;
1310 @@ -1761,7 +1761,7 @@
1311 }
1312 up(&dentry->d_inode->i_sem);
1313 if (!error) {
1314 - inode_dir_notify(dir, DN_DELETE);
1315 + fsnotify_rmdir(dentry, dentry->d_inode, dir);
1316 d_delete(dentry);
1317 }
1318 dput(dentry);
1319 @@ -1834,9 +1834,10 @@
1320
1321 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1322 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1323 + fsnotify_unlink(dentry, dir);
1324 d_delete(dentry);
1325 - inode_dir_notify(dir, DN_DELETE);
1326 }
1327 +
1328 return error;
1329 }
1330
1331 @@ -1910,7 +1911,7 @@
1332 DQUOT_INIT(dir);
1333 error = dir->i_op->symlink(dir, dentry, oldname);
1334 if (!error) {
1335 - inode_dir_notify(dir, DN_CREATE);
1336 + fsnotify_create(dir, dentry->d_name.name);
1337 security_inode_post_symlink(dir, dentry, oldname);
1338 }
1339 return error;
1340 @@ -1983,7 +1984,7 @@
1341 error = dir->i_op->link(old_dentry, dir, new_dentry);
1342 up(&old_dentry->d_inode->i_sem);
1343 if (!error) {
1344 - inode_dir_notify(dir, DN_CREATE);
1345 + fsnotify_create(dir, new_dentry->d_name.name);
1346 security_inode_post_link(old_dentry, dir, new_dentry);
1347 }
1348 return error;
1349 @@ -2147,6 +2148,7 @@
1350 {
1351 int error;
1352 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1353 + char *old_name;
1354
1355 if (old_dentry->d_inode == new_dentry->d_inode)
1356 return 0;
1357 @@ -2168,18 +2170,18 @@
1358 DQUOT_INIT(old_dir);
1359 DQUOT_INIT(new_dir);
1360
1361 + old_name = fsnotify_oldname_init(old_dentry);
1362 +
1363 if (is_dir)
1364 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
1365 else
1366 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
1367 if (!error) {
1368 - if (old_dir == new_dir)
1369 - inode_dir_notify(old_dir, DN_RENAME);
1370 - else {
1371 - inode_dir_notify(old_dir, DN_DELETE);
1372 - inode_dir_notify(new_dir, DN_CREATE);
1373 - }
1374 + const char *new_name = old_dentry->d_name.name;
1375 + fsnotify_move(old_dir, new_dir, old_name, new_name);
1376 }
1377 + fsnotify_oldname_free(old_name);
1378 +
1379 return error;
1380 }
1381
1382 diff -urN linux-2.6.12-rc3/fs/open.c linux/fs/open.c
1383 --- linux-2.6.12-rc3/fs/open.c 2005-03-02 02:37:47.000000000 -0500
1384 +++ linux/fs/open.c 2005-04-21 01:17:27.000000000 -0400
1385 @@ -10,7 +10,7 @@
1386 #include <linux/file.h>
1387 #include <linux/smp_lock.h>
1388 #include <linux/quotaops.h>
1389 -#include <linux/dnotify.h>
1390 +#include <linux/fsnotify.h>
1391 #include <linux/module.h>
1392 #include <linux/slab.h>
1393 #include <linux/tty.h>
1394 @@ -944,9 +944,11 @@
1395 fd = get_unused_fd();
1396 if (fd >= 0) {
1397 struct file *f = filp_open(tmp, flags, mode);
1398 +
1399 error = PTR_ERR(f);
1400 if (IS_ERR(f))
1401 goto out_error;
1402 + fsnotify_open(f->f_dentry);
1403 fd_install(fd, f);
1404 }
1405 out:
1406 diff -urN linux-2.6.12-rc3/fs/read_write.c linux/fs/read_write.c
1407 --- linux-2.6.12-rc3/fs/read_write.c 2005-04-20 22:47:06.000000000 -0400
1408 +++ linux/fs/read_write.c 2005-04-21 00:56:28.000000000 -0400
1409 @@ -10,7 +10,7 @@
1410 #include <linux/file.h>
1411 #include <linux/uio.h>
1412 #include <linux/smp_lock.h>
1413 -#include <linux/dnotify.h>
1414 +#include <linux/fsnotify.h>
1415 #include <linux/security.h>
1416 #include <linux/module.h>
1417 #include <linux/syscalls.h>
1418 @@ -239,7 +239,7 @@
1419 else
1420 ret = do_sync_read(file, buf, count, pos);
1421 if (ret > 0) {
1422 - dnotify_parent(file->f_dentry, DN_ACCESS);
1423 + fsnotify_access(file->f_dentry);
1424 current->rchar += ret;
1425 }
1426 current->syscr++;
1427 @@ -287,7 +287,7 @@
1428 else
1429 ret = do_sync_write(file, buf, count, pos);
1430 if (ret > 0) {
1431 - dnotify_parent(file->f_dentry, DN_MODIFY);
1432 + fsnotify_modify(file->f_dentry);
1433 current->wchar += ret;
1434 }
1435 current->syscw++;
1436 @@ -523,9 +523,12 @@
1437 out:
1438 if (iov != iovstack)
1439 kfree(iov);
1440 - if ((ret + (type == READ)) > 0)
1441 - dnotify_parent(file->f_dentry,
1442 - (type == READ) ? DN_ACCESS : DN_MODIFY);
1443 + if ((ret + (type == READ)) > 0) {
1444 + if (type == READ)
1445 + fsnotify_access(file->f_dentry);
1446 + else
1447 + fsnotify_modify(file->f_dentry);
1448 + }
1449 return ret;
1450 Efault:
1451 ret = -EFAULT;
1452 diff -urN linux-2.6.12-rc3/fs/xattr.c linux/fs/xattr.c
1453 --- linux-2.6.12-rc3/fs/xattr.c 2005-03-02 02:38:07.000000000 -0500
1454 +++ linux/fs/xattr.c 2005-04-21 01:24:24.000000000 -0400
1455 @@ -16,6 +16,7 @@
1456 #include <linux/security.h>
1457 #include <linux/syscalls.h>
1458 #include <linux/module.h>
1459 +#include <linux/fsnotify.h>
1460 #include <asm/uaccess.h>
1461
1462 /*
1463 @@ -57,8 +58,10 @@
1464 if (error)
1465 goto out;
1466 error = d->d_inode->i_op->setxattr(d, kname, kvalue, size, flags);
1467 - if (!error)
1468 + if (!error) {
1469 + fsnotify_xattr(d);
1470 security_inode_post_setxattr(d, kname, kvalue, size, flags);
1471 + }
1472 out:
1473 up(&d->d_inode->i_sem);
1474 }
1475 diff -urN linux-2.6.12-rc3/include/linux/fs.h linux/include/linux/fs.h
1476 --- linux-2.6.12-rc3/include/linux/fs.h 2005-04-20 22:47:07.000000000 -0400
1477 +++ linux/include/linux/fs.h 2005-04-21 00:56:28.000000000 -0400
1478 @@ -471,6 +471,11 @@
1479 struct dnotify_struct *i_dnotify; /* for directory notifications */
1480 #endif
1481
1482 +#ifdef CONFIG_INOTIFY
1483 + struct list_head inotify_watches; /* watches on this inode */
1484 + struct semaphore inotify_sem; /* protects the watches list */
1485 +#endif
1486 +
1487 unsigned long i_state;
1488 unsigned long dirtied_when; /* jiffies of first dirtying */
1489
1490 @@ -1365,7 +1370,6 @@
1491 extern int do_remount_sb(struct super_block *sb, int flags,
1492 void *data, int force);
1493 extern sector_t bmap(struct inode *, sector_t);
1494 -extern int setattr_mask(unsigned int);
1495 extern int notify_change(struct dentry *, struct iattr *);
1496 extern int permission(struct inode *, int, struct nameidata *);
1497 extern int generic_permission(struct inode *, int,
1498 diff -urN linux-2.6.12-rc3/include/linux/fsnotify.h linux/include/linux/fsnotify.h
1499 --- linux-2.6.12-rc3/include/linux/fsnotify.h 1969-12-31 19:00:00.000000000 -0500
1500 +++ linux/include/linux/fsnotify.h 2005-04-21 01:23:06.000000000 -0400
1501 @@ -0,0 +1,230 @@
1502 +#ifndef _LINUX_FS_NOTIFY_H
1503 +#define _LINUX_FS_NOTIFY_H
1504 +
1505 +/*
1506 + * include/linux/fs_notify.h - generic hooks for filesystem notification, to
1507 + * reduce in-source duplication from both dnotify and inotify.
1508 + *
1509 + * We don't compile any of this away in some complicated menagerie of ifdefs.
1510 + * Instead, we rely on the code inside to optimize away as needed.
1511 + *
1512 + * (C) Copyright 2005 Robert Love
1513 + */
1514 +
1515 +#ifdef __KERNEL__
1516 +
1517 +#include <linux/dnotify.h>
1518 +#include <linux/inotify.h>
1519 +
1520 +/*
1521 + * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
1522 + */
1523 +static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
1524 + const char *old_name, const char *new_name)
1525 +{
1526 + u32 cookie;
1527 +
1528 + if (old_dir == new_dir)
1529 + inode_dir_notify(old_dir, DN_RENAME);
1530 + else {
1531 + inode_dir_notify(old_dir, DN_DELETE);
1532 + inode_dir_notify(new_dir, DN_CREATE);
1533 + }
1534 +
1535 + cookie = inotify_get_cookie();
1536 +
1537 + inotify_inode_queue_event(old_dir, IN_MOVED_FROM, cookie, old_name);
1538 + inotify_inode_queue_event(new_dir, IN_MOVED_TO, cookie, new_name);
1539 +}
1540 +
1541 +/*
1542 + * fsnotify_unlink - file was unlinked
1543 + */
1544 +static inline void fsnotify_unlink(struct dentry *dentry, struct inode *dir)
1545 +{
1546 + struct inode *inode = dentry->d_inode;
1547 +
1548 + inode_dir_notify(dir, DN_DELETE);
1549 + inotify_inode_queue_event(dir, IN_DELETE_FILE, 0, dentry->d_name.name);
1550 + inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL);
1551 +
1552 + inotify_inode_is_dead(inode);
1553 +}
1554 +
1555 +/*
1556 + * fsnotify_rmdir - directory was removed
1557 + */
1558 +static inline void fsnotify_rmdir(struct dentry *dentry, struct inode *inode,
1559 + struct inode *dir)
1560 +{
1561 + inode_dir_notify(dir, DN_DELETE);
1562 + inotify_inode_queue_event(dir, IN_DELETE_SUBDIR,0,dentry->d_name.name);
1563 + inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL);
1564 +
1565 + inotify_inode_is_dead(inode);
1566 +}
1567 +
1568 +/*
1569 + * fsnotify_create - filename was linked in
1570 + */
1571 +static inline void fsnotify_create(struct inode *inode, const char *filename)
1572 +{
1573 + inode_dir_notify(inode, DN_CREATE);
1574 + inotify_inode_queue_event(inode, IN_CREATE_FILE, 0, filename);
1575 +}
1576 +
1577 +/*
1578 + * fsnotify_mkdir - directory 'name' was created
1579 + */
1580 +static inline void fsnotify_mkdir(struct inode *inode, const char *name)
1581 +{
1582 + inode_dir_notify(inode, DN_CREATE);
1583 + inotify_inode_queue_event(inode, IN_CREATE_SUBDIR, 0, name);
1584 +}
1585 +
1586 +/*
1587 + * fsnotify_access - file was read
1588 + */
1589 +static inline void fsnotify_access(struct dentry *dentry)
1590 +{
1591 + dnotify_parent(dentry, DN_ACCESS);
1592 + inotify_dentry_parent_queue_event(dentry, IN_ACCESS, 0,
1593 + dentry->d_name.name);
1594 + inotify_inode_queue_event(dentry->d_inode, IN_ACCESS, 0, NULL);
1595 +}
1596 +
1597 +/*
1598 + * fsnotify_modify - file was modified
1599 + */
1600 +static inline void fsnotify_modify(struct dentry *dentry)
1601 +{
1602 + dnotify_parent(dentry, DN_MODIFY);
1603 + inotify_dentry_parent_queue_event(dentry, IN_MODIFY, 0,
1604 + dentry->d_name.name);
1605 + inotify_inode_queue_event(dentry->d_inode, IN_MODIFY, 0, NULL);
1606 +}
1607 +
1608 +/*
1609 + * fsnotify_open - file was opened
1610 + */
1611 +static inline void fsnotify_open(struct dentry *dentry)
1612 +{
1613 + inotify_inode_queue_event(dentry->d_inode, IN_OPEN, 0, NULL);
1614 + inotify_dentry_parent_queue_event(dentry, IN_OPEN, 0,
1615 + dentry->d_name.name);
1616 +}
1617 +
1618 +/*
1619 + * fsnotify_close - file was closed
1620 + */
1621 +static inline void fsnotify_close(struct file *file)
1622 +{
1623 + struct dentry *dentry = file->f_dentry;
1624 + struct inode *inode = dentry->d_inode;
1625 + const char *filename = dentry->d_name.name;
1626 + mode_t mode = file->f_mode;
1627 + u32 mask;
1628 +
1629 + mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
1630 + inotify_dentry_parent_queue_event(dentry, mask, 0, filename);
1631 + inotify_inode_queue_event(inode, mask, 0, NULL);
1632 +}
1633 +
1634 +/*
1635 + * fsnotify_xattr - extended attributes were changed
1636 + */
1637 +static inline void fsnotify_xattr(struct dentry *dentry)
1638 +{
1639 + inotify_dentry_parent_queue_event(dentry, IN_ATTRIB, 0,
1640 + dentry->d_name.name);
1641 + inotify_inode_queue_event(dentry->d_inode, IN_ATTRIB, 0, NULL);
1642 +}
1643 +
1644 +/*
1645 + * fsnotify_change - notify_change event. file was modified and/or metadata
1646 + * was changed.
1647 + */
1648 +static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
1649 +{
1650 + int dn_mask = 0;
1651 + u32 in_mask = 0;
1652 +
1653 + if (ia_valid & ATTR_UID) {
1654 + in_mask |= IN_ATTRIB;
1655 + dn_mask |= DN_ATTRIB;
1656 + }
1657 + if (ia_valid & ATTR_GID) {
1658 + in_mask |= IN_ATTRIB;
1659 + dn_mask |= DN_ATTRIB;
1660 + }
1661 + if (ia_valid & ATTR_SIZE) {
1662 + in_mask |= IN_MODIFY;
1663 + dn_mask |= DN_MODIFY;
1664 + }
1665 + /* both times implies a utime(s) call */
1666 + if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
1667 + {
1668 + in_mask |= IN_ATTRIB;
1669 + dn_mask |= DN_ATTRIB;
1670 + } else if (ia_valid & ATTR_ATIME) {
1671 + in_mask |= IN_ACCESS;
1672 + dn_mask |= DN_ACCESS;
1673 + } else if (ia_valid & ATTR_MTIME) {
1674 + in_mask |= IN_MODIFY;
1675 + dn_mask |= DN_MODIFY;
1676 + }
1677 + if (ia_valid & ATTR_MODE) {
1678 + in_mask |= IN_ATTRIB;
1679 + dn_mask |= DN_ATTRIB;
1680 + }
1681 +
1682 + if (dn_mask)
1683 + dnotify_parent(dentry, dn_mask);
1684 + if (in_mask) {
1685 + inotify_inode_queue_event(dentry->d_inode, in_mask, 0, NULL);
1686 + inotify_dentry_parent_queue_event(dentry, in_mask, 0,
1687 + dentry->d_name.name);
1688 + }
1689 +}
1690 +
1691 +#ifdef CONFIG_INOTIFY /* inotify helpers */
1692 +
1693 +/*
1694 + * fsnotify_oldname_init - save off the old filename before we change it
1695 + *
1696 + * this could be kstrdup if only we could add that to lib/string.c
1697 + */
1698 +static inline char *fsnotify_oldname_init(struct dentry *old_dentry)
1699 +{
1700 + char *old_name;
1701 +
1702 + old_name = kmalloc(strlen(old_dentry->d_name.name) + 1, GFP_KERNEL);
1703 + if (old_name)
1704 + strcpy(old_name, old_dentry->d_name.name);
1705 + return old_name;
1706 +}
1707 +
1708 +/*
1709 + * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
1710 + */
1711 +static inline void fsnotify_oldname_free(const char *old_name)
1712 +{
1713 + kfree(old_name);
1714 +}
1715 +
1716 +#else /* CONFIG_INOTIFY */
1717 +
1718 +static inline char *fsnotify_oldname_init(struct dentry *old_dentry)
1719 +{
1720 + return NULL;
1721 +}
1722 +
1723 +static inline void fsnotify_oldname_free(const char *old_name)
1724 +{
1725 +}
1726 +
1727 +#endif /* ! CONFIG_INOTIFY */
1728 +
1729 +#endif /* __KERNEL__ */
1730 +
1731 +#endif /* _LINUX_FS_NOTIFY_H */
1732 diff -urN linux-2.6.12-rc3/include/linux/inotify.h linux/include/linux/inotify.h
1733 --- linux-2.6.12-rc3/include/linux/inotify.h 1969-12-31 19:00:00.000000000 -0500
1734 +++ linux/include/linux/inotify.h 2005-04-21 00:56:28.000000000 -0400
1735 @@ -0,0 +1,111 @@
1736 +/*
1737 + * Inode based directory notification for Linux
1738 + *
1739 + * Copyright (C) 2005 John McCutchan
1740 + */
1741 +
1742 +#ifndef _LINUX_INOTIFY_H
1743 +#define _LINUX_INOTIFY_H
1744 +
1745 +#include <linux/types.h>
1746 +
1747 +/*
1748 + * struct inotify_event - structure read from the inotify device for each event
1749 + *
1750 + * When you are watching a directory, you will receive the filename for events
1751 + * such as IN_CREATE, IN_DELETE, IN_OPEN, IN_CLOSE, ..., relative to the wd.
1752 + */
1753 +struct inotify_event {
1754 + __s32 wd; /* watch descriptor */
1755 + __u32 mask; /* watch mask */
1756 + __u32 cookie; /* cookie to synchronize two events */
1757 + __u32 len; /* length (including nulls) of name */
1758 + char name[0]; /* stub for possible name */
1759 +};
1760 +
1761 +/*
1762 + * struct inotify_watch_request - represents a watch request
1763 + *
1764 + * Pass to the inotify device via the INOTIFY_WATCH ioctl
1765 + */
1766 +struct inotify_watch_request {
1767 + int fd; /* fd of filename to watch */
1768 + __u32 mask; /* event mask */
1769 +};
1770 +
1771 +/* the following are legal, implemented events */
1772 +#define IN_ACCESS 0x00000001 /* File was accessed */
1773 +#define IN_MODIFY 0x00000002 /* File was modified */
1774 +#define IN_ATTRIB 0x00000004 /* File changed attributes */
1775 +#define IN_CLOSE_WRITE 0x00000008 /* Writtable file was closed */
1776 +#define IN_CLOSE_NOWRITE 0x00000010 /* Unwrittable file closed */
1777 +#define IN_OPEN 0x00000020 /* File was opened */
1778 +#define IN_MOVED_FROM 0x00000040 /* File was moved from X */
1779 +#define IN_MOVED_TO 0x00000080 /* File was moved to Y */
1780 +#define IN_DELETE_SUBDIR 0x00000100 /* Subdir was deleted */
1781 +#define IN_DELETE_FILE 0x00000200 /* Subfile was deleted */
1782 +#define IN_CREATE_SUBDIR 0x00000400 /* Subdir was created */
1783 +#define IN_CREATE_FILE 0x00000800 /* Subfile was created */
1784 +#define IN_DELETE_SELF 0x00001000 /* Self was deleted */
1785 +#define IN_UNMOUNT 0x00002000 /* Backing fs was unmounted */
1786 +#define IN_Q_OVERFLOW 0x00004000 /* Event queued overflowed */
1787 +#define IN_IGNORED 0x00008000 /* File was ignored */
1788 +
1789 +/* special flags */
1790 +#define IN_ALL_EVENTS 0xffffffff /* All the events */
1791 +#define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
1792 +
1793 +#define INOTIFY_IOCTL_MAGIC 'Q'
1794 +#define INOTIFY_IOCTL_MAXNR 2
1795 +
1796 +#define INOTIFY_WATCH _IOR(INOTIFY_IOCTL_MAGIC, 1, struct inotify_watch_request)
1797 +#define INOTIFY_IGNORE _IOR(INOTIFY_IOCTL_MAGIC, 2, int)
1798 +
1799 +#ifdef __KERNEL__
1800 +
1801 +#include <linux/dcache.h>
1802 +#include <linux/fs.h>
1803 +#include <linux/config.h>
1804 +
1805 +#ifdef CONFIG_INOTIFY
1806 +
1807 +extern void inotify_inode_queue_event(struct inode *, __u32, __u32,
1808 + const char *);
1809 +extern void inotify_dentry_parent_queue_event(struct dentry *, __u32, __u32,
1810 + const char *);
1811 +extern void inotify_unmount_inodes(struct list_head *);
1812 +extern void inotify_inode_is_dead(struct inode *);
1813 +extern u32 inotify_get_cookie(void);
1814 +
1815 +#else
1816 +
1817 +static inline void inotify_inode_queue_event(struct inode *inode,
1818 + __u32 mask, __u32 cookie,
1819 + const char *filename)
1820 +{
1821 +}
1822 +
1823 +static inline void inotify_dentry_parent_queue_event(struct dentry *dentry,
1824 + __u32 mask, __u32 cookie,
1825 + const char *filename)
1826 +{
1827 +}
1828 +
1829 +static inline void inotify_unmount_inodes(struct list_head *list)
1830 +{
1831 +}
1832 +
1833 +static inline void inotify_inode_is_dead(struct inode *inode)
1834 +{
1835 +}
1836 +
1837 +static inline u32 inotify_get_cookie(void)
1838 +{
1839 + return 0;
1840 +}
1841 +
1842 +#endif /* CONFIG_INOTIFY */
1843 +
1844 +#endif /* __KERNEL __ */
1845 +
1846 +#endif /* _LINUX_INOTIFY_H */
1847 diff -urN linux-2.6.12-rc3/include/linux/sched.h linux/include/linux/sched.h
1848 --- linux-2.6.12-rc3/include/linux/sched.h 2005-04-20 22:47:07.000000000 -0400
1849 +++ linux/include/linux/sched.h 2005-04-21 00:56:28.000000000 -0400
1850 @@ -404,6 +404,10 @@
1851 atomic_t processes; /* How many processes does this user have? */
1852 atomic_t files; /* How many open files does this user have? */
1853 atomic_t sigpending; /* How many pending signals does this user have? */
1854 +#ifdef CONFIG_INOTIFY
1855 + atomic_t inotify_watches; /* How many inotify watches does this user have? */
1856 + atomic_t inotify_devs; /* How many inotify devs does this user have opened? */
1857 +#endif
1858 /* protected by mq_lock */
1859 unsigned long mq_bytes; /* How many bytes can be allocated to mqueue? */
1860 unsigned long locked_shm; /* How many pages of mlocked shm ? */
1861 diff -urN linux-2.6.12-rc3/kernel/user.c linux/kernel/user.c
1862 --- linux-2.6.12-rc3/kernel/user.c 2005-04-20 22:47:08.000000000 -0400
1863 +++ linux/kernel/user.c 2005-04-21 00:56:28.000000000 -0400
1864 @@ -120,6 +120,10 @@
1865 atomic_set(&new->processes, 0);
1866 atomic_set(&new->files, 0);
1867 atomic_set(&new->sigpending, 0);
1868 +#ifdef CONFIG_INOTIFY
1869 + atomic_set(&new->inotify_watches, 0);
1870 + atomic_set(&new->inotify_devs, 0);
1871 +#endif
1872
1873 new->mq_bytes = 0;
1874 new->locked_shm = 0;

  ViewVC Help
Powered by ViewVC 1.1.20