inotify 0.19
Subsystems (5)
Announcing release 0.19.0 of inotify.
Changes in this release include:
- Rename event.filename to event.name (breaks api)
- Rename request.dirname to request.name (breaks api)
- Add Mike's patch to fix unmount race
- Remove watch_mask and all the associated code.
- Don't check the mask in inotify_dev_queue_event()
- Adding Robert Love to Authors list
- Update Copyright to 2005
- Check the mask in the one place we actually need to.
- Break ignore_helper up, adding a lockless version
- Remove the event parameter from ignore_helper, handle that one case as needed, separately
- Rename ignore_helper to remove_watch
- More comments!
Attached is a patch for 2.6.10
Attached is a patch for gamin CVS
Attached is an updated inotify-utils
Or,
http://tentacle.dhs.org/inotify/
And for even more patches, ftp://ftp.kernel.org/pub/linux/kernel/people/rml/inotify
John McCutchan
Inotify documentation:
--HOWTO USE--
Inotify is a character device that when opened offers 2 IOCTL's.
INOTIFY_WATCH:
Which takes a path and event mask and returns a unique (to the instance of the driver) integer (wd [watch descriptor] from here on) that is a 1:1 mapping to the path passed. What happens is inotify refs the inode for the path and
adds a inotify_watch structure to the inode. Each inotify device instance can only watch an inode once.
INOTIFY_IGNORE:
Which takes an integer (that you got from INOTIFY_WATCH) representing a wd that you are not interested in watching anymore. This will:
send an IGNORE event to the device
remove the inotify_watch structure from the device and from the inode and unref the inode.
After you are watching 1 or more paths, you can read from the fd and get events. The events are struct inotify_event. If you are watching a directory and something happens to a file in the directory the event will contain the filename (just the filename not the full path).
-- EVENTS --IN_ACCESS - Sent when file is accessed.IN_MODIFY - Sent when file is modified.IN_ATTRIB - Sent when file is chmod'ed.IN_CLOSE_WRITE - Sent when file is closed and was opened for writing. IN_CLOSE_NOWRITE - Sent when file is closed but was not opened for writing.IN_CLOSE - either of the two above events.IN_OPEN - Sent when file is opened.IN_MOVED_FROM - Sent to the source folder of a move. IN_MOVED_TO - Sent to the destination folder of a move. IN_DELETE_SUBDIR - Sent when a sub directory is deleted.(When watching parent)IN_DELETE_FILE - Sent when a file is deleted. (When watching parent) IN_CREATE_SUBDIR - Sent when a sub directory is created.(When watching parent)IN_CREATE_FILE - Sent when a file is created. (When watching parent) IN_DELETE_SELF - Sent when file is deleted.IN_UNMOUNT - Sent when the filesystem is being unmounted. IN_Q_OVERFLOW - Sent when your event queue has over flowed.
The MOVED_FROM/MOVED_TO events are always sent in pairs. MOVED_FROM/MOVED_TO
is also sent when a file is renamed. The cookie field in the event structure
allows you to pair MOVED_FROM/MOVED_TO events. These two events are not guaranteed to be successive in the event stream. You must rely on the cookie to pair them up.
If you aren't watching the source and destination folders of a move/rename
You will only get MOVED_TO or MOVED_FROM. In this case, MOVED_TO is equivelent to a CREATE and MOVED_FROM is equivelent to a DELETE.
--Why Not dnotify and Why inotify (By Robert Love)--
Everyone seems quick to deride the blunder known as "dnotify" and applaud a
replacement, any replacement, man anything but that current mess, but in the
name of fairness I present my treatise on why dnotify is what one might call
not good:
* dnotify requires the opening of one fd per each directory that you intend to
watch.
o The file descriptor pins the directory, disallowing the backing device to be unmounted, which absolutely wrecks havoc with removable media.
o Watching many directories results in many open file descriptors, possibly hitting a per-process fd limit.
* 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 around to compare things in order to find out which file.
* dnotify's interface to user-space is awful.
o dnotify uses signals to communicate with user-space.
o Specifically, dnotify uses SIGIO.
o But then you can pick a different signal! So by "signals," I really meant you need to use real-time signals if you want to queue the events.
* dnotify basically ignores any problems that would arise in the VFS from hard
links.
* Rumor is that the "d" in "dnotify" does not stand for "directory" but for "suck."
A suitable replacement is "inotify." And now, my tract on what inotify brings
to the table:
* inotify's interface is a device node, not SIGIO.
o You open only a single fd, to the device node. No more pinning directories or opening a million file descriptors.
o Usage is nice: open the device, issue simple commands via ioctl(), and then block on the device. It returns events when, well, there are events to be returned.
o You can select() on the device node and so it integrates with main loops like coffee mixed with vanilla milkshake.
* inotify has an event that says "the filesystem that the item you were watching is on was unmounted" (this is particularly cool).
* inotify can watch directories or files.
* The "i" in inotify does not stand for "suck" but for "inode" -- the logical
choice since inotify is inode-based.
--COMPLEXITY--
I have been asked what the complexity of inotify is. Inotify has 2 path codes where complexity could be an issue:
Adding a watch to a device
This code has to check if the inode is already being watched by the device, this is O(N) where N is the number of devices watching the inode
Removing a watch from a device
This code
This code has to do a search of all watches on the device to find the watch descriptor that is being asked to remove. This involves a linear search, but should not really be an issue because it is limited to 8192 entries. If this does turn in to a concern, I would replace the list of watches on the device with a sorted binary tree, so that the search could be done very quickly.
In the near future this will be based on idr, and the complexity will be O(logn)
The calls to inotify from the VFS code has a complexity of O(1).
--MEMORY USAGE--
The inotify data structures are light weight:
**NOTE** The memory analysis is out of date as of dynamic filename lengths
inotify watch is 40 bytes
inotify device is 68 bytes
inotify event is 272 bytes
So assuming a device has 8192 watches, the structures are only going to consume 320KB of memory. With a maximum number of 8 devices allowed to exist at a time, this is still only 2.5 MB
Each device can also have 256 events queued at a time, which sums to 68KB per device. And only .5 MB if all devices are opened and have a full event queue.
So approximately 3 MB of memory are used in the rare case of everything open and full.
Each inotify watch pins the inode of a directory/file in memory, the size of an inode is different per file system but lets assume that it is 512 byes.
So assuming the maximum number of global watches are active, this would pin down 32 MB of inodes in the inode cache. Again not a problem on a modern system.
On smaller systems, the maximum watches / events could be lowered to provide a smaller foot print.
Keep in mind that this is an absolute worst case memory analysis. In reality it will most likely cost approximately 5MB.
--KERNEL CHANGES--
inotify char device driver.
Adding calls to inotify_inode_queue_event and inotify_dentry_parent_queue_event from VFS operations. Dnotify has the same function calls.
Adding a call to inotify_super_block_umount fromgeneric_shutdown_superblock
inotify_super_block_umount:
find all of the inodes that are on the super block being shut down foreach of these inodes:
remove the watchers on them
send UNMOUNT and IGNORED events
unref the inode
inotify, bitches
arch/sparc64/Kconfig | 13 drivers/char/Kconfig | 13 drivers/char/Makefile | 2 drivers/char/inotify.c | 1053 +++++++++++++++++++++++++++++++++++++++++++++ drivers/char/misc.c | 14 fs/attr.c | 34 - fs/compat.c | 14 fs/file_table.c | 4 fs/inode.c | 3 fs/namei.c | 38 - fs/open.c | 9 fs/read_write.c | 28 - fs/super.c | 3 include/linux/fs.h | 7 include/linux/fsnotify.h | 235 ++++++++++ include/linux/inotify.h | 118 +++++ include/linux/miscdevice.h | 5 include/linux/sched.h | 2 kernel/user.c | 2 19 files changed, 1522 insertions(+), 75 deletions(-)
arch/sparc64/Kconfig +13 -0
@@ -88,6 +88,19 @@ bool default y +config INOTIFY + bool "Inotify file change notification support" + default y + ---help--- + Say Y here to enable inotify support and the /dev/inotify character + device. Inotify is a file change notification system and a + replacement for dnotify. Inotify fixes numerous shortcomings in + dnotify and introduces several new features. It allows monitoring + of both files and directories via a single open fd. Multiple file + events are supported. + + If unsure, say Y. + config SMP bool "Symmetric multi-processing support" ---help---
drivers/char/inotify.c +1053 -0
1,053 lines not on this page · raw message
drivers/char/Kconfig +13 -0
@@ -62,6 +62,19 @@ depends on VT && !S390 && !USERMODE default y +config INOTIFY + bool "Inotify file change notification support" + default y + ---help--- + Say Y here to enable inotify support and the /dev/inotify character + device. Inotify is a file change notification system and a + replacement for dnotify. Inotify fixes numerous shortcomings in + dnotify and introduces several new features. It allows monitoring + of both files and directories via a single open fd. Multiple file + events are supported. + + If unsure, say Y. + config SERIAL_NONSTANDARD bool "Non-standard serial port support" ---help---
drivers/char/Makefile +2 -0
@@ -9,6 +9,8 @@ obj-y += mem.o random.o tty_io.o n_tty.o tty_ioctl.o + +obj-$(CONFIG_INOTIFY) += inotify.o obj-$(CONFIG_LEGACY_PTYS) += pty.o obj-$(CONFIG_UNIX98_PTYS) += pty.o obj-y += misc.o
drivers/char/misc.c +6 -8
@@ -207,10 +207,9 @@int misc_register (struct miscdevice * misc) {struct miscdevice *c; -struct class_device *class;dev_t dev;int err; - +down (&misc_sem);list_for_each_entry (c, &misc_list, list) {if (c->minor == misc->minor) {
@@ -224,8 +223,7 @@while (--i >=0 )if ( (misc_minors[i>>3 ] & (1 << (i&7 ))) ==0 )break ; -if (i<0 ) - { +if (i<0 ) {up (&misc_sem);return -EBUSY ; }
@@ -240,10 +238,10 @@ } dev =MKDEV (MISC_MAJOR , misc->minor); - class =class_simple_device_add (misc_class, dev, - misc->dev, misc->name); -if (IS_ERR (class)) { - err =PTR_ERR (class); + misc->class =class_simple_device_add (misc_class, dev, + misc->dev, misc->name); +if (IS_ERR (misc->class)) { + err =PTR_ERR (misc->class); goto out; }
fs/attr.c +5 -29
@@ -10,7 +10,7 @@#include <linux/mm.h> #include <linux/string.h> #include <linux/smp_lock.h> -#include <linux/dnotify.h> +#include <linux/fsnotify.h> #include <linux/fcntl.h> #include <linux/quotaops.h> #include <linux/security.h>
@@ -103,31 +103,8 @@ out:return error; } -EXPORT_SYMBOL (inode_setattr); -int setattr_mask (unsigned int ia_valid) -{ -unsigned long dn_mask =0 ; - -if (ia_valid &ATTR_UID ) - dn_mask |=DN_ATTRIB ; -if (ia_valid &ATTR_GID ) - dn_mask |=DN_ATTRIB ; -if (ia_valid &ATTR_SIZE ) - dn_mask |=DN_MODIFY ; -/* both times implies a utime(s) call */ -if ((ia_valid & (ATTR_ATIME |ATTR_MTIME )) == (ATTR_ATIME |ATTR_MTIME )) - dn_mask |=DN_ATTRIB ; -else if (ia_valid &ATTR_ATIME ) - dn_mask |=DN_ACCESS ; -else if (ia_valid &ATTR_MTIME ) - dn_mask |=DN_MODIFY ; -if (ia_valid &ATTR_MODE ) - dn_mask |=DN_ATTRIB ; -return dn_mask; -} -int notify_change (struct dentry * dentry,struct iattr * attr) {struct inode *inode = dentry->d_inode;
@@ -183,11 +160,10 @@ error =inode_setattr (inode, attr); } } -if (!error) { -unsigned long dn_mask =setattr_mask (ia_valid); -if (dn_mask) -dnotify_parent (dentry, dn_mask); - } + +if (!error) +fsnotify_change (dentry, ia_valid); +return error; }
fs/compat.c +10 -4
@@ -35,7 +35,7 @@#include <linux/ctype.h> #include <linux/module.h> #include <linux/dirent.h> -#include <linux/dnotify.h> +#include <linux/fsnotify.h> #include <linux/highuid.h> #include <linux/sunrpc/svc.h> #include <linux/nfsd/nfsd.h>
@@ -1192,9 +1192,15 @@ out:if (iov != iovstack)kfree (iov); -if ((ret + (type ==READ )) >0 ) -dnotify_parent (file->f_dentry, - (type ==READ ) ?DN_ACCESS :DN_MODIFY ); +if ((ret + (type ==READ )) >0 ) { +struct dentry *dentry = file->f_dentry; +if (type ==READ ) +fsnotify_access (dentry, dentry->d_inode, + dentry->d_name.name); +else +fsnotify_modify (dentry, dentry->d_inode, + dentry->d_name.name); + }return ret; }
fs/file_table.c +4 -0
@@ -16,6 +16,7 @@#include <linux/eventpoll.h> #include <linux/mount.h> #include <linux/cdev.h> +#include <linux/fsnotify.h> /* sysctl tunables... */ struct files_stat_struct files_stat = {
@@ -121,6 +122,9 @@struct vfsmount *mnt = file->f_vfsmnt;struct inode *inode = dentry->d_inode; + +fsnotify_close (dentry, inode, file->f_mode, dentry->d_name.name); +might_sleep ();/* * The function eventpoll_release() should be the first called
fs/inode.c +3 -0
@@ -130,6 +130,9 @@#ifdef CONFIG_QUOTA memset (&inode->i_dquot,0 ,sizeof (inode->i_dquot));#endif +#ifdef CONFIG_INOTIFY + inode->inotify_data =NULL ; +#endif inode->i_pipe =NULL ; inode->i_bdev =NULL ; inode->i_cdev =NULL ;
fs/namei.c +18 -20
@@ -21,7 +21,7 @@#include <linux/namei.h> #include <linux/quotaops.h> #include <linux/pagemap.h> -#include <linux/dnotify.h> +#include <linux/fsnotify.h> #include <linux/smp_lock.h> #include <linux/personality.h> #include <linux/security.h>
@@ -1241,7 +1241,7 @@DQUOT_INIT (dir); error = dir->i_op->create (dir, dentry, mode, nd);if (!error) { -inode_dir_notify (dir,DN_CREATE ); +fsnotify_create (dir, dentry->d_name.name);security_inode_post_create (dir, dentry, mode); }return error;
@@ -1555,7 +1555,7 @@DQUOT_INIT (dir); error = dir->i_op->mknod (dir, dentry, mode, dev);if (!error) { -inode_dir_notify (dir,DN_CREATE ); +fsnotify_create (dir, dentry->d_name.name);security_inode_post_mknod (dir, dentry, mode, dev); }return error;
@@ -1628,7 +1628,7 @@DQUOT_INIT (dir); error = dir->i_op->mkdir (dir, dentry, mode);if (!error) { -inode_dir_notify (dir,DN_CREATE ); +fsnotify_mkdir (dir, dentry->d_name.name);security_inode_post_mkdir (dir,dentry, mode); }return error;
@@ -1722,10 +1722,8 @@ } }up (&dentry->d_inode->i_sem); -if (!error) { -inode_dir_notify (dir,DN_DELETE ); -d_delete (dentry); - } +if (!error) +fsnotify_rmdir (dentry, dentry->d_inode, dir);dput (dentry);return error;
@@ -1795,10 +1793,9 @@up (&dentry->d_inode->i_sem);/* We don't d_delete() NFS sillyrenamed files--they still exist. */ -if (!error && !(dentry->d_flags &DCACHE_NFSFS_RENAMED )) { -d_delete (dentry); -inode_dir_notify (dir,DN_DELETE ); - } +if (!error && !(dentry->d_flags &DCACHE_NFSFS_RENAMED )) +fsnotify_unlink (dentry->d_inode, dir, dentry); +return error; }
@@ -1872,7 +1869,7 @@DQUOT_INIT (dir); error = dir->i_op->symlink (dir, dentry, oldname);if (!error) { -inode_dir_notify (dir,DN_CREATE ); +fsnotify_create (dir, dentry->d_name.name);security_inode_post_symlink (dir, dentry, oldname); }return error;
@@ -1945,7 +1942,7 @@ error = dir->i_op->link (old_dentry, dir, new_dentry);up (&old_dentry->d_inode->i_sem);if (!error) { -inode_dir_notify (dir,DN_CREATE ); +fsnotify_create (dir, new_dentry->d_name.name);security_inode_post_link (old_dentry, dir, new_dentry); }return error;
@@ -2109,6 +2106,7 @@ {int error;int is_dir =S_ISDIR (old_dentry->d_inode->i_mode); +char *old_name;if (old_dentry->d_inode == new_dentry->d_inode)return 0 ;
@@ -2130,18 +2128,18 @@DQUOT_INIT (old_dir);DQUOT_INIT (new_dir); + old_name =fsnotify_oldname_init (old_dentry); +if (is_dir) error =vfs_rename_dir (old_dir,old_dentry,new_dir,new_dentry);else error =vfs_rename_other (old_dir,old_dentry,new_dir,new_dentry);if (!error) { -if (old_dir == new_dir) -inode_dir_notify (old_dir,DN_RENAME ); -else { -inode_dir_notify (old_dir,DN_DELETE ); -inode_dir_notify (new_dir,DN_CREATE ); - } +const char *new_name = old_dentry->d_name.name; +fsnotify_move (old_dir, new_dir, old_name, new_name); } +fsnotify_oldname_free (old_name); +return error; }
fs/open.c +7 -2
@@ -10,7 +10,7 @@#include <linux/file.h> #include <linux/smp_lock.h> #include <linux/quotaops.h> -#include <linux/dnotify.h> +#include <linux/fsnotify.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/tty.h>
@@ -953,9 +953,14 @@ fd =get_unused_fd ();if (fd >=0 ) {struct file *f =filp_open (tmp, flags, mode); +struct dentry *dentry; + error =PTR_ERR (f);if (IS_ERR (f)) goto out_error; + dentry = f->f_dentry; +fsnotify_open (dentry, dentry->d_inode, + dentry->d_name.name);fd_install (fd, f); } out:
@@ -1007,7 +1012,7 @@ retval = err; } -dnotify_flush (filp, id); +fsnotify_flush (filp, id);locks_remove_posix (filp, id);fput (filp);return retval;
fs/read_write.c +20 -8
@@ -10,7 +10,7 @@#include <linux/file.h> #include <linux/uio.h> #include <linux/smp_lock.h> -#include <linux/dnotify.h> +#include <linux/fsnotify.h> #include <linux/security.h> #include <linux/module.h> #include <linux/syscalls.h>
@@ -216,8 +216,11 @@ ret = file->f_op->read (file, buf, count, pos); else ret =do_sync_read (file, buf, count, pos); -if (ret >0 ) -dnotify_parent (file->f_dentry,DN_ACCESS ); +if (ret >0 ) { +struct dentry *dentry = file->f_dentry; +fsnotify_access (dentry, inode, + dentry->d_name.name); + } } }
@@ -260,8 +263,11 @@ ret = file->f_op->write (file, buf, count, pos); else ret =do_sync_write (file, buf, count, pos); -if (ret >0 ) -dnotify_parent (file->f_dentry,DN_MODIFY ); +if (ret >0 ) { +struct dentry *dentry = file->f_dentry; +fsnotify_modify (dentry, inode, + dentry->d_name.name); + } } }
@@ -493,9 +499,15 @@ out:if (iov != iovstack)kfree (iov); -if ((ret + (type ==READ )) >0 ) -dnotify_parent (file->f_dentry, - (type ==READ ) ?DN_ACCESS :DN_MODIFY ); +if ((ret + (type ==READ )) >0 ) { +struct dentry *dentry = file->f_dentry; +struct inode *inode = dentry->d_inode; + +if (type ==READ ) +fsnotify_access (dentry, inode, dentry->d_name.name); +else +fsnotify_modify (dentry, inode, dentry->d_name.name); + }return ret; }
fs/super.c +2 -1
@@ -37,9 +37,9 @@#include <linux/writeback.h> /* for the emergency remount stuff */ #include <linux/idr.h> #include <linux/kobject.h> +#include <linux/fsnotify.h> #include <asm/uaccess.h> -void get_filesystem (struct file_system_type *fs);void put_filesystem (struct file_system_type *fs);struct file_system_type *get_fs_type (const char *name);
@@ -227,6 +227,7 @@if (root) { sb->s_root =NULL ; +fsnotify_sb_umount (sb);shrink_dcache_parent (root);shrink_dcache_anon (&sb->s_anon);dput (root);
include/linux/fs.h +6 -1
@@ -27,6 +27,7 @@struct kstatfs ;struct vm_area_struct ;struct vfsmount ; +struct inotify_inode_data ;/* * It's silly to have NR_OPEN bigger than NR_FILE, but you can change
@@ -473,6 +474,10 @@struct dnotify_struct *i_dnotify;/* for directory notifications */ #endif +#ifdef CONFIG_INOTIFY +struct inotify_inode_data *inotify_data; +#endif +unsigned long i_state;unsigned long dirtied_when;/* jiffies of first dirtying */
@@ -1353,7 +1358,7 @@extern int do_remount_sb (struct super_block *sb,int flags,void *data,int force);extern sector_t bmap (struct inode *,sector_t ); -extern int setattr_mask (unsigned int ); +extern void setattr_mask (unsigned int ,int *,u32 *);extern int notify_change (struct dentry *,struct iattr *);extern int permission (struct inode *,int ,struct nameidata *); extern int generic_permission(struct inode *, int,
include/linux/fsnotify.h +235 -0
235 lines not on this page · raw message
include/linux/inotify.h +118 -0
118 lines not on this page · raw message
include/linux/miscdevice.h +3 -2
@@ -2,6 +2,7 @@#define _LINUX_MISCDEVICE_H#include <linux/module.h> #include <linux/major.h> +#include <linux/device.h> #define PSMOUSE_MINOR 1#define MS_BUSMOUSE_MINOR 2
@@ -32,13 +33,13 @@struct device ; -struct miscdevice -{ +struct miscdevice {int minor;const char *name;struct file_operations *fops;struct list_head list;struct device *dev; +struct class_device *class;char devfs_name[64 ]; };
include/linux/sched.h +2 -0
@@ -353,6 +353,8 @@atomic_t processes;/* How many processes does this user have? */ atomic_t files;/* How many open files does this user have? */ atomic_t sigpending;/* How many pending signals does this user have? */ +atomic_t inotify_watches;/* How many inotify watches does this user have? */ +atomic_t inotify_devs;/* How many inotify devs does this user have opened? */ /* protected by mq_lock */ unsigned long mq_bytes;/* How many bytes can be allocated to mqueue? */ unsigned long locked_shm;/* How many pages of mlocked shm ? */
kernel/user.c +2 -0
@@ -119,6 +119,8 @@atomic_set (&new->processes,0 );atomic_set (&new->files,0 );atomic_set (&new->sigpending,0 ); +atomic_set (&new->inotify_watches,0 ); +atomic_set (&new->inotify_devs,0 ); new->mq_bytes =0 ; new->locked_shm =0 ;
Index: server/gam_inotify.c
===================================================================
RCS file: /cvs/gnome/gamin/server/gam_inotify.c,v
retrieving revision 1.12
gam_inotify.c +5 -5
@@ -116,11 +116,11 @@return ; } - iwr.dirname =g_strdup (path); + iwr.name =g_strdup (path); iwr.mask =0xffffffff ;// all events wd =ioctl (fd,INOTIFY_WATCH , &iwr); -g_free (iwr.dirname); +g_free (iwr.name);if (wd <0 ) {G_UNLOCK (inotify);
@@ -204,12 +204,12 @@return ; } -if (event->filename[0 ] !='\0' ) { +if (event->name[0 ] !='\0' ) {int pathlen =strlen (data->path);if (data->path[pathlen-1 ] =='/' ) { - event_path =g_strconcat (data->path, event->filename,NULL ); + event_path =g_strconcat (data->path, event->name,NULL ); }else { - event_path =g_strconcat (data->path,"/" , event->filename,NULL ); + event_path =g_strconcat (data->path,"/" , event->name,NULL ); } }else { event_path =g_strdup (data->path);
Index: server/local_inotify.h
===================================================================
RCS file: /cvs/gnome/gamin/server/local_inotify.h,v retrieving revision 1.4
local_inotify.h +11 -53
107 lines not on this page · raw message