File: /usr/src/linux/fs/coda/psdev.c

1     /*
2      *      	An implementation of a loadable kernel mode driver providing
3      *		multiple kernel/user space bidirectional communications links.
4      *
5      * 		Author: 	Alan Cox <alan@redhat.com>
6      *
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.
11      * 
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>           
15      *
16      *              Changes for Linux 2.1
17      *              Copyright (c) 1997 Carnegie-Mellon University
18      */
19     
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/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>
34     #include <linux/fs.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>
40     #include <asm/io.h>
41     #include <asm/segment.h>
42     #include <asm/system.h>
43     #include <asm/poll.h>
44     #include <asm/uaccess.h>
45     
46     #include <linux/coda.h>
47     #include <linux/coda_linux.h>
48     #include <linux/coda_fs_i.h>
49     #include <linux/coda_psdev.h>
50     #include <linux/coda_proc.h>
51     
52     #define upc_free(r) kfree(r)
53     
54     /* 
55      * Coda stuff
56      */
57     extern struct file_system_type coda_fs_type;
58     
59     /* statistics */
60     int           coda_hard;         /* allows signals during upcalls */
61     unsigned long coda_timeout = 30; /* .. secs, then signals will dequeue */
62     
63     
64     struct venus_comm coda_comms[MAX_CODADEVS];
65     kmem_cache_t *cii_cache, *cred_cache, *upc_cache;
66     
67     /*
68      * Device operations
69      */
70     
71     static unsigned int coda_psdev_poll(struct file *file, poll_table * wait)
72     {
73             struct venus_comm *vcp = (struct venus_comm *) file->private_data;
74     	unsigned int mask = POLLOUT | POLLWRNORM;
75     
76     	poll_wait(file, &vcp->vc_waitq, wait);
77     	if (!list_empty(&vcp->vc_pending))
78                     mask |= POLLIN | POLLRDNORM;
79     
80     	return mask;
81     }
82     
83     static int coda_psdev_ioctl(struct inode * inode, struct file * filp, 
84     			    unsigned int cmd, unsigned long arg)
85     {
86     	unsigned int data;
87     
88     	switch(cmd) {
89     	case CIOC_KERNEL_VERSION:
90     		data = CODA_KERNEL_VERSION;
91     		return put_user(data, (int *) arg);
92     	default:
93     		return -ENOTTY;
94     	}
95     
96     	return 0;
97     }
98     
99     /*
100      *	Receive a message written by Venus to the psdev
101      */
102      
103     static ssize_t coda_psdev_write(struct file *file, const char *buf, 
104     				size_t nbytes, loff_t *off)
105     {
106             struct venus_comm *vcp = (struct venus_comm *) file->private_data;
107             struct upc_req *req = NULL;
108             struct upc_req *tmp;
109     	struct list_head *lh;
110     	struct coda_in_hdr hdr;
111     	ssize_t retval = 0, count = 0;
112     	int error;
113     
114             /* Peek at the opcode, uniquefier */
115     	if (copy_from_user(&hdr, buf, 2 * sizeof(u_long)))
116     	        return -EFAULT;
117     
118     	CDEBUG(D_PSDEV, "(process,opc,uniq)=(%d,%ld,%ld), nbytes %ld\n", 
119     	       current->pid, hdr.opcode, hdr.unique, (long)nbytes);
120     
121             if (DOWNCALL(hdr.opcode)) {
122     		struct super_block *sb = NULL;
123                     union outputArgs *dcbuf;
124     		int size = sizeof(*dcbuf);
125     
126     		sb = vcp->vc_sb;
127     		if ( !sb ) {
128     			CDEBUG(D_PSDEV, "coda_psdev_write: downcall, no SB!\n");
129                             count = nbytes;
130                             goto out;
131     		}
132     		CDEBUG(D_PSDEV, "handling downcall\n");
133     
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;
139     		}
140     		if ( nbytes > size ) {
141     		        printk("Coda: downcall opc %ld, uniq %ld, too much!",
142     			       hdr.opcode, hdr.unique);
143     		        nbytes = size;
144     		}
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;
150     		}
151     
152     		/* what downcall errors does Venus handle ? */
153     		lock_kernel();
154     		error = coda_downcall(hdr.opcode, dcbuf, sb);
155     		unlock_kernel();
156     
157     		CODA_FREE(dcbuf, nbytes);
158     		if (error) {
159     		        printk("psdev_write: coda_downcall error: %d\n", error);
160     			retval = error;
161     			goto out;
162     		}
163     		count = nbytes;
164     		goto out;
165     	}
166             
167     	/* Look for the message on the processing queue. */
168     	lock_kernel();
169     	list_for_each(lh, &vcp->vc_processing) {
170     		tmp = list_entry(lh, struct upc_req , uc_chain);
171     		if (tmp->uc_unique == hdr.unique) {
172     			req = tmp;
173     			list_del(&req->uc_chain);
174     			break;
175     		}
176     	}
177     	unlock_kernel();
178     
179     	if (!req) {
180     		printk("psdev_write: msg (%ld, %ld) not found\n", 
181     			hdr.opcode, hdr.unique);
182     		retval = -ESRCH;
183     		goto out;
184     	}
185     
186     	CDEBUG(D_PSDEV,"Eureka: uniq %ld on queue!\n", hdr.unique);
187     
188             /* move data into response buffer. */
189     	if (req->uc_outSize < nbytes) {
190                     printk("psdev_write: too much cnt: %d, cnt: %ld, opc: %ld, uniq: %ld.\n",
191     		       req->uc_outSize, (long)nbytes, hdr.opcode, hdr.unique);
192     		nbytes = req->uc_outSize; /* don't have more space! */
193     	}
194             if (copy_from_user(req->uc_data, buf, nbytes)) {
195     		req->uc_flags |= REQ_ABORT;
196     		wake_up(&req->uc_sleep);
197     		retval = -EFAULT;
198     		goto out;
199     	}
200     
201     	/* adjust outsize. is this useful ?? */
202             req->uc_outSize = nbytes;	
203             req->uc_flags |= REQ_WRITE;
204     	count = nbytes;
205     
206     	/* Convert filedescriptor into a file handle */
207     	if (req->uc_opcode == CODA_OPEN_BY_FD) {
208     		struct coda_open_by_fd_out *outp =
209     			(struct coda_open_by_fd_out *)req->uc_data;
210     		outp->fh = fget(outp->fd);
211     	}
212     
213     	CDEBUG(D_PSDEV, 
214     	       "Found! Count %ld for (opc,uniq)=(%ld,%ld), upc_req at %p\n", 
215     	        (long)count, hdr.opcode, hdr.unique, &req);
216     
217             wake_up(&req->uc_sleep);
218     out:
219             return(count ? count : retval);  
220     }
221     
222     /*
223      *	Read a message from the kernel to Venus
224      */
225     
226     static ssize_t coda_psdev_read(struct file * file, char * buf, 
227     			       size_t nbytes, loff_t *off)
228     {
229     	DECLARE_WAITQUEUE(wait, current);
230             struct venus_comm *vcp = (struct venus_comm *) file->private_data;
231             struct upc_req *req;
232     	ssize_t retval = 0, count = 0;
233     
234     	if (nbytes == 0)
235     		return 0;
236     
237     	lock_kernel();
238     
239     	add_wait_queue(&vcp->vc_waitq, &wait);
240     	set_current_state(TASK_INTERRUPTIBLE);
241     
242     	while (list_empty(&vcp->vc_pending)) {
243     		if (file->f_flags & O_NONBLOCK) {
244     			retval = -EAGAIN;
245     			break;
246     		}
247     		if (signal_pending(current)) {
248     			retval = -ERESTARTSYS;
249     			break;
250     		}
251     		schedule();
252     	}
253     
254     	set_current_state(TASK_RUNNING);
255     	remove_wait_queue(&vcp->vc_waitq, &wait);
256     
257     	if (retval)
258     		goto out;
259     
260     	req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
261     	list_del(&req->uc_chain);
262     
263     	/* Move the input args into userspace */
264     	count = req->uc_inSize;
265     	if (nbytes < req->uc_inSize) {
266                     printk ("psdev_read: Venus read %ld bytes of %d in message\n",
267     			(long)nbytes, req->uc_inSize);
268     		count = nbytes;
269             }
270     
271     	if (copy_to_user(buf, req->uc_data, count))
272     	        retval = -EFAULT;
273             
274     	/* If request was not a signal, enqueue and don't free */
275     	if (!(req->uc_flags & REQ_ASYNC)) {
276     		req->uc_flags |= REQ_READ;
277     		list_add(&(req->uc_chain), vcp->vc_processing.prev);
278     		goto out;
279     	}
280     
281     	CDEBUG(D_PSDEV, "vcread: signal msg (%d, %d)\n", 
282     			req->uc_opcode, req->uc_unique);
283     
284     	CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
285     	upc_free(req);
286     out:
287     	unlock_kernel();
288     	return (count ? count : retval);
289     }
290     
291     static int coda_psdev_open(struct inode * inode, struct file * file)
292     {
293             struct venus_comm *vcp;
294     	int idx;
295             ENTRY;
296     
297     	lock_kernel();
298     	idx = MINOR(inode->i_rdev);
299     	if(idx >= MAX_CODADEVS) {
300     		unlock_kernel();
301     		return -ENODEV;
302     	}
303     
304     	vcp = &coda_comms[idx];
305     	if(vcp->vc_inuse) {
306     		unlock_kernel();
307     		return -EBUSY;
308     	}
309     	
310     	if (!vcp->vc_inuse++) {
311     		INIT_LIST_HEAD(&vcp->vc_pending);
312     		INIT_LIST_HEAD(&vcp->vc_processing);
313     		init_waitqueue_head(&vcp->vc_waitq);
314     		vcp->vc_sb = 0;
315     		vcp->vc_seq = 0;
316     	}
317     	
318     	file->private_data = vcp;
319     
320     	CDEBUG(D_PSDEV, "device %i - inuse: %d\n", idx, vcp->vc_inuse);
321     
322     	EXIT;
323     	unlock_kernel();
324             return 0;
325     }
326     
327     
328     static int coda_psdev_release(struct inode * inode, struct file * file)
329     {
330             struct venus_comm *vcp = (struct venus_comm *) file->private_data;
331             struct upc_req *req;
332     	struct list_head *lh, *next;
333     	ENTRY;
334     
335     	lock_kernel();
336     	if ( !vcp->vc_inuse ) {
337     		unlock_kernel();
338     		printk("psdev_release: Not open.\n");
339     		return -1;
340     	}
341     
342     	CDEBUG(D_PSDEV, "psdev_release: inuse %d\n", vcp->vc_inuse);
343     	if (--vcp->vc_inuse) {
344     		unlock_kernel();
345     		return 0;
346     	}
347             
348             /* Wakeup clients so they can return. */
349     	CDEBUG(D_PSDEV, "wake up pending clients\n");
350     	lh = vcp->vc_pending.next;
351     	next = lh;
352     	while ( (lh = next) != &vcp->vc_pending) {
353     		next = lh->next;
354     		req = list_entry(lh, struct upc_req, uc_chain);
355     		/* Async requests need to be freed here */
356     		if (req->uc_flags & REQ_ASYNC) {
357     			CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
358     			upc_free(req);
359     			continue;
360     		}
361     		req->uc_flags |= REQ_ABORT;
362     		wake_up(&req->uc_sleep);
363             }
364             
365     	lh = &vcp->vc_processing;
366     	CDEBUG(D_PSDEV, "wake up processing clients\n");
367     	while ( (lh = lh->next) != &vcp->vc_processing) {
368     		req = list_entry(lh, struct upc_req, uc_chain);
369     		req->uc_flags |= REQ_ABORT;
370     	        wake_up(&req->uc_sleep);
371             }
372     	CDEBUG(D_PSDEV, "Done.\n");
373     
374     	EXIT;
375     	unlock_kernel();
376     	return 0;
377     }
378     
379     
380     static struct file_operations coda_psdev_fops = {
381     	owner:		THIS_MODULE,
382     	read:		coda_psdev_read,
383     	write:		coda_psdev_write,
384     	poll:		coda_psdev_poll,
385     	ioctl:		coda_psdev_ioctl,
386     	open:		coda_psdev_open,
387     	release:	coda_psdev_release,
388     };
389     
390     static devfs_handle_t devfs_handle;
391     
392     static int init_coda_psdev(void)
393     {
394     	if(devfs_register_chrdev(CODA_PSDEV_MAJOR,"coda_psdev",
395     				 &coda_psdev_fops)) {
396                   printk(KERN_ERR "coda_psdev: unable to get major %d\n", 
397     		     CODA_PSDEV_MAJOR);
398                   return -EIO;
399     	}
400     	devfs_handle = devfs_mk_dir (NULL, "coda", NULL);
401     	devfs_register_series (devfs_handle, "%u", MAX_CODADEVS, DEVFS_FL_NONE,
402     			       CODA_PSDEV_MAJOR, 0,
403     			       S_IFCHR | S_IRUSR | S_IWUSR,
404     			       &coda_psdev_fops, NULL);
405     
406     	coda_sysctl_init();
407     
408     	return 0;
409     }
410     
411     
412     MODULE_AUTHOR("Peter J. Braam <braam@cs.cmu.edu>");
413     
414     static int __init init_coda(void)
415     {
416     	int status;
417     	printk(KERN_INFO "Coda Kernel/Venus communications, v5.3.15, coda@cs.cmu.edu\n");
418     
419     	status = init_coda_psdev();
420     	if ( status ) {
421     		printk("Problem (%d) in init_coda_psdev\n", status);
422     		return status;
423     	}
424     	
425     	status = register_filesystem(&coda_fs_type);
426     	if (status) {
427     		printk("coda: failed to register filesystem!\n");
428     		devfs_unregister(devfs_handle);
429     		devfs_unregister_chrdev(CODA_PSDEV_MAJOR,"coda_psdev");
430     		coda_sysctl_clean();
431     	}
432     	return status;
433     }
434     
435     static void __exit exit_coda(void)
436     {
437             int err;
438     
439             ENTRY;
440     
441     	err = unregister_filesystem(&coda_fs_type);
442             if ( err != 0 ) {
443                     printk("coda: failed to unregister filesystem\n");
444             }
445     	devfs_unregister(devfs_handle);
446     	devfs_unregister_chrdev(CODA_PSDEV_MAJOR, "coda_psdev");
447     	coda_sysctl_clean();
448     }
449     
450     module_init(init_coda);
451     module_exit(exit_coda);
452     
453