File: /usr/src/linux/fs/dnotify.c

1     /*
2      * Directory notifications for Linux.
3      *
4      * Copyright (C) 2000 Stephen Rothwell
5      *
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.
10      *
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.
15      */
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>
22     
23     extern void send_sigio(struct fown_struct *fown, int fd, int band);
24     
25     int dir_notify_enable = 1;
26     
27     static rwlock_t dn_lock = RW_LOCK_UNLOCKED;
28     static kmem_cache_t *dn_cache;
29     
30     static void redo_inode_mask(struct inode *inode)
31     {
32     	unsigned long new_mask;
33     	struct dnotify_struct *dn;
34     
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;
39     }
40     
41     int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
42     {
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;
48     
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;
58     	}
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;
70     		}
71     		odn->dn_fd = fd;
72     		odn->dn_mask |= arg;
73     		inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
74     		goto out_free;
75     	}
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;
94     }
95     
96     void __inode_dir_notify(struct inode *inode, unsigned long event)
97     {
98     	struct dnotify_struct *	dn;
99     	struct dnotify_struct **prev;
100     	struct fown_struct *	fown;
101     	int			changed = 0;
102     
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;
110     		}
111     		if ((dn->dn_mask & event) == 0) {
112     			prev = &dn->dn_next;
113     			continue;
114     		}
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);
124     		}
125     	}
126     	if (changed)
127     		redo_inode_mask(inode);
128     out:
129     	write_unlock(&dn_lock);
130     }
131     
132     static int __init dnotify_init(void)
133     {
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;
139     }
140     
141     module_init(dnotify_init)
142