2 * An implementation of a loadable kernel mode driver providing
3 * multiple kernel/user space bidirectional communications links.
5 * Author: Alan Cox <alan@redhat.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Adapted to become the Linux 2.0 Coda pseudo device
13 * Peter Braam <braam@maths.ox.ac.uk>
14 * Michael Callahan <mjc@emmy.smith.edu>
16 * Changes for Linux 2.1
17 * Copyright (c) 1997 Carnegie-Mellon University
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/major.h>
24 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/ioport.h>
28 #include <linux/fcntl.h>
29 #include <linux/delay.h>
30 #include <linux/skbuff.h>
31 #include <linux/proc_fs.h>
32 #include <linux/devfs_fs_kernel.h>
33 #include <linux/vmalloc.h>
35 #include <linux/file.h>
36 #include <linux/poll.h>
37 #include <linux/init.h>
38 #include <linux/list.h>
39 #include <linux/smp_lock.h>
41 #include <asm/system.h>
43 #include <asm/uaccess.h>
45 #include <linux/coda.h>
46 #include <linux/coda_linux.h>
47 #include <linux/coda_fs_i.h>
48 #include <linux/coda_psdev.h>
49 #include <linux/coda_proc.h>
51 #define upc_free(r) kfree(r)
56 extern struct file_system_type coda_fs_type;
59 int coda_hard; /* allows signals during upcalls */
60 unsigned long coda_timeout = 30; /* .. secs, then signals will dequeue */
63 struct venus_comm coda_comms[MAX_CODADEVS];
64 kmem_cache_t *cii_cache, *cred_cache, *upc_cache;
70 static unsigned int coda_psdev_poll(struct file *file, poll_table * wait)
72 struct venus_comm *vcp = (struct venus_comm *) file->private_data;
73 unsigned int mask = POLLOUT | POLLWRNORM;
75 poll_wait(file, &vcp->vc_waitq, wait);
76 if (!list_empty(&vcp->vc_pending))
77 mask |= POLLIN | POLLRDNORM;
82 static int coda_psdev_ioctl(struct inode * inode, struct file * filp,
83 unsigned int cmd, unsigned long arg)
88 case CIOC_KERNEL_VERSION:
89 data = CODA_KERNEL_VERSION;
90 return put_user(data, (int *) arg);
99 * Receive a message written by Venus to the psdev
102 static ssize_t coda_psdev_write(struct file *file, const char *buf,
103 size_t nbytes, loff_t *off)
105 struct venus_comm *vcp = (struct venus_comm *) file->private_data;
106 struct upc_req *req = NULL;
108 struct list_head *lh;
109 struct coda_in_hdr hdr;
110 ssize_t retval = 0, count = 0;
113 /* Peek at the opcode, uniquefier */
114 if (copy_from_user(&hdr, buf, 2 * sizeof(u_long)))
117 CDEBUG(D_PSDEV, "(process,opc,uniq)=(%d,%ld,%ld), nbytes %ld\n",
118 current->pid, hdr.opcode, hdr.unique, (long)nbytes);
120 if (DOWNCALL(hdr.opcode)) {
121 struct super_block *sb = NULL;
122 union outputArgs *dcbuf;
123 int size = sizeof(*dcbuf);
127 CDEBUG(D_PSDEV, "coda_psdev_write: downcall, no SB!\n");
131 CDEBUG(D_PSDEV, "handling downcall\n");
133 if ( nbytes < sizeof(struct coda_out_hdr) ) {
134 printk("coda_downcall opc %ld uniq %ld, not enough!\n",
135 hdr.opcode, hdr.unique);
139 if ( nbytes > size ) {
140 printk("Coda: downcall opc %ld, uniq %ld, too much!",
141 hdr.opcode, hdr.unique);
144 CODA_ALLOC(dcbuf, union outputArgs *, nbytes);
145 if (copy_from_user(dcbuf, buf, nbytes)) {
146 CODA_FREE(dcbuf, nbytes);
151 /* what downcall errors does Venus handle ? */
153 error = coda_downcall(hdr.opcode, dcbuf, sb);
156 CODA_FREE(dcbuf, nbytes);
158 printk("psdev_write: coda_downcall error: %d\n", error);
166 /* Look for the message on the processing queue. */
168 list_for_each(lh, &vcp->vc_processing) {
169 tmp = list_entry(lh, struct upc_req , uc_chain);
170 if (tmp->uc_unique == hdr.unique) {
172 list_del(&req->uc_chain);
179 printk("psdev_write: msg (%ld, %ld) not found\n",
180 hdr.opcode, hdr.unique);
185 CDEBUG(D_PSDEV,"Eureka: uniq %ld on queue!\n", hdr.unique);
187 /* move data into response buffer. */
188 if (req->uc_outSize < nbytes) {
189 printk("psdev_write: too much cnt: %d, cnt: %ld, opc: %ld, uniq: %ld.\n",
190 req->uc_outSize, (long)nbytes, hdr.opcode, hdr.unique);
191 nbytes = req->uc_outSize; /* don't have more space! */
193 if (copy_from_user(req->uc_data, buf, nbytes)) {
194 req->uc_flags |= REQ_ABORT;
195 wake_up(&req->uc_sleep);
200 /* adjust outsize. is this useful ?? */
201 req->uc_outSize = nbytes;
202 req->uc_flags |= REQ_WRITE;
205 /* Convert filedescriptor into a file handle */
206 if (req->uc_opcode == CODA_OPEN_BY_FD) {
207 struct coda_open_by_fd_out *outp =
208 (struct coda_open_by_fd_out *)req->uc_data;
209 outp->fh = fget(outp->fd);
213 "Found! Count %ld for (opc,uniq)=(%ld,%ld), upc_req at %p\n",
214 (long)count, hdr.opcode, hdr.unique, &req);
216 wake_up(&req->uc_sleep);
218 return(count ? count : retval);
222 * Read a message from the kernel to Venus
225 static ssize_t coda_psdev_read(struct file * file, char * buf,
226 size_t nbytes, loff_t *off)
228 DECLARE_WAITQUEUE(wait, current);
229 struct venus_comm *vcp = (struct venus_comm *) file->private_data;
231 ssize_t retval = 0, count = 0;
238 add_wait_queue(&vcp->vc_waitq, &wait);
239 set_current_state(TASK_INTERRUPTIBLE);
241 while (list_empty(&vcp->vc_pending)) {
242 if (file->f_flags & O_NONBLOCK) {
246 if (signal_pending(current)) {
247 retval = -ERESTARTSYS;
253 set_current_state(TASK_RUNNING);
254 remove_wait_queue(&vcp->vc_waitq, &wait);
259 req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
260 list_del(&req->uc_chain);
262 /* Move the input args into userspace */
263 count = req->uc_inSize;
264 if (nbytes < req->uc_inSize) {
265 printk ("psdev_read: Venus read %ld bytes of %d in message\n",
266 (long)nbytes, req->uc_inSize);
270 if (copy_to_user(buf, req->uc_data, count))
273 /* If request was not a signal, enqueue and don't free */
274 if (!(req->uc_flags & REQ_ASYNC)) {
275 req->uc_flags |= REQ_READ;
276 list_add(&(req->uc_chain), vcp->vc_processing.prev);
280 CDEBUG(D_PSDEV, "vcread: signal msg (%d, %d)\n",
281 req->uc_opcode, req->uc_unique);
283 CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
287 return (count ? count : retval);
290 static int coda_psdev_open(struct inode * inode, struct file * file)
292 struct venus_comm *vcp;
296 idx = MINOR(inode->i_rdev);
297 if(idx >= MAX_CODADEVS) {
302 vcp = &coda_comms[idx];
308 if (!vcp->vc_inuse++) {
309 INIT_LIST_HEAD(&vcp->vc_pending);
310 INIT_LIST_HEAD(&vcp->vc_processing);
311 init_waitqueue_head(&vcp->vc_waitq);
316 file->private_data = vcp;
318 CDEBUG(D_PSDEV, "device %i - inuse: %d\n", idx, vcp->vc_inuse);
325 static int coda_psdev_release(struct inode * inode, struct file * file)
327 struct venus_comm *vcp = (struct venus_comm *) file->private_data;
329 struct list_head *lh, *next;
332 if ( !vcp->vc_inuse ) {
334 printk("psdev_release: Not open.\n");
338 CDEBUG(D_PSDEV, "psdev_release: inuse %d\n", vcp->vc_inuse);
339 if (--vcp->vc_inuse) {
344 /* Wakeup clients so they can return. */
345 CDEBUG(D_PSDEV, "wake up pending clients\n");
346 lh = vcp->vc_pending.next;
348 while ( (lh = next) != &vcp->vc_pending) {
350 req = list_entry(lh, struct upc_req, uc_chain);
351 /* Async requests need to be freed here */
352 if (req->uc_flags & REQ_ASYNC) {
353 CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
357 req->uc_flags |= REQ_ABORT;
358 wake_up(&req->uc_sleep);
361 lh = &vcp->vc_processing;
362 CDEBUG(D_PSDEV, "wake up processing clients\n");
363 while ( (lh = lh->next) != &vcp->vc_processing) {
364 req = list_entry(lh, struct upc_req, uc_chain);
365 req->uc_flags |= REQ_ABORT;
366 wake_up(&req->uc_sleep);
368 CDEBUG(D_PSDEV, "Done.\n");
375 static struct file_operations coda_psdev_fops = {
377 read: coda_psdev_read,
378 write: coda_psdev_write,
379 poll: coda_psdev_poll,
380 ioctl: coda_psdev_ioctl,
381 open: coda_psdev_open,
382 release: coda_psdev_release,
385 static devfs_handle_t devfs_handle;
387 static int init_coda_psdev(void)
389 if(devfs_register_chrdev(CODA_PSDEV_MAJOR,"coda_psdev",
391 printk(KERN_ERR "coda_psdev: unable to get major %d\n",
395 devfs_handle = devfs_mk_dir (NULL, "coda", NULL);
396 devfs_register_series (devfs_handle, "%u", MAX_CODADEVS, DEVFS_FL_NONE,
398 S_IFCHR | S_IRUSR | S_IWUSR,
399 &coda_psdev_fops, NULL);
407 MODULE_AUTHOR("Peter J. Braam <braam@cs.cmu.edu>");
408 MODULE_LICENSE("GPL");
410 static int __init init_coda(void)
413 printk(KERN_INFO "Coda Kernel/Venus communications, v5.3.18, coda@cs.cmu.edu\n");
415 status = init_coda_psdev();
417 printk("Problem (%d) in init_coda_psdev\n", status);
421 status = register_filesystem(&coda_fs_type);
423 printk("coda: failed to register filesystem!\n");
424 devfs_unregister(devfs_handle);
425 devfs_unregister_chrdev(CODA_PSDEV_MAJOR,"coda_psdev");
431 static void __exit exit_coda(void)
435 err = unregister_filesystem(&coda_fs_type);
437 printk("coda: failed to unregister filesystem\n");
439 devfs_unregister(devfs_handle);
440 devfs_unregister_chrdev(CODA_PSDEV_MAJOR, "coda_psdev");
444 module_init(init_coda);
445 module_exit(exit_coda);