1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * An implementation of a loadable kernel mode driver providing
5 * multiple kernel/user space bidirectional communications links.
7 * Author: Alan Cox <alan@cymru.net>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * Adapted to become the Linux 2.0 Coda pseudo device
14 * Peter Braam <braam@maths.ox.ac.uk>
15 * Michael Callahan <mjc@emmy.smith.edu>
17 * Changes for Linux 2.1
18 * Copyright (c) 1997 Carnegie-Mellon University
20 * Redone again for InterMezzo
21 * Copyright (c) 1998 Peter J. Braam
22 * Copyright (c) 2000 Mountain View Data, Inc.
23 * Copyright (c) 2000 Tacitus Systems, Inc.
24 * Copyright (c) 2001 Cluster File Systems, Inc.
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/major.h>
32 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/ioport.h>
36 #include <linux/fcntl.h>
37 #include <linux/delay.h>
38 #include <linux/skbuff.h>
39 #include <linux/proc_fs.h>
40 #include <linux/vmalloc.h>
42 #include <linux/file.h>
43 #include <linux/poll.h>
44 #include <linux/init.h>
45 #include <linux/list.h>
46 #include <linux/devfs_fs_kernel.h>
48 #include <asm/segment.h>
49 #include <asm/system.h>
51 #include <asm/uaccess.h>
52 #include <linux/miscdevice.h>
54 #include <linux/intermezzo_fs.h>
55 #include <linux/intermezzo_psdev.h>
59 int presto_print_entry = 1;
60 int presto_debug = 4095;
62 int presto_print_entry = 0;
66 /* Like inode.c (presto_sym_iops), the initializer is just to prevent
67 izo_channels from appearing as a COMMON symbol (and therefore
68 interfering with other modules that use the same variable name). */
69 struct upc_channel izo_channels[MAX_CHANNEL] = {{0}};
71 int izo_psdev_get_free_channel(void)
75 for (i = 0 ; i < MAX_CHANNEL ; i++ ) {
76 if (list_empty(&(izo_channels[i].uc_cache_list))) {
85 int izo_psdev_setpid(int minor)
87 struct upc_channel *channel;
88 if (minor < 0 || minor >= MAX_CHANNEL) {
92 channel = &(izo_channels[minor]);
94 * This ioctl is performed by each Lento that starts up
95 * and wants to do further communication with presto.
97 CDEBUG(D_PSDEV, "Setting current pid to %d channel %d\n",
99 channel->uc_pid = current->pid;
100 spin_lock(&channel->uc_lock);
101 if ( !list_empty(&channel->uc_processing) ) {
102 struct list_head *lh;
104 CERROR("WARNING: setpid & processing not empty!\n");
105 list_for_each(lh, &channel->uc_processing) {
106 req = list_entry(lh, struct upc_req, rq_chain);
107 /* freeing of req and data is done by the sleeper */
108 wake_up(&req->rq_sleep);
111 if ( !list_empty(&channel->uc_processing) ) {
112 CERROR("BAD: FAILDED TO CLEAN PROCESSING LIST!\n");
114 spin_unlock(&channel->uc_lock);
119 int izo_psdev_setchannel(struct file *file, int fd)
122 struct file *psdev_file = fget(fd);
123 struct presto_cache *cache = presto_get_cache(file->f_dentry->d_inode);
126 CERROR("%s: no psdev_file!\n", __FUNCTION__);
131 CERROR("%s: no cache!\n", __FUNCTION__);
136 if (psdev_file->private_data) {
137 CERROR("%s: channel already set!\n", __FUNCTION__);
142 psdev_file->private_data = cache->cache_psdev;
148 inline int presto_lento_up(int minor)
150 return izo_channels[minor].uc_pid;
153 static unsigned int presto_psdev_poll(struct file *file, poll_table * wait)
155 struct upc_channel *channel = (struct upc_channel *)file->private_data;
156 unsigned int mask = POLLOUT | POLLWRNORM;
158 /* ENTRY; this will flood you */
160 CERROR("%s: bad psdev file\n", __FUNCTION__);
164 poll_wait(file, &(channel->uc_waitq), wait);
166 spin_lock(&channel->uc_lock);
167 if (!list_empty(&channel->uc_pending)) {
168 CDEBUG(D_PSDEV, "Non-empty pending list.\n");
169 mask |= POLLIN | POLLRDNORM;
171 spin_unlock(&channel->uc_lock);
173 /* EXIT; will flood you */
178 * Receive a message written by Lento to the psdev
180 static ssize_t presto_psdev_write(struct file *file, const char *buf,
181 size_t count, loff_t *off)
183 struct upc_channel *channel = (struct upc_channel *)file->private_data;
184 struct upc_req *req = NULL;
186 struct list_head *lh;
187 struct izo_upcall_resp hdr;
191 CERROR("%s: bad psdev file\n", __FUNCTION__);
195 /* Peek at the opcode, uniquefier */
196 if ( count < sizeof(hdr) ) {
197 CERROR("presto_psdev_write: Lento didn't write full hdr.\n");
201 error = copy_from_user(&hdr, buf, sizeof(hdr));
205 CDEBUG(D_PSDEV, "(process,opc,uniq)=(%d,%d,%d)\n",
206 current->pid, hdr.opcode, hdr.unique);
208 spin_lock(&channel->uc_lock);
209 /* Look for the message on the processing queue. */
210 list_for_each(lh, &channel->uc_processing) {
211 tmp = list_entry(lh, struct upc_req , rq_chain);
212 if (tmp->rq_unique == hdr.unique) {
214 /* unlink here: keeps search length minimal */
215 list_del_init(&req->rq_chain);
216 CDEBUG(D_PSDEV,"Eureka opc %d uniq %d!\n",
217 hdr.opcode, hdr.unique);
221 spin_unlock(&channel->uc_lock);
223 CERROR("psdev_write: msg (%d, %d) not found\n",
224 hdr.opcode, hdr.unique);
228 /* move data into response buffer. */
229 if (req->rq_bufsize < count) {
230 CERROR("psdev_write: too much cnt: %d, cnt: %d, "
231 "opc: %d, uniq: %d.\n",
232 req->rq_bufsize, count, hdr.opcode, hdr.unique);
233 count = req->rq_bufsize; /* don't have more space! */
235 error = copy_from_user(req->rq_data, buf, count);
239 /* adjust outsize: good upcalls can be aware of this */
240 req->rq_rep_size = count;
241 req->rq_flags |= REQ_WRITE;
243 wake_up(&req->rq_sleep);
248 * Read a message from the kernel to Lento
250 static ssize_t presto_psdev_read(struct file * file, char * buf,
251 size_t count, loff_t *off)
253 struct upc_channel *channel = (struct upc_channel *)file->private_data;
258 CERROR("%s: bad psdev file\n", __FUNCTION__);
262 spin_lock(&channel->uc_lock);
263 if (list_empty(&(channel->uc_pending))) {
264 CDEBUG(D_UPCALL, "Empty pending list in read, not good\n");
265 spin_unlock(&channel->uc_lock);
268 req = list_entry((channel->uc_pending.next), struct upc_req, rq_chain);
269 list_del(&(req->rq_chain));
270 if (! (req->rq_flags & REQ_ASYNC) ) {
271 list_add(&(req->rq_chain), channel->uc_processing.prev);
273 spin_unlock(&channel->uc_lock);
275 req->rq_flags |= REQ_READ;
277 /* Move the input args into userspace */
278 CDEBUG(D_PSDEV, "\n");
279 if (req->rq_bufsize <= count) {
280 result = req->rq_bufsize;
283 if (count < req->rq_bufsize) {
284 CERROR ("psdev_read: buffer too small, read %d of %d bytes\n",
285 count, req->rq_bufsize);
288 if ( copy_to_user(buf, req->rq_data, result) ) {
293 /* If request was asynchronous don't enqueue, but free */
294 if (req->rq_flags & REQ_ASYNC) {
295 CDEBUG(D_PSDEV, "psdev_read: async msg (%d, %d), result %d\n",
296 req->rq_opcode, req->rq_unique, result);
297 PRESTO_FREE(req->rq_data, req->rq_bufsize);
298 PRESTO_FREE(req, sizeof(*req));
306 static int presto_psdev_open(struct inode * inode, struct file * file)
310 file->private_data = NULL;
314 CDEBUG(D_PSDEV, "Psdev_open: caller: %d, flags: %d\n", current->pid, file->f_flags);
322 static int presto_psdev_release(struct inode * inode, struct file * file)
324 struct upc_channel *channel = (struct upc_channel *)file->private_data;
326 struct list_head *lh;
330 CERROR("%s: bad psdev file\n", __FUNCTION__);
335 CDEBUG(D_PSDEV, "Lento: pid %d\n", current->pid);
338 /* Wake up clients so they can return. */
339 CDEBUG(D_PSDEV, "Wake up clients sleeping for pending.\n");
340 spin_lock(&channel->uc_lock);
341 list_for_each(lh, &channel->uc_pending) {
342 req = list_entry(lh, struct upc_req, rq_chain);
344 /* Async requests stay around for a new lento */
345 if (req->rq_flags & REQ_ASYNC) {
348 /* the sleeper will free the req and data */
349 req->rq_flags |= REQ_DEAD;
350 wake_up(&req->rq_sleep);
353 CDEBUG(D_PSDEV, "Wake up clients sleeping for processing\n");
354 list_for_each(lh, &channel->uc_processing) {
355 req = list_entry(lh, struct upc_req, rq_chain);
356 /* freeing of req and data is done by the sleeper */
357 req->rq_flags |= REQ_DEAD;
358 wake_up(&req->rq_sleep);
360 spin_unlock(&channel->uc_lock);
361 CDEBUG(D_PSDEV, "Done.\n");
367 static struct file_operations presto_psdev_fops = {
368 .read = presto_psdev_read,
369 .write = presto_psdev_write,
370 .poll = presto_psdev_poll,
371 .open = presto_psdev_open,
372 .release = presto_psdev_release
376 static struct miscdevice intermezzo_psdev = {
382 int presto_psdev_init(void)
387 if ( (err = misc_register(&intermezzo_psdev)) ) {
388 CERROR("%s: cannot register %d err %d\n",
389 __FUNCTION__, INTERMEZZO_MINOR, err);
393 memset(&izo_channels, 0, sizeof(izo_channels));
394 for ( i = 0 ; i < MAX_CHANNEL ; i++ ) {
395 struct upc_channel *channel = &(izo_channels[i]);
396 INIT_LIST_HEAD(&channel->uc_pending);
397 INIT_LIST_HEAD(&channel->uc_processing);
398 INIT_LIST_HEAD(&channel->uc_cache_list);
399 init_waitqueue_head(&channel->uc_waitq);
400 channel->uc_lock = SPIN_LOCK_UNLOCKED;
401 channel->uc_hard = 0;
402 channel->uc_no_filter = 0;
403 channel->uc_no_journal = 0;
404 channel->uc_no_upcall = 0;
405 channel->uc_timeout = 30;
406 channel->uc_errorval = 0;
407 channel->uc_minor = i;
412 void presto_psdev_cleanup(void)
416 misc_deregister(&intermezzo_psdev);
418 for ( i = 0 ; i < MAX_CHANNEL ; i++ ) {
419 struct upc_channel *channel = &(izo_channels[i]);
420 struct list_head *lh, *next;
422 spin_lock(&channel->uc_lock);
423 if ( ! list_empty(&channel->uc_pending)) {
424 CERROR("Weird, tell Peter: module cleanup and pending list not empty dev %d\n", i);
426 if ( ! list_empty(&channel->uc_processing)) {
427 CERROR("Weird, tell Peter: module cleanup and processing list not empty dev %d\n", i);
429 if ( ! list_empty(&channel->uc_cache_list)) {
430 CERROR("Weird, tell Peter: module cleanup and cache listnot empty dev %d\n", i);
432 list_for_each_safe(lh, next, &channel->uc_pending) {
435 req = list_entry(lh, struct upc_req, rq_chain);
436 if ( req->rq_flags & REQ_ASYNC ) {
437 list_del(&(req->rq_chain));
438 CDEBUG(D_UPCALL, "free pending upcall type %d\n",
440 PRESTO_FREE(req->rq_data, req->rq_bufsize);
441 PRESTO_FREE(req, sizeof(struct upc_req));
443 req->rq_flags |= REQ_DEAD;
444 wake_up(&req->rq_sleep);
447 list_for_each(lh, &channel->uc_processing) {
449 req = list_entry(lh, struct upc_req, rq_chain);
450 list_del(&(req->rq_chain));
451 req->rq_flags |= REQ_DEAD;
452 wake_up(&req->rq_sleep);
454 spin_unlock(&channel->uc_lock);
459 * lento_upcall and lento_downcall routines
461 static inline unsigned long lento_waitfor_upcall
462 (struct upc_channel *channel, struct upc_req *req, int minor)
464 DECLARE_WAITQUEUE(wait, current);
465 unsigned long posttime;
467 req->rq_posttime = posttime = jiffies;
469 add_wait_queue(&req->rq_sleep, &wait);
471 if ( izo_channels[minor].uc_hard == 0 )
472 set_current_state(TASK_INTERRUPTIBLE);
474 set_current_state(TASK_UNINTERRUPTIBLE);
477 if ( req->rq_flags & (REQ_WRITE | REQ_DEAD) )
480 /* these cases only apply when TASK_INTERRUPTIBLE */
481 if ( !izo_channels[minor].uc_hard && signal_pending(current) ) {
482 /* if this process really wants to die, let it go */
483 if (sigismember(&(current->pending.signal), SIGKILL)||
484 sigismember(&(current->pending.signal), SIGINT) )
486 /* signal is present: after timeout always return
487 really smart idea, probably useless ... */
488 if ( time_after(jiffies, req->rq_posttime +
489 izo_channels[minor].uc_timeout * HZ) )
495 spin_lock(&channel->uc_lock);
496 list_del_init(&req->rq_chain);
497 spin_unlock(&channel->uc_lock);
498 remove_wait_queue(&req->rq_sleep, &wait);
499 set_current_state(TASK_RUNNING);
501 CDEBUG(D_SPECIAL, "posttime: %ld, returned: %ld\n",
502 posttime, jiffies-posttime);
503 return (jiffies - posttime);
507 * lento_upcall will return an error in the case of
508 * failed communication with Lento _or_ will peek at Lento
509 * reply and return Lento's error.
511 * As lento has 2 types of errors, normal errors (positive) and internal
512 * errors (negative), normal errors are negated, while internal errors
513 * are all mapped to -EINTR, while showing a nice warning message. (jh)
515 * lento_upcall will always free buffer, either directly, when an upcall
516 * is read (in presto_psdev_read), when the filesystem is unmounted, or
517 * when the module is unloaded.
519 int izo_upc_upcall(int minor, int *size, struct izo_upcall_hdr *buffer,
522 unsigned long runtime;
523 struct upc_channel *channel;
524 struct izo_upcall_resp *out;
529 channel = &(izo_channels[minor]);
531 if (channel->uc_no_upcall) {
535 if (!channel->uc_pid && !async) {
541 /* Format the request message. */
542 PRESTO_ALLOC(req, sizeof(struct upc_req));
548 req->rq_data = (void *)buffer;
550 req->rq_bufsize = *size;
551 req->rq_rep_size = 0;
552 req->rq_opcode = buffer->u_opc;
553 req->rq_unique = ++channel->uc_seq;
554 init_waitqueue_head(&req->rq_sleep);
556 /* Fill in the common input args. */
557 buffer->u_uniq = req->rq_unique;
558 buffer->u_async = async;
560 /* Remove potential datarace possibility*/
562 req->rq_flags = REQ_ASYNC;
564 spin_lock(&channel->uc_lock);
565 /* Append msg to pending queue and poke Lento. */
566 list_add(&req->rq_chain, channel->uc_pending.prev);
567 spin_unlock(&channel->uc_lock);
569 "Proc %d waking Lento %d for(opc,uniq) =(%d,%d) msg at %p.\n",
570 current->pid, channel->uc_pid, req->rq_opcode,
571 req->rq_unique, req);
572 wake_up_interruptible(&channel->uc_waitq);
575 /* req, rq_data are freed in presto_psdev_read for async */
576 /* req->rq_flags = REQ_ASYNC;*/
581 /* We can be interrupted while we wait for Lento to process
582 * our request. If the interrupt occurs before Lento has read
583 * the request, we dequeue and return. If it occurs after the
584 * read but before the reply, we dequeue, send a signal
585 * message, and return. If it occurs after the reply we ignore
586 * it. In no case do we want to restart the syscall. If it
587 * was interrupted by a lento shutdown (psdev_close), return
590 /* Go to sleep. Wake up on signals only after the timeout. */
591 runtime = lento_waitfor_upcall(channel, req, minor);
593 CDEBUG(D_TIMING, "opc: %d time: %ld uniq: %d size: %d\n",
594 req->rq_opcode, jiffies - req->rq_posttime,
595 req->rq_unique, req->rq_rep_size);
597 "..process %d woken up by Lento for req at 0x%x, data at %x\n",
598 current->pid, (int)req, (int)req->rq_data);
600 if (channel->uc_pid) { /* i.e. Lento is still alive */
601 /* Op went through, interrupt or not we go on */
602 if (req->rq_flags & REQ_WRITE) {
603 out = (struct izo_upcall_resp *)req->rq_data;
604 /* here we map positive Lento errors to kernel errors */
605 if ( out->result < 0 ) {
606 CERROR("Tell Peter: Lento returns negative error %d, for oc %d!\n",
607 out->result, out->opcode);
608 out->result = EINVAL;
610 error = -out->result;
611 CDEBUG(D_UPCALL, "upcall: (u,o,r) (%d, %d, %d) out at %p\n",
612 out->unique, out->opcode, out->result, out);
613 *size = req->rq_rep_size;
617 /* Interrupted before lento read it. */
618 if ( !(req->rq_flags & REQ_READ) && signal_pending(current)) {
620 "Interrupt before read: (op,un)=(%d,%d), flags %x\n",
621 req->rq_opcode, req->rq_unique, req->rq_flags);
622 /* perhaps the best way to convince the app to give up? */
628 /* interrupted after Lento did its read, send signal */
629 if ( (req->rq_flags & REQ_READ) && signal_pending(current) ) {
630 CDEBUG(D_UPCALL,"Interrupt after read: op = %d.%d, flags = %x\n",
631 req->rq_opcode, req->rq_unique, req->rq_flags);
635 CERROR("Lento: Strange interruption - tell Peter.\n");
638 } else { /* If lento died i.e. !UC_OPEN(channel) */
639 CERROR("lento_upcall: Lento dead on (op,un) (%d.%d) flags %d\n",
640 req->rq_opcode, req->rq_unique, req->rq_flags);
645 PRESTO_FREE(req, sizeof(struct upc_req));
647 PRESTO_FREE(buffer,*size);