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 "intermezzo_fs.h"
55 #include "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: %Zd, "
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 %Zd 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;
312 CDEBUG(D_PSDEV, "Psdev_open: caller: %d, flags: %d\n", current->pid, file->f_flags);
320 static int presto_psdev_release(struct inode * inode, struct file * file)
322 struct upc_channel *channel = (struct upc_channel *)file->private_data;
324 struct list_head *lh;
328 CERROR("%s: bad psdev file\n", __FUNCTION__);
332 CDEBUG(D_PSDEV, "Lento: pid %d\n", current->pid);
335 /* Wake up clients so they can return. */
336 CDEBUG(D_PSDEV, "Wake up clients sleeping for pending.\n");
337 spin_lock(&channel->uc_lock);
338 list_for_each(lh, &channel->uc_pending) {
339 req = list_entry(lh, struct upc_req, rq_chain);
341 /* Async requests stay around for a new lento */
342 if (req->rq_flags & REQ_ASYNC) {
345 /* the sleeper will free the req and data */
346 req->rq_flags |= REQ_DEAD;
347 wake_up(&req->rq_sleep);
350 CDEBUG(D_PSDEV, "Wake up clients sleeping for processing\n");
351 list_for_each(lh, &channel->uc_processing) {
352 req = list_entry(lh, struct upc_req, rq_chain);
353 /* freeing of req and data is done by the sleeper */
354 req->rq_flags |= REQ_DEAD;
355 wake_up(&req->rq_sleep);
357 spin_unlock(&channel->uc_lock);
358 CDEBUG(D_PSDEV, "Done.\n");
364 static struct file_operations presto_psdev_fops = {
365 .owner = THIS_MODULE,
366 .read = presto_psdev_read,
367 .write = presto_psdev_write,
368 .poll = presto_psdev_poll,
369 .open = presto_psdev_open,
370 .release = presto_psdev_release
374 static struct miscdevice intermezzo_psdev = {
380 int presto_psdev_init(void)
385 if ( (err = misc_register(&intermezzo_psdev)) ) {
386 CERROR("%s: cannot register %d err %d\n",
387 __FUNCTION__, INTERMEZZO_MINOR, err);
391 memset(&izo_channels, 0, sizeof(izo_channels));
392 for ( i = 0 ; i < MAX_CHANNEL ; i++ ) {
393 struct upc_channel *channel = &(izo_channels[i]);
394 INIT_LIST_HEAD(&channel->uc_pending);
395 INIT_LIST_HEAD(&channel->uc_processing);
396 INIT_LIST_HEAD(&channel->uc_cache_list);
397 init_waitqueue_head(&channel->uc_waitq);
398 channel->uc_lock = SPIN_LOCK_UNLOCKED;
399 channel->uc_hard = 0;
400 channel->uc_no_filter = 0;
401 channel->uc_no_journal = 0;
402 channel->uc_no_upcall = 0;
403 channel->uc_timeout = 30;
404 channel->uc_errorval = 0;
405 channel->uc_minor = i;
410 void presto_psdev_cleanup(void)
414 misc_deregister(&intermezzo_psdev);
416 for ( i = 0 ; i < MAX_CHANNEL ; i++ ) {
417 struct upc_channel *channel = &(izo_channels[i]);
418 struct list_head *lh, *next;
420 spin_lock(&channel->uc_lock);
421 if ( ! list_empty(&channel->uc_pending)) {
422 CERROR("Weird, tell Peter: module cleanup and pending list not empty dev %d\n", i);
424 if ( ! list_empty(&channel->uc_processing)) {
425 CERROR("Weird, tell Peter: module cleanup and processing list not empty dev %d\n", i);
427 if ( ! list_empty(&channel->uc_cache_list)) {
428 CERROR("Weird, tell Peter: module cleanup and cache listnot empty dev %d\n", i);
430 list_for_each_safe(lh, next, &channel->uc_pending) {
433 req = list_entry(lh, struct upc_req, rq_chain);
434 if ( req->rq_flags & REQ_ASYNC ) {
435 list_del(&(req->rq_chain));
436 CDEBUG(D_UPCALL, "free pending upcall type %d\n",
438 PRESTO_FREE(req->rq_data, req->rq_bufsize);
439 PRESTO_FREE(req, sizeof(struct upc_req));
441 req->rq_flags |= REQ_DEAD;
442 wake_up(&req->rq_sleep);
445 list_for_each(lh, &channel->uc_processing) {
447 req = list_entry(lh, struct upc_req, rq_chain);
448 list_del(&(req->rq_chain));
449 req->rq_flags |= REQ_DEAD;
450 wake_up(&req->rq_sleep);
452 spin_unlock(&channel->uc_lock);
457 * lento_upcall and lento_downcall routines
459 static inline unsigned long lento_waitfor_upcall
460 (struct upc_channel *channel, struct upc_req *req, int minor)
462 DECLARE_WAITQUEUE(wait, current);
463 unsigned long posttime;
465 req->rq_posttime = posttime = jiffies;
467 add_wait_queue(&req->rq_sleep, &wait);
469 if ( izo_channels[minor].uc_hard == 0 )
470 set_current_state(TASK_INTERRUPTIBLE);
472 set_current_state(TASK_UNINTERRUPTIBLE);
475 if ( req->rq_flags & (REQ_WRITE | REQ_DEAD) )
478 /* these cases only apply when TASK_INTERRUPTIBLE */
479 if ( !izo_channels[minor].uc_hard && signal_pending(current) ) {
480 /* if this process really wants to die, let it go */
481 if (sigismember(&(current->pending.signal), SIGKILL)||
482 sigismember(&(current->pending.signal), SIGINT) )
484 /* signal is present: after timeout always return
485 really smart idea, probably useless ... */
486 if ( time_after(jiffies, req->rq_posttime +
487 izo_channels[minor].uc_timeout * HZ) )
493 spin_lock(&channel->uc_lock);
494 list_del_init(&req->rq_chain);
495 spin_unlock(&channel->uc_lock);
496 remove_wait_queue(&req->rq_sleep, &wait);
497 set_current_state(TASK_RUNNING);
499 CDEBUG(D_SPECIAL, "posttime: %ld, returned: %ld\n",
500 posttime, jiffies-posttime);
501 return (jiffies - posttime);
505 * lento_upcall will return an error in the case of
506 * failed communication with Lento _or_ will peek at Lento
507 * reply and return Lento's error.
509 * As lento has 2 types of errors, normal errors (positive) and internal
510 * errors (negative), normal errors are negated, while internal errors
511 * are all mapped to -EINTR, while showing a nice warning message. (jh)
513 * lento_upcall will always free buffer, either directly, when an upcall
514 * is read (in presto_psdev_read), when the filesystem is unmounted, or
515 * when the module is unloaded.
517 int izo_upc_upcall(int minor, int *size, struct izo_upcall_hdr *buffer,
520 unsigned long runtime;
521 struct upc_channel *channel;
522 struct izo_upcall_resp *out;
527 channel = &(izo_channels[minor]);
529 if (channel->uc_no_upcall) {
533 if (!channel->uc_pid && !async) {
539 /* Format the request message. */
540 PRESTO_ALLOC(req, sizeof(struct upc_req));
546 req->rq_data = (void *)buffer;
548 req->rq_bufsize = *size;
549 req->rq_rep_size = 0;
550 req->rq_opcode = buffer->u_opc;
551 req->rq_unique = ++channel->uc_seq;
552 init_waitqueue_head(&req->rq_sleep);
554 /* Fill in the common input args. */
555 buffer->u_uniq = req->rq_unique;
556 buffer->u_async = async;
558 /* Remove potential datarace possibility*/
560 req->rq_flags = REQ_ASYNC;
562 spin_lock(&channel->uc_lock);
563 /* Append msg to pending queue and poke Lento. */
564 list_add(&req->rq_chain, channel->uc_pending.prev);
565 spin_unlock(&channel->uc_lock);
567 "Proc %d waking Lento %d for(opc,uniq) =(%d,%d) msg at %p.\n",
568 current->pid, channel->uc_pid, req->rq_opcode,
569 req->rq_unique, req);
570 wake_up_interruptible(&channel->uc_waitq);
573 /* req, rq_data are freed in presto_psdev_read for async */
574 /* req->rq_flags = REQ_ASYNC;*/
579 /* We can be interrupted while we wait for Lento to process
580 * our request. If the interrupt occurs before Lento has read
581 * the request, we dequeue and return. If it occurs after the
582 * read but before the reply, we dequeue, send a signal
583 * message, and return. If it occurs after the reply we ignore
584 * it. In no case do we want to restart the syscall. If it
585 * was interrupted by a lento shutdown (psdev_close), return
588 /* Go to sleep. Wake up on signals only after the timeout. */
589 runtime = lento_waitfor_upcall(channel, req, minor);
591 CDEBUG(D_TIMING, "opc: %d time: %ld uniq: %d size: %d\n",
592 req->rq_opcode, jiffies - req->rq_posttime,
593 req->rq_unique, req->rq_rep_size);
595 "..process %d woken up by Lento for req at 0x%p, data at %p\n",
596 current->pid, req, req->rq_data);
598 if (channel->uc_pid) { /* i.e. Lento is still alive */
599 /* Op went through, interrupt or not we go on */
600 if (req->rq_flags & REQ_WRITE) {
601 out = (struct izo_upcall_resp *)req->rq_data;
602 /* here we map positive Lento errors to kernel errors */
603 if ( out->result < 0 ) {
604 CERROR("Tell Peter: Lento returns negative error %d, for oc %d!\n",
605 out->result, out->opcode);
606 out->result = EINVAL;
608 error = -out->result;
609 CDEBUG(D_UPCALL, "upcall: (u,o,r) (%d, %d, %d) out at %p\n",
610 out->unique, out->opcode, out->result, out);
611 *size = req->rq_rep_size;
615 /* Interrupted before lento read it. */
616 if ( !(req->rq_flags & REQ_READ) && signal_pending(current)) {
618 "Interrupt before read: (op,un)=(%d,%d), flags %x\n",
619 req->rq_opcode, req->rq_unique, req->rq_flags);
620 /* perhaps the best way to convince the app to give up? */
626 /* interrupted after Lento did its read, send signal */
627 if ( (req->rq_flags & REQ_READ) && signal_pending(current) ) {
628 CDEBUG(D_UPCALL,"Interrupt after read: op = %d.%d, flags = %x\n",
629 req->rq_opcode, req->rq_unique, req->rq_flags);
633 CERROR("Lento: Strange interruption - tell Peter.\n");
636 } else { /* If lento died i.e. !UC_OPEN(channel) */
637 CERROR("lento_upcall: Lento dead on (op,un) (%d.%d) flags %d\n",
638 req->rq_opcode, req->rq_unique, req->rq_flags);
643 PRESTO_FREE(req, sizeof(struct upc_req));
645 PRESTO_FREE(buffer,*size);