2 * Directory notifications for Linux.
4 * Copyright (C) 2000,2001,2002 Stephen Rothwell
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
17 #include <linux/sched.h>
18 #include <linux/dnotify.h>
19 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/slab.h>
22 #include <linux/file.h>
24 extern void send_sigio(struct fown_struct *fown, int fd, int band);
26 int dir_notify_enable = 1;
28 static rwlock_t dn_lock = RW_LOCK_UNLOCKED;
29 static kmem_cache_t *dn_cache;
31 static void redo_inode_mask(struct inode *inode)
33 unsigned long new_mask;
34 struct dnotify_struct *dn;
37 for (dn = inode->i_dnotify; dn != NULL; dn = dn->dn_next)
38 new_mask |= dn->dn_mask & ~DN_MULTISHOT;
39 inode->i_dnotify_mask = new_mask;
42 void dnotify_flush(struct file *filp, fl_owner_t id)
44 struct dnotify_struct *dn;
45 struct dnotify_struct **prev;
48 inode = filp->f_dentry->d_inode;
49 if (!S_ISDIR(inode->i_mode))
52 prev = &inode->i_dnotify;
53 while ((dn = *prev) != NULL) {
54 if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
56 redo_inode_mask(inode);
57 kmem_cache_free(dn_cache, dn);
62 write_unlock(&dn_lock);
65 int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
67 struct dnotify_struct *dn;
68 struct dnotify_struct *odn;
69 struct dnotify_struct **prev;
71 fl_owner_t id = current->files;
74 if ((arg & ~DN_MULTISHOT) == 0) {
75 dnotify_flush(filp, id);
78 if (!dir_notify_enable)
80 inode = filp->f_dentry->d_inode;
81 if (!S_ISDIR(inode->i_mode))
83 dn = kmem_cache_alloc(dn_cache, SLAB_KERNEL);
87 prev = &inode->i_dnotify;
88 while ((odn = *prev) != NULL) {
89 if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
92 inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
93 kmem_cache_free(dn_cache, dn);
99 /* we'd lost the race with close(), sod off silently */
100 read_lock(¤t->files->file_lock);
102 read_unlock(¤t->files->file_lock);
104 kmem_cache_free(dn_cache, dn);
108 filp->f_owner.pid = current->pid;
109 filp->f_owner.uid = current->uid;
110 filp->f_owner.euid = current->euid;
115 inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
116 dn->dn_next = inode->i_dnotify;
117 inode->i_dnotify = dn;
119 write_unlock(&dn_lock);
123 void __inode_dir_notify(struct inode *inode, unsigned long event)
125 struct dnotify_struct * dn;
126 struct dnotify_struct **prev;
127 struct fown_struct * fown;
130 write_lock(&dn_lock);
131 prev = &inode->i_dnotify;
132 while ((dn = *prev) != NULL) {
133 if ((dn->dn_mask & event) == 0) {
137 fown = &dn->dn_filp->f_owner;
139 send_sigio(fown, dn->dn_fd, POLL_MSG);
140 if (dn->dn_mask & DN_MULTISHOT)
145 kmem_cache_free(dn_cache, dn);
149 redo_inode_mask(inode);
150 write_unlock(&dn_lock);
153 static int __init dnotify_init(void)
155 dn_cache = kmem_cache_create("dnotify_cache",
156 sizeof(struct dnotify_struct), 0, 0, NULL, NULL);
158 panic("cannot create dnotify slab cache");
162 module_init(dnotify_init)