1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      	An implementation of a loadable kernel mode driver providing
4  *		multiple kernel/user space bidirectional communications links.
5  *
6  * 		Author: 	Alan Cox <alan@lxorguk.ukuu.org.uk>
7  *
8  *              Adapted to become the Linux 2.0 Coda pseudo device
9  *              Peter  Braam  <braam@maths.ox.ac.uk>
10  *              Michael Callahan <mjc@emmy.smith.edu>
11  *
12  *              Changes for Linux 2.1
13  *              Copyright (c) 1997 Carnegie-Mellon University
14  */
15 
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/major.h>
20 #include <linux/time.h>
21 #include <linux/sched/signal.h>
22 #include <linux/slab.h>
23 #include <linux/ioport.h>
24 #include <linux/fcntl.h>
25 #include <linux/delay.h>
26 #include <linux/skbuff.h>
27 #include <linux/proc_fs.h>
28 #include <linux/vmalloc.h>
29 #include <linux/fs.h>
30 #include <linux/file.h>
31 #include <linux/poll.h>
32 #include <linux/init.h>
33 #include <linux/list.h>
34 #include <linux/mutex.h>
35 #include <linux/device.h>
36 #include <linux/pid_namespace.h>
37 #include <asm/io.h>
38 #include <linux/uaccess.h>
39 
40 #include <linux/coda.h>
41 #include "coda_psdev.h"
42 #include "coda_linux.h"
43 
44 #include "coda_int.h"
45 
46 /* statistics */
47 int           coda_hard;         /* allows signals during upcalls */
48 unsigned long coda_timeout = 30; /* .. secs, then signals will dequeue */
49 
50 
51 struct venus_comm coda_comms[MAX_CODADEVS];
52 static struct class *coda_psdev_class;
53 
54 /*
55  * Device operations
56  */
57 
coda_psdev_poll(struct file * file,poll_table * wait)58 static __poll_t coda_psdev_poll(struct file *file, poll_table * wait)
59 {
60         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
61 	__poll_t mask = EPOLLOUT | EPOLLWRNORM;
62 
63 	poll_wait(file, &vcp->vc_waitq, wait);
64 	mutex_lock(&vcp->vc_mutex);
65 	if (!list_empty(&vcp->vc_pending))
66                 mask |= EPOLLIN | EPOLLRDNORM;
67 	mutex_unlock(&vcp->vc_mutex);
68 
69 	return mask;
70 }
71 
coda_psdev_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)72 static long coda_psdev_ioctl(struct file * filp, unsigned int cmd, unsigned long arg)
73 {
74 	unsigned int data;
75 
76 	switch(cmd) {
77 	case CIOC_KERNEL_VERSION:
78 		data = CODA_KERNEL_VERSION;
79 		return put_user(data, (int __user *) arg);
80 	default:
81 		return -ENOTTY;
82 	}
83 
84 	return 0;
85 }
86 
87 /*
88  *	Receive a message written by Venus to the psdev
89  */
90 
coda_psdev_write(struct file * file,const char __user * buf,size_t nbytes,loff_t * off)91 static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
92 				size_t nbytes, loff_t *off)
93 {
94         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
95         struct upc_req *req = NULL;
96         struct upc_req *tmp;
97 	struct list_head *lh;
98 	struct coda_in_hdr hdr;
99 	ssize_t retval = 0, count = 0;
100 	int error;
101 
102 	/* make sure there is enough to copy out the (opcode, unique) values */
103 	if (nbytes < (2 * sizeof(u_int32_t)))
104 		return -EINVAL;
105 
106         /* Peek at the opcode, uniquefier */
107 	if (copy_from_user(&hdr, buf, 2 * sizeof(u_int32_t)))
108 	        return -EFAULT;
109 
110         if (DOWNCALL(hdr.opcode)) {
111 		union outputArgs *dcbuf;
112 		int size = sizeof(*dcbuf);
113 
114 		if  ( nbytes < sizeof(struct coda_out_hdr) ) {
115 			pr_warn("coda_downcall opc %d uniq %d, not enough!\n",
116 				hdr.opcode, hdr.unique);
117 			count = nbytes;
118 			goto out;
119 		}
120 		if ( nbytes > size ) {
121 			pr_warn("downcall opc %d, uniq %d, too much!",
122 				hdr.opcode, hdr.unique);
123 		        nbytes = size;
124 		}
125 		dcbuf = kvmalloc(nbytes, GFP_KERNEL);
126 		if (!dcbuf) {
127 			retval = -ENOMEM;
128 			goto out;
129 		}
130 		if (copy_from_user(dcbuf, buf, nbytes)) {
131 			kvfree(dcbuf);
132 			retval = -EFAULT;
133 			goto out;
134 		}
135 
136 		/* what downcall errors does Venus handle ? */
137 		error = coda_downcall(vcp, hdr.opcode, dcbuf, nbytes);
138 
139 		kvfree(dcbuf);
140 		if (error) {
141 			pr_warn("%s: coda_downcall error: %d\n",
142 				__func__, error);
143 			retval = error;
144 			goto out;
145 		}
146 		count = nbytes;
147 		goto out;
148 	}
149 
150 	/* Look for the message on the processing queue. */
151 	mutex_lock(&vcp->vc_mutex);
152 	list_for_each(lh, &vcp->vc_processing) {
153 		tmp = list_entry(lh, struct upc_req , uc_chain);
154 		if (tmp->uc_unique == hdr.unique) {
155 			req = tmp;
156 			list_del(&req->uc_chain);
157 			break;
158 		}
159 	}
160 	mutex_unlock(&vcp->vc_mutex);
161 
162 	if (!req) {
163 		pr_warn("%s: msg (%d, %d) not found\n",
164 			__func__, hdr.opcode, hdr.unique);
165 		retval = -ESRCH;
166 		goto out;
167 	}
168 
169         /* move data into response buffer. */
170 	if (req->uc_outSize < nbytes) {
171 		pr_warn("%s: too much cnt: %d, cnt: %ld, opc: %d, uniq: %d.\n",
172 			__func__, req->uc_outSize, (long)nbytes,
173 			hdr.opcode, hdr.unique);
174 		nbytes = req->uc_outSize; /* don't have more space! */
175 	}
176         if (copy_from_user(req->uc_data, buf, nbytes)) {
177 		req->uc_flags |= CODA_REQ_ABORT;
178 		wake_up(&req->uc_sleep);
179 		retval = -EFAULT;
180 		goto out;
181 	}
182 
183 	/* adjust outsize. is this useful ?? */
184 	req->uc_outSize = nbytes;
185 	req->uc_flags |= CODA_REQ_WRITE;
186 	count = nbytes;
187 
188 	/* Convert filedescriptor into a file handle */
189 	if (req->uc_opcode == CODA_OPEN_BY_FD) {
190 		struct coda_open_by_fd_out *outp =
191 			(struct coda_open_by_fd_out *)req->uc_data;
192 		if (!outp->oh.result) {
193 			outp->fh = fget(outp->fd);
194 			if (!outp->fh)
195 				return -EBADF;
196 		}
197 	}
198 
199         wake_up(&req->uc_sleep);
200 out:
201         return(count ? count : retval);
202 }
203 
204 /*
205  *	Read a message from the kernel to Venus
206  */
207 
coda_psdev_read(struct file * file,char __user * buf,size_t nbytes,loff_t * off)208 static ssize_t coda_psdev_read(struct file * file, char __user * buf,
209 			       size_t nbytes, loff_t *off)
210 {
211 	DECLARE_WAITQUEUE(wait, current);
212         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
213         struct upc_req *req;
214 	ssize_t retval = 0, count = 0;
215 
216 	if (nbytes == 0)
217 		return 0;
218 
219 	mutex_lock(&vcp->vc_mutex);
220 
221 	add_wait_queue(&vcp->vc_waitq, &wait);
222 	set_current_state(TASK_INTERRUPTIBLE);
223 
224 	while (list_empty(&vcp->vc_pending)) {
225 		if (file->f_flags & O_NONBLOCK) {
226 			retval = -EAGAIN;
227 			break;
228 		}
229 		if (signal_pending(current)) {
230 			retval = -ERESTARTSYS;
231 			break;
232 		}
233 		mutex_unlock(&vcp->vc_mutex);
234 		schedule();
235 		mutex_lock(&vcp->vc_mutex);
236 	}
237 
238 	set_current_state(TASK_RUNNING);
239 	remove_wait_queue(&vcp->vc_waitq, &wait);
240 
241 	if (retval)
242 		goto out;
243 
244 	req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
245 	list_del(&req->uc_chain);
246 
247 	/* Move the input args into userspace */
248 	count = req->uc_inSize;
249 	if (nbytes < req->uc_inSize) {
250 		pr_warn("%s: Venus read %ld bytes of %d in message\n",
251 			__func__, (long)nbytes, req->uc_inSize);
252 		count = nbytes;
253         }
254 
255 	if (copy_to_user(buf, req->uc_data, count))
256 	        retval = -EFAULT;
257 
258 	/* If request was not a signal, enqueue and don't free */
259 	if (!(req->uc_flags & CODA_REQ_ASYNC)) {
260 		req->uc_flags |= CODA_REQ_READ;
261 		list_add_tail(&(req->uc_chain), &vcp->vc_processing);
262 		goto out;
263 	}
264 
265 	kvfree(req->uc_data);
266 	kfree(req);
267 out:
268 	mutex_unlock(&vcp->vc_mutex);
269 	return (count ? count : retval);
270 }
271 
coda_psdev_open(struct inode * inode,struct file * file)272 static int coda_psdev_open(struct inode * inode, struct file * file)
273 {
274 	struct venus_comm *vcp;
275 	int idx, err;
276 
277 	if (task_active_pid_ns(current) != &init_pid_ns)
278 		return -EINVAL;
279 
280 	if (current_user_ns() != &init_user_ns)
281 		return -EINVAL;
282 
283 	idx = iminor(inode);
284 	if (idx < 0 || idx >= MAX_CODADEVS)
285 		return -ENODEV;
286 
287 	err = -EBUSY;
288 	vcp = &coda_comms[idx];
289 	mutex_lock(&vcp->vc_mutex);
290 
291 	if (!vcp->vc_inuse) {
292 		vcp->vc_inuse++;
293 
294 		INIT_LIST_HEAD(&vcp->vc_pending);
295 		INIT_LIST_HEAD(&vcp->vc_processing);
296 		init_waitqueue_head(&vcp->vc_waitq);
297 		vcp->vc_sb = NULL;
298 		vcp->vc_seq = 0;
299 
300 		file->private_data = vcp;
301 		err = 0;
302 	}
303 
304 	mutex_unlock(&vcp->vc_mutex);
305 	return err;
306 }
307 
308 
coda_psdev_release(struct inode * inode,struct file * file)309 static int coda_psdev_release(struct inode * inode, struct file * file)
310 {
311 	struct venus_comm *vcp = (struct venus_comm *) file->private_data;
312 	struct upc_req *req, *tmp;
313 
314 	if (!vcp || !vcp->vc_inuse ) {
315 		pr_warn("%s: Not open.\n", __func__);
316 		return -1;
317 	}
318 
319 	mutex_lock(&vcp->vc_mutex);
320 
321 	/* Wakeup clients so they can return. */
322 	list_for_each_entry_safe(req, tmp, &vcp->vc_pending, uc_chain) {
323 		list_del(&req->uc_chain);
324 
325 		/* Async requests need to be freed here */
326 		if (req->uc_flags & CODA_REQ_ASYNC) {
327 			kvfree(req->uc_data);
328 			kfree(req);
329 			continue;
330 		}
331 		req->uc_flags |= CODA_REQ_ABORT;
332 		wake_up(&req->uc_sleep);
333 	}
334 
335 	list_for_each_entry_safe(req, tmp, &vcp->vc_processing, uc_chain) {
336 		list_del(&req->uc_chain);
337 
338 		req->uc_flags |= CODA_REQ_ABORT;
339 		wake_up(&req->uc_sleep);
340 	}
341 
342 	file->private_data = NULL;
343 	vcp->vc_inuse--;
344 	mutex_unlock(&vcp->vc_mutex);
345 	return 0;
346 }
347 
348 
349 static const struct file_operations coda_psdev_fops = {
350 	.owner		= THIS_MODULE,
351 	.read		= coda_psdev_read,
352 	.write		= coda_psdev_write,
353 	.poll		= coda_psdev_poll,
354 	.unlocked_ioctl	= coda_psdev_ioctl,
355 	.open		= coda_psdev_open,
356 	.release	= coda_psdev_release,
357 	.llseek		= noop_llseek,
358 };
359 
init_coda_psdev(void)360 static int __init init_coda_psdev(void)
361 {
362 	int i, err = 0;
363 	if (register_chrdev(CODA_PSDEV_MAJOR, "coda", &coda_psdev_fops)) {
364 		pr_err("%s: unable to get major %d\n",
365 		       __func__, CODA_PSDEV_MAJOR);
366 		return -EIO;
367 	}
368 	coda_psdev_class = class_create(THIS_MODULE, "coda");
369 	if (IS_ERR(coda_psdev_class)) {
370 		err = PTR_ERR(coda_psdev_class);
371 		goto out_chrdev;
372 	}
373 	for (i = 0; i < MAX_CODADEVS; i++) {
374 		mutex_init(&(&coda_comms[i])->vc_mutex);
375 		device_create(coda_psdev_class, NULL,
376 			      MKDEV(CODA_PSDEV_MAJOR, i), NULL, "cfs%d", i);
377 	}
378 	coda_sysctl_init();
379 	goto out;
380 
381 out_chrdev:
382 	unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
383 out:
384 	return err;
385 }
386 
387 MODULE_AUTHOR("Jan Harkes, Peter J. Braam");
388 MODULE_DESCRIPTION("Coda Distributed File System VFS interface");
389 MODULE_ALIAS_CHARDEV_MAJOR(CODA_PSDEV_MAJOR);
390 MODULE_LICENSE("GPL");
391 MODULE_VERSION("7.0");
392 
init_coda(void)393 static int __init init_coda(void)
394 {
395 	int status;
396 	int i;
397 
398 	status = coda_init_inodecache();
399 	if (status)
400 		goto out2;
401 	status = init_coda_psdev();
402 	if ( status ) {
403 		pr_warn("Problem (%d) in init_coda_psdev\n", status);
404 		goto out1;
405 	}
406 
407 	status = register_filesystem(&coda_fs_type);
408 	if (status) {
409 		pr_warn("failed to register filesystem!\n");
410 		goto out;
411 	}
412 	return 0;
413 out:
414 	for (i = 0; i < MAX_CODADEVS; i++)
415 		device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
416 	class_destroy(coda_psdev_class);
417 	unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
418 	coda_sysctl_clean();
419 out1:
420 	coda_destroy_inodecache();
421 out2:
422 	return status;
423 }
424 
exit_coda(void)425 static void __exit exit_coda(void)
426 {
427         int err, i;
428 
429 	err = unregister_filesystem(&coda_fs_type);
430 	if (err != 0)
431 		pr_warn("failed to unregister filesystem\n");
432 	for (i = 0; i < MAX_CODADEVS; i++)
433 		device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
434 	class_destroy(coda_psdev_class);
435 	unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
436 	coda_sysctl_clean();
437 	coda_destroy_inodecache();
438 }
439 
440 module_init(init_coda);
441 module_exit(exit_coda);
442 
443