1 /*
2  * Directory notifications for Linux.
3  *
4  * Copyright (C) 2000,2001,2002 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 #include <linux/file.h>
23 
24 extern void send_sigio(struct fown_struct *fown, int fd, int band);
25 
26 int dir_notify_enable = 1;
27 
28 static rwlock_t dn_lock = RW_LOCK_UNLOCKED;
29 static kmem_cache_t *dn_cache;
30 
redo_inode_mask(struct inode * inode)31 static void redo_inode_mask(struct inode *inode)
32 {
33 	unsigned long new_mask;
34 	struct dnotify_struct *dn;
35 
36 	new_mask = 0;
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;
40 }
41 
dnotify_flush(struct file * filp,fl_owner_t id)42 void dnotify_flush(struct file *filp, fl_owner_t id)
43 {
44 	struct dnotify_struct *dn;
45 	struct dnotify_struct **prev;
46 	struct inode *inode;
47 
48 	inode = filp->f_dentry->d_inode;
49 	if (!S_ISDIR(inode->i_mode))
50 		return;
51 	write_lock(&dn_lock);
52 	prev = &inode->i_dnotify;
53 	while ((dn = *prev) != NULL) {
54 		if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
55 			*prev = dn->dn_next;
56 			redo_inode_mask(inode);
57 			kmem_cache_free(dn_cache, dn);
58 			break;
59 		}
60 		prev = &dn->dn_next;
61 	}
62 	write_unlock(&dn_lock);
63 }
64 
fcntl_dirnotify(int fd,struct file * filp,unsigned long arg)65 int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
66 {
67 	struct dnotify_struct *dn;
68 	struct dnotify_struct *odn;
69 	struct dnotify_struct **prev;
70 	struct inode *inode;
71 	fl_owner_t id = current->files;
72 	struct file *f;
73 
74 	if ((arg & ~DN_MULTISHOT) == 0) {
75 		dnotify_flush(filp, id);
76 		return 0;
77 	}
78 	if (!dir_notify_enable)
79 		return -EINVAL;
80 	inode = filp->f_dentry->d_inode;
81 	if (!S_ISDIR(inode->i_mode))
82 		return -ENOTDIR;
83 	dn = kmem_cache_alloc(dn_cache, SLAB_KERNEL);
84 	if (dn == NULL)
85 		return -ENOMEM;
86 	write_lock(&dn_lock);
87 	prev = &inode->i_dnotify;
88 	while ((odn = *prev) != NULL) {
89 		if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
90 			odn->dn_fd = fd;
91 			odn->dn_mask |= arg;
92 			inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
93 			kmem_cache_free(dn_cache, dn);
94 			goto out;
95 		}
96 		prev = &odn->dn_next;
97 	}
98 
99 	/* we'd lost the race with close(), sod off silently */
100 	read_lock(&current->files->file_lock);
101 	f = fcheck(fd);
102 	read_unlock(&current->files->file_lock);
103 	if (f != filp) {
104 		kmem_cache_free(dn_cache, dn);
105 		goto out;
106 	}
107 
108 	filp->f_owner.pid = current->pid;
109 	filp->f_owner.uid = current->uid;
110 	filp->f_owner.euid = current->euid;
111 	dn->dn_mask = arg;
112 	dn->dn_fd = fd;
113 	dn->dn_filp = filp;
114 	dn->dn_owner = id;
115 	inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
116 	dn->dn_next = inode->i_dnotify;
117 	inode->i_dnotify = dn;
118 out:
119 	write_unlock(&dn_lock);
120 	return 0;
121 }
122 
__inode_dir_notify(struct inode * inode,unsigned long event)123 void __inode_dir_notify(struct inode *inode, unsigned long event)
124 {
125 	struct dnotify_struct *	dn;
126 	struct dnotify_struct **prev;
127 	struct fown_struct *	fown;
128 	int			changed = 0;
129 
130 	write_lock(&dn_lock);
131 	prev = &inode->i_dnotify;
132 	while ((dn = *prev) != NULL) {
133 		if ((dn->dn_mask & event) == 0) {
134 			prev = &dn->dn_next;
135 			continue;
136 		}
137 		fown = &dn->dn_filp->f_owner;
138 		if (fown->pid)
139 		        send_sigio(fown, dn->dn_fd, POLL_MSG);
140 		if (dn->dn_mask & DN_MULTISHOT)
141 			prev = &dn->dn_next;
142 		else {
143 			*prev = dn->dn_next;
144 			changed = 1;
145 			kmem_cache_free(dn_cache, dn);
146 		}
147 	}
148 	if (changed)
149 		redo_inode_mask(inode);
150 	write_unlock(&dn_lock);
151 }
152 
dnotify_init(void)153 static int __init dnotify_init(void)
154 {
155 	dn_cache = kmem_cache_create("dnotify_cache",
156 		sizeof(struct dnotify_struct), 0, 0, NULL, NULL);
157 	if (!dn_cache)
158 		panic("cannot create dnotify slab cache");
159 	return 0;
160 }
161 
162 module_init(dnotify_init)
163