Import 2.4.0-test2pre7
[davej-history.git] / fs / coda / psdev.c
blob582ea7000d7c59690586f8fd68c92377bc8c602a
1 /*
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>
25 #include <linux/lp.h>
26 #include <linux/malloc.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>
34 #include <linux/fs.h>
35 #include <linux/poll.h>
36 #include <linux/init.h>
37 #include <linux/list.h>
38 #include <linux/smp_lock.h>
39 #include <asm/io.h>
40 #include <asm/segment.h>
41 #include <asm/system.h>
42 #include <asm/poll.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_cache.h>
50 #include <linux/coda_proc.h>
52 /*
53 * Coda stuff
55 extern struct file_system_type coda_fs_type;
56 extern int init_coda_fs(void);
58 /* statistics */
59 int coda_hard = 0; /* allows signals during upcalls */
60 unsigned long coda_timeout = 30; /* .. secs, then signals will dequeue */
62 struct coda_sb_info coda_super_info;
63 struct venus_comm coda_upc_comm;
66 * Device operations
69 static unsigned int coda_psdev_poll(struct file *file, poll_table * wait)
71 struct venus_comm *vcp = &coda_upc_comm;
72 unsigned int mask = POLLOUT | POLLWRNORM;
74 poll_wait(file, &vcp->vc_waitq, wait);
75 if (!list_empty(&vcp->vc_pending))
76 mask |= POLLIN | POLLRDNORM;
78 return mask;
81 static int coda_psdev_ioctl(struct inode * inode, struct file * filp,
82 unsigned int cmd, unsigned long arg)
84 unsigned int data;
86 switch(cmd) {
87 case CIOC_KERNEL_VERSION:
88 data = CODA_KERNEL_VERSION;
89 return put_user(data, (int *) arg);
90 default:
91 return -ENOTTY;
94 return 0;
98 * Receive a message written by Venus to the psdev
101 static ssize_t coda_psdev_write(struct file *file, const char *buf,
102 size_t nbytes, loff_t *off)
104 struct venus_comm *vcp = &coda_upc_comm;
105 struct upc_req *req = NULL;
106 struct upc_req *tmp;
107 struct list_head *lh;
108 struct coda_in_hdr hdr;
109 ssize_t retval = 0, count = 0;
110 int error;
112 if ( !coda_upc_comm.vc_pid )
113 return -EIO;
114 /* Peek at the opcode, uniquefier */
115 if (copy_from_user(&hdr, buf, 2 * sizeof(u_long)))
116 return -EFAULT;
118 CDEBUG(D_PSDEV, "(process,opc,uniq)=(%d,%ld,%ld), nbytes %ld\n",
119 current->pid, hdr.opcode, hdr.unique, (long)nbytes);
121 if (DOWNCALL(hdr.opcode)) {
122 struct super_block *sb = NULL;
123 union outputArgs *dcbuf;
124 int size = sizeof(*dcbuf);
126 sb = coda_super_info.sbi_sb;
127 if ( !sb ) {
128 CDEBUG(D_PSDEV, "coda_psdev_write: downcall, no SB!\n");
129 count = nbytes;
130 goto out;
132 CDEBUG(D_PSDEV, "handling downcall\n");
134 if ( nbytes < sizeof(struct coda_out_hdr) ) {
135 printk("coda_downcall opc %ld uniq %ld, not enough!\n",
136 hdr.opcode, hdr.unique);
137 count = nbytes;
138 goto out;
140 if ( nbytes > size ) {
141 printk("Coda: downcall opc %ld, uniq %ld, too much!",
142 hdr.opcode, hdr.unique);
143 nbytes = size;
145 CODA_ALLOC(dcbuf, union outputArgs *, nbytes);
146 if (copy_from_user(dcbuf, buf, nbytes)) {
147 CODA_FREE(dcbuf, nbytes);
148 retval = -EFAULT;
149 goto out;
152 /* what downcall errors does Venus handle ? */
153 lock_kernel();
154 error = coda_downcall(hdr.opcode, dcbuf, sb);
155 unlock_kernel();
157 CODA_FREE(dcbuf, nbytes);
158 if (error) {
159 printk("psdev_write: coda_downcall error: %d\n", error);
160 retval = error;
161 goto out;
163 count = nbytes;
164 goto out;
167 /* Look for the message on the processing queue. */
168 lock_kernel();
169 lh = &vcp->vc_processing;
170 while ( (lh = lh->next) != &vcp->vc_processing ) {
171 tmp = list_entry(lh, struct upc_req , uc_chain);
172 if (tmp->uc_unique == hdr.unique) {
173 req = tmp;
174 list_del(&req->uc_chain);
175 CDEBUG(D_PSDEV,"Eureka: uniq %ld on queue!\n",
176 hdr.unique);
177 break;
180 unlock_kernel();
182 if (!req) {
183 printk("psdev_write: msg (%ld, %ld) not found\n",
184 hdr.opcode, hdr.unique);
185 retval = -ESRCH;
186 goto out;
189 /* move data into response buffer. */
190 if (req->uc_outSize < nbytes) {
191 printk("psdev_write: too much cnt: %d, cnt: %ld, opc: %ld, uniq: %ld.\n",
192 req->uc_outSize, (long)nbytes, hdr.opcode, hdr.unique);
193 nbytes = req->uc_outSize; /* don't have more space! */
195 if (copy_from_user(req->uc_data, buf, nbytes)) {
196 req->uc_flags |= REQ_ABORT;
197 wake_up(&req->uc_sleep);
198 retval = -EFAULT;
199 goto out;
202 /* adjust outsize. is this usefull ?? */
203 req->uc_outSize = nbytes;
204 req->uc_flags |= REQ_WRITE;
205 count = nbytes;
207 CDEBUG(D_PSDEV,
208 "Found! Count %ld for (opc,uniq)=(%ld,%ld), upc_req at %p\n",
209 (long)count, hdr.opcode, hdr.unique, &req);
211 wake_up(&req->uc_sleep);
212 out:
213 return(count ? count : retval);
217 * Read a message from the kernel to Venus
220 static ssize_t coda_psdev_read(struct file * file, char * buf,
221 size_t nbytes, loff_t *off)
223 DECLARE_WAITQUEUE(wait, current);
224 struct venus_comm *vcp = &coda_upc_comm;
225 struct upc_req *req;
226 ssize_t retval = 0, count = 0;
228 if (nbytes == 0)
229 return 0;
231 lock_kernel();
233 add_wait_queue(&vcp->vc_waitq, &wait);
234 set_current_state(TASK_INTERRUPTIBLE);
236 while (list_empty(&vcp->vc_pending)) {
237 if (file->f_flags & O_NONBLOCK) {
238 retval = -EAGAIN;
239 break;
241 if (signal_pending(current)) {
242 retval = -ERESTARTSYS;
243 break;
245 schedule();
248 current->state = TASK_RUNNING;
249 remove_wait_queue(&vcp->vc_waitq, &wait);
251 if (retval)
252 goto out;
254 req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
255 list_del(&req->uc_chain);
257 /* Move the input args into userspace */
258 count = req->uc_inSize;
259 if (nbytes < req->uc_inSize) {
260 printk ("psdev_read: Venus read %ld bytes of %d in message\n",
261 (long)nbytes, req->uc_inSize);
262 count = nbytes;
265 if (copy_to_user(buf, req->uc_data, count)) {
266 retval = -EFAULT;
267 goto free_out;
270 /* If request was not a signal, enqueue and don't free */
271 if (req->uc_opcode != CODA_SIGNAL) {
272 req->uc_flags |= REQ_READ;
273 list_add(&(req->uc_chain), vcp->vc_processing.prev);
274 goto out;
277 CDEBUG(D_PSDEV, "vcread: signal msg (%d, %d)\n",
278 req->uc_opcode, req->uc_unique);
280 free_out:
281 CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
282 CODA_FREE(req, sizeof(struct upc_req));
283 out:
284 unlock_kernel();
285 return (count ? count : retval);
289 static int coda_psdev_open(struct inode * inode, struct file * file)
291 struct venus_comm *vcp = &coda_upc_comm;
292 ENTRY;
294 /* first opener: must be lento. Initialize & take its pid */
295 if ( (file->f_flags & O_ACCMODE) == O_RDWR ) {
296 if ( vcp->vc_pid ) {
297 printk("Venus pid already set to %d!!\n", vcp->vc_pid);
298 return -1;
300 if ( vcp->vc_inuse ) {
301 printk("psdev_open: Cannot O_RDWR while open.\n");
302 return -1;
306 vcp->vc_inuse++;
308 if ( (file->f_flags & O_ACCMODE) == O_RDWR ) {
309 vcp->vc_pid = current->pid;
310 vcp->vc_seq = 0;
311 INIT_LIST_HEAD(&vcp->vc_pending);
312 INIT_LIST_HEAD(&vcp->vc_processing);
315 CDEBUG(D_PSDEV, "inuse: %d, vc_pid %d, caller %d\n",
316 vcp->vc_inuse, vcp->vc_pid, current->pid);
318 EXIT;
319 return 0;
323 static int coda_psdev_release(struct inode * inode, struct file * file)
325 struct venus_comm *vcp = &coda_upc_comm;
326 struct upc_req *req;
327 struct list_head *lh, *next;
328 ENTRY;
330 if ( !vcp->vc_inuse ) {
331 printk("psdev_release: Not open.\n");
332 return -1;
335 vcp->vc_inuse--;
336 CDEBUG(D_PSDEV, "inuse: %d, vc_pid %d, caller %d\n",
337 vcp->vc_inuse, vcp->vc_pid, current->pid);
339 if ( vcp->vc_pid != current->pid ) {
340 /* FIXME: this is broken. If venus does fork(), accounting goes wrong */
341 printk( "Closed by someone else than caller?\n" );
342 return 0;
345 vcp->vc_pid = 0;
346 /* Wakeup clients so they can return. */
347 CDEBUG(D_PSDEV, "wake up pending clients\n");
348 lh = vcp->vc_pending.next;
349 next = lh;
350 while ( (lh = next) != &vcp->vc_pending) {
351 next = lh->next;
352 req = list_entry(lh, struct upc_req, uc_chain);
353 /* Async requests need to be freed here */
354 if (req->uc_flags & REQ_ASYNC) {
355 CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
356 CODA_FREE(req, (u_int)sizeof(struct upc_req));
357 continue;
359 req->uc_flags |= REQ_ABORT;
360 wake_up(&req->uc_sleep);
363 lh = &vcp->vc_processing;
364 CDEBUG(D_PSDEV, "wake up processing clients\n");
365 while ( (lh = lh->next) != &vcp->vc_processing) {
366 req = list_entry(lh, struct upc_req, uc_chain);
367 req->uc_flags |= REQ_ABORT;
368 wake_up(&req->uc_sleep);
370 CDEBUG(D_PSDEV, "Done.\n");
372 EXIT;
373 return 0;
377 static struct file_operations coda_psdev_fops = {
378 owner: THIS_MODULE,
379 read: coda_psdev_read,
380 write: coda_psdev_write,
381 poll: coda_psdev_poll,
382 ioctl: coda_psdev_ioctl,
383 open: coda_psdev_open,
384 release: coda_psdev_release,
389 int __init init_coda(void)
391 int status;
392 printk(KERN_INFO "Coda Kernel/Venus communications, v4.6.0, braam@cs.cmu.edu\n");
394 status = init_coda_psdev();
395 if ( status ) {
396 printk("Problem (%d) in init_coda_psdev\n", status);
397 return status;
400 status = init_coda_fs();
401 if (status) {
402 printk("coda: failed in init_coda_fs!\n");
404 return status;
407 static devfs_handle_t devfs_handle = NULL;
409 int init_coda_psdev(void)
411 if(devfs_register_chrdev(CODA_PSDEV_MAJOR,"coda_psdev",
412 &coda_psdev_fops)) {
413 printk(KERN_ERR "coda_psdev: unable to get major %d\n",
414 CODA_PSDEV_MAJOR);
415 return -EIO;
417 devfs_handle = devfs_mk_dir (NULL, "coda", 4, NULL);
418 devfs_register_series (devfs_handle, "%u", MAX_CODADEVS, DEVFS_FL_NONE,
419 CODA_PSDEV_MAJOR, 0,
420 S_IFCHR | S_IRUSR | S_IWUSR, 0, 0,
421 &coda_psdev_fops, NULL);
422 memset(&coda_upc_comm, 0, sizeof(coda_upc_comm));
423 memset(&coda_super_info, 0, sizeof(coda_super_info));
424 init_waitqueue_head(&coda_upc_comm.vc_waitq);
426 coda_sysctl_init();
428 return 0;
432 #ifdef MODULE
434 MODULE_AUTHOR("Peter J. Braam <braam@cs.cmu.edu>");
436 int init_module(void)
438 int status;
439 printk(KERN_INFO "Coda Kernel/Venus communications (module), v5.0-pre1, braam@cs.cmu.edu.\n");
441 status = init_coda_psdev();
442 if ( status ) {
443 printk("Problem (%d) in init_coda_psdev\n", status);
444 return status;
447 status = init_coda_fs();
448 if (status) {
449 printk("coda: failed in init_coda_fs!\n");
451 return status;
455 void cleanup_module(void)
457 int err;
459 ENTRY;
461 if ( (err = unregister_filesystem(&coda_fs_type)) != 0 ) {
462 printk("coda: failed to unregister filesystem\n");
464 devfs_unregister (devfs_handle);
465 devfs_unregister_chrdev(CODA_PSDEV_MAJOR,"coda_psdev");
466 coda_sysctl_clean();
469 #endif