FreeBSD/Linux Kernel Cross Reference
sys/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/system.h>
42 #include <asm/poll.h>
43 #include <asm/uaccess.h>
44
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>
50
51 #define upc_free(r) kfree(r)
52
53 /*
54 * Coda stuff
55 */
56 extern struct file_system_type coda_fs_type;
57
58 /* statistics */
59 int coda_hard; /* allows signals during upcalls */
60 unsigned long coda_timeout = 30; /* .. secs, then signals will dequeue */
61
62
63 struct venus_comm coda_comms[MAX_CODADEVS];
64 kmem_cache_t *cii_cache, *cred_cache, *upc_cache;
65
66 /*
67 * Device operations
68 */
69
70 static unsigned int coda_psdev_poll(struct file *file, poll_table * wait)
71 {
72 struct venus_comm *vcp = (struct venus_comm *) file->private_data;
73 unsigned int mask = POLLOUT | POLLWRNORM;
74
75 poll_wait(file, &vcp->vc_waitq, wait);
76 if (!list_empty(&vcp->vc_pending))
77 mask |= POLLIN | POLLRDNORM;
78
79 return mask;
80 }
81
82 static int coda_psdev_ioctl(struct inode * inode, struct file * filp,
83 unsigned int cmd, unsigned long arg)
84 {
85 unsigned int data;
86
87 switch(cmd) {
88 case CIOC_KERNEL_VERSION:
89 data = CODA_KERNEL_VERSION;
90 return put_user(data, (int *) arg);
91 default:
92 return -ENOTTY;
93 }
94
95 return 0;
96 }
97
98 /*
99 * Receive a message written by Venus to the psdev
100 */
101
102 static ssize_t coda_psdev_write(struct file *file, const char *buf,
103 size_t nbytes, loff_t *off)
104 {
105 struct venus_comm *vcp = (struct venus_comm *) file->private_data;
106 struct upc_req *req = NULL;
107 struct upc_req *tmp;
108 struct list_head *lh;
109 struct coda_in_hdr hdr;
110 ssize_t retval = 0, count = 0;
111 int error;
112
113 /* Peek at the opcode, uniquefier */
114 if (copy_from_user(&hdr, buf, 2 * sizeof(u_long)))
115 return -EFAULT;
116
117 CDEBUG(D_PSDEV, "(process,opc,uniq)=(%d,%ld,%ld), nbytes %ld\n",
118 current->pid, hdr.opcode, hdr.unique, (long)nbytes);
119
120 if (DOWNCALL(hdr.opcode)) {
121 struct super_block *sb = NULL;
122 union outputArgs *dcbuf;
123 int size = sizeof(*dcbuf);
124
125 sb = vcp->vc_sb;
126 if ( !sb ) {
127 CDEBUG(D_PSDEV, "coda_psdev_write: downcall, no SB!\n");
128 count = nbytes;
129 goto out;
130 }
131 CDEBUG(D_PSDEV, "handling downcall\n");
132
133 if ( nbytes < sizeof(struct coda_out_hdr) ) {
134 printk("coda_downcall opc %ld uniq %ld, not enough!\n",
135 hdr.opcode, hdr.unique);
136 count = nbytes;
137 goto out;
138 }
139 if ( nbytes > size ) {
140 printk("Coda: downcall opc %ld, uniq %ld, too much!",
141 hdr.opcode, hdr.unique);
142 nbytes = size;
143 }
144 CODA_ALLOC(dcbuf, union outputArgs *, nbytes);
145 if (copy_from_user(dcbuf, buf, nbytes)) {
146 CODA_FREE(dcbuf, nbytes);
147 retval = -EFAULT;
148 goto out;
149 }
150
151 /* what downcall errors does Venus handle ? */
152 lock_kernel();
153 error = coda_downcall(hdr.opcode, dcbuf, sb);
154 unlock_kernel();
155
156 CODA_FREE(dcbuf, nbytes);
157 if (error) {
158 printk("psdev_write: coda_downcall error: %d\n", error);
159 retval = error;
160 goto out;
161 }
162 count = nbytes;
163 goto out;
164 }
165
166 /* Look for the message on the processing queue. */
167 lock_kernel();
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) {
171 req = tmp;
172 list_del(&req->uc_chain);
173 break;
174 }
175 }
176 unlock_kernel();
177
178 if (!req) {
179 printk("psdev_write: msg (%ld, %ld) not found\n",
180 hdr.opcode, hdr.unique);
181 retval = -ESRCH;
182 goto out;
183 }
184
185 CDEBUG(D_PSDEV,"Eureka: uniq %ld on queue!\n", hdr.unique);
186
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! */
192 }
193 if (copy_from_user(req->uc_data, buf, nbytes)) {
194 req->uc_flags |= REQ_ABORT;
195 wake_up(&req->uc_sleep);
196 retval = -EFAULT;
197 goto out;
198 }
199
200 /* adjust outsize. is this useful ?? */
201 req->uc_outSize = nbytes;
202 req->uc_flags |= REQ_WRITE;
203 count = nbytes;
204
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);
210 }
211
212 CDEBUG(D_PSDEV,
213 "Found! Count %ld for (opc,uniq)=(%ld,%ld), upc_req at %p\n",
214 (long)count, hdr.opcode, hdr.unique, &req);
215
216 wake_up(&req->uc_sleep);
217 out:
218 return(count ? count : retval);
219 }
220
221 /*
222 * Read a message from the kernel to Venus
223 */
224
225 static ssize_t coda_psdev_read(struct file * file, char * buf,
226 size_t nbytes, loff_t *off)
227 {
228 DECLARE_WAITQUEUE(wait, current);
229 struct venus_comm *vcp = (struct venus_comm *) file->private_data;
230 struct upc_req *req;
231 ssize_t retval = 0, count = 0;
232
233 if (nbytes == 0)
234 return 0;
235
236 lock_kernel();
237
238 add_wait_queue(&vcp->vc_waitq, &wait);
239 set_current_state(TASK_INTERRUPTIBLE);
240
241 while (list_empty(&vcp->vc_pending)) {
242 if (file->f_flags & O_NONBLOCK) {
243 retval = -EAGAIN;
244 break;
245 }
246 if (signal_pending(current)) {
247 retval = -ERESTARTSYS;
248 break;
249 }
250 schedule();
251 }
252
253 set_current_state(TASK_RUNNING);
254 remove_wait_queue(&vcp->vc_waitq, &wait);
255
256 if (retval)
257 goto out;
258
259 req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
260 list_del(&req->uc_chain);
261
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);
267 count = nbytes;
268 }
269
270 if (copy_to_user(buf, req->uc_data, count))
271 retval = -EFAULT;
272
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);
277 goto out;
278 }
279
280 CDEBUG(D_PSDEV, "vcread: signal msg (%d, %d)\n",
281 req->uc_opcode, req->uc_unique);
282
283 CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
284 upc_free(req);
285 out:
286 unlock_kernel();
287 return (count ? count : retval);
288 }
289
290 static int coda_psdev_open(struct inode * inode, struct file * file)
291 {
292 struct venus_comm *vcp;
293 int idx;
294
295 lock_kernel();
296 idx = MINOR(inode->i_rdev);
297 if(idx >= MAX_CODADEVS) {
298 unlock_kernel();
299 return -ENODEV;
300 }
301
302 vcp = &coda_comms[idx];
303 if(vcp->vc_inuse) {
304 unlock_kernel();
305 return -EBUSY;
306 }
307
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);
312 vcp->vc_sb = 0;
313 vcp->vc_seq = 0;
314 }
315
316 file->private_data = vcp;
317
318 CDEBUG(D_PSDEV, "device %i - inuse: %d\n", idx, vcp->vc_inuse);
319
320 unlock_kernel();
321 return 0;
322 }
323
324
325 static int coda_psdev_release(struct inode * inode, struct file * file)
326 {
327 struct venus_comm *vcp = (struct venus_comm *) file->private_data;
328 struct upc_req *req;
329 struct list_head *lh, *next;
330
331 lock_kernel();
332 if ( !vcp->vc_inuse ) {
333 unlock_kernel();
334 printk("psdev_release: Not open.\n");
335 return -1;
336 }
337
338 CDEBUG(D_PSDEV, "psdev_release: inuse %d\n", vcp->vc_inuse);
339 if (--vcp->vc_inuse) {
340 unlock_kernel();
341 return 0;
342 }
343
344 /* Wakeup clients so they can return. */
345 CDEBUG(D_PSDEV, "wake up pending clients\n");
346 lh = vcp->vc_pending.next;
347 next = lh;
348 while ( (lh = next) != &vcp->vc_pending) {
349 next = lh->next;
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));
354 upc_free(req);
355 continue;
356 }
357 req->uc_flags |= REQ_ABORT;
358 wake_up(&req->uc_sleep);
359 }
360
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);
367 }
368 CDEBUG(D_PSDEV, "Done.\n");
369
370 unlock_kernel();
371 return 0;
372 }
373
374
375 static struct file_operations coda_psdev_fops = {
376 owner: THIS_MODULE,
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,
383 };
384
385 static devfs_handle_t devfs_handle;
386
387 static int init_coda_psdev(void)
388 {
389 if(devfs_register_chrdev(CODA_PSDEV_MAJOR,"coda_psdev",
390 &coda_psdev_fops)) {
391 printk(KERN_ERR "coda_psdev: unable to get major %d\n",
392 CODA_PSDEV_MAJOR);
393 return -EIO;
394 }
395 devfs_handle = devfs_mk_dir (NULL, "coda", NULL);
396 devfs_register_series (devfs_handle, "%u", MAX_CODADEVS, DEVFS_FL_NONE,
397 CODA_PSDEV_MAJOR, 0,
398 S_IFCHR | S_IRUSR | S_IWUSR,
399 &coda_psdev_fops, NULL);
400
401 coda_sysctl_init();
402
403 return 0;
404 }
405
406
407 MODULE_AUTHOR("Peter J. Braam <braam@cs.cmu.edu>");
408 MODULE_LICENSE("GPL");
409
410 static int __init init_coda(void)
411 {
412 int status;
413 printk(KERN_INFO "Coda Kernel/Venus communications, v5.3.18, coda@cs.cmu.edu\n");
414
415 status = init_coda_psdev();
416 if ( status ) {
417 printk("Problem (%d) in init_coda_psdev\n", status);
418 return status;
419 }
420
421 status = register_filesystem(&coda_fs_type);
422 if (status) {
423 printk("coda: failed to register filesystem!\n");
424 devfs_unregister(devfs_handle);
425 devfs_unregister_chrdev(CODA_PSDEV_MAJOR,"coda_psdev");
426 coda_sysctl_clean();
427 }
428 return status;
429 }
430
431 static void __exit exit_coda(void)
432 {
433 int err;
434
435 err = unregister_filesystem(&coda_fs_type);
436 if ( err != 0 ) {
437 printk("coda: failed to unregister filesystem\n");
438 }
439 devfs_unregister(devfs_handle);
440 devfs_unregister_chrdev(CODA_PSDEV_MAJOR, "coda_psdev");
441 coda_sysctl_clean();
442 }
443
444 module_init(init_coda);
445 module_exit(exit_coda);
446
Cache object: 784d41afb3a8f9c6a5ba6ceec4f9fff9
|