- pre4:
[davej-history.git] / fs / dnotify.c
blob5711e6b4175207ba547d7c03ac57ff2170766ca8
1 /*
2 * Directory notifications for Linux.
4 * Copyright (C) 2000 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
9 * later version.
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.
16 #include <linux/fs.h>
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>
23 extern void send_sigio(struct fown_struct *fown, int fd, int band);
25 int dir_notify_enable = 1;
27 static rwlock_t dn_lock = RW_LOCK_UNLOCKED;
28 static kmem_cache_t *dn_cache;
30 static void redo_inode_mask(struct inode *inode)
32 unsigned long new_mask;
33 struct dnotify_struct *dn;
35 new_mask = 0;
36 for (dn = inode->i_dnotify; dn != NULL; dn = dn->dn_next)
37 new_mask |= dn->dn_mask & ~DN_MULTISHOT;
38 inode->i_dnotify_mask = new_mask;
41 int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
43 struct dnotify_struct *dn = NULL;
44 struct dnotify_struct *odn;
45 struct dnotify_struct **prev;
46 struct inode *inode;
47 int turning_off = (arg & ~DN_MULTISHOT) == 0;
49 if (!turning_off && !dir_notify_enable)
50 return -EINVAL;
51 inode = filp->f_dentry->d_inode;
52 if (!S_ISDIR(inode->i_mode))
53 return -ENOTDIR;
54 if (!turning_off) {
55 dn = kmem_cache_alloc(dn_cache, SLAB_KERNEL);
56 if (dn == NULL)
57 return -ENOMEM;
59 write_lock(&dn_lock);
60 prev = &inode->i_dnotify;
61 for (odn = *prev; odn != NULL; prev = &odn->dn_next, odn = *prev)
62 if (odn->dn_filp == filp)
63 break;
64 if (odn != NULL) {
65 if (turning_off) {
66 *prev = odn->dn_next;
67 redo_inode_mask(inode);
68 dn = odn;
69 goto out_free;
71 odn->dn_fd = fd;
72 odn->dn_mask |= arg;
73 inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
74 goto out_free;
76 if (turning_off)
77 goto out;
78 filp->f_owner.pid = current->pid;
79 filp->f_owner.uid = current->uid;
80 filp->f_owner.euid = current->euid;
81 dn->dn_magic = DNOTIFY_MAGIC;
82 dn->dn_mask = arg;
83 dn->dn_fd = fd;
84 dn->dn_filp = filp;
85 inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
86 dn->dn_next = inode->i_dnotify;
87 inode->i_dnotify = dn;
88 out:
89 write_unlock(&dn_lock);
90 return 0;
91 out_free:
92 kmem_cache_free(dn_cache, dn);
93 goto out;
96 void __inode_dir_notify(struct inode *inode, unsigned long event)
98 struct dnotify_struct * dn;
99 struct dnotify_struct **prev;
100 struct fown_struct * fown;
101 int changed = 0;
103 write_lock(&dn_lock);
104 prev = &inode->i_dnotify;
105 while ((dn = *prev) != NULL) {
106 if (dn->dn_magic != DNOTIFY_MAGIC) {
107 printk(KERN_ERR "__inode_dir_notify: bad magic "
108 "number in dnotify_struct!\n");
109 goto out;
111 if ((dn->dn_mask & event) == 0) {
112 prev = &dn->dn_next;
113 continue;
115 fown = &dn->dn_filp->f_owner;
116 if (fown->pid)
117 send_sigio(fown, dn->dn_fd, POLL_MSG);
118 if (dn->dn_mask & DN_MULTISHOT)
119 prev = &dn->dn_next;
120 else {
121 *prev = dn->dn_next;
122 changed = 1;
123 kmem_cache_free(dn_cache, dn);
126 if (changed)
127 redo_inode_mask(inode);
128 out:
129 write_unlock(&dn_lock);
132 static int __init dnotify_init(void)
134 dn_cache = kmem_cache_create("dnotify cache",
135 sizeof(struct dnotify_struct), 0, 0, NULL, NULL);
136 if (!dn_cache)
137 panic("cannot create dnotify slab cache");
138 return 0;
141 module_init(dnotify_init)